AM243x MCU+ SDK  09.00.00
unibase_macros.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023 Texas Instruments Incorporated
3  * Copyright (c) 2023 Excelfore Corporation (https://excelfore.com)
4  *
5  * All rights reserved not granted herein.
6  * Limited License.
7  *
8  * Texas Instruments Incorporated grants a world-wide, royalty-free,
9  * non-exclusive license under copyrights and patents it now or hereafter
10  * owns or controls to make, have made, use, import, offer to sell and sell ("Utilize")
11  * this software subject to the terms herein. With respect to the foregoing patent
12  * license, such license is granted solely to the extent that any such patent is necessary
13  * to Utilize the software alone. The patent license shall not apply to any combinations which
14  * include this software, other than combinations with devices manufactured by or for TI ("TI Devices").
15  * No hardware patent is licensed hereunder.
16  *
17  * Redistributions must preserve existing copyright notices and reproduce this license (including the
18  * above copyright notice and the disclaimer and (if applicable) source code license limitations below)
19  * in the documentation and/or other materials provided with the distribution
20  *
21  * Redistribution and use in binary form, without modification, are permitted provided that the following
22  * conditions are met:
23  *
24  * * No reverse engineering, decompilation, or disassembly of this software is permitted with respect to any
25  * software provided in binary form.
26  * * any redistribution and use are licensed by TI for use only with TI Devices.
27  * * Nothing shall obligate TI to provide you with source code for the software licensed and provided to you in object code.
28  *
29  * If software source code is provided to you, modification and redistribution of the source code are permitted
30  * provided that the following conditions are met:
31  *
32  * * any redistribution and use of the source code, including any resulting derivative works, are licensed by
33  * TI for use only with TI Devices.
34  * * any redistribution and use of any object code compiled from the source code and any resulting derivative
35  * works, are licensed by TI for use only with TI Devices.
36  *
37  * Neither the name of Texas Instruments Incorporated nor the names of its suppliers may be used to endorse or
38  * promote products derived from this software without specific prior written permission.
39  *
40  * DISCLAIMER.
41  *
42  * THIS SOFTWARE IS PROVIDED BY TI AND TI"S LICENSORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
43  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
44  * IN NO EVENT SHALL TI AND TI"S LICENSORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
45  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
46  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
48  * POSSIBILITY OF SUCH DAMAGE.
49 */
58 #ifndef UNIBASE_MARCORS_H_
59 #define UNIBASE_MARCORS_H_
60 
61 #include <stdio.h>
62 
63 /************************************************************
64  * utility macros
65  ************************************************************/
66 #define UB_SEC_NS 1000000000LL
67 #define UB_MSEC_NS 1000000
68 #define UB_USEC_NS 1000
69 #define UB_SEC_US 1000000LL
70 #define UB_MSEC_US 1000
71 #define UB_SEC_MS 1000
72 
73 #define UB_CHARS_IN_LINE 384
74 
75 #define UB_BIT(x) (1U<<(x))
76 #define UB_MAX(x,y) ((x)>(y)?(x):(y))
77 #define UB_MIN(x,y) ((x)<(y)?(x):(y))
78 
79 #define UB_CONSOLE_PRINT(...) \
80  { \
81  char coutstr[UB_CHARS_IN_LINE]; \
82  (void)snprintf(coutstr, UB_CHARS_IN_LINE, __VA_ARGS__); \
83  ub_console_debug_select_print(true, false, coutstr); \
84  }
85 
86 #define UB_DEBUGMEM_PRINT(...) \
87  { \
88  char coutstr[UB_CHARS_IN_LINE]; \
89  (void)snprintf(coutstr, UB_CHARS_IN_LINE, __VA_ARGS__); \
90  ub_console_debug_select_print(false, true, coutstr); \
91  }
92 
93 #define UB_CONSOLE_DEBUGMEM_PRINT(...) \
94  { \
95  char coutstr[UB_CHARS_IN_LINE]; \
96  (void)snprintf(coutstr, UB_CHARS_IN_LINE, __VA_ARGS__); \
97  ub_console_debug_select_print(true, true, coutstr); \
98  }
99 
100 #define UB_SELECT_PRINT(console, debugmem, ...) \
101  { \
102  char coutstr[UB_CHARS_IN_LINE]; \
103  (void)snprintf(coutstr, UB_CHARS_IN_LINE, __VA_ARGS__); \
104  ub_console_debug_select_print(console, debugmem, coutstr); \
105  }
106 
116 #define UB_LOG(level, ...) \
117  { \
118  char coutstr[UB_CHARS_IN_LINE]; \
119  (void)snprintf(coutstr, UB_CHARS_IN_LINE, __VA_ARGS__); \
120  (void)ub_log_print(UB_LOGCAT, 0, level, coutstr); \
121  }
122 
126 #define UB_TLOG(level, ...) \
127  { \
128  char coutstr[UB_CHARS_IN_LINE]; \
129  (void)snprintf(coutstr, UB_CHARS_IN_LINE, __VA_ARGS__); \
130  (void)ub_log_print(UB_LOGCAT, UB_LOGTSTYPE, level, coutstr); \
131  }
132 
134 #define UB_PRIhexB8 "%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X"
135 #define UB_ARRAY_B8(x) (x)[0],(x)[1],(x)[2],(x)[3],(x)[4],(x)[5],(x)[6],(x)[7]
137 
138 #define UB_PRIhexB6 "%02X:%02X:%02X:%02X:%02X:%02X"
139 #define UB_ARRAY_B6(x) (x)[0],(x)[1],(x)[2],(x)[3],(x)[4],(x)[5]
140 
141 
142 #define UB_NON_ZERO_B6(x) (((x)[0]|(x)[1]|(x)[2]|(x)[3]|(x)[4]|(x)[5])!=0u)
143 
145 #define UB_NON_ZERO_B8(x) (((x)[0]|(x)[1]|(x)[2]|(x)[3]|(x)[4]|(x)[5]|(x)[6]|(x)[7])!=0u)
146 
148 #define UB_ALLFF_B6(x) (((x)[0]&(x)[1]&(x)[2]&(x)[3]&(x)[4]&(x)[5])==0xffu)
149 
151 #define UB_ALLFF_B8(x) (((x)[0]&(x)[1]&(x)[2]&(x)[3]&(x)[4]&(x)[5]&(x)[6]&(x)[7])==0xffu)
152 
161 #define UB_ABIT8_FIELD(name, s, m) \
162  static inline int name##_bit_field(uint8_t x) \
163  { return (x >> (s)) & (m); } \
164  static inline int name##_set_bit_field(uint8_t x, uint8_t v) \
165  { return (x & ~((m) << (s))) | ((v & (m)) << (s)) ; }
166 
168 #define UB_ABIT16_FIELD(name, s, m) \
169  static inline int name##_bit_field(uint16_t x) \
170  { return (htons(x) >> (s)) & (m); } \
171  static inline int name##_set_bit_field(uint16_t x, uint16_t v) \
172  {return ntohs((htons(x) & ~((m) << (s))) \
173  | ((v & (m)) << (s)));}
174 
176 #define UB_ABIT32_FIELD(name, s, m) \
177  static inline int name##_bit_field(uint32_t x) \
178  { return (htonl(x) >> (s)) & (m); } \
179  static inline int name##_set_bit_field(uint32_t x, uint32_t v) \
180  {return ntohl((htonl(x) & ~((m) << (s))) \
181  | ((v & (m)) << (s)));}
182 
190 #define UB_ABIT8_TOGGLE_FIELD(name, s, m) \
191  static inline int name##_toggle_bit_field(uint8_t x) \
192  {return (x ^ ((m) << (s)));}
193 
195 #define UB_ABIT16_TOGGLE_FIELD(name, s, m) \
196  static inline int name##_toggle_bit_field(uint16_t x) \
197  {return ntohs((htons(x) ^ ((m) << (s))));}
198 
200 #define UB_ABIT32_TOGGLE_FIELD(name, s, m) \
201  static inline int name##_toggle_bit_field(uint32_t x) \
202  {return ntohl((htonl(x) ^ ((m) << (s))));}
203 
205 #define UB_TS2NSEC(ts) (((ts).tv_sec*1000000000)+(ts).tv_nsec)
206 
208 #define UB_TS2USEC(ts) (((ts).tv_sec*1000000)+(ts).tv_nsec/UB_USEC_NS)
209 
211 #define UB_TS2MSEC(ts) (((ts).tv_sec*UB_SEC_MS)+(ts).tv_nsec/UB_MSEC_NS)
212 
214 #define UB_TV2NSEC(tv) (((tv).tv_sec*1000000000)+(tv).tv_usec*UB_USEC_NS)
215 
217 #define UB_TV2USEC(tv) (((tv).tv_sec*1000000)+(tv).tv_usec)
218 
220 #define UB_TV2MSEC(tv) (((tv).tv_sec*UB_SEC_MS)+(tv).tv_usec/UB_MSEC_US)
221 
223 #define UB_NSEC2TS(ns, ts) {(ts).tv_sec=(ns)/1000000000;(ts).tv_nsec=(ns)%1000000000;}
224 
226 #define UB_USEC2TS(us, ts) {(ts).tv_sec=(us)/1000000;(ts).tv_nsec=((us)%1000000)*UB_USEC_NS;}
227 
229 #define UB_MSEC2TS(ms, ts) {(ts).tv_sec=(ms)/UB_SEC_MS;(ts).tv_nsec=((ms)%UB_SEC_MS)*UB_MSEC_NS;}
230 
232 #define UB_NSEC2TV(ns, tv) {(tv).tv_sec=(ns)/1000000000;(tv).tv_usec=((ns)%1000000000)/UB_USEC_NS;}
233 
235 #define UB_USEC2TV(us, tv) {(tv).tv_sec=(us)/1000000;(tv).tv_usec=(us)%1000000;}
236 
238 #define UB_MSEC2TV(ms, tv) {(tv).tv_sec=(ms)/UB_SEC_MS;(tv).tv_usec=((ms)%UB_SEC_MS)*UB_MSEC_US;}
239 
241 #define UB_TV_DIFF64NS(tv1,tv2) (UB_TV2NSEC(tv1)-UB_TV2NSEC(tv2))
242 
244 #define UB_TV_ADD64NS(tv1,tv2) (UB_TV2NSEC(tv1)+UB_TV2NSEC(tv2))
245 
247 #define UB_TS_DIFF64NS(ts1,ts2) (UB_TS2NSEC(ts1)-UB_TS2NSEC(ts2))
248 
250 #define UB_TS_ADD64NS(ts1,ts2) (UB_TS2NSEC(ts1)+UB_TS2NSEC(ts2))
251 
253 #define UB_TV_DIFF_TV(rtv,tv1,tv2) { \
254  int64_t ns_ub_m_=(int64_t)UB_TV_DIFF64NS(tv1,tv2); \
255  UB_NSEC2TV(ns_ub_m_,rtv); \
256  }
257 
259 #define UB_TS_DIFF_TS(rts,ts1,ts2) { \
260  int64_t ns_ub_m_=(int64_t)UB_TS_DIFF64NS(ts1,ts2); \
261  UB_NSEC2TS(ns_ub_m_,rts); \
262  }
263 
265 #define UB_TV_ADD_TV(rtv,tv1,tv2) { \
266  int64_t ns_ub_m_=(int64_t)UB_TV_ADD64NS(tv1,tv2); \
267  UB_NSEC2TV(ns_ub_m_,rtv); \
268  }
269 
271 #define UB_TS_ADD_TS(rts,ts1,ts2) { \
272  int64_t ns_ub_m_=(int64_t)UB_TS_ADD64NS(ts1,ts2); \
273  UB_NSEC2TS(ns_ub_m_,rts); \
274  }
275 
281 #define UB_HTONLL(x) ((1==htonl(1)) ? (x) : ((uint64_t)htonl((x) & 0xFFFFFFFF) << 32) | \
282  htonl((x) >> 32))
283 
289 #define UB_NTOHLL(x) ((1==ntohl(1)) ? (x) : ((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | \
290  ntohl((x) >> 32))
291 
297 #define UB_STATIC_ASSERT(cond, error) typedef char type[(cond) ? 1 : -1]
298 
299 #endif
300