am_reg_macros.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. //*****************************************************************************
  2. //
  3. // am_reg_macros.h
  4. //! @file
  5. //!
  6. //! @brief Helper macros for using hardware registers.
  7. //
  8. //*****************************************************************************
  9. //*****************************************************************************
  10. //
  11. // Copyright (c) 2017, Ambiq Micro
  12. // All rights reserved.
  13. //
  14. // Redistribution and use in source and binary forms, with or without
  15. // modification, are permitted provided that the following conditions are met:
  16. //
  17. // 1. Redistributions of source code must retain the above copyright notice,
  18. // this list of conditions and the following disclaimer.
  19. //
  20. // 2. Redistributions in binary form must reproduce the above copyright
  21. // notice, this list of conditions and the following disclaimer in the
  22. // documentation and/or other materials provided with the distribution.
  23. //
  24. // 3. Neither the name of the copyright holder nor the names of its
  25. // contributors may be used to endorse or promote products derived from this
  26. // software without specific prior written permission.
  27. //
  28. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  29. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  32. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  34. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  35. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  37. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. // POSSIBILITY OF SUCH DAMAGE.
  39. //
  40. // This is part of revision 1.2.11 of the AmbiqSuite Development Package.
  41. //
  42. //*****************************************************************************
  43. #ifndef AM_REG_MACROS_H
  44. #define AM_REG_MACROS_H
  45. #ifdef __cplusplus
  46. extern "C"
  47. {
  48. #endif
  49. //*****************************************************************************
  50. //
  51. // Include the inline assembly macros.
  52. //
  53. //*****************************************************************************
  54. #include "am_reg_macros_asm.h"
  55. //*****************************************************************************
  56. //
  57. // High-level Helper Macros.
  58. //
  59. // Usage:
  60. //
  61. // For direct 32-bit access to a register, use AM_REGVAL:
  62. // AM_REGVAL(REG_VCOMP_BASEADDR + AM_VCOMP_VCMPCFG_O) |= 0xDEADBEEF;
  63. //
  64. // The AM_REG macro can also be used as a shorthand version of AM_REGVAL:
  65. // AM_REG(VCOMP, VCMPCFG) |= 0xDEADBEEF;
  66. //
  67. // The AM_REGn macro is used for accessing registers of peripherals with
  68. // multiple instances, such as IOMSTR.
  69. // AM_REGn(IOMSTR, 1, CLKCFG) |= 0xDEADBEEF;
  70. //
  71. // To write to a specific bitfield within a register, use AM_BFW or AM_BFWn:
  72. // AM_BFW(CTIMER, 0, CTCTRL0, TMRB0FN, 0x3);
  73. //
  74. // To read a field, use AM_BFR or AM_BFRn:
  75. // ui32Timer0Fn = AM_BFR((CTIMER, 0, CTCTRL0, TMRB0FN);
  76. //
  77. // Note:
  78. //
  79. // AM_REGn, AM_BFW and AM_BFR are concatenation-based, which means that
  80. // standalone macro definitions should not be used for the 'module', 'reg', and
  81. // 'field' arguments.All macro names in the various peripheral header files are
  82. // written in one of the following forms:
  83. // - AM_REG_##module_reg_O
  84. // - AM_REG_##module_reg_field_S
  85. // - AM_REG_##module_reg_field_M
  86. //
  87. // The "module", "reg" and "field" fragments may be used as valid arguments to
  88. // the AM_REGn, AM_BFW, and AM_BFR macros, all of which are able to perform the
  89. // necessary concatenation operations to reconstruct the full macros and look
  90. // up the appropriate base address for the instance number given. For
  91. // peripherals with only one instance, use instance number 0.
  92. //
  93. // The AM_REGVAL macro does not perform any concatenation operations, so the
  94. // complete macro name (including any suffix) must be specified.
  95. //
  96. //*****************************************************************************
  97. #define AM_REGVAL(x) (*((volatile uint32_t *)(x)))
  98. #define AM_REGVAL_FLOAT(x) (*((volatile float *)(x)))
  99. //*****************************************************************************
  100. //
  101. // Register access macros for single-instance modules
  102. // AM_REG - Write a register of a module.
  103. // AM_BFW - Write a value to a bitfield of a register.
  104. // AM_BFWe - Use a defined enum value to write a value to a register bitfield.
  105. // AM_BFR - Read a bitfield value from a register.
  106. // AM_BFM - Read and mask a bitfield from a register, but leave the value in
  107. // its bit position. Useful for comparing with enums.
  108. //
  109. // AM_BFV - Move a value to a bitfield. This macro is used for creating a
  110. // value, it does not modify any register.
  111. // AM_BFX - Extract the value of a bitfield from a 32-bit value, such as that
  112. // read from a register. Does not read or modify any register.
  113. //
  114. //*****************************************************************************
  115. #define AM_REG(module, reg) \
  116. AM_REGn(module, 0, reg)
  117. #define AM_BFW(module, reg, field, value) \
  118. AM_BFWn(module, 0, reg, field, value)
  119. #define AM_BFWe(module, reg, field, enumval) \
  120. AM_BFWen(module, 0, reg, field, enumval)
  121. #define AM_BFR(module, reg, field) \
  122. AM_BFRn(module, 0, reg, field)
  123. #define AM_BFM(module, reg, field) \
  124. AM_BFMn(module, 0, reg, field)
  125. #define AM_BFV(module, reg, field, value) \
  126. (((uint32_t)(value) << AM_REG_##module##_##reg##_##field##_S) & \
  127. AM_REG_##module##_##reg##_##field##_M)
  128. #define AM_BFX(module, reg, field, value) \
  129. (((uint32_t)(value) & AM_REG_##module##_##reg##_##field##_M) >> \
  130. AM_REG_##module##_##reg##_##field##_S)
  131. //*****************************************************************************
  132. //
  133. // Register access macros for multi-instance modules
  134. // AM_REGADDRn - Calc the register address inside a multiple instance module.
  135. // AM_REGn - Write a register of a multiple instance module.
  136. // AM_BFWn - Write a value to a bitfield of a register in a multiple instance.
  137. // AM_BFWen - Use a defined enum value to write a value to a bitfield of a
  138. // register in a multiple instance.
  139. // AM_BFRn - Read a bitfield value from a register in a multiple instance.
  140. // AM_BFMn - Read and mask a bitfield, but leave the value in its bit position.
  141. // (Useful for comparing with enums.)
  142. //
  143. //*****************************************************************************
  144. #define AM_REGADDRn(module, instance, reg) \
  145. (AM_REG_##module##n(instance) + AM_REG_##module##_##reg##_O)
  146. #define AM_REGn(module, instance, reg) \
  147. AM_REGVAL(AM_REG_##module##n(instance) + AM_REG_##module##_##reg##_O)
  148. #define AM_BFWn(module, instance, reg, field, value) \
  149. AM_REGn(module, instance, reg) = \
  150. (AM_BFV(module, reg, field, value) | \
  151. (AM_REGn(module, instance, reg) & \
  152. (~AM_REG_##module##_##reg##_##field##_M)))
  153. #define AM_BFWen(module, instance, reg, field, enumval) \
  154. AM_REGn(module, instance, reg) = \
  155. (AM_REG_##module##_##reg##_##field##_##enumval | \
  156. (AM_REGn(module, instance, reg) & \
  157. (~AM_REG_##module##_##reg##_##field##_M)))
  158. #define AM_BFRn(module, instance, reg, field) \
  159. AM_BFX(module, reg, field, AM_REGn(module, instance, reg))
  160. #define AM_BFMn(module, instance, reg, field) \
  161. (AM_REGn(module, instance, reg) & AM_REG_##module##_##reg##_##field##_M)
  162. //*****************************************************************************
  163. //
  164. // "Atomic" register access macros - use when a read-modify-write is required.
  165. //
  166. // These macros will be slower than the normal macros, but they will also
  167. // guarantee threadsafe hardware access.
  168. //
  169. // These macros require a nesting-friendly critical section implementation. If
  170. // you are using the HAL, you can use the default definitions below. If not,
  171. // you will need to supply your own.
  172. //
  173. // Atomic register access macros usage:
  174. // AM_REGa - Write a register of a single instance module. Provide operator
  175. // (&,|,etc) to perform that operation on the reg using value, or
  176. // no operator to simply write the value atomically.
  177. // AM_REGa_SET - Set bits in a single instance module according to the mask.
  178. // AM_REGa_CLR - Clear bits in a single instance module according to the mask.
  179. // AM_REGan - Multiple module version of AM_REGa.
  180. // AM_REGan_SET - Multiple instance version of AM_REGa_SET.
  181. // AM_REGan_CLR - Multiple instance version of AM_REGa_CLR.
  182. // AM_BFWa - Write a value to a register bitfield.
  183. // AM_BFWae - Use a defined enum value to write a value to a bitfield.
  184. // AM_BFWan - Write a value to a bitfield of a register in a multiple instance.
  185. // AM_BFWaen - Use a defined enum value to write a value to a bitfield of a
  186. // register in a multiple instance.
  187. //
  188. //*****************************************************************************
  189. #ifndef AM_CRITICAL_BEGIN
  190. #define AM_CRITICAL_BEGIN uint32_t ui32Primask = am_hal_interrupt_master_disable()
  191. #define AM_CRITICAL_END am_hal_interrupt_master_set(ui32Primask)
  192. #endif
  193. #define AM_REGan(module, instance, reg, operator, value) \
  194. AM_CRITICAL_BEGIN_ASM \
  195. AM_REGn(module, instance, reg) operator##= (value); \
  196. AM_CRITICAL_END_ASM
  197. #define AM_REGan_SET(module, instance, reg, mask) \
  198. AM_CRITICAL_BEGIN_ASM \
  199. AM_REGn(module, instance, reg) |= (mask); \
  200. AM_CRITICAL_END_ASM
  201. #define AM_REGan_CLR(module, instance, reg, mask) \
  202. AM_CRITICAL_BEGIN_ASM \
  203. AM_REGn(module, instance, reg) &= (~mask); \
  204. AM_CRITICAL_END_ASM
  205. #define AM_REGa(module, reg, operator, value) \
  206. AM_REGan(module, 0, reg, operator, value)
  207. #define AM_REGa_CLR(module, reg, mask) \
  208. AM_REGan_CLR(module, 0, reg, mask)
  209. #define AM_REGa_SET(module, reg, mask) \
  210. AM_REGan_SET(module, 0, reg, mask)
  211. #define AM_BFWa(module, reg, field, value) \
  212. AM_CRITICAL_BEGIN_ASM \
  213. AM_BFW(module, reg, field, value); \
  214. AM_CRITICAL_END_ASM
  215. #define AM_BFWae(module, reg, field, enumval) \
  216. AM_CRITICAL_BEGIN_ASM \
  217. AM_BFWe(module, reg, field, enumval); \
  218. AM_CRITICAL_END_ASM
  219. #define AM_BFWan(module, instance, reg, field, value) \
  220. AM_CRITICAL_BEGIN_ASM \
  221. AM_BFWn(module, instance, reg, field, enumval); \
  222. AM_CRITICAL_END_ASM
  223. #define AM_BFWaen(module, instance, reg, field, enumval) \
  224. AM_CRITICAL_BEGIN_ASM \
  225. AM_BFWen(module, instance reg, field, enumval); \
  226. AM_CRITICAL_END_ASM
  227. //*****************************************************************************
  228. //
  229. // Other helper Macros.
  230. //
  231. // Note: These macros make use of macro concatenation, so the '_S' or '_M'
  232. // suffix on a register bitfield macro should not be supplied by the user.
  233. // The macro will apply each suffix as needed.
  234. //
  235. //*****************************************************************************
  236. //
  237. // AM_ENUMX extracts a register bitfield enumeration to the bit 0 position,
  238. // which makes it possible to use enums directly with existing macros such
  239. // as AM_BFR() or AM_BFW().
  240. // Brief overview: bitfield enumerations are pre-shifted such that the defined
  241. // value lines up with the bitfield. This is convenient for many operations,
  242. // but not so when using AM_BFR() to read the value of a register bitfield
  243. // as AM_BFR() shifts the bitfield value to the bit 0 position.
  244. // Note that this type of bitfield extraction is Cortex efficient via the
  245. // UBFX (unsigned bit field extract) instruction.
  246. //
  247. // Alternately, AM_BFM() can also be used. AM_BFM() reads a register and masks
  248. // the bitfield value (without shifting), thereby allowing direct comparison
  249. // with a defined enum.
  250. //
  251. // Examples:
  252. // if ( AM_BFR(CLKGEN, CCTRL, CORESEL) ==
  253. // AM_ENUMX(CLKGEN, CCTRL, CORESEL, HFRC) )
  254. //
  255. // or alternatively:
  256. // if ( AM_BFM(CLKGEN, CCTRL, CORESEL) == AM_REG_CLKGEN_CCTRL_CORESEL_HFRC )
  257. //
  258. #define AM_ENUMX(module, reg, field, enumname) \
  259. ((AM_REG_##module##_##reg##_##field##_##enumname) >> \
  260. (AM_REG_##module##_##reg##_##field##_S))
  261. //
  262. // AM_WRITE_SM performs a shift/mask operation to prepare the value 'x' to be
  263. // written to the register field 'field'.
  264. //
  265. // For example:
  266. // AM_REGVAL(ui32Base + AM_VCOMP_VCMP_CFG_O) |=
  267. // AM_WRITE_SM(AM_VCOMP_VCMP_CFG_LVLSEL, ui32Value);
  268. //
  269. #define AM_WRITE_SM(field, x) (((x) << field##_S) & field##_M)
  270. //
  271. // AM_READ_SM performs a shift/mask operation to make it easier to interpret
  272. // the value of a given bitfield. This is essentially the reverse of the
  273. // AM_WRITE_SM operation. In most cases, you will want to use the shorter
  274. // AM_BFR macro instead of this one.
  275. //
  276. // For example:
  277. // ui32Value = AM_READ_SM(AM_VCOMP_VCMP_CFG_NSEL,
  278. // AM_REGVAL(ui32Base + AM_VCOMP_VCMP_CFG_O));
  279. //
  280. #define AM_READ_SM(field, x) (((x) & field##_M) >> field##_S)
  281. #ifdef __cplusplus
  282. }
  283. #endif
  284. #endif // AM_REG_MACROS_H