1 /* USER CODE BEGIN Header */
2 /**
3   ******************************************************************************
4   * @file    utilities_common.h
5   * @author  MCD Application Team
6   * @brief   Common file to utilities
7   ******************************************************************************
8   * @attention
9   *
10   * Copyright (c) 2022 STMicroelectronics.
11   * All rights reserved.
12   *
13   * This software is licensed under terms that can be found in the LICENSE file
14   * in the root directory of this software component.
15   * If no LICENSE file comes with this software, it is provided AS-IS.
16   *
17   ******************************************************************************
18   */
19 /* USER CODE END Header */
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 #ifdef __cplusplus
128 }
129 #endif
130 
131 #define SYS_WAITING_CYCLES_25() do {\
132             __asm("mov r0, r0");__asm("mov r0, r0");__asm("mov r0, r0");__asm("mov r0, r0");__asm("mov r0, r0");\
133             __asm("mov r0, r0");__asm("mov r0, r0");__asm("mov r0, r0");__asm("mov r0, r0");__asm("mov r0, r0");\
134             __asm("mov r0, r0");__asm("mov r0, r0");__asm("mov r0, r0");__asm("mov r0, r0");__asm("mov r0, r0");\
135             __asm("mov r0, r0");__asm("mov r0, r0");__asm("mov r0, r0");__asm("mov r0, r0");__asm("mov r0, r0");\
136             __asm("mov r0, r0");__asm("mov r0, r0");__asm("mov r0, r0");__asm("mov r0, r0");__asm("mov r0, r0");\
137            } while(0)
138 
139 #define SYS_WAITING_RNGCLK_DEACTIVATION() do {\
140             for(volatile unsigned int cpt = 178 ; cpt!=0 ; --cpt);\
141            } while(0)
142 
143 #endif /* UTILITIES_COMMON_H */
144