app_gpiote.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* Copyright (c) 2012 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. /** @file
  13. *
  14. * @defgroup app_gpiote GPIOTE Handler
  15. * @{
  16. * @ingroup app_common
  17. *
  18. * @brief GPIOTE handler module.
  19. *
  20. * @details The GPIOTE handler allows several modules ("users") to share the GPIOTE interrupt,
  21. * each user defining a set of pins able to generate events to the user.
  22. * When a GPIOTE interrupt occurs, the GPIOTE interrupt handler will call the event handler
  23. * of each user for which at least one of the pins generated an event.
  24. *
  25. * The GPIOTE users are responsible for configuring all their corresponding pins, except
  26. * the SENSE field, which should be initialized to GPIO_PIN_CNF_SENSE_Disabled.
  27. * The SENSE field will be updated by the GPIOTE module when it is enabled or disabled,
  28. * and also while it is enabled.
  29. *
  30. * The module specifies on which pins events should be generated if the pin(s) goes
  31. * from low->high or high->low or both directions.
  32. *
  33. * @note Even if the application is using the @ref app_scheduler, the GPIOTE event handlers will
  34. * be called directly from the GPIOTE interrupt handler.
  35. *
  36. * @warning If multiple users registers for the same pins the behavior for those pins are undefined.
  37. */
  38. #ifndef APP_GPIOTE_H__
  39. #define APP_GPIOTE_H__
  40. #include <stdint.h>
  41. #include <stdbool.h>
  42. #include "nrf.h"
  43. #include "app_error.h"
  44. #include "app_util.h"
  45. #define GPIOTE_USER_NODE_SIZE 24 /**< Size of app_gpiote.gpiote_user_t (only for use inside APP_GPIOTE_BUF_SIZE()). */
  46. #define NO_OF_PINS 32 /**< Number of GPIO pins on the \nRFXX chip. */
  47. /**@brief Compute number of bytes required to hold the GPIOTE data structures.
  48. *
  49. * @param[in] MAX_USERS Maximum number of GPIOTE users.
  50. *
  51. * @retval Required buffer size (in bytes).
  52. */
  53. #define APP_GPIOTE_BUF_SIZE(MAX_USERS) ((MAX_USERS) * GPIOTE_USER_NODE_SIZE)
  54. typedef uint8_t app_gpiote_user_id_t;
  55. /**@brief GPIOTE event handler type. */
  56. typedef void (*app_gpiote_event_handler_t)(uint32_t event_pins_low_to_high,
  57. uint32_t event_pins_high_to_low);
  58. /**@brief GPIOTE input event handler type. */
  59. typedef void (*app_gpiote_input_event_handler_t)(void);
  60. /**@brief Macro for initializing the GPIOTE module.
  61. *
  62. * @details It will handle dimensioning and allocation of the memory buffer required by the module,
  63. * making sure that the buffer is correctly aligned.
  64. *
  65. * @param[in] MAX_USERS Maximum number of GPIOTE users.
  66. *
  67. * @note Since this macro allocates a buffer, it must only be called once (it is OK to call it
  68. * several times as long as it is from the same location, e.g. to do a reinitialization).
  69. */
  70. /*lint -emacro(506, APP_GPIOTE_INIT) */ /* Suppress "Constant value Boolean */
  71. #define APP_GPIOTE_INIT(MAX_USERS) \
  72. do \
  73. { \
  74. static uint32_t app_gpiote_buf[CEIL_DIV(APP_GPIOTE_BUF_SIZE(MAX_USERS), sizeof(uint32_t))];\
  75. uint32_t ERR_CODE = app_gpiote_init((MAX_USERS), app_gpiote_buf); \
  76. APP_ERROR_CHECK(ERR_CODE); \
  77. } while (0)
  78. /**@brief Function for initializing the GPIOTE module.
  79. *
  80. * @note Normally initialization should be done using the APP_GPIOTE_INIT() macro, as that will
  81. * allocate the buffer needed by the GPIOTE module (including aligning the buffer correctly).
  82. *
  83. * @param[in] max_users Maximum number of GPIOTE users.
  84. * @param[in] p_buffer Pointer to memory buffer for internal use in the app_gpiote
  85. * module. The size of the buffer can be computed using the
  86. * APP_GPIOTE_BUF_SIZE() macro. The buffer must be aligned to
  87. * a 4 byte boundary.
  88. *
  89. * @retval NRF_SUCCESS Successful initialization.
  90. * @retval NRF_ERROR_INVALID_PARAM Invalid parameter (buffer not aligned to a 4 byte
  91. * boundary).
  92. */
  93. uint32_t app_gpiote_init(uint8_t max_users, void * p_buffer);
  94. /**@brief Function for registering a GPIOTE user.
  95. *
  96. * @param[out] p_user_id Id for the new GPIOTE user.
  97. * @param[in] pins_low_to_high_mask Mask defining which pins will generate events to this user
  98. * when state is changed from low->high.
  99. * @param[in] pins_high_to_low_mask Mask defining which pins will generate events to this user
  100. * when state is changed from high->low.
  101. * @param[in] event_handler Pointer to function to be executed when an event occurs.
  102. *
  103. * @retval NRF_SUCCESS Successful initialization.
  104. * @retval NRF_ERROR_INVALID_PARAM Invalid parameter (buffer not aligned to a 4 byte boundary).
  105. * @retval NRF_ERROR_INALID_STATE If @ref app_gpiote_init has not been called on the GPIOTE
  106. * module.
  107. * @retval NRF_ERROR_NO_MEM Returned if the application tries to register more users
  108. * than defined when the GPIOTE module was initialized in
  109. * @ref app_gpiote_init.
  110. */
  111. uint32_t app_gpiote_user_register(app_gpiote_user_id_t * p_user_id,
  112. uint32_t pins_low_to_high_mask,
  113. uint32_t pins_high_to_low_mask,
  114. app_gpiote_event_handler_t event_handler);
  115. /**@brief Function for informing the GPIOTE module that the specified user wants to use the GPIOTE module.
  116. *
  117. * @param[in] user_id Id of user to enable.
  118. *
  119. * @retval NRF_SUCCESS On success.
  120. * @retval NRF_ERROR_INVALID_PARAM Invalid user_id provided, No a valid user.
  121. * @retval NRF_ERROR_INALID_STATE If @ref app_gpiote_init has not been called on the GPIOTE
  122. * module.
  123. */
  124. uint32_t app_gpiote_user_enable(app_gpiote_user_id_t user_id);
  125. /**@brief Function for informing the GPIOTE module that the specified user is done using the GPIOTE module.
  126. *
  127. * @param[in] user_id Id of user to enable.
  128. *
  129. * @retval NRF_SUCCESS On success.
  130. * @retval NRF_ERROR_INVALID_PARAM Invalid user_id provided, No a valid user.
  131. * @retval NRF_ERROR_INALID_STATE If @ref app_gpiote_init has not been called on the GPIOTE
  132. * module.
  133. */
  134. uint32_t app_gpiote_user_disable(app_gpiote_user_id_t user_id);
  135. /**@brief Function for getting the state of the pins which are registered for the specified user.
  136. *
  137. * @param[in] user_id Id of user to check.
  138. * @param[out] p_pins Bit mask corresponding to the pins configured to generate events to
  139. * the specified user. All bits corresponding to pins in the state
  140. * 'high' will have value '1', all others will have value '0'.
  141. *
  142. * @retval NRF_SUCCESS On success.
  143. * @retval NRF_ERROR_INVALID_PARAM Invalid user_id provided, No a valid user.
  144. * @retval NRF_ERROR_INALID_STATE If @ref app_gpiote_init has not been called on the GPIOTE
  145. * module.
  146. */
  147. uint32_t app_gpiote_pins_state_get(app_gpiote_user_id_t user_id, uint32_t * p_pins);
  148. /**@brief Function for registering event handlers for GPIOTE IN events.
  149. *
  150. * @param[in] channel GPIOTE channel [0..3].
  151. * @param[in] pin Pins associated with GPIOTE channel. Changes on following pins will generate events.
  152. * @param[in] polarity Specify operation on input that shall trigger IN event.
  153. * @param[in] event_handler Event handler invoked on the IN event in the GPIOTE interrupt.
  154. *
  155. * @retval NRF_SUCCESS On success.
  156. * @retval NRF_ERROR_INVALID_PARAM Invalid channel or pin number.
  157. * @retval NRF_ERROR_NOT_SUPPORTED Driver doesn't support IN events.
  158. */
  159. uint32_t app_gpiote_input_event_handler_register(const uint8_t channel,
  160. const uint32_t pin,
  161. const uint32_t polarity,
  162. app_gpiote_input_event_handler_t event_handler);
  163. /**@brief Function for unregistering event handlers for GPIOTE IN events.
  164. *
  165. * @retval NRF_SUCCESS On success.
  166. * @retval NRF_ERROR_NOT_SUPPORTED Driver doesn't support IN events.
  167. */
  168. uint32_t app_gpiote_input_event_handler_unregister(const uint8_t channel);
  169. /**@brief Function for registering event handler invoked at the end of a GPIOTE interrupt.
  170. *
  171. * @param[in] event_handler Event handler invoked at the end of the GPIOTE interrupt.
  172. *
  173. * @retval NRF_SUCCESS On success.
  174. * @retval NRF_ERROR_NOT_SUPPORTED Driver doesn't support IN events.
  175. */
  176. uint32_t app_gpiote_end_irq_event_handler_register(app_gpiote_input_event_handler_t event_handler);
  177. /**@brief Function for unregistering event handler invoked at the end of a GPIOTE interrupt.
  178. *
  179. * @retval NRF_SUCCESS On success.
  180. * @retval NRF_ERROR_NOT_SUPPORTED Driver doesn't support IN events.
  181. */
  182. uint32_t app_gpiote_end_irq_event_handler_unregister(void);
  183. /**@brief Function for enabling interrupts in the GPIOTE driver.
  184. *
  185. * @retval NRF_SUCCESS On success.
  186. * @retval NRF_ERROR_NOT_SUPPORTED Driver doesn't support.
  187. */
  188. uint32_t app_gpiote_enable_interrupts(void);
  189. /**@brief Function for disabling interrupts in the GPIOTE driver.
  190. *
  191. * @retval NRF_SUCCESS On success.
  192. * @retval NRF_ERROR_NOT_SUPPORTED Driver doesn't support.
  193. */
  194. uint32_t app_gpiote_disable_interrupts(void);
  195. #endif // APP_GPIOTE_H__
  196. /** @} */