nrf_gpio.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. /**
  2. * Copyright (c) 2015 - 2019, 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. /**
  47. * @defgroup nrf_gpio_hal GPIO HAL
  48. * @{
  49. * @ingroup nrf_gpio
  50. * @brief Hardware access layer for managing the GPIO peripheral.
  51. */
  52. #ifndef NRF_P0
  53. #define NRF_P0 NRF_GPIO
  54. #endif
  55. #if (GPIO_COUNT == 1)
  56. #define NUMBER_OF_PINS (P0_PIN_NUM)
  57. #define GPIO_REG_LIST {NRF_P0}
  58. #elif (GPIO_COUNT == 2)
  59. #define NUMBER_OF_PINS (P0_PIN_NUM + P1_PIN_NUM)
  60. #define GPIO_REG_LIST {NRF_P0, NRF_P1}
  61. #else
  62. #error "Not supported."
  63. #endif
  64. /**
  65. * @brief Macro for mapping port and pin numbers to values understandable for nrf_gpio functions.
  66. */
  67. #define NRF_GPIO_PIN_MAP(port, pin) (((port) << 5) | ((pin) & 0x1F))
  68. /**
  69. * @brief Pin direction definitions.
  70. */
  71. typedef enum
  72. {
  73. NRF_GPIO_PIN_DIR_INPUT = GPIO_PIN_CNF_DIR_Input, ///< Input.
  74. NRF_GPIO_PIN_DIR_OUTPUT = GPIO_PIN_CNF_DIR_Output ///< Output.
  75. } nrf_gpio_pin_dir_t;
  76. /**
  77. * @brief Connection of input buffer.
  78. */
  79. typedef enum
  80. {
  81. NRF_GPIO_PIN_INPUT_CONNECT = GPIO_PIN_CNF_INPUT_Connect, ///< Connect input buffer.
  82. NRF_GPIO_PIN_INPUT_DISCONNECT = GPIO_PIN_CNF_INPUT_Disconnect ///< Disconnect input buffer.
  83. } nrf_gpio_pin_input_t;
  84. /**
  85. * @brief Enumerator used for selecting the pin to be pulled down or up at the time of pin configuration.
  86. */
  87. typedef enum
  88. {
  89. NRF_GPIO_PIN_NOPULL = GPIO_PIN_CNF_PULL_Disabled, ///< Pin pull-up resistor disabled.
  90. NRF_GPIO_PIN_PULLDOWN = GPIO_PIN_CNF_PULL_Pulldown, ///< Pin pull-down resistor enabled.
  91. NRF_GPIO_PIN_PULLUP = GPIO_PIN_CNF_PULL_Pullup, ///< Pin pull-up resistor enabled.
  92. } nrf_gpio_pin_pull_t;
  93. /**
  94. * @brief Enumerator used for selecting output drive mode.
  95. */
  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. /**
  108. * @brief Enumerator used for selecting the pin to sense high or low level on the pin input.
  109. */
  110. typedef enum
  111. {
  112. NRF_GPIO_PIN_NOSENSE = GPIO_PIN_CNF_SENSE_Disabled, ///< Pin sense level disabled.
  113. NRF_GPIO_PIN_SENSE_LOW = GPIO_PIN_CNF_SENSE_Low, ///< Pin sense low level.
  114. NRF_GPIO_PIN_SENSE_HIGH = GPIO_PIN_CNF_SENSE_High, ///< Pin sense high level.
  115. } nrf_gpio_pin_sense_t;
  116. /**
  117. * @brief Function for configuring the GPIO pin range as output pins with normal drive strength.
  118. * This function can be used to configure pin range as simple output with gate driving GPIO_PIN_CNF_DRIVE_S0S1 (normal cases).
  119. *
  120. * @param pin_range_start Specifies the start number (inclusive) in the range of pin numbers to be configured (allowed values 0-30).
  121. *
  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. * @note For configuring only one pin as output, use @ref nrf_gpio_cfg_output.
  125. * Sense capability on the pin is disabled and input is disconnected from the buffer as the pins are configured as output.
  126. */
  127. __STATIC_INLINE void nrf_gpio_range_cfg_output(uint32_t pin_range_start, uint32_t pin_range_end);
  128. /**
  129. * @brief Function for configuring the GPIO pin range as input pins with given initial value set, hiding inner details.
  130. * This function can be used to configure pin range as simple input.
  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. *
  134. * @param pin_range_end Specifies the end number (inclusive) in the range of pin numbers to be configured (allowed values 0-30).
  135. *
  136. * @param pull_config State of the pin range pull resistor (no pull, pulled down, or pulled high).
  137. *
  138. * @note For configuring only one pin as input, use @ref nrf_gpio_cfg_input.
  139. * Sense capability on the pin is disabled and input is connected to buffer so that the GPIO->IN register is readable.
  140. */
  141. __STATIC_INLINE void nrf_gpio_range_cfg_input(uint32_t pin_range_start,
  142. uint32_t pin_range_end,
  143. nrf_gpio_pin_pull_t pull_config);
  144. /**
  145. * @brief Pin configuration function.
  146. *
  147. * The main pin configuration function.
  148. * This function allows to set any aspect in PIN_CNF register.
  149. * @param pin_number Specifies the pin number.
  150. * @param dir Pin direction.
  151. * @param input Connect or disconnect the input buffer.
  152. * @param pull Pull configuration.
  153. * @param drive Drive configuration.
  154. * @param sense Pin sensing mechanism.
  155. */
  156. __STATIC_INLINE void nrf_gpio_cfg(
  157. uint32_t pin_number,
  158. nrf_gpio_pin_dir_t dir,
  159. nrf_gpio_pin_input_t input,
  160. nrf_gpio_pin_pull_t pull,
  161. nrf_gpio_pin_drive_t drive,
  162. nrf_gpio_pin_sense_t sense);
  163. /**
  164. * @brief Function for configuring the given GPIO pin number as output, hiding inner details.
  165. * This function can be used to configure a pin as simple output with gate driving GPIO_PIN_CNF_DRIVE_S0S1 (normal cases).
  166. *
  167. * @param pin_number Specifies the pin number.
  168. *
  169. * @note Sense capability on the pin is disabled and input is disconnected from the buffer as the pins are configured as output.
  170. */
  171. __STATIC_INLINE void nrf_gpio_cfg_output(uint32_t pin_number);
  172. /**
  173. * @brief Function for configuring the given GPIO pin number as input, hiding inner details.
  174. * This function can be used to configure a pin as simple input.
  175. *
  176. * @param pin_number Specifies the pin number.
  177. * @param pull_config State of the pin range pull resistor (no pull, pulled down, or pulled high).
  178. *
  179. * @note Sense capability on the pin is disabled and input is connected to buffer so that the GPIO->IN register is readable.
  180. */
  181. __STATIC_INLINE void nrf_gpio_cfg_input(uint32_t pin_number, nrf_gpio_pin_pull_t pull_config);
  182. /**
  183. * @brief Function for resetting pin configuration to its default state.
  184. *
  185. * @param pin_number Specifies the pin number.
  186. */
  187. __STATIC_INLINE void nrf_gpio_cfg_default(uint32_t pin_number);
  188. /**
  189. * @brief Function for configuring the given GPIO pin number as a watcher. Only input is connected.
  190. *
  191. * @param pin_number Specifies the pin number.
  192. *
  193. */
  194. __STATIC_INLINE void nrf_gpio_cfg_watcher(uint32_t pin_number);
  195. /**
  196. * @brief Function for disconnecting input for the given GPIO.
  197. *
  198. * @param pin_number Specifies the pin number.
  199. *
  200. */
  201. __STATIC_INLINE void nrf_gpio_input_disconnect(uint32_t pin_number);
  202. /**
  203. * @brief Function for configuring the given GPIO pin number as input, hiding inner details.
  204. * This function can be used to configure pin range as simple input.
  205. * Sense capability on the pin is configurable and input is connected to buffer so that the GPIO->IN register is readable.
  206. *
  207. * @param pin_number Specifies the pin number.
  208. * @param pull_config State of the pin pull resistor (no pull, pulled down, or pulled high).
  209. * @param sense_config Sense level of the pin (no sense, sense low, or sense high).
  210. */
  211. __STATIC_INLINE void nrf_gpio_cfg_sense_input(uint32_t pin_number,
  212. nrf_gpio_pin_pull_t pull_config,
  213. nrf_gpio_pin_sense_t sense_config);
  214. /**
  215. * @brief Function for configuring sense level for the given GPIO.
  216. *
  217. * @param pin_number Specifies the pin number.
  218. * @param sense_config Sense configuration.
  219. *
  220. */
  221. __STATIC_INLINE void nrf_gpio_cfg_sense_set(uint32_t pin_number, nrf_gpio_pin_sense_t sense_config);
  222. /**
  223. * @brief Function for setting the direction for a GPIO pin.
  224. *
  225. * @param pin_number Specifies the pin number for which to set the direction.
  226. *
  227. * @param direction Specifies the direction.
  228. */
  229. __STATIC_INLINE void nrf_gpio_pin_dir_set(uint32_t pin_number, nrf_gpio_pin_dir_t direction);
  230. /**
  231. * @brief Function for setting a GPIO pin.
  232. *
  233. * Note that the pin must be configured as an output for this function to have any effect.
  234. *
  235. * @param pin_number Specifies the pin number to set.
  236. */
  237. __STATIC_INLINE void nrf_gpio_pin_set(uint32_t pin_number);
  238. /**
  239. * @brief Function for clearing a GPIO pin.
  240. *
  241. * Note that the pin must be configured as an output for this
  242. * function to have any effect.
  243. *
  244. * @param pin_number Specifies the pin number to clear.
  245. */
  246. __STATIC_INLINE void nrf_gpio_pin_clear(uint32_t pin_number);
  247. /**
  248. * @brief Function for toggling a GPIO pin.
  249. *
  250. * Note that the pin must be configured as an output for this
  251. * function to have any effect.
  252. *
  253. * @param pin_number Specifies the pin number to toggle.
  254. */
  255. __STATIC_INLINE void nrf_gpio_pin_toggle(uint32_t pin_number);
  256. /**
  257. * @brief Function for writing a value to a GPIO pin.
  258. *
  259. * Note that the pin must be configured as an output for this
  260. * function to have any effect.
  261. *
  262. * @param pin_number Specifies the pin number to write.
  263. *
  264. * @param value Specifies the value to be written to the pin.
  265. * @arg 0 Clears the pin.
  266. * @arg >=1 Sets the pin.
  267. */
  268. __STATIC_INLINE void nrf_gpio_pin_write(uint32_t pin_number, uint32_t value);
  269. /**
  270. * @brief Function for reading the input level of a GPIO pin.
  271. *
  272. * Note that the pin must have input connected for the value
  273. * returned from this function to be valid.
  274. *
  275. * @param pin_number Specifies the pin number to read.
  276. *
  277. * @return 0 if the pin input level is low. Positive value if the pin is high.
  278. */
  279. __STATIC_INLINE uint32_t nrf_gpio_pin_read(uint32_t pin_number);
  280. /**
  281. * @brief Function for reading the output level of a GPIO pin.
  282. *
  283. * @param pin_number Specifies the pin number to read.
  284. *
  285. * @return 0 if the pin output level is low. Positive value if pin output is high.
  286. */
  287. __STATIC_INLINE uint32_t nrf_gpio_pin_out_read(uint32_t pin_number);
  288. /**
  289. * @brief Function for reading the sense configuration of a GPIO pin.
  290. *
  291. * @param pin_number Specifies the pin number to read.
  292. *
  293. * @retval Sense configuration.
  294. */
  295. __STATIC_INLINE nrf_gpio_pin_sense_t nrf_gpio_pin_sense_get(uint32_t pin_number);
  296. /**
  297. * @brief Function for reading the direction configuration of a GPIO pin.
  298. *
  299. * @param pin_number Specifies the pin number to read.
  300. *
  301. * @retval Direction configuration.
  302. */
  303. __STATIC_INLINE nrf_gpio_pin_dir_t nrf_gpio_pin_dir_get(uint32_t pin_number);
  304. /**
  305. * @brief Function for reading the status of GPIO pin input buffer.
  306. *
  307. * @param pin_number Pin number to be read.
  308. *
  309. * @retval Input buffer configuration.
  310. */
  311. __STATIC_INLINE nrf_gpio_pin_input_t nrf_gpio_pin_input_get(uint32_t pin_number);
  312. /**
  313. * @brief Function for reading the pull configuration of a GPIO pin.
  314. *
  315. * @param pin_number Specifies the pin number to read.
  316. *
  317. * @retval Pull configuration.
  318. */
  319. __STATIC_INLINE nrf_gpio_pin_pull_t nrf_gpio_pin_pull_get(uint32_t pin_number);
  320. /**
  321. * @brief Function for setting output direction on selected pins on a given port.
  322. *
  323. * @param p_reg Pointer to the peripheral registers structure.
  324. * @param out_mask Mask specifying the pins to set as output.
  325. *
  326. */
  327. __STATIC_INLINE void nrf_gpio_port_dir_output_set(NRF_GPIO_Type * p_reg, uint32_t out_mask);
  328. /**
  329. * @brief Function for setting input direction on selected pins on a given port.
  330. *
  331. * @param p_reg Pointer to the peripheral registers structure.
  332. * @param in_mask Mask specifying the pins to set as input.
  333. *
  334. */
  335. __STATIC_INLINE void nrf_gpio_port_dir_input_set(NRF_GPIO_Type * p_reg, uint32_t in_mask);
  336. /**
  337. * @brief Function for writing the direction configuration of GPIO pins in a given port.
  338. *
  339. * @param p_reg Pointer to the peripheral registers structure.
  340. * @param dir_mask Mask specifying the direction of pins. Bit set means that the given pin is configured as output.
  341. *
  342. */
  343. __STATIC_INLINE void nrf_gpio_port_dir_write(NRF_GPIO_Type * p_reg, uint32_t dir_mask);
  344. /**
  345. * @brief Function for reading the direction configuration of a GPIO port.
  346. *
  347. * @param p_reg Pointer to the peripheral registers structure.
  348. *
  349. * @retval Pin configuration of the current direction settings. Bit set means that the given pin is configured as output.
  350. */
  351. __STATIC_INLINE uint32_t nrf_gpio_port_dir_read(NRF_GPIO_Type const * p_reg);
  352. /**
  353. * @brief Function for reading the input signals of GPIO pins on a given port.
  354. *
  355. * @param p_reg Pointer to the peripheral registers structure.
  356. *
  357. * @retval Port input values.
  358. */
  359. __STATIC_INLINE uint32_t nrf_gpio_port_in_read(NRF_GPIO_Type const * p_reg);
  360. /**
  361. * @brief Function for reading the output signals of GPIO pins of a given port.
  362. *
  363. * @param p_reg Pointer to the peripheral registers structure.
  364. *
  365. * @retval Port output values.
  366. */
  367. __STATIC_INLINE uint32_t nrf_gpio_port_out_read(NRF_GPIO_Type const * p_reg);
  368. /**
  369. * @brief Function for writing the GPIO pins output on a given port.
  370. *
  371. * @param p_reg Pointer to the peripheral registers structure.
  372. * @param value Output port mask.
  373. *
  374. */
  375. __STATIC_INLINE void nrf_gpio_port_out_write(NRF_GPIO_Type * p_reg, uint32_t value);
  376. /**
  377. * @brief Function for setting high level on selected GPIO pins of a given port.
  378. *
  379. * @param p_reg Pointer to the peripheral registers structure.
  380. * @param set_mask Mask with pins to set as logical high level.
  381. *
  382. */
  383. __STATIC_INLINE void nrf_gpio_port_out_set(NRF_GPIO_Type * p_reg, uint32_t set_mask);
  384. /**
  385. * @brief Function for setting low level on selected GPIO pins of a given port.
  386. *
  387. * @param p_reg Pointer to the peripheral registers structure.
  388. * @param clr_mask Mask with pins to set as logical low level.
  389. *
  390. */
  391. __STATIC_INLINE void nrf_gpio_port_out_clear(NRF_GPIO_Type * p_reg, uint32_t clr_mask);
  392. /**
  393. * @brief Function for reading pins state of multiple consecutive ports.
  394. *
  395. * @param start_port Index of the first port to read.
  396. * @param length Number of ports to read.
  397. * @param p_masks Pointer to output array where port states will be stored.
  398. */
  399. __STATIC_INLINE void nrf_gpio_ports_read(uint32_t start_port, uint32_t length, uint32_t * p_masks);
  400. #if defined(GPIO_DETECTMODE_DETECTMODE_LDETECT) || defined(__NRF_DOXYGEN__)
  401. /**
  402. * @brief Function for reading latch state of multiple consecutive ports.
  403. *
  404. * @param start_port Index of the first port to read.
  405. * @param length Number of ports to read.
  406. * @param p_masks Pointer to output array where latch states will be stored.
  407. */
  408. __STATIC_INLINE void nrf_gpio_latches_read(uint32_t start_port, uint32_t length,
  409. uint32_t * p_masks);
  410. /**
  411. * @brief Function for reading latch state of single pin.
  412. *
  413. * @param pin_number Pin number.
  414. * @return 0 if latch is not set. Positive value otherwise.
  415. *
  416. */
  417. __STATIC_INLINE uint32_t nrf_gpio_pin_latch_get(uint32_t pin_number);
  418. /**
  419. * @brief Function for clearing latch state of a single pin.
  420. *
  421. * @param pin_number Pin number.
  422. *
  423. */
  424. __STATIC_INLINE void nrf_gpio_pin_latch_clear(uint32_t pin_number);
  425. #endif
  426. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  427. /**
  428. * @brief Function for extracting port and relative pin number from absolute pin number.
  429. *
  430. * @param[inout] Pointer to absolute pin number which is overriden by relative to port pin number.
  431. *
  432. * @return Pointer to port register set.
  433. *
  434. */
  435. __STATIC_INLINE NRF_GPIO_Type * nrf_gpio_pin_port_decode(uint32_t * p_pin)
  436. {
  437. NRFX_ASSERT(*p_pin < NUMBER_OF_PINS);
  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 & (P0_PIN_NUM - 1);
  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. #ifdef GPIO_DETECTMODE_DETECTMODE_LDETECT
  676. __STATIC_INLINE void nrf_gpio_latches_read(uint32_t start_port, uint32_t length, uint32_t * p_masks)
  677. {
  678. NRF_GPIO_Type * gpio_regs[GPIO_COUNT] = GPIO_REG_LIST;
  679. uint32_t i;
  680. for (i = start_port; i < (start_port + length); i++)
  681. {
  682. *p_masks = gpio_regs[i]->LATCH;
  683. p_masks++;
  684. }
  685. }
  686. __STATIC_INLINE uint32_t nrf_gpio_pin_latch_get(uint32_t pin_number)
  687. {
  688. NRF_GPIO_Type * reg = nrf_gpio_pin_port_decode(&pin_number);
  689. return (reg->LATCH & (1 << pin_number)) ? 1 : 0;
  690. }
  691. __STATIC_INLINE void nrf_gpio_pin_latch_clear(uint32_t pin_number)
  692. {
  693. NRF_GPIO_Type * reg = nrf_gpio_pin_port_decode(&pin_number);
  694. reg->LATCH = (1 << pin_number);
  695. }
  696. #endif
  697. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  698. /** @} */
  699. #ifdef __cplusplus
  700. }
  701. #endif
  702. #endif // NRF_GPIO_H__