Skip to content

Commit

Permalink
Force FreeRTOS to use exclusively static memory
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgecrw committed Nov 15, 2023
1 parent bad3802 commit 0e63dbb
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ wsfOs_t wsfOs;
#include "FreeRTOS.h"
#include "event_groups.h"
EventGroupHandle_t xRadioTaskEventObject = NULL;
StaticEventGroup_t xEventGroupBuffer;

/*************************************************************************************************/
/*!
Expand Down Expand Up @@ -341,7 +342,7 @@ void WsfOsInit(void)

if( xRadioTaskEventObject == NULL)
{
xRadioTaskEventObject = xEventGroupCreate();
xRadioTaskEventObject = xEventGroupCreateStatic(&xEventGroupBuffer);

WSF_ASSERT(xRadioTaskEventObject != NULL);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

wsfQueue_t wsfTimerTimerQueue; /*!< Timer queue */
TimerHandle_t xWsfTimer;
StaticTimer_t xTimerBuffer;
static uint32_t g_ui32LastTime = 0;

/*************************************************************************************************/
Expand Down Expand Up @@ -156,8 +157,8 @@ void WsfTimerInit(void)

if(xWsfTimer == NULL)
{
xWsfTimer = xTimerCreate("WSF Timer", pdMS_TO_TICKS(WSF_MS_PER_TICK),
pdFALSE, NULL, WsfTimer_handler);
xWsfTimer = xTimerCreateStatic("WSF Timer", pdMS_TO_TICKS(WSF_MS_PER_TICK),
pdFALSE, NULL, WsfTimer_handler, &xTimerBuffer);
configASSERT(xWsfTimer);
g_ui32LastTime = xTaskGetTickCount();
}
Expand Down
2 changes: 0 additions & 2 deletions software/firmware/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ VPATH += src/tasks/ranging
SRC =
SRC += am_devices_cooper.c
SRC += am_devices_led.c
SRC += am_resources.c
SRC += am_util_ble_cooper.c
SRC += am_util_delay.c
SRC += am_util_stdio.c
Expand Down Expand Up @@ -186,7 +185,6 @@ SRC += hci_evt.c
SRC += hci_main.c
SRC += hci_tr.c
SRC += hci_vs_cooper.c
SRC += heap_4.c
SRC += l2c_main.c
SRC += l2c_slave.c
SRC += list.c
Expand Down
8 changes: 5 additions & 3 deletions software/firmware/src/boards/FreeRTOSConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
#define configUSE_PREEMPTION 1
#define configUSE_PORT_OPTIMISED_TASK_SELECTION 1

#define configSUPPORT_STATIC_ALLOCATION 1
#define configSUPPORT_DYNAMIC_ALLOCATION 0

#define configCPU_CLOCK_HZ AM_HAL_CLKGEN_FREQ_MAX_HZ
#define configTICK_RATE_HZ 1000
#define configMAX_PRIORITIES 6
#define configMINIMAL_STACK_SIZE (256)
#define configTOTAL_HEAP_SIZE (16 * 1024)
#define configMINIMAL_STACK_SIZE 512
#define configMAX_TASK_NAME_LEN 16
#define configUSE_16_BIT_TICKS 0
#define configIDLE_SHOULD_YIELD 1
Expand Down Expand Up @@ -38,7 +40,7 @@
#define configUSE_TIMERS 1
#define configTIMER_TASK_PRIORITY 3
#define configTIMER_QUEUE_LENGTH 5
#define configTIMER_TASK_STACK_DEPTH configMINIMAL_STACK_SIZE
#define configTIMER_TASK_STACK_DEPTH 256

/* Interrupt nesting behaviour configuration. */
#define NVIC_configKERNEL_INTERRUPT_PRIORITY (0x7)
Expand Down
45 changes: 0 additions & 45 deletions software/firmware/src/boards/am_resources.c

This file was deleted.

18 changes: 18 additions & 0 deletions software/firmware/src/peripherals/src/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,24 @@ void vAssertCalled(const char * const pcFileName, unsigned long ulLine)
taskEXIT_CRITICAL();
}

void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize)
{
static StaticTask_t xIdleTaskTCB;
static StackType_t uxIdleTaskStack[256];
*ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
*ppxIdleTaskStackBuffer = uxIdleTaskStack;
*pulIdleTaskStackSize = 256;
}

