app_button.c 11 KB

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