Skip to content

Commit

Permalink
Ensure that no storage items are pushed from an ISR
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgecrw committed Feb 2, 2024
1 parent ec23f84 commit 758ab1d
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 41 deletions.
2 changes: 1 addition & 1 deletion software/firmware/.settings/language.settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
<provider copy-of="extension" id="org.eclipse.cdt.managedbuilder.core.GCCBuildCommandParser"/>
<provider class="org.eclipse.cdt.managedbuilder.language.settings.providers.GCCBuiltinSpecsDetector" console="false" env-hash="-1085615316169670327" id="org.eclipse.embedcdt.managedbuild.cross.arm.core.GCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT Arm Cross GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} ${cross_toolchain_flags} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
<provider class="org.eclipse.cdt.managedbuilder.language.settings.providers.GCCBuiltinSpecsDetector" console="false" env-hash="-339510081203349785" id="org.eclipse.embedcdt.managedbuild.cross.arm.core.GCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT Arm Cross GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} ${cross_toolchain_flags} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
<language-scope id="org.eclipse.cdt.core.gcc"/>
<language-scope id="org.eclipse.cdt.core.g++"/>
</provider>
Expand Down
2 changes: 1 addition & 1 deletion software/firmware/src/boards/FreeRTOSConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ extern void vAssertCalled(const char * const pcFileName, unsigned long ulLine);
#define INCLUDE_vTaskPrioritySet 0
#define INCLUDE_uxTaskPriorityGet 0
#define INCLUDE_vTaskDelete 0
#define INCLUDE_vTaskSuspend 1
#define INCLUDE_vTaskSuspend 0
#define INCLUDE_xResumeFromISR 0
#define INCLUDE_vTaskDelayUntil 0
#define INCLUDE_vTaskDelay 1
Expand Down
1 change: 0 additions & 1 deletion software/firmware/src/tasks/app_task_maintenance.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ static void handle_notification(app_notification_t notification)
static void battery_event_handler(battery_event_t battery_event)
{
// Notify the app of a change in the plugged-in status of the device
print("INFO: Battery event occurred: %d\n", (uint32_t)battery_event);
if ((battery_event == BATTERY_PLUGGED) || (battery_event == BATTERY_UNPLUGGED))
{
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
Expand Down
12 changes: 6 additions & 6 deletions software/firmware/src/tasks/app_task_ranging.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ 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) != 0)
storage_write_motion_status(imu_read_in_motion());
if (((notification & APP_NOTIFY_NETWORK_LOST) != 0) || ((notification & APP_NOTIFY_NETWORK_CONNECTED) != 0) ||
((notification & APP_NOTIFY_VERIFY_CONFIGURATION) != 0))
verify_app_configuration();
Expand Down Expand Up @@ -162,17 +164,15 @@ static void handle_notification(app_notification_t notification)

static void battery_event_handler(battery_event_t battery_event)
{
// Store the battery event to non-volatile memory and notify the app
storage_write_charging_event(battery_event);
print("INFO: Battery event occurred: %d\n", (uint32_t)battery_event);
// Notify the app of a change in the plugged-in status of the device
if ((battery_event == BATTERY_PLUGGED) || (battery_event == BATTERY_UNPLUGGED))
app_notify(APP_NOTIFY_BATTERY_EVENT, true);
}

static void motion_change_handler(bool in_motion)
static void motion_change_handler(bool)
{
// Store the motion change to non-volatile memory
storage_write_motion_status(in_motion);
// Notify the app about a change in motion
app_notify(APP_NOTIFY_MOTION_EVENT, true);
}

static void ble_discovery_handler(const uint8_t ble_address[EUI_LEN], uint8_t ranging_role)
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,6 +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_BATTERY_EVENT = 0b00100000,
APP_NOTIFY_FIND_MY_TOTTAG_ACTIVATED = 0b10000000
} app_notification_t;
Expand All @@ -41,7 +42,6 @@ bool ranging_active(void);
// Storage Task Public Functions
void storage_flush_and_shutdown(void);
void storage_write_battery_level(uint32_t battery_voltage_mV);
void storage_write_charging_event(battery_event_t battery_event);
void storage_write_motion_status(bool in_motion);
void storage_write_ranging_data(uint32_t timestamp, const uint8_t *ranging_data, uint32_t ranging_data_len);

Expand Down
38 changes: 7 additions & 31 deletions software/firmware/src/tasks/storage_task.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@ typedef enum {
STORAGE_TYPE_RANGES
} storage_data_type_t;

typedef struct storage_item_t { uint32_t timestamp, value, type; } storage_item_t;
typedef struct storage_item_t { uint32_t timestamp, value; uint8_t type; } storage_item_t;
typedef struct ranging_data_t { uint8_t data[MAX_COMPRESSED_RANGE_DATA_LENGTH]; uint32_t length; } ranging_data_t;


