AM263Px MCU+ SDK  10.01.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, ...) UB_LOG_##level(__VA_ARGS__)
117 
121 #define UB_TLOG(level, ...) UB_TLOG_##level(__VA_ARGS__)
122 
136 #define UB_VLOG(var, ...) \
137  if((var)==UBL_DEBUGV){UB_LOG(UBL_DEBUGV,__VA_ARGS__);}\
138  else if((var)==UBL_DEBUG){UB_LOG(UBL_DEBUG,__VA_ARGS__);}\
139  else if((var)==UBL_INFOV){UB_LOG(UBL_INFOV,__VA_ARGS__);}\
140  else if((var)==UBL_INFO){UB_LOG(UBL_INFO,__VA_ARGS__);}\
141  else if((var)==UBL_WARN){UB_LOG(UBL_WARN,__VA_ARGS__);}\
142  else if((var)==UBL_ERROR){UB_LOG(UBL_ERROR,__VA_ARGS__);}\
143  else if((var)==UBL_FATAL){UB_LOG(UBL_FATAL,__VA_ARGS__);}\
144  else{;}
145 
147 #define UB_PRIhexB8 "%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X"
148 #define UB_ARRAY_B8(x) (x)[0],(x)[1],(x)[2],(x)[3],(x)[4],(x)[5],(x)[6],(x)[7]
150 
151 #define UB_PRIhexB6 "%02X:%02X:%02X:%02X:%02X:%02X"
152 #define UB_ARRAY_B6(x) (x)[0],(x)[1],(x)[2],(x)[3],(x)[4],(x)[5]
153 
154 
155 #define UB_NON_ZERO_B6(x) (((x)[0]|(x)[1]|(x)[2]|(x)[3]|(x)[4]|(x)[5])!=0u)
156 
158 #define UB_NON_ZERO_B8(x) (((x)[0]|(x)[1]|(x)[2]|(x)[3]|(x)[4]|(x)[5]|(x)[6]|(x)[7])!=0u)
159 
161 #define UB_ALLFF_B6(x) (((x)[0]&(x)[1]&(x)[2]&(x)[3]&(x)[4]&(x)[5])==0xffu)
162 
164 #define UB_ALLFF_B8(x) (((x)[0]&(x)[1]&(x)[2]&(x)[3]&(x)[4]&(x)[5]&(x)[6]&(x)[7])==0xffu)
165 
174 #define UB_ABIT8_FIELD(name, s, m) \
175  static inline int name##_bit_field(uint8_t x) \
176  { return (x >> (s)) & (m); } \
177  static inline int name##_set_bit_field(uint8_t x, uint8_t v) \
178  { return (x & ~((m) << (s))) | ((v & (m)) << (s)) ; }
179 
181 #define UB_ABIT16_FIELD(name, s, m) \
182  static inline int name##_bit_field(uint16_t x) \
183  { return (htons(x) >> (s)) & (m); } \
184  static inline int name##_set_bit_field(uint16_t x, uint16_t v) \
185  {return ntohs((htons(x) & ~((m) << (s))) \
186  | ((v & (m)) << (s)));}
187 
189 #define UB_ABIT32_FIELD(name, s, m) \
190  static inline int name##_bit_field(uint32_t x) \
191  { return (htonl(x) >> (s)) & (m); } \
192  static inline int name##_set_bit_field(uint32_t x, uint32_t v) \
193  {return ntohl((htonl(x) & ~((m) << (s))) \
194  | ((v & (m)) << (s)));}
195 
203 #define UB_ABIT8_TOGGLE_FIELD(name, s, m) \
204  static inline int name##_toggle_bit_field(uint8_t x) \
205  {return (x ^ ((m) << (s)));}
206 
208 #define UB_ABIT16_TOGGLE_FIELD(name, s, m) \
209  static inline int name##_toggle_bit_field(uint16_t x) \
210  {return ntohs((htons(x) ^ ((m) << (s))));}
211 
213 #define UB_ABIT32_TOGGLE_FIELD(name, s, m) \
214  static inline int name##_toggle_bit_field(uint32_t x) \
215  {return ntohl((htonl(x) ^ ((m) << (s))));}
216 
218 #define UB_TS2NSEC(ts) (((int64_t)(ts).tv_sec*1000000000)+(ts).tv_nsec)
219 
221 #define UB_TS2USEC(ts) (((int64_t)(ts).tv_sec*1000000)+(ts).tv_nsec/UB_USEC_NS)
222 
224 #define UB_TS2MSEC(ts) (((int64_t)(ts).tv_sec*UB_SEC_MS)+(ts).tv_nsec/UB_MSEC_NS)
225 
227 #define UB_TV2NSEC(tv) (((int64_t)(tv).tv_sec*1000000000)+(int64_t)(tv).tv_usec*UB_USEC_NS)
228 
230 #define UB_TV2USEC(tv) (((int64_t)(tv).tv_sec*1000000)+(tv).tv_usec)
231 
233 #define UB_TV2MSEC(tv) (((int64_t)(tv).tv_sec*UB_SEC_MS)+(tv).tv_usec/UB_MSEC_US)
234 
236 #define UB_NSEC2TS(ns, ts) {(ts).tv_sec=(ns)/1000000000;(ts).tv_nsec=(ns)%1000000000;}
237 
239 #define UB_USEC2TS(us, ts) {(ts).tv_sec=(us)/1000000;(ts).tv_nsec=((us)%1000000)*UB_USEC_NS;}
240 
242 #define UB_MSEC2TS(ms, ts) {(ts).tv_sec=(ms)/UB_SEC_MS;(ts).tv_nsec=((ms)%UB_SEC_MS)*UB_MSEC_NS;}
243 
245 #define UB_NSEC2TV(ns, tv) {(tv).tv_sec=(ns)/1000000000;(tv).tv_usec=((ns)%1000000000)/UB_USEC_NS;}
246 
248 #define UB_USEC2TV(us, tv) {(tv).tv_sec=(us)/1000000;(tv).tv_usec=(us)%1000000;}
249 
251 #define UB_MSEC2TV(ms, tv) {(tv).tv_sec=(ms)/UB_SEC_MS;(tv).tv_usec=((ms)%UB_SEC_MS)*UB_MSEC_US;}
252 
254 #define UB_TV_DIFF64NS(tv1,tv2) (UB_TV2NSEC(tv1)-UB_TV2NSEC(tv2))
255 
257 #define UB_TV_ADD64NS(tv1,tv2) (UB_TV2NSEC(tv1)+UB_TV2NSEC(tv2))
258 
260 #define UB_TS_DIFF64NS(ts1,ts2) (UB_TS2NSEC(ts1)-UB_TS2NSEC(ts2))
261 
263 #define UB_TS_ADD64NS(ts1,ts2) (UB_TS2NSEC(ts1)+UB_TS2NSEC(ts2))
264 
266 #define UB_TV_DIFF_TV(rtv,tv1,tv2) { \
267  int64_t ns_ub_m_=(int64_t)UB_TV_DIFF64NS(tv1,tv2); \
268  UB_NSEC2TV(ns_ub_m_,rtv); \
269  }
270 
272 #define UB_TS_DIFF_TS(rts,ts1,ts2) { \
273  int64_t ns_ub_m_=(int64_t)UB_TS_DIFF64NS(ts1,ts2); \
274  UB_NSEC2TS(ns_ub_m_,rts); \
275  }
276 
278 #define UB_TV_ADD_TV(rtv,tv1,tv2) { \
279  int64_t ns_ub_m_=(int64_t)UB_TV_ADD64NS(tv1,tv2); \
280  UB_NSEC2TV(ns_ub_m_,rtv); \
281  }
282 
284 #define UB_TS_ADD_TS(rts,ts1,ts2) { \
285  int64_t ns_ub_m_=(int64_t)UB_TS_ADD64NS(ts1,ts2); \
286  UB_NSEC2TS(ns_ub_m_,rts); \
287  }
288 
294 #define UB_HTONLL(x) ((1==htonl(1)) ? (x) : ((uint64_t)htonl((x) & 0xFFFFFFFF) << 32) | \
295  htonl((x) >> 32))
296 
302 #define UB_NTOHLL(x) ((1==ntohl(1)) ? (x) : ((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | \
303  ntohl((x) >> 32))
304 
310 #define UB_STATIC_ASSERT(cond, error) typedef char type[(cond) ? 1 : -1]
311 
317 #define UB_PROTECTED_FUNC(cbfunc, rval, ...) \
318  int ub_func_private_mutex_lock(void); \
319  int ub_func_private_mutex_unlock(void); \
320  ub_func_private_mutex_lock(); \
321  rval=cbfunc(__VA_ARGS__); \
322  ub_func_private_mutex_unlock();
323 
327 #define UB_PROTECTED_FUNC_VOID(cbfunc, ...) \
328  int ub_func_private_mutex_lock(void); \
329  int ub_func_private_mutex_unlock(void); \
330  ub_func_private_mutex_lock(); \
331  cbfunc(__VA_ARGS__); \
332  ub_func_private_mutex_unlock();
333 
334 #endif
335