bsp.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. /**
  2. * Copyright (c) 2014 - 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 "bsp.h"
  41. #include <stddef.h>
  42. #include <stdio.h>
  43. #include "nordic_common.h"
  44. #include "nrf.h"
  45. #include "nrf_gpio.h"
  46. #include "nrf_error.h"
  47. #include "bsp_config.h"
  48. #include "boards.h"
  49. #ifndef BSP_SIMPLE
  50. #include "app_timer.h"
  51. #include "app_button.h"
  52. #endif // BSP_SIMPLE
  53. #if LEDS_NUMBER > 0 && !(defined BSP_SIMPLE)
  54. static bsp_indication_t m_stable_state = BSP_INDICATE_IDLE;
  55. static bool m_leds_clear = false;
  56. static uint32_t m_indication_type = 0;
  57. static bool m_alert_on = false;
  58. APP_TIMER_DEF(m_bsp_leds_tmr);
  59. APP_TIMER_DEF(m_bsp_alert_tmr);
  60. #endif // LEDS_NUMBER > 0 && !(defined BSP_SIMPLE)
  61. #if BUTTONS_NUMBER > 0
  62. #ifndef BSP_SIMPLE
  63. static bsp_event_callback_t m_registered_callback = NULL;
  64. static bsp_button_event_cfg_t m_events_list[BUTTONS_NUMBER] = {{BSP_EVENT_NOTHING, BSP_EVENT_NOTHING}};
  65. APP_TIMER_DEF(m_bsp_button_tmr);
  66. static void bsp_button_event_handler(uint8_t pin_no, uint8_t button_action);
  67. #endif // BSP_SIMPLE
  68. #ifndef BSP_SIMPLE
  69. static const app_button_cfg_t app_buttons[BUTTONS_NUMBER] =
  70. {
  71. #ifdef BSP_BUTTON_0
  72. {BSP_BUTTON_0, false, BUTTON_PULL, bsp_button_event_handler},
  73. #endif // BUTTON_0
  74. #ifdef BSP_BUTTON_1
  75. {BSP_BUTTON_1, false, BUTTON_PULL, bsp_button_event_handler},
  76. #endif // BUTTON_1
  77. #ifdef BSP_BUTTON_2
  78. {BSP_BUTTON_2, false, BUTTON_PULL, bsp_button_event_handler},
  79. #endif // BUTTON_2
  80. #ifdef BSP_BUTTON_3
  81. {BSP_BUTTON_3, false, BUTTON_PULL, bsp_button_event_handler},
  82. #endif // BUTTON_3
  83. #ifdef BSP_BUTTON_4
  84. {BSP_BUTTON_4, false, BUTTON_PULL, bsp_button_event_handler},
  85. #endif // BUTTON_4
  86. #ifdef BSP_BUTTON_5
  87. {BSP_BUTTON_5, false, BUTTON_PULL, bsp_button_event_handler},
  88. #endif // BUTTON_5
  89. #ifdef BSP_BUTTON_6
  90. {BSP_BUTTON_6, false, BUTTON_PULL, bsp_button_event_handler},
  91. #endif // BUTTON_6
  92. #ifdef BSP_BUTTON_7
  93. {BSP_BUTTON_7, false, BUTTON_PULL, bsp_button_event_handler},
  94. #endif // BUTTON_7
  95. };
  96. #endif // BSP_SIMPLE
  97. #endif // BUTTONS_NUMBER > 0
  98. #if (BUTTONS_NUMBER > 0)
  99. bool bsp_button_is_pressed(uint32_t button)
  100. {
  101. if (button < BUTTONS_NUMBER)
  102. {
  103. return bsp_board_button_state_get(button);
  104. }
  105. else
  106. {
  107. //If button is not present always return false
  108. return false;
  109. }
  110. }
  111. #endif
  112. #if (BUTTONS_NUMBER > 0) && !(defined BSP_SIMPLE)
  113. /**@brief Function for handling button events.
  114. *
  115. * @param[in] pin_no The pin number of the button pressed.
  116. * @param[in] button_action Action button.
  117. */
  118. static void bsp_button_event_handler(uint8_t pin_no, uint8_t button_action)
  119. {
  120. bsp_event_t event = BSP_EVENT_NOTHING;
  121. uint32_t button = 0;
  122. uint32_t err_code;
  123. static uint8_t current_long_push_pin_no; /**< Pin number of a currently pushed button, that could become a long push if held long enough. */
  124. static bsp_event_t release_event_at_push[BUTTONS_NUMBER]; /**< Array of what the release event of each button was last time it was pushed, so that no release event is sent if the event was bound after the push of the button. */
  125. button = bsp_board_pin_to_button_idx(pin_no);
  126. if (button < BUTTONS_NUMBER)
  127. {
  128. switch (button_action)
  129. {
  130. case APP_BUTTON_PUSH:
  131. event = m_events_list[button].push_event;
  132. if (m_events_list[button].long_push_event != BSP_EVENT_NOTHING)
  133. {
  134. err_code = app_timer_start(m_bsp_button_tmr, APP_TIMER_TICKS(BSP_LONG_PUSH_TIMEOUT_MS), (void*)&current_long_push_pin_no);
  135. if (err_code == NRF_SUCCESS)
  136. {
  137. current_long_push_pin_no = pin_no;
  138. }
  139. }
  140. release_event_at_push[button] = m_events_list[button].release_event;
  141. break;
  142. case APP_BUTTON_RELEASE:
  143. (void)app_timer_stop(m_bsp_button_tmr);
  144. if (release_event_at_push[button] == m_events_list[button].release_event)
  145. {
  146. event = m_events_list[button].release_event;
  147. }
  148. break;
  149. case BSP_BUTTON_ACTION_LONG_PUSH:
  150. event = m_events_list[button].long_push_event;
  151. }
  152. }
  153. if ((event != BSP_EVENT_NOTHING) && (m_registered_callback != NULL))
  154. {
  155. m_registered_callback(event);
  156. }
  157. }
  158. /**@brief Handle events from button timer.
  159. *
  160. * @param[in] p_context parameter registered in timer start function.
  161. */
  162. static void button_timer_handler(void * p_context)
  163. {
  164. bsp_button_event_handler(*(uint8_t *)p_context, BSP_BUTTON_ACTION_LONG_PUSH);
  165. }
  166. #endif // (BUTTONS_NUMBER > 0) && !(defined BSP_SIMPLE)
  167. #if LEDS_NUMBER > 0 && !(defined BSP_SIMPLE)
  168. static void leds_off(void)
  169. {
  170. if (m_alert_on)
  171. {
  172. uint32_t i;
  173. for (i = 0; i < LEDS_NUMBER; i++)
  174. {
  175. if (i != BSP_LED_ALERT)
  176. {
  177. bsp_board_led_off(i);
  178. }
  179. }
  180. }
  181. else
  182. {
  183. bsp_board_leds_off();
  184. }
  185. }
  186. /**@brief Configure leds to indicate required state.
  187. * @param[in] indicate State to be indicated.
  188. */
  189. static uint32_t bsp_led_indication(bsp_indication_t indicate)
  190. {
  191. uint32_t err_code = NRF_SUCCESS;
  192. uint32_t next_delay = 0;
  193. if (m_leds_clear)
  194. {
  195. m_leds_clear = false;
  196. leds_off();
  197. }
  198. switch (indicate)
  199. {
  200. case BSP_INDICATE_IDLE:
  201. leds_off();
  202. err_code = app_timer_stop(m_bsp_leds_tmr);
  203. m_stable_state = indicate;
  204. break;
  205. case BSP_INDICATE_SCANNING:
  206. case BSP_INDICATE_ADVERTISING:
  207. // in advertising blink LED_0
  208. if (bsp_board_led_state_get(BSP_LED_INDICATE_INDICATE_ADVERTISING))
  209. {
  210. bsp_board_led_off(BSP_LED_INDICATE_INDICATE_ADVERTISING);
  211. next_delay = indicate ==
  212. BSP_INDICATE_ADVERTISING ? ADVERTISING_LED_OFF_INTERVAL :
  213. ADVERTISING_SLOW_LED_OFF_INTERVAL;
  214. }
  215. else
  216. {
  217. bsp_board_led_on(BSP_LED_INDICATE_INDICATE_ADVERTISING);
  218. next_delay = indicate ==
  219. BSP_INDICATE_ADVERTISING ? ADVERTISING_LED_ON_INTERVAL :
  220. ADVERTISING_SLOW_LED_ON_INTERVAL;
  221. }
  222. m_stable_state = indicate;
  223. err_code = app_timer_start(m_bsp_leds_tmr, APP_TIMER_TICKS(next_delay), NULL);
  224. break;
  225. case BSP_INDICATE_ADVERTISING_WHITELIST:
  226. // in advertising quickly blink LED_0
  227. if (bsp_board_led_state_get(BSP_LED_INDICATE_ADVERTISING_WHITELIST))
  228. {
  229. bsp_board_led_off(BSP_LED_INDICATE_ADVERTISING_WHITELIST);
  230. next_delay = indicate ==
  231. BSP_INDICATE_ADVERTISING_WHITELIST ?
  232. ADVERTISING_WHITELIST_LED_OFF_INTERVAL :
  233. ADVERTISING_SLOW_LED_OFF_INTERVAL;
  234. }
  235. else
  236. {
  237. bsp_board_led_on(BSP_LED_INDICATE_ADVERTISING_WHITELIST);
  238. next_delay = indicate ==
  239. BSP_INDICATE_ADVERTISING_WHITELIST ?
  240. ADVERTISING_WHITELIST_LED_ON_INTERVAL :
  241. ADVERTISING_SLOW_LED_ON_INTERVAL;
  242. }
  243. m_stable_state = indicate;
  244. err_code = app_timer_start(m_bsp_leds_tmr, APP_TIMER_TICKS(next_delay), NULL);
  245. break;
  246. case BSP_INDICATE_ADVERTISING_SLOW:
  247. // in advertising slowly blink LED_0
  248. if (bsp_board_led_state_get(BSP_LED_INDICATE_ADVERTISING_SLOW))
  249. {
  250. bsp_board_led_off(BSP_LED_INDICATE_ADVERTISING_SLOW);
  251. next_delay = indicate ==
  252. BSP_INDICATE_ADVERTISING_SLOW ? ADVERTISING_SLOW_LED_OFF_INTERVAL :
  253. ADVERTISING_SLOW_LED_OFF_INTERVAL;
  254. }
  255. else
  256. {
  257. bsp_board_led_on(BSP_LED_INDICATE_ADVERTISING_SLOW);
  258. next_delay = indicate ==
  259. BSP_INDICATE_ADVERTISING_SLOW ? ADVERTISING_SLOW_LED_ON_INTERVAL :
  260. ADVERTISING_SLOW_LED_ON_INTERVAL;
  261. }
  262. m_stable_state = indicate;
  263. err_code = app_timer_start(m_bsp_leds_tmr, APP_TIMER_TICKS(next_delay), NULL);
  264. break;
  265. case BSP_INDICATE_ADVERTISING_DIRECTED:
  266. // in advertising very quickly blink LED_0
  267. if (bsp_board_led_state_get(BSP_LED_INDICATE_ADVERTISING_DIRECTED))
  268. {
  269. bsp_board_led_off(BSP_LED_INDICATE_ADVERTISING_DIRECTED);
  270. next_delay = indicate ==
  271. BSP_INDICATE_ADVERTISING_DIRECTED ?
  272. ADVERTISING_DIRECTED_LED_OFF_INTERVAL :
  273. ADVERTISING_SLOW_LED_OFF_INTERVAL;
  274. }
  275. else
  276. {
  277. bsp_board_led_on(BSP_LED_INDICATE_ADVERTISING_DIRECTED);
  278. next_delay = indicate ==
  279. BSP_INDICATE_ADVERTISING_DIRECTED ?
  280. ADVERTISING_DIRECTED_LED_ON_INTERVAL :
  281. ADVERTISING_SLOW_LED_ON_INTERVAL;
  282. }
  283. m_stable_state = indicate;
  284. err_code = app_timer_start(m_bsp_leds_tmr, APP_TIMER_TICKS(next_delay), NULL);
  285. break;
  286. case BSP_INDICATE_BONDING:
  287. // in bonding fast blink LED_0
  288. bsp_board_led_invert(BSP_LED_INDICATE_BONDING);
  289. m_stable_state = indicate;
  290. err_code =
  291. app_timer_start(m_bsp_leds_tmr, APP_TIMER_TICKS(BONDING_INTERVAL), NULL);
  292. break;
  293. case BSP_INDICATE_CONNECTED:
  294. bsp_board_led_on(BSP_LED_INDICATE_CONNECTED);
  295. m_stable_state = indicate;
  296. break;
  297. case BSP_INDICATE_SENT_OK:
  298. // when sending shortly invert LED_1
  299. m_leds_clear = true;
  300. bsp_board_led_invert(BSP_LED_INDICATE_SENT_OK);
  301. err_code = app_timer_start(m_bsp_leds_tmr, APP_TIMER_TICKS(SENT_OK_INTERVAL), NULL);
  302. break;
  303. case BSP_INDICATE_SEND_ERROR:
  304. // on receving error invert LED_1 for long time
  305. m_leds_clear = true;
  306. bsp_board_led_invert(BSP_LED_INDICATE_SEND_ERROR);
  307. err_code = app_timer_start(m_bsp_leds_tmr, APP_TIMER_TICKS(SEND_ERROR_INTERVAL), NULL);
  308. break;
  309. case BSP_INDICATE_RCV_OK:
  310. // when receving shortly invert LED_1
  311. m_leds_clear = true;
  312. bsp_board_led_invert(BSP_LED_INDICATE_RCV_OK);
  313. err_code = app_timer_start(m_bsp_leds_tmr, APP_TIMER_TICKS(RCV_OK_INTERVAL), NULL);
  314. break;
  315. case BSP_INDICATE_RCV_ERROR:
  316. // on receving error invert LED_1 for long time
  317. m_leds_clear = true;
  318. bsp_board_led_invert(BSP_LED_INDICATE_RCV_ERROR);
  319. err_code = app_timer_start(m_bsp_leds_tmr, APP_TIMER_TICKS(RCV_ERROR_INTERVAL), NULL);
  320. break;
  321. case BSP_INDICATE_FATAL_ERROR:
  322. // on fatal error turn on all leds
  323. bsp_board_leds_on();
  324. m_stable_state = indicate;
  325. break;
  326. case BSP_INDICATE_ALERT_0:
  327. case BSP_INDICATE_ALERT_1:
  328. case BSP_INDICATE_ALERT_2:
  329. case BSP_INDICATE_ALERT_3:
  330. case BSP_INDICATE_ALERT_OFF:
  331. err_code = app_timer_stop(m_bsp_alert_tmr);
  332. next_delay = (uint32_t)BSP_INDICATE_ALERT_OFF - (uint32_t)indicate;
  333. // a little trick to find out that if it did not fall through ALERT_OFF
  334. if (next_delay && (err_code == NRF_SUCCESS))
  335. {
  336. if (next_delay > 1)
  337. {
  338. err_code = app_timer_start(m_bsp_alert_tmr,
  339. APP_TIMER_TICKS(((uint16_t)next_delay * ALERT_INTERVAL)),
  340. NULL);
  341. }
  342. bsp_board_led_on(BSP_LED_ALERT);
  343. m_alert_on = true;
  344. }
  345. else
  346. {
  347. bsp_board_led_off(BSP_LED_ALERT);
  348. m_alert_on = false;
  349. }
  350. break;
  351. case BSP_INDICATE_USER_STATE_OFF:
  352. leds_off();
  353. m_stable_state = indicate;
  354. break;
  355. case BSP_INDICATE_USER_STATE_0:
  356. leds_off();
  357. bsp_board_led_on(BSP_LED_INDICATE_USER_LED1);
  358. m_stable_state = indicate;
  359. break;
  360. case BSP_INDICATE_USER_STATE_1:
  361. leds_off();
  362. bsp_board_led_on(BSP_LED_INDICATE_USER_LED2);
  363. m_stable_state = indicate;
  364. break;
  365. case BSP_INDICATE_USER_STATE_2:
  366. leds_off();
  367. bsp_board_led_on(BSP_LED_INDICATE_USER_LED1);
  368. bsp_board_led_on(BSP_LED_INDICATE_USER_LED2);
  369. m_stable_state = indicate;
  370. break;
  371. case BSP_INDICATE_USER_STATE_3:
  372. case BSP_INDICATE_USER_STATE_ON:
  373. bsp_board_leds_on();
  374. m_stable_state = indicate;
  375. break;
  376. default:
  377. break;
  378. }
  379. return err_code;
  380. }
  381. /**@brief Handle events from leds timer.
  382. *
  383. * @note Timer handler does not support returning an error code.
  384. * Errors from bsp_led_indication() are not propagated.
  385. *
  386. * @param[in] p_context parameter registered in timer start function.
  387. */
  388. static void leds_timer_handler(void * p_context)
  389. {
  390. UNUSED_PARAMETER(p_context);
  391. if (m_indication_type & BSP_INIT_LEDS)
  392. {
  393. UNUSED_VARIABLE(bsp_led_indication(m_stable_state));
  394. }
  395. }
  396. /**@brief Handle events from alert timer.
  397. *
  398. * @param[in] p_context parameter registered in timer start function.
  399. */
  400. static void alert_timer_handler(void * p_context)
  401. {
  402. UNUSED_PARAMETER(p_context);
  403. bsp_board_led_invert(BSP_LED_ALERT);
  404. }
  405. #endif // #if LEDS_NUMBER > 0 && !(defined BSP_SIMPLE)
  406. /**@brief Configure indicators to required state.
  407. */
  408. uint32_t bsp_indication_set(bsp_indication_t indicate)
  409. {
  410. uint32_t err_code = NRF_SUCCESS;
  411. #if LEDS_NUMBER > 0 && !(defined BSP_SIMPLE)
  412. if (m_indication_type & BSP_INIT_LEDS)
  413. {
  414. err_code = bsp_led_indication(indicate);
  415. }
  416. #endif // LEDS_NUMBER > 0 && !(defined BSP_SIMPLE)
  417. return err_code;
  418. }
  419. uint32_t bsp_init(uint32_t type, bsp_event_callback_t callback)
  420. {
  421. uint32_t err_code = NRF_SUCCESS;
  422. #if LEDS_NUMBER > 0 && !(defined BSP_SIMPLE)
  423. m_indication_type = type;
  424. #endif // LEDS_NUMBER > 0 && !(defined BSP_SIMPLE)
  425. #if (BUTTONS_NUMBER > 0) && !(defined BSP_SIMPLE)
  426. m_registered_callback = callback;
  427. // BSP will support buttons and generate events
  428. if (type & BSP_INIT_BUTTONS)
  429. {
  430. uint32_t num;
  431. for (num = 0; ((num < BUTTONS_NUMBER) && (err_code == NRF_SUCCESS)); num++)
  432. {
  433. err_code = bsp_event_to_button_action_assign(num, BSP_BUTTON_ACTION_PUSH, BSP_EVENT_DEFAULT);
  434. }
  435. if (err_code == NRF_SUCCESS)
  436. {
  437. err_code = app_button_init((app_button_cfg_t *)app_buttons,
  438. BUTTONS_NUMBER,
  439. APP_TIMER_TICKS(50));
  440. }
  441. if (err_code == NRF_SUCCESS)
  442. {
  443. err_code = app_button_enable();
  444. }
  445. if (err_code == NRF_SUCCESS)
  446. {
  447. err_code = app_timer_create(&m_bsp_button_tmr,
  448. APP_TIMER_MODE_SINGLE_SHOT,
  449. button_timer_handler);
  450. }
  451. }
  452. #elif (BUTTONS_NUMBER > 0) && (defined BSP_SIMPLE)
  453. bsp_board_init(type);
  454. #endif // (BUTTONS_NUMBER > 0) && !(defined BSP_SIMPLE)
  455. #if LEDS_NUMBER > 0 && !(defined BSP_SIMPLE)
  456. if (type & BSP_INIT_LEDS)
  457. {
  458. //handle LEDs only. Buttons are already handled.
  459. bsp_board_init(BSP_INIT_LEDS);
  460. // timers module must be already initialized!
  461. if (err_code == NRF_SUCCESS)
  462. {
  463. err_code =
  464. app_timer_create(&m_bsp_leds_tmr, APP_TIMER_MODE_SINGLE_SHOT, leds_timer_handler);
  465. }
  466. if (err_code == NRF_SUCCESS)
  467. {
  468. err_code =
  469. app_timer_create(&m_bsp_alert_tmr, APP_TIMER_MODE_REPEATED, alert_timer_handler);
  470. }
  471. }
  472. #endif // LEDS_NUMBER > 0 && !(defined BSP_SIMPLE)
  473. return err_code;
  474. }
  475. #ifndef BSP_SIMPLE
  476. /**@brief Assign specific event to button.
  477. */
  478. uint32_t bsp_event_to_button_action_assign(uint32_t button, bsp_button_action_t action, bsp_event_t event)
  479. {
  480. uint32_t err_code = NRF_SUCCESS;
  481. #if BUTTONS_NUMBER > 0
  482. if (button < BUTTONS_NUMBER)
  483. {
  484. if (event == BSP_EVENT_DEFAULT)
  485. {
  486. // Setting default action: BSP_EVENT_KEY_x for PUSH actions, BSP_EVENT_NOTHING for RELEASE and LONG_PUSH actions.
  487. event = (action == BSP_BUTTON_ACTION_PUSH) ? (bsp_event_t)(BSP_EVENT_KEY_0 + button) : BSP_EVENT_NOTHING;
  488. }
  489. switch (action)
  490. {
  491. case BSP_BUTTON_ACTION_PUSH:
  492. m_events_list[button].push_event = event;
  493. break;
  494. case BSP_BUTTON_ACTION_LONG_PUSH:
  495. m_events_list[button].long_push_event = event;
  496. break;
  497. case BSP_BUTTON_ACTION_RELEASE:
  498. m_events_list[button].release_event = event;
  499. break;
  500. default:
  501. err_code = NRF_ERROR_INVALID_PARAM;
  502. break;
  503. }
  504. }
  505. else
  506. {
  507. err_code = NRF_ERROR_INVALID_PARAM;
  508. }
  509. #else
  510. err_code = NRF_ERROR_INVALID_PARAM;
  511. #endif // BUTTONS_NUMBER > 0
  512. return err_code;
  513. }
  514. #endif // BSP_SIMPLE
  515. uint32_t bsp_buttons_enable()
  516. {
  517. #if (BUTTONS_NUMBER > 0) && !defined(BSP_SIMPLE)
  518. return app_button_enable();
  519. #else
  520. return NRF_ERROR_NOT_SUPPORTED;
  521. #endif
  522. }
  523. uint32_t bsp_buttons_disable()
  524. {
  525. #if (BUTTONS_NUMBER > 0) && !defined(BSP_SIMPLE)
  526. return app_button_disable();
  527. #else
  528. return NRF_ERROR_NOT_SUPPORTED;
  529. #endif
  530. }
  531. static uint32_t wakeup_button_cfg(uint32_t button_idx, bool enable)
  532. {
  533. #if !defined(BSP_SIMPLE)
  534. if (button_idx < BUTTONS_NUMBER)
  535. {
  536. nrf_gpio_pin_sense_t sense = enable ?
  537. (BUTTONS_ACTIVE_STATE ? NRF_GPIO_PIN_SENSE_HIGH : NRF_GPIO_PIN_SENSE_LOW) :
  538. NRF_GPIO_PIN_NOSENSE;
  539. nrf_gpio_cfg_sense_set(bsp_board_button_idx_to_pin(button_idx), sense);
  540. return NRF_SUCCESS;
  541. }
  542. #else
  543. UNUSED_PARAMETER(button_idx);
  544. UNUSED_PARAMETER(enable);
  545. #endif
  546. return NRF_ERROR_NOT_SUPPORTED;
  547. }
  548. uint32_t bsp_wakeup_button_enable(uint32_t button_idx)
  549. {
  550. return wakeup_button_cfg(button_idx, true);
  551. }
  552. uint32_t bsp_wakeup_button_disable(uint32_t button_idx)
  553. {
  554. return wakeup_button_cfg(button_idx, false);
  555. }