nrf_drv_gpiote.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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_DRV_GPIOTE__
  13. #define NRF_DRV_GPIOTE__
  14. /**
  15. * @addtogroup nrf_gpiote GPIOTE abstraction and driver
  16. * @ingroup nrf_drivers
  17. * @brief GPIOTE APIs.
  18. * @defgroup nrf_drv_gpiote GPIOTE driver
  19. * @{
  20. * @ingroup nrf_gpiote
  21. * @brief GPIOTE driver for managing input and output pins.
  22. */
  23. #include "nrf_gpiote.h"
  24. #include "nrf_gpio.h"
  25. #include "nrf_drv_config.h"
  26. #include "sdk_errors.h"
  27. #include <stdint.h>
  28. #include <stdbool.h>
  29. /**@brief Input pin configuration. */
  30. typedef struct
  31. {
  32. nrf_gpiote_polarity_t sense; /**< Transition that triggers interrupt. */
  33. nrf_gpio_pin_pull_t pull; /**< Pulling mode. */
  34. bool is_watcher; /**< True when the input pin is tracking an output pin. */
  35. bool hi_accuracy;/**< True when high accuracy (IN_EVENT) is used. */
  36. } nrf_drv_gpiote_in_config_t;
  37. /**@brief Macro for configuring a pin to use a GPIO IN or PORT EVENT to detect low-to-high transition.
  38. * @details Set hi_accu to true to use IN_EVENT. */
  39. #define GPIOTE_CONFIG_IN_SENSE_LOTOHI(hi_accu) \
  40. { \
  41. .is_watcher = false, \
  42. .hi_accuracy = hi_accu, \
  43. .pull = NRF_GPIO_PIN_NOPULL, \
  44. .sense = NRF_GPIOTE_POLARITY_LOTOHI, \
  45. }
  46. /**@brief Macro for configuring a pin to use a GPIO IN or PORT EVENT to detect high-to-low transition.
  47. * @details Set hi_accu to true to use IN_EVENT. */
  48. #define GPIOTE_CONFIG_IN_SENSE_HITOLO(hi_accu) \
  49. { \
  50. .is_watcher = false, \
  51. .hi_accuracy = hi_accu, \
  52. .pull = NRF_GPIO_PIN_NOPULL, \
  53. .sense = NRF_GPIOTE_POLARITY_HITOLO, \
  54. }
  55. /**@brief Macro for configuring a pin to use a GPIO IN or PORT EVENT to detect any change on the pin.
  56. * @details Set hi_accu to true to use IN_EVENT.*/
  57. #define GPIOTE_CONFIG_IN_SENSE_TOGGLE(hi_accu) \
  58. { \
  59. .is_watcher = false, \
  60. .hi_accuracy = hi_accu, \
  61. .pull = NRF_GPIO_PIN_NOPULL, \
  62. .sense = NRF_GPIOTE_POLARITY_TOGGLE, \
  63. }
  64. /**@brief Output pin configuration. */
  65. typedef struct
  66. {
  67. nrf_gpiote_polarity_t action; /**< Configuration of the pin task. */
  68. nrf_gpiote_outinit_t init_state; /**< Initial state of the output pin. */
  69. bool task_pin; /**< True if the pin is controlled by a GPIOTE task. */
  70. } nrf_drv_gpiote_out_config_t;
  71. /**@brief Macro for configuring a pin to use as output. GPIOTE is not used for the pin. */
  72. #define GPIOTE_CONFIG_OUT_SIMPLE(init_high) \
  73. { \
  74. .init_state = init_high ? NRF_GPIOTE_INITIAL_VALUE_HIGH : NRF_GPIOTE_INITIAL_VALUE_LOW, \
  75. .task_pin = false, \
  76. }
  77. /**@brief Macro for configuring a pin to use the GPIO OUT TASK to change the state from high to low.
  78. * @details The task will clear the pin. Therefore, the pin is set initially. */
  79. #define GPIOTE_CONFIG_OUT_TASK_LOW \
  80. { \
  81. .init_state = NRF_GPIOTE_INITIAL_VALUE_HIGH, \
  82. .task_pin = true, \
  83. .action = NRF_GPIOTE_POLARITY_HITOLO, \
  84. }
  85. /**@brief Macro for configuring a pin to use the GPIO OUT TASK to change the state from low to high.
  86. * @details The task will set the pin. Therefore, the pin is cleared initially. */
  87. #define GPIOTE_CONFIG_OUT_TASK_HIGH \
  88. { \
  89. .init_state = NRF_GPIOTE_INITIAL_VALUE_LOW, \
  90. .task_pin = true, \
  91. .action = NRF_GPIOTE_POLARITY_LOTOHI, \
  92. }
  93. /**@brief Macro for configuring a pin to use the GPIO OUT TASK to toggle the pin state.
  94. * @details The initial pin state must be provided. */
  95. #define GPIOTE_CONFIG_OUT_TASK_TOGGLE(init_high) \
  96. { \
  97. .init_state = init_high ? NRF_GPIOTE_INITIAL_VALUE_HIGH : NRF_GPIOTE_INITIAL_VALUE_LOW, \
  98. .task_pin = true, \
  99. .action = NRF_GPIOTE_POLARITY_TOGGLE, \
  100. }
  101. /** @brief Pin. */
  102. typedef uint32_t nrf_drv_gpiote_pin_t;
  103. /**
  104. * @brief Pin event handler prototype.
  105. * @param pin Pin that triggered this event.
  106. * @param action Action that lead to triggering this event.
  107. */
  108. typedef void (*nrf_drv_gpiote_evt_handler_t)(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action);
  109. /**
  110. * @brief Function for initializing the GPIOTE module.
  111. *
  112. * @details Only static configuration is supported to prevent the shared
  113. * resource being customized by the initiator.
  114. *
  115. * @retval NRF_SUCCESS If initialization was successful.
  116. * @retval NRF_ERROR_INVALID_STATE If the driver was already initialized.
  117. */
  118. ret_code_t nrf_drv_gpiote_init(void);
  119. /**
  120. * @brief Function for checking if the GPIOTE module is initialized.
  121. *
  122. * @details The GPIOTE module is a shared module. Therefore, you should check if
  123. * the module is already initialized and skip initialization if it is.
  124. *
  125. * @retval true If the module is already initialized.
  126. * @retval false If the module is not initialized.
  127. */
  128. bool nrf_drv_gpiote_is_init(void);
  129. /**
  130. * @brief Function for uninitializing the GPIOTE module.
  131. */
  132. void nrf_drv_gpiote_uninit(void);
  133. /**
  134. * @brief Function for initializing a GPIOTE output pin.
  135. * @details The output pin can be controlled by the CPU or by PPI. The initial
  136. * configuration specifies which mode is used. If PPI mode is used, the driver
  137. * attempts to allocate one of the available GPIOTE channels. If no channel is
  138. * available, an error is returned.
  139. *
  140. * @param[in] pin Pin.
  141. * @param[in] p_config Initial configuration.
  142. *
  143. * @retval NRF_SUCCESS If initialization was successful.
  144. * @retval NRF_ERROR_INVALID_STATE If the driver is not initialized or the pin is already used.
  145. * @retval NRF_ERROR_NO_MEM If no GPIOTE channel is available.
  146. */
  147. ret_code_t nrf_drv_gpiote_out_init(nrf_drv_gpiote_pin_t pin,
  148. nrf_drv_gpiote_out_config_t const * p_config);
  149. /**
  150. * @brief Function for uninitializing a GPIOTE output pin.
  151. * @details The driver frees the GPIOTE channel if the output pin was using one.
  152. *
  153. * @param[in] pin Pin.
  154. */
  155. void nrf_drv_gpiote_out_uninit(nrf_drv_gpiote_pin_t pin);
  156. /**
  157. * @brief Function for setting a GPIOTE output pin.
  158. *
  159. * @param[in] pin Pin.
  160. */
  161. void nrf_drv_gpiote_out_set(nrf_drv_gpiote_pin_t pin);
  162. /**
  163. * @brief Function for clearing a GPIOTE output pin.
  164. *
  165. * @param[in] pin Pin.
  166. */
  167. void nrf_drv_gpiote_out_clear(nrf_drv_gpiote_pin_t pin);
  168. /**
  169. * @brief Function for toggling a GPIOTE output pin.
  170. *
  171. * @param[in] pin Pin.
  172. */
  173. void nrf_drv_gpiote_out_toggle(nrf_drv_gpiote_pin_t pin);
  174. /**
  175. * @brief Function for enabling a GPIOTE output pin task.
  176. *
  177. * @param[in] pin Pin.
  178. */
  179. void nrf_drv_gpiote_out_task_enable(nrf_drv_gpiote_pin_t pin);
  180. /**
  181. * @brief Function for disabling a GPIOTE output pin task.
  182. *
  183. * @param[in] pin Pin.
  184. */
  185. void nrf_drv_gpiote_out_task_disable(nrf_drv_gpiote_pin_t pin);
  186. /**
  187. * @brief Function for getting the address of a configurable GPIOTE task.
  188. *
  189. * @param[in] pin Pin.
  190. */
  191. uint32_t nrf_drv_gpiote_out_task_addr_get(nrf_drv_gpiote_pin_t pin);
  192. /**
  193. * @brief Function for initializing a GPIOTE input pin.
  194. * @details The input pin can act in two ways:
  195. * - lower accuracy but low power (high frequency clock not needed)
  196. * - higher accuracy (high frequency clock required)
  197. *
  198. * The initial configuration specifies which mode is used.
  199. * If high-accuracy mode is used, the driver attempts to allocate one
  200. * of the available GPIOTE channels. If no channel is
  201. * available, an error is returned.
  202. * In low accuracy mode SENSE feature is used. In this case only one active pin
  203. * can be detected at a time. It can be worked around by setting all of the used
  204. * low accuracy pins to toggle mode.
  205. * For more information about SENSE functionality, refer to Product Specification.
  206. *
  207. * @param[in] pin Pin.
  208. * @param[in] p_config Initial configuration.
  209. * @param[in] evt_handler User function to be called when the configured transition occurs.
  210. *
  211. * @retval NRF_SUCCESS If initialization was successful.
  212. * @retval NRF_ERROR_INVALID_STATE If the driver is not initialized or the pin is already used.
  213. * @retval NRF_ERROR_NO_MEM If no GPIOTE channel is available.
  214. */
  215. ret_code_t nrf_drv_gpiote_in_init(nrf_drv_gpiote_pin_t pin,
  216. nrf_drv_gpiote_in_config_t const * p_config,
  217. nrf_drv_gpiote_evt_handler_t evt_handler);
  218. /**
  219. * @brief Function for uninitializing a GPIOTE input pin.
  220. * @details The driver frees the GPIOTE channel if the input pin was using one.
  221. *
  222. * @param[in] pin Pin.
  223. */
  224. void nrf_drv_gpiote_in_uninit(nrf_drv_gpiote_pin_t pin);
  225. /**
  226. * @brief Function for enabling sensing of a GPIOTE input pin.
  227. *
  228. * @details If the input pin is configured as high-accuracy pin, the function
  229. * enables an IN_EVENT. Otherwise, the function enables the GPIO sense mechanism.
  230. * Note that a PORT event is shared between multiple pins, therefore the
  231. * interrupt is always enabled.
  232. *
  233. * @param[in] pin Pin.
  234. * @param[in] int_enable True to enable the interrupt. Always valid for a high-accuracy pin.
  235. */
  236. void nrf_drv_gpiote_in_event_enable(nrf_drv_gpiote_pin_t pin, bool int_enable);
  237. /**
  238. * @brief Function for disabling a GPIOTE input pin.
  239. *
  240. * @param[in] pin Pin.
  241. */
  242. void nrf_drv_gpiote_in_event_disable(nrf_drv_gpiote_pin_t pin);
  243. /**
  244. * @brief Function for checking if a GPIOTE input pin is set.
  245. *
  246. * @param[in] pin Pin.
  247. * @retval true If the input pin is set.
  248. * @retval false If the input pin is not set.
  249. */
  250. bool nrf_drv_gpiote_in_is_set(nrf_drv_gpiote_pin_t pin);
  251. /**
  252. * @brief Function for getting the address of a GPIOTE input pin event.
  253. * @details If the pin is configured to use low-accuracy mode, the address of the PORT event is returned.
  254. *
  255. * @param[in] pin Pin.
  256. */
  257. uint32_t nrf_drv_gpiote_in_event_addr_get(nrf_drv_gpiote_pin_t pin);
  258. /**
  259. * @brief Function for forcing a specific state on the pin configured as task.
  260. *
  261. * @param[in] pin Pin.
  262. * @param[in] state Pin state.
  263. */
  264. void nrf_drv_gpiote_out_task_force(nrf_drv_gpiote_pin_t pin, uint8_t state);
  265. /**
  266. * @brief Function for triggering the task manually.
  267. *
  268. * @param[in] pin Pin.
  269. */
  270. void nrf_drv_gpiote_out_task_trigger(nrf_drv_gpiote_pin_t pin);
  271. /**
  272. *@}
  273. **/
  274. #endif //NRF_DRV_GPIOTE__