Skip to content

Commit 0016f49

Browse files
authored
Add firmware abstraction that simplifies periodic/event-driven task simulation (#7)
1 parent b05482a commit 0016f49

File tree

12 files changed

+204
-93
lines changed

12 files changed

+204
-93
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ add_subdirectory(sim)
1515
add_subdirectory(libs/freertos)
1616
add_subdirectory(libs/sutsim/c)
1717

18+
if (SUTSIM_ENABLE_TASK_CONFIG STREQUAL "ON")
19+
add_subdirectory(libs/task_config)
20+
endif()
21+
1822
foreach(FIRMWARE_DIR ${SUTSIM_FIRMWARE_DIRS})
1923
add_subdirectory(${FIRMWARE_DIR})
2024
endforeach()

examples/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ set(SUTSIM_FIRMWARE_DIRS
77
${CMAKE_CURRENT_SOURCE_DIR}/src
88
)
99
set(SUTSIM_FIRMWARE_FREERTOS_CONFIG_PATH ${CMAKE_CURRENT_SOURCE_DIR}/src)
10+
set(SUTSIM_ENABLE_TASK_CONFIG ON)
1011

11-
add_subdirectory(.. build)
12+
add_subdirectory(.. build)

examples/src/CMakeLists.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# Build the firmware as a shared library for dynamic loading
22

33
set(SOURCES
4-
firmware.c
5-
rtos_app_hooks.c
4+
sutsim_hooks.c
5+
app_task_config.c
6+
app_temperature.c
67
temperature_sensor.c
78
)
89

@@ -16,9 +17,10 @@ target_include_directories(firmware PRIVATE ${INCLUDE_DIRS})
1617
target_link_libraries(firmware
1718
freertos_with_port
1819
sutsim
20+
task_config
1921
)
2022

21-
# Set output directory for firmware shared libraries (to test/sim_artifacts)
23+
# Set output directory for firmware shared libraries
2224
set_target_properties(firmware PROPERTIES
2325
LIBRARY_OUTPUT_DIRECTORY ${SUTSIM_TEST_DIR}/sim_artifacts
2426
)

examples/src/app_task_config.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "task_config.h"
2+
3+
#include <stdint.h>
4+
#include <stdbool.h>
5+
#include <stddef.h>
6+
7+
extern void taskFunction_readTemperature_init_100Hz(void);
8+
extern void taskFunction_readTemperature_run_100Hz(void);
9+
extern void taskFunction_processTemperature_eventDriven(void);
10+
11+
static taskConfig_S task_config[] = {
12+
{
13+
.type = TASK_TYPE_PERIODIC,
14+
.period_ms = 10,
15+
.task_init_func = taskFunction_readTemperature_init_100Hz,
16+
.task_run_func = taskFunction_readTemperature_run_100Hz,
17+
},
18+
{
19+
.type = TASK_TYPE_EVENT_DRIVEN,
20+
.task_init_func = NULL,
21+
.task_run_func = taskFunction_processTemperature_eventDriven,
22+
}
23+
};
24+
25+
taskConfigList_S task_config_list = {
26+
.task_config = task_config,
27+
.task_count = sizeof(task_config) / sizeof(taskConfig_S),
28+
};

examples/src/app_temperature.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include "temperature_sensor.h"
2+
3+
#include "FreeRTOS.h"
4+
#include "queue.h"
5+
6+
#include <stdio.h>
7+
#include <stdint.h>
8+
9+
/* DEFINES */
10+
#define APP_TEMPERATURES_QUEUE_LENGTH 10
11+
#define APP_TEMPERATURES_QUEUE_ITEM_SIZE sizeof(float)
12+
13+
/* TYPEDEFS */
14+
typedef struct {
15+
QueueHandle_t temperature_queue;
16+
StaticQueue_t temperature_queueBuffer;
17+
uint8_t temperature_queueStorageArea[APP_TEMPERATURES_QUEUE_LENGTH * APP_TEMPERATURES_QUEUE_ITEM_SIZE];
18+
} app_temperatures_S;
19+
20+
/* PRIVATE VARIABLES */
21+
static app_temperatures_S app_temperatures = {0};
22+
23+
/* PUBLIC FUNCTIONS */
24+
// Init function to be called before tasks are created
25+
void app_temperature_init(void) {
26+
app_temperatures.temperature_queue = xQueueCreateStatic(APP_TEMPERATURES_QUEUE_LENGTH, APP_TEMPERATURES_QUEUE_ITEM_SIZE, app_temperatures.temperature_queueStorageArea, &app_temperatures.temperature_queueBuffer);
27+
}
28+
29+
// Temperature reading task (periodic)
30+
void taskFunction_readTemperature_init_100Hz(void) {
31+
(void)temperature_sensor_init();
32+
}
33+
34+
void taskFunction_readTemperature_run_100Hz(void) {
35+
float t = 0.0f;
36+
if (temperature_sensor_readCelsius(&t) == TEMPERATURE_SENSOR_SUCCESS) {
37+
(void)xQueueSend(app_temperatures.temperature_queue, &t, 0);
38+
}
39+
}
40+
41+
// Temperature processing task (event-driven)
42+
void taskFunction_processTemperature_eventDriven(void) {
43+
float t = 0.0f;
44+
45+
// Note: Non-zero block time does not work in sim
46+
if (xQueueReceive(app_temperatures.temperature_queue, &t, 0U) == pdTRUE) {
47+
printf("Firmware: Current temperature is %.2f°C\n", t);
48+
}
49+
}

examples/src/firmware.c

Lines changed: 0 additions & 31 deletions
This file was deleted.

examples/src/rtos_app_hooks.c

Lines changed: 0 additions & 53 deletions
This file was deleted.

examples/src/sutsim_hooks.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include "sutsim.h"
2+
#include "task_config.h"
3+
4+
#include <stdio.h>
5+
#include <stdint.h>
6+
#include <stdbool.h>
7+
8+
#include "FreeRTOS.h"
9+
#include "task.h"
10+
11+
// Hooks defined by application
12+
extern void app_temperature_init(void);
13+
14+
void sut_init_hook(void) {
15+
// Module initialization
16+
app_temperature_init();
17+
18+
// Task initialization
19+
task_executeAllInit();
20+
}
21+
22+
void sut_tick_hook(void) {
23+
// Assume tick rate is 1kHz
24+
static uint32_t tickCounter = 0;
25+
26+
task_executeAll(tickCounter);
27+
28+
tickCounter = (tickCounter + 1) % 1000;
29+
}
30+
31+
void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
32+
StackType_t ** ppxIdleTaskStackBuffer,
33+
configSTACK_DEPTH_TYPE * puxIdleTaskStackSize )
34+
{
35+
static StaticTask_t xIdleTaskTCB;
36+
static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];
37+
38+
*ppxIdleTaskTCBBuffer = &( xIdleTaskTCB );
39+
*ppxIdleTaskStackBuffer = &( uxIdleTaskStack[ 0 ] );
40+
*puxIdleTaskStackSize = configMINIMAL_STACK_SIZE;
41+
}

examples/test/test_temperature_sensor.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ def test_data_types():
99
# Initialize the simulator
1010
simulator.initSim("device", firmware_lib_path)
1111

12+
# Set the temperature sensor data and verify it was set correctly
1213
simulator.setSutDataFloat("device.temperature_sensor.temperature", 100.0)
13-
14-
for _ in range(20):
14+
simulator.getSutDataFloat("device.temperature_sensor.temperature") == 100.0
15+
16+
for _ in range(25):
1517
simulator.runTick()
16-
17-
# # Get the temperature
18-
assert simulator.getSutDataFloat("device.temperature_sensor.temperature") == 100.0
18+

libs/task_config/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
add_library(task_config STATIC
2+
task_config.c
3+
)
4+
5+
target_include_directories(task_config PUBLIC
6+
${CMAKE_CURRENT_SOURCE_DIR}
7+
)

0 commit comments

Comments
 (0)