Skip to content

Commit

Permalink
Store everything in proper places with proper lifetimes
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgecrw committed Nov 15, 2023
1 parent 0e63dbb commit 808a7af
Show file tree
Hide file tree
Showing 19 changed files with 69 additions and 88 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="-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">
<provider class="org.eclipse.cdt.managedbuilder.language.settings.providers.GCCBuiltinSpecsDetector" console="false" env-hash="1347002660722691700" 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
14 changes: 4 additions & 10 deletions software/firmware/src/peripherals/src/battery.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ static void signal_charge_complete(bool charge_complete)
static void plugged_in_status_changed(void *args)
{
// Retrieve the current plugged-in status of the charger
bool is_plugged_in = battery_monitor_is_plugged_in();
const bool is_plugged_in = battery_monitor_is_plugged_in();

// Toggle the interrupt direction (dual-edge interrupts not available due to errata)
am_hal_gpio_pincfg_t pin_config = AM_HAL_GPIO_PINCFG_INPUT;
Expand All @@ -46,7 +46,7 @@ static void plugged_in_status_changed(void *args)
static void charging_status_changed(void *args)
{
// Retrieve the current charging statuses of the charger
bool is_charging = battery_monitor_is_charging();
const bool is_charging = battery_monitor_is_charging();

// Toggle the interrupt direction (dual-edge interrupts not available due to errata)
am_hal_gpio_pincfg_t pin_config = AM_HAL_GPIO_PINCFG_INPUT;
Expand Down Expand Up @@ -224,18 +224,12 @@ bool battery_monitor_is_plugged_in(void)
{
// Return the current plugged-in status of the battery
static uint32_t status;
if (am_hal_gpio_state_read(PIN_BATTERY_INPUT_POWER_GOOD, AM_HAL_GPIO_INPUT_READ, &status) == AM_HAL_STATUS_SUCCESS)
return (status == 0);
else
return false;
return (am_hal_gpio_state_read(PIN_BATTERY_INPUT_POWER_GOOD, AM_HAL_GPIO_INPUT_READ, &status) == AM_HAL_STATUS_SUCCESS) && !status;
}

bool battery_monitor_is_charging(void)
{
// Return the current charging status of the battery
static uint32_t status;
if (am_hal_gpio_state_read(PIN_BATTERY_CHARGING_STATUS, AM_HAL_GPIO_INPUT_READ, &status) == AM_HAL_STATUS_SUCCESS)
return (status == 0);
else
return false;
return (am_hal_gpio_state_read(PIN_BATTERY_CHARGING_STATUS, AM_HAL_GPIO_INPUT_READ, &status) == AM_HAL_STATUS_SUCCESS) && !status;
}
25 changes: 11 additions & 14 deletions software/firmware/src/peripherals/src/buzzer.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,10 @@

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

static const uint16_t unplugged_frequencies[] = { 1760, 1, 1320, 1, 1110, 1, 880, 0 };
static const uint16_t unplugged_durations[] = { 100, 10, 100, 10, 100, 10, 100 };
static const uint16_t plugged_frequencies[] = { 880, 1, 1110, 1, 1320, 1, 1760, 0 };
static const uint16_t plugged_durations[] = { 100, 10, 100, 10, 100, 10, 100 };
static const uint16_t invalid_rtc_frequencies[] = { 760, 1, 760, 0 };
static const uint16_t invalid_rtc_durations[] = { 100, 100, 400 };
static const uint16_t error_frequencies[] = { 880, 1, 587, 0 };
static const uint16_t error_durations[] = { 300, 100, 500 };
static const uint16_t locator_frequencies[] = { 1047, 1, 1047, 830, 1, 880, 698, 0 };
static const uint16_t locator_durations[] = { 100, 100, 100, 100, 100, 100, 200 };

static am_hal_timer_config_t timer_config;
static const uint32_t buzzer_clock_hz = AM_HAL_CLKGEN_FREQ_MAX_HZ / 256;
static volatile const uint16_t *current_frequency, *current_duration;
static volatile uint32_t interrupt_counter_index, interrupt_counter_max;
static uint32_t buzzer_clock_hz;


// Private Helper Functions --------------------------------------------------------------------------------------------

Expand Down Expand Up @@ -97,7 +85,6 @@ void am_timer00_isr(void)
void buzzer_init(void)
{
// Initialize static variables
buzzer_clock_hz = AM_HAL_CLKGEN_FREQ_MAX_HZ / 256;
current_frequency = NULL;
current_duration = NULL;

Expand Down Expand Up @@ -135,6 +122,8 @@ void buzzer_deinit(void)

void buzzer_indicate_plugged_in(void)
{
static const uint16_t plugged_frequencies[] = { 880, 1, 1110, 1, 1320, 1, 1760, 0 };
static const uint16_t plugged_durations[] = { 100, 10, 100, 10, 100, 10, 100 };
if (!current_frequency)
{
current_frequency = plugged_frequencies;
Expand All @@ -145,6 +134,8 @@ void buzzer_indicate_plugged_in(void)

void buzzer_indicate_unplugged(void)
{
static const uint16_t unplugged_frequencies[] = { 1760, 1, 1320, 1, 1110, 1, 880, 0 };
static const uint16_t unplugged_durations[] = { 100, 10, 100, 10, 100, 10, 100 };
if (!current_frequency)
{
current_frequency = unplugged_frequencies;
Expand All @@ -155,6 +146,8 @@ void buzzer_indicate_unplugged(void)

void buzzer_indicate_invalid_rtc_time(void)
{
static const uint16_t invalid_rtc_frequencies[] = { 760, 1, 760, 0 };
static const uint16_t invalid_rtc_durations[] = { 100, 100, 400 };
if (!current_frequency)
{
current_frequency = invalid_rtc_frequencies;
Expand All @@ -165,6 +158,8 @@ void buzzer_indicate_invalid_rtc_time(void)

void buzzer_indicate_error(void)
{
static const uint16_t error_frequencies[] = { 880, 1, 587, 0 };
static const uint16_t error_durations[] = { 300, 100, 500 };
if (!current_frequency)
{
current_frequency = error_frequencies;
Expand All @@ -175,6 +170,8 @@ void buzzer_indicate_error(void)

void buzzer_indicate_location(void)
{
static const uint16_t locator_frequencies[] = { 1047, 1, 1047, 830, 1, 880, 698, 0 };
static const uint16_t locator_durations[] = { 100, 100, 100, 100, 100, 100, 200 };
if (!current_frequency)
{
current_frequency = locator_frequencies;
Expand Down
16 changes: 8 additions & 8 deletions software/firmware/src/peripherals/src/ranging.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ static void wakeup_device_with_io(void)
static void dwt_xfer3000(const uint32_t regFileID, const uint16_t indx, const uint16_t length, uint8_t *buffer, const spi_modes_e mode)
{
// Set up local variables
uint8_t header[2]; // Buffer to compose header in
uint16_t cnt = 1; // Counter for length of a header
uint16_t reg_file = 0x1F & ((regFileID + indx) >> 16), reg_offset = 0x7F & (regFileID + indx);
uint8_t header[2];
uint16_t cnt = 1;
const uint16_t reg_file = 0x1F & ((regFileID + indx) >> 16), reg_offset = 0x7F & (regFileID + indx);

// Write message header selecting WRITE operation and addresses as appropriate
uint16_t addr = (reg_file << 9) | (reg_offset << 2);
const uint16_t addr = (reg_file << 9) | (reg_offset << 2);
header[0] = (uint8_t)((mode | addr) >> 8); // bit7 + addr[4:0] + sub_addr[6:6]
header[1] = (uint8_t)(addr | (mode & 0x03)); // EAM: subaddr[5:0] + R/W/AND_OR

Expand All @@ -146,12 +146,12 @@ static void dwt_xfer3000(const uint32_t regFileID, const uint16_t indx, const ui

static uint32_t dwt_read32bitoffsetreg(int regFileID, int regOffset)
{
uint8_t buffer[4];
uint32_t regval = 0;
dwt_xfer3000(regFileID ,regOffset ,4, buffer, DW3000_SPI_RD_BIT);
static uint8_t buffer[4];
dwt_xfer3000(regFileID, regOffset, 4, buffer, DW3000_SPI_RD_BIT);
for (int j = 3; j >= 0; --j)
regval = (regval << 8) + buffer[j];
return (regval);
return regval;
}

static void dwt_write32bitoffsetreg(int regFileID, int regOffset, uint32_t regval)
Expand All @@ -168,7 +168,7 @@ static void dwt_write16bitoffsetreg(int regFileID, int regOffset, uint16_t regva

static uint8_t dwt_read8bitoffsetreg(int regFileID, int regOffset)
{
uint8_t regval;
static uint8_t regval;
dwt_xfer3000(regFileID, regOffset, 1, &regval, DW3000_SPI_RD_BIT);
return regval;
}
Expand Down
4 changes: 2 additions & 2 deletions software/firmware/src/peripherals/src/storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ typedef struct __attribute__ ((__packed__)) { uint16_t lba, pba; } bbm_lut_t;
static void *spi_handle;
static bbm_lut_t bad_block_lookup_table_internal[BBM_INTERNAL_LUT_NUM_ENTRIES];
static uint8_t cache[2 * MEMORY_PAGE_SIZE_BYTES], transfer_buffer[MEMORY_PAGE_SIZE_BYTES];
static uint32_t starting_page, current_page, reading_page, cache_index;
static bool is_reading, in_maintenance_mode, disabled;
static volatile uint32_t starting_page, current_page, reading_page, cache_index;
static volatile bool is_reading, in_maintenance_mode, disabled;


// Private Helper Functions --------------------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion software/firmware/src/tasks/app_task_maintenance.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ void app_maintenance_activate_find_my_tottag(uint32_t seconds_to_activate)
void AppTaskMaintenance(void *uid)
{
// Store the application task handle
static uint32_t notification_bits = 0;
app_task_handle = xTaskGetCurrentTaskHandle();
uint32_t notification_bits = 0;

// Register handler for battery status changes and verify correct mode of operation
battery_register_event_callback(battery_event_handler);
Expand Down
14 changes: 6 additions & 8 deletions software/firmware/src/tasks/app_task_ranging.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// Static Global Variables ---------------------------------------------------------------------------------------------

static uint8_t device_uid_short;
static TaskHandle_t app_task_handle = 0;
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;
Expand Down Expand Up @@ -49,6 +49,9 @@ static void handle_notification(app_notification_t notification)
verify_app_configuration();
if ((notification & APP_NOTIFY_NETWORK_FOUND) != 0)
{
// Stop scanning for additional devices
bluetooth_stop_scanning(true);

// Determine if an actively ranging device was located
bool ranging_device_located = false, idle_device_located = false;
for (uint8_t i = 0; !ranging_device_located && (i < num_discovered_devices); ++i)
Expand Down Expand Up @@ -80,8 +83,6 @@ static void handle_notification(app_notification_t notification)
}

// Reset the devices-found flag and verify the app configuration
while (bluetooth_is_scanning())
vTaskDelay(pdMS_TO_TICKS(1));
devices_found = false;
verify_app_configuration();
}
Expand Down Expand Up @@ -134,9 +135,6 @@ static void ble_discovery_handler(const uint8_t ble_address[EUI_LEN], uint8_t ra

void am_timer04_isr(void)
{
// Immediately stop scanning for additional devices
bluetooth_stop_scanning(false);

// Notify the main task to handle the interrupt
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
am_hal_timer_interrupt_clear(AM_HAL_TIMER_MASK(BLE_SCANNING_TIMER_NUMBER, AM_HAL_TIMER_COMPARE_BOTH));
Expand Down Expand Up @@ -180,9 +178,9 @@ void app_activate_find_my_tottag(uint32_t seconds_to_activate)
void AppTaskRanging(void *uid)
{
// Store the UID and application task handle
static uint32_t notification_bits = 0;
device_uid_short = ((uint8_t*)uid)[0];
app_task_handle = xTaskGetCurrentTaskHandle();
uint32_t notification_bits = 0;

// Initialize the BLE scanning window timer
am_hal_timer_config_t scanning_timer_config;
Expand All @@ -203,7 +201,7 @@ void AppTaskRanging(void *uid)
#endif

// Retrieve current experiment details from non-volatile storage
experiment_details_t current_experiment;
static experiment_details_t current_experiment;
storage_retrieve_experiment_details(&current_experiment);

// Wait until the BLE stack has been fully initialized
Expand Down
4 changes: 2 additions & 2 deletions software/firmware/src/tasks/app_tasks.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ static StaticTask_t storage_task_tcb, time_aligned_task_tcb;
static StackType_t app_task_stack[configMINIMAL_STACK_SIZE], ble_task_stack[2*configMINIMAL_STACK_SIZE];
static StackType_t ranging_task_stack[configMINIMAL_STACK_SIZE], storage_task_stack[configMINIMAL_STACK_SIZE];
static StackType_t time_aligned_task_stack[configMINIMAL_STACK_SIZE];
static experiment_details_t scheduled_experiment;


// Public API Functions ------------------------------------------------------------------------------------------------
Expand All @@ -45,6 +44,7 @@ void run_tasks(void)
ranging_radio_sleep(true);

// Determine whether there is an active experiment taking place
static experiment_details_t scheduled_experiment;
storage_retrieve_experiment_details(&scheduled_experiment);
uint32_t timestamp = rtc_get_timestamp(), time_of_day = rtc_get_time_of_day();
bool valid_experiment = rtc_is_valid() && scheduled_experiment.num_devices;
Expand Down Expand Up @@ -93,7 +93,7 @@ void run_tasks(void)
// IdleTask < TimeAlignedTask < AppTask < BLETask < RangingTask < StorageTask
xTaskCreateStatic(StorageTask, "StorageTask", configMINIMAL_STACK_SIZE, allow_ranging ? uid : NULL, 5, storage_task_stack, &storage_task_tcb);
xTaskCreateStatic(RangingTask, "RangingTask", configMINIMAL_STACK_SIZE, allow_ranging ? uid : NULL, 4, ranging_task_stack, &ranging_task_tcb);
xTaskCreateStatic(BLETask, "BLETask", 2*configMINIMAL_STACK_SIZE, NULL, 3, ble_task_stack, &ble_task_tcb);
xTaskCreateStatic(BLETask, "BLETask", 2 * configMINIMAL_STACK_SIZE, NULL, 3, ble_task_stack, &ble_task_tcb);
xTaskCreateStatic(allow_ranging ? AppTaskRanging : AppTaskMaintenance, "AppTask", configMINIMAL_STACK_SIZE, uid, 2, app_task_stack, &app_task_tcb);
xTaskCreateStatic(TimeAlignedTask, "TimeAlignedTask", configMINIMAL_STACK_SIZE, allow_ranging ? &scheduled_experiment : NULL, 1, time_aligned_task_stack, &time_aligned_task_tcb);

Expand Down
2 changes: 1 addition & 1 deletion software/firmware/src/tasks/ble_task.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ static void ble_stack_init(void)
WsfTimerInit();

// Initialize a buffer pool for WSF dynamic memory needs
uint16_t wsfBufMemLen = WsfBufInit(sizeof(g_pui32BufMem), (uint8_t*)g_pui32BufMem, WSF_BUF_POOLS, g_psPoolDescriptors);
const uint16_t wsfBufMemLen = WsfBufInit(sizeof(g_pui32BufMem), (uint8_t*)g_pui32BufMem, WSF_BUF_POOLS, g_psPoolDescriptors);
if (wsfBufMemLen > sizeof(g_pui32BufMem))
print("ERROR: Memory pool is too small by %d\n", wsfBufMemLen - sizeof(g_pui32BufMem));

Expand Down
4 changes: 1 addition & 3 deletions software/firmware/src/tasks/bluetooth/device_info_service.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,12 @@ static const attsAttr_t deviceInfoList[] =
}
};

static attsGroup_t deviceInfoGroup;
static attsGroup_t deviceInfoGroup = { 0, (attsAttr_t*)deviceInfoList, 0, 0, DEVICE_INFO_SERVICE_HANDLE, DEVICE_INFO_MAX_HANDLE-1 };


// Public API ----------------------------------------------------------------------------------------------------------

void deviceInfoAddGroup(void)
{
memset(deviceInfoSysId, 0, sizeof(deviceInfoSysId));
deviceInfoGroup = (attsGroup_t){ 0, (attsAttr_t*)deviceInfoList, 0, 0, DEVICE_INFO_SERVICE_HANDLE, DEVICE_INFO_MAX_HANDLE-1 };
AttsAddGroup(&deviceInfoGroup);
}
12 changes: 4 additions & 8 deletions software/firmware/src/tasks/bluetooth/gap_gatt_service.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ static const attsAttr_t gapList[] =
},
};

static attsGroup_t gapGroup;
static attsGroup_t gapGroup = { 0, (attsAttr_t*)gapList, 0, 0, GAP_SERVICE_HANDLE, GAP_MAX_HANDLE-1 };


// GATT Services and Characteristics -----------------------------------------------------------------------------------
Expand Down Expand Up @@ -174,7 +174,7 @@ static const attsAttr_t gattList[] =
}
};

static attsGroup_t gattGroup;
static attsGroup_t gattGroup = { 0, (attsAttr_t*)gattList, NULL, NULL, GATT_SERVICE_HANDLE, GATT_MAX_HANDLE-1 };

void SvcCoreGapCentAddrResUpdate(bool_t value) { gapCentralAddrRes[0] = value; }

Expand All @@ -189,10 +189,6 @@ void gapGattAddGroup(void)

void gapGattRegisterCallbacks(attsReadCback_t readCallback, attsWriteCback_t writeCallback)
{
memset(gapCentralAddrRes, 0, sizeof(gapCentralAddrRes));
memset(gattServiceChangedCcc, 0, sizeof(gattServiceChangedCcc));
memset(gattCsf, 0, sizeof(gattCsf));
memset(gattDbHash, 0, sizeof(gattDbHash));
gapGroup = (attsGroup_t){ 0, (attsAttr_t*)gapList, 0, 0, GAP_SERVICE_HANDLE, GAP_MAX_HANDLE-1 };
gattGroup = (attsGroup_t){ 0, (attsAttr_t*)gattList, readCallback, writeCallback, GATT_SERVICE_HANDLE, GATT_MAX_HANDLE-1 };
gattGroup.readCback = readCallback;
gattGroup.writeCback = writeCallback;
}
7 changes: 3 additions & 4 deletions software/firmware/src/tasks/bluetooth/live_stats_service.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ static const attsAttr_t liveStatsList[] =
}
};

static attsGroup_t liveStatsGroup;
static attsGroup_t liveStatsGroup = { 0, (attsAttr_t*)liveStatsList, NULL, NULL, LIVE_STATS_SERVICE_HANDLE, LIVE_STATS_MAX_HANDLE-1 };


// Public API ----------------------------------------------------------------------------------------------------------
Expand All @@ -170,7 +170,6 @@ void liveStatsAddGroup(void)

void liveStatsRegisterCallbacks(attsReadCback_t readCallback, attsWriteCback_t writeCallback)
{
timestamp = findMyTottagDuration = batteryLevel = 0;
memset(ranges, 0, sizeof(ranges));
liveStatsGroup = (attsGroup_t){ 0, (attsAttr_t*)liveStatsList, readCallback, writeCallback, LIVE_STATS_SERVICE_HANDLE, LIVE_STATS_MAX_HANDLE-1 };
liveStatsGroup.readCback = readCallback;
liveStatsGroup.writeCback = writeCallback;
}
9 changes: 3 additions & 6 deletions software/firmware/src/tasks/bluetooth/maintenance_service.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ static const attsAttr_t maintenanceList[] =
}
};

static attsGroup_t maintenanceGroup;
static attsGroup_t maintenanceGroup = { 0, (attsAttr_t*)maintenanceList, NULL, NULL, MAINTENANCE_SERVICE_HANDLE, MAINTENANCE_MAX_HANDLE-1 };


// Public API ----------------------------------------------------------------------------------------------------------
Expand All @@ -139,9 +139,6 @@ void deviceMaintenanceAddGroup(void)

void deviceMaintenanceRegisterCallbacks(attsReadCback_t readCallback, attsWriteCback_t writeCallback)
{
memset(&experimentDetails, 0, sizeof(experimentDetails));
memset(maintenanceCommand, 0, sizeof(maintenanceCommand));
memset(maintenanceResult, 0, sizeof(maintenanceResult));
memset(maintenanceResultCcc, 0, sizeof(maintenanceResultCcc));
maintenanceGroup = (attsGroup_t){ 0, (attsAttr_t*)maintenanceList, readCallback, writeCallback, MAINTENANCE_SERVICE_HANDLE, MAINTENANCE_MAX_HANDLE-1 };
maintenanceGroup.readCback = readCallback;
maintenanceGroup.writeCback = writeCallback;
}
Loading

0 comments on commit 808a7af

Please sign in to comment.