1 /******************************************************************************
3 @file util.c
5 @brief Utility functions commonly used by TIMAC applications
7 Group: WCS LPC
8 $Target Devices: Linux: AM335x, Embedded Devices: CC1310, CC1350, CC1352$
10 ******************************************************************************
11 $License: BSD3 2016 $
13 Copyright (c) 2015, Texas Instruments Incorporated
14 All rights reserved.
16 Redistribution and use in source and binary forms, with or without
17 modification, are permitted provided that the following conditions
18 are met:
20 * Redistributions of source code must retain the above copyright
21 notice, this list of conditions and the following disclaimer.
23 * Redistributions in binary form must reproduce the above copyright
24 notice, this list of conditions and the following disclaimer in the
25 documentation and/or other materials provided with the distribution.
27 * Neither the name of Texas Instruments Incorporated nor the names of
28 its contributors may be used to endorse or promote products derived
29 from this software without specific prior written permission.
31 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
32 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
33 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
34 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
36 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
37 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
38 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
39 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
40 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
41 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 ******************************************************************************
43 $Release Name: TI-15.4Stack Linux x64 SDK$
44 $Release Date: Sept 27, 2017 (2.04.00.13)$
45 *****************************************************************************/
47 #define UTIL_SADDR_EXT_LEN 8
49 /******************************************************************************
50 Includes
51 *****************************************************************************/
53 #include "compiler.h"
54 #include <string.h>
55 #include <stdint.h>
56 #include "hlos_specific.h"
57 #include "util.h"
60 /*!
61 Get the high byte of a uint16_t variable
63 Public function defined in util.h
64 */
65 uint8_t Util_hiUint16(uint16_t a)
66 {
67 return((a >> 8) & 0xFF);
68 }
70 /*!
71 Get the low byte of a uint16_t variable
73 Public function defined in util.h
74 */
75 uint8_t Util_loUint16(uint16_t a)
76 {
77 return((a) & 0xFF);
78 }
80 /*!
81 Build a uint16_t out of 2 uint8_t variables
83 Public function defined in util.h
84 */
85 uint16_t Util_buildUint16(uint8_t loByte, uint8_t hiByte)
86 {
87 return((uint16_t)(((loByte) & 0x00FF) + (((hiByte) & 0x00FF) << 8)));
88 }
90 /*!
91 Build a uint32_t out of 4 uint8_t variables
93 Public function defined in util.h
94 */
95 uint32_t Util_buildUint32(uint8_t byte0, uint8_t byte1, uint8_t byte2,
96 uint8_t byte3)
97 {
98 return((uint32_t)((uint32_t)((byte0) & 0x00FF) +
99 ((uint32_t)((byte1) & 0x00FF) << 8) +
100 ((uint32_t)((byte2) & 0x00FF) << 16) +
101 ((uint32_t)((byte3) & 0x00FF) << 24)));
102 }
104 /*!
105 Pull 1 uint8_t out of a uint32_t
107 Public function defined in util.h
108 */
109 uint8_t Util_breakUint32(uint32_t var, int byteNum)
110 {
111 return(uint8_t)((uint32_t)(((var) >> ((byteNum) * 8)) & 0x00FF));
112 }
114 /*!
115 Build a uint16_t from a uint8_t array
117 Public function defined in util.h
118 */
119 uint16_t Util_parseUint16(uint8_t *pArray)
120 {
121 return(Util_buildUint16(pArray[0], pArray[1]));
122 }
124 /*!
125 Build a uint32_t from a uint8_t array
127 Public function defined in util.h
128 */
129 uint32_t Util_parseUint32(uint8_t *pArray)
130 {
131 return(Util_buildUint32(pArray[0], pArray[1], pArray[2], pArray[3]));
132 }
134 /*!
135 Break and buffer a uint16_t value - LSB first
137 Public function defined in util.h
138 */
139 uint8_t *Util_bufferUint16(uint8_t *pBuf, uint16_t val)
140 {
141 *pBuf++ = Util_loUint16(val);
142 *pBuf++ = Util_hiUint16(val);
144 return(pBuf);
145 }
147 /*!
148 Break and buffer a uint32_t value - LSB first
150 Public function defined in util.h
151 */
152 uint8_t *Util_bufferUint32(uint8_t *pBuf, uint32_t val)
153 {
154 *pBuf++ = Util_breakUint32(val, 0);
155 *pBuf++ = Util_breakUint32(val, 1);
156 *pBuf++ = Util_breakUint32(val, 2);
157 *pBuf++ = Util_breakUint32(val, 3);
159 return(pBuf);
160 }
162 /*!
163 Utility function to clear an event
165 Public function defined in util.h
166 */
167 void Util_clearEvent(uint16_t *pEvent, uint16_t event)
168 {
169 _ATOMIC_global_lock();
170 /* Clear the event */
171 *pEvent &= ~(event);
173 _ATOMIC_global_unlock();
174 }
176 /*!
177 Utility function to set an event
179 Public function defined in util.h
180 */
181 void Util_setEvent(uint16_t *pEvent, uint16_t event)
182 {
183 _ATOMIC_global_lock();
185 /* Set the event */
186 *pEvent |= event;
188 _ATOMIC_global_unlock();
189 }
191 /*!
192 Utility function to copy the extended address
194 Public function defined in util.h
195 */
196 void Util_copyExtAddr(void *pSrcAddr, void *pDstAddr)
197 {
198 memcpy(pSrcAddr, pDstAddr, (UTIL_SADDR_EXT_LEN));
199 }
201 /*
202 * ========================================
203 * Texas Instruments Micro Controller Style
204 * ========================================
205 * Local Variables:
206 * mode: c
207 * c-file-style: "bsd"
208 * tab-width: 4
209 * c-basic-offset: 4
210 * indent-tabs-mode: nil
211 * End:
212 * vim:set filetype=c tabstop=4 shiftwidth=4 expandtab=true
213 */