nrf_gpio.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. /**
  2. * Copyright (c) 2015 - 2018, 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. #if (GPIO_COUNT == 1)
  53. #define NUMBER_OF_PINS (P0_PIN_NUM)
  54. #define GPIO_REG_LIST {NRF_GPIO}
  55. #elif (GPIO_COUNT == 2)
  56. #define NUMBER_OF_PINS (P0_PIN_NUM + P1_PIN_NUM)
  57. #define GPIO_REG_LIST {NRF_P0, NRF_P1}
  58. #else
  59. #error "Not supported."
  60. #endif
  61. /**
  62. * @brief Macro for mapping port and pin numbers to values understandable for nrf_gpio functions.
  63. */
  64. #define NRF_GPIO_PIN_MAP(port, pin) (((port) << 5) | ((pin) & 0x1F))
  65. /**
  66. * @brief Pin direction definitions.
  67. */
  68. typedef enum
  69. {
  70. NRF_GPIO_PIN_DIR_INPUT = GPIO_PIN_CNF_DIR_Input, ///< Input.
  71. NRF_GPIO_PIN_DIR_OUTPUT = GPIO_PIN_CNF_DIR_Output ///< Output.
  72. } nrf_gpio_pin_dir_t;
  73. /**
  74. * @brief Connection of input buffer.
  75. */
  76. typedef enum
  77. {
  78. NRF_GPIO_PIN_INPUT_CONNECT = GPIO_PIN_CNF_INPUT_Connect, ///< Connect input buffer.
  79. NRF_GPIO_PIN_INPUT_DISCONNECT = GPIO_PIN_CNF_INPUT_Disconnect ///< Disconnect input buffer.
  80. } nrf_gpio_pin_input_t;
  81. /**
  82. * @brief Enumerator used for selecting the pin to be pulled down or up at the time of pin configuration.
  83. */
  84. typedef enum
  85. {
  86. NRF_GPIO_PIN_NOPULL = GPIO_PIN_CNF_PULL_Disabled, ///< Pin pull-up resistor disabled.
  87. NRF_GPIO_PIN_PULLDOWN = GPIO_PIN_CNF_PULL_Pulldown, ///< Pin pull-down resistor enabled.
  88. NRF_GPIO_PIN_PULLUP = GPIO_PIN_CNF_PULL_Pullup, ///< Pin pull-up resistor enabled.
  89. } nrf_gpio_pin_pull_t;
  90. /**
  91. * @brief Enumerator used for selecting output drive mode.
  92. */
  93. typedef enum
  94. {
  95. NRF_GPIO_PIN_S0S1 = GPIO_PIN_CNF_DRIVE_S0S1, ///< !< Standard '0', standard '1'.
  96. NRF_GPIO_PIN_H0S1 = GPIO_PIN_CNF_DRIVE_H0S1, ///< !< High-drive '0', standard '1'.
  97. NRF_GPIO_PIN_S0H1 = GPIO_PIN_CNF_DRIVE_S0H1, ///< !< Standard '0', high-drive '1'.
  98. NRF_GPIO_PIN_H0H1 = GPIO_PIN_CNF_DRIVE_H0H1, ///< !< High drive '0', high-drive '1'.
  99. NRF_GPIO_PIN_D0S1 = GPIO_PIN_CNF_DRIVE_D0S1, ///< !< Disconnect '0' standard '1'.
  100. NRF_GPIO_PIN_D0H1 = GPIO_PIN_CNF_DRIVE_D0H1, ///< !< Disconnect '0', high-drive '1'.
  101. NRF_GPIO_PIN_S0D1 = GPIO_PIN_CNF_DRIVE_S0D1, ///< !< Standard '0', disconnect '1'.
  102. NRF_GPIO_PIN_H0D1 = GPIO_PIN_CNF_DRIVE_H0D1, ///< !< High-drive '0', disconnect '1'.
  103. } nrf_gpio_pin_drive_t;
  104. /**
  105. * @brief Enumerator used for selecting the pin to sense high or low level on the pin input.
  106. */
  107. typedef enum
  108. {
  109. NRF_GPIO_PIN_NOSENSE = GPIO_PIN_CNF_SENSE_Disabled, ///< Pin sense level disabled.
  110. NRF_GPIO_PIN_SENSE_LOW = GPIO_PIN_CNF_SENSE_Low, ///< Pin sense low level.
  111. NRF_GPIO_PIN_SENSE_HIGH = GPIO_PIN_CNF_SENSE_High, ///< Pin sense high level.
  112. } nrf_gpio_pin_sense_t;
  113. /**
  114. * @brief Function for configuring the GPIO pin range as output pins with normal drive strength.
  115. * This function can be used to configure pin range as simple output with gate driving GPIO_PIN_CNF_DRIVE_S0S1 (normal cases).
  116. *
  117. * @param pin_range_start Specifies the start number (inclusive) in the range of pin numbers to be configured (allowed values 0-30).
  118. *
  119. * @param pin_range_end Specifies the end number (inclusive) in the range of pin numbers to be configured (allowed values 0-30).
  120. *
  121. * @note For configuring only one pin as output, use @ref nrf_gpio_cfg_output.
  122. * Sense capability on the pin is disabled and input is disconnected from the buffer as the pins are configured as output.
  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. * @param pin_range_start Specifies the start number (inclusive) in the range of pin numbers to be configured (allowed values 0-30).
  130. *
  131. * @param pin_range_end Specifies the end number (inclusive) in the range of pin numbers to be configured (allowed values 0-30).
  132. *
  133. * @param pull_config State of the pin range pull resistor (no pull, pulled down, or pulled high).
  134. *
  135. * @note For configuring only one pin as input, use @ref nrf_gpio_cfg_input.
  136. * Sense capability on the pin is disabled and input is connected to buffer so that the GPIO->IN register is readable.
  137. */
  138. __STATIC_INLINE void nrf_gpio_range_cfg_input(uint32_t pin_range_start,
  139. uint32_t pin_range_end,
  140. nrf_gpio_pin_pull_t pull_config);
  141. /**
  142. * @brief Pin configuration function.
  143. *
  144. * The main pin configuration function.
  145. * This function allows to set any aspect in PIN_CNF register.
  146. * @param pin_number Specifies the pin number.
  147. * @param dir Pin direction.
  148. * @param input Connect or disconnect the input buffer.
  149. * @param pull Pull configuration.
  150. * @param drive Drive configuration.
  151. * @param sense Pin sensing mechanism.
  152. */
  153. __STATIC_INLINE void nrf_gpio_cfg(
  154. uint32_t pin_number,
  155. nrf_gpio_pin_dir_t dir,
  156. nrf_gpio_pin_input_t input,
  157. nrf_gpio_pin_pull_t pull,
  158. nrf_gpio_pin_drive_t drive,
  159. nrf_gpio_pin_sense_t sense);
  160. /**
  161. * @brief Function for configuring the given GPIO pin number as output, hiding inner details.
  162. * This function can be used to configure a pin as simple output with gate driving GPIO_PIN_CNF_DRIVE_S0S1 (normal cases).
  163. *
  164. * @param pin_number Specifies the pin number.
  165. *
  166. * @note Sense capability on the pin is disabled and input is disconnected from the buffer as the pins are configured as output.
  167. */
  168. __STATIC_INLINE void nrf_gpio_cfg_output(uint32_t pin_number);
  169. /**
  170. * @brief Function for configuring the given GPIO pin number as input, hiding inner details.
  171. * This function can be used to configure a pin as simple input.
  172. *
  173. * @param pin_number Specifies the pin number.
  174. * @param pull_config State of the pin range pull resistor (no pull, pulled down, or pulled high).
  175. *
  176. * @note Sense capability on the pin is disabled and input is connected to buffer so that the GPIO->IN register is readable.
  177. */
  178. __STATIC_INLINE void nrf_gpio_cfg_input(uint32_t pin_number, nrf_gpio_pin_pull_t pull_config);
  179. /**
  180. * @brief Function for resetting pin configuration to its default state.
  181. *
  182. * @param pin_number Specifies the pin number.
  183. */
  184. __STATIC_INLINE void nrf_gpio_cfg_default(uint32_t pin_number);
  185. /**
  186. * @brief Function for configuring the given GPIO pin number as a watcher. Only input is connected.
  187. *
  188. * @param pin_number Specifies the pin number.
  189. *
  190. */
  191. __STATIC_INLINE void nrf_gpio_cfg_watcher(uint32_t pin_number);
  192. /**
  193. * @brief Function for disconnecting input for the given GPIO.
  194. *
  195. * @param pin_number Specifies the pin number.
  196. *
  197. */
  198. __STATIC_INLINE void nrf_gpio_input_disconnect(uint32_t pin_number);
  199. /**
  200. * @brief Function for configuring the given GPIO pin number as input, hiding inner details.
  201. * This function can be used to configure pin range as simple input.
  202. * Sense capability on the pin is configurable and input is connected to buffer so that the GPIO->IN register is readable.
  203. *
  204. * @param pin_number Specifies the pin number.
  205. * @param pull_config State of the pin pull resistor (no pull, pulled down, or pulled high).
  206. * @param sense_config Sense level of the pin (no sense, sense low, or sense high).
  207. */
  208. __STATIC_INLINE void nrf_gpio_cfg_sense_input(uint32_t pin_number,
  209. nrf_gpio_pin_pull_t pull_config,
  210. nrf_gpio_pin_sense_t sense_config);
  211. /**
  212. * @brief Function for configuring sense level for the given GPIO.
  213. *
  214. * @param pin_number Specifies the pin number.
  215. * @param sense_config Sense configuration.
  216. *
  217. */
  218. __STATIC_INLINE void nrf_gpio_cfg_sense_set(uint32_t pin_number, nrf_gpio_pin_sense_t sense_config);
  219. /**
  220. * @brief Function for setting the direction for a GPIO pin.
  221. *
  222. * @param pin_number Specifies the pin number for which to set the direction.
  223. *
  224. * @param direction Specifies the direction.
  225. */
  226. __STATIC_INLINE void nrf_gpio_pin_dir_set(uint32_t pin_number, nrf_gpio_pin_dir_t direction);
  227. /**
  228. * @brief Function for setting a GPIO pin.
  229. *
  230. * Note that the pin must be configured as an output for this function to have any effect.
  231. *
  232. * @param pin_number Specifies the pin number to set.
  233. */
  234. __STATIC_INLINE void nrf_gpio_pin_set(uint32_t pin_number);
  235. /**
  236. * @brief Function for clearing a GPIO pin.
  237. *
  238. * Note that the pin must be configured as an output for this
  239. * function to have any effect.
  240. *
  241. * @param pin_number Specifies the pin number to clear.
  242. */
  243. __STATIC_INLINE void nrf_gpio_pin_clear(uint32_t pin_number);
  244. /**
  245. * @brief Function for toggling a GPIO pin.
  246. *
  247. * Note that the pin must be configured as an output for this
  248. * function to have any effect.
  249. *
  250. * @param pin_number Specifies the pin number to toggle.
  251. */
  252. __STATIC_INLINE void nrf_gpio_pin_toggle(uint32_t pin_number);
  253. /**
  254. * @brief Function for writing a value to a GPIO pin.
  255. *
  256. * Note that the pin must be configured as an output for this
  257. * function to have any effect.
  258. *
  259. * @param pin_number Specifies the pin number to write.
  260. *
  261. * @param value Specifies the value to be written to the pin.
  262. * @arg 0 Clears the pin.
  263. * @arg >=1 Sets the pin.
  264. */
  265. __STATIC_INLINE void nrf_gpio_pin_write(uint32_t pin_number, uint32_t value);
  266. /**
  267. * @brief Function for reading the input level of a GPIO pin.
  268. *
  269. * Note that the pin must have input connected for the value
  270. * returned from this function to be valid.
  271. *
  272. * @param pin_number Specifies the pin number to read.
  273. *
  274. * @return 0 if the pin input level is low. Positive value if the pin is high.
  275. */
  276. __STATIC_INLINE uint32_t nrf_gpio_pin_read(uint32_t pin_number);
  277. /**
  278. * @brief Function for reading the output level of a GPIO pin.
  279. *
  280. * @param pin_number Specifies the pin number to read.
  281. *
  282. * @return 0 if the pin output level is low. Positive value if pin output is high.
  283. */
  284. __STATIC_INLINE uint32_t nrf_gpio_pin_out_read(uint32_t pin_number);
  285. /**
  286. * @brief Function for reading the sense configuration of a GPIO pin.
  287. *
  288. * @param pin_number Specifies the pin number to read.
  289. *
  290. * @retval Sense configuration.
  291. */
  292. __STATIC_INLINE nrf_gpio_pin_sense_t nrf_gpio_pin_sense_get(uint32_t pin_number);
  293. /**
  294. * @brief Function for reading the direction configuration of a GPIO pin.
  295. *
  296. * @param pin_number Specifies the pin number to read.
  297. *
  298. * @retval Direction configuration.
  299. */
  300. __STATIC_INLINE nrf_gpio_pin_dir_t nrf_gpio_pin_dir_get(uint32_t pin_number);
  301. /**
  302. * @brief Function for setting output direction on selected pins on a given port.
  303. *
  304. * @param p_reg Pointer to the peripheral registers structure.
  305. * @param out_mask Mask specifying the pins to set as output.
  306. *
  307. */
  308. __STATIC_INLINE void nrf_gpio_port_dir_output_set(NRF_GPIO_Type * p_reg, uint32_t out_mask);
  309. /**
  310. * @brief Function for setting input direction on selected pins on a given port.
  311. *
  312. * @param p_reg Pointer to the peripheral registers structure.
  313. * @param in_mask Mask specifying the pins to set as input.
  314. *
  315. */
  316. __STATIC_INLINE void nrf_gpio_port_dir_input_set(NRF_GPIO_Type * p_reg, uint32_t in_mask);
  317. /**
  318. * @brief Function for writing the direction configuration of GPIO pins in a given port.
  319. *
  320. * @param p_reg Pointer to the peripheral registers structure.
  321. * @param dir_mask Mask specifying the direction of pins. Bit set means that the given pin is configured as output.
  322. *
  323. */
  324. __STATIC_INLINE void nrf_gpio_port_dir_write(NRF_GPIO_Type * p_reg, uint32_t dir_mask);
  325. /**
  326. * @brief Function for reading the direction configuration of a GPIO port.
  327. *
  328. * @param p_reg Pointer to the peripheral registers structure.
  329. *
  330. * @retval Pin configuration of the current direction settings. Bit set means that the given pin is configured as output.
  331. */
  332. __STATIC_INLINE uint32_t nrf_gpio_port_dir_read(NRF_GPIO_Type const * p_reg);
  333. /**
  334. * @brief Function for reading the input signals of GPIO pins on a given port.
  335. *
  336. * @param p_reg Pointer to the peripheral registers structure.
  337. *
  338. * @retval Port input values.
  339. */
  340. __STATIC_INLINE uint32_t nrf_gpio_port_in_read(NRF_GPIO_Type const * p_reg);
  341. /**
  342. * @brief Function for reading the output signals of GPIO pins of a given port.
  343. *
  344. * @param p_reg Pointer to the peripheral registers structure.
  345. *
  346. * @retval Port output values.
  347. */
  348. __STATIC_INLINE uint32_t nrf_gpio_port_out_read(NRF_GPIO_Type const * p_reg);
  349. /**
  350. * @brief Function for writing the GPIO pins output on a given port.
  351. *
  352. * @param p_reg Pointer to the peripheral registers structure.
  353. * @param value Output port mask.
  354. *
  355. */
  356. __STATIC_INLINE void nrf_gpio_port_out_write(NRF_GPIO_Type * p_reg, uint32_t value);
  357. /**
  358. * @brief Function for setting high level on selected GPIO pins of a given port.
  359. *
  360. * @param p_reg Pointer to the peripheral registers structure.
  361. * @param set_mask Mask with pins to set as logical high level.
  362. *
  363. */
  364. __STATIC_INLINE void nrf_gpio_port_out_set(NRF_GPIO_Type * p_reg, uint32_t set_mask);
  365. /**
  366. * @brief Function for setting low level on selected GPIO pins of a given port.
  367. *
  368. * @param p_reg Pointer to the peripheral registers structure.
  369. * @param clr_mask Mask with pins to set as logical low level.
  370. *
  371. */
  372. __STATIC_INLINE void nrf_gpio_port_out_clear(NRF_GPIO_Type * p_reg, uint32_t clr_mask);
  373. /**
  374. * @brief Function for reading pins state of multiple consecutive ports.
  375. *
  376. * @param start_port Index of the first port to read.
  377. * @param length Number of ports to read.
  378. * @param p_masks Pointer to output array where port states will be stored.
  379. */
  380. __STATIC_INLINE void nrf_gpio_ports_read(uint32_t start_port, uint32_t length, uint32_t * p_masks);
  381. #if defined(GPIO_DETECTMODE_DETECTMODE_LDETECT) || defined(__NRF_DOXYGEN__)
  382. /**
  383. * @brief Function for reading latch state of multiple consecutive ports.
  384. *
  385. * @param start_port Index of the first port to read.
  386. * @param length Number of ports to read.
  387. * @param p_masks Pointer to output array where latch states will be stored.
  388. */
  389. __STATIC_INLINE void nrf_gpio_latches_read(uint32_t start_port, uint32_t length,
  390. uint32_t * p_masks);
  391. /**
  392. * @brief Function for reading latch state of single pin.
  393. *
  394. * @param pin_number Pin number.
  395. * @return 0 if latch is not set. Positive value otherwise.
  396. *
  397. */
  398. __STATIC_INLINE uint32_t nrf_gpio_pin_latch_get(uint32_t pin_number);
  399. /**
  400. * @brief Function for clearing latch state of a single pin.
  401. *
  402. * @param pin_number Pin number.
  403. *
  404. */
  405. __STATIC_INLINE void nrf_gpio_pin_latch_clear(uint32_t pin_number);
  406. #endif
  407. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  408. /**
  409. * @brief Function for extracting port and relative pin number from absolute pin number.
  410. *
  411. * @param[inout] Pointer to absolute pin number which is overriden by relative to port pin number.
  412. *
  413. * @return Pointer to port register set.
  414. *
  415. */
  416. __STATIC_INLINE NRF_GPIO_Type * nrf_gpio_pin_port_decode(uint32_t * p_pin)
  417. {
  418. NRFX_ASSERT(*p_pin < NUMBER_OF_PINS);
  419. #if (GPIO_COUNT == 1)
  420. // The oldest definition case
  421. return NRF_GPIO;
  422. #else
  423. if (*p_pin < P0_PIN_NUM)
  424. {
  425. return NRF_P0;
  426. }
  427. else
  428. {
  429. *p_pin = *p_pin & (P0_PIN_NUM - 1);
  430. return NRF_P1;
  431. }
  432. #endif
  433. }
  434. __STATIC_INLINE void nrf_gpio_range_cfg_output(uint32_t pin_range_start, uint32_t pin_range_end)
  435. {
  436. /*lint -e{845} // A zero has been given as right argument to operator '|'" */
  437. for (; pin_range_start <= pin_range_end; pin_range_start++)
  438. {
  439. nrf_gpio_cfg_output(pin_range_start);
  440. }
  441. }
  442. __STATIC_INLINE void nrf_gpio_range_cfg_input(uint32_t pin_range_start,
  443. uint32_t pin_range_end,
  444. nrf_gpio_pin_pull_t pull_config)
  445. {
  446. /*lint -e{845} // A zero has been given as right argument to operator '|'" */
  447. for (; pin_range_start <= pin_range_end; pin_range_start++)
  448. {
  449. nrf_gpio_cfg_input(pin_range_start, pull_config);
  450. }
  451. }
  452. __STATIC_INLINE void nrf_gpio_cfg(
  453. uint32_t pin_number,
  454. nrf_gpio_pin_dir_t dir,
  455. nrf_gpio_pin_input_t input,
  456. nrf_gpio_pin_pull_t pull,
  457. nrf_gpio_pin_drive_t drive,
  458. nrf_gpio_pin_sense_t sense)
  459. {
  460. NRF_GPIO_Type * reg = nrf_gpio_pin_port_decode(&pin_number);
  461. reg->PIN_CNF[pin_number] = ((uint32_t)dir << GPIO_PIN_CNF_DIR_Pos)
  462. | ((uint32_t)input << GPIO_PIN_CNF_INPUT_Pos)
  463. | ((uint32_t)pull << GPIO_PIN_CNF_PULL_Pos)
  464. | ((uint32_t)drive << GPIO_PIN_CNF_DRIVE_Pos)
  465. | ((uint32_t)sense << GPIO_PIN_CNF_SENSE_Pos);
  466. }
  467. __STATIC_INLINE void nrf_gpio_cfg_output(uint32_t pin_number)
  468. {
  469. nrf_gpio_cfg(
  470. pin_number,
  471. NRF_GPIO_PIN_DIR_OUTPUT,
  472. NRF_GPIO_PIN_INPUT_DISCONNECT,
  473. NRF_GPIO_PIN_NOPULL,
  474. NRF_GPIO_PIN_S0S1,
  475. NRF_GPIO_PIN_NOSENSE);
  476. }
  477. __STATIC_INLINE void nrf_gpio_cfg_input(uint32_t pin_number, nrf_gpio_pin_pull_t pull_config)
  478. {
  479. nrf_gpio_cfg(
  480. pin_number,
  481. NRF_GPIO_PIN_DIR_INPUT,
  482. NRF_GPIO_PIN_INPUT_CONNECT,
  483. pull_config,
  484. NRF_GPIO_PIN_S0S1,
  485. NRF_GPIO_PIN_NOSENSE);
  486. }
  487. __STATIC_INLINE void nrf_gpio_cfg_default(uint32_t pin_number)
  488. {
  489. nrf_gpio_cfg(
  490. pin_number,
  491. NRF_GPIO_PIN_DIR_INPUT,
  492. NRF_GPIO_PIN_INPUT_DISCONNECT,
  493. NRF_GPIO_PIN_NOPULL,
  494. NRF_GPIO_PIN_S0S1,
  495. NRF_GPIO_PIN_NOSENSE);
  496. }
  497. __STATIC_INLINE void nrf_gpio_cfg_watcher(uint32_t pin_number)
  498. {
  499. NRF_GPIO_Type * reg = nrf_gpio_pin_port_decode(&pin_number);
  500. /*lint -e{845} // A zero has been given as right argument to operator '|'" */
  501. uint32_t cnf = reg->PIN_CNF[pin_number] & ~GPIO_PIN_CNF_INPUT_Msk;
  502. reg->PIN_CNF[pin_number] = cnf | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos);
  503. }
  504. __STATIC_INLINE void nrf_gpio_input_disconnect(uint32_t pin_number)
  505. {
  506. NRF_GPIO_Type * reg = nrf_gpio_pin_port_decode(&pin_number);
  507. /*lint -e{845} // A zero has been given as right argument to operator '|'" */
  508. uint32_t cnf = reg->PIN_CNF[pin_number] & ~GPIO_PIN_CNF_INPUT_Msk;
  509. reg->PIN_CNF[pin_number] = cnf | (GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos);
  510. }
  511. __STATIC_INLINE void nrf_gpio_cfg_sense_input(uint32_t pin_number,
  512. nrf_gpio_pin_pull_t pull_config,
  513. nrf_gpio_pin_sense_t sense_config)
  514. {
  515. nrf_gpio_cfg(
  516. pin_number,
  517. NRF_GPIO_PIN_DIR_INPUT,
  518. NRF_GPIO_PIN_INPUT_CONNECT,
  519. pull_config,
  520. NRF_GPIO_PIN_S0S1,
  521. sense_config);
  522. }
  523. __STATIC_INLINE void nrf_gpio_cfg_sense_set(uint32_t pin_number, nrf_gpio_pin_sense_t sense_config)
  524. {
  525. NRF_GPIO_Type * reg = nrf_gpio_pin_port_decode(&pin_number);
  526. /*lint -e{845} // A zero has been given as right argument to operator '|'" */
  527. reg->PIN_CNF[pin_number] &= ~GPIO_PIN_CNF_SENSE_Msk;
  528. reg->PIN_CNF[pin_number] |= (sense_config << GPIO_PIN_CNF_SENSE_Pos);
  529. }
  530. __STATIC_INLINE void nrf_gpio_pin_dir_set(uint32_t pin_number, nrf_gpio_pin_dir_t direction)
  531. {
  532. if (direction == NRF_GPIO_PIN_DIR_INPUT)
  533. {
  534. nrf_gpio_cfg(
  535. pin_number,
  536. NRF_GPIO_PIN_DIR_INPUT,
  537. NRF_GPIO_PIN_INPUT_CONNECT,
  538. NRF_GPIO_PIN_NOPULL,
  539. NRF_GPIO_PIN_S0S1,
  540. NRF_GPIO_PIN_NOSENSE);
  541. }
  542. else
  543. {
  544. NRF_GPIO_Type * reg = nrf_gpio_pin_port_decode(&pin_number);
  545. reg->DIRSET = (1UL << pin_number);
  546. }
  547. }
  548. __STATIC_INLINE void nrf_gpio_pin_set(uint32_t pin_number)
  549. {
  550. NRF_GPIO_Type * reg = nrf_gpio_pin_port_decode(&pin_number);
  551. nrf_gpio_port_out_set(reg, 1UL << pin_number);
  552. }
  553. __STATIC_INLINE void nrf_gpio_pin_clear(uint32_t pin_number)
  554. {
  555. NRF_GPIO_Type * reg = nrf_gpio_pin_port_decode(&pin_number);
  556. nrf_gpio_port_out_clear(reg, 1UL << pin_number);
  557. }
  558. __STATIC_INLINE void nrf_gpio_pin_toggle(uint32_t pin_number)
  559. {
  560. NRF_GPIO_Type * reg = nrf_gpio_pin_port_decode(&pin_number);
  561. uint32_t pins_state = reg->OUT;
  562. reg->OUTSET = (~pins_state & (1UL << pin_number));
  563. reg->OUTCLR = (pins_state & (1UL << pin_number));
  564. }
  565. __STATIC_INLINE void nrf_gpio_pin_write(uint32_t pin_number, uint32_t value)
  566. {
  567. if (value == 0)
  568. {
  569. nrf_gpio_pin_clear(pin_number);
  570. }
  571. else
  572. {
  573. nrf_gpio_pin_set(pin_number);
  574. }
  575. }
  576. __STATIC_INLINE uint32_t nrf_gpio_pin_read(uint32_t pin_number)
  577. {
  578. NRF_GPIO_Type * reg = nrf_gpio_pin_port_decode(&pin_number);
  579. return ((nrf_gpio_port_in_read(reg) >> pin_number) & 1UL);
  580. }
  581. __STATIC_INLINE uint32_t nrf_gpio_pin_out_read(uint32_t pin_number)
  582. {
  583. NRF_GPIO_Type * reg = nrf_gpio_pin_port_decode(&pin_number);
  584. return ((nrf_gpio_port_out_read(reg) >> pin_number) & 1UL);
  585. }
  586. __STATIC_INLINE nrf_gpio_pin_sense_t nrf_gpio_pin_sense_get(uint32_t pin_number)
  587. {
  588. NRF_GPIO_Type * reg = nrf_gpio_pin_port_decode(&pin_number);
  589. return (nrf_gpio_pin_sense_t)((reg->PIN_CNF[pin_number] &
  590. GPIO_PIN_CNF_SENSE_Msk) >> GPIO_PIN_CNF_SENSE_Pos);
  591. }
  592. __STATIC_INLINE nrf_gpio_pin_dir_t nrf_gpio_pin_dir_get(uint32_t pin_number)
  593. {
  594. NRF_GPIO_Type * reg = nrf_gpio_pin_port_decode(&pin_number);
  595. return (nrf_gpio_pin_dir_t)((reg->PIN_CNF[pin_number] &
  596. GPIO_PIN_CNF_DIR_Msk) >> GPIO_PIN_CNF_DIR_Pos);
  597. }
  598. __STATIC_INLINE void nrf_gpio_port_dir_output_set(NRF_GPIO_Type * p_reg, uint32_t out_mask)
  599. {
  600. p_reg->DIRSET = out_mask;
  601. }
  602. __STATIC_INLINE void nrf_gpio_port_dir_input_set(NRF_GPIO_Type * p_reg, uint32_t in_mask)
  603. {
  604. p_reg->DIRCLR = in_mask;
  605. }
  606. __STATIC_INLINE void nrf_gpio_port_dir_write(NRF_GPIO_Type * p_reg, uint32_t value)
  607. {
  608. p_reg->DIR = value;
  609. }
  610. __STATIC_INLINE uint32_t nrf_gpio_port_dir_read(NRF_GPIO_Type const * p_reg)
  611. {
  612. return p_reg->DIR;
  613. }
  614. __STATIC_INLINE uint32_t nrf_gpio_port_in_read(NRF_GPIO_Type const * p_reg)
  615. {
  616. return p_reg->IN;
  617. }
  618. __STATIC_INLINE uint32_t nrf_gpio_port_out_read(NRF_GPIO_Type const * p_reg)
  619. {
  620. return p_reg->OUT;
  621. }
  622. __STATIC_INLINE void nrf_gpio_port_out_write(NRF_GPIO_Type * p_reg, uint32_t value)
  623. {
  624. p_reg->OUT = value;
  625. }
  626. __STATIC_INLINE void nrf_gpio_port_out_set(NRF_GPIO_Type * p_reg, uint32_t set_mask)
  627. {
  628. p_reg->OUTSET = set_mask;
  629. }
  630. __STATIC_INLINE void nrf_gpio_port_out_clear(NRF_GPIO_Type * p_reg, uint32_t clr_mask)
  631. {
  632. p_reg->OUTCLR = clr_mask;
  633. }
  634. __STATIC_INLINE void nrf_gpio_ports_read(uint32_t start_port, uint32_t length, uint32_t * p_masks)
  635. {
  636. NRF_GPIO_Type * gpio_regs[GPIO_COUNT] = GPIO_REG_LIST;
  637. NRFX_ASSERT(start_port + length <= GPIO_COUNT);
  638. uint32_t i;
  639. for (i = start_port; i < (start_port + length); i++)
  640. {
  641. *p_masks = nrf_gpio_port_in_read(gpio_regs[i]);
  642. p_masks++;
  643. }
  644. }
  645. #ifdef GPIO_DETECTMODE_DETECTMODE_LDETECT
  646. __STATIC_INLINE void nrf_gpio_latches_read(uint32_t start_port, uint32_t length, uint32_t * p_masks)
  647. {
  648. NRF_GPIO_Type * gpio_regs[GPIO_COUNT] = GPIO_REG_LIST;
  649. uint32_t i;
  650. for (i = start_port; i < (start_port + length); i++)
  651. {
  652. *p_masks = gpio_regs[i]->LATCH;
  653. p_masks++;
  654. }
  655. }
  656. __STATIC_INLINE uint32_t nrf_gpio_pin_latch_get(uint32_t pin_number)
  657. {
  658. NRF_GPIO_Type * reg = nrf_gpio_pin_port_decode(&pin_number);
  659. return (reg->LATCH & (1 << pin_number)) ? 1 : 0;
  660. }
  661. __STATIC_INLINE void nrf_gpio_pin_latch_clear(uint32_t pin_number)
  662. {
  663. NRF_GPIO_Type * reg = nrf_gpio_pin_port_decode(&pin_number);
  664. reg->LATCH = (1 << pin_number);
  665. }
  666. #endif
  667. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  668. /** @} */
  669. #ifdef __cplusplus
  670. }
  671. #endif
  672. #endif // NRF_GPIO_H__