am_hal_gpio.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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.9 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
  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. // Pad configuration options.
  120. // (Configuration value bits 7:0.)
  121. //
  122. //*****************************************************************************
  123. #define AM_HAL_GPIO_HIGH_DRIVE (0x04 << CFGVAL_PADREG_S)
  124. #define AM_HAL_GPIO_LOW_DRIVE (0x00 << CFGVAL_PADREG_S)
  125. #define AM_HAL_GPIO_PULLUP (0x01 << CFGVAL_PADREG_S)
  126. #define AM_HAL_GPIO_PULL1_5K ( (0x01 << CFGVAL_PADREG_S) | \
  127. AM_HAL_GPIO_PULLUP )
  128. #define AM_HAL_GPIO_PULL6K ( (0x40 << CFGVAL_PADREG_S) | \
  129. AM_HAL_GPIO_PULLUP )
  130. #define AM_HAL_GPIO_PULL12K ( (0x80 << CFGVAL_PADREG_S) | \
  131. AM_HAL_GPIO_PULLUP )
  132. #define AM_HAL_GPIO_PULL24K ( (0xC0 << CFGVAL_PADREG_S) | \
  133. AM_HAL_GPIO_PULLUP )
  134. // POWER SWITCH is available on selected pins
  135. #define AM_HAL_GPIO_POWER (0x80 << CFGVAL_PADREG_S)
  136. //*****************************************************************************
  137. //
  138. //! ALTPADREG configuration options.
  139. //! (Configuration value bits 23:16.)
  140. //!
  141. //! All Apollo2 GPIO pins can be configured for 2mA or 4mA.
  142. //! AM_HAL_GPIO_DRIVE_2MA = 2mA configuration.
  143. //! AM_HAL_GPIO_DRIVE_4MA = 4mA configuration.
  144. //!
  145. //! Certain Apollo2 GPIO pins can be configured to drive up to 12mA.
  146. //! AM_HAL_GPIO_DRIVE_8MA = 8mA configuration.
  147. //! AM_HAL_GPIO_DRIVE_12MA = 12mA configuration.
  148. //!
  149. //! Notes:
  150. //! - Always consult the Apollo2 data sheet for the latest details.
  151. //! - The higher drive GPIOxx pads generally include:
  152. //! 0-2,5,7-8,10,12-13,22-23,26-29,38-39,42,44-48.
  153. //! - GPIOxx pads that do not support the higher drive:
  154. //! 3-4,6,9,11,14-21,24-25,30-37,40-41,43,49.
  155. //! - User is responsible for ensuring that the selected pin actually supports
  156. //! the higher drive (8mA or 12mA) capabilities. See the Apollo2 data sheet.
  157. //! - Attempting to set the higher drive (8mA or 12mA) configuration on a
  158. //! non-supporting pad will actually set the pad for 4mA drive strength,
  159. //! regardless of the lower bit setting.
  160. //
  161. //*****************************************************************************
  162. #define AM_HAL_GPIO_DRIVE_2MA ( 0 )
  163. #define AM_HAL_GPIO_DRIVE_4MA AM_HAL_GPIO_HIGH_DRIVE
  164. #define AM_HAL_GPIO_DRIVE_8MA ( 0x01 << CFGVAL_ALTPAD_S )
  165. #define AM_HAL_GPIO_DRIVE_12MA ( (0x01 << CFGVAL_ALTPAD_S) | \
  166. AM_HAL_GPIO_HIGH_DRIVE )
  167. #define AM_HAL_GPIO_SLEWRATE ( 0x10 << CFGVAL_ALTPAD_S )
  168. //*****************************************************************************
  169. //
  170. // Interrupt polarity
  171. // These values can be used directly in the configval.
  172. //
  173. //*****************************************************************************
  174. #define AM_HAL_GPIOCFGVAL_FALLING ((1 << 2) << CFGVAL_GPIOCFG_S)
  175. #define AM_HAL_GPIOCFGVAL_RISING ((0 << 2) << CFGVAL_GPIOCFG_S)
  176. //*****************************************************************************
  177. //
  178. // Pad function select
  179. // This macro represents the 3 bit function select field in the PADREG byte.
  180. //
  181. //*****************************************************************************
  182. #define AM_HAL_GPIO_FUNC(x) ((x & 0x7) << 3)
  183. //*****************************************************************************
  184. //
  185. //! Interrupt polarity
  186. //!
  187. //! Important:
  188. //! These values are to be used with am_hal_gpio_int_polarity_bit_set().
  189. // They are not intended to be used as part of the GPIO configval.
  190. //
  191. //*****************************************************************************
  192. #define AM_HAL_GPIO_FALLING 0x00000001
  193. #define AM_HAL_GPIO_RISING 0x00000000
  194. //*****************************************************************************
  195. //
  196. // A few common pin configurations.
  197. //
  198. //*****************************************************************************
  199. #define AM_HAL_GPIO_DISABLE \
  200. (AM_HAL_GPIO_FUNC(3))
  201. #define AM_HAL_GPIO_INPUT \
  202. (AM_HAL_GPIO_FUNC(3) | AM_HAL_GPIO_INPEN)
  203. #define AM_HAL_GPIO_OUTPUT \
  204. (AM_HAL_GPIO_FUNC(3) | AM_HAL_GPIO_OUT_PUSHPULL)
  205. #define AM_HAL_GPIO_OPENDRAIN \
  206. (AM_HAL_GPIO_FUNC(3) | AM_HAL_GPIO_OUT_OPENDRAIN | AM_HAL_GPIO_INPEN)
  207. #define AM_HAL_GPIO_3STATE \
  208. (AM_HAL_GPIO_FUNC(3) | AM_HAL_GPIO_OUT_3STATE)
  209. //*****************************************************************************
  210. //
  211. // PADREG helper macros.
  212. //
  213. //*****************************************************************************
  214. #define AM_HAL_GPIO_PADREG(n) \
  215. (AM_REG_GPIOn(0) + AM_REG_GPIO_PADREGA_O + (n & 0xFC))
  216. #define AM_HAL_GPIO_PADREG_S(n) \
  217. (((uint32_t)(n) % 4) << 3)
  218. #define AM_HAL_GPIO_PADREG_M(n) \
  219. ((uint32_t) 0xFF << AM_HAL_GPIO_PADREG_S(n))
  220. #define AM_HAL_GPIO_PADREG_FIELD(n, configval) \
  221. (((uint32_t)(configval) & CFGVAL_PADREG_M) << AM_HAL_GPIO_PADREG_S(n))
  222. #define AM_HAL_GPIO_PADREG_W(n, configval) \
  223. AM_REGVAL(AM_HAL_GPIO_PADREG(n)) = \
  224. (AM_HAL_GPIO_PADREG_FIELD(n, configval) | \
  225. (AM_REGVAL(AM_HAL_GPIO_PADREG(n)) & ~AM_HAL_GPIO_PADREG_M(n)))
  226. #define AM_HAL_GPIO_PADREG_R(n) \
  227. ((AM_REGVAL(AM_HAL_GPIO_PADREG(n)) & AM_HAL_GPIO_PADREG_M(n)) >> \
  228. AM_HAL_GPIO_PADREG_S(n))
  229. //*****************************************************************************
  230. //
  231. // ALTPADCFG helper macros.
  232. // The ALTPADCFG bits are located in [23:16] of the configval.
  233. //
  234. //*****************************************************************************
  235. #define AM_HAL_GPIO_ALTPADREG(n) \
  236. (AM_REG_GPIOn(0) + AM_REG_GPIO_ALTPADCFGA_O + (n & 0xFC))
  237. #define AM_HAL_GPIO_ALTPADREG_S(n) \
  238. (((uint32_t)(n) % 4) << 3)
  239. #define AM_HAL_GPIO_ALTPADREG_M(n) \
  240. ((uint32_t) 0xFF << AM_HAL_GPIO_ALTPADREG_S(n))
  241. #define AM_HAL_GPIO_ALTPADREG_FIELD(n, configval) \
  242. (CFGVAL_ALTPAD_X(configval) << AM_HAL_GPIO_ALTPADREG_S(n))
  243. #define AM_HAL_GPIO_ALTPADREG_W(n, configval) \
  244. AM_REGVAL(AM_HAL_GPIO_ALTPADREG(n)) = \
  245. (AM_HAL_GPIO_ALTPADREG_FIELD(n, configval) | \
  246. (AM_REGVAL(AM_HAL_GPIO_ALTPADREG(n)) & ~AM_HAL_GPIO_ALTPADREG_M(n)))
  247. #define AM_HAL_GPIO_ALTPADREG_R(n) \
  248. ((AM_REGVAL(AM_HAL_GPIO_ALTPADREG(n)) & AM_HAL_GPIO_ALTPADREG_M(n)) >> \
  249. AM_HAL_GPIO_ALTPADREG_S(n))
  250. //*****************************************************************************
  251. //
  252. // CFG helper macros.
  253. //
  254. //*****************************************************************************
  255. #define AM_HAL_GPIO_CFG(n) \
  256. (AM_REG_GPIOn(0) + AM_REG_GPIO_CFGA_O + ((n & 0xF8) >> 1))
  257. #define AM_HAL_GPIO_CFG_S(n) \
  258. (((uint32_t)(n) % 8) << 2)
  259. #define AM_HAL_GPIO_CFG_M(n) \
  260. ((uint32_t) 0x7 << AM_HAL_GPIO_CFG_S(n))
  261. #define AM_HAL_GPIO_CFG_FIELD(n, configval) \
  262. ((((uint32_t)(configval) & 0x700) >> 8) << AM_HAL_GPIO_CFG_S(n))
  263. #define AM_HAL_GPIO_CFG_W(n, configval) \
  264. AM_REGVAL(AM_HAL_GPIO_CFG(n)) = \
  265. (AM_HAL_GPIO_CFG_FIELD(n, configval) | \
  266. (AM_REGVAL(AM_HAL_GPIO_CFG(n)) & ~AM_HAL_GPIO_CFG_M(n)))
  267. #define AM_HAL_GPIO_CFG_R(n) \
  268. ((AM_REGVAL(AM_HAL_GPIO_CFG(n)) & AM_HAL_GPIO_CFG_M(n)) >> \
  269. AM_HAL_GPIO_CFG_S(n))
  270. //*****************************************************************************
  271. //
  272. // Polarity helper macros.
  273. //
  274. //*****************************************************************************
  275. #define AM_HAL_GPIO_POL_S(n) \
  276. ((((uint32_t)(n) % 8) << 2) + 3)
  277. #define AM_HAL_GPIO_POL_M(n) \
  278. ((uint32_t) 0x1 << AM_HAL_GPIO_POL_S(n))
  279. #define AM_HAL_GPIO_POL_FIELD(n, polarity) \
  280. (((uint32_t)(polarity) & 0x1) << AM_HAL_GPIO_POL_S(n))
  281. #define AM_HAL_GPIO_POL_W(n, polarity) \
  282. AM_REGVAL(AM_HAL_GPIO_CFG(n)) = \
  283. (AM_HAL_GPIO_POL_FIELD(n, polarity) | \
  284. (AM_REGVAL(AM_HAL_GPIO_CFG(n)) & ~AM_HAL_GPIO_POL_M(n)))
  285. //*****************************************************************************
  286. //
  287. // RD helper macros.
  288. //
  289. //*****************************************************************************
  290. #define AM_HAL_GPIO_RD_REG(n) \
  291. (AM_REG_GPIOn(0) + AM_REG_GPIO_RDA_O + (((uint32_t)(n) & 0x20) >> 3))
  292. #define AM_HAL_GPIO_RD_S(n) \
  293. ((uint32_t)(n) % 32)
  294. #define AM_HAL_GPIO_RD_M(n) \
  295. ((uint32_t) 0x1 << AM_HAL_GPIO_RD_S(n))
  296. #define AM_HAL_GPIO_RD(n) \
  297. AM_REGVAL(AM_HAL_GPIO_RD_REG(n))
  298. //*****************************************************************************
  299. //
  300. // WT helper macros.
  301. //
  302. //*****************************************************************************
  303. #define AM_HAL_GPIO_WT_REG(n) \
  304. (AM_REG_GPIOn(0) + AM_REG_GPIO_WTA_O + (((uint32_t)(n) & 0x20) >> 3))
  305. #define AM_HAL_GPIO_WT_S(n) \
  306. ((uint32_t)(n) % 32)
  307. #define AM_HAL_GPIO_WT_M(n) \
  308. ((uint32_t) 0x1 << AM_HAL_GPIO_WT_S(n))
  309. #define AM_HAL_GPIO_WT(n) \
  310. AM_REGVAL(AM_HAL_GPIO_WT_REG(n))
  311. //*****************************************************************************
  312. //
  313. // WTS helper macros.
  314. //
  315. //*****************************************************************************
  316. #define AM_HAL_GPIO_WTS_REG(n) \
  317. (AM_REG_GPIOn(0) + AM_REG_GPIO_WTSA_O + (((uint32_t)(n) & 0x20) >> 3))
  318. #define AM_HAL_GPIO_WTS_S(n) \
  319. ((uint32_t)(n) % 32)
  320. #define AM_HAL_GPIO_WTS_M(n) \
  321. ((uint32_t) 0x1 << AM_HAL_GPIO_WTS_S(n))
  322. #define AM_HAL_GPIO_WTS(n) \
  323. AM_REGVAL(AM_HAL_GPIO_WTS_REG(n))
  324. //*****************************************************************************
  325. //
  326. // WTC helper macros.
  327. //
  328. //*****************************************************************************
  329. #define AM_HAL_GPIO_WTC_REG(n) \
  330. (AM_REG_GPIOn(0) + AM_REG_GPIO_WTCA_O + (((uint32_t)(n) & 0x20) >> 3))
  331. #define AM_HAL_GPIO_WTC_S(n) \
  332. ((uint32_t)(n) % 32)
  333. #define AM_HAL_GPIO_WTC_M(n) \
  334. ((uint32_t) 0x1 << AM_HAL_GPIO_WTC_S(n))
  335. #define AM_HAL_GPIO_WTC(n) \
  336. AM_REGVAL(AM_HAL_GPIO_WTC_REG(n))
  337. //*****************************************************************************
  338. //
  339. // EN helper macros.
  340. //
  341. //*****************************************************************************
  342. #define AM_HAL_GPIO_EN_REG(n) \
  343. (AM_REG_GPIOn(0) + AM_REG_GPIO_ENA_O + (((uint32_t)(n) & 0x20) >> 3))
  344. #define AM_HAL_GPIO_EN_S(n) \
  345. ((uint32_t)(n) % 32)
  346. #define AM_HAL_GPIO_EN_M(n) \
  347. ((uint32_t) 0x1 << AM_HAL_GPIO_EN_S(n))
  348. #define AM_HAL_GPIO_EN(n) \
  349. AM_REGVAL(AM_HAL_GPIO_EN_REG(n))
  350. //*****************************************************************************
  351. //
  352. // ENS helper macros.
  353. //
  354. //*****************************************************************************
  355. #define AM_HAL_GPIO_ENS_REG(n) \
  356. (AM_REG_GPIOn(0) + AM_REG_GPIO_ENSA_O + (((uint32_t)(n) & 0x20) >> 3))
  357. #define AM_HAL_GPIO_ENS_S(n) \
  358. ((uint32_t)(n) % 32)
  359. #define AM_HAL_GPIO_ENS_M(n) \
  360. ((uint32_t) 0x1 << AM_HAL_GPIO_ENS_S(n))
  361. #define AM_HAL_GPIO_ENS(n) \
  362. AM_REGVAL(AM_HAL_GPIO_ENS_REG(n))
  363. //*****************************************************************************
  364. //
  365. // ENC helper macros.
  366. //
  367. //*****************************************************************************
  368. #define AM_HAL_GPIO_ENC_REG(n) \
  369. (AM_REG_GPIOn(0) + AM_REG_GPIO_ENCA_O + (((uint32_t)(n) & 0x20) >> 3))
  370. #define AM_HAL_GPIO_ENC_S(n) \
  371. ((uint32_t)(n) % 32)
  372. #define AM_HAL_GPIO_ENC_M(n) \
  373. ((uint32_t) 0x1 << AM_HAL_GPIO_ENC_S(n))
  374. #define AM_HAL_GPIO_ENC(n) \
  375. AM_REGVAL(AM_HAL_GPIO_ENC_REG(n))
  376. //*****************************************************************************
  377. //
  378. //! @brief Configure the GPIO PAD MUX & GPIO PIN Configurations
  379. //!
  380. //! @param ui32PinNumber - GPIO pin number.
  381. //! @param ui32Config - Configuration options.
  382. //!
  383. //! This function applies the settings for a single GPIO. For a list of valid
  384. //! options please see the top of this file (am_hal_gpio.h) and am_hal_pin.h.
  385. //!
  386. //! Usage examples:
  387. //! am_hal_gpio_pin_config(11, AM_HAL_GPIO_INPUT);
  388. //! am_hal_gpio_pin_config(10, AM_HAL_GPIO_OUTPUT);
  389. //! am_hal_gpio_pin_config(14, AM_HAL_GPIO_OUTPUT | AM_HAL_GPIO_SLEWRATE);
  390. //! am_hal_gpio_pin_config(15, AM_HAL_GPIO_OUTPUT | AM_HAL_GPIO_HIGHDRIVESTR);
  391. //
  392. //*****************************************************************************
  393. #define am_hal_gpio_pin_config(ui32PinNumber, ui32Config) \
  394. if ( (uint32_t)(ui32PinNumber) < AM_HAL_GPIO_MAX_PADS ) \
  395. { \
  396. AM_CRITICAL_BEGIN_ASM \
  397. \
  398. AM_REGn(GPIO, 0, PADKEY) = AM_REG_GPIO_PADKEY_KEYVAL; \
  399. \
  400. AM_HAL_GPIO_CFG_W(ui32PinNumber, ui32Config); \
  401. AM_HAL_GPIO_PADREG_W(ui32PinNumber, ui32Config); \
  402. AM_HAL_GPIO_ALTPADREG_W(ui32PinNumber, ui32Config); \
  403. \
  404. AM_REGn(GPIO, 0, PADKEY) = 0; \
  405. \
  406. AM_CRITICAL_END_ASM \
  407. }
  408. //*****************************************************************************
  409. //
  410. //! @brief Set the state of one GPIO polarity bit.
  411. //!
  412. //! @param ui32BitNum - GPIO number.
  413. //! @param ui32Polarity - Desired state.
  414. //!
  415. //! This function sets the state of one GPIO polarity bit to a supplied value.
  416. //! The ui32Polarity parameter should be one of the following values:
  417. //!
  418. //! AM_HAL_GPIO_FALLING
  419. //! AM_HAL_GPIO_RISING
  420. //!
  421. //! @return None.
  422. //
  423. //*****************************************************************************
  424. #define am_hal_gpio_int_polarity_bit_set(ui32PinNumber, ui32Polarity) \
  425. if ( (uint32_t)(ui32PinNumber) < AM_HAL_GPIO_MAX_PADS ) \
  426. { \
  427. AM_CRITICAL_BEGIN_ASM \
  428. \
  429. AM_REGn(GPIO, 0, PADKEY) = AM_REG_GPIO_PADKEY_KEYVAL; \
  430. AM_HAL_GPIO_POL_W(ui32PinNumber, ui32Polarity); \
  431. AM_REGn(GPIO, 0, PADKEY) = 0; \
  432. \
  433. AM_CRITICAL_END_ASM \
  434. }
  435. //*****************************************************************************
  436. //
  437. //! @brief Get the state of one GPIO from the INPUT READ REGISTER.
  438. //!
  439. //! @param ui32BitNum - GPIO number.
  440. //!
  441. //! This function retrieves the state of one GPIO from the INPUT READ
  442. //! REGISTER.
  443. //!
  444. //! @return the state for the requested GPIO.
  445. //
  446. //*****************************************************************************
  447. #define am_hal_gpio_input_bit_read(ui32BitNum) \
  448. ((AM_HAL_GPIO_RD(ui32BitNum) & AM_HAL_GPIO_RD_M(ui32BitNum)) != 0)
  449. //*****************************************************************************
  450. //
  451. //! @brief Get the state of one GPIO in the DATA OUTPUT REGISTER
  452. //!
  453. //! @param ui32BitNum - GPIO number.
  454. //!
  455. //! This function retrieves the state of one GPIO in the DATA OUTPUT REGISTER.
  456. //!
  457. //! @return the state for the requested GPIO or -1 for error.
  458. //
  459. //*****************************************************************************
  460. #define am_hal_gpio_out_bit_read(ui32BitNum) \
  461. ((AM_HAL_GPIO_WT(ui32BitNum) & AM_HAL_GPIO_WT_M(ui32BitNum)) != 0)
  462. //*****************************************************************************
  463. //
  464. //! @brief Set the output state high for one GPIO.
  465. //!
  466. //! @param ui32BitNum - GPIO number.
  467. //!
  468. //! This function sets the output state to high for one GPIO.
  469. //!
  470. //! @return None.
  471. //
  472. //*****************************************************************************
  473. #define am_hal_gpio_out_bit_set(ui32BitNum) \
  474. AM_HAL_GPIO_WTS(ui32BitNum) = AM_HAL_GPIO_WTS_M(ui32BitNum)
  475. //*****************************************************************************
  476. //
  477. //! @brief Sets the output state to low for one GPIO.
  478. //!
  479. //! @param ui32BitNum - GPIO number.
  480. //!
  481. //! This function sets the output state to low for one GPIO.
  482. //!
  483. //! @return None.
  484. //
  485. //*****************************************************************************
  486. #define am_hal_gpio_out_bit_clear(ui32BitNum) \
  487. AM_HAL_GPIO_WTC(ui32BitNum) = AM_HAL_GPIO_WTC_M(ui32BitNum)
  488. //*****************************************************************************
  489. //
  490. //! @brief Sets the output state to ui32Value for one GPIO.
  491. //!
  492. //! @param ui32BitNum - GPIO number.
  493. //! @param ui32Value - Desired output state.
  494. //!
  495. //! This function sets the output state to ui32Value for one GPIO.
  496. //!
  497. //! @return None.
  498. //
  499. //*****************************************************************************
  500. #define am_hal_gpio_out_bit_replace(ui32BitNum, ui32Value) \
  501. if ( ui32Value ) \
  502. { \
  503. AM_HAL_GPIO_WTS(ui32BitNum) = AM_HAL_GPIO_WTS_M(ui32BitNum); \
  504. } \
  505. else \
  506. { \
  507. AM_HAL_GPIO_WTC(ui32BitNum) = AM_HAL_GPIO_WTC_M(ui32BitNum); \
  508. }
  509. //*****************************************************************************
  510. //
  511. //! @brief Toggle the output state of one GPIO.
  512. //!
  513. //! @param ui32BitNum - GPIO number.
  514. //!
  515. //! This function toggles the output state of one GPIO.
  516. //!
  517. //! @return None.
  518. //
  519. //*****************************************************************************
  520. #define am_hal_gpio_out_bit_toggle(ui32BitNum) \
  521. if ( 1 ) \
  522. { \
  523. AM_CRITICAL_BEGIN_ASM \
  524. AM_HAL_GPIO_WT(ui32BitNum) ^= AM_HAL_GPIO_WT_M(ui32BitNum); \
  525. AM_CRITICAL_END_ASM \
  526. }
  527. //*****************************************************************************
  528. //
  529. //! @brief Sets the output enable for one GPIO.
  530. //!
  531. //! @param ui32BitNum - GPIO number.
  532. //!
  533. //! This function sets the output enable for one GPIO.
  534. //!
  535. //! @return None.
  536. //
  537. //*****************************************************************************
  538. #define am_hal_gpio_out_enable_bit_set(ui32BitNum) \
  539. AM_HAL_GPIO_ENS(ui32BitNum) = AM_HAL_GPIO_ENS_M(ui32BitNum)
  540. //*****************************************************************************
  541. //
  542. //! @brief Clears the output enable for one GPIO.
  543. //!
  544. //! @param ui32BitNum - GPIO number.
  545. //!
  546. //! This function clears the output enable for one GPIO.
  547. //!
  548. //! @return None.
  549. //
  550. //*****************************************************************************
  551. #define am_hal_gpio_out_enable_bit_clear(ui32BitNum) \
  552. AM_HAL_GPIO_ENC(ui32BitNum) = AM_HAL_GPIO_ENC_M(ui32BitNum)
  553. //*****************************************************************************
  554. //
  555. // Function pointer type for GPIO interrupt handlers.
  556. //
  557. //*****************************************************************************
  558. typedef void (*am_hal_gpio_handler_t)(void);
  559. //*****************************************************************************
  560. //
  561. // External function prototypes
  562. //
  563. //*****************************************************************************
  564. #ifdef __cplusplus
  565. extern "C"
  566. {
  567. #endif
  568. extern uint32_t am_hal_gpio_pin_config_read(uint32_t ui32PinNumber);
  569. extern uint64_t am_hal_gpio_input_read(void);
  570. extern uint64_t am_hal_gpio_out_read(void);
  571. extern uint32_t am_hal_gpio_out_enable_bit_get(uint32_t ui32BitNum);
  572. extern uint64_t am_hal_gpio_out_enable_get(void);
  573. extern void am_hal_gpio_int_enable(uint64_t ui64InterruptMask);
  574. extern uint64_t am_hal_gpio_int_enable_get(void);
  575. extern void am_hal_gpio_int_disable(uint64_t ui64InterruptMask);
  576. extern void am_hal_gpio_int_clear(uint64_t ui64InterruptMask);
  577. extern void am_hal_gpio_int_set(uint64_t ui64InterruptMask);
  578. extern uint64_t am_hal_gpio_int_status_get(bool bEnabledOnly);
  579. extern void am_hal_gpio_int_service(uint64_t ui64Status);
  580. extern void am_hal_gpio_int_register(uint32_t ui32GPIONumber,
  581. am_hal_gpio_handler_t pfnHandler);
  582. extern bool am_hal_gpio_int_polarity_bit_get(uint32_t ui32BitNum);
  583. #ifdef __cplusplus
  584. }
  585. #endif
  586. #endif // AM_HAL_GPIO_H
  587. //*****************************************************************************
  588. //
  589. // End Doxygen group.
  590. //! @}
  591. //
  592. //*****************************************************************************