TI BLE5-Stack API Documentation  9.12.00
comdef.h
Go to the documentation of this file.
1 /******************************************************************************
2 
3  @file comdef.h
4 
5  @brief This file contains useful macros and data types
6 
7  Group: WCS, LPC, BTS
8  $Target Device: DEVICES $
9 
10  ******************************************************************************
11  $License: BSD3 2004 $
12  ******************************************************************************
13  $Release Name: PACKAGE NAME $
14  $Release Date: PACKAGE RELEASE DATE $
15  *****************************************************************************/
16 
22 #ifndef COMDEF_H
23 #define COMDEF_H
24 
25 #ifdef __cplusplus
26 extern "C"
27 {
28 #endif
29 
30 
31 /*********************************************************************
32  * INCLUDES
33  */
34 
35 #include <stdint.h>
36 #include <stdbool.h>
37 
39 
40 /*********************************************************************
41  * Lint Keywords
42  */
43 #define VOID (void)
44 
45 /*********************************************************************
46  * CONSTANTS
47  */
48 
49 #ifndef false
50  #define false 0
51 #endif
52 
53 #ifndef true
54  #define true 1
55 #endif
56 
57 #ifndef GENERIC
58  #define GENERIC
59 #endif
60 
61 #ifndef TRUE
62 #define TRUE 1
63 #endif
64 
65 #ifndef UTRUE
66 #define UTRUE 1U
67 #endif
68 
69 #ifndef FALSE
70 #define FALSE 0
71 #endif
72 
73 #ifndef UFALSE
74 #define UFALSE 0U
75 #endif
76 
77 #ifndef NULL
78 #define NULL 0L
79 #endif
80 
82 
83 /*** Generic Status Return Values ***/
84 #define SUCCESS 0x00
85 #define USUCCESS 0U
86 #define FAILURE 0x01
87 #define UFAILURE 1U
88 #define INVALIDPARAMETER 0x02
89 #define UINVALIDPARAMETER 2U
90 #define INVALID_TASK 0x03
91 #define MSG_BUFFER_NOT_AVAIL 0x04
92 #define INVALID_MSG_POINTER 0x05
93 #define INVALID_EVENT_ID 0x06
94 #define INVALID_INTERRUPT_ID 0x07
95 #define NO_TIMER_AVAIL 0x08
96 #define NV_ITEM_UNINIT 0x09
97 #define NV_OPER_FAILED 0x0A
98 #define INVALID_MEM_SIZE 0x0B
99 #define NV_BAD_ITEM_LEN 0x0C
100 
101 /*********************************************************************
102  * TYPEDEFS
103  */
104 
106 
107 // Generic Status return
108 typedef uint8_t Status_t;
109 
110 // Data types
111 typedef signed char int8;
112 typedef unsigned char uint8;
113 
114 typedef signed short int16;
115 typedef unsigned short uint16;
116 
117 typedef signed long int32;
118 typedef unsigned long uint32;
119 
120 typedef uint32_t halDataAlign_t;
121 
123 
124 /*********************************************************************
125  * MACROS
126  */
127 
128 #ifndef BV
129 #define BV(n) (1 << (n))
130 #endif
131 
132 #ifndef BF
133 #define BF(x,b,s) (((x) & (b)) >> (s))
134 #endif
135 
136 #ifndef ABS
137 #define ABS(n) (((n) < 0) ? -(n) : (n))
138 #endif
139 
140 // Break a uint32_t variable into a specific byte
141 #define BREAK_UINT32(var, ByteNum) \
142  ((((var) >> ((ByteNum) * 8)) & 0x00FF))
143 
144 // Build a uint32_t from four bytes
145 #define BUILD_UINT32(Byte0, Byte1, Byte2, Byte3) ( (((Byte3) & 0xFF) << 24) | \
146  (((Byte2) & 0xFF) << 16) | \
147  (((Byte1) & 0xFF) << 8) | \
148  ((Byte0) & 0xFF) )
149 
150 // Build a uint16_t from two bytes
151 #define BUILD_UINT16(loByte, hiByte) ( ((loByte) & 0xFF) | (((hiByte) & 0xFF) << 8) )
152 
153 // Get the high byte of a uint16_t
154 #define HI_UINT16(a) (((a) >> 8) & 0xFF)
155 
156 // Get the low byte of a uint16_t
157 #define LO_UINT16(a) ((a) & 0xFF)
158 
159 // Get a uint16_t from a pointer to a byte array
160 #define GET_UINT16(a) ((uint16_t)((*((uint8_t *)a)) + (((uint16_t)(*(((uint8_t *)a) + 1))) << 8)))
161 
162 // Build a uint8_t from two nibbles
163 #define BUILD_UINT8(hiByte, loByte) \
164  (((loByte) & 0x0F) + (((hiByte) & 0x0F) << 4))
165 
166 // Get the high nibble of a uint8_t
167 #define HI_UINT8(a) (((a) >> 4) & 0x0F)
168 
169 // Get the low nibble of a uint8_t
170 #define LO_UINT8(a) ((a) & 0x0F)
171 
172 // Write the 32bit value of 'val' in little endian format to the buffer pointed
173 // to by pBuf, and increment pBuf by 4
174 #define UINT32_TO_BUF_LITTLE_ENDIAN(pBuf, val) \
175  do { \
176  *(pBuf)++ = ((val >> 0) & 0xFF); \
177  *(pBuf)++ = ((val >> 8) & 0xFF); \
178  *(pBuf)++ = ((val >> 16) & 0xFF); \
179  *(pBuf)++ = ((val >> 24) & 0xFF); \
180  } while (0)
181 
182 // Return the 32bit little-endian formatted value pointed to by pBuf, and increment pBuf by 4
183 #define BUF_TO_UINT32_LITTLE_ENDIAN(pBuf) (((pBuf) += 4), BUILD_UINT32((pBuf)[-1], (pBuf)[-2], (pBuf)[-3], (pBuf)[-4]))
184 
185 #ifndef GET_BIT
186 // Macro to get the value of a specific bit in a byte array
187 #define GET_BIT(DISCS, IDX) (((DISCS)[((IDX) / 8)] & BV((IDX) % 8)) ? TRUE : FALSE)
188 #endif
189 
190 #ifndef SET_BIT
191 // Macro to set a specific bit in a byte array
192 #define SET_BIT(DISCS, IDX) (((DISCS)[((IDX) / 8)] |= BV((IDX) % 8)))
193 #endif
194 
195 #ifndef CLR_BIT
196 // Macro to clear a specific bit in a byte array
197 #define CLR_BIT(DISCS, IDX) (((DISCS)[((IDX) / 8)] &= (BV((IDX) % 8) ^ 0xFF)))
198 #endif
199 
200 /*
201  * This macro is for use by other macros to form a fully valid C statement.
202  * Without this, the if/else conditionals could show unexpected behavior.
203  *
204  * For example, use...
205  * #define SET_REGS() st( ioreg1 = 0; ioreg2 = 0; )
206  * instead of ...
207  * #define SET_REGS() { ioreg1 = 0; ioreg2 = 0; }
208  * or
209  * #define SET_REGS() ioreg1 = 0; ioreg2 = 0;
210  * The last macro would not behave as expected in the if/else construct.
211  * The second to last macro will cause a compiler error in certain uses
212  * of if/else construct
213  *
214  * It is not necessary, or recommended, to use this macro where there is
215  * already a valid C statement. For example, the following is redundant...
216  * #define CALL_FUNC() st( func(); )
217  * This should simply be...
218  * #define CALL_FUNC() func()
219  *
220  * (The while condition below evaluates false without generating a
221  * constant-controlling-loop type of warning on most compilers.)
222  */
223 #define st(x) do { x } while (__LINE__ == -1)
224 
225 /*
226  * The offsetof macro is a standard macro defined in the C standard library header <stddef.h>
227  * It is used to determine the byte offset of a member within a structure type.
228  */
229 #ifndef offsetof
230 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
231 #endif
232 /*********************************************************************
233  * MEMORY ATTRIBUTES
234  */
235 
236 #if defined (__IAR_SYSTEMS_ICC__)
237 #define XDATA
238 #define CODE
239 #define DATA_ALIGN(x) _Pragma data_alignment=(x)
240 #define ALIGNED
241 #define PACKED __packed
242 #define PACKED_STRUCT PACKED struct
243 #define PACKED_TYPEDEF_STRUCT PACKED typedef struct
244 #define PACKED_TYPEDEF_CONST_STRUCT PACKED typedef const struct
245 #define PACKED_TYPEDEF_UNION PACKED typedef union
246 #define PACKED_ALIGNED PACKED
247 #define PACKED_ALIGNED_TYPEDEF_STRUCT PACKED_TYPEDEF_STRUCT
248 
249 #elif defined __TI_COMPILER_VERSION || defined __TI_COMPILER_VERSION__
250 #define XDATA
251 #define CODE
252 #define DATA
253 #define NEARFUNC
254 #define ALIGNED
255 #define PACKED __attribute__((packed))
256 #define PACKED_STRUCT struct PACKED
257 #define PACKED_TYPEDEF_STRUCT typedef struct PACKED
258 #define PACKED_TYPEDEF_CONST_STRUCT typedef const struct PACKED
259 #define PACKED_TYPEDEF_UNION typedef union PACKED
260 #define PACKED_ALIGNED __attribute__((packed,aligned(4)))
261 #define PACKED_ALIGNED_TYPEDEF_STRUCT typedef struct PACKED_ALIGNED
262 
263 #elif defined (__GNUC__)
264 #define ALIGNED __attribute__((aligned(4)))
265 #ifdef CC33xx
266 #define PACKED __attribute__((aligned(1))) __attribute__((packed))
267 #else
268 #define PACKED __attribute__((__packed__))
269 #endif
270 #define PACKED_STRUCT struct PACKED
271 #define PACKED_TYPEDEF_STRUCT typedef struct PACKED
272 #define PACKED_TYPEDEF_CONST_STRUCT typedef const struct PACKED
273 #define PACKED_TYPEDEF_UNION typedef union PACKED
274 #define PACKED_ALIGNED __attribute__((packed,aligned(4)))
275 #define PACKED_ALIGNED_TYPEDEF_STRUCT typedef struct PACKED_ALIGNED
276 #endif
277 
278 /*********************************************************************
279  * COMPILER MACROS
280  */
281 
282 /* ----------- IAR Compiler ----------- */
283 #ifdef __IAR_SYSTEMS_ICC__
284 #define ASM_NOP asm("NOP")
285 #define NO_INIT __no_init
286 #define WEAK_FUNC __weak
287 
288 /* ----------- CCS Compiler ----------- */
289 #elif defined __TI_COMPILER_VERSION || defined __TI_COMPILER_VERSION__
290 #define ASM_NOP asm(" NOP")
291 #define NO_INIT __attribute__((noinit))
292 
293 /* ----------- GNU Compiler ----------- */
294 #elif defined __GNUC__
295 #define ASM_NOP __asm__ __volatile__ ("nop")
296 #define WEAK_FUNC __attribute__((__weak__))
297 
298 /* ----------- Unrecognized Compiler ----------- */
299 #else
300 #error "ERROR: Unknown compiler."
301 #endif
302 
303 /*********************************************************************
304  * GLOBAL VARIABLES
305  */
306 
307 /*********************************************************************
308  * FUNCTIONS
309  */
310 
311 /*********************************************************************
312 *********************************************************************/
313 
314 #ifdef __cplusplus
315 }
316 #endif
317 
318 #endif /* COMDEF_H */
© Copyright 1995-2025, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale