app_gpiote.c 12 KB

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