am_hal_gpio.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. //*****************************************************************************
  2. //
  3. // am_hal_gpio.h
  4. //! @file
  5. //!
  6. //! @brief Functions for accessing and configuring the GPIO module.
  7. //!
  8. //! @addtogroup gpio2 GPIO
  9. //! @ingroup apollo2hal
  10. //! @{
  11. //
  12. //*****************************************************************************
  13. //*****************************************************************************
  14. //
  15. // Copyright (c) 2017, Ambiq Micro
  16. // All rights reserved.
  17. //
  18. // Redistribution and use in source and binary forms, with or without
  19. // modification, are permitted provided that the following conditions are met:
  20. //
  21. // 1. Redistributions of source code must retain the above copyright notice,
  22. // this list of conditions and the following disclaimer.
  23. //
  24. // 2. Redistributions in binary form must reproduce the above copyright
  25. // notice, this list of conditions and the following disclaimer in the
  26. // documentation and/or other materials provided with the distribution.
  27. //
  28. // 3. Neither the name of the copyright holder nor the names of its
  29. // contributors may be used to endorse or promote products derived from this
  30. // software without specific prior written permission.
  31. //
  32. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  33. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  34. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  36. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  37. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  38. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  39. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  40. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  42. // POSSIBILITY OF SUCH DAMAGE.
  43. //
  44. // This is part of revision 1.2.11 of the AmbiqSuite Development Package.
  45. //
  46. //*****************************************************************************
  47. #ifndef AM_HAL_GPIO_H
  48. #define AM_HAL_GPIO_H
  49. // DEVICE ADDRESS IS 8-bits
  50. #define AM_HAL_GPIO_DEV_ADDR_8 (0)
  51. // DEVICE ADDRESS IS 16-bits
  52. #define AM_HAL_GPIO_DEV_ADDR_16 (1)
  53. // DEVICE OFFSET IS 8-bits
  54. #define AM_HAL_GPIO_DEV_OFFSET_8 (0x00000000)
  55. // DEVICE OFFSET IS 16-bits
  56. #define AM_HAL_GPIO_DEV_OFFSET_16 (0x00010000)
  57. // Maximum number of GPIOs on this device
  58. #define AM_HAL_GPIO_MAX_PADS (50)
  59. //*****************************************************************************
  60. //
  61. //! @name GPIO Pin defines
  62. //! @brief GPIO Pin defines for use with interrupt functions
  63. //!
  64. //! These macros may be used to with \e am_hal_gpio_int_x().
  65. //!
  66. //! @{
  67. //
  68. //*****************************************************************************
  69. #define AM_HAL_GPIO_BIT(n) (((uint64_t) 0x1) << n)
  70. //! @}
  71. //
  72. // Helper macros used for unraveling the GPIO configuration value (configval).
  73. //
  74. // Note that the configval, which is passed into functions such as
  75. // am_hal_gpio_pin_config() as well as various helper macros, is a concatenated
  76. // value that contains values used in multiple configuration registers.
  77. //
  78. // The GPIO configuration value fields are arranged as follows:
  79. // [ 7: 0] PADREG configuration.
  80. // [11: 8] GPIOCFG
  81. // [15:12] Unused.
  82. // [23:16] ALTPADREG configuration.
  83. //
  84. // Define macros describing these configval fields.
  85. //
  86. #define CFGVAL_PADREG_S 0
  87. #define CFGVAL_PADREG_M (0xFF << CFGVAL_PADREG_S)
  88. #define CFGVAL_GPIOCFG_S 8
  89. #define CFGVAL_GPIOCFG_M (0x0F << CFGVAL_GPIOCFG_S)
  90. #define CFGVAL_ALTPAD_S 16
  91. #define CFGVAL_ALTPAD_M (0xFF << CFGVAL_ALTPAD_S)
  92. //
  93. // Extraction macros
  94. //
  95. #define CFGVAL_PADREG_X(x) (((uint32_t)(x) & CFGVAL_PADREG_M) >> \
  96. CFGVAL_PADREG_S)
  97. #define CFGVAL_GPIOCFG_X(x) (((uint32_t)(x) & CFGVAL_GPIOCFG_M) >> \
  98. CFGVAL_GPIOCFG_S)
  99. #define CFGVAL_ALTPAD_X(x) (((uint32_t)(x) & CFGVAL_ALTPAD_M) >> \
  100. CFGVAL_ALTPAD_S)
  101. //*****************************************************************************
  102. //
  103. // Input options.
  104. //
  105. //*****************************************************************************
  106. #define AM_HAL_GPIO_INPEN (0x02 << CFGVAL_PADREG_S) // Enable input transistors.
  107. #define AM_HAL_GPIO_INCFG_RDZERO (0x01 << CFGVAL_GPIOCFG_S) // Disable input read registers.
  108. //*****************************************************************************
  109. //
  110. // Output options (OUTCFG)
  111. //
  112. //*****************************************************************************
  113. #define AM_HAL_GPIO_OUT_DISABLE ((0x0 << 1) << CFGVAL_GPIOCFG_S)
  114. #define AM_HAL_GPIO_OUT_PUSHPULL ((0x1 << 1) << CFGVAL_GPIOCFG_S)
  115. #define AM_HAL_GPIO_OUT_OPENDRAIN ((0x2 << 1) << CFGVAL_GPIOCFG_S)
  116. #define AM_HAL_GPIO_OUT_3STATE ((0x3 << 1) << CFGVAL_GPIOCFG_S)
  117. //*****************************************************************************
  118. //
  119. // Special options for IOM0 and IOM4 clocks.
  120. // For 24MHz operation, a special enable must be selected. The 24MHZ select is
  121. // selected via bit0 of OUTCFG (which is, in a way,an alias of OUT_PUSHPULL).
  122. //
  123. //*****************************************************************************
  124. #define AM_HAL_GPIO_24MHZ_ENABLE ((0x1 << 1) << CFGVAL_GPIOCFG_S)
  125. //*****************************************************************************
  126. //
  127. // Pad configuration options.
  128. // (Configuration value bits 7:0.)
  129. //
  130. //*****************************************************************************
  131. #define AM_HAL_GPIO_HIGH_DRIVE (0x04 << CFGVAL_PADREG_S)
  132. #define AM_HAL_GPIO_LOW_DRIVE (0x00 << CFGVAL_PADREG_S)
  133. #define AM_HAL_GPIO_PULLUP (0x01 << CFGVAL_PADREG_S)
  134. #define AM_HAL_GPIO_PULL1_5K ( (0x01 << CFGVAL_PADREG_S) | \
  135. AM_HAL_GPIO_PULLUP )
  136. #define AM_HAL_GPIO_PULL6K ( (0x40 << CFGVAL_PADREG_S) | \
  137. AM_HAL_GPIO_PULLUP )
  138. #define AM_HAL_GPIO_PULL12K ( (0x80 << CFGVAL_PADREG_S) | \
  139. AM_HAL_GPIO_PULLUP )
  140. #define AM_HAL_GPIO_PULL24K ( (0xC0 << CFGVAL_PADREG_S) | \
  141. AM_HAL_GPIO_PULLUP )
  142. // POWER SWITCH is available on selected pins
  143. #define AM_HAL_GPIO_POWER (0x80 << CFGVAL_PADREG_S)
  144. //*****************************************************************************
  145. //
  146. //! ALTPADREG configuration options.
  147. //! (Configuration value bits 23:16.)
  148. //!
  149. //! All Apollo2 GPIO pins can be configured for 2mA or 4mA.
  150. //! AM_HAL_GPIO_DRIVE_2MA = 2mA configuration.
  151. //! AM_HAL_GPIO_DRIVE_4MA = 4mA configuration.
  152. //!
  153. //! Certain Apollo2 GPIO pins can be configured to drive up to 12mA.
  154. //! AM_HAL_GPIO_DRIVE_8MA = 8mA configuration.
  155. //! AM_HAL_GPIO_DRIVE_12MA = 12mA configuration.
  156. //!
  157. //! Notes:
  158. //! - Always consult the Apollo2 data sheet for the latest details.
  159. //! - The higher drive GPIOxx pads generally include:
  160. //! 0-2,5,7-8,10,12-13,22-23,26-29,38-39,42,44-48.
  161. //! - GPIOxx pads that do not support the higher drive:
  162. //! 3-4,6,9,11,14-21,24-25,30-37,40-41,43,49.
  163. //! - User is responsible for ensuring that the selected pin actually supports
  164. //! the higher drive (8mA or 12mA) capabilities. See the Apollo2 data sheet.
  165. //! - Attempting to set the higher drive (8mA or 12mA) configuration on a
  166. //! non-supporting pad will actually set the pad for 4mA drive strength,
  167. //! regardless of the lower bit setting.
  168. //
  169. //*****************************************************************************
  170. #define AM_HAL_GPIO_DRIVE_2MA ( 0 )
  171. #define AM_HAL_GPIO_DRIVE_4MA AM_HAL_GPIO_HIGH_DRIVE
  172. #define AM_HAL_GPIO_DRIVE_8MA ( 0x01 << CFGVAL_ALTPAD_S )
  173. #define AM_HAL_GPIO_DRIVE_12MA ( (0x01 << CFGVAL_ALTPAD_S) | \
  174. AM_HAL_GPIO_HIGH_DRIVE )
  175. #define AM_HAL_GPIO_SLEWRATE ( 0x10 << CFGVAL_ALTPAD_S )
  176. //*****************************************************************************
  177. //
  178. // Interrupt polarity
  179. // These values can be used directly in the configval.
  180. //
  181. //*****************************************************************************
  182. #define AM_HAL_GPIOCFGVAL_FALLING ((1 << 2) << CFGVAL_GPIOCFG_S)
  183. #define AM_HAL_GPIOCFGVAL_RISING ((0 << 2) << CFGVAL_GPIOCFG_S)
  184. //*****************************************************************************
  185. //
  186. // Pad function select
  187. // This macro represents the 3 bit function select field in the PADREG byte.
  188. //
  189. //*****************************************************************************
  190. #define AM_HAL_GPIO_FUNC(x) ((x & 0x7) << 3)
  191. //*****************************************************************************
  192. //
  193. //! Interrupt polarity
  194. //!
  195. //! Important:
  196. //! These values are to be used with am_hal_gpio_int_polarity_bit_set().
  197. // They are not intended to be used as part of the GPIO configval.
  198. //
  199. //*****************************************************************************
  200. #define AM_HAL_GPIO_FALLING 0x00000001
  201. #define AM_HAL_GPIO_RISING 0x00000000
  202. //*****************************************************************************
  203. //
  204. // A few common pin configurations.
  205. //
  206. //*****************************************************************************
  207. #define AM_HAL_GPIO_DISABLE \
  208. (AM_HAL_GPIO_FUNC(3))
  209. #define AM_HAL_GPIO_INPUT \
  210. (AM_HAL_GPIO_FUNC(3) | AM_HAL_GPIO_INPEN)
  211. #define AM_HAL_GPIO_OUTPUT \
  212. (AM_HAL_GPIO_FUNC(3) | AM_HAL_GPIO_OUT_PUSHPULL)
  213. #define AM_HAL_GPIO_OPENDRAIN \
  214. (AM_HAL_GPIO_FUNC(3) | AM_HAL_GPIO_OUT_OPENDRAIN | AM_HAL_GPIO_INPEN)
  215. #define AM_HAL_GPIO_3STATE \
  216. (AM_HAL_GPIO_FUNC(3) | AM_HAL_GPIO_OUT_3STATE)
  217. //*****************************************************************************
  218. //
  219. // PADREG helper macros.
  220. //
  221. //*****************************************************************************
  222. #define AM_HAL_GPIO_PADREG(n) \
  223. (AM_REG_GPIOn(0) + AM_REG_GPIO_PADREGA_O + (n & 0xFC))
  224. #define AM_HAL_GPIO_PADREG_S(n) \
  225. (((uint32_t)(n) % 4) << 3)
  226. #define AM_HAL_GPIO_PADREG_M(n) \
  227. ((uint32_t) 0xFF << AM_HAL_GPIO_PADREG_S(n))
  228. #define AM_HAL_GPIO_PADREG_FIELD(n, configval) \
  229. (((uint32_t)(configval) & CFGVAL_PADREG_M) << AM_HAL_GPIO_PADREG_S(n))
  230. #define AM_HAL_GPIO_PADREG_W(n, configval) \
  231. AM_REGVAL(AM_HAL_GPIO_PADREG(n)) = \
  232. (AM_HAL_GPIO_PADREG_FIELD(n, configval) | \
  233. (AM_REGVAL(AM_HAL_GPIO_PADREG(n)) & ~AM_HAL_GPIO_PADREG_M(n)))
  234. #define AM_HAL_GPIO_PADREG_R(n) \
  235. ((AM_REGVAL(AM_HAL_GPIO_PADREG(n)) & AM_HAL_GPIO_PADREG_M(n)) >> \
  236. AM_HAL_GPIO_PADREG_S(n))
  237. //*****************************************************************************
  238. //
  239. // ALTPADCFG helper macros.
  240. // The ALTPADCFG bits are located in [23:16] of the configval.
  241. //
  242. //*****************************************************************************
  243. #define AM_HAL_GPIO_ALTPADREG(n) \
  244. (AM_REG_GPIOn(0) + AM_REG_GPIO_ALTPADCFGA_O + (n & 0xFC))
  245. #define AM_HAL_GPIO_ALTPADREG_S(n) \
  246. (((uint32_t)(n) % 4) << 3)
  247. #define AM_HAL_GPIO_ALTPADREG_M(n) \
  248. ((uint32_t) 0xFF << AM_HAL_GPIO_ALTPADREG_S(n))
  249. #define AM_HAL_GPIO_ALTPADREG_FIELD(n, configval) \
  250. (CFGVAL_ALTPAD_X(configval) << AM_HAL_GPIO_ALTPADREG_S(n))
  251. #define AM_HAL_GPIO_ALTPADREG_W(n, configval) \
  252. AM_REGVAL(AM_HAL_GPIO_ALTPADREG(n)) = \
  253. (AM_HAL_GPIO_ALTPADREG_FIELD(n, configval) | \
  254. (AM_REGVAL(AM_HAL_GPIO_ALTPADREG(n)) & ~AM_HAL_GPIO_ALTPADREG_M(n)))
  255. #define AM_HAL_GPIO_ALTPADREG_R(n) \
  256. ((AM_REGVAL(AM_HAL_GPIO_ALTPADREG(n)) & AM_HAL_GPIO_ALTPADREG_M(n)) >> \
  257. AM_HAL_GPIO_ALTPADREG_S(n))
  258. //*****************************************************************************
  259. //
  260. // CFG helper macros.
  261. //
  262. //*****************************************************************************
  263. #define AM_HAL_GPIO_CFG(n) \
  264. (AM_REG_GPIOn(0) + AM_REG_GPIO_CFGA_O + ((n & 0xF8) >> 1))
  265. #define AM_HAL_GPIO_CFG_S(n) \
  266. (((uint32_t)(n) % 8) << 2)
  267. #define AM_HAL_GPIO_CFG_M(n) \
  268. ((uint32_t) 0x7 << AM_HAL_GPIO_CFG_S(n))
  269. #define AM_HAL_GPIO_CFG_FIELD(n, configval) \
  270. ((((uint32_t)(configval) & 0x700) >> 8) << AM_HAL_GPIO_CFG_S(n))
  271. #define AM_HAL_GPIO_CFG_W(n, configval) \
  272. AM_REGVAL(AM_HAL_GPIO_CFG(n)) = \
  273. (AM_HAL_GPIO_CFG_FIELD(n, configval) | \
  274. (AM_REGVAL(AM_HAL_GPIO_CFG(n)) & ~AM_HAL_GPIO_CFG_M(n)))
  275. #define AM_HAL_GPIO_CFG_R(n) \
  276. ((AM_REGVAL(AM_HAL_GPIO_CFG(n)) & AM_HAL_GPIO_CFG_M(n)) >> \
  277. AM_HAL_GPIO_CFG_S(n))
  278. //*****************************************************************************
  279. //
  280. // Polarity helper macros.
  281. //
  282. //*****************************************************************************
  283. #define AM_HAL_GPIO_POL_S(n) \
  284. ((((uint32_t)(n) % 8) << 2) + 3)
  285. #define AM_HAL_GPIO_POL_M(n) \
  286. ((uint32_t) 0x1 << AM_HAL_GPIO_POL_S(n))
  287. #define AM_HAL_GPIO_POL_FIELD(n, polarity) \
  288. (((uint32_t)(polarity) & 0x1) << AM_HAL_GPIO_POL_S(n))
  289. #define AM_HAL_GPIO_POL_W(n, polarity) \
  290. AM_REGVAL(AM_HAL_GPIO_CFG(n)) = \
  291. (AM_HAL_GPIO_POL_FIELD(n, polarity) | \
  292. (AM_REGVAL(AM_HAL_GPIO_CFG(n)) & ~AM_HAL_GPIO_POL_M(n)))
  293. //*****************************************************************************
  294. //
  295. // RD helper macros.
  296. //
  297. //*****************************************************************************
  298. #define AM_HAL_GPIO_RD_REG(n) \
  299. (AM_REG_GPIOn(0) + AM_REG_GPIO_RDA_O + (((uint32_t)(n) & 0x20) >> 3))
  300. #define AM_HAL_GPIO_RD_S(n) \
  301. ((uint32_t)(n) % 32)
  302. #define AM_HAL_GPIO_RD_M(n) \
  303. ((uint32_t) 0x1 << AM_HAL_GPIO_RD_S(n))
  304. #define AM_HAL_GPIO_RD(n) \
  305. AM_REGVAL(AM_HAL_GPIO_RD_REG(n))
  306. //*****************************************************************************
  307. //
  308. // WT helper macros.
  309. //
  310. //*****************************************************************************
  311. #define AM_HAL_GPIO_WT_REG(n) \
  312. (AM_REG_GPIOn(0) + AM_REG_GPIO_WTA_O + (((uint32_t)(n) & 0x20) >> 3))
  313. #define AM_HAL_GPIO_WT_S(n) \
  314. ((uint32_t)(n) % 32)
  315. #define AM_HAL_GPIO_WT_M(n) \
  316. ((uint32_t) 0x1 << AM_HAL_GPIO_WT_S(n))
  317. #define AM_HAL_GPIO_WT(n) \
  318. AM_REGVAL(AM_HAL_GPIO_WT_REG(n))
  319. //*****************************************************************************
  320. //
  321. // WTS helper macros.
  322. //
  323. //*****************************************************************************
  324. #define AM_HAL_GPIO_WTS_REG(n) \
  325. (AM_REG_GPIOn(0) + AM_REG_GPIO_WTSA_O + (((uint32_t)(n) & 0x20) >> 3))
  326. #define AM_HAL_GPIO_WTS_S(n) \
  327. ((uint32_t)(n) % 32)
  328. #define AM_HAL_GPIO_WTS_M(n) \
  329. ((uint32_t) 0x1 << AM_HAL_GPIO_WTS_S(n))
  330. #define AM_HAL_GPIO_WTS(n) \
  331. AM_REGVAL(AM_HAL_GPIO_WTS_REG(n))
  332. //*****************************************************************************
  333. //
  334. // WTC helper macros.
  335. //
  336. //*****************************************************************************
  337. #define AM_HAL_GPIO_WTC_REG(n) \
  338. (AM_REG_GPIOn(0) + AM_REG_GPIO_WTCA_O + (((uint32_t)(n) & 0x20) >> 3))
  339. #define AM_HAL_GPIO_WTC_S(n) \
  340. ((uint32_t)(n) % 32)
  341. #define AM_HAL_GPIO_WTC_M(n) \
  342. ((uint32_t) 0x1 << AM_HAL_GPIO_WTC_S(n))
  343. #define AM_HAL_GPIO_WTC(n) \
  344. AM_REGVAL(AM_HAL_GPIO_WTC_REG(n))
  345. //*****************************************************************************
  346. //
  347. // EN helper macros.
  348. //
  349. //*****************************************************************************
  350. #define AM_HAL_GPIO_EN_REG(n) \
  351. (AM_REG_GPIOn(0) + AM_REG_GPIO_ENA_O + (((uint32_t)(n) & 0x20) >> 3))
  352. #define AM_HAL_GPIO_EN_S(n) \
  353. ((uint32_t)(n) % 32)
  354. #define AM_HAL_GPIO_EN_M(n) \
  355. ((uint32_t) 0x1 << AM_HAL_GPIO_EN_S(n))
  356. #define AM_HAL_GPIO_EN(n) \
  357. AM_REGVAL(AM_HAL_GPIO_EN_REG(n))
  358. //*****************************************************************************
  359. //
  360. // ENS helper macros.
  361. //
  362. //*****************************************************************************
  363. #define AM_HAL_GPIO_ENS_REG(n) \
  364. (AM_REG_GPIOn(0) + AM_REG_GPIO_ENSA_O + (((uint32_t)(n) & 0x20) >> 3))
  365. #define AM_HAL_GPIO_ENS_S(n) \
  366. ((uint32_t)(n) % 32)
  367. #define AM_HAL_GPIO_ENS_M(n) \
  368. ((uint32_t) 0x1 << AM_HAL_GPIO_ENS_S(n))
  369. #define AM_HAL_GPIO_ENS(n) \
  370. AM_REGVAL(AM_HAL_GPIO_ENS_REG(n))
  371. //*****************************************************************************
  372. //
  373. // ENC helper macros.
  374. //
  375. //*****************************************************************************
  376. #define AM_HAL_GPIO_ENC_REG(n) \
  377. (AM_REG_GPIOn(0) + AM_REG_GPIO_ENCA_O + (((uint32_t)(n) & 0x20) >> 3))
  378. #define AM_HAL_GPIO_ENC_S(n) \
  379. ((uint32_t)(n) % 32)
  380. #define AM_HAL_GPIO_ENC_M(n) \
  381. ((uint32_t) 0x1 << AM_HAL_GPIO_ENC_S(n))
  382. #define AM_HAL_GPIO_ENC(n) \
  383. AM_REGVAL(AM_HAL_GPIO_ENC_REG(n))
  384. //*****************************************************************************
  385. //
  386. //! @brief Configure the GPIO PAD MUX & GPIO PIN Configurations
  387. //!
  388. //! @param ui32PinNumber - GPIO pin number.
  389. //! @param ui32Config - Configuration options.
  390. //!
  391. //! This function applies the settings for a single GPIO. For a list of valid
  392. //! options please see the top of this file (am_hal_gpio.h) and am_hal_pin.h.
  393. //!
  394. //! Usage examples:
  395. //! am_hal_gpio_pin_config(11, AM_HAL_GPIO_INPUT);
  396. //! am_hal_gpio_pin_config(10, AM_HAL_GPIO_OUTPUT);
  397. //! am_hal_gpio_pin_config(14, AM_HAL_GPIO_OUTPUT | AM_HAL_GPIO_SLEWRATE);
  398. //! am_hal_gpio_pin_config(15, AM_HAL_GPIO_OUTPUT | AM_HAL_GPIO_HIGHDRIVESTR);
  399. //
  400. //*****************************************************************************
  401. #define am_hal_gpio_pin_config(ui32PinNumber, ui32Config) \
  402. if ( (uint32_t)(ui32PinNumber) < AM_HAL_GPIO_MAX_PADS ) \
  403. { \
  404. AM_CRITICAL_BEGIN_ASM \
  405. \
  406. AM_REGn(GPIO, 0, PADKEY) = AM_REG_GPIO_PADKEY_KEYVAL; \
  407. \
  408. AM_HAL_GPIO_CFG_W(ui32PinNumber, ui32Config); \
  409. AM_HAL_GPIO_PADREG_W(ui32PinNumber, ui32Config); \
  410. AM_HAL_GPIO_ALTPADREG_W(ui32PinNumber, ui32Config); \
  411. \
  412. AM_REGn(GPIO, 0, PADKEY) = 0; \
  413. \
  414. AM_CRITICAL_END_ASM \
  415. }
  416. //*****************************************************************************
  417. //
  418. //! @brief Set the state of one GPIO polarity bit.
  419. //!
  420. //! @param ui32BitNum - GPIO number.
  421. //! @param ui32Polarity - Desired state.
  422. //!
  423. //! This function sets the state of one GPIO polarity bit to a supplied value.
  424. //! The ui32Polarity parameter should be one of the following values:
  425. //!
  426. //! AM_HAL_GPIO_FALLING
  427. //! AM_HAL_GPIO_RISING
  428. //!
  429. //! @return None.
  430. //
  431. //*****************************************************************************
  432. #define am_hal_gpio_int_polarity_bit_set(ui32BitNum, ui32Polarity) \
  433. if ( (uint32_t)(ui32BitNum) < AM_HAL_GPIO_MAX_PADS ) \
  434. { \
  435. AM_CRITICAL_BEGIN_ASM \
  436. \
  437. AM_REGn(GPIO, 0, PADKEY) = AM_REG_GPIO_PADKEY_KEYVAL; \
  438. AM_HAL_GPIO_POL_W(ui32BitNum, ui32Polarity); \
  439. AM_REGn(GPIO, 0, PADKEY) = 0; \
  440. \
  441. AM_CRITICAL_END_ASM \
  442. }
  443. //*****************************************************************************
  444. //
  445. //! @brief Get the state of one GPIO from the INPUT READ REGISTER.
  446. //!
  447. //! @param ui32BitNum - GPIO number.
  448. //!
  449. //! This function retrieves the state of one GPIO from the INPUT READ
  450. //! REGISTER.
  451. //!
  452. //! @return the state for the requested GPIO.
  453. //
  454. //*****************************************************************************
  455. #define am_hal_gpio_input_bit_read(ui32BitNum) \
  456. ((AM_HAL_GPIO_RD(ui32BitNum) & AM_HAL_GPIO_RD_M(ui32BitNum)) != 0)
  457. //*****************************************************************************
  458. //
  459. //! @brief Get the state of one GPIO in the DATA OUTPUT REGISTER
  460. //!
  461. //! @param ui32BitNum - GPIO number.
  462. //!
  463. //! This function retrieves the state of one GPIO in the DATA OUTPUT REGISTER.
  464. //!
  465. //! @return the state for the requested GPIO or -1 for error.
  466. //
  467. //*****************************************************************************
  468. #define am_hal_gpio_out_bit_read(ui32BitNum) \
  469. ((AM_HAL_GPIO_WT(ui32BitNum) & AM_HAL_GPIO_WT_M(ui32BitNum)) != 0)
  470. //*****************************************************************************
  471. //
  472. //! @brief Set the output state high for one GPIO.
  473. //!
  474. //! @param ui32BitNum - GPIO number.
  475. //!
  476. //! This function sets the output state to high for one GPIO.
  477. //!
  478. //! @return None.
  479. //
  480. //*****************************************************************************
  481. #define am_hal_gpio_out_bit_set(ui32BitNum) \
  482. AM_HAL_GPIO_WTS(ui32BitNum) = AM_HAL_GPIO_WTS_M(ui32BitNum)
  483. //*****************************************************************************
  484. //
  485. //! @brief Sets the output state to low for one GPIO.
  486. //!
  487. //! @param ui32BitNum - GPIO number.
  488. //!
  489. //! This function sets the output state to low for one GPIO.
  490. //!
  491. //! @return None.
  492. //
  493. //*****************************************************************************
  494. #define am_hal_gpio_out_bit_clear(ui32BitNum) \
  495. AM_HAL_GPIO_WTC(ui32BitNum) = AM_HAL_GPIO_WTC_M(ui32BitNum)
  496. //*****************************************************************************
  497. //
  498. //! @brief Sets the output state to ui32Value for one GPIO.
  499. //!
  500. //! @param ui32BitNum - GPIO number.
  501. //! @param ui32Value - Desired output state.
  502. //!
  503. //! This function sets the output state to ui32Value for one GPIO.
  504. //!
  505. //! @return None.
  506. //
  507. //*****************************************************************************
  508. #define am_hal_gpio_out_bit_replace(ui32BitNum, ui32Value) \
  509. if ( ui32Value ) \
  510. { \
  511. AM_HAL_GPIO_WTS(ui32BitNum) = AM_HAL_GPIO_WTS_M(ui32BitNum); \
  512. } \
  513. else \
  514. { \
  515. AM_HAL_GPIO_WTC(ui32BitNum) = AM_HAL_GPIO_WTC_M(ui32BitNum); \
  516. }
  517. //*****************************************************************************
  518. //
  519. //! @brief Toggle the output state of one GPIO.
  520. //!
  521. //! @param ui32BitNum - GPIO number.
  522. //!
  523. //! This function toggles the output state of one GPIO.
  524. //!
  525. //! @return None.
  526. //
  527. //*****************************************************************************
  528. #define am_hal_gpio_out_bit_toggle(ui32BitNum) \
  529. if ( 1 ) \
  530. { \
  531. AM_CRITICAL_BEGIN_ASM \
  532. AM_HAL_GPIO_WT(ui32BitNum) ^= AM_HAL_GPIO_WT_M(ui32BitNum); \
  533. AM_CRITICAL_END_ASM \
  534. }
  535. //*****************************************************************************
  536. //
  537. //! @brief Sets the output enable for one GPIO.
  538. //!
  539. //! @param ui32BitNum - GPIO number.
  540. //!
  541. //! This function sets the output enable for one GPIO.
  542. //!
  543. //! @return None.
  544. //
  545. //*****************************************************************************
  546. #define am_hal_gpio_out_enable_bit_set(ui32BitNum) \
  547. AM_HAL_GPIO_ENS(ui32BitNum) = AM_HAL_GPIO_ENS_M(ui32BitNum)
  548. //*****************************************************************************
  549. //
  550. //! @brief Clears the output enable for one GPIO.
  551. //!
  552. //! @param ui32BitNum - GPIO number.
  553. //!
  554. //! This function clears the output enable for one GPIO.
  555. //!
  556. //! @return None.
  557. //
  558. //*****************************************************************************
  559. #define am_hal_gpio_out_enable_bit_clear(ui32BitNum) \
  560. AM_HAL_GPIO_ENC(ui32BitNum) = AM_HAL_GPIO_ENC_M(ui32BitNum)
  561. //*****************************************************************************
  562. //
  563. // Function pointer type for GPIO interrupt handlers.
  564. //
  565. //*****************************************************************************
  566. typedef void (*am_hal_gpio_handler_t)(void);
  567. //*****************************************************************************
  568. //
  569. // External function prototypes
  570. //
  571. //*****************************************************************************
  572. #ifdef __cplusplus
  573. extern "C"
  574. {
  575. #endif
  576. extern uint32_t am_hal_gpio_pin_config_read(uint32_t ui32PinNumber);
  577. extern uint64_t am_hal_gpio_input_read(void);
  578. extern uint64_t am_hal_gpio_out_read(void);
  579. extern uint32_t am_hal_gpio_out_enable_bit_get(uint32_t ui32BitNum);
  580. extern uint64_t am_hal_gpio_out_enable_get(void);
  581. extern void am_hal_gpio_int_enable(uint64_t ui64InterruptMask);
  582. extern uint64_t am_hal_gpio_int_enable_get(void);
  583. extern void am_hal_gpio_int_disable(uint64_t ui64InterruptMask);
  584. extern void am_hal_gpio_int_clear(uint64_t ui64InterruptMask);
  585. extern void am_hal_gpio_int_set(uint64_t ui64InterruptMask);
  586. extern uint64_t am_hal_gpio_int_status_get(bool bEnabledOnly);
  587. extern void am_hal_gpio_int_service(uint64_t ui64Status);
  588. extern void am_hal_gpio_int_register(uint32_t ui32GPIONumber,
  589. am_hal_gpio_handler_t pfnHandler);
  590. extern bool am_hal_gpio_int_polarity_bit_get(uint32_t ui32BitNum);
  591. #ifdef __cplusplus
  592. }
  593. #endif
  594. #endif // AM_HAL_GPIO_H
  595. //*****************************************************************************
  596. //
  597. // End Doxygen group.
  598. //! @}
  599. //
  600. //*****************************************************************************