diff --git a/software/firmware/src/app/app_config.h b/software/firmware/src/app/app_config.h index 520cc674..b62324f6 100644 --- a/software/firmware/src/app/app_config.h +++ b/software/firmware/src/app/app_config.h @@ -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 diff --git a/software/firmware/src/boards/revEVB/pinout.h b/software/firmware/src/boards/revEVB/pinout.h index a0aced49..561ed34f 100644 --- a/software/firmware/src/boards/revEVB/pinout.h +++ b/software/firmware/src/boards/revEVB/pinout.h @@ -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__ diff --git a/software/firmware/src/boards/revI/pinout.h b/software/firmware/src/boards/revI/pinout.h index d0ea69ef..4d45e182 100644 --- a/software/firmware/src/boards/revI/pinout.h +++ b/software/firmware/src/boards/revI/pinout.h @@ -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__ diff --git a/software/firmware/src/boards/revK/pinout.h b/software/firmware/src/boards/revK/pinout.h index b6e19c9f..0eb98bbe 100644 --- a/software/firmware/src/boards/revK/pinout.h +++ b/software/firmware/src/boards/revK/pinout.h @@ -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__ diff --git a/software/firmware/src/boards/revL/pinout.h b/software/firmware/src/boards/revL/pinout.h index d0028636..842c0508 100644 --- a/software/firmware/src/boards/revL/pinout.h +++ b/software/firmware/src/boards/revL/pinout.h @@ -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__ diff --git a/software/firmware/src/boards/revM/pinout.h b/software/firmware/src/boards/revM/pinout.h index 233f6700..b87146be 100644 --- a/software/firmware/src/boards/revM/pinout.h +++ b/software/firmware/src/boards/revM/pinout.h @@ -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__ diff --git a/software/firmware/src/peripherals/src/bluetooth.c b/software/firmware/src/peripherals/src/bluetooth.c index 22dfe511..d8c1df36 100644 --- a/software/firmware/src/peripherals/src/bluetooth.c +++ b/software/firmware/src/peripherals/src/bluetooth.c @@ -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) @@ -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); @@ -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(); @@ -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) @@ -395,7 +381,7 @@ 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); } } @@ -403,7 +389,6 @@ 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"); @@ -424,8 +409,8 @@ 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); } } @@ -433,7 +418,6 @@ 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"); @@ -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)