void vApplicationGetTimerTaskMemory(StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize)
{
static StaticTask_t xTimerTaskTCB;
static StackType_t uxTimerTaskStack[configTIMER_TASK_STACK_DEPTH];
*ppxTimerTaskTCBBuffer = &xTimerTaskTCB;
*ppxTimerTaskStackBuffer = uxTimerTaskStack;
*pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
}


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

Expand Down
17 changes: 10 additions & 7 deletions software/firmware/src/tasks/app_tasks.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@

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

static TaskHandle_t app_task_handle, ble_task_handle, ranging_task_handle;
static TaskHandle_t storage_task_handle, time_aligned_task_handle;
static StaticTask_t app_task_tcb, ble_task_tcb, ranging_task_tcb;
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;


Expand Down Expand Up @@ -88,11 +91,11 @@ void run_tasks(void)

// Create tasks with the following priority order:
// IdleTask < TimeAlignedTask < AppTask < BLETask < RangingTask < StorageTask
configASSERT1(xTaskCreate(StorageTask, "StorageTask", 512, allow_ranging ? uid : NULL, 5, &storage_task_handle));
configASSERT1(xTaskCreate(RangingTask, "RangingTask", 512, allow_ranging ? uid : NULL, 4, &ranging_task_handle));
configASSERT1(xTaskCreate(BLETask, "BLETask", 1024, NULL, 3, &ble_task_handle));
configASSERT1(xTaskCreate(allow_ranging ? AppTaskRanging : AppTaskMaintenance, "AppTask", 512, uid, 2, &app_task_handle));
configASSERT1(xTaskCreate(TimeAlignedTask, "TimeAlignedTask", 512, allow_ranging ? &allow_ranging : NULL, 1, &time_aligned_task_handle));
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(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);

