app_button.c 6.1 KB

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