Skip to content

Commit

Permalink
Add 10s timer to check for BLE ADV errors
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgecrw committed Jan 12, 2024
1 parent 5069e71 commit e4f9485
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 7 deletions.
2 changes: 1 addition & 1 deletion software/firmware/src/app/app_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ typedef enum { BATTERY_EMPTY = 3500, BATTERY_CRITICAL = 3680, BATTERY_NOMINAL =

#define MAX_NUM_CONNECTIONS 1

#define BLE_ADVERTISING_DURATION_MS 0
#define BLE_ADVERTISING_DURATION_MS 10000
#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
Expand Down
2 changes: 2 additions & 0 deletions software/firmware/src/boards/revEVB/pinout.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,7 @@
#define BLE_SCAN_PROBLEM_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__
2 changes: 2 additions & 0 deletions software/firmware/src/boards/revI/pinout.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,7 @@
#define BLE_SCAN_PROBLEM_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__
2 changes: 2 additions & 0 deletions software/firmware/src/boards/revK/pinout.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,7 @@
#define BLE_SCAN_PROBLEM_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__
2 changes: 2 additions & 0 deletions software/firmware/src/boards/revL/pinout.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,7 @@
#define BLE_SCAN_PROBLEM_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__
2 changes: 2 additions & 0 deletions software/firmware/src/boards/revM/pinout.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,7 @@
#define BLE_SCAN_PROBLEM_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__
30 changes: 24 additions & 6 deletions software/firmware/src/peripherals/src/bluetooth.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ void am_timer03_isr(void)
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);
AppAdvStop();
}

static void deviceManagerCallback(dmEvt_t *pDmEvt)
{
// Handle the Device Manager message based on its type
Expand All @@ -126,6 +134,7 @@ static void deviceManagerCallback(dmEvt_t *pDmEvt)
print("TotTag BLE: deviceManagerCallback: Received DM_RESET_CMPL_IND\n");
if (first_initialization)
AttsCalculateDbHash();
advertising_setup();
is_advertising = is_scanning = first_initialization = false;
is_initialized = true;
if (expected_advertising)
Expand Down Expand Up @@ -250,16 +259,21 @@ void bluetooth_init(uint8_t* uid)
pAppUpdateCfg = (appUpdateCfg_t*)&ble_update_cfg;
pAttCfg = (attCfg_t*)&ble_att_cfg;

// Initialize the BLE scanning error detection timer
am_hal_timer_config_t scan_error_timer_config;
am_hal_timer_default_config_set(&scan_error_timer_config);
scan_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, &scan_error_timer_config);
// Initialize the BLE error detection timers
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);
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);

// Set the Bluetooth address and boot the BLE radio
HciVscSetCustom_BDAddr(uid);
Expand All @@ -270,8 +284,10 @@ 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));

// Shut down the BLE controller
Expand Down Expand Up @@ -333,6 +349,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);
}

void bluetooth_register_discovery_callback(ble_discovery_callback_t callback)
Expand Down Expand Up @@ -370,8 +387,8 @@ void bluetooth_start_advertising(void)
expected_advertising = true;
if (is_initialized && !is_advertising)
{
advertising_setup();
print("TotTag BLE: Starting advertising...\n");
am_hal_timer_clear(BLE_ADV_PROBLEM_TIMER_NUMBER);
AppAdvStart(APP_MODE_CONNECTABLE);
}
}
Expand All @@ -383,6 +400,7 @@ void bluetooth_stop_advertising(void)
if (is_initialized && is_advertising)
{
print("TotTag BLE: Stopping advertising...\n");
am_hal_timer_disable(BLE_ADV_PROBLEM_TIMER_NUMBER);
AppAdvStop();
}
}
Expand Down

0 comments on commit e4f9485

Please sign in to comment.