app_gpiote.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /**
  2. * Copyright (c) 2015 - 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. #include "sdk_common.h"
  41. #if NRF_MODULE_ENABLED(APP_GPIOTE)
  42. #include "app_gpiote.h"
  43. #include "nrf_bitmask.h"
  44. #define MODULE_INITIALIZED (mp_users != NULL) /**< Macro designating whether the module has been initialized properly. */
  45. #if (GPIO_COUNT == 1)
  46. #define MAX_PIN_NUMBER 32
  47. #elif (GPIO_COUNT == 2)
  48. #define MAX_PIN_NUMBER (32 + P1_PIN_NUM)
  49. #else
  50. #error "Not supported."
  51. #endif
  52. /**@brief GPIOTE user type. */
  53. typedef struct
  54. {
  55. uint32_t pins_mask[GPIO_COUNT]; /**< Mask defining which pins user wants to monitor. */
  56. uint32_t pins_low_to_high_mask[GPIO_COUNT]; /**< Mask defining which pins will generate events to this user when toggling low->high. */
  57. uint32_t pins_high_to_low_mask[GPIO_COUNT]; /**< Mask defining which pins will generate events to this user when toggling high->low. */
  58. uint32_t sense_high_pins[GPIO_COUNT]; /**< Mask defining which pins are configured to generate GPIOTE interrupt on transition to high level. */
  59. app_gpiote_event_handler_t event_handler; /**< Pointer to function to be executed when an event occurs. */
  60. bool enabled; /**< Flag indicating whether user is enabled. */
  61. } gpiote_user_t;
  62. STATIC_ASSERT(sizeof(gpiote_user_t) <= GPIOTE_USER_NODE_SIZE);
  63. STATIC_ASSERT(sizeof(gpiote_user_t) % 4 == 0);
  64. static uint8_t m_user_array_size; /**< Size of user array. */
  65. static uint8_t m_user_count; /**< Number of registered users. */
  66. static gpiote_user_t * mp_users = NULL; /**< Array of GPIOTE users. */
  67. static uint32_t m_pins[GPIO_COUNT]; /**< Mask of initialized pins. */
  68. static uint32_t m_last_pins_state[GPIO_COUNT]; /**< Most recent state of pins. */
  69. void gpiote_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
  70. {
  71. int i;
  72. uint32_t pin_mask[GPIO_COUNT] = {0};
  73. uint32_t empty_pin_mask[GPIO_COUNT] = {0};
  74. nrf_bitmask_bit_set(pin, pin_mask);
  75. bool hitolo = nrf_bitmask_bit_is_set(pin, m_last_pins_state);
  76. nrf_gpio_ports_read(0, GPIO_COUNT, m_last_pins_state);
  77. for (i = 0; i < m_user_count; i++)
  78. {
  79. if (mp_users[i].enabled && nrf_bitmask_bit_is_set(pin, mp_users[i].pins_mask))
  80. {
  81. if (
  82. nrf_bitmask_bit_is_set(pin, mp_users[i].pins_high_to_low_mask)
  83. && hitolo)
  84. {
  85. mp_users[i].event_handler(empty_pin_mask,pin_mask);
  86. }
  87. else if (
  88. nrf_bitmask_bit_is_set(pin, mp_users[i].pins_low_to_high_mask)
  89. && !hitolo)
  90. {
  91. mp_users[i].event_handler(pin_mask,empty_pin_mask);
  92. }
  93. }
  94. }
  95. }
  96. uint32_t app_gpiote_init(uint8_t max_users, void * p_buffer)
  97. {
  98. uint32_t ret_code = NRF_SUCCESS;
  99. if (p_buffer == NULL)
  100. {
  101. return NRF_ERROR_INVALID_PARAM;
  102. }
  103. // Check that buffer is correctly aligned.
  104. if (!is_word_aligned(p_buffer))
  105. {
  106. return NRF_ERROR_INVALID_PARAM;
  107. }
  108. // Initialize file globals.
  109. mp_users = (gpiote_user_t *)p_buffer;
  110. m_user_array_size = max_users;
  111. m_user_count = 0;
  112. memset(m_pins,0, sizeof(m_pins));
  113. memset(mp_users, 0, m_user_array_size * sizeof(gpiote_user_t));
  114. if (nrf_drv_gpiote_is_init()==false)
  115. {
  116. ret_code = nrf_drv_gpiote_init();
  117. }
  118. return ret_code;
  119. }
  120. uint32_t app_gpiote_user_register(app_gpiote_user_id_t * p_user_id,
  121. uint32_t const * p_pins_low_to_high_mask,
  122. uint32_t const * p_pins_high_to_low_mask,
  123. app_gpiote_event_handler_t event_handler)
  124. {
  125. uint32_t user_pin_mask[GPIO_COUNT];
  126. ASSERT(event_handler != NULL);
  127. // Check state and parameters.
  128. VERIFY_MODULE_INITIALIZED();
  129. if (m_user_count >= m_user_array_size)
  130. {
  131. return NRF_ERROR_NO_MEM;
  132. }
  133. nrf_bitmask_masks_or(p_pins_low_to_high_mask, p_pins_high_to_low_mask,
  134. user_pin_mask, sizeof(user_pin_mask));
  135. // Allocate new user.
  136. mp_users[m_user_count].enabled = false;
  137. mp_users[m_user_count].event_handler = event_handler;
  138. memcpy(mp_users[m_user_count].pins_mask,
  139. user_pin_mask,
  140. sizeof(mp_users[m_user_count].pins_mask));
  141. memcpy(mp_users[m_user_count].pins_low_to_high_mask,
  142. p_pins_low_to_high_mask,
  143. sizeof(mp_users[m_user_count].pins_low_to_high_mask));
  144. memcpy(mp_users[m_user_count].pins_high_to_low_mask,
  145. p_pins_high_to_low_mask,
  146. sizeof(mp_users[m_user_count].pins_high_to_low_mask));
  147. uint32_t i;
  148. const nrf_drv_gpiote_in_config_t config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(false);
  149. uint32_t num_of_pins = MAX_PIN_NUMBER;
  150. for (i = 0; i < num_of_pins; i++)
  151. {
  152. if (nrf_bitmask_bit_is_set(i, user_pin_mask) &&
  153. !nrf_bitmask_bit_is_set(i, m_pins))
  154. {
  155. uint32_t ret_val = nrf_drv_gpiote_in_init(i, &config, gpiote_handler);
  156. VERIFY_SUCCESS(ret_val);
  157. nrf_bitmask_bit_set(i, m_pins);
  158. }
  159. }
  160. /* Success - return user id and increment counter */
  161. *p_user_id = m_user_count++;
  162. return NRF_SUCCESS;
  163. }
  164. uint32_t app_gpiote_user_register_ex(app_gpiote_user_id_t * p_user_id,
  165. app_gpiote_user_pin_config_t const * p_pins_config,
  166. size_t pin_count,
  167. app_gpiote_event_handler_t event_handler)
  168. {
  169. ASSERT(event_handler != NULL);
  170. // Check state and parameters.
  171. VERIFY_MODULE_INITIALIZED();
  172. if (m_user_count >= m_user_array_size)
  173. {
  174. return NRF_ERROR_NO_MEM;
  175. }
  176. /* Prepare user structure */
  177. gpiote_user_t * p_user = &mp_users[m_user_count];
  178. p_user->enabled = false;
  179. memset(p_user, 0, sizeof(gpiote_user_t));
  180. mp_users[m_user_count].event_handler = event_handler;
  181. for (; pin_count != 0; --pin_count, ++p_pins_config)
  182. {
  183. nrfx_gpiote_pin_t pin = (nrfx_gpiote_pin_t)p_pins_config->pin_number;
  184. const nrf_drv_gpiote_in_config_t config = GPIOTE_RAW_CONFIG_IN_SENSE_TOGGLE(false);
  185. if (!nrf_bitmask_bit_is_set(pin, m_pins))
  186. {
  187. uint32_t ret_val = nrf_drv_gpiote_in_init(pin, &config, gpiote_handler);
  188. VERIFY_SUCCESS(ret_val);
  189. nrf_bitmask_bit_set(pin, m_pins);
  190. }
  191. //lint -save -e650 Lint seems not properly support bitfields that uses enum as a base type
  192. if ((p_pins_config->sense == NRF_GPIOTE_POLARITY_LOTOHI) ||
  193. (p_pins_config->sense == NRF_GPIOTE_POLARITY_TOGGLE))
  194. {
  195. nrf_bitmask_bit_set(pin, p_user->pins_low_to_high_mask);
  196. }
  197. if ((p_pins_config->sense == NRF_GPIOTE_POLARITY_HITOLO) ||
  198. (p_pins_config->sense == NRF_GPIOTE_POLARITY_TOGGLE))
  199. {
  200. nrf_bitmask_bit_set(pin, p_user->pins_high_to_low_mask);
  201. }
  202. //lint -restore
  203. }
  204. /* Mark all pins used by the selected user */
  205. nrf_bitmask_masks_or(
  206. p_user->pins_low_to_high_mask,
  207. p_user->pins_high_to_low_mask,
  208. p_user->pins_mask,
  209. sizeof(p_user->pins_mask));
  210. /* Success - return user id and increment counter */
  211. *p_user_id = m_user_count++;
  212. return NRF_SUCCESS;
  213. }
  214. __STATIC_INLINE uint32_t error_check(app_gpiote_user_id_t user_id)
  215. {
  216. // Check state and parameters.
  217. VERIFY_MODULE_INITIALIZED();
  218. if (user_id >= m_user_count)
  219. {
  220. return NRF_ERROR_INVALID_PARAM;
  221. }
  222. return NRF_SUCCESS;
  223. }
  224. /**
  225. * @brief Function for enabling event on pin (if not yet enabled) or disabling the event if no other
  226. * user requires it.
  227. *
  228. * @param pin Pin to enable
  229. * @param enable If true function will attempt to enable the pin else it will attempt to disable it.
  230. */
  231. static void pin_event_enable(uint32_t pin, bool enable)
  232. {
  233. uint32_t i;
  234. bool enabled = false;
  235. //search if any user already enabled given pin
  236. for (i = 0; i < m_user_count; i++)
  237. {
  238. if (mp_users[i].enabled &&
  239. nrf_bitmask_bit_is_set(pin, mp_users[i].pins_mask))
  240. {
  241. enabled = true;
  242. break;
  243. }
  244. }
  245. if (!enabled)
  246. {
  247. if (enable)
  248. {
  249. nrf_gpio_ports_read(0, GPIO_COUNT, m_last_pins_state);
  250. nrf_drv_gpiote_in_event_enable(pin, true);
  251. }
  252. else
  253. {
  254. nrf_drv_gpiote_in_event_disable(pin);
  255. }
  256. }
  257. }
  258. /**
  259. * @brief Function for enabling or disabling events for pins used by the user.
  260. *
  261. * Function will enable pin events only if they are not yet enabled. Function will disable pin
  262. * events only if there is no other enabled user that is using them.
  263. *
  264. * @param user_id User id.
  265. * @param enable If true function will attempt to enable the pin else it will attempt to disable it.
  266. */
  267. static uint32_t user_enable(app_gpiote_user_id_t user_id, bool enable)
  268. {
  269. uint32_t ret_code = error_check(user_id);
  270. if (ret_code == NRF_SUCCESS)
  271. {
  272. uint32_t i;
  273. for (i = 0; i < MAX_PIN_NUMBER; i++)
  274. {
  275. if (nrf_bitmask_bit_is_set(i, mp_users[user_id].pins_mask))
  276. {
  277. pin_event_enable(i, enable);
  278. }
  279. }
  280. }
  281. return ret_code;
  282. }
  283. uint32_t app_gpiote_user_enable(app_gpiote_user_id_t user_id)
  284. {
  285. uint32_t ret_code = NRF_SUCCESS;
  286. if (mp_users[user_id].enabled == false)
  287. {
  288. ret_code = user_enable(user_id, true);
  289. VERIFY_SUCCESS(ret_code);
  290. mp_users[user_id].enabled = true;
  291. return ret_code;
  292. }
  293. else
  294. {
  295. return ret_code;
  296. }
  297. }
  298. uint32_t app_gpiote_user_disable(app_gpiote_user_id_t user_id)
  299. {
  300. uint32_t ret_code = NRF_SUCCESS;
  301. if (mp_users[user_id].enabled)
  302. {
  303. mp_users[user_id].enabled = false;
  304. ret_code = user_enable(user_id, false);
  305. }
  306. return ret_code;
  307. }
  308. uint32_t app_gpiote_pins_state_get(app_gpiote_user_id_t user_id, uint32_t * p_pins)
  309. {
  310. gpiote_user_t * p_user;
  311. uint32_t ret_code = error_check(user_id);
  312. if (ret_code == NRF_SUCCESS)
  313. {
  314. p_user = &mp_users[user_id];
  315. nrf_gpio_ports_read(0, GPIO_COUNT, p_pins);
  316. nrf_bitmask_masks_and(p_pins, p_user->pins_mask, p_pins, sizeof(p_user->pins_mask));
  317. }
  318. return ret_code;
  319. }
  320. #endif //NRF_MODULE_ENABLED(APP_GPIOTE)