nrf_gpio.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #ifndef NRF_GPIO_H__
  13. #define NRF_GPIO_H__
  14. #include "nrf.h"
  15. #include <stdbool.h>
  16. /**
  17. * @defgroup nrf_gpio GPIO abstraction
  18. * @{
  19. * @ingroup nrf_drivers
  20. * @brief GPIO pin abstraction and port abstraction for reading and writing byte-wise to GPIO ports.
  21. *
  22. * Here, the GPIO ports are defined as follows:
  23. * - Port 0 -> pin 0-7
  24. * - Port 1 -> pin 8-15
  25. * - Port 2 -> pin 16-23
  26. * - Port 3 -> pin 24-31
  27. */
  28. #define NUMBER_OF_PINS 32
  29. /**
  30. * @brief Enumerator used for selecting between port 0 - 3.
  31. */
  32. typedef enum
  33. {
  34. NRF_GPIO_PORT_SELECT_PORT0 = 0, ///< Port 0 (GPIO pin 0-7)
  35. NRF_GPIO_PORT_SELECT_PORT1, ///< Port 1 (GPIO pin 8-15)
  36. NRF_GPIO_PORT_SELECT_PORT2, ///< Port 2 (GPIO pin 16-23)
  37. NRF_GPIO_PORT_SELECT_PORT3, ///< Port 3 (GPIO pin 24-31)
  38. } nrf_gpio_port_select_t;
  39. /**
  40. * @brief Enumerator used for setting the direction of a GPIO port.
  41. */
  42. typedef enum
  43. {
  44. NRF_GPIO_PORT_DIR_OUTPUT, ///< Output
  45. NRF_GPIO_PORT_DIR_INPUT ///< Input
  46. } nrf_gpio_port_dir_t;
  47. /**
  48. * @brief Pin direction definitions.
  49. */
  50. typedef enum
  51. {
  52. NRF_GPIO_PIN_DIR_INPUT = GPIO_PIN_CNF_DIR_Input, ///< Input
  53. NRF_GPIO_PIN_DIR_OUTPUT = GPIO_PIN_CNF_DIR_Output ///< Output
  54. } nrf_gpio_pin_dir_t;
  55. /**
  56. * @brief Connection of input buffer
  57. */
  58. typedef enum
  59. {
  60. NRF_GPIO_PIN_INPUT_CONNECT = GPIO_PIN_CNF_INPUT_Connect, ///< Connect input buffer
  61. NRF_GPIO_PIN_INPUT_DISCONNECT = GPIO_PIN_CNF_INPUT_Disconnect ///< Disconnect input buffer
  62. } nrf_gpio_pin_input_t;
  63. /**
  64. * @brief Enumerator used for selecting the pin to be pulled down or up at the time of pin configuration
  65. */
  66. typedef enum
  67. {
  68. NRF_GPIO_PIN_NOPULL = GPIO_PIN_CNF_PULL_Disabled, ///< Pin pullup resistor disabled
  69. NRF_GPIO_PIN_PULLDOWN = GPIO_PIN_CNF_PULL_Pulldown, ///< Pin pulldown resistor enabled
  70. NRF_GPIO_PIN_PULLUP = GPIO_PIN_CNF_PULL_Pullup, ///< Pin pullup resistor enabled
  71. } nrf_gpio_pin_pull_t;
  72. /**
  73. * @brief Enumerator used for selecting output drive mode
  74. */
  75. typedef enum
  76. {
  77. NRF_GPIO_PIN_S0S1 = GPIO_PIN_CNF_DRIVE_S0S1, ///< !< Standard '0', standard '1'
  78. NRF_GPIO_PIN_H0S1 = GPIO_PIN_CNF_DRIVE_H0S1, ///< !< High drive '0', standard '1'
  79. NRF_GPIO_PIN_S0H1 = GPIO_PIN_CNF_DRIVE_S0H1, ///< !< Standard '0', high drive '1'
  80. NRF_GPIO_PIN_H0H1 = GPIO_PIN_CNF_DRIVE_H0H1, ///< !< High drive '0', high 'drive '1''
  81. NRF_GPIO_PIN_D0S1 = GPIO_PIN_CNF_DRIVE_D0S1, ///< !< Disconnect '0' standard '1'
  82. NRF_GPIO_PIN_D0H1 = GPIO_PIN_CNF_DRIVE_D0H1, ///< !< Disconnect '0', high drive '1'
  83. NRF_GPIO_PIN_S0D1 = GPIO_PIN_CNF_DRIVE_S0D1, ///< !< Standard '0'. disconnect '1'
  84. NRF_GPIO_PIN_H0D1 = GPIO_PIN_CNF_DRIVE_H0D1, ///< !< High drive '0', disconnect '1'
  85. } nrf_gpio_pin_drive_t;
  86. /**
  87. * @brief Enumerator used for selecting the pin to sense high or low level on the pin input.
  88. */
  89. typedef enum
  90. {
  91. NRF_GPIO_PIN_NOSENSE = GPIO_PIN_CNF_SENSE_Disabled, ///< Pin sense level disabled.
  92. NRF_GPIO_PIN_SENSE_LOW = GPIO_PIN_CNF_SENSE_Low, ///< Pin sense low level.
  93. NRF_GPIO_PIN_SENSE_HIGH = GPIO_PIN_CNF_SENSE_High, ///< Pin sense high level.
  94. } nrf_gpio_pin_sense_t;
  95. /**
  96. * @brief Function for configuring the GPIO pin range as outputs with normal drive strength.
  97. * This function can be used to configure pin range as simple output with gate driving GPIO_PIN_CNF_DRIVE_S0S1 (normal cases).
  98. *
  99. * @param pin_range_start specifies the start number (inclusive) in the range of pin numbers to be configured (allowed values 0-30)
  100. *
  101. * @param pin_range_end specifies the end number (inclusive) in the range of pin numbers to be configured (allowed values 0-30)
  102. *
  103. * @note For configuring only one pin as output use @ref nrf_gpio_cfg_output
  104. * Sense capability on the pin is disabled, and input is disconnected from the buffer as the pins are configured as output.
  105. */
  106. __STATIC_INLINE void nrf_gpio_range_cfg_output(uint32_t pin_range_start, uint32_t pin_range_end);
  107. /**
  108. * @brief Function for configuring the GPIO pin range as inputs with given initial value set, hiding inner details.
  109. * This function can be used to configure pin range as simple input.
  110. *
  111. * @param pin_range_start specifies the start number (inclusive) in the range of pin numbers to be configured (allowed values 0-30)
  112. *
  113. * @param pin_range_end specifies the end number (inclusive) in the range of pin numbers to be configured (allowed values 0-30)
  114. *
  115. * @param pull_config State of the pin range pull resistor (no pull, pulled down or pulled high)
  116. *
  117. * @note For configuring only one pin as input use @ref nrf_gpio_cfg_input
  118. * Sense capability on the pin is disabled, and input is connected to buffer so that the GPIO->IN register is readable
  119. */
  120. __STATIC_INLINE void nrf_gpio_range_cfg_input(uint32_t pin_range_start, uint32_t pin_range_end, nrf_gpio_pin_pull_t pull_config);
  121. /**
  122. * @brief Pin configuration function
  123. *
  124. * The main pin configuration function.
  125. * This function allows to set any aspect in PIN_CNF register.
  126. * @param pin_number Specifies the pin number (allowed values 0-31).
  127. * @param dir Pin direction
  128. * @param input Connect or disconnect input buffer
  129. * @param pull Pull configuration
  130. * @param drive Drive configuration
  131. * @param sense Pin sensing mechanism
  132. */
  133. __STATIC_INLINE void nrf_gpio_cfg(
  134. uint32_t pin_number,
  135. nrf_gpio_pin_dir_t dir,
  136. nrf_gpio_pin_input_t input,
  137. nrf_gpio_pin_pull_t pull,
  138. nrf_gpio_pin_drive_t drive,
  139. nrf_gpio_pin_sense_t sense);
  140. /**
  141. * @brief Function for configuring the given GPIO pin number as output with given initial value set, hiding inner details.
  142. * This function can be used to configure pin range as simple input with gate driving GPIO_PIN_CNF_DRIVE_S0S1 (normal cases).
  143. *
  144. * @param pin_number specifies the pin number (allowed values 0-31)
  145. *
  146. * @note Sense capability on the pin is disabled, and input is disconnected from the buffer as the pins are configured as output.
  147. */
  148. __STATIC_INLINE void nrf_gpio_cfg_output(uint32_t pin_number);
  149. /**
  150. * @brief Function for configuring the given GPIO pin number as input with given initial value set, hiding inner details.
  151. * This function can be used to configure pin range as simple input with gate driving GPIO_PIN_CNF_DRIVE_S0S1 (normal cases).
  152. *
  153. * @param pin_number Specifies the pin number (allowed values 0-30).
  154. * @param pull_config State of the pin range pull resistor (no pull, pulled down or pulled high).
  155. *
  156. * @note Sense capability on the pin is disabled, and input is connected to buffer so that the GPIO->IN register is readable
  157. */
  158. __STATIC_INLINE void nrf_gpio_cfg_input(uint32_t pin_number, nrf_gpio_pin_pull_t pull_config);
  159. /**
  160. * @brief Function for reseting pin configuration to its default state.
  161. *
  162. * @param pin_number Specifies the pin number (allowed values 0-31).
  163. */
  164. __STATIC_INLINE void nrf_gpio_cfg_default(uint32_t pin_number);
  165. /**
  166. * @brief Function for configuring the given GPIO pin number as a watcher. Only input is connected.
  167. *
  168. * @param pin_number Specifies the pin number (allowed values 0-31).
  169. *
  170. */
  171. __STATIC_INLINE void nrf_gpio_cfg_watcher(uint32_t pin_number);
  172. /**
  173. * @brief Function for disconnecting input for the given GPIO.
  174. *
  175. * @param pin_number Specifies the pin number (allowed values 0-31).
  176. *
  177. */
  178. __STATIC_INLINE void nrf_gpio_input_disconnect(uint32_t pin_number);
  179. /**
  180. * @brief Function for configuring the given GPIO pin number as input with given initial value set, hiding inner details.
  181. * This function can be used to configure pin range as simple input with gate driving GPIO_PIN_CNF_DRIVE_S0S1 (normal cases).
  182. * Sense capability on the pin is configurable, and input is connected to buffer so that the GPIO->IN register is readable.
  183. *
  184. * @param pin_number Specifies the pin number (allowed values 0-30).
  185. * @param pull_config State of the pin pull resistor (no pull, pulled down or pulled high).
  186. * @param sense_config Sense level of the pin (no sense, sense low or sense high).
  187. */
  188. __STATIC_INLINE void nrf_gpio_cfg_sense_input(uint32_t pin_number, nrf_gpio_pin_pull_t pull_config, nrf_gpio_pin_sense_t sense_config);
  189. /**
  190. * @brief Function for configuring sense level for the given GPIO.
  191. *
  192. * @param pin_number Specifies the pin number of gpio pin numbers to be configured (allowed values 0-30).
  193. * @param sense_config Sense configuration.
  194. *
  195. */
  196. __STATIC_INLINE void nrf_gpio_cfg_sense_set(uint32_t pin_number, nrf_gpio_pin_sense_t sense_config);
  197. /**
  198. * @brief Function for setting the direction for a GPIO pin.
  199. *
  200. * @param pin_number specifies the pin number (0-31) for which to
  201. * set the direction.
  202. *
  203. * @param direction specifies the direction
  204. */
  205. __STATIC_INLINE void nrf_gpio_pin_dir_set(uint32_t pin_number, nrf_gpio_pin_dir_t direction);
  206. /**
  207. * @brief Function for setting a GPIO pin.
  208. *
  209. * Note that the pin must be configured as an output for this
  210. * function to have any effect.
  211. *
  212. * @param pin_number Specifies the pin number (0-31) to set.
  213. */
  214. __STATIC_INLINE void nrf_gpio_pin_set(uint32_t pin_number);
  215. /**
  216. * @brief Function for setting GPIO pins.
  217. *
  218. * Note that the pins must be configured as outputs for this
  219. * function to have any effect.
  220. *
  221. * @param pin_mask Specifies the pins to set.
  222. */
  223. __STATIC_INLINE void nrf_gpio_pins_set(uint32_t pin_mask);
  224. /**
  225. * @brief Function for clearing a GPIO pin.
  226. *
  227. * Note that the pin must be configured as an output for this
  228. * function to have any effect.
  229. *
  230. * @param pin_number Specifies the pin number (0-31) to clear.
  231. */
  232. __STATIC_INLINE void nrf_gpio_pin_clear(uint32_t pin_number);
  233. /**
  234. * @brief Function for clearing GPIO pins.
  235. *
  236. * Note that the pins must be configured as outputs for this
  237. * function to have any effect.
  238. *
  239. * @param pin_mask Specifies the pins to clear.
  240. */
  241. __STATIC_INLINE void nrf_gpio_pins_clear(uint32_t pin_mask);
  242. /**
  243. * @brief Function for toggling a GPIO pin.
  244. *
  245. * Note that the pin must be configured as an output for this
  246. * function to have any effect.
  247. *
  248. * @param pin_number Specifies the pin number (0-31) to toggle.
  249. */
  250. __STATIC_INLINE void nrf_gpio_pin_toggle(uint32_t pin_number);
  251. /**
  252. * @brief Function for toggling GPIO pins.
  253. *
  254. * Note that the pins must be configured as outputs for this
  255. * function to have any effect.
  256. *
  257. * @param pin_mask Specifies the pins to toggle.
  258. */
  259. __STATIC_INLINE void nrf_gpio_pins_toggle(uint32_t pin_mask);
  260. /**
  261. * @brief Function for writing a value to a GPIO pin.
  262. *
  263. * Note that the pin must be configured as an output for this
  264. * function to have any effect.
  265. *
  266. * @param pin_number specifies the pin number (0-31) to
  267. * write.
  268. *
  269. * @param value specifies the value to be written to the pin.
  270. * @arg 0 clears the pin
  271. * @arg >=1 sets the pin.
  272. */
  273. __STATIC_INLINE void nrf_gpio_pin_write(uint32_t pin_number, uint32_t value);
  274. /**
  275. * @brief Function for reading the input level of a GPIO pin.
  276. *
  277. * Note that the pin must have input connected for the value
  278. * returned from this function to be valid.
  279. *
  280. * @param pin_number specifies the pin number (0-31) to
  281. * read.
  282. *
  283. * @return
  284. * @retval 0 if the pin input level is low.
  285. * @retval 1 if the pin input level is high.
  286. * @retval > 1 should never occur.
  287. */
  288. __STATIC_INLINE uint32_t nrf_gpio_pin_read(uint32_t pin_number);
  289. /**
  290. * @brief Function for reading the input level of all GPIO pins.
  291. *
  292. * Note that the pin must have input connected for the value
  293. * returned from this function to be valid.
  294. *
  295. * @retval Status of input of all pins
  296. */
  297. __STATIC_INLINE uint32_t nrf_gpio_pins_read(void);
  298. /**
  299. * @brief Function for reading the sense configuration of a GPIO pin.
  300. *
  301. * @param pin_number specifies the pin number (0-31) to
  302. * read.
  303. *
  304. * @retval Sense configuration
  305. */
  306. __STATIC_INLINE nrf_gpio_pin_sense_t nrf_gpio_pin_sense_get(uint32_t pin_number);
  307. /**
  308. * @brief Generic function for writing a single byte of a 32 bit word at a given
  309. * address.
  310. *
  311. * This function should not be called from outside the nrf_gpio
  312. * abstraction layer.
  313. *
  314. * @param word_address is the address of the word to be written.
  315. *
  316. * @param byte_no is the word byte number (0-3) to be written.
  317. *
  318. * @param value is the value to be written to byte "byte_no" of word
  319. * at address "word_address"
  320. */
  321. __STATIC_INLINE void nrf_gpio_word_byte_write(volatile uint32_t * word_address, uint8_t byte_no, uint8_t value);
  322. /**
  323. * @brief Generic function for reading a single byte of a 32 bit word at a given
  324. * address.
  325. *
  326. * This function should not be called from outside the nrf_gpio
  327. * abstraction layer.
  328. *
  329. * @param word_address is the address of the word to be read.
  330. *
  331. * @param byte_no is the byte number (0-3) of the word to be read.
  332. *
  333. * @return byte "byte_no" of word at address "word_address".
  334. */
  335. __STATIC_INLINE uint8_t nrf_gpio_word_byte_read(const volatile uint32_t* word_address, uint8_t byte_no);
  336. /**
  337. * @brief Function for setting the direction of a port.
  338. *
  339. * @param port is the port for which to set the direction.
  340. *
  341. * @param dir direction to be set for this port.
  342. */
  343. __STATIC_INLINE void nrf_gpio_port_dir_set(nrf_gpio_port_select_t port, nrf_gpio_port_dir_t dir);
  344. /**
  345. * @brief Function for reading a GPIO port.
  346. *
  347. * @param port is the port to read.
  348. *
  349. * @return the input value on this port.
  350. */
  351. __STATIC_INLINE uint8_t nrf_gpio_port_read(nrf_gpio_port_select_t port);
  352. /**
  353. * @brief Function for writing to a GPIO port.
  354. *
  355. * @param port is the port to write.
  356. *
  357. * @param value is the value to write to this port.
  358. *
  359. * @sa nrf_gpio_port_dir_set()
  360. */
  361. __STATIC_INLINE void nrf_gpio_port_write(nrf_gpio_port_select_t port, uint8_t value);
  362. /**
  363. * @brief Function for setting individual pins on GPIO port.
  364. *
  365. * @param port is the port for which to set the pins.
  366. *
  367. * @param set_mask is a mask specifying which pins to set. A bit
  368. * set to 1 indicates that the corresponding port pin shall be
  369. * set.
  370. *
  371. * @sa nrf_gpio_port_dir_set()
  372. */
  373. __STATIC_INLINE void nrf_gpio_port_set(nrf_gpio_port_select_t port, uint8_t set_mask);
  374. /**
  375. * @brief Function for clearing individual pins on GPIO port.
  376. *
  377. * @param port is the port for which to clear the pins.
  378. *
  379. * @param clr_mask is a mask specifying which pins to clear. A bit
  380. * set to 1 indicates that the corresponding port pin shall be
  381. * cleared.
  382. *
  383. * @sa nrf_gpio_port_dir_set()
  384. */
  385. __STATIC_INLINE void nrf_gpio_port_clear(nrf_gpio_port_select_t port, uint8_t clr_mask);
  386. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  387. __STATIC_INLINE void nrf_gpio_range_cfg_output(uint32_t pin_range_start, uint32_t pin_range_end)
  388. {
  389. /*lint -e{845} // A zero has been given as right argument to operator '|'" */
  390. for (; pin_range_start <= pin_range_end; pin_range_start++)
  391. {
  392. nrf_gpio_cfg_output(pin_range_start);
  393. }
  394. }
  395. __STATIC_INLINE void nrf_gpio_range_cfg_input(uint32_t pin_range_start, uint32_t pin_range_end, nrf_gpio_pin_pull_t pull_config)
  396. {
  397. /*lint -e{845} // A zero has been given as right argument to operator '|'" */
  398. for (; pin_range_start <= pin_range_end; pin_range_start++)
  399. {
  400. nrf_gpio_cfg_input(pin_range_start, pull_config);
  401. }
  402. }
  403. __STATIC_INLINE void nrf_gpio_cfg(
  404. uint32_t pin_number,
  405. nrf_gpio_pin_dir_t dir,
  406. nrf_gpio_pin_input_t input,
  407. nrf_gpio_pin_pull_t pull,
  408. nrf_gpio_pin_drive_t drive,
  409. nrf_gpio_pin_sense_t sense)
  410. {
  411. NRF_GPIO->PIN_CNF[pin_number] = ((uint32_t)dir << GPIO_PIN_CNF_DIR_Pos)
  412. | ((uint32_t)input << GPIO_PIN_CNF_INPUT_Pos)
  413. | ((uint32_t)pull << GPIO_PIN_CNF_PULL_Pos)
  414. | ((uint32_t)drive << GPIO_PIN_CNF_DRIVE_Pos)
  415. | ((uint32_t)sense << GPIO_PIN_CNF_SENSE_Pos);
  416. }
  417. __STATIC_INLINE void nrf_gpio_cfg_output(uint32_t pin_number)
  418. {
  419. nrf_gpio_cfg(
  420. pin_number,
  421. NRF_GPIO_PIN_DIR_OUTPUT,
  422. NRF_GPIO_PIN_INPUT_DISCONNECT,
  423. NRF_GPIO_PIN_NOPULL,
  424. NRF_GPIO_PIN_S0S1,
  425. NRF_GPIO_PIN_NOSENSE);
  426. }
  427. __STATIC_INLINE void nrf_gpio_cfg_input(uint32_t pin_number, nrf_gpio_pin_pull_t pull_config)
  428. {
  429. nrf_gpio_cfg(
  430. pin_number,
  431. NRF_GPIO_PIN_DIR_INPUT,
  432. NRF_GPIO_PIN_INPUT_CONNECT,
  433. pull_config,
  434. NRF_GPIO_PIN_S0S1,
  435. NRF_GPIO_PIN_NOSENSE);
  436. }
  437. __STATIC_INLINE void nrf_gpio_cfg_default(uint32_t pin_number)
  438. {
  439. nrf_gpio_cfg(
  440. pin_number,
  441. NRF_GPIO_PIN_DIR_INPUT,
  442. NRF_GPIO_PIN_INPUT_DISCONNECT,
  443. NRF_GPIO_PIN_NOPULL,
  444. NRF_GPIO_PIN_S0S1,
  445. NRF_GPIO_PIN_NOSENSE);
  446. }
  447. __STATIC_INLINE void nrf_gpio_cfg_watcher(uint32_t pin_number)
  448. {
  449. /*lint -e{845} // A zero has been given as right argument to operator '|'" */
  450. uint32_t cnf = NRF_GPIO->PIN_CNF[pin_number] & ~GPIO_PIN_CNF_INPUT_Msk;
  451. NRF_GPIO->PIN_CNF[pin_number] = cnf | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos);
  452. }
  453. __STATIC_INLINE void nrf_gpio_input_disconnect(uint32_t pin_number)
  454. {
  455. /*lint -e{845} // A zero has been given as right argument to operator '|'" */
  456. uint32_t cnf = NRF_GPIO->PIN_CNF[pin_number] & ~GPIO_PIN_CNF_INPUT_Msk;
  457. NRF_GPIO->PIN_CNF[pin_number] = cnf | (GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos);
  458. }
  459. __STATIC_INLINE void nrf_gpio_cfg_sense_input(uint32_t pin_number, nrf_gpio_pin_pull_t pull_config, nrf_gpio_pin_sense_t sense_config)
  460. {
  461. nrf_gpio_cfg(
  462. pin_number,
  463. NRF_GPIO_PIN_DIR_INPUT,
  464. NRF_GPIO_PIN_INPUT_CONNECT,
  465. pull_config,
  466. NRF_GPIO_PIN_S0S1,
  467. sense_config);
  468. }
  469. __STATIC_INLINE void nrf_gpio_cfg_sense_set(uint32_t pin_number, nrf_gpio_pin_sense_t sense_config)
  470. {
  471. /*lint -e{845} // A zero has been given as right argument to operator '|'" */
  472. //uint32_t cnf = NRF_GPIO->PIN_CNF[pin_number] & ~GPIO_PIN_CNF_SENSE_Msk;
  473. NRF_GPIO->PIN_CNF[pin_number] &= ~GPIO_PIN_CNF_SENSE_Msk;
  474. NRF_GPIO->PIN_CNF[pin_number] |= (sense_config << GPIO_PIN_CNF_SENSE_Pos);
  475. }
  476. __STATIC_INLINE void nrf_gpio_pin_dir_set(uint32_t pin_number, nrf_gpio_pin_dir_t direction)
  477. {
  478. if(direction == NRF_GPIO_PIN_DIR_INPUT)
  479. {
  480. nrf_gpio_cfg(
  481. pin_number,
  482. NRF_GPIO_PIN_DIR_INPUT,
  483. NRF_GPIO_PIN_INPUT_CONNECT,
  484. NRF_GPIO_PIN_NOPULL,
  485. NRF_GPIO_PIN_S0S1,
  486. NRF_GPIO_PIN_NOSENSE);
  487. }
  488. else
  489. {
  490. NRF_GPIO->DIRSET = (1UL << pin_number);
  491. }
  492. }
  493. __STATIC_INLINE void nrf_gpio_pin_set(uint32_t pin_number)
  494. {
  495. NRF_GPIO->OUTSET = (1UL << pin_number);
  496. }
  497. __STATIC_INLINE void nrf_gpio_pins_set(uint32_t pin_mask)
  498. {
  499. NRF_GPIO->OUTSET = pin_mask;
  500. }
  501. __STATIC_INLINE void nrf_gpio_pin_clear(uint32_t pin_number)
  502. {
  503. NRF_GPIO->OUTCLR = (1UL << pin_number);
  504. }
  505. __STATIC_INLINE void nrf_gpio_pins_clear(uint32_t pin_mask)
  506. {
  507. NRF_GPIO->OUTCLR = pin_mask;
  508. }
  509. __STATIC_INLINE void nrf_gpio_pin_toggle(uint32_t pin_number)
  510. {
  511. nrf_gpio_pins_toggle(1UL << pin_number);
  512. }
  513. __STATIC_INLINE void nrf_gpio_pins_toggle(uint32_t pin_mask)
  514. {
  515. uint32_t pins_state = NRF_GPIO->OUT;
  516. NRF_GPIO->OUTSET = (~pins_state & pin_mask);
  517. NRF_GPIO->OUTCLR = ( pins_state & pin_mask);
  518. }
  519. __STATIC_INLINE void nrf_gpio_pin_write(uint32_t pin_number, uint32_t value)
  520. {
  521. if (value == 0)
  522. {
  523. nrf_gpio_pin_clear(pin_number);
  524. }
  525. else
  526. {
  527. nrf_gpio_pin_set(pin_number);
  528. }
  529. }
  530. __STATIC_INLINE uint32_t nrf_gpio_pin_read(uint32_t pin_number)
  531. {
  532. return ((NRF_GPIO->IN >> pin_number) & 1UL);
  533. }
  534. __STATIC_INLINE uint32_t nrf_gpio_pins_read(void)
  535. {
  536. return NRF_GPIO->IN;
  537. }
  538. __STATIC_INLINE nrf_gpio_pin_sense_t nrf_gpio_pin_sense_get(uint32_t pin_number)
  539. {
  540. return (nrf_gpio_pin_sense_t)((NRF_GPIO->PIN_CNF[pin_number] & GPIO_PIN_CNF_SENSE_Msk) >> GPIO_PIN_CNF_SENSE_Pos);
  541. }
  542. __STATIC_INLINE void nrf_gpio_word_byte_write(volatile uint32_t * word_address, uint8_t byte_no, uint8_t value)
  543. {
  544. *((volatile uint8_t*)(word_address) + byte_no) = value;
  545. }
  546. __STATIC_INLINE uint8_t nrf_gpio_word_byte_read(const volatile uint32_t* word_address, uint8_t byte_no)
  547. {
  548. return (*((const volatile uint8_t*)(word_address) + byte_no));
  549. }
  550. __STATIC_INLINE void nrf_gpio_port_dir_set(nrf_gpio_port_select_t port, nrf_gpio_port_dir_t dir)
  551. {
  552. if (dir == NRF_GPIO_PORT_DIR_OUTPUT)
  553. {
  554. nrf_gpio_word_byte_write(&NRF_GPIO->DIRSET, port, 0xFF);
  555. }
  556. else
  557. {
  558. nrf_gpio_range_cfg_input(port*8, (port+1)*8-1, NRF_GPIO_PIN_NOPULL);
  559. }
  560. }
  561. __STATIC_INLINE uint8_t nrf_gpio_port_read(nrf_gpio_port_select_t port)
  562. {
  563. return nrf_gpio_word_byte_read(&NRF_GPIO->IN, port);
  564. }
  565. __STATIC_INLINE void nrf_gpio_port_write(nrf_gpio_port_select_t port, uint8_t value)
  566. {
  567. nrf_gpio_word_byte_write(&NRF_GPIO->OUT, port, value);
  568. }
  569. __STATIC_INLINE void nrf_gpio_port_set(nrf_gpio_port_select_t port, uint8_t set_mask)
  570. {
  571. nrf_gpio_word_byte_write(&NRF_GPIO->OUTSET, port, set_mask);
  572. }
  573. __STATIC_INLINE void nrf_gpio_port_clear(nrf_gpio_port_select_t port, uint8_t clr_mask)
  574. {
  575. nrf_gpio_word_byte_write(&NRF_GPIO->OUTCLR, port, clr_mask);
  576. }
  577. #endif //SUPPRESS_INLINE_IMPLEMENTATION
  578. /** @} */
  579. #endif