// Static Global Variables ---------------------------------------------------------------------------------------------

static ranging_data_t range_data[STORAGE_QUEUE_MAX_NUM_ITEMS];
static uint8_t ucQueueStorage[STORAGE_QUEUE_MAX_NUM_ITEMS * sizeof(storage_item_t)];
static StaticQueue_t xQueueBuffer;
static QueueHandle_t storage_queue;
static ranging_data_t range_data[STORAGE_QUEUE_MAX_NUM_ITEMS];
static uint32_t range_data_index;


// Private Helper Functions --------------------------------------------------------------------------------------------
Expand All @@ -42,15 +41,6 @@ static void store_battery_voltage(uint32_t timestamp, uint32_t battery_voltage_m
storage_flush(false);
}

static void store_charging_event(uint32_t timestamp, uint8_t event_code)
{
const uint8_t storage_type = STORAGE_TYPE_CHARGING_EVENT;
storage_store(&storage_type, sizeof(storage_type));
storage_store(&timestamp, sizeof(timestamp));
storage_store(&event_code, sizeof(event_code));
storage_flush(false);
}

static void store_motion_change(uint32_t timestamp, bool in_motion)
{
const uint8_t storage_type = STORAGE_TYPE_MOTION;
Expand All @@ -75,45 +65,35 @@ static void store_ranges(uint32_t timestamp, const uint8_t *range_data, uint32_t
void storage_flush_and_shutdown(void)
{
const storage_item_t storage_item = { .timestamp = rtc_get_timestamp(), .value = 0, .type = STORAGE_TYPE_SHUTDOWN };
xQueueSendToBack(storage_queue, &storage_item, portMAX_DELAY);
xQueueSendToBack(storage_queue, &storage_item, 0);
}

void storage_write_battery_level(uint32_t battery_voltage_mV)
{
const storage_item_t storage_item = { .timestamp = rtc_get_timestamp(), .value = battery_voltage_mV, .type = STORAGE_TYPE_VOLTAGE };
xQueueSendToBack(storage_queue, &storage_item, portMAX_DELAY);
}

void storage_write_charging_event(battery_event_t battery_event)
{
const storage_item_t storage_item = { .timestamp = rtc_get_timestamp(), .value = battery_event, .type = STORAGE_TYPE_CHARGING_EVENT };
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
xQueueSendToBackFromISR(storage_queue, &storage_item, &xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
xQueueSendToBack(storage_queue, &storage_item, 0);
}

void storage_write_motion_status(bool in_motion)
{
const storage_item_t storage_item = { .timestamp = rtc_get_timestamp(), .value = in_motion, .type = STORAGE_TYPE_MOTION };
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
xQueueSendToBackFromISR(storage_queue, &storage_item, &xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
xQueueSendToBack(storage_queue, &storage_item, 0);
}

void storage_write_ranging_data(uint32_t timestamp, const uint8_t *ranging_data, uint32_t ranging_data_len)
{
static uint32_t range_data_index = 0;
const storage_item_t storage_item = { .timestamp = timestamp, .value = range_data_index, .type = STORAGE_TYPE_RANGES };
memcpy(range_data[range_data_index].data, ranging_data, ranging_data_len);
range_data[range_data_index].length = ranging_data_len;
range_data_index = (range_data_index + 1) % STORAGE_QUEUE_MAX_NUM_ITEMS;
xQueueSendToBack(storage_queue, &storage_item, portMAX_DELAY);
xQueueSendToBack(storage_queue, &storage_item, 0);
}

#else

void storage_flush_and_shutdown(void) {}
void storage_write_battery_level(uint32_t battery_voltage_mV) {}
void storage_write_charging_event(battery_event_t battery_event) {}
void storage_write_motion_status(bool in_motion) {}
void storage_write_ranging_data(uint32_t timestamp, const uint8_t *ranging_data, uint32_t ranging_data_len) {}

Expand All @@ -122,7 +102,6 @@ void storage_write_ranging_data(uint32_t timestamp, const uint8_t *ranging_data,
void StorageTask(void *params)
{
// Create a queue to hold pending storage items
range_data_index = 0;
static storage_item_t item;
storage_queue = xQueueCreateStatic(STORAGE_QUEUE_MAX_NUM_ITEMS, sizeof(storage_item_t), ucQueueStorage, &xQueueBuffer);

Expand All @@ -148,9 +127,6 @@ void StorageTask(void *params)
case STORAGE_TYPE_VOLTAGE:
store_battery_voltage(item.timestamp, item.value);
break;
case STORAGE_TYPE_CHARGING_EVENT:
store_charging_event(item.timestamp, (uint8_t)item.value);
break;
case STORAGE_TYPE_MOTION:
store_motion_change(item.timestamp, item.value);
break;
Expand Down

0 comments on commit 758ab1d

Please sign in to comment.