bsp.c 21 KB

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