app_button.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. #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. #define NRF_LOG_MODULE_NAME app_button
  48. #if APP_BUTTON_CONFIG_LOG_ENABLED
  49. #define NRF_LOG_LEVEL APP_BUTTON_CONFIG_LOG_LEVEL
  50. #define NRF_LOG_INFO_COLOR APP_BUTTON_CONFIG_INFO_COLOR
  51. #define NRF_LOG_DEBUG_COLOR APP_BUTTON_CONFIG_DEBUG_COLOR
  52. #else //APP_BUTTON_CONFIG_LOG_ENABLED
  53. #define NRF_LOG_LEVEL 0
  54. #endif //APP_BUTTON_CONFIG_LOG_ENABLED
  55. #include "nrf_log.h"
  56. NRF_LOG_MODULE_REGISTER();
  57. /*
  58. * For each pin state machine is used. Since GPIOTE PORT event is common for all pin is might be
  59. * missed. Module relies on interrupt from GPIOTE only to active periodic app_timer in which pin
  60. * is sampled. Timer is stopped when there is no active buttons (all buttons are in idle state).
  61. *
  62. * Transition to the new state is based on currently sampled button value. State machine has
  63. * following transitions:
  64. *
  65. * -----------------------------------------------------
  66. * | value | current state | new state |
  67. * |---------------------------------------------------|
  68. * | 0 | IDLE | IDLE |
  69. * | 1 | IDLE | PRESS_ARMED |
  70. * | 0 | PRESS_ARMED | IDLE |
  71. * | 1 | PRESS_ARMED | PRESS_DETECTED |
  72. * | 1 | PRESS_DETECTED | PRESSED (push event) |
  73. * | 0 | PRESS_DETECTED | PRESS_ARMED |
  74. * | 0 | PRESSED | RELEASE_DETECTED |
  75. * | 1 | PRESSED | PRESSED |
  76. * | 0 | RELEASE_DETECTED | IDLE (release event) |
  77. * | 1 | RELEASE_DETECTED | PRESSED |
  78. * -----------------------------------------------------
  79. *
  80. */
  81. static app_button_cfg_t const * mp_buttons = NULL; /**< Button configuration. */
  82. static uint8_t m_button_count; /**< Number of configured buttons. */
  83. static uint32_t m_detection_delay; /**< Delay before a button is reported as pushed. */
  84. APP_TIMER_DEF(m_detection_delay_timer_id); /**< Polling timer id. */
  85. static uint64_t m_pin_active;
  86. #define BIT_PER_PIN 4
  87. #define PINS 32*GPIO_COUNT
  88. STATIC_ASSERT(BIT_PER_PIN == 4);
  89. static uint8_t m_pin_states[PINS*BIT_PER_PIN/8];
  90. typedef enum {
  91. BTN_IDLE,
  92. BTN_PRESS_ARMED,
  93. BTN_PRESS_DETECTED,
  94. BTN_PRESSED,
  95. BTN_RELEASE_DETECTED
  96. } btn_state_t;
  97. /* Retrieve given pin state. States are stored in pairs (4 bit per pin) in byte array. */
  98. static btn_state_t state_get(uint8_t pin)
  99. {
  100. uint8_t pair_state = m_pin_states[pin >> 1];
  101. uint8_t state = (pin & 0x1) ? (pair_state >> BIT_PER_PIN) : (pair_state & 0x0F);
  102. return (btn_state_t)state;
  103. }
  104. /* Set pin state. */
  105. static void state_set(uint8_t pin, btn_state_t state)
  106. {
  107. uint8_t mask = (pin & 1) ? 0x0F : 0xF0;
  108. uint8_t state_mask = (pin & 1) ?
  109. ((uint8_t)state << BIT_PER_PIN) : (uint8_t)state;
  110. m_pin_states[pin >> 1] &= mask;
  111. m_pin_states[pin >> 1] |= state_mask;
  112. }
  113. /* Find configuration structure for given pin. */
  114. static app_button_cfg_t const * button_get(uint8_t pin)
  115. {
  116. for (int i = 0; i < m_button_count; i++)
  117. {
  118. app_button_cfg_t const * p_btn = &mp_buttons[i];
  119. if (pin == p_btn->pin_no) {
  120. return p_btn;
  121. }
  122. }
  123. /* If button is not found then configuration is wrong. */
  124. ASSERT(false);
  125. return NULL;
  126. }
  127. static void usr_event(uint8_t pin, uint8_t type)
  128. {
  129. app_button_cfg_t const * p_btn = button_get(pin);
  130. if (p_btn && p_btn->button_handler)
  131. {
  132. NRF_LOG_DEBUG("Pin %d %s", pin, (type == APP_BUTTON_PUSH) ? "pressed" : "released");
  133. p_btn->button_handler(pin, type);
  134. }
  135. }
  136. /* State machine processing. */
  137. void evt_handle(uint8_t pin, uint8_t value)
  138. {
  139. switch(state_get(pin))
  140. {
  141. case BTN_IDLE:
  142. if (value)
  143. {
  144. NRF_LOG_DEBUG("Pin %d idle->armed", pin);
  145. state_set(pin, BTN_PRESS_ARMED);
  146. CRITICAL_REGION_ENTER();
  147. m_pin_active |= 1ULL << pin;
  148. CRITICAL_REGION_EXIT();
  149. }
  150. else
  151. {
  152. /* stay in IDLE */
  153. }
  154. break;
  155. case BTN_PRESS_ARMED:
  156. state_set(pin, value ? BTN_PRESS_DETECTED : BTN_IDLE);
  157. NRF_LOG_DEBUG("Pin %d armed->%s", pin, value ? "detected" : "idle");
  158. break;
  159. case BTN_PRESS_DETECTED:
  160. if (value)
  161. {
  162. state_set(pin, BTN_PRESSED);
  163. usr_event(pin, APP_BUTTON_PUSH);
  164. }
  165. else
  166. {
  167. state_set(pin, BTN_PRESS_ARMED);
  168. }
  169. NRF_LOG_DEBUG("Pin %d detected->%s", pin, value ? "pressed" : "armed");
  170. break;
  171. case BTN_PRESSED:
  172. if (value == 0)
  173. {
  174. NRF_LOG_DEBUG("Pin %d pressed->release_detected", pin);
  175. state_set(pin, BTN_RELEASE_DETECTED);
  176. }
  177. else
  178. {
  179. /* stay in pressed */
  180. }
  181. break;
  182. case BTN_RELEASE_DETECTED:
  183. if (value)
  184. {
  185. state_set(pin, BTN_PRESSED);
  186. }
  187. else
  188. {
  189. state_set(pin, BTN_IDLE);
  190. usr_event(pin, APP_BUTTON_RELEASE);
  191. CRITICAL_REGION_ENTER();
  192. m_pin_active &= ~(1ULL << pin);
  193. CRITICAL_REGION_EXIT();
  194. }
  195. NRF_LOG_DEBUG("Pin %d release_detected->%s", pin, value ? "pressed" : "idle");
  196. break;
  197. }
  198. }
  199. static void timer_start(void)
  200. {
  201. uint32_t err_code = app_timer_start(m_detection_delay_timer_id, m_detection_delay/2, NULL);
  202. if (err_code != NRF_SUCCESS)
  203. {
  204. NRF_LOG_WARNING("Failed to start app_timer (err:%d)", err_code);
  205. }
  206. }
  207. static void detection_delay_timeout_handler(void * p_context)
  208. {
  209. for (int i = 0; i < m_button_count; i++)
  210. {
  211. app_button_cfg_t const * p_btn = &mp_buttons[i];
  212. bool is_set = nrf_drv_gpiote_in_is_set(p_btn->pin_no);
  213. bool is_active = !((p_btn->active_state == APP_BUTTON_ACTIVE_HIGH) ^ is_set);
  214. evt_handle(p_btn->pin_no, is_active);
  215. }
  216. if (m_pin_active)
  217. {
  218. timer_start();
  219. }
  220. else
  221. {
  222. NRF_LOG_DEBUG("No active buttons, stopping timer");
  223. }
  224. }
  225. /* GPIOTE event is used only to start periodic timer when first button is activated. */
  226. static void gpiote_event_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
  227. {
  228. app_button_cfg_t const * p_btn = button_get(pin);
  229. bool is_set = nrf_drv_gpiote_in_is_set(p_btn->pin_no);
  230. bool is_active = !((p_btn->active_state == APP_BUTTON_ACTIVE_HIGH) ^ is_set);
  231. /* If event indicates that pin is active and no other pin is active start the timer. All
  232. * action happens in timeout event.
  233. */
  234. if (is_active && (m_pin_active == 0))
  235. {
  236. NRF_LOG_DEBUG("First active button, starting periodic timer");
  237. timer_start();
  238. }
  239. }
  240. uint32_t app_button_init(app_button_cfg_t const * p_buttons,
  241. uint8_t button_count,
  242. uint32_t detection_delay)
  243. {
  244. uint32_t err_code;
  245. if (detection_delay < 2*APP_TIMER_MIN_TIMEOUT_TICKS)
  246. {
  247. return NRF_ERROR_INVALID_PARAM;
  248. }
  249. if (!nrf_drv_gpiote_is_init())
  250. {
  251. err_code = nrf_drv_gpiote_init();
  252. VERIFY_SUCCESS(err_code);
  253. }
  254. /* Save configuration. */
  255. mp_buttons = p_buttons;
  256. m_button_count = button_count;
  257. m_detection_delay = detection_delay;
  258. memset(m_pin_states, 0, sizeof(m_pin_states));
  259. m_pin_active = 0;
  260. while (button_count--)
  261. {
  262. app_button_cfg_t const * p_btn = &p_buttons[button_count];
  263. #if defined(BUTTON_HIGH_ACCURACY_ENABLED) && (BUTTON_HIGH_ACCURACY_ENABLED == 1)
  264. nrf_drv_gpiote_in_config_t config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(p_btn->hi_accuracy);
  265. #else
  266. nrf_drv_gpiote_in_config_t config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(false);
  267. #endif
  268. config.pull = p_btn->pull_cfg;
  269. err_code = nrf_drv_gpiote_in_init(p_btn->pin_no, &config, gpiote_event_handler);
  270. VERIFY_SUCCESS(err_code);
  271. }
  272. /* Create polling timer. */
  273. return app_timer_create(&m_detection_delay_timer_id,
  274. APP_TIMER_MODE_SINGLE_SHOT,
  275. detection_delay_timeout_handler);
  276. }
  277. uint32_t app_button_enable(void)
  278. {
  279. ASSERT(mp_buttons);
  280. uint32_t i;
  281. for (i = 0; i < m_button_count; i++)
  282. {
  283. nrf_drv_gpiote_in_event_enable(mp_buttons[i].pin_no, true);
  284. }
  285. return NRF_SUCCESS;
  286. }
  287. uint32_t app_button_disable(void)
  288. {
  289. ASSERT(mp_buttons);
  290. uint32_t i;
  291. for (i = 0; i < m_button_count; i++)
  292. {
  293. nrf_drv_gpiote_in_event_disable(mp_buttons[i].pin_no);
  294. }
  295. CRITICAL_REGION_ENTER();
  296. m_pin_active = 0;
  297. CRITICAL_REGION_EXIT();
  298. /* Make sure polling timer is not running. */
  299. return app_timer_stop(m_detection_delay_timer_id);
  300. }
  301. bool app_button_is_pushed(uint8_t button_id)
  302. {
  303. ASSERT(button_id <= m_button_count);
  304. ASSERT(mp_buttons != NULL);
  305. app_button_cfg_t const * p_btn = &mp_buttons[button_id];
  306. bool is_set = nrf_drv_gpiote_in_is_set(p_btn->pin_no);
  307. return !(is_set ^ (p_btn->active_state == APP_BUTTON_ACTIVE_HIGH));
  308. }
  309. #endif //NRF_MODULE_ENABLED(BUTTON)