• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef __PLAT_TYPES_H__
16 #define __PLAT_TYPES_H__
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 #include "stddef.h"
23 #include "stdint.h"
24 #include "stdbool.h"
25 
26 #define __TCMDATA __attribute__((section(".tcmdata")))
27 #define __RAMCODE __attribute__((section(".ramcode")))
28 #define __RAMDATA __attribute__((section(".ramdata")))
29 
30 #ifdef RESTRICTED_RAM
31 #define __RRAMCODE __attribute__((section(".rram_text")))
32 #define __RRAMDATA __attribute__((section(".rram_data")))
33 #define __RRAMBSS __attribute__((section(".rram_bss")))
34 #else
35 #define __RRAMCODE
36 #define __RRAMDATA
37 #define __RRAMBSS
38 #endif
39 #define __SRAMCODE __attribute__((section(".sram_text")))
40 #define __SRAMDATA __attribute__((section(".sram_data")))
41 #define __SRAMBSS __attribute__((section(".sram_bss")))
42 #define __AUDMA __attribute__((section(".audma")))
43 #define __FSRAMCODE __attribute__((section(".fast_text_sram")))
44 #define __BOOTSRAMCODE   __attribute__((section(".boot_text_sram")))
45 
46 #if defined(CHIP_HAS_PSRAM) && defined(PSRAM_ENABLE)
47 #define __PSRAMCODE __attribute__((section(".psram_text")))
48 #define __PSRAMDATA __attribute__((section(".psram_data")))
49 #define __PSRAMBSS __attribute__((section(".psram_bss")))
50 #elif defined(CHIP_HAS_PSRAMUHS) && defined(PSRAMUHS_ENABLE)
51 #define __PSRAMUHSCODE		__attribute__((section(".psramuhs_text")))
52 #define __PSRAMUHSDATA	__attribute__((section(".psramuhs_data")))
53 #define __PSRAMUHSBSS		__attribute__((section(".psramuhs_bss")))
54 #define __PSRAMCODE		__PSRAMUHSCODE
55 #define __PSRAMDATA		__PSRAMUHSDATA
56 // #define __PSRAMBSS			__PSRAMUHSBSS
57 #define __PSRAMBSS
58 #else
59 #define __PSRAMCODE
60 #define __PSRAMDATA
61 #define __PSRAMBSS
62 #endif
63 #define __SRAM_EXT_CODE  __PSRAMCODE
64 #define __SRAM_EXT_DATA  __PSRAMDATA
65 #define __SRAM_EXT_BSS     __PSRAMBSS
66 
67 #ifndef TRUE
68 #define TRUE 1
69 #endif
70 #ifndef FALSE
71 #define FALSE 0
72 #endif
73 
74 typedef unsigned char               u8;
75 typedef unsigned short              u16;
76 typedef unsigned long               u32;
77 typedef unsigned long long          u64;
78 typedef char                        s8;
79 typedef short                       s16;
80 typedef long                        s32;
81 typedef long long                   s64;
82 
83 /* IO definitions (access restrictions to peripheral registers) */
84 /**
85     \defgroup CMSIS_glob_defs CMSIS Global Defines
86 
87     <strong>IO Type Qualifiers</strong> are used
88     \li to specify the access to peripheral variables.
89     \li for automatic generation of peripheral register debug information.
90 */
91 #ifndef __I
92 #ifdef __cplusplus
93   #define   __I     volatile             /*!< Defines 'read only' permissions                 */
94 #else
95   #define   __I     volatile const       /*!< Defines 'read only' permissions                 */
96 #endif
97 #define     __O     volatile             /*!< Defines 'write only' permissions                */
98 #define     __IO    volatile             /*!< Defines 'read / write' permissions              */
99 #endif
100 
101 
102 #define BITFIELD_VAL(field, val)        (((val) & (field ## _MASK >> field ## _SHIFT)) << field ## _SHIFT)
103 #define SET_BITFIELD(reg, field, val)   (((reg) & ~field ## _MASK) | BITFIELD_VAL(field, val))
104 #define GET_BITFIELD(reg, field)        (((reg) & field ## _MASK) >> field ## _SHIFT)
105 
106 
107 /* Frequently used macros */
108 
109 #ifndef _ALIGN
110 #define _ALIGN(val,exp)                  (((val) + ((exp)-1)) & ~((exp)-1))
111 #endif
112 
113 #define ARRAY_SIZE(a)                   (sizeof(a)/sizeof(a[0]))
114 #define LAST_ELEMENT(x)                 (&x[ARRAY_SIZE(x)-1])
115 #define BOUND(x, min, max)              ( (x) < (min) ? (min) : ((x) > (max) ? (max):(x)) )
116 #define ROUND_SIZEOF(t)                 ((sizeof(t)+sizeof(int)-1)&~(sizeof(int)-1))
117 
118 // Structure offset
119 #ifndef OFFSETOF
120 #define OFFSETOF(TYPE, MEMBER)          ((size_t) &((TYPE *)0)->MEMBER)
121 #endif
122 
123 #ifndef MAX
124 #define MAX(a,b)                        (((a) > (b)) ? (a) : (b))
125 #endif
126 #ifndef MIN
127 #define MIN(a,b)                        (((a) < (b)) ? (a) : (b))
128 #endif
129 #ifndef ABS
130 #define ABS(x)                          ((x<0)?(-(x)):(x))
131 #endif
132 
133 #define TO_STRING_A(s)                  # s
134 #define TO_STRING(s)                    TO_STRING_A(s)
135 
136 /* Remove const cast-away warnings from gcc -Wcast-qual */
137 #define __UNCONST(a)                    ((void *)(unsigned long)(const void *)(a))
138 
139 #ifdef __GNUC__
140 
141 /// From http://www.ibm.com/developerworks/linux/library/l-gcc-hacks/
142 /// Macro to use in a if statement to tell the compiler this branch
143 /// is likely taken, and optimize accordingly.
144 #define LIKELY(x)                       __builtin_expect(!!(x), 1)
145 /// Macro to use in a if statement to tell the compiler this branch
146 /// is unlikely take, and optimize accordingly.
147 #define UNLIKELY(x)                     __builtin_expect(!!(x), 0)
148 
149 /// For packing structure
150 #define PACKED                          __attribute__((packed))
151 
152 /// To describe alignment
153 #define ALIGNED(a)                      __attribute__((aligned(a)))
154 
155 /// For possibly unused functions or variables (e.g., debugging stuff)
156 #define POSSIBLY_UNUSED                 __attribute__((unused))
157 
158 /// For functions or variables must be emitted even if not referenced
159 #define USED                            __attribute__((used))
160 
161 /// For inline functions
162 #define ALWAYS_INLINE                   __attribute__((always_inline))
163 
164 // For functions never inlined
165 #define NOINLINE                        __attribute__((noinline))
166 
167 // For functions not caring performance
168 #ifdef __ARMCC_VERSION
169 #define OPT_SIZE
170 #else
171 #define OPT_SIZE                        __attribute__((optimize("Os")))
172 #endif
173 
174 // For functions not returning
175 #ifndef NORETURN
176 #define NORETURN                        __attribute__((noreturn))
177 #endif
178 // For ASM functions in C
179 #ifdef __arm__
180 #define NAKED                           __attribute__((naked))
181 #else
182 #define NAKED                           __attribute__((error("Unsupport naked functions")))
183 #endif
184 
185 // For weak symbols
186 #define WEAK                            __attribute__((weak))
187 
188 /**
189  * CONTAINER_OF - cast a member of a structure out to the containing structure
190  * @ptr:        the pointer to the member.
191  * @type:       the type of the container struct this is embedded in.
192  * @member:     the name of the member within the struct.
193  *
194  */
195 #ifndef CONTAINER_OF
196 #if __cplusplus >= 201103L
197 #define CONTAINER_OF(ptr, type, member) ({          \
198         const decltype(((type *)0)->member)*__mptr = (ptr);    \
199     (type *)((char *)__mptr - OFFSETOF(type, member)); })
200 #else
201 #define CONTAINER_OF(ptr, type, member) ({          \
202         const typeof(((type *)0)->member)*__mptr = (ptr);    \
203     (type *)((char *)__mptr - OFFSETOF(type, member)); })
204 #endif
205 #endif
206 
207 #else // Not GCC
208 
209 #define LIKELY(x)
210 #define UNLIKELY(x)
211 #define PACKED
212 #define ALIGNED(a)
213 #define POSSIBLY_UNUSED
214 #define USED
215 #define ALWAYS_INLINE
216 #define NOINLINE
217 #define OPT_SIZE
218 #define NORETURN
219 #define NAKED
220 #define WEAK
221 
222 #define CONTAINER_OF(ptr, type, member) (   \
223     (type *)((char *)(ptr) - OFFSETOF(type, member)))
224 
225 #endif // Not GCC
226 
227 /// C preprocessor conditional check
228 /// --------------------------------
229 #define GCC_VERSION (__GNUC__ * 10000 \
230                        + __GNUC_MINOR__ * 100 \
231                        + __GNUC_PATCHLEVEL__)
232 
233 #if defined(__GNUC__) && (GCC_VERSION >= 40600) && !defined(__cplusplus)
234 
235 // GCC 4.6 or later
236 #define STATIC_ASSERT(e, m)             _Static_assert(e, m)
237 
238 #elif defined(__GNUC__) && (GCC_VERSION >= 40300) && defined(__cplusplus) && (__cplusplus >= 201103L)
239 
240 #define STATIC_ASSERT(e, m)             static_assert(e, m)
241 
242 #else // No built-in static assert
243 
244 /// FROM: http://www.pixelbeat.org/programming/gcc/static_assert.html
245 #define ASSERT_CONCAT_(a, b)            a##b
246 #define ASSERT_CONCAT(a, b)             ASSERT_CONCAT_(a, b)
247 /* These can't be used after statements in c89. */
248 #ifdef __COUNTER__
249 #define STATIC_ASSERT(e, m) \
250     enum { ASSERT_CONCAT(static_assert_, __COUNTER__) = 1/(!!(e)) };
251 #else
252 /* This can't be used twice on the same line so ensure if using in headers
253  * that the headers are not included twice (by wrapping in #ifndef...#endif)
254  * Note it doesn't cause an issue when used on same line of separate modules
255  * compiled with gcc -combine -fwhole-program.  */
256 #define STATIC_ASSERT(e, m) \
257     enum { ASSERT_CONCAT(assert_line_, __LINE__) = 1/(!!(e)) };
258 #endif
259 
260 #endif // No built-in static assert
261 /// --------------------------------
262 
263 #ifdef __cplusplus
264 }
265 #endif
266 
267 #endif
268 
269