Skip to content

Commit

Permalink
Consolidate BLE error timers into one
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgecrw committed Jan 12, 2024
1 parent eada61d commit 2122a38
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 57 deletions.
4 changes: 2 additions & 2 deletions software/firmware/src/app/app_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ typedef enum { BATTERY_EMPTY = 3500, BATTERY_CRITICAL = 3680, BATTERY_NOMINAL =

#define MAX_NUM_CONNECTIONS 1

#define BLE_ADVERTISING_DURATION_MS 10000
#define BLE_ADVERTISING_DURATION_MS 0
#define BLE_ADVERTISING_INTERVAL_0_625_MS 120 // 75 ms
#define BLE_SCANNING_WINDOW_0_625_MS 160 // 100 ms
#define BLE_SCANNING_INTERVAL_0_625_MS 1600 // 1000 ms
#define BLE_SCANNING_DURATION_MS 10000
#define BLE_SCANNING_DURATION_MS 0

#define BLE_DESIRED_MTU 247
#define BLE_TRANSACTION_TIMEOUT_S 1
Expand Down
6 changes: 2 additions & 4 deletions software/firmware/src/boards/revEVB/pinout.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,9 @@
#define BUZZER_TIMER_NUMBER 0
#define RADIO_WAKEUP_TIMER_NUMBER 2
#define RADIO_WAKEUP_TIMER_TICK_RATE_HZ (AM_HAL_CLKGEN_FREQ_MAX_HZ / 16)
#define BLE_SCAN_PROBLEM_TIMER_NUMBER 3
#define BLE_SCAN_PROBLEM_TIMER_TICK_RATE_HZ (AM_HAL_CLKGEN_FREQ_MAX_HZ / 16)
#define BLE_ERROR_TIMER_NUMBER 3
#define BLE_ERROR_TIMER_TICK_RATE_HZ (AM_HAL_CLKGEN_FREQ_MAX_HZ / 16)
#define BLE_SCANNING_TIMER_NUMBER 4
#define BLE_SCANNING_TIMER_TICK_RATE_HZ (AM_HAL_CLKGEN_FREQ_MAX_HZ / 16)
#define BLE_ADV_PROBLEM_TIMER_NUMBER 5
#define BLE_ADV_PROBLEM_TIMER_TICK_RATE_HZ (AM_HAL_CLKGEN_FREQ_MAX_HZ / 16)

#endif // #ifndef __PINOUT_HEADER_H__
6 changes: 2 additions & 4 deletions software/firmware/src/boards/revI/pinout.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,9 @@
#define BUZZER_TIMER_NUMBER 0
#define RADIO_WAKEUP_TIMER_NUMBER 2
#define RADIO_WAKEUP_TIMER_TICK_RATE_HZ (AM_HAL_CLKGEN_FREQ_MAX_HZ / 16)
#define BLE_SCAN_PROBLEM_TIMER_NUMBER 3
#define BLE_SCAN_PROBLEM_TIMER_TICK_RATE_HZ (AM_HAL_CLKGEN_FREQ_MAX_HZ / 16)
#define BLE_ERROR_TIMER_NUMBER 3
#define BLE_ERROR_TIMER_TICK_RATE_HZ (AM_HAL_CLKGEN_FREQ_MAX_HZ / 16)
#define BLE_SCANNING_TIMER_NUMBER 4
#define BLE_SCANNING_TIMER_TICK_RATE_HZ (AM_HAL_CLKGEN_FREQ_MAX_HZ / 16)
#define BLE_ADV_PROBLEM_TIMER_NUMBER 5
#define BLE_ADV_PROBLEM_TIMER_TICK_RATE_HZ (AM_HAL_CLKGEN_FREQ_MAX_HZ / 16)

#endif // #ifndef __PINOUT_HEADER_H__
6 changes: 2 additions & 4 deletions software/firmware/src/boards/revK/pinout.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,9 @@
#define BUZZER_TIMER_NUMBER 0
#define RADIO_WAKEUP_TIMER_NUMBER 2
#define RADIO_WAKEUP_TIMER_TICK_RATE_HZ (AM_HAL_CLKGEN_FREQ_MAX_HZ / 16)
#define BLE_SCAN_PROBLEM_TIMER_NUMBER 3
#define BLE_SCAN_PROBLEM_TIMER_TICK_RATE_HZ (AM_HAL_CLKGEN_FREQ_MAX_HZ / 16)
#define BLE_ERROR_TIMER_NUMBER 3
#define BLE_ERROR_TIMER_TICK_RATE_HZ (AM_HAL_CLKGEN_FREQ_MAX_HZ / 16)
#define BLE_SCANNING_TIMER_NUMBER 4
#define BLE_SCANNING_TIMER_TICK_RATE_HZ (AM_HAL_CLKGEN_FREQ_MAX_HZ / 16)
#define BLE_ADV_PROBLEM_TIMER_NUMBER 5
#define BLE_ADV_PROBLEM_TIMER_TICK_RATE_HZ (AM_HAL_CLKGEN_FREQ_MAX_HZ / 16)

#endif // #ifndef __PINOUT_HEADER_H__
6 changes: 2 additions & 4 deletions software/firmware/src/boards/revL/pinout.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,9 @@
#define BUZZER_TIMER_NUMBER 0
#define RADIO_WAKEUP_TIMER_NUMBER 2
#define RADIO_WAKEUP_TIMER_TICK_RATE_HZ (AM_HAL_CLKGEN_FREQ_MAX_HZ / 16)
#define BLE_SCAN_PROBLEM_TIMER_NUMBER 3
#define BLE_SCAN_PROBLEM_TIMER_TICK_RATE_HZ (AM_HAL_CLKGEN_FREQ_MAX_HZ / 16)
#define BLE_ERROR_TIMER_NUMBER 3
#define BLE_ERROR_TIMER_TICK_RATE_HZ (AM_HAL_CLKGEN_FREQ_MAX_HZ / 16)
#define BLE_SCANNING_TIMER_NUMBER 4
#define BLE_SCANNING_TIMER_TICK_RATE_HZ (AM_HAL_CLKGEN_FREQ_MAX_HZ / 16)
#define BLE_ADV_PROBLEM_TIMER_NUMBER 5
#define BLE_ADV_PROBLEM_TIMER_TICK_RATE_HZ (AM_HAL_CLKGEN_FREQ_MAX_HZ / 16)

#endif // #ifndef __PINOUT_HEADER_H__
6 changes: 2 additions & 4 deletions software/firmware/src/boards/revM/pinout.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,9 @@
#define BUZZER_TIMER_NUMBER 0
#define RADIO_WAKEUP_TIMER_NUMBER 2
#define RADIO_WAKEUP_TIMER_TICK_RATE_HZ (AM_HAL_CLKGEN_FREQ_MAX_HZ / 16)
#define BLE_SCAN_PROBLEM_TIMER_NUMBER 3
#define BLE_SCAN_PROBLEM_TIMER_TICK_RATE_HZ (AM_HAL_CLKGEN_FREQ_MAX_HZ / 16)
#define BLE_ERROR_TIMER_NUMBER 3
#define BLE_ERROR_TIMER_TICK_RATE_HZ (AM_HAL_CLKGEN_FREQ_MAX_HZ / 16)
#define BLE_SCANNING_TIMER_NUMBER 4
#define BLE_SCANNING_TIMER_TICK_RATE_HZ (AM_HAL_CLKGEN_FREQ_MAX_HZ / 16)
#define BLE_ADV_PROBLEM_TIMER_NUMBER 5
#define BLE_ADV_PROBLEM_TIMER_TICK_RATE_HZ (AM_HAL_CLKGEN_FREQ_MAX_HZ / 16)

#endif // #ifndef __PINOUT_HEADER_H__
51 changes: 16 additions & 35 deletions software/firmware/src/peripherals/src/bluetooth.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,11 @@ void appUiBtnPoll(void) {}
void am_timer03_isr(void)
{
// Force stop the BLE scanning cycle
am_hal_timer_interrupt_clear(AM_HAL_TIMER_MASK(BLE_SCAN_PROBLEM_TIMER_NUMBER, AM_HAL_TIMER_COMPARE_BOTH));
am_hal_timer_clear(BLE_SCAN_PROBLEM_TIMER_NUMBER);
DmScanStop();
}

void am_timer05_isr(void)
{
// Force stop the BLE scanning cycle
am_hal_timer_interrupt_clear(AM_HAL_TIMER_MASK(BLE_ADV_PROBLEM_TIMER_NUMBER, AM_HAL_TIMER_COMPARE_BOTH));
am_hal_timer_clear(BLE_ADV_PROBLEM_TIMER_NUMBER);
am_hal_timer_interrupt_clear(AM_HAL_TIMER_MASK(BLE_ERROR_TIMER_NUMBER, AM_HAL_TIMER_COMPARE_BOTH));
if (is_initialized && (expected_advertising || expected_scanning))
am_hal_timer_clear(BLE_ERROR_TIMER_NUMBER);
appAdvStop(0, NULL);
DmScanStop();
}

static void deviceManagerCallback(dmEvt_t *pDmEvt)
Expand Down Expand Up @@ -261,21 +255,16 @@ void bluetooth_init(uint8_t* uid)
pAppUpdateCfg = (appUpdateCfg_t*)&ble_update_cfg;
pAttCfg = (attCfg_t*)&ble_att_cfg;

// Initialize the BLE error detection timers
// Initialize the BLE error detection timer
am_hal_timer_config_t ble_error_timer_config;
am_hal_timer_default_config_set(&ble_error_timer_config);
ble_error_timer_config.ui32Compare0 = (uint32_t)((BLE_SCANNING_DURATION_MS / 700) * BLE_SCAN_PROBLEM_TIMER_TICK_RATE_HZ);
am_hal_timer_config(BLE_SCAN_PROBLEM_TIMER_NUMBER, &ble_error_timer_config);
am_hal_timer_interrupt_enable(AM_HAL_TIMER_MASK(BLE_SCAN_PROBLEM_TIMER_NUMBER, AM_HAL_TIMER_COMPARE0));
ble_error_timer_config.ui32Compare0 = (uint32_t)((BLE_ADVERTISING_DURATION_MS / 700) * BLE_SCAN_PROBLEM_TIMER_TICK_RATE_HZ);
am_hal_timer_config(BLE_ADV_PROBLEM_TIMER_NUMBER, &ble_error_timer_config);
am_hal_timer_interrupt_enable(AM_HAL_TIMER_MASK(BLE_ADV_PROBLEM_TIMER_NUMBER, AM_HAL_TIMER_COMPARE0));
NVIC_SetPriority(TIMER0_IRQn + BLE_SCAN_PROBLEM_TIMER_NUMBER, NVIC_configKERNEL_INTERRUPT_PRIORITY);
NVIC_SetPriority(TIMER0_IRQn + BLE_ADV_PROBLEM_TIMER_NUMBER, NVIC_configKERNEL_INTERRUPT_PRIORITY);
ble_error_timer_config.ui32Compare0 = (uint32_t)(10 * BLE_ERROR_TIMER_TICK_RATE_HZ);
am_hal_timer_config(BLE_ERROR_TIMER_NUMBER, &ble_error_timer_config);
am_hal_timer_interrupt_enable(AM_HAL_TIMER_MASK(BLE_ERROR_TIMER_NUMBER, AM_HAL_TIMER_COMPARE0));
NVIC_SetPriority(TIMER0_IRQn + BLE_ERROR_TIMER_NUMBER, NVIC_configKERNEL_INTERRUPT_PRIORITY);
NVIC_SetPriority(COOPER_IOM_IRQn, NVIC_configMAX_SYSCALL_INTERRUPT_PRIORITY);
NVIC_SetPriority(AM_COOPER_IRQn, NVIC_configMAX_SYSCALL_INTERRUPT_PRIORITY);
NVIC_EnableIRQ(TIMER0_IRQn + BLE_SCAN_PROBLEM_TIMER_NUMBER);
NVIC_EnableIRQ(TIMER0_IRQn + BLE_ADV_PROBLEM_TIMER_NUMBER);
NVIC_EnableIRQ(TIMER0_IRQn + BLE_ERROR_TIMER_NUMBER);

// Set the Bluetooth address and boot the BLE radio
HciVscSetCustom_BDAddr(uid);
Expand All @@ -286,11 +275,9 @@ void bluetooth_init(uint8_t* uid)
void bluetooth_deinit(void)
{
// Stop all running timers
am_hal_timer_disable(BLE_ADV_PROBLEM_TIMER_NUMBER);
am_hal_timer_disable(BLE_SCAN_PROBLEM_TIMER_NUMBER);
NVIC_DisableIRQ(TIMER0_IRQn + BLE_SCAN_PROBLEM_TIMER_NUMBER);
am_hal_timer_interrupt_disable(AM_HAL_TIMER_MASK(BLE_ADV_PROBLEM_TIMER_NUMBER, AM_HAL_TIMER_COMPARE0));
am_hal_timer_interrupt_disable(AM_HAL_TIMER_MASK(BLE_SCAN_PROBLEM_TIMER_NUMBER, AM_HAL_TIMER_COMPARE0));
am_hal_timer_disable(BLE_ERROR_TIMER_NUMBER);
NVIC_DisableIRQ(TIMER0_IRQn + BLE_ERROR_TIMER_NUMBER);
am_hal_timer_interrupt_disable(AM_HAL_TIMER_MASK(BLE_ERROR_TIMER_NUMBER, AM_HAL_TIMER_COMPARE0));;

// Shut down the BLE controller
HciDrvRadioShutdown();
Expand Down Expand Up @@ -352,8 +339,7 @@ void bluetooth_set_uninitialized(void)
{
// Force all initialization and activity flags to false
is_initialized = is_advertising = is_scanning = false;
am_hal_timer_disable(BLE_SCAN_PROBLEM_TIMER_NUMBER);
am_hal_timer_disable(BLE_ADV_PROBLEM_TIMER_NUMBER);
am_hal_timer_disable(BLE_ERROR_TIMER_NUMBER);
}

void bluetooth_register_discovery_callback(ble_discovery_callback_t callback)
Expand Down Expand Up @@ -395,15 +381,14 @@ void bluetooth_start_advertising(void)
uint8_t advHandle = DM_ADV_HANDLE_DEFAULT, maxEaEvents = 0;
appSlaveCb.advState[DM_ADV_HANDLE_DEFAULT] = APP_ADV_STATE1;
appSlaveAdvStart(1, &advHandle, &(pAppAdvCfg->advInterval[APP_ADV_STATE1]), &(pAppAdvCfg->advDuration[APP_ADV_STATE1]), &maxEaEvents, TRUE, APP_MODE_CONNECTABLE);
am_hal_timer_clear(BLE_ADV_PROBLEM_TIMER_NUMBER);
am_hal_timer_clear(BLE_ERROR_TIMER_NUMBER);
}
}

