1 //*****************************************************************************
2 //
3 //  am_reg_macros.h
4 //! @file
5 //!
6 //! @brief Helper macros for using hardware registers.
7 //
8 //*****************************************************************************
9 
10 //*****************************************************************************
11 //
12 // Copyright (c) 2024, Ambiq Micro, Inc.
13 // All rights reserved.
14 //
15 // Redistribution and use in source and binary forms, with or without
16 // modification, are permitted provided that the following conditions are met:
17 //
18 // 1. Redistributions of source code must retain the above copyright notice,
19 // this list of conditions and the following disclaimer.
20 //
21 // 2. Redistributions in binary form must reproduce the above copyright
22 // notice, this list of conditions and the following disclaimer in the
23 // documentation and/or other materials provided with the distribution.
24 //
25 // 3. Neither the name of the copyright holder nor the names of its
26 // contributors may be used to endorse or promote products derived from this
27 // software without specific prior written permission.
28 //
29 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
30 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
33 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 // POSSIBILITY OF SUCH DAMAGE.
40 //
41 // This is part of revision release_sdk_3_2_0-dd5f40c14b of the AmbiqSuite Development Package.
42 //
43 //*****************************************************************************
44 
45 #ifndef AM_REG_MACROS_H
46 #define AM_REG_MACROS_H
47 
48 #ifdef __cplusplus
49 extern "C"
50 {
51 #endif
52 
53 //*****************************************************************************
54 //
55 // For direct 32-bit access to a register or memory location, use AM_REGVAL:
56 //      AM_REGVAL(0x1234567) |= 0xDEADBEEF;
57 //
58 //*****************************************************************************
59 #define AM_REGVAL(x)               (*((volatile uint32_t *)(x)))
60 #define AM_REGVAL_FLOAT(x)         (*((volatile float *)(x)))
61 
62 //*****************************************************************************
63 //
64 // AM_REGADDR()
65 // One thing CMSIS does not do well natively is to provide for static register
66 // address computation. The address-of operator (e.g. &periph->reg) is helpful,
67 // but does run into problems, such as when attempting to cast the resulting
68 // pointer to a uint32_t.  The standard C macro, offsetof() can help.
69 //
70 // Use AM_REGADDR() for single-module peripherals.
71 // Use AM_REGADDRn() for multi-module peripherals (e.g. IOM, UART).
72 //
73 //*****************************************************************************
74 #define AM_REGADDR(periph, reg) ( periph##_BASE + offsetof(periph##_Type, reg) )
75 
76 #define AM_REGADDRn(periph, n, reg) ( periph##0_BASE                    +   \
77                                       offsetof(periph##0_Type, reg)     +   \
78                                       (n * (periph##1_BASE - periph##0_BASE)) )
79 
80 //*****************************************************************************
81 //
82 // Critical section assembly macros
83 //
84 // These macros call functions that implement critical section protection using
85 // inline assembly for various compilers.  They are intended to be used in other
86 // register macros or directly in sections of code.
87 //
88 // Important usage note: These macros create a local scope and therefore MUST
89 // be used in pairs.
90 //
91 //*****************************************************************************
92 #define AM_CRITICAL_BEGIN                                                   \
93     if ( 1 )                                                                \
94     {                                                                       \
95         volatile uint32_t ui32Primask_04172010;                             \
96         ui32Primask_04172010 = am_hal_interrupt_master_disable();
97 
98 #define AM_CRITICAL_END                                                     \
99         am_hal_interrupt_master_set(ui32Primask_04172010);                  \
100     }
101 
102 //*****************************************************************************
103 //
104 // Compiler-specific macros.
105 //
106 // Macros that accomplish compiler-specific tasks.
107 // For example, suppression of certain compiler warnings.
108 //
109 //*****************************************************************************
110 #if defined(__IAR_SYSTEMS_ICC__)
111 /* Suppress IAR compiler warning about volatile ordering  */
112 #define DIAG_SUPPRESS_VOLATILE_ORDER()  _Pragma("diag_suppress=Pa082")
113 /* Restore IAR compiler warning to default */
114 #define DIAG_DEFAULT_VOLATILE_ORDER()   _Pragma("diag_default=Pa082")
115 #else
116 #define DIAG_SUPPRESS_VOLATILE_ORDER()
117 #define DIAG_DEFAULT_VOLATILE_ORDER()
118 #endif
119 
120 //
121 // The intrinsic for IAR's CLZ instruction is different than other compilers.
122 //
123 #ifdef __IAR_SYSTEMS_ICC__
124 #define AM_ASM_CLZ(ui32val)     __CLZ(ui32val)
125 #else
126 #define AM_ASM_CLZ(ui32val)     __builtin_clz(ui32val)
127 #endif
128 
129 
130 #ifdef __cplusplus
131 }
132 #endif
133 
134 #endif // AM_REG_MACROS_H
135 
136