xref: /btstack/port/stm32-wb55xx-nucleo-freertos/Middlewares/STM32_WPAN/utilities/utilities_common.h (revision 0561b2d8d5dba972c7daa57d5e677f7a1327edfd)
1 /**
2  ******************************************************************************
3  * @file    utilities_common.h
4  * @author  MCD Application Team
5  * @brief   Common file to utilities
6  ******************************************************************************
7   * @attention
8   *
9   * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
10   * All rights reserved.</center></h2>
11   *
12   * This software component is licensed by ST under BSD 3-Clause license,
13   * the "License"; You may not use this file except in compliance with the
14   * License. You may obtain a copy of the License at:
15   *                        opensource.org/licenses/BSD-3-Clause
16   *
17   ******************************************************************************
18  */
19 
20 
21 /* Define to prevent recursive inclusion -------------------------------------*/
22 #ifndef __UTILITIES_COMMON_H
23 #define __UTILITIES_COMMON_H
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 #include <stdint.h>
30 #include <string.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <stdarg.h>
34 
35 #include "app_conf.h"
36 
37   /* -------------------------------- *
38    *  Basic definitions               *
39    * -------------------------------- */
40 
41 #undef NULL
42 #define NULL                    0
43 
44 #undef FALSE
45 #define FALSE                   0
46 
47 #undef TRUE
48 #define TRUE                    (!0)
49 
50   /* -------------------------------- *
51    *  Critical Section definition     *
52    * -------------------------------- */
53 #undef BACKUP_PRIMASK
54 #define BACKUP_PRIMASK()    uint32_t primask_bit= __get_PRIMASK()
55 
56 #undef DISABLE_IRQ
57 #define DISABLE_IRQ()       __disable_irq()
58 
59 #undef RESTORE_PRIMASK
60 #define RESTORE_PRIMASK()   __set_PRIMASK(primask_bit)
61 
62   /* -------------------------------- *
63    *  Macro delimiters                *
64    * -------------------------------- */
65 #undef M_BEGIN
66 #define M_BEGIN     do {
67 
68 #undef  M_END
69 #define M_END       } while(0)
70 
71   /* -------------------------------- *
72    *  Some useful macro definitions   *
73    * -------------------------------- */
74 #undef MAX
75 #define MAX( x, y )          (((x)>(y))?(x):(y))
76 
77 #undef MIN
78 #define MIN( x, y )          (((x)<(y))?(x):(y))
79 
80 #undef MODINC
81 #define MODINC( a, m )       M_BEGIN  (a)++;  if ((a)>=(m)) (a)=0;  M_END
82 
83 #undef MODDEC
84 #define MODDEC( a, m )       M_BEGIN  if ((a)==0) (a)=(m);  (a)--;  M_END
85 
86 #undef MODADD
87 #define MODADD( a, b, m )    M_BEGIN  (a)+=(b);  if ((a)>=(m)) (a)-=(m);  M_END
88 
89 #undef MODSUB
90 #define MODSUB( a, b, m )    MODADD( a, (m)-(b), m )
91 
92 #undef ALIGN
93 #ifdef WIN32
94 #define ALIGN(n)
95 #else
96 #define ALIGN(n)             __attribute__((aligned(n)))
97 #endif
98 
99 #undef PAUSE
100 #define PAUSE( t )           M_BEGIN \
101                                volatile int _i; \
102                                for ( _i = t; _i > 0; _i -- ); \
103                              M_END
104 #undef DIVF
105 #define DIVF( x, y )         ((x)/(y))
106 
107 #undef DIVC
108 #define DIVC( x, y )         (((x)+(y)-1)/(y))
109 
110 #undef DIVR
111 #define DIVR( x, y )         (((x)+((y)/2))/(y))
112 
113 #undef SHRR
114 #define SHRR( x, n )         ((((x)>>((n)-1))+1)>>1)
115 
116 #undef BITN
117 #define BITN( w, n )         (((w)[(n)/32] >> ((n)%32)) & 1)
118 
119 #undef BITNSET
120 #define BITNSET( w, n, b )   M_BEGIN (w)[(n)/32] |= ((U32)(b))<<((n)%32); M_END
121 
122 /* -------------------------------- *
123  *  Section attribute               *
124  * -------------------------------- */
125 #define PLACE_IN_SECTION( __x__ )  __attribute__((section (__x__)))
126 
127 /* ----------------------------------- *
128  *  Packed usage (compiler dependent)  *
129  * ----------------------------------- */
130 #undef PACKED__
131 #undef PACKED_STRUCT
132 
133 #if defined ( __CC_ARM )
134   #if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
135     #define PACKED__ __attribute__((packed))
136     #define PACKED_STRUCT struct PACKED__
137   #else
138     #define PACKED__(TYPE) __packed TYPE
139     #define PACKED_STRUCT PACKED__(struct)
140   #endif
141 #elif defined   ( __GNUC__ )
142   #define PACKED__ __attribute__((packed))
143   #define PACKED_STRUCT struct PACKED__
144 #elif defined (__ICCARM__)
145   #define PACKED_STRUCT __packed struct
146 #elif
147   #define PACKED_STRUCT __packed struct
148 #endif
149 
150 #ifdef __cplusplus
151 }
152 #endif
153 
154 #endif /*__UTILITIES_COMMON_H */
155 
156 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
157