void bluetooth_stop_advertising(void)
{
// Attempt to stop advertising
expected_advertising = false;
am_hal_timer_disable(BLE_ADV_PROBLEM_TIMER_NUMBER);
if (is_initialized && is_advertising)
{
print("TotTag BLE: Stopping advertising...\n");
Expand All @@ -424,16 +409,15 @@ void bluetooth_start_scanning(void)
if (is_initialized && !is_scanning)
{
print("TotTag BLE: Starting scanning...\n");
am_hal_timer_clear(BLE_SCAN_PROBLEM_TIMER_NUMBER);
DmScanStart(HCI_SCAN_PHY_LE_1M_BIT, ble_master_cfg.discMode, &ble_master_cfg.scanType, TRUE, ble_master_cfg.scanDuration, 0);
am_hal_timer_clear(BLE_ERROR_TIMER_NUMBER);
}
}

void bluetooth_stop_scanning(void)
{
// Attempt to stop scanning
expected_scanning = false;
am_hal_timer_disable(BLE_SCAN_PROBLEM_TIMER_NUMBER);
if (is_initialized && is_scanning)
{
print("TotTag BLE: Stopping scanning...\n");
Expand All @@ -445,10 +429,7 @@ void bluetooth_reset_scanning(void)
{
// Attempt to stop scanning without changing the scanning expectation
if (is_initialized && is_scanning)
{
am_hal_timer_clear(BLE_SCAN_PROBLEM_TIMER_NUMBER);
DmScanStop();
}
}

bool bluetooth_is_scanning(void)
Expand Down

0 comments on commit 2122a38

Please sign in to comment.