nrf_gpio.h~RF11bd4c15.TMP 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. /**
  2. * Copyright (c) 2015 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef NRF_GPIO_H__
  41. #define NRF_GPIO_H__
  42. #include <nrfx.h>
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. #ifndef NRF_P0
  47. #define NRF_P0 NRF_GPIO
  48. #endif
  49. #if (GPIO_COUNT == 1)
  50. #define NUMBER_OF_PINS (P0_PIN_NUM)
  51. #define GPIO_REG_LIST {NRF_P0}
  52. #elif (GPIO_COUNT == 2)
  53. #define NUMBER_OF_PINS (P0_PIN_NUM + P1_PIN_NUM)
  54. #define GPIO_REG_LIST {NRF_P0, NRF_P1}
  55. #else
  56. #error "Not supported."
  57. #endif
  58. #if defined(NRF52820_XXAA)
  59. #include <nrf_erratas.h>
  60. #endif
  61. /**
  62. * @defgroup nrf_gpio_hal GPIO HAL
  63. * @{
  64. * @ingroup nrf_gpio
  65. * @brief Hardware access layer for managing the GPIO peripheral.
  66. */
  67. #if defined(GPIO_LATCH_PIN0_Msk) || defined(__NRFX_DOXYGEN__)
  68. /** @brief Symbol indicating whether the functionality of latching GPIO state change is present. */
  69. #define NRF_GPIO_LATCH_PRESENT
  70. #endif
  71. /** @brief Macro for mapping port and pin numbers to values understandable for nrf_gpio functions. */
  72. #define NRF_GPIO_PIN_MAP(port, pin) (((port) << 5) | ((pin) & 0x1F))
  73. /** @brief Pin direction definitions. */
  74. typedef enum
  75. {
  76. NRF_GPIO_PIN_DIR_INPUT = GPIO_PIN_CNF_DIR_Input, ///< Input.
  77. NRF_GPIO_PIN_DIR_OUTPUT = GPIO_PIN_CNF_DIR_Output ///< Output.
  78. } nrf_gpio_pin_dir_t;
  79. /** @brief Connection of input buffer. */
  80. typedef enum
  81. {
  82. NRF_GPIO_PIN_INPUT_CONNECT = GPIO_PIN_CNF_INPUT_Connect, ///< Connect input buffer.
  83. NRF_GPIO_PIN_INPUT_DISCONNECT = GPIO_PIN_CNF_INPUT_Disconnect ///< Disconnect input buffer.
  84. } nrf_gpio_pin_input_t;
  85. /**
  86. * @brief Enumerator used for selecting the pin to be pulled down or up at the time of pin
  87. * configuration.
  88. */
  89. typedef enum
  90. {
  91. NRF_GPIO_PIN_NOPULL = GPIO_PIN_CNF_PULL_Disabled, ///< Pin pull-up resistor disabled.
  92. NRF_GPIO_PIN_PULLDOWN = GPIO_PIN_CNF_PULL_Pulldown, ///< Pin pull-down resistor enabled.
  93. NRF_GPIO_PIN_PULLUP = GPIO_PIN_CNF_PULL_Pullup, ///< Pin pull-up resistor enabled.
  94. } nrf_gpio_pin_pull_t;
  95. /** @brief Enumerator used for selecting output drive mode. */
  96. typedef enum
  97. {
  98. NRF_GPIO_PIN_S0S1 = GPIO_PIN_CNF_DRIVE_S0S1, ///< !< Standard '0', standard '1'.
  99. NRF_GPIO_PIN_H0S1 = GPIO_PIN_CNF_DRIVE_H0S1, ///< !< High-drive '0', standard '1'.
  100. NRF_GPIO_PIN_S0H1 = GPIO_PIN_CNF_DRIVE_S0H1, ///< !< Standard '0', high-drive '1'.
  101. NRF_GPIO_PIN_H0H1 = GPIO_PIN_CNF_DRIVE_H0H1, ///< !< High drive '0', high-drive '1'.
  102. NRF_GPIO_PIN_D0S1 = GPIO_PIN_CNF_DRIVE_D0S1, ///< !< Disconnect '0' standard '1'.
  103. NRF_GPIO_PIN_D0H1 = GPIO_PIN_CNF_DRIVE_D0H1, ///< !< Disconnect '0', high-drive '1'.
  104. NRF_GPIO_PIN_S0D1 = GPIO_PIN_CNF_DRIVE_S0D1, ///< !< Standard '0', disconnect '1'.
  105. NRF_GPIO_PIN_H0D1 = GPIO_PIN_CNF_DRIVE_H0D1, ///< !< High-drive '0', disconnect '1'.
  106. } nrf_gpio_pin_drive_t;
  107. /** @brief Enumerator used for selecting the pin to sense high or low level on the pin input. */
  108. typedef enum
  109. {
  110. NRF_GPIO_PIN_NOSENSE = GPIO_PIN_CNF_SENSE_Disabled, ///< Pin sense level disabled.
  111. NRF_GPIO_PIN_SENSE_LOW = GPIO_PIN_CNF_SENSE_Low, ///< Pin sense low level.
  112. NRF_GPIO_PIN_SENSE_HIGH = GPIO_PIN_CNF_SENSE_High, ///< Pin sense high level.
  113. } nrf_gpio_pin_sense_t;
  114. /**
  115. * @brief Function for configuring the GPIO pin range as output pins with normal drive strength.
  116. * This function can be used to configure pin range as simple output with gate driving GPIO_PIN_CNF_DRIVE_S0S1 (normal cases).
  117. *
  118. * @note For configuring only one pin as output, use @ref nrf_gpio_cfg_output.
  119. * Sense capability on the pin is disabled and input is disconnected from the buffer as the pins are configured as output.
  120. *
  121. * @param pin_range_start Specifies the start number (inclusive) in the range of pin numbers to be configured (allowed values 0-30).
  122. * @param pin_range_end Specifies the end number (inclusive) in the range of pin numbers to be configured (allowed values 0-30).
  123. */
  124. __STATIC_INLINE void nrf_gpio_range_cfg_output(uint32_t pin_range_start, uint32_t pin_range_end);
  125. /**
  126. * @brief Function for configuring the GPIO pin range as input pins with given initial value set, hiding inner details.
  127. * This function can be used to configure pin range as simple input.
  128. *
  129. * @note For configuring only one pin as input, use @ref nrf_gpio_cfg_input.
  130. * Sense capability on the pin is disabled and input is connected to buffer so that the GPIO->IN register is readable.
  131. *
  132. * @param pin_range_start Specifies the start number (inclusive) in the range of pin numbers to be configured (allowed values 0-30).
  133. * @param pin_range_end Specifies the end number (inclusive) in the range of pin numbers to be configured (allowed values 0-30).
  134. * @param pull_config State of the pin range pull resistor (no pull, pulled down, or pulled high).
  135. */
  136. __STATIC_INLINE void nrf_gpio_range_cfg_input(uint32_t pin_range_start,
  137. uint32_t pin_range_end,
  138. nrf_gpio_pin_pull_t pull_config);
  139. /**
  140. * @brief Pin configuration function.
  141. *
  142. * The main pin configuration function.
  143. * This function allows to set any aspect in PIN_CNF register.
  144. *
  145. * @param pin_number Specifies the pin number.
  146. * @param dir Pin direction.
  147. * @param input Connect or disconnect the input buffer.
  148. * @param pull Pull configuration.
  149. * @param drive Drive configuration.
  150. * @param sense Pin sensing mechanism.
  151. */
  152. __STATIC_INLINE void nrf_gpio_cfg(
  153. uint32_t pin_number,
  154. nrf_gpio_pin_dir_t dir,
  155. nrf_gpio_pin_input_t input,
  156. nrf_gpio_pin_pull_t pull,
  157. nrf_gpio_pin_drive_t drive,
  158. nrf_gpio_pin_sense_t sense);
  159. /**
  160. * @brief Function for configuring the given GPIO pin number as output, hiding inner details.
  161. * This function can be used to configure a pin as simple output with gate driving GPIO_PIN_CNF_DRIVE_S0S1 (normal cases).
  162. *
  163. * @note Sense capability on the pin is disabled and input is disconnected from the buffer as the pins are configured as output.
  164. *
  165. * @param pin_number Specifies the pin number.
  166. */
  167. __STATIC_INLINE void nrf_gpio_cfg_output(uint32_t pin_number);
  168. /**
  169. * @brief Function for configuring the given GPIO pin number as input, hiding inner details.
  170. * This function can be used to configure a pin as simple input.
  171. *
  172. * @note Sense capability on the pin is disabled and input is connected to buffer so that the GPIO->IN register is readable.
  173. *
  174. * @param pin_number Specifies the pin number.
  175. * @param pull_config State of the pin range pull resistor (no pull, pulled down, or pulled high).
  176. */
  177. __STATIC_INLINE void nrf_gpio_cfg_input(uint32_t pin_number, nrf_gpio_pin_pull_t pull_config);
  178. /**
  179. * @brief Function for resetting pin configuration to its default state.
  180. *
  181. * @param pin_number Specifies the pin number.
  182. */
  183. __STATIC_INLINE void nrf_gpio_cfg_default(uint32_t pin_number);
  184. /**
  185. * @brief Function for configuring the given GPIO pin number as a watcher. Only input is connected.
  186. *
  187. * @param pin_number Specifies the pin number.
  188. *
  189. */
  190. __STATIC_INLINE void nrf_gpio_cfg_watcher(uint32_t pin_number);
  191. /**
  192. * @brief Function for disconnecting input for the given GPIO.
  193. *
  194. * @param pin_number Specifies the pin number.
  195. */
  196. __STATIC_INLINE void nrf_gpio_input_disconnect(uint32_t pin_number);
  197. /**
  198. * @brief Function for configuring the given GPIO pin number as input, hiding inner details.
  199. * This function can be used to configure pin range as simple input.
  200. * Sense capability on the pin is configurable and input is connected to buffer so that the GPIO->IN register is readable.
  201. *
  202. * @param pin_number Specifies the pin number.
  203. * @param pull_config State of the pin pull resistor (no pull, pulled down, or pulled high).
  204. * @param sense_config Sense level of the pin (no sense, sense low, or sense high).
  205. */
  206. __STATIC_INLINE void nrf_gpio_cfg_sense_input(uint32_t pin_number,
  207. nrf_gpio_pin_pull_t pull_config,
  208. nrf_gpio_pin_sense_t sense_config);
  209. /**
  210. * @brief Function for configuring sense level for the given GPIO.
  211. *
  212. * @param pin_number Specifies the pin number.
  213. * @param sense_config Sense configuration.
  214. */
  215. __STATIC_INLINE void nrf_gpio_cfg_sense_set(uint32_t pin_number, nrf_gpio_pin_sense_t sense_config);
  216. /**
  217. * @brief Function for setting the direction for a GPIO pin.
  218. *
  219. * @param pin_number Specifies the pin number for which to set the direction.
  220. * @param direction Specifies the direction.
  221. */
  222. __STATIC_INLINE void nrf_gpio_pin_dir_set(uint32_t pin_number, nrf_gpio_pin_dir_t direction);
  223. /**
  224. * @brief Function for setting a GPIO pin.
  225. *
  226. * For this function to have any effect, the pin must be configured as an output.
  227. *
  228. * @param pin_number Specifies the pin number to be set.
  229. */
  230. __STATIC_INLINE void nrf_gpio_pin_set(uint32_t pin_number);
  231. /**
  232. * @brief Function for clearing a GPIO pin.
  233. *
  234. * For this function to have any effect, the pin must be configured as an output.
  235. *
  236. * @param pin_number Specifies the pin number to clear.
  237. */
  238. __STATIC_INLINE void nrf_gpio_pin_clear(uint32_t pin_number);
  239. /**
  240. * @brief Function for toggling a GPIO pin.
  241. *
  242. * For this function to have any effect, the pin must be configured as an output.
  243. *
  244. * @param pin_number Specifies the pin number to toggle.
  245. */
  246. __STATIC_INLINE void nrf_gpio_pin_toggle(uint32_t pin_number);
  247. /**
  248. * @brief Function for writing a value to a GPIO pin.
  249. *
  250. * For this function to have any effect, the pin must be configured as an output.
  251. *
  252. * @param pin_number Specifies the pin number to write.
  253. * @param value Specifies the value to be written to the pin.
  254. * @arg 0 Clears the pin.
  255. * @arg >=1 Sets the pin.
  256. */
  257. __STATIC_INLINE void nrf_gpio_pin_write(uint32_t pin_number, uint32_t value);
  258. /**
  259. * @brief Function for reading the input level of a GPIO pin.
  260. *
  261. * If the value returned by this function is to be valid, the pin's input buffer must be connected.
  262. *
  263. * @param pin_number Specifies the pin number to read.
  264. *
  265. * @return 0 if the pin input level is low. Positive value if the pin is high.
  266. */
  267. __STATIC_INLINE uint32_t nrf_gpio_pin_read(uint32_t pin_number);
  268. /**
  269. * @brief Function for reading the output level of a GPIO pin.
  270. *
  271. * @param pin_number Specifies the pin number to read.
  272. *
  273. * @return 0 if the pin output level is low. Positive value if pin output is high.
  274. */
  275. __STATIC_INLINE uint32_t nrf_gpio_pin_out_read(uint32_t pin_number);
  276. /**
  277. * @brief Function for reading the sense configuration of a GPIO pin.
  278. *
  279. * @param pin_number Specifies the pin number to read.
  280. *
  281. * @return Sense configuration.
  282. */
  283. __STATIC_INLINE nrf_gpio_pin_sense_t nrf_gpio_pin_sense_get(uint32_t pin_number);
  284. /**
  285. * @brief Function for reading the direction configuration of a GPIO pin.
  286. *
  287. * @param pin_number Specifies the pin number to read.
  288. *
  289. * @return Direction configuration.
  290. */
  291. __STATIC_INLINE nrf_gpio_pin_dir_t nrf_gpio_pin_dir_get(uint32_t pin_number);
  292. /**
  293. * @brief Function for reading the status of GPIO pin input buffer.
  294. *
  295. * @param pin_number Pin number to be read.
  296. *
  297. * @retval Input buffer configuration.
  298. */
  299. __STATIC_INLINE nrf_gpio_pin_input_t nrf_gpio_pin_input_get(uint32_t pin_number);
  300. /**
  301. * @brief Function for reading the pull configuration of a GPIO pin.
  302. *
  303. * @param pin_number Specifies the pin number to read.
  304. *
  305. * @retval Pull configuration.
  306. */
  307. __STATIC_INLINE nrf_gpio_pin_pull_t nrf_gpio_pin_pull_get(uint32_t pin_number);
  308. /**
  309. * @brief Function for setting output direction on the selected pins on the given port.
  310. *
  311. * @param p_reg Pointer to the structure of registers of the peripheral.
  312. * @param out_mask Mask specifying the pins to set as output.
  313. */
  314. __STATIC_INLINE void nrf_gpio_port_dir_output_set(NRF_GPIO_Type * p_reg, uint32_t out_mask);
  315. /**
  316. * @brief Function for setting input direction on selected pins on a given port.
  317. *
  318. * @param p_reg Pointer to the structure of registers of the peripheral.
  319. * @param in_mask Mask that specifies the pins to be set as input.
  320. */
  321. __STATIC_INLINE void nrf_gpio_port_dir_input_set(NRF_GPIO_Type * p_reg, uint32_t in_mask);
  322. /**
  323. * @brief Function for writing the direction configuration of the GPIO pins in the given port.
  324. *
  325. * @param p_reg Pointer to the structure of registers of the peripheral.
  326. * @param dir_mask Mask that specifies the direction of pins. Bit set means that the given pin is configured as output.
  327. */
  328. __STATIC_INLINE void nrf_gpio_port_dir_write(NRF_GPIO_Type * p_reg, uint32_t dir_mask);
  329. /**
  330. * @brief Function for reading the direction configuration of a GPIO port.
  331. *
  332. * @param p_reg Pointer to the structure of registers of the peripheral.
  333. *
  334. * @return Pin configuration of the current direction settings. Bit set means that the given pin is configured as output.
  335. */
  336. __STATIC_INLINE uint32_t nrf_gpio_port_dir_read(NRF_GPIO_Type const * p_reg);
  337. /**
  338. * @brief Function for reading the input signals of the GPIO pins on the given port.
  339. *
  340. * @param p_reg Pointer to the peripheral registers structure.
  341. *
  342. * @return Port input values.
  343. */
  344. __STATIC_INLINE uint32_t nrf_gpio_port_in_read(NRF_GPIO_Type const * p_reg);
  345. /**
  346. * @brief Function for reading the output signals of the GPIO pins on the given port.
  347. *
  348. * @param p_reg Pointer to the peripheral registers structure.
  349. *
  350. * @return Port output values.
  351. */
  352. __STATIC_INLINE uint32_t nrf_gpio_port_out_read(NRF_GPIO_Type const * p_reg);
  353. /**
  354. * @brief Function for writing the GPIO pins output on a given port.
  355. *
  356. * @param p_reg Pointer to the structure of registers of the peripheral.
  357. * @param value Output port mask.
  358. */
  359. __STATIC_INLINE void nrf_gpio_port_out_write(NRF_GPIO_Type * p_reg, uint32_t value);
  360. /**
  361. * @brief Function for setting high level on selected the GPIO pins on the given port.
  362. *
  363. * @param p_reg Pointer to the structure of registers of the peripheral.
  364. * @param set_mask Mask with pins to be set as logical high level.
  365. */
  366. __STATIC_INLINE void nrf_gpio_port_out_set(NRF_GPIO_Type * p_reg, uint32_t set_mask);
  367. /**
  368. * @brief Function for setting low level on selected the GPIO pins on the given port.
  369. *
  370. * @param p_reg Pointer to the structure of registers of the peripheral.
  371. * @param clr_mask Mask with pins to be set as logical low level.
  372. */
  373. __STATIC_INLINE void nrf_gpio_port_out_clear(NRF_GPIO_Type * p_reg, uint32_t clr_mask);
  374. /**
  375. * @brief Function for reading pin state of multiple consecutive ports.
  376. *
  377. * @param start_port Index of the first port to read.
  378. * @param length Number of ports to read.
  379. * @param p_masks Pointer to output array where port states will be stored.
  380. */
  381. __STATIC_INLINE void nrf_gpio_ports_read(uint32_t start_port, uint32_t length, uint32_t * p_masks);
  382. #if defined(NRF_GPIO_LATCH_PRESENT)
  383. /**
  384. * @brief Function for reading latch state of multiple consecutive ports.
  385. *
  386. * @param start_port Index of the first port to read.
  387. * @param length Number of ports to read.
  388. * @param p_masks Pointer to output array where latch states will be stored.
  389. */
  390. __STATIC_INLINE void nrf_gpio_latches_read(uint32_t start_port,
  391. uint32_t length,
  392. uint32_t * p_masks);
  393. /**
  394. * @brief Function for reading and immediate clearing latch state of multiple consecutive ports.
  395. *
  396. * @param start_port Index of the first port to read and clear.
  397. * @param length Number of ports to read and clear.
  398. * @param p_masks Pointer to output array where latch states will be stored.
  399. */
  400. __STATIC_INLINE void nrf_gpio_latches_read_and_clear(uint32_t start_port,
  401. uint32_t length,
  402. uint32_t * p_masks);
  403. /**
  404. * @brief Function for reading latch state of single pin.
  405. *
  406. * @param pin_number Pin number.
  407. *
  408. * @return 0 if latch is not set. Positive value otherwise.
  409. */
  410. __STATIC_INLINE uint32_t nrf_gpio_pin_latch_get(uint32_t pin_number);
  411. /**
  412. * @brief Function for clearing latch state of a single pin.
  413. *
  414. * @param pin_number Pin number.
  415. */
  416. __STATIC_INLINE void nrf_gpio_pin_latch_clear(uint32_t pin_number);
  417. #endif // defined(NRF_GPIO_LATCH_PRESENT)
  418. /**
  419. * @brief Function for checking if provided pin is present on the MCU.
  420. *
  421. * @param[in] pin_number Number of the pin to be checked.
  422. *
  423. * @retval true Pin is present.
  424. * @retval false Pin is not present.
  425. */
  426. __STATIC_INLINE bool nrf_gpio_pin_present_check(uint32_t pin_number);
  427. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  428. /**
  429. * @brief Function for extracting port and the relative pin number from the absolute pin number.
  430. *
  431. * @param[in,out] p_pin Pointer to the absolute pin number overriden by the pin number that is relative to the port.
  432. *
  433. * @return Pointer to port register set.
  434. */
  435. __STATIC_INLINE NRF_GPIO_Type * nrf_gpio_pin_port_decode(uint32_t * p_pin)
  436. {
  437. NRFX_ASSERT(nrf_gpio_pin_present_check(*p_pin));
  438. #if (GPIO_COUNT == 1)
  439. return NRF_P0;
  440. #else
  441. if (*p_pin < P0_PIN_NUM)
  442. {
  443. return NRF_P0;
  444. }
  445. else
  446. {
  447. *p_pin = *p_pin & 0x1F;
  448. return NRF_P1;
  449. }
  450. #endif
  451. }
  452. __STATIC_INLINE void nrf_gpio_range_cfg_output(uint32_t pin_range_start, uint32_t pin_range_end)
  453. {
  454. /*lint -e{845} // A zero has been given as right argument to operator '|'" */
  455. for (; pin_range_start <= pin_range_end; pin_range_start++)
  456. {
  457. nrf_gpio_cfg_output(pin_range_start);
  458. }
  459. }
  460. __STATIC_INLINE void nrf_gpio_range_cfg_input(uint32_t pin_range_start,
  461. uint32_t pin_range_end,
  462. nrf_gpio_pin_pull_t pull_config)
  463. {
  464. /*lint -e{845} // A zero has been given as right argument to operator '|'" */
  465. for (; pin_range_start <= pin_range_end; pin_range_start++)
  466. {
  467. nrf_gpio_cfg_input(pin_range_start, pull_config);
  468. }
  469. }
  470. __STATIC_INLINE void nrf_gpio_cfg(
  471. uint32_t pin_number,
  472. nrf_gpio_pin_dir_t dir,
  473. nrf_gpio_pin_input_t input,
  474. nrf_gpio_pin_pull_t pull,
  475. nrf_gpio_pin_drive_t drive,
  476. nrf_gpio_pin_sense_t sense)
  477. {
  478. NRF_GPIO_Type * reg = nrf_gpio_pin_port_decode(&pin_number);
  479. reg->PIN_CNF[pin_number] = ((uint32_t)dir << GPIO_PIN_CNF_DIR_Pos)
  480. | ((uint32_t)input << GPIO_PIN_CNF_INPUT_Pos)
  481. | ((uint32_t)pull << GPIO_PIN_CNF_PULL_Pos)
  482. | ((uint32_t)drive << GPIO_PIN_CNF_DRIVE_Pos)
  483. | ((uint32_t)sense << GPIO_PIN_CNF_SENSE_Pos);
  484. }
  485. __STATIC_INLINE void nrf_gpio_cfg_output(uint32_t pin_number)
  486. {
  487. nrf_gpio_cfg(
  488. pin_number,
  489. NRF_GPIO_PIN_DIR_OUTPUT,
  490. NRF_GPIO_PIN_INPUT_DISCONNECT,
  491. NRF_GPIO_PIN_NOPULL,
  492. NRF_GPIO_PIN_S0S1,
  493. NRF_GPIO_PIN_NOSENSE);
  494. }
  495. __STATIC_INLINE void nrf_gpio_cfg_input(uint32_t pin_number, nrf_gpio_pin_pull_t pull_config)
  496. {
  497. nrf_gpio_cfg(
  498. pin_number,
  499. NRF_GPIO_PIN_DIR_INPUT,
  500. NRF_GPIO_PIN_INPUT_CONNECT,
  501. pull_config,
  502. NRF_GPIO_PIN_S0S1,
  503. NRF_GPIO_PIN_NOSENSE);
  504. }
  505. __STATIC_INLINE void nrf_gpio_cfg_default(uint32_t pin_number)
  506. {
  507. nrf_gpio_cfg(
  508. pin_number,
  509. NRF_GPIO_PIN_DIR_INPUT,
  510. NRF_GPIO_PIN_INPUT_DISCONNECT,
  511. NRF_GPIO_PIN_NOPULL,
  512. NRF_GPIO_PIN_S0S1,
  513. NRF_GPIO_PIN_NOSENSE);
  514. }
  515. __STATIC_INLINE void nrf_gpio_cfg_watcher(uint32_t pin_number)
  516. {
  517. NRF_GPIO_Type * reg = nrf_gpio_pin_port_decode(&pin_number);
  518. /*lint -e{845} // A zero has been given as right argument to operator '|'" */
  519. uint32_t cnf = reg->PIN_CNF[pin_number] & ~GPIO_PIN_CNF_INPUT_Msk;
  520. reg->PIN_CNF[pin_number] = cnf | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos);
  521. }
  522. __STATIC_INLINE void nrf_gpio_input_disconnect(uint32_t pin_number)
  523. {
  524. NRF_GPIO_Type * reg = nrf_gpio_pin_port_decode(&pin_number);
  525. /*lint -e{845} // A zero has been given as right argument to operator '|'" */
  526. uint32_t cnf = reg->PIN_CNF[pin_number] & ~GPIO_PIN_CNF_INPUT_Msk;
  527. reg->PIN_CNF[pin_number] = cnf | (GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos);
  528. }
  529. __STATIC_INLINE void nrf_gpio_cfg_sense_input(uint32_t pin_number,
  530. nrf_gpio_pin_pull_t pull_config,
  531. nrf_gpio_pin_sense_t sense_config)
  532. {
  533. nrf_gpio_cfg(
  534. pin_number,
  535. NRF_GPIO_PIN_DIR_INPUT,
  536. NRF_GPIO_PIN_INPUT_CONNECT,
  537. pull_config,
  538. NRF_GPIO_PIN_S0S1,
  539. sense_config);
  540. }
  541. __STATIC_INLINE void nrf_gpio_cfg_sense_set(uint32_t pin_number, nrf_gpio_pin_sense_t sense_config)
  542. {
  543. NRF_GPIO_Type * reg = nrf_gpio_pin_port_decode(&pin_number);
  544. /*lint -e{845} // A zero has been given as right argument to operator '|'" */
  545. reg->PIN_CNF[pin_number] &= ~GPIO_PIN_CNF_SENSE_Msk;
  546. reg->PIN_CNF[pin_number] |= (sense_config << GPIO_PIN_CNF_SENSE_Pos);
  547. }
  548. __STATIC_INLINE void nrf_gpio_pin_dir_set(uint32_t pin_number, nrf_gpio_pin_dir_t direction)
  549. {
  550. if (direction == NRF_GPIO_PIN_DIR_INPUT)
  551. {
  552. nrf_gpio_cfg(
  553. pin_number,
  554. NRF_GPIO_PIN_DIR_INPUT,
  555. NRF_GPIO_PIN_INPUT_CONNECT,
  556. NRF_GPIO_PIN_NOPULL,
  557. NRF_GPIO_PIN_S0S1,
  558. NRF_GPIO_PIN_NOSENSE);
  559. }
  560. else
  561. {
  562. NRF_GPIO_Type * reg = nrf_gpio_pin_port_decode(&pin_number);
  563. reg->DIRSET = (1UL << pin_number);
  564. }
  565. }
  566. __STATIC_INLINE void nrf_gpio_pin_set(uint32_t pin_number)
  567. {
  568. NRF_GPIO_Type * reg = nrf_gpio_pin_port_decode(&pin_number);
  569. nrf_gpio_port_out_set(reg, 1UL << pin_number);
  570. }
  571. __STATIC_INLINE void nrf_gpio_pin_clear(uint32_t pin_number)
  572. {
  573. NRF_GPIO_Type * reg = nrf_gpio_pin_port_decode(&pin_number);
  574. nrf_gpio_port_out_clear(reg, 1UL << pin_number);
  575. }
  576. __STATIC_INLINE void nrf_gpio_pin_toggle(uint32_t pin_number)
  577. {
  578. NRF_GPIO_Type * reg = nrf_gpio_pin_port_decode(&pin_number);
  579. uint32_t pins_state = reg->OUT;
  580. reg->OUTSET = (~pins_state & (1UL << pin_number));
  581. reg->OUTCLR = (pins_state & (1UL << pin_number));
  582. }
  583. __STATIC_INLINE void nrf_gpio_pin_write(uint32_t pin_number, uint32_t value)
  584. {
  585. if (value == 0)
  586. {
  587. nrf_gpio_pin_clear(pin_number);
  588. }
  589. else
  590. {
  591. nrf_gpio_pin_set(pin_number);
  592. }
  593. }
  594. __STATIC_INLINE uint32_t nrf_gpio_pin_read(uint32_t pin_number)
  595. {
  596. NRF_GPIO_Type * reg = nrf_gpio_pin_port_decode(&pin_number);
  597. return ((nrf_gpio_port_in_read(reg) >> pin_number) & 1UL);
  598. }
  599. __STATIC_INLINE uint32_t nrf_gpio_pin_out_read(uint32_t pin_number)
  600. {
  601. NRF_GPIO_Type * reg = nrf_gpio_pin_port_decode(&pin_number);
  602. return ((nrf_gpio_port_out_read(reg) >> pin_number) & 1UL);
  603. }
  604. __STATIC_INLINE nrf_gpio_pin_sense_t nrf_gpio_pin_sense_get(uint32_t pin_number)
  605. {
  606. NRF_GPIO_Type * reg = nrf_gpio_pin_port_decode(&pin_number);
  607. return (nrf_gpio_pin_sense_t)((reg->PIN_CNF[pin_number] &
  608. GPIO_PIN_CNF_SENSE_Msk) >> GPIO_PIN_CNF_SENSE_Pos);
  609. }
  610. __STATIC_INLINE nrf_gpio_pin_dir_t nrf_gpio_pin_dir_get(uint32_t pin_number)
  611. {
  612. NRF_GPIO_Type * reg = nrf_gpio_pin_port_decode(&pin_number);
  613. return (nrf_gpio_pin_dir_t)((reg->PIN_CNF[pin_number] &
  614. GPIO_PIN_CNF_DIR_Msk) >> GPIO_PIN_CNF_DIR_Pos);
  615. }
  616. __STATIC_INLINE nrf_gpio_pin_input_t nrf_gpio_pin_input_get(uint32_t pin_number)
  617. {
  618. NRF_GPIO_Type * reg = nrf_gpio_pin_port_decode(&pin_number);
  619. return (nrf_gpio_pin_input_t)((reg->PIN_CNF[pin_number] &
  620. GPIO_PIN_CNF_INPUT_Msk) >> GPIO_PIN_CNF_INPUT_Pos);
  621. }
  622. __STATIC_INLINE nrf_gpio_pin_pull_t nrf_gpio_pin_pull_get(uint32_t pin_number)
  623. {
  624. NRF_GPIO_Type * reg = nrf_gpio_pin_port_decode(&pin_number);
  625. return (nrf_gpio_pin_pull_t)((reg->PIN_CNF[pin_number] &
  626. GPIO_PIN_CNF_PULL_Msk) >> GPIO_PIN_CNF_PULL_Pos);
  627. }
  628. __STATIC_INLINE void nrf_gpio_port_dir_output_set(NRF_GPIO_Type * p_reg, uint32_t out_mask)
  629. {
  630. p_reg->DIRSET = out_mask;
  631. }
  632. __STATIC_INLINE void nrf_gpio_port_dir_input_set(NRF_GPIO_Type * p_reg, uint32_t in_mask)
  633. {
  634. p_reg->DIRCLR = in_mask;
  635. }
  636. __STATIC_INLINE void nrf_gpio_port_dir_write(NRF_GPIO_Type * p_reg, uint32_t value)
  637. {
  638. p_reg->DIR = value;
  639. }
  640. __STATIC_INLINE uint32_t nrf_gpio_port_dir_read(NRF_GPIO_Type const * p_reg)
  641. {
  642. return p_reg->DIR;
  643. }
  644. __STATIC_INLINE uint32_t nrf_gpio_port_in_read(NRF_GPIO_Type const * p_reg)
  645. {
  646. return p_reg->IN;
  647. }
  648. __STATIC_INLINE uint32_t nrf_gpio_port_out_read(NRF_GPIO_Type const * p_reg)
  649. {
  650. return p_reg->OUT;
  651. }
  652. __STATIC_INLINE void nrf_gpio_port_out_write(NRF_GPIO_Type * p_reg, uint32_t value)
  653. {
  654. p_reg->OUT = value;
  655. }
  656. __STATIC_INLINE void nrf_gpio_port_out_set(NRF_GPIO_Type * p_reg, uint32_t set_mask)
  657. {
  658. p_reg->OUTSET = set_mask;
  659. }
  660. __STATIC_INLINE void nrf_gpio_port_out_clear(NRF_GPIO_Type * p_reg, uint32_t clr_mask)
  661. {
  662. p_reg->OUTCLR = clr_mask;
  663. }
  664. __STATIC_INLINE void nrf_gpio_ports_read(uint32_t start_port, uint32_t length, uint32_t * p_masks)
  665. {
  666. NRF_GPIO_Type * gpio_regs[GPIO_COUNT] = GPIO_REG_LIST;
  667. NRFX_ASSERT(start_port + length <= GPIO_COUNT);
  668. uint32_t i;
  669. for (i = start_port; i < (start_port + length); i++)
  670. {
  671. *p_masks = nrf_gpio_port_in_read(gpio_regs[i]);
  672. p_masks++;
  673. }
  674. }
  675. #if defined(NRF_GPIO_LATCH_PRESENT)
  676. __STATIC_INLINE void nrf_gpio_latches_read(uint32_t start_port,
  677. uint32_t length,
  678. uint32_t * p_masks)
  679. {
  680. NRF_GPIO_Type * gpio_regs[GPIO_COUNT] = GPIO_REG_LIST;
  681. uint32_t i;
  682. for (i = start_port; i < (start_port + length); i++)
  683. {
  684. *p_masks = gpio_regs[i]->LATCH;
  685. p_masks++;
  686. }
  687. }
  688. __STATIC_INLINE void nrf_gpio_latches_read_and_clear(uint32_t start_port,
  689. uint32_t length,
  690. uint32_t * p_masks)
  691. {
  692. NRF_GPIO_Type * gpio_regs[GPIO_COUNT] = GPIO_REG_LIST;
  693. uint32_t i;
  694. for (i = start_port; i < (start_port + length); i++)
  695. {
  696. *p_masks = gpio_regs[i]->LATCH;
  697. // The LATCH register is cleared by writing a '1' to the bit that shall be cleared.
  698. gpio_regs[i]->LATCH = *p_masks;
  699. p_masks++;
  700. }
  701. }
  702. __STATIC_INLINE uint32_t nrf_gpio_pin_latch_get(uint32_t pin_number)
  703. {
  704. NRF_GPIO_Type * reg = nrf_gpio_pin_port_decode(&pin_number);
  705. return (reg->LATCH & (1 << pin_number)) ? 1 : 0;
  706. }
  707. __STATIC_INLINE void nrf_gpio_pin_latch_clear(uint32_t pin_number)
  708. {
  709. NRF_GPIO_Type * reg = nrf_gpio_pin_port_decode(&pin_number);
  710. reg->LATCH = (1 << pin_number);
  711. }
  712. #endif // defined(NRF_GPIO_LATCH_PRESENT)
  713. __STATIC_INLINE bool nrf_gpio_pin_present_check(uint32_t pin_number)
  714. {
  715. uint32_t port = pin_number >> 5;
  716. uint32_t mask = 0;
  717. switch (port)
  718. {
  719. #ifdef P0_FEATURE_PINS_PRESENT
  720. case 0:
  721. mask = P0_FEATURE_PINS_PRESENT;
  722. #if defined(NRF52820_XXAA) && defined(DEVELOP_IN_NRF52833)
  723. /* Allow use of the following additional GPIOs that are connected to LEDs and buttons
  724. * on the nRF52833 DK:
  725. * - P0.11 - Button 1
  726. * - P0.12 - Button 2
  727. * - P0.13 - LED 1
  728. * - P0.24 - Button 3
  729. * - P0.25 - Button 4
  730. */
  731. mask |= 0x03003800;
  732. #endif // defined(NRF52820_XXAA) && defined(DEVELOP_IN_NRF52833)
  733. break;
  734. #endif
  735. #ifdef P1_FEATURE_PINS_PRESENT
  736. case 1:
  737. mask = P1_FEATURE_PINS_PRESENT;
  738. break;
  739. #endif
  740. }
  741. pin_number &= 0x1F;
  742. return (mask & (1UL << pin_number)) ? true : false;
  743. }
  744. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  745. /** @} */
  746. #ifdef __cplusplus
  747. }
  748. #endif
  749. #endif // NRF_GPIO_H__