// Start the task scheduler
vTaskStartScheduler();
Expand Down
4 changes: 3 additions & 1 deletion software/firmware/src/tasks/storage_task.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ typedef struct ranging_data_t { uint8_t data[MAX_COMPRESSED_RANGE_DATA_LENGTH];

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

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;
Expand Down Expand Up @@ -110,7 +112,7 @@ void StorageTask(void *params)
// Create a queue to hold pending storage items
storage_item_t item;
range_data_index = 0;
storage_queue = xQueueCreate(STORAGE_QUEUE_MAX_NUM_ITEMS, sizeof(storage_item_t));
storage_queue = xQueueCreateStatic(STORAGE_QUEUE_MAX_NUM_ITEMS, sizeof(storage_item_t), ucQueueStorage, &xQueueBuffer);

// Set whether the storage peripheral should be in maintenance mode
if (params)
Expand Down
19 changes: 8 additions & 11 deletions software/firmware/src/tasks/time_aligned_task.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,16 @@
#include "battery.h"
#include "logging.h"
#include "rtc.h"
#include "storage.h"


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

void TimeAlignedTask(void *scheduled_experiment)
{
// Set up local variables
experiment_details_t experiment_details;
experiment_details_t *experiment_details = scheduled_experiment ? (experiment_details_t*)scheduled_experiment : NULL;
const TickType_t ticks_between_iterations = pdMS_TO_TICKS(BATTERY_CHECK_INTERVAL_S * 1000);
TickType_t last_wake_time = xTaskGetTickCount();
if (scheduled_experiment)
storage_retrieve_experiment_details(&experiment_details);

// Loop forever
while (true)
Expand All @@ -31,17 +28,17 @@ void TimeAlignedTask(void *scheduled_experiment)

// Determine if an active experiment has ended
bool experiment_ended = false;
if (scheduled_experiment)
if (experiment_details)
{
if (rtc_get_timestamp() > experiment_details.experiment_end_time)
if (rtc_get_timestamp() > experiment_details->experiment_end_time)
experiment_ended = true;
else if (experiment_details.use_daily_times)
else if (experiment_details->use_daily_times)
{
uint32_t time_of_day = rtc_get_time_of_day();
if (((experiment_details.daily_start_time < experiment_details.daily_end_time) &&
((time_of_day < experiment_details.daily_start_time) || (time_of_day > experiment_details.daily_end_time))) ||
((experiment_details.daily_start_time > experiment_details.daily_end_time) &&
((time_of_day < experiment_details.daily_start_time) && (time_of_day > experiment_details.daily_end_time))))
if (((experiment_details->daily_start_time < experiment_details->daily_end_time) &&
((time_of_day < experiment_details->daily_start_time) || (time_of_day > experiment_details->daily_end_time))) ||
((experiment_details->daily_start_time > experiment_details->daily_end_time) &&
((time_of_day < experiment_details->daily_start_time) && (time_of_day > experiment_details->daily_end_time))))
experiment_ended = true;
}
}
Expand Down
2 changes: 0 additions & 2 deletions software/firmware/tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ VPATH += ./tools
SRC =
SRC += am_devices_cooper.c
SRC += am_devices_led.c
SRC += am_resources.c
SRC += am_util_ble_cooper.c
SRC += am_util_delay.c
SRC += am_util_stdio.c
Expand Down Expand Up @@ -195,7 +194,6 @@ SRC += hci_evt.c
SRC += hci_main.c
SRC += hci_tr.c
SRC += hci_vs_cooper.c
SRC += heap_4.c
SRC += l2c_main.c
SRC += l2c_slave.c
SRC += list.c
Expand Down
10 changes: 6 additions & 4 deletions software/firmware/tests/tasks/test_ble_and_ranging.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
#include "rtc.h"
#include "system.h"

static TaskHandle_t app_task_handle, ble_task_handle, ranging_task_handle;
static StaticTask_t app_task_tcb, ble_task_tcb, ranging_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];

void storage_retrieve_experiment_details(experiment_details_t *details) { memset(details, 0, sizeof(*details)); };
void storage_begin_reading(void) {}
Expand Down Expand Up @@ -38,9 +40,9 @@ int main(void)

// Create tasks with the following priority order:
// IdleTask < AppTask < BLETask < RangingTask
configASSERT1(xTaskCreate(RangingTask, "RangingTask", 512, uid, 5, &ranging_task_handle));
configASSERT1(xTaskCreate(BLETask, "BLETask", 1024, NULL, 3, &ble_task_handle));
configASSERT1(xTaskCreate(AppTaskRanging, "AppTask", 512, uid, 2, &app_task_handle));
xTaskCreateStatic(RangingTask, "RangingTask", configMINIMAL_STACK_SIZE, uid, 4, ranging_task_stack, &ranging_task_tcb);
xTaskCreateStatic(BLETask, "BLETask", 2*configMINIMAL_STACK_SIZE, NULL, 3, ble_task_stack, &ble_task_tcb);
xTaskCreateStatic(AppTaskRanging, "AppTask", configMINIMAL_STACK_SIZE, uid, 2, app_task_stack, &app_task_tcb);

// Start the task scheduler
vTaskStartScheduler();
Expand Down
6 changes: 4 additions & 2 deletions software/firmware/tests/tasks/test_ranging_task.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

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

static StaticTask_t ranging_task_tcb, test_task_tcb;
static TaskHandle_t ranging_task_handle, test_task_handle;
static StackType_t ranging_task_stack[configMINIMAL_STACK_SIZE], test_task_stack[configMINIMAL_STACK_SIZE];
static uint8_t uid[EUI_LEN], test_state;
static volatile bool is_ranging;

Expand Down Expand Up @@ -110,8 +112,8 @@ int main(void)
ranging_radio_sleep(true);

// Create the ranging tasks and start the task scheduler
configASSERT1(xTaskCreate(RangeTask, "RangeTask", 512, uid, 5, &ranging_task_handle));
configASSERT1(xTaskCreate(TestTask, "TestTask", 512, NULL, 4, &test_task_handle));
ranging_task_handle = xTaskCreateStatic(RangeTask, "RangeTask", configMINIMAL_STACK_SIZE, uid, 5, ranging_task_stack, &ranging_task_tcb);
test_task_handle = xTaskCreateStatic(TestTask, "TestTask", configMINIMAL_STACK_SIZE, NULL, 4, test_task_stack, &test_task_tcb);
vTaskStartScheduler();
return 0;
}

0 comments on commit 0e63dbb

Please sign in to comment.