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