1 /******************************************************************************
3 @file util.c
5 @brief Utility functions commonly used by TIMAC applications
7 Group: WCS LPC
8 Target Device: CC13xx
10 ******************************************************************************
12 Copyright (c) 2016-2017, Texas Instruments Incorporated
13 All rights reserved.
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions
17 are met:
19 * Redistributions of source code must retain the above copyright
20 notice, this list of conditions and the following disclaimer.
22 * Redistributions in binary form must reproduce the above copyright
23 notice, this list of conditions and the following disclaimer in the
24 documentation and/or other materials provided with the distribution.
26 * Neither the name of Texas Instruments Incorporated nor the names of
27 its contributors may be used to endorse or promote products derived
28 from this software without specific prior written permission.
30 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
31 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
32 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
35 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
36 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
37 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
38 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
39 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
40 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 ******************************************************************************
43 Release Name: simplelink_cc13x0_sdk_1_30_00_06"
44 Release Date: 2017-03-08 14:43:30
45 *****************************************************************************/
47 /******************************************************************************
48 Includes
49 *****************************************************************************/
51 #include <string.h>
52 #include <stdint.h>
53 #include <stdbool.h>
55 #include "icall.h"
56 #include "util.h"
57 #include "api_mac.h"
59 /******************************************************************************
60 Typedefs
61 *****************************************************************************/
62 #define UTIL_SADDR_EXT_LEN 8
64 /******************************************************************************
65 Public Functions
66 *****************************************************************************/
68 /*!
69 Converts from a uint16 to ascii hex string.
71 Public function defined in util.h
72 */
73 void Util_uint16toa(uint16_t u, char *string)
74 {
75 if(string == NULL)
76 {
77 return;
78 }
80 /* add preceding zeros */
81 if(u < 0x1000)
82 {
83 *string++ = '0';
84 }
86 if(u < 0x0100)
87 {
88 *string++ = '0';
89 }
91 if(u < 0x0010)
92 {
93 *string++ = '0';
94 }
96 Util_ltoa( (unsigned long)u, (unsigned char *)string, 16 );
97 }
99 /*!
100 Convert a 16bit number to ASCII
102 Public function defined in util.h
103 */
104 void Util_itoa(uint16_t num, uint8_t *buf, uint8_t radix)
105 {
106 char c, i;
107 uint8_t *p, rst[5];
109 p = rst;
110 for(i = 0; i < 5; i++, p++)
111 {
112 c = num % radix; /* Isolate a digit */
113 *p = c + ( (c < 10) ? '0' : '7' ); /* Convert to Ascii */
114 num /= radix;
115 if(!num)
116 {
117 break;
118 }
119 }
121 for(c = 0; c <= i; c++)
122 {
123 *buf++ = *p--; /* Reverse character order */
124 }
126 *buf = '\0';
127 }
129 /*!
130 Convert a long unsigned int to a string.
132 Public function defined in util.h
133 */
134 unsigned char *Util_ltoa(uint32_t l, uint8_t *buf, uint8_t radix)
135 {
136 #if defined (__GNUC__)
137 return( (char *)ltoa(l, buf, radix) );
138 #else
139 unsigned char tmp1[10] = "", tmp2[10] = "", tmp3[10] = "";
140 unsigned short num1, num2, num3;
141 unsigned char i;
143 buf[0] = '\0';
145 if(radix == 10)
146 {
147 num1 = l % 10000;
148 num2 = (l / 10000) % 10000;
149 num3 = (unsigned short)(l / 100000000);
151 if(num3)
152 {
153 Util_itoa(num3, tmp3, 10);
154 }
155 if(num2)
156 {
157 Util_itoa(num2, tmp2, 10);
158 }
159 if(num1)
160 {
161 Util_itoa(num1, tmp1, 10);
162 }
164 if(num3)
165 {
166 strcpy( (char *)buf, (char const *)tmp3 );
167 for(i = 0; i < 4 - strlen( (char const *)tmp2 ); i++)
168 {
169 strcat( (char *)buf, "0" );
170 }
171 }
172 strcat( (char *)buf, (char const *)tmp2 );
173 if(num3 || num2)
174 {
175 for(i = 0; i < 4 - strlen( (char const *)tmp1 ); i++)
176 {
177 strcat( (char *)buf, "0" );
178 }
179 }
180 strcat( (char *)buf, (char const *)tmp1 );
181 if(!num3 && !num2 && !num1)
182 {
183 strcpy( (char *)buf, "0" );
184 }
185 }
186 else if(radix == 16)
187 {
188 num1 = l & 0x0000FFFF;
189 num2 = l >> 16;
191 if(num2)
192 {
193 Util_itoa(num2, tmp2, 16);
194 }
195 if(num1)
196 {
197 Util_itoa(num1, tmp1, 16);
198 }
200 if(num2)
201 {
202 strcpy( (char *)buf, (char const *)tmp2 );
203 for(i = 0; i < 4 - strlen( (char const *)tmp1 ); i++)
204 {
205 strcat( (char *)buf, "0" );
206 }
207 }
208 strcat( (char *)buf, (char const *)tmp1 );
209 if(!num2 && !num1)
210 {
211 strcpy( (char *)buf, "0" );
212 }
213 }
214 else
215 {
216 return(NULL);
217 }
219 return(buf);
220 #endif
221 }
223 /*!
224 Get the high byte of a uint16_t variable
226 Public function defined in util.h
227 */
228 uint8_t Util_hiUint16(uint16_t a)
229 {
230 return((a >> 8) & 0xFF);
231 }
233 /*!
234 Get the low byte of a uint16_t variable
236 Public function defined in util.h
237 */
238 uint8_t Util_loUint16(uint16_t a)
239 {
240 return((a) & 0xFF);
241 }
243 /*!
244 Build a uint16_t out of 2 uint8_t variables
246 Public function defined in util.h
247 */
248 uint16_t Util_buildUint16(uint8_t loByte, uint8_t hiByte)
249 {
250 return((uint16_t)(((loByte) & 0x00FF) + (((hiByte) & 0x00FF) << 8)));
251 }
253 /*!
254 Build a uint32_t out of 4 uint8_t variables
256 Public function defined in util.h
257 */
258 uint32_t Util_buildUint32(uint8_t byte0, uint8_t byte1, uint8_t byte2,
259 uint8_t byte3)
260 {
261 return((uint32_t)((uint32_t)((byte0) & 0x00FF) +
262 ((uint32_t)((byte1) & 0x00FF) << 8) +
263 ((uint32_t)((byte2) & 0x00FF) << 16) +
264 ((uint32_t)((byte3) & 0x00FF) << 24)));
265 }
267 /*!
268 Pull 1 uint8_t out of a uint32_t
270 Public function defined in util.h
271 */
272 uint8_t Util_breakUint32(uint32_t var, int byteNum)
273 {
274 return(uint8_t)((uint32_t)(((var) >> ((byteNum) * 8)) & 0x00FF));
275 }
277 /*!
278 Build a uint16_t from a uint8_t array
280 Public function defined in util.h
281 */
282 uint16_t Util_parseUint16(uint8_t *pArray)
283 {
284 return(Util_buildUint16(pArray[0], pArray[1]));
285 }
287 /*!
288 Build a uint32_t from a uint8_t array
290 Public function defined in util.h
291 */
292 uint32_t Util_parseUint32(uint8_t *pArray)
293 {
294 return(Util_buildUint32(pArray[0], pArray[1], pArray[2], pArray[3]));
295 }
297 /*!
298 Break and buffer a uint16_t value - LSB first
300 Public function defined in util.h
301 */
302 uint8_t *Util_bufferUint16(uint8_t *pBuf, uint16_t val)
303 {
304 *pBuf++ = Util_loUint16(val);
305 *pBuf++ = Util_hiUint16(val);
307 return(pBuf);
308 }
310 /*!
311 Break and buffer a uint32_t value - LSB first
313 Public function defined in util.h
314 */
315 uint8_t *Util_bufferUint32(uint8_t *pBuf, uint32_t val)
316 {
317 *pBuf++ = Util_breakUint32(val, 0);
318 *pBuf++ = Util_breakUint32(val, 1);
319 *pBuf++ = Util_breakUint32(val, 2);
320 *pBuf++ = Util_breakUint32(val, 3);
322 return(pBuf);
323 }
325 /*!
326 Utility function to clear an event
328 Public function defined in util.h
329 */
330 void Util_clearEvent(uint16_t *pEvent, uint16_t event)
331 {
332 ICall_CSState state;
334 /* Enter critical section */
335 state = ICall_enterCriticalSection();
337 /* Clear the event */
338 *pEvent &= ~(event);
340 /* Exit critical section */
341 ICall_leaveCriticalSection(state);
342 }
344 /*!
345 Utility function to set an event
347 Public function defined in util.h
348 */
349 void Util_setEvent(uint16_t *pEvent, uint16_t event)
350 {
351 ICall_CSState state;
353 /* Enter critical section */
354 state = ICall_enterCriticalSection();
356 /* Set the event */
357 *pEvent |= event;
359 /* Exit critical section */
360 ICall_leaveCriticalSection(state);
361 }
363 /*!
364 Utility function to copy the extended address
366 Public function defined in util.h
367 */
368 void Util_copyExtAddr(void *pSrcAddr, void *pDstAddr)
369 {
370 memcpy(pSrcAddr, pDstAddr, (UTIL_SADDR_EXT_LEN));
371 }