1 //*****************************************************************************
2 //
3 //! @file am_hal_global.h
4 //!
5 //! @brief Locate all HAL global variables here.
6 
7 //! This module contains global variables that are used throughout the HAL,
8 //! but not necessarily those designated as const (which typically end up in
9 //! flash). Consolidating globals here will make it easier to manage them.
10 //!
11 //! @addtogroup globals_4p Globals - HAL globals
12 //! @ingroup apollo4p_hal
13 //! @{
14 //
15 //*****************************************************************************
16 
17 //*****************************************************************************
18 //
19 // Copyright (c) 2023, Ambiq Micro, Inc.
20 // All rights reserved.
21 //
22 // Redistribution and use in source and binary forms, with or without
23 // modification, are permitted provided that the following conditions are met:
24 //
25 // 1. Redistributions of source code must retain the above copyright notice,
26 // this list of conditions and the following disclaimer.
27 //
28 // 2. Redistributions in binary form must reproduce the above copyright
29 // notice, this list of conditions and the following disclaimer in the
30 // documentation and/or other materials provided with the distribution.
31 //
32 // 3. Neither the name of the copyright holder nor the names of its
33 // contributors may be used to endorse or promote products derived from this
34 // software without specific prior written permission.
35 //
36 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
37 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
40 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
41 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
42 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
43 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
44 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
46 // POSSIBILITY OF SUCH DAMAGE.
47 //
48 // This is part of revision release_sdk_4_4_0-3c5977e664 of the AmbiqSuite Development Package.
49 //
50 //*****************************************************************************
51 #ifndef AM_HAL_GLOBAL_H
52 #define AM_HAL_GLOBAL_H
53 
54 #ifdef __cplusplus
55 extern "C"
56 {
57 #endif
58 
59 //*****************************************************************************
60 //
61 // Include the SDK global version information.
62 //
63 //*****************************************************************************
64 #include "../../am_sdk_version.h"
65 
66 //*****************************************************************************
67 //
68 //! Device definitions
69 //
70 //*****************************************************************************
71 #define AM_HAL_DEVICE_NAME      "Apollo4"
72 
73 //*****************************************************************************
74 //
75 //! Some Ambiqsuite workaround implementations use a TIMER interrupt
76 //! AM_HAL_WRITE_WAIT_TIMER (TIMER13 used for this in default SDK).
77 //! The interrupt is configured as the highest priority (0) interrupt to prevent
78 //! unintentional break out due to other interrupts. In order for this to work
79 //! reliably, it is required that all the other interrupts in the system are set
80 //! at a lower priority, reserving the highest priority interrupt exclusively
81 //! for AmbiqSuite workaround.
82 //
83 #define AM_HAL_WRITE_WAIT_TIMER  13
84 
85 //*****************************************************************************
86 //
87 //! @name Macro definitions
88 //! Utility for compile time assertions
89 //! Will cause divide by 0 error at build time
90 //! @{
91 //
92 //*****************************************************************************
93 #define _AM_ASSERT_CONCAT_(a, b) a##b
94 #define _AM_ASSERT_CONCAT(a, b) _AM_ASSERT_CONCAT_(a, b)
95 #define am_ct_assert(e) enum { _AM_ASSERT_CONCAT(assert_line_, __LINE__) = 1/(!!(e)) }
96 //! @}
97 
98 //*****************************************************************************
99 //
100 //! STATIC_ASSERT will do a static (compile-time) check of a sizeof() operation
101 //! (such as the size of a structure) without creating any code.
102 //! This can be useful in a situation such as initializing a structure in a
103 //! member-by-member fashion to make sure the entire structure is initialized,
104 //! particularly if that structure might be changed in the future.
105 //!
106 //! Example usage (assumes some_structure_s contains 20 uint32_t's):
107 //!  STATIC_ASSERT(sizeof(struct some_structure_s) != (20 * 4));
108 //!
109 //! If the condition is not met, a compile error will be induced that will
110 //! will typically display a message along the lines of
111 //! "The size of an array must be greater than zero."
112 //!
113 //! The STATIC_ASSERT macro is specific to sizeof() and is not recommended
114 //! for use in other assert situations.
115 //
116 //*****************************************************************************
117 #define STATIC_ASSERT(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
118 
119 //*****************************************************************************
120 //
121 //! @name Keil attribute macros
122 //! @{
123 //
124 //*****************************************************************************
125 #if defined(keil) || defined(keil6)
126 #define AM_SHARED_RW      __attribute__((section("SHARED_RW"))) __attribute__((used))
127 #define AM_RESOURCE_TABLE __attribute__((section("RESOURCE_TABLE"))) __attribute__((used))
128 #define AM_USED           __attribute__((used))
129 #define AM_SECTION(x)     __attribute__((section(x)))
130 //! @}
131 
132 //*****************************************************************************
133 //
134 //! @name IAR attribute macros
135 //! @{
136 //
137 //*****************************************************************************
138 #elif defined(iar)
139 #define AM_SHARED_RW      __attribute__((section("SHARED_RW"))) __root
140 #define AM_RESOURCE_TABLE __attribute__((section("RESOURCE_TABLE"))) __root
141 #define AM_USED           __root
142 #define AM_SECTION(x)     __attribute__((section(x)))
143 //! @}
144 
145 //*****************************************************************************
146 //
147 //! @name GCC attribute macros
148 //! @{
149 //
150 //*****************************************************************************
151 #elif defined(gcc)
152 #define AM_SHARED_RW      __attribute__((section(".shared")))
153 #define AM_RESOURCE_TABLE __attribute__((section(".resource_table")))
154 #define AM_USED           __attribute__((used))
155 #define AM_SECTION(x)     __attribute__((section(x)))
156 //! @}
157 
158 //*****************************************************************************
159 //
160 //! @name XTENSA attribute macros
161 //! @{
162 //
163 //*****************************************************************************
164 #elif defined(xtensa)
165 #define AM_SHARED_RW
166 #define AM_SECTION(x)
167 //! @}
168 
169 //*****************************************************************************
170 //
171 //! @name Attribute macro stubs.
172 //! @{
173 //
174 //*****************************************************************************
175 #else
176 #define AM_SHARED_RW
177 #define AM_SECTION(x)
178 //! @}
179 
180 #endif // End of tool-specific attribute macros.
181 
182 //*****************************************************************************
183 //
184 //! Core ID
185 //
186 //*****************************************************************************
187 typedef enum
188 {
189     AM_HAL_COREID_CM4F,
190     AM_HAL_COREID_DSP0,
191     AM_HAL_COREID_DSP1,
192     AM_HAL_CORE_MAX
193 }
194 am_hal_core_e;
195 
196 //*****************************************************************************
197 //
198 //! DSP selector
199 //
200 //*****************************************************************************
201 typedef enum
202 {
203     AM_HAL_DSP0,
204     AM_HAL_DSP1
205 }
206 am_hal_dsp_select_e;
207 
208 //*****************************************************************************
209 //
210 //! @name Macros to determine compiler version information
211 //! @{
212 //!
213 //! Since the stringize operator itself does not first expand macros, two levels
214 //!  of indirection are required in order to fully resolve the pre-defined
215 //!  compiler (integer) macros.  The 1st level expands the macro, and the 2nd
216 //!  level actually stringizes it.
217 //! This method will also work even if the argument is not a macro. However, if
218 //!  the argument is already a string, the string will end up with inserted quote
219 //!   marks.
220 //
221 //*****************************************************************************
222 #define STRINGIZE_VAL(n)                    STRINGIZE_VAL2(n)
223 #define STRINGIZE_VAL2(n)                   #n
224 //! @}
225 
226 //*****************************************************************************
227 //
228 //! @name Utility Macros
229 //! @{
230 
231 //! As long as the two values are not apart by more that 2^31, this should give
232 //! correct result, taking care of wraparound
233 //
234 //*****************************************************************************
235 #define AM_HAL_U32_GREATER(val1, val2)     ((int32_t)((int32_t)(val1) - (int32_t)(val2)) > 0)
236 #define AM_HAL_U32_SMALLER(val1, val2)     ((int32_t)((int32_t)(val1) - (int32_t)(val2)) < 0)
237 //! @}
238 
239 //*****************************************************************************
240 //
241 // Resources used for HAL internal usage only
242 //
243 //*****************************************************************************
244 
245 //******************************************************************************
246 //
247 //! @name Global typedefs
248 //! @{
249 //
250 //******************************************************************************
251 //*****************************************************************************
252 //
253 //! HAL Version
254 //
255 //*****************************************************************************
256 typedef union
257 {
258     uint32_t    u32;
259     struct
260     {
261         uint32_t    resvd       : 7;    // [6:0]
262         uint32_t    bAMREGS     : 1;    // [7]
263         uint32_t    Revision    : 8;    // [15:8]
264         uint32_t    Minor       : 8;    // [23:16]
265         uint32_t    Major       : 8;    // [31:24]
266     } s;
267 } am_hal_version_t;
268 
269 //*****************************************************************************
270 //
271 //! HAL Handle Prefix
272 //
273 //*****************************************************************************
274 typedef union
275 {
276     uint32_t    u32;
277     struct
278     {
279         uint32_t    magic   : 24;
280         uint32_t    bInit   : 1;
281         uint32_t    bEnable : 1;
282         uint32_t    resv    : 6;
283     } s;
284 } am_hal_handle_prefix_t;
285 //! @}
286 
287 //*****************************************************************************
288 //
289 // Global Variables extern declarations.
290 //
291 //*****************************************************************************
292 extern const    uint8_t  g_ui8HALcompiler[];
293 extern const    am_hal_version_t g_ui32HALversion;
294 #ifdef APOLLO4_FPGA
295 extern uint32_t g_ui32FPGAfreqMHz;
296 extern void am_hal_global_FPGAfreqSet(uint32_t ui32FPGAfreqMhz);
297 #endif // APOLLO4_FPGA
298 
299 #if (defined (__ARMCC_VERSION)) && (__ARMCC_VERSION < 6000000)
300 __asm void
301 am_hal_triple_read( uint32_t ui32TimerAddr, uint32_t ui32Data[]);
302 #elif (defined (__ARMCC_VERSION)) && (__ARMCC_VERSION >= 6000000)
303 void
304 am_hal_triple_read(uint32_t ui32TimerAddr, uint32_t ui32Data[]);
305 #elif defined(__GNUC_STDC_INLINE__)
306 __attribute__((naked))
307 void
308 am_hal_triple_read(uint32_t ui32TimerAddr, uint32_t ui32Data[]);
309 #elif defined(__IAR_SYSTEMS_ICC__)
310 __stackless void
311 am_hal_triple_read( uint32_t ui32TimerAddr, uint32_t ui32Data[]);
312 #else
313 #error Compiler is unknown, please contact Ambiq support team
314 #endif
315 
316 #ifdef __cplusplus
317 }
318 #endif
319 
320 #endif // AM_HAL_GLOBAL_H
321 
322 //*****************************************************************************
323 //
324 // End Doxygen group.
325 //! @}
326 //
327 //*****************************************************************************
328 
329