app_button.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /**
  2. * Copyright (c) 2012 - 2019, 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(BUTTON)
  42. #include "app_button.h"
  43. #include "app_timer.h"
  44. #include "app_error.h"
  45. #include "nrf_drv_gpiote.h"
  46. #include "nrf_assert.h"
  47. static app_button_cfg_t const * mp_buttons = NULL; /**< Button configuration. */
  48. static uint8_t m_button_count; /**< Number of configured buttons. */
  49. static uint32_t m_detection_delay; /**< Delay before a button is reported as pushed. */
  50. APP_TIMER_DEF(m_detection_delay_timer_id); /**< Polling timer id. */
  51. static uint64_t m_pin_state;
  52. static uint64_t m_pin_transition;
  53. /**@brief Function for handling the timeout that delays reporting buttons as pushed.
  54. *
  55. * @details The detection_delay_timeout_handler(...) is a call-back issued from the app_timer
  56. * module. It is called with the p_context parameter. The p_context parameter is
  57. * provided to the app_timer module when a timer is started, using the call
  58. * @ref app_timer_start. On @ref app_timer_start the p_context will be holding the
  59. * currently pressed buttons.
  60. *
  61. * @param[in] p_context Pointer used for passing information app_start_timer() was called.
  62. * In the app_button module the p_context holds information on pressed
  63. * buttons.
  64. */
  65. static void detection_delay_timeout_handler(void * p_context)
  66. {
  67. uint8_t i;
  68. // Pushed button(s) detected, execute button handler(s).
  69. for (i = 0; i < m_button_count; i++)
  70. {
  71. app_button_cfg_t const * p_btn = &mp_buttons[i];
  72. uint64_t btn_mask = 1ULL << p_btn->pin_no;
  73. if (btn_mask & m_pin_transition)
  74. {
  75. m_pin_transition &= ~btn_mask;
  76. bool pin_is_set = nrf_drv_gpiote_in_is_set(p_btn->pin_no);
  77. if ((m_pin_state & (1ULL << p_btn->pin_no)) == (((uint64_t)pin_is_set) << p_btn->pin_no))
  78. {
  79. uint64_t transition = !(pin_is_set ^ (p_btn->active_state == APP_BUTTON_ACTIVE_HIGH));
  80. if (p_btn->button_handler)
  81. {
  82. p_btn->button_handler(p_btn->pin_no, transition);
  83. }
  84. }
  85. }
  86. }
  87. }
  88. static void gpiote_event_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
  89. {
  90. uint32_t err_code;
  91. uint64_t pin_mask = 1ULL << pin;
  92. // Start detection timer. If timer is already running, the detection period is restarted.
  93. // NOTE: Using the p_context parameter of app_timer_start() to transfer the pin states to the
  94. // timeout handler (by casting event_pins_mask into the equally sized void * p_context
  95. // parameter).
  96. err_code = app_timer_stop(m_detection_delay_timer_id);
  97. if (err_code != NRF_SUCCESS)
  98. {
  99. // The impact in app_button of the app_timer queue running full is losing a button press.
  100. // The current implementation ensures that the system will continue working as normal.
  101. return;
  102. }
  103. if (!(m_pin_transition & pin_mask))
  104. {
  105. if (nrf_drv_gpiote_in_is_set(pin))
  106. {
  107. m_pin_state |= pin_mask;
  108. }
  109. else
  110. {
  111. m_pin_state &= ~(pin_mask);
  112. }
  113. m_pin_transition |= (pin_mask);
  114. err_code = app_timer_start(m_detection_delay_timer_id, m_detection_delay, NULL);
  115. if (err_code != NRF_SUCCESS)
  116. {
  117. // The impact in app_button of the app_timer queue running full is losing a button press.
  118. // The current implementation ensures that the system will continue working as normal.
  119. }
  120. }
  121. else
  122. {
  123. m_pin_transition &= ~pin_mask;
  124. }
  125. }
  126. uint32_t app_button_init(app_button_cfg_t const * p_buttons,
  127. uint8_t button_count,
  128. uint32_t detection_delay)
  129. {
  130. uint32_t err_code;
  131. if (detection_delay < APP_TIMER_MIN_TIMEOUT_TICKS)
  132. {
  133. return NRF_ERROR_INVALID_PARAM;
  134. }
  135. if (!nrf_drv_gpiote_is_init())
  136. {
  137. err_code = nrf_drv_gpiote_init();
  138. VERIFY_SUCCESS(err_code);
  139. }
  140. // Save configuration.
  141. mp_buttons = p_buttons;
  142. m_button_count = button_count;
  143. m_detection_delay = detection_delay;
  144. m_pin_state = 0;
  145. m_pin_transition = 0;
  146. while (button_count--)
  147. {
  148. app_button_cfg_t const * p_btn = &p_buttons[button_count];
  149. #if defined(BUTTON_HIGH_ACCURACY_ENABLED) && (BUTTON_HIGH_ACCURACY_ENABLED == 1)
  150. nrf_drv_gpiote_in_config_t config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(p_btn->hi_accuracy);
  151. #else
  152. nrf_drv_gpiote_in_config_t config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(false);
  153. #endif
  154. config.pull = p_btn->pull_cfg;
  155. err_code = nrf_drv_gpiote_in_init(p_btn->pin_no, &config, gpiote_event_handler);
  156. VERIFY_SUCCESS(err_code);
  157. }
  158. // Create polling timer.
  159. return app_timer_create(&m_detection_delay_timer_id,
  160. APP_TIMER_MODE_SINGLE_SHOT,
  161. detection_delay_timeout_handler);
  162. }
  163. uint32_t app_button_enable(void)
  164. {
  165. ASSERT(mp_buttons);
  166. uint32_t i;
  167. for (i = 0; i < m_button_count; i++)
  168. {
  169. nrf_drv_gpiote_in_event_enable(mp_buttons[i].pin_no, true);
  170. }
  171. return NRF_SUCCESS;
  172. }
  173. uint32_t app_button_disable(void)
  174. {
  175. ASSERT(mp_buttons);
  176. uint32_t i;
  177. for (i = 0; i < m_button_count; i++)
  178. {
  179. nrf_drv_gpiote_in_event_disable(mp_buttons[i].pin_no);
  180. }
  181. // Make sure polling timer is not running.
  182. return app_timer_stop(m_detection_delay_timer_id);
  183. }
  184. bool app_button_is_pushed(uint8_t button_id)
  185. {
  186. ASSERT(button_id <= m_button_count);
  187. ASSERT(mp_buttons != NULL);
  188. app_button_cfg_t const * p_btn = &mp_buttons[button_id];
  189. bool is_set = nrf_drv_gpiote_in_is_set(p_btn->pin_no);
  190. return !(is_set ^ (p_btn->active_state == APP_BUTTON_ACTIVE_HIGH));
  191. }
  192. #endif //NRF_MODULE_ENABLED(BUTTON)