app_gpiote.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /**
  2. * Copyright (c) 2012 - 2020, 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. /** @file
  41. *
  42. * @defgroup app_gpiote GPIOTE Handler
  43. * @{
  44. * @ingroup app_common
  45. *
  46. * @brief GPIOTE handler module.
  47. *
  48. * @details The GPIOTE handler allows several modules ("users") to share the GPIOTE interrupt,
  49. * each user defining a set of pins able to generate events to the user.
  50. * When a GPIOTE interrupt occurs, the GPIOTE interrupt handler will call the event handler
  51. * of each user for which at least one of the pins generated an event.
  52. *
  53. * The GPIOTE users are responsible for configuring all their corresponding pins, except
  54. * the SENSE field, which should be initialized to GPIO_PIN_CNF_SENSE_Disabled.
  55. * The SENSE field will be updated by the GPIOTE module when it is enabled or disabled,
  56. * and also while it is enabled.
  57. *
  58. * The module specifies on which pins events should be generated if the pin(s) goes
  59. * from low->high or high->low or both directions.
  60. *
  61. * @note Even if the application is using the @ref app_scheduler, the GPIOTE event handlers will
  62. * be called directly from the GPIOTE interrupt handler.
  63. *
  64. * @warning If multiple users registers for the same pins the behavior for those pins are undefined.
  65. */
  66. #ifndef APP_GPIOTE_H__
  67. #define APP_GPIOTE_H__
  68. #include <stdint.h>
  69. #include <stdbool.h>
  70. #include "nrf.h"
  71. #include "nrf_drv_gpiote.h"
  72. #include "app_error.h"
  73. #include "app_util.h"
  74. #ifdef __cplusplus
  75. extern "C" {
  76. #endif
  77. #define GPIOTE_USER_NODE_SIZE ((4*sizeof(uint32_t)*GPIO_COUNT)+8) /**< Size of app_gpiote.gpiote_user_t (only for use inside APP_GPIOTE_BUF_SIZE()). */
  78. /**@brief Compute number of bytes required to hold the GPIOTE data structures.
  79. *
  80. * @param[in] MAX_USERS Maximum number of GPIOTE users.
  81. *
  82. * @retval Required buffer size (in bytes).
  83. */
  84. #define APP_GPIOTE_BUF_SIZE(MAX_USERS) ((MAX_USERS) * GPIOTE_USER_NODE_SIZE)
  85. typedef uint8_t app_gpiote_user_id_t;
  86. /**@brief GPIOTE event handler type. */
  87. typedef void (*app_gpiote_event_handler_t)(uint32_t const * p_event_pins_low_to_high,
  88. uint32_t const * p_event_pins_high_to_low);
  89. /**@brief GPIOTE input event handler type. */
  90. typedef void (*app_gpiote_input_event_handler_t)(void);
  91. /* Make the pin config descriptor packed */
  92. #pragma pack(push, 1)
  93. /**
  94. * @brief Single pin configuration
  95. *
  96. * Structure used to describe single pin configuration
  97. * when registering user.
  98. * @sa app_gpiote_user_register_ex
  99. */
  100. typedef struct
  101. {
  102. /** Pin number to observe */
  103. uint32_t pin_number : VBITS(NRF_GPIO_PIN_MAP(GPIO_COUNT - 1, 31));
  104. /** Transition to observe */
  105. nrf_gpiote_polarity_t sense : 2;
  106. } app_gpiote_user_pin_config_t;
  107. // Check if we can fitt all the nrf_gpiote_polarity_t values into 2 bits field
  108. STATIC_ASSERT(NRF_GPIOTE_POLARITY_LOTOHI <= 3);
  109. STATIC_ASSERT(NRF_GPIOTE_POLARITY_HITOLO <= 3);
  110. STATIC_ASSERT(NRF_GPIOTE_POLARITY_TOGGLE <= 3);
  111. #pragma pack(pop)
  112. /**@brief Macro for initializing the GPIOTE module.
  113. *
  114. * @details It will handle dimensioning and allocation of the memory buffer required by the module,
  115. * making sure that the buffer is correctly aligned.
  116. *
  117. * @param[in] MAX_USERS Maximum number of GPIOTE users.
  118. *
  119. * @note Since this macro allocates a buffer, it must only be called once (it is OK to call it
  120. * several times as long as it is from the same location, e.g. to do a reinitialization).
  121. */
  122. /*lint -emacro(506, APP_GPIOTE_INIT) */ /* Suppress "Constant value Boolean */
  123. #define APP_GPIOTE_INIT(MAX_USERS) \
  124. do \
  125. { \
  126. static uint32_t app_gpiote_buf[CEIL_DIV(APP_GPIOTE_BUF_SIZE(MAX_USERS), sizeof(uint32_t))];\
  127. uint32_t ERR_CODE = app_gpiote_init((MAX_USERS), app_gpiote_buf); \
  128. APP_ERROR_CHECK(ERR_CODE); \
  129. } while (0)
  130. /**@brief Function for initializing the GPIOTE module.
  131. *
  132. * @note Normally initialization should be done using the APP_GPIOTE_INIT() macro, as that will
  133. * allocate the buffer needed by the GPIOTE module (including aligning the buffer correctly).
  134. *
  135. * @param[in] max_users Maximum number of GPIOTE users.
  136. * @param[in] p_buffer Pointer to memory buffer for internal use in the app_gpiote
  137. * module. The size of the buffer can be computed using the
  138. * APP_GPIOTE_BUF_SIZE() macro. The buffer must be aligned to
  139. * a 4 byte boundary.
  140. *
  141. * @retval NRF_SUCCESS Successful initialization.
  142. * @retval NRF_ERROR_INVALID_PARAM Invalid parameter (buffer not aligned to a 4 byte
  143. * boundary).
  144. */
  145. uint32_t app_gpiote_init(uint8_t max_users, void * p_buffer);
  146. /**@brief Function for registering a GPIOTE user.
  147. *
  148. * @param[out] p_user_id Id for the new GPIOTE user.
  149. * @param[in] p_pins_low_to_high_mask Pointer to word array with mask defining which pins
  150. * will generate events to this user when state is changed
  151. * from low->high. Size of array depends on number of ports
  152. * in the device.
  153. * @param[in] p_pins_high_to_low_mask Pointer to word array with mask defining which pins
  154. * will generate events to this user when state is changed
  155. * from high->low. Size of array depends on number of ports
  156. * in the device.
  157. * @param[in] event_handler Pointer to function to be executed when an event occurs.
  158. * Cannot be NULL.
  159. *
  160. * @retval NRF_SUCCESS Successful initialization.
  161. * @retval NRF_ERROR_INALID_STATE If @ref app_gpiote_init has not been called on the GPIOTE
  162. * module.
  163. * @retval NRF_ERROR_NO_MEM Returned if the application tries to register more users
  164. * than defined when the GPIOTE module was initialized in
  165. * @ref app_gpiote_init.
  166. *
  167. * @note The function can also return error codes from internally
  168. * called @ref nrf_drv_gpiote_in_init
  169. *
  170. * @sa app_gpiote_user_register_ex
  171. */
  172. uint32_t app_gpiote_user_register(app_gpiote_user_id_t * p_user_id,
  173. uint32_t const * p_pins_low_to_high_mask,
  174. uint32_t const * p_pins_high_to_low_mask,
  175. app_gpiote_event_handler_t event_handler);
  176. /**@brief Function for registering GPIOTE user using pins configuration list.
  177. *
  178. * Function for registering GPIOTE user that uses array of pins configuration.
  179. * This function do not change pins configuration.
  180. * Pins must be configured before calling this function.
  181. *
  182. * @param[out] p_user_id Id for the new GPIOTE user.
  183. * @param[in] p_pins_config Pointer to the array of pins configuration for the user.
  184. * @param[in] pin_count Number of pins to configure for the user.
  185. * @param[in] event_handler Pointer to function to be executed when an event occurs.
  186. * Cannot be NULL.
  187. *
  188. * @retval NRF_SUCCESS Successful user registration.
  189. * @retval NRF_ERROR_INVALID_STATE If @ref app_gpiote_init has not been called before calling
  190. * this function.
  191. * @retval NRF_ERROR_NO_MEM Returned if the application tries to register more users
  192. * than defined when the GPIOTE module was initialized in
  193. * @ref app_gpiote_init.
  194. *
  195. * @note The function can also return error codes from internally
  196. * called @ref nrf_drv_gpiote_in_init
  197. *
  198. * @sa app_gpiote_user_register
  199. */
  200. uint32_t app_gpiote_user_register_ex(app_gpiote_user_id_t * p_user_id,
  201. app_gpiote_user_pin_config_t const * p_pins_config,
  202. size_t pin_count,
  203. app_gpiote_event_handler_t event_handler);
  204. /**@brief Function for informing the GPIOTE module that the specified user wants to use the GPIOTE module.
  205. *
  206. * @param[in] user_id Id of user to enable.
  207. *
  208. * @retval NRF_SUCCESS On success.
  209. * @retval NRF_ERROR_INVALID_PARAM Invalid user_id provided, No a valid user.
  210. * @retval NRF_ERROR_INALID_STATE If @ref app_gpiote_init has not been called on the GPIOTE
  211. * module.
  212. */
  213. uint32_t app_gpiote_user_enable(app_gpiote_user_id_t user_id);
  214. /**@brief Function for informing the GPIOTE module that the specified user is done using the GPIOTE module.
  215. *
  216. * @param[in] user_id Id of user to enable.
  217. *
  218. * @retval NRF_SUCCESS On success.
  219. * @retval NRF_ERROR_INVALID_PARAM Invalid user_id provided, No a valid user.
  220. * @retval NRF_ERROR_INALID_STATE If @ref app_gpiote_init has not been called on the GPIOTE
  221. * module.
  222. */
  223. uint32_t app_gpiote_user_disable(app_gpiote_user_id_t user_id);
  224. /**@brief Function for getting the state of the pins which are registered for the specified user.
  225. *
  226. * @param[in] user_id Id of user to check.
  227. * @param[out] p_pins Pointer to array of words with bit mask corresponding to the pins
  228. * configured to generate events to the specified user. All bits
  229. * corresponding to pins in the state 'high' will have value '1',
  230. * all others will have value '0'. Size of array depends on number
  231. * of ports in the device.
  232. *
  233. * @retval NRF_SUCCESS On success.
  234. * @retval NRF_ERROR_INVALID_PARAM Invalid user_id provided, No a valid user.
  235. * @retval NRF_ERROR_INALID_STATE If @ref app_gpiote_init has not been called on the GPIOTE
  236. * module.
  237. */
  238. uint32_t app_gpiote_pins_state_get(app_gpiote_user_id_t user_id, uint32_t * p_pins);
  239. #ifdef __cplusplus
  240. }
  241. #endif
  242. #endif // APP_GPIOTE_H__
  243. /** @} */