123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638 |
- #include "bsp.h"
- #include <stddef.h>
- #include <stdio.h>
- #include "nordic_common.h"
- #include "nrf.h"
- #include "nrf_gpio.h"
- #include "nrf_error.h"
- #include "bsp_config.h"
- #include "boards.h"
- #ifndef BSP_SIMPLE
- #include "app_timer.h"
- #include "app_button.h"
- #endif
- #if LEDS_NUMBER > 0 && !(defined BSP_SIMPLE)
- static bsp_indication_t m_stable_state = BSP_INDICATE_IDLE;
- static bool m_leds_clear = false;
- static uint32_t m_indication_type = 0;
- static bool m_alert_on = false;
- APP_TIMER_DEF(m_bsp_leds_tmr);
- APP_TIMER_DEF(m_bsp_alert_tmr);
- #endif
- #if BUTTONS_NUMBER > 0
- #ifndef BSP_SIMPLE
- static bsp_event_callback_t m_registered_callback = NULL;
- static bsp_button_event_cfg_t m_events_list[BUTTONS_NUMBER] = {{BSP_EVENT_NOTHING, BSP_EVENT_NOTHING}};
- APP_TIMER_DEF(m_bsp_button_tmr);
- static void bsp_button_event_handler(uint8_t pin_no, uint8_t button_action);
- #endif
- #ifndef BSP_SIMPLE
- static const app_button_cfg_t app_buttons[BUTTONS_NUMBER] =
- {
- #ifdef BSP_BUTTON_0
- {BSP_BUTTON_0, false, BUTTON_PULL, bsp_button_event_handler},
- #endif
- #ifdef BSP_BUTTON_1
- {BSP_BUTTON_1, false, BUTTON_PULL, bsp_button_event_handler},
- #endif
- #ifdef BSP_BUTTON_2
- {BSP_BUTTON_2, false, BUTTON_PULL, bsp_button_event_handler},
- #endif
- #ifdef BSP_BUTTON_3
- {BSP_BUTTON_3, false, BUTTON_PULL, bsp_button_event_handler},
- #endif
- #ifdef BSP_BUTTON_4
- {BSP_BUTTON_4, false, BUTTON_PULL, bsp_button_event_handler},
- #endif
- #ifdef BSP_BUTTON_5
- {BSP_BUTTON_5, false, BUTTON_PULL, bsp_button_event_handler},
- #endif
- #ifdef BSP_BUTTON_6
- {BSP_BUTTON_6, false, BUTTON_PULL, bsp_button_event_handler},
- #endif
- #ifdef BSP_BUTTON_7
- {BSP_BUTTON_7, false, BUTTON_PULL, bsp_button_event_handler},
- #endif
- };
- #endif
- #endif
- #if (BUTTONS_NUMBER > 0)
- bool bsp_button_is_pressed(uint32_t button)
- {
- if (button < BUTTONS_NUMBER)
- {
- return bsp_board_button_state_get(button);
- }
- else
- {
-
- return false;
- }
- }
- #endif
- #if (BUTTONS_NUMBER > 0) && !(defined BSP_SIMPLE)
- static void bsp_button_event_handler(uint8_t pin_no, uint8_t button_action)
- {
- bsp_event_t event = BSP_EVENT_NOTHING;
- uint32_t button = 0;
- uint32_t err_code;
- static uint8_t current_long_push_pin_no;
- static bsp_event_t release_event_at_push[BUTTONS_NUMBER];
- button = bsp_board_pin_to_button_idx(pin_no);
- if (button < BUTTONS_NUMBER)
- {
- switch (button_action)
- {
- case APP_BUTTON_PUSH:
- event = m_events_list[button].push_event;
- if (m_events_list[button].long_push_event != BSP_EVENT_NOTHING)
- {
- err_code = app_timer_start(m_bsp_button_tmr, APP_TIMER_TICKS(BSP_LONG_PUSH_TIMEOUT_MS), (void*)¤t_long_push_pin_no);
- if (err_code == NRF_SUCCESS)
- {
- current_long_push_pin_no = pin_no;
- }
- }
- release_event_at_push[button] = m_events_list[button].release_event;
- break;
- case APP_BUTTON_RELEASE:
- (void)app_timer_stop(m_bsp_button_tmr);
- if (release_event_at_push[button] == m_events_list[button].release_event)
- {
- event = m_events_list[button].release_event;
- }
- break;
- case BSP_BUTTON_ACTION_LONG_PUSH:
- event = m_events_list[button].long_push_event;
- }
- }
- if ((event != BSP_EVENT_NOTHING) && (m_registered_callback != NULL))
- {
- m_registered_callback(event);
- }
- }
- static void button_timer_handler(void * p_context)
- {
- bsp_button_event_handler(*(uint8_t *)p_context, BSP_BUTTON_ACTION_LONG_PUSH);
- }
- #endif
- #if LEDS_NUMBER > 0 && !(defined BSP_SIMPLE)
- static void leds_off(void)
- {
- if (m_alert_on)
- {
- uint32_t i;
- for (i = 0; i < LEDS_NUMBER; i++)
- {
- if (i != BSP_LED_ALERT)
- {
- bsp_board_led_off(i);
- }
- }
- }
- else
- {
- bsp_board_leds_off();
- }
- }
- static uint32_t bsp_led_indication(bsp_indication_t indicate)
- {
- uint32_t err_code = NRF_SUCCESS;
- uint32_t next_delay = 0;
- if (m_leds_clear)
- {
- m_leds_clear = false;
- leds_off();
- }
- switch (indicate)
- {
- case BSP_INDICATE_IDLE:
- leds_off();
- err_code = app_timer_stop(m_bsp_leds_tmr);
- m_stable_state = indicate;
- break;
- case BSP_INDICATE_SCANNING:
- case BSP_INDICATE_ADVERTISING:
-
- if (bsp_board_led_state_get(BSP_LED_INDICATE_INDICATE_ADVERTISING))
- {
- bsp_board_led_off(BSP_LED_INDICATE_INDICATE_ADVERTISING);
- next_delay = indicate ==
- BSP_INDICATE_ADVERTISING ? ADVERTISING_LED_OFF_INTERVAL :
- ADVERTISING_SLOW_LED_OFF_INTERVAL;
- }
- else
- {
- bsp_board_led_on(BSP_LED_INDICATE_INDICATE_ADVERTISING);
- next_delay = indicate ==
- BSP_INDICATE_ADVERTISING ? ADVERTISING_LED_ON_INTERVAL :
- ADVERTISING_SLOW_LED_ON_INTERVAL;
- }
- m_stable_state = indicate;
- err_code = app_timer_start(m_bsp_leds_tmr, APP_TIMER_TICKS(next_delay), NULL);
- break;
- case BSP_INDICATE_ADVERTISING_WHITELIST:
-
- if (bsp_board_led_state_get(BSP_LED_INDICATE_ADVERTISING_WHITELIST))
- {
- bsp_board_led_off(BSP_LED_INDICATE_ADVERTISING_WHITELIST);
- next_delay = indicate ==
- BSP_INDICATE_ADVERTISING_WHITELIST ?
- ADVERTISING_WHITELIST_LED_OFF_INTERVAL :
- ADVERTISING_SLOW_LED_OFF_INTERVAL;
- }
- else
- {
- bsp_board_led_on(BSP_LED_INDICATE_ADVERTISING_WHITELIST);
- next_delay = indicate ==
- BSP_INDICATE_ADVERTISING_WHITELIST ?
- ADVERTISING_WHITELIST_LED_ON_INTERVAL :
- ADVERTISING_SLOW_LED_ON_INTERVAL;
- }
- m_stable_state = indicate;
- err_code = app_timer_start(m_bsp_leds_tmr, APP_TIMER_TICKS(next_delay), NULL);
- break;
- case BSP_INDICATE_ADVERTISING_SLOW:
-
- if (bsp_board_led_state_get(BSP_LED_INDICATE_ADVERTISING_SLOW))
- {
- bsp_board_led_off(BSP_LED_INDICATE_ADVERTISING_SLOW);
- next_delay = indicate ==
- BSP_INDICATE_ADVERTISING_SLOW ? ADVERTISING_SLOW_LED_OFF_INTERVAL :
- ADVERTISING_SLOW_LED_OFF_INTERVAL;
- }
- else
- {
- bsp_board_led_on(BSP_LED_INDICATE_ADVERTISING_SLOW);
- next_delay = indicate ==
- BSP_INDICATE_ADVERTISING_SLOW ? ADVERTISING_SLOW_LED_ON_INTERVAL :
- ADVERTISING_SLOW_LED_ON_INTERVAL;
- }
- m_stable_state = indicate;
- err_code = app_timer_start(m_bsp_leds_tmr, APP_TIMER_TICKS(next_delay), NULL);
- break;
- case BSP_INDICATE_ADVERTISING_DIRECTED:
-
- if (bsp_board_led_state_get(BSP_LED_INDICATE_ADVERTISING_DIRECTED))
- {
- bsp_board_led_off(BSP_LED_INDICATE_ADVERTISING_DIRECTED);
- next_delay = indicate ==
- BSP_INDICATE_ADVERTISING_DIRECTED ?
- ADVERTISING_DIRECTED_LED_OFF_INTERVAL :
- ADVERTISING_SLOW_LED_OFF_INTERVAL;
- }
- else
- {
- bsp_board_led_on(BSP_LED_INDICATE_ADVERTISING_DIRECTED);
- next_delay = indicate ==
- BSP_INDICATE_ADVERTISING_DIRECTED ?
- ADVERTISING_DIRECTED_LED_ON_INTERVAL :
- ADVERTISING_SLOW_LED_ON_INTERVAL;
- }
- m_stable_state = indicate;
- err_code = app_timer_start(m_bsp_leds_tmr, APP_TIMER_TICKS(next_delay), NULL);
- break;
- case BSP_INDICATE_BONDING:
-
- bsp_board_led_invert(BSP_LED_INDICATE_BONDING);
- m_stable_state = indicate;
- err_code =
- app_timer_start(m_bsp_leds_tmr, APP_TIMER_TICKS(BONDING_INTERVAL), NULL);
- break;
- case BSP_INDICATE_CONNECTED:
- bsp_board_led_on(BSP_LED_INDICATE_CONNECTED);
- m_stable_state = indicate;
- break;
- case BSP_INDICATE_SENT_OK:
-
- m_leds_clear = true;
- bsp_board_led_invert(BSP_LED_INDICATE_SENT_OK);
- err_code = app_timer_start(m_bsp_leds_tmr, APP_TIMER_TICKS(SENT_OK_INTERVAL), NULL);
- break;
- case BSP_INDICATE_SEND_ERROR:
-
- m_leds_clear = true;
- bsp_board_led_invert(BSP_LED_INDICATE_SEND_ERROR);
- err_code = app_timer_start(m_bsp_leds_tmr, APP_TIMER_TICKS(SEND_ERROR_INTERVAL), NULL);
- break;
- case BSP_INDICATE_RCV_OK:
-
- m_leds_clear = true;
- bsp_board_led_invert(BSP_LED_INDICATE_RCV_OK);
- err_code = app_timer_start(m_bsp_leds_tmr, APP_TIMER_TICKS(RCV_OK_INTERVAL), NULL);
- break;
- case BSP_INDICATE_RCV_ERROR:
-
- m_leds_clear = true;
- bsp_board_led_invert(BSP_LED_INDICATE_RCV_ERROR);
- err_code = app_timer_start(m_bsp_leds_tmr, APP_TIMER_TICKS(RCV_ERROR_INTERVAL), NULL);
- break;
- case BSP_INDICATE_FATAL_ERROR:
-
- bsp_board_leds_on();
- m_stable_state = indicate;
- break;
- case BSP_INDICATE_ALERT_0:
- case BSP_INDICATE_ALERT_1:
- case BSP_INDICATE_ALERT_2:
- case BSP_INDICATE_ALERT_3:
- case BSP_INDICATE_ALERT_OFF:
- err_code = app_timer_stop(m_bsp_alert_tmr);
- next_delay = (uint32_t)BSP_INDICATE_ALERT_OFF - (uint32_t)indicate;
-
- if (next_delay && (err_code == NRF_SUCCESS))
- {
- if (next_delay > 1)
- {
- err_code = app_timer_start(m_bsp_alert_tmr,
- APP_TIMER_TICKS(((uint16_t)next_delay * ALERT_INTERVAL)),
- NULL);
- }
- bsp_board_led_on(BSP_LED_ALERT);
- m_alert_on = true;
- }
- else
- {
- bsp_board_led_off(BSP_LED_ALERT);
- m_alert_on = false;
- }
- break;
- case BSP_INDICATE_USER_STATE_OFF:
- leds_off();
- m_stable_state = indicate;
- break;
- case BSP_INDICATE_USER_STATE_0:
- leds_off();
- bsp_board_led_on(BSP_LED_INDICATE_USER_LED1);
- m_stable_state = indicate;
- break;
- case BSP_INDICATE_USER_STATE_1:
- leds_off();
- bsp_board_led_on(BSP_LED_INDICATE_USER_LED2);
- m_stable_state = indicate;
- break;
- case BSP_INDICATE_USER_STATE_2:
- leds_off();
- bsp_board_led_on(BSP_LED_INDICATE_USER_LED1);
- bsp_board_led_on(BSP_LED_INDICATE_USER_LED2);
- m_stable_state = indicate;
- break;
- case BSP_INDICATE_USER_STATE_3:
- case BSP_INDICATE_USER_STATE_ON:
- bsp_board_leds_on();
- m_stable_state = indicate;
- break;
- default:
- break;
- }
- return err_code;
- }
- static void leds_timer_handler(void * p_context)
- {
- UNUSED_PARAMETER(p_context);
- if (m_indication_type & BSP_INIT_LEDS)
- {
- UNUSED_VARIABLE(bsp_led_indication(m_stable_state));
- }
- }
- static void alert_timer_handler(void * p_context)
- {
- UNUSED_PARAMETER(p_context);
- bsp_board_led_invert(BSP_LED_ALERT);
- }
- #endif
- uint32_t bsp_indication_set(bsp_indication_t indicate)
- {
- uint32_t err_code = NRF_SUCCESS;
- #if LEDS_NUMBER > 0 && !(defined BSP_SIMPLE)
- if (m_indication_type & BSP_INIT_LEDS)
- {
- err_code = bsp_led_indication(indicate);
- }
- #endif
- return err_code;
- }
- uint32_t bsp_init(uint32_t type, bsp_event_callback_t callback)
- {
- uint32_t err_code = NRF_SUCCESS;
- #if LEDS_NUMBER > 0 && !(defined BSP_SIMPLE)
- m_indication_type = type;
- #endif
- #if (BUTTONS_NUMBER > 0) && !(defined BSP_SIMPLE)
- m_registered_callback = callback;
-
- if (type & BSP_INIT_BUTTONS)
- {
- uint32_t num;
- for (num = 0; ((num < BUTTONS_NUMBER) && (err_code == NRF_SUCCESS)); num++)
- {
- err_code = bsp_event_to_button_action_assign(num, BSP_BUTTON_ACTION_PUSH, BSP_EVENT_DEFAULT);
- }
- if (err_code == NRF_SUCCESS)
- {
- err_code = app_button_init((app_button_cfg_t *)app_buttons,
- BUTTONS_NUMBER,
- APP_TIMER_TICKS(50));
- }
- if (err_code == NRF_SUCCESS)
- {
- err_code = app_button_enable();
- }
- if (err_code == NRF_SUCCESS)
- {
- err_code = app_timer_create(&m_bsp_button_tmr,
- APP_TIMER_MODE_SINGLE_SHOT,
- button_timer_handler);
- }
- }
- #elif (BUTTONS_NUMBER > 0) && (defined BSP_SIMPLE)
- bsp_board_init(type);
- #endif
- #if LEDS_NUMBER > 0 && !(defined BSP_SIMPLE)
- if (type & BSP_INIT_LEDS)
- {
-
- bsp_board_init(BSP_INIT_LEDS);
-
- if (err_code == NRF_SUCCESS)
- {
- err_code =
- app_timer_create(&m_bsp_leds_tmr, APP_TIMER_MODE_SINGLE_SHOT, leds_timer_handler);
- }
- if (err_code == NRF_SUCCESS)
- {
- err_code =
- app_timer_create(&m_bsp_alert_tmr, APP_TIMER_MODE_REPEATED, alert_timer_handler);
- }
- }
- #endif
- return err_code;
- }
- #ifndef BSP_SIMPLE
- uint32_t bsp_event_to_button_action_assign(uint32_t button, bsp_button_action_t action, bsp_event_t event)
- {
- uint32_t err_code = NRF_SUCCESS;
- #if BUTTONS_NUMBER > 0
- if (button < BUTTONS_NUMBER)
- {
- if (event == BSP_EVENT_DEFAULT)
- {
-
- event = (action == BSP_BUTTON_ACTION_PUSH) ? (bsp_event_t)(BSP_EVENT_KEY_0 + button) : BSP_EVENT_NOTHING;
- }
- switch (action)
- {
- case BSP_BUTTON_ACTION_PUSH:
- m_events_list[button].push_event = event;
- break;
- case BSP_BUTTON_ACTION_LONG_PUSH:
- m_events_list[button].long_push_event = event;
- break;
- case BSP_BUTTON_ACTION_RELEASE:
- m_events_list[button].release_event = event;
- break;
- default:
- err_code = NRF_ERROR_INVALID_PARAM;
- break;
- }
- }
- else
- {
- err_code = NRF_ERROR_INVALID_PARAM;
- }
- #else
- err_code = NRF_ERROR_INVALID_PARAM;
- #endif
- return err_code;
- }
- #endif
- uint32_t bsp_buttons_enable()
- {
- #if (BUTTONS_NUMBER > 0) && !defined(BSP_SIMPLE)
- return app_button_enable();
- #else
- return NRF_ERROR_NOT_SUPPORTED;
- #endif
- }
- uint32_t bsp_buttons_disable()
- {
- #if (BUTTONS_NUMBER > 0) && !defined(BSP_SIMPLE)
- return app_button_disable();
- #else
- return NRF_ERROR_NOT_SUPPORTED;
- #endif
- }
- static uint32_t wakeup_button_cfg(uint32_t button_idx, bool enable)
- {
- #if !defined(BSP_SIMPLE)
- if (button_idx < BUTTONS_NUMBER)
- {
- nrf_gpio_pin_sense_t sense = enable ?
- (BUTTONS_ACTIVE_STATE ? NRF_GPIO_PIN_SENSE_HIGH : NRF_GPIO_PIN_SENSE_LOW) :
- NRF_GPIO_PIN_NOSENSE;
- nrf_gpio_cfg_sense_set(bsp_board_button_idx_to_pin(button_idx), sense);
- return NRF_SUCCESS;
- }
- #else
- UNUSED_PARAMETER(button_idx);
- UNUSED_PARAMETER(enable);
- #endif
- return NRF_ERROR_NOT_SUPPORTED;
- }
- uint32_t bsp_wakeup_button_enable(uint32_t button_idx)
- {
- return wakeup_button_cfg(button_idx, true);
- }
- uint32_t bsp_wakeup_button_disable(uint32_t button_idx)
- {
- return wakeup_button_cfg(button_idx, false);
- }
|