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 stable-7da8bae71f 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 #define AM_BIT_ALIGNED(x) __attribute__((aligned(x>>3))) 131 //! @} 132 133 //***************************************************************************** 134 // 135 //! @name IAR attribute macros 136 //! @{ 137 // 138 //***************************************************************************** 139 #elif defined(iar) 140 #define AM_SHARED_RW __attribute__((section("SHARED_RW"))) __root 141 #define AM_RESOURCE_TABLE __attribute__((section("RESOURCE_TABLE"))) __root 142 #define AM_USED __root 143 #define AM_SECTION(x) __attribute__((section(x))) 144 #define AM_BIT_ALIGNED(x) __attribute__((aligned(x>>3))) 145 //! @} 146 147 //***************************************************************************** 148 // 149 //! @name GCC attribute macros 150 //! @{ 151 // 152 //***************************************************************************** 153 #elif defined(gcc) 154 #define AM_SHARED_RW __attribute__((section(".shared"))) 155 #define AM_RESOURCE_TABLE __attribute__((section(".resource_table"))) 156 #define AM_USED __attribute__((used)) 157 #define AM_SECTION(x) __attribute__((section(x))) 158 #define AM_BIT_ALIGNED(x) __attribute__((aligned(x>>3))) 159 //! @} 160 161 //***************************************************************************** 162 // 163 //! @name XTENSA attribute macros 164 //! @{ 165 // 166 //***************************************************************************** 167 #elif defined(xtensa) 168 #define AM_SHARED_RW 169 #define AM_SECTION(x) 170 //! @} 171 172 //***************************************************************************** 173 // 174 //! @name Attribute macro stubs. 175 //! @{ 176 // 177 //***************************************************************************** 178 #else 179 #define AM_SHARED_RW 180 #define AM_SECTION(x) 181 //! @} 182 183 #endif // End of tool-specific attribute macros. 184 185 //***************************************************************************** 186 // 187 //! Core ID 188 // 189 //***************************************************************************** 190 typedef enum 191 { 192 AM_HAL_COREID_CM4F, 193 AM_HAL_COREID_DSP0, 194 AM_HAL_COREID_DSP1, 195 AM_HAL_CORE_MAX 196 } 197 am_hal_core_e; 198 199 //***************************************************************************** 200 // 201 //! DSP selector 202 // 203 //***************************************************************************** 204 typedef enum 205 { 206 AM_HAL_DSP0, 207 AM_HAL_DSP1 208 } 209 am_hal_dsp_select_e; 210 211 //***************************************************************************** 212 // 213 //! @name Macros to determine compiler version information 214 //! @{ 215 //! 216 //! Since the stringize operator itself does not first expand macros, two levels 217 //! of indirection are required in order to fully resolve the pre-defined 218 //! compiler (integer) macros. The 1st level expands the macro, and the 2nd 219 //! level actually stringizes it. 220 //! This method will also work even if the argument is not a macro. However, if 221 //! the argument is already a string, the string will end up with inserted quote 222 //! marks. 223 // 224 //***************************************************************************** 225 #define STRINGIZE_VAL(n) STRINGIZE_VAL2(n) 226 #define STRINGIZE_VAL2(n) #n 227 //! @} 228 229 // 230 // The Arm6 compiler defines both GNUC and ARMCC_VERSION. So check ARMCC first. 231 // 232 #if defined(__ARMCC_VERSION) 233 #define COMPILER_VERSION ("ARMCC " STRINGIZE_VAL(__ARMCC_VERSION)) 234 #elif __GNUC__ 235 #define COMPILER_VERSION ("GCC " __VERSION__) 236 #elif defined(__KEIL__) 237 #define COMPILER_VERSION "KEIL_CARM " STRINGIZE_VAL(__CA__) 238 #elif defined(__IAR_SYSTEMS_ICC__) 239 #define COMPILER_VERSION __VERSION__ 240 #else 241 #define COMPILER_VERSION "Compiler unknown" 242 #endif 243 244 //***************************************************************************** 245 // 246 //! @name Utility Macros 247 //! @{ 248 249 //! As long as the two values are not apart by more that 2^31, this should give 250 //! correct result, taking care of wraparound 251 // 252 //***************************************************************************** 253 #define AM_HAL_U32_GREATER(val1, val2) ((int32_t)((int32_t)(val1) - (int32_t)(val2)) > 0) 254 #define AM_HAL_U32_SMALLER(val1, val2) ((int32_t)((int32_t)(val1) - (int32_t)(val2)) < 0) 255 //! @} 256 257 //***************************************************************************** 258 // 259 // Resources used for HAL internal usage only 260 // 261 //***************************************************************************** 262 263 //****************************************************************************** 264 // 265 //! @name Global typedefs 266 //! @{ 267 // 268 //****************************************************************************** 269 //***************************************************************************** 270 // 271 //! HAL Version 272 // 273 //***************************************************************************** 274 typedef union 275 { 276 uint32_t u32; 277 struct 278 { 279 uint32_t resvd : 7; // [6:0] 280 uint32_t bAMREGS : 1; // [7] 281 uint32_t Revision : 8; // [15:8] 282 uint32_t Minor : 8; // [23:16] 283 uint32_t Major : 8; // [31:24] 284 } s; 285 } am_hal_version_t; 286 287 //***************************************************************************** 288 // 289 //! HAL Handle Prefix 290 // 291 //***************************************************************************** 292 typedef union 293 { 294 uint32_t u32; 295 struct 296 { 297 uint32_t magic : 24; 298 uint32_t bInit : 1; 299 uint32_t bEnable : 1; 300 uint32_t resv : 6; 301 } s; 302 } am_hal_handle_prefix_t; 303 //! @} 304 305 //***************************************************************************** 306 // 307 // Global Variables extern declarations. 308 // 309 //***************************************************************************** 310 extern const uint8_t g_ui8HALcompiler[]; 311 extern const am_hal_version_t g_ui32HALversion; 312 313 #if (defined (__ARMCC_VERSION)) && (__ARMCC_VERSION < 6000000) 314 __asm void 315 am_hal_triple_read( uint32_t ui32TimerAddr, uint32_t ui32Data[]); 316 #elif (defined (__ARMCC_VERSION)) && (__ARMCC_VERSION >= 6000000) 317 void 318 am_hal_triple_read(uint32_t ui32TimerAddr, uint32_t ui32Data[]); 319 #elif defined(__GNUC_STDC_INLINE__) 320 __attribute__((naked)) 321 void 322 am_hal_triple_read(uint32_t ui32TimerAddr, uint32_t ui32Data[]); 323 #elif defined(__IAR_SYSTEMS_ICC__) 324 __stackless void 325 am_hal_triple_read( uint32_t ui32TimerAddr, uint32_t ui32Data[]); 326 #else 327 #error Compiler is unknown, please contact Ambiq support team 328 #endif 329 330 #ifdef __cplusplus 331 } 332 #endif 333 334 #endif // AM_HAL_GLOBAL_H 335 336 //***************************************************************************** 337 // 338 // End Doxygen group. 339 //! @} 340 // 341 //***************************************************************************** 342 343