Skip to content

Commit

Permalink
Don't try to store or BLE write inside ISR context
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgecrw committed Jul 18, 2024
1 parent 2e2e690 commit b2540cb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 26 deletions.
75 changes: 50 additions & 25 deletions software/firmware/src/tasks/app_task_ranging.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,15 @@ static TaskHandle_t app_task_handle;
static volatile uint8_t discovered_devices[MAX_NUM_RANGING_DEVICES][1+EUI_LEN];
static volatile uint32_t seconds_to_activate_buzzer;
static volatile uint8_t num_discovered_devices;
static volatile bool devices_found;
static volatile bool devices_found, motion_changed, imu_data_ready;
static uint32_t download_start_timestamp, download_end_timestamp;
static uint8_t imu_calibration_data;
static int16_t imu_accel_data[3];

#ifdef _TEST_IMU_DATA
static volatile uint32_t imu_raw_data_length;
static uint8_t imu_raw_data[MAX_IMU_DATA_LENGTH];
#endif


// Private Helper Functions --------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -99,8 +106,32 @@ static void verify_app_configuration(void)
static void handle_notification(app_notification_t notification)
{
// Handle the notification based on which bits are set
if ((notification & APP_NOTIFY_MOTION_EVENT))
storage_write_motion_status(imu_read_in_motion());
if ((notification & APP_NOTIFY_IMU_EVENT))
{
if (motion_changed)
{
motion_changed = false;
storage_write_motion_status(imu_read_in_motion());
}
if (imu_data_ready)
{
imu_data_ready = false;

// Write IMU data over BLE characteristic
#ifdef _LIVE_IMU_DATA
bluetooth_write_imu_data(imu_raw_data, imu_raw_data_length);
#endif

// Store relevant IMU data
#ifndef _TEST_NO_STORAGE
#ifdef _TEST_IMU_DATA
storage_write_imu_data(imu_raw_data, imu_raw_data_length);
#else
storage_write_imu_data(&imu_calibration_data, imu_accel_data);
#endif
#endif
}
}
if (((notification & APP_NOTIFY_NETWORK_LOST)) || ((notification & APP_NOTIFY_NETWORK_CONNECTED)) ||
((notification & APP_NOTIFY_VERIFY_CONFIGURATION)))
verify_app_configuration();
Expand Down Expand Up @@ -215,27 +246,22 @@ static void battery_event_handler(battery_event_t battery_event)
static void motion_change_handler(bool in_motion)
{
// Notify the app about a change in motion
app_notify(APP_NOTIFY_MOTION_EVENT, true);
motion_changed = true;
app_notify(APP_NOTIFY_IMU_EVENT, true);
}

#ifdef _TEST_IMU_DATA
static void data_ready_handler(uint8_t *calib_data, int16_t *linear_accel_data, uint8_t *raw_data, uint32_t raw_data_length)
{
//TODO
#ifdef _LIVE_IMU_DATA
bluetooth_write_imu_data(raw_data, raw_data_length);
#endif

// Store relevant IMU data
#ifndef _TEST_NO_STORAGE
// Notify the app about a change in IMU data
imu_data_ready = true;
imu_calibration_data = *calib_data;
memcpy(imu_accel_data, linear_accel_data, 3 * sizeof(int16_t));
#ifdef _TEST_IMU_DATA
storage_write_imu_data(raw_data, raw_data_length);
#else
storage_write_imu_data(calib_data, linear_accel_data);
#endif
memcpy(imu_raw_data, raw_data, raw_data_length);
imu_raw_data_length = raw_data_length;
#endif
app_notify(APP_NOTIFY_IMU_EVENT, true);
}
#endif

static void ble_discovery_handler(const uint8_t ble_address[EUI_LEN], uint8_t ranging_role)
{
Expand Down Expand Up @@ -360,6 +386,7 @@ void AppTaskRanging(void *uid)
NVIC_EnableIRQ(TIMER0_IRQn + BLE_SCANNING_TIMER_NUMBER);

// Register handlers for motion detection, battery status changes, and BLE events
motion_changed = imu_data_ready = false;
bluetooth_register_discovery_callback(ble_discovery_handler);
#ifndef _TEST_NO_BATTERY_CALLBACK
if (battery_monitor_is_plugged_in())
Expand All @@ -369,19 +396,17 @@ void AppTaskRanging(void *uid)
battery_register_event_callback(battery_event_handler);
}
#endif

#if !defined(_TEST_NO_STORAGE) && !defined(_TEST_IMU_DATA)
storage_write_motion_status(imu_read_in_motion());
imu_set_fusion_mode(OPERATION_MODE_ACCONLY);
imu_register_motion_change_callback(motion_change_handler);
#endif

#ifdef _TEST_IMU_DATA
imu_register_motion_change_callback(motion_change_handler);
imu_register_data_ready_callback(data_ready_handler);
#ifdef _TEST_IMU_DATA
imu_set_power_mode(POWER_MODE_NORMAL);
//imu_set_power_mode(POWER_MODE_LOWPOWER);
imu_set_fusion_mode(OPERATION_MODE_NDOF);
#else
#ifndef _TEST_NO_STORAGE
storage_write_motion_status(imu_read_in_motion());
#endif
imu_set_fusion_mode(OPERATION_MODE_ACCONLY);
#endif

// Retrieve current experiment details from non-volatile storage
Expand Down
2 changes: 1 addition & 1 deletion software/firmware/src/tasks/app_tasks.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ typedef enum {
APP_NOTIFY_NETWORK_LOST = 0b00000010,
APP_NOTIFY_NETWORK_FOUND = 0b00000100,
APP_NOTIFY_NETWORK_CONNECTED = 0b00001000,
APP_NOTIFY_MOTION_EVENT = 0b00010000,
APP_NOTIFY_IMU_EVENT = 0b00010000,
APP_NOTIFY_BATTERY_EVENT = 0b00100000,
APP_NOTIFY_DOWNLOAD_SEGGER_LOG = 0b01000000,
APP_NOTIFY_FIND_MY_TOTTAG_ACTIVATED = 0b10000000
Expand Down

0 comments on commit b2540cb

Please sign in to comment.