Skip to content

Commit 11368da

Browse files
committed
[freertos] Add better configuration management
1 parent ad2ef4b commit 11368da

File tree

5 files changed

+218
-121
lines changed

5 files changed

+218
-121
lines changed

ext/aws/FreeRTOSConfig.h.in

Lines changed: 129 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -35,119 +35,157 @@ your application. */
3535
#include <modm/architecture/utils.hpp>
3636
#include <modm/platform/device.hpp>
3737

38+
/* Define to trap errors during development. */
39+
#define configASSERT( x ) { modm_assert((x), "freertos", __FILE__, MODM_STRINGIFY(__LINE__)); }
40+
3841
// declared in modm:platform:clock
3942
#ifdef __cplusplus
4043
extern "C" uint32_t SystemCoreClock;
4144
#else
4245
extern uint32_t SystemCoreClock;
4346
#endif
4447

45-
#define configUSE_PREEMPTION 1
46-
#define configUSE_PORT_OPTIMISED_TASK_SELECTION {{ ("m0" not in core) | int }}
47-
#define configUSE_TICKLESS_IDLE 0
4848
#define configCPU_CLOCK_HZ ( SystemCoreClock )
49-
#define configTICK_RATE_HZ ( {{ options["::frequency"] }} )
50-
#define configMAX_PRIORITIES 5
51-
#define configMINIMAL_STACK_SIZE 128
52-
#define configMAX_TASK_NAME_LEN 16
53-
#define configUSE_16_BIT_TICKS 0
54-
#define configIDLE_SHOULD_YIELD 1
55-
#define configUSE_TASK_NOTIFICATIONS 1
56-
#define configUSE_MUTEXES 1
57-
#define configUSE_RECURSIVE_MUTEXES 1
58-
#define configUSE_COUNTING_SEMAPHORES 1
59-
#define configQUEUE_REGISTRY_SIZE 10
60-
#define configUSE_QUEUE_SETS 0
61-
#define configUSE_TIME_SLICING 0
62-
#define configUSE_NEWLIB_REENTRANT 0
63-
#define configENABLE_BACKWARD_COMPATIBILITY 0
64-
#define configNUM_THREAD_LOCAL_STORAGE_POINTERS 5
65-
66-
49+
#define configTICK_RATE_HZ ( {{ frequency }} )
6750
/* Memory allocation related definitions. */
6851
#define configSUPPORT_STATIC_ALLOCATION 0
6952
#define configSUPPORT_DYNAMIC_ALLOCATION 1
70-
#define configTOTAL_HEAP_SIZE 0 // modm provides heap
71-
#define configAPPLICATION_ALLOCATED_HEAP 1
72-
73-
/* Hook function related definitions. */
74-
#define configUSE_IDLE_HOOK 0
7553
// used by modm:platform:clock for modm::Clock::increment(): vApplicationTickHook()
7654
#define configUSE_TICK_HOOK 1
77-
#define configCHECK_FOR_STACK_OVERFLOW 1
78-
#define configUSE_MALLOC_FAILED_HOOK 0 // modm provides heap assertions
79-
#define configUSE_DAEMON_TASK_STARTUP_HOOK 0
80-
81-
/* Run time and task stats gathering related definitions. */
82-
#define configGENERATE_RUN_TIME_STATS 0
83-
#define configUSE_TRACE_FACILITY 0
84-
#define configUSE_STATS_FORMATTING_FUNCTIONS 0
85-
86-
/* Co-routine related definitions. */
87-
#define configUSE_CO_ROUTINES 0
88-
#define configMAX_CO_ROUTINE_PRIORITIES 1
89-
90-
/* Software timer related definitions. */
91-
#define configUSE_TIMERS 1
92-
#define configTIMER_TASK_PRIORITY 3
93-
#define configTIMER_QUEUE_LENGTH 10
94-
#define configTIMER_TASK_STACK_DEPTH configMINIMAL_STACK_SIZE
95-
96-
/* Define to trap errors during development. */
97-
#define configASSERT( x ) { modm_assert((x), "freertos.assert", __FILE__, MODM_STRINGIFY(__LINE__)); }
98-
99-
/* FreeRTOS MPU specific definitions. */
100-
#define configINCLUDE_APPLICATION_DEFINED_PRIVILEGED_FUNCTIONS 0
101-
102-
/* Optional functions - most linkers will remove unused functions anyway. */
103-
#define INCLUDE_vTaskPrioritySet 1
104-
#define INCLUDE_uxTaskPriorityGet 1
105-
#define INCLUDE_vTaskDelete 1
106-
#define INCLUDE_vTaskSuspend 1
107-
#define INCLUDE_xResumeFromISR 1
108-
#define INCLUDE_vTaskDelayUntil 1
109-
#define INCLUDE_vTaskDelay 1
110-
#define INCLUDE_xTaskGetSchedulerState 1
111-
#define INCLUDE_xTaskGetCurrentTaskHandle 1
112-
#define INCLUDE_uxTaskGetStackHighWaterMark 0
113-
#define INCLUDE_xTaskGetIdleTaskHandle 0
114-
#define INCLUDE_eTaskGetState 0
115-
#define INCLUDE_xEventGroupSetBitFromISR 1
116-
#define INCLUDE_xTimerPendFunctionCall 0
117-
#define INCLUDE_xTaskAbortDelay 0
118-
#define INCLUDE_xTaskGetHandle 0
119-
#define INCLUDE_xTaskResumeFromISR 1
120-
121-
122-
/* __NVIC_PRIO_BITS will be specified when CMSIS is being used. */
123-
#define configPRIO_BITS __NVIC_PRIO_BITS
124-
125-
/* The lowest interrupt priority that can be used in a call to a "set priority"
126-
function. */
127-
#define configLIBRARY_LOWEST_INTERRUPT_PRIORITY 15
128-
129-
/* The highest interrupt priority that can be used by any interrupt service
130-
routine that makes calls to interrupt safe FreeRTOS API functions. DO NOT CALL
131-
INTERRUPT SAFE FREERTOS API FUNCTIONS FROM ANY INTERRUPT THAT HAS A HIGHER
132-
PRIORITY THAN THIS! (higher priorities are lower numeric values. */
133-
#define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY 4
13455

13556
/* Interrupt priorities used by the kernel port layer itself. These are generic
13657
to all Cortex-M ports, and do not rely on any particular library functions. */
137-
#define configKERNEL_INTERRUPT_PRIORITY ( configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
138-
/* !!!! configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to zero !!!!
139-
See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */
140-
#define configMAX_SYSCALL_INTERRUPT_PRIORITY ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
58+
#define configKERNEL_INTERRUPT_PRIORITY (((1u << __NVIC_PRIO_BITS) - 1u) << (8u - __NVIC_PRIO_BITS))
14159

14260
/* Definitions that map the FreeRTOS port interrupt handlers to their CMSIS
14361
standard names. */
144-
#define xPortPendSVHandler PendSV_Handler
145-
#define vPortSVCHandler SVC_Handler
146-
#define xPortSysTickHandler SysTick_Handler
62+
#define xPortPendSVHandler PendSV_Handler
63+
#define vPortSVCHandler SVC_Handler
64+
#define xPortSysTickHandler SysTick_Handler
65+
14766

14867
/* A header file that overwrites with local project settings. */
14968
#if __has_include(<FreeRTOSConfigLocal.h>)
150-
#include <FreeRTOSConfigLocal.h>
69+
# include <FreeRTOSConfigLocal.h>
70+
#endif
71+
72+
73+
/* Required config by FreeRTOS and defaulted by modm */
74+
75+
/* !!!! configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to zero !!!!
76+
See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */
77+
#ifndef configMAX_SYSCALL_INTERRUPT_PRIORITY
78+
# define configMAX_SYSCALL_INTERRUPT_PRIORITY (__NVIC_PRIO_BITS << (8u - __NVIC_PRIO_BITS))
79+
#endif
80+
81+
#ifndef configMINIMAL_STACK_SIZE
82+
# define configMINIMAL_STACK_SIZE 128
83+
#endif
84+
#ifndef configMAX_PRIORITIES
85+
# define configMAX_PRIORITIES 5
86+
#endif
87+
#ifndef configUSE_PREEMPTION
88+
# define configUSE_PREEMPTION 1
89+
#endif
90+
#ifndef configUSE_IDLE_HOOK
91+
# define configUSE_IDLE_HOOK 0
92+
#endif
93+
#ifndef configUSE_16_BIT_TICKS
94+
# define configUSE_16_BIT_TICKS 0
95+
#endif
96+
97+
/* Defaulted by FreeRTOS but changed by modm */
98+
99+
#ifndef configUSE_MUTEXES
100+
# define configUSE_MUTEXES 1
101+
#endif
102+
#ifndef configUSE_RECURSIVE_MUTEXES
103+
# define configUSE_RECURSIVE_MUTEXES 1
104+
#endif
105+
#ifndef configUSE_COUNTING_SEMAPHORES
106+
# define configUSE_COUNTING_SEMAPHORES 1
107+
#endif
108+
#ifndef configQUEUE_REGISTRY_SIZE
109+
# define configQUEUE_REGISTRY_SIZE 10
110+
#endif
111+
#ifndef configENABLE_BACKWARD_COMPATIBILITY
112+
# define configENABLE_BACKWARD_COMPATIBILITY 0
113+
#endif
114+
#ifndef configNUM_THREAD_LOCAL_STORAGE_POINTERS
115+
# define configNUM_THREAD_LOCAL_STORAGE_POINTERS 5
116+
#endif
117+
#ifndef configCHECK_FOR_STACK_OVERFLOW
118+
# define configCHECK_FOR_STACK_OVERFLOW 1
119+
#endif
120+
121+
/* The timers module relies on xTaskGetSchedulerState(). */
122+
#ifndef configUSE_TIMERS
123+
# define configUSE_TIMERS 1
124+
#endif
125+
#ifndef configTIMER_TASK_PRIORITY
126+
# define configTIMER_TASK_PRIORITY 3
127+
#endif
128+
#ifndef configTIMER_QUEUE_LENGTH
129+
# define configTIMER_QUEUE_LENGTH 10
130+
#endif
131+
#ifndef configTIMER_TASK_STACK_DEPTH
132+
# define configTIMER_TASK_STACK_DEPTH configMINIMAL_STACK_SIZE
133+
#endif
134+
135+
/* Optional functions all enabled. */
136+
#ifndef INCLUDE_vTaskPrioritySet
137+
# define INCLUDE_vTaskPrioritySet 1
138+
#endif
139+
#ifndef INCLUDE_uxTaskPriorityGet
140+
# define INCLUDE_uxTaskPriorityGet 1
141+
#endif
142+
#ifndef INCLUDE_vTaskDelete
143+
# define INCLUDE_vTaskDelete 1
144+
#endif
145+
#ifndef INCLUDE_vTaskSuspend
146+
# define INCLUDE_vTaskSuspend 1
147+
#endif
148+
#ifndef INCLUDE_xTaskDelayUntil
149+
# define INCLUDE_xTaskDelayUntil 1
150+
#endif
151+
#ifndef INCLUDE_vTaskDelay
152+
# define INCLUDE_vTaskDelay 1
153+
#endif
154+
#ifndef INCLUDE_xTaskGetIdleTaskHandle
155+
# define INCLUDE_xTaskGetIdleTaskHandle 1
156+
#endif
157+
#ifndef INCLUDE_xTaskAbortDelay
158+
# define INCLUDE_xTaskAbortDelay 1
159+
#endif
160+
#ifndef INCLUDE_xQueueGetMutexHolder
161+
# define INCLUDE_xQueueGetMutexHolder 1
162+
#endif
163+
#ifndef INCLUDE_xSemaphoreGetMutexHolder
164+
# define INCLUDE_xSemaphoreGetMutexHolder 1
165+
#endif
166+
#ifndef INCLUDE_xTaskGetHandle
167+
# define INCLUDE_xTaskGetHandle 1
168+
#endif
169+
#ifndef INCLUDE_uxTaskGetStackHighWaterMark
170+
# define INCLUDE_uxTaskGetStackHighWaterMark 1
171+
#endif
172+
#ifndef INCLUDE_uxTaskGetStackHighWaterMark2
173+
# define INCLUDE_uxTaskGetStackHighWaterMark2 1
174+
#endif
175+
#ifndef INCLUDE_eTaskGetState
176+
# define INCLUDE_eTaskGetState 1
177+
#endif
178+
#ifndef INCLUDE_xTaskResumeFromISR
179+
# define INCLUDE_xTaskResumeFromISR 1
180+
#endif
181+
#ifndef INCLUDE_xTimerPendFunctionCall
182+
# define INCLUDE_xTimerPendFunctionCall 1
183+
#endif
184+
#ifndef INCLUDE_xTaskGetSchedulerState
185+
# define INCLUDE_xTaskGetSchedulerState 1
186+
#endif
187+
#ifndef INCLUDE_xTaskGetCurrentTaskHandle
188+
# define INCLUDE_xTaskGetCurrentTaskHandle 1
151189
#endif
152190

153191
#endif /* FREERTOS_CONFIG_H */

ext/aws/freertos.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# FreeRTOS
2+
3+
Amazon FreeRTOS is an open source, real-time operating system (RTOS) for
4+
microcontrollers in small, low-power devices.
5+
6+
This module provides the latest FreeRTOS LTS release intergrated with modm:
7+
8+
- Chooses the right Cortex-M port for the target.
9+
- Variable tick rate integrated with `modm::Clock` via `modm:platform:clock`.
10+
- Dynamic memory provided by the `modm:platform:heap` module with thread-safe
11+
locking of Newlib's `malloc/free`.
12+
- Assertions integrated into the `modm:architecture:assert` interface.
13+
- Interrupt handling integrated with CMSIS NVIC functions.
14+
15+
Note that we recommend using the FreeRTOS API directly and only if you write
16+
threaded code that also runs on other platforms (x86 simulators for example)
17+
should you consider using the API defined in `modm:processing:rtos` module.
18+
19+
20+
## Configuration
21+
22+
This module generates a `FreeRTOSConfig.h` config file for modm integration with
23+
these settings:
24+
25+
- `configASSERT(x)` implemented with `modm_assert(x, "freertos")`.
26+
- `configCPU_CLOCK_HZ` implemented with CMSIS `SystemCoreClock`.
27+
- `configTICK_RATE_HZ` set to `modm:freertos:frequency` or 1kHz on Cortex-M0.
28+
- `configSUPPORT_DYNAMIC_ALLOCATION = 1` as implemented by `modm:platform:heap`.
29+
- `configUSE_TICK_HOOK = 1` used by `modm:platform:clock` to provide `modm::Clock`.
30+
31+
In addition we define these overwritable default settings:
32+
33+
- `configMAX_SYSCALL_INTERRUPT_PRIORITY` = `(__NVIC_PRIO_BITS << (8u - __NVIC_PRIO_BITS))`
34+
35+
- `configMINIMAL_STACK_SIZE` = 128
36+
- `configMAX_PRIORITIES` = 5
37+
- `configUSE_PREEMPTION` = 1
38+
- `configUSE_IDLE_HOOK` = 0
39+
- `configUSE_16_BIT_TICKS` = 0
40+
41+
- `configUSE_MUTEXES` = 1
42+
- `configUSE_RECURSIVE_MUTEXES` = 1
43+
- `configUSE_COUNTING_SEMAPHORES` = 1
44+
- `configQUEUE_REGISTRY_SIZE` = 10
45+
- `configNUM_THREAD_LOCAL_STORAGE_POINTERS` = 5
46+
- `configCHECK_FOR_STACK_OVERFLOW` = 1
47+
48+
- `configUSE_TIMERS` = 1
49+
- `configTIMER_TASK_PRIORITY` = 3
50+
- `configTIMER_QUEUE_LENGTH` = 10
51+
- `configTIMER_TASK_STACK_DEPTH` = `configMINIMAL_STACK_SIZE`
52+
53+
All other config settings are unchanged by modm and are defaulted by FreeRTOS
54+
itself, please refer to the [FreeRTOS Config documentation][config] to
55+
understand what each of these do.
56+
57+
To change a configuration setting, define a `<FreeRTOSConfigLocal.h>` file,
58+
which is included *before* the optional settings, so you can overwrite them
59+
easily.
60+
61+
An example `<FreeRTOSConfigLocal.h>` file:
62+
63+
```c
64+
// Use a bigger minimal stack size
65+
#define configMINIMAL_STACK_SIZE 256
66+
// Longer timer queue
67+
#define configTIMER_QUEUE_LENGTH 20
68+
```
69+
70+
[config]: https://www.freertos.org/a00110.html

ext/aws/module.lb

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -71,41 +71,31 @@ the config macros, then redefine them with your options, for example:
7171

7272
def init(module):
7373
module.name = "freertos"
74-
module.description = """\
75-
# a:FreeRTOS
76-
77-
Amazon FreeRTOS port for modm to be used with via the `modm:processing:rtos` module.
78-
79-
## Custom Configuration
80-
81-
This module defines a default FreeRTOS config. If you need to change a
82-
configuration setting, then you can define a `FreeRTOSConfigLocal.h` file, which
83-
is included *after* at the end of the config. You must therefore first `#undef`
84-
the config macros, then redefine them with your options, for example:
85-
86-
```c
87-
#undef configCHECK_FOR_STACK_OVERFLOW
88-
#define configCHECK_FOR_STACK_OVERFLOW 0
89-
```
90-
"""
74+
module.description = FileReader("freertos.md")
9175

9276
def prepare(module, options):
9377
device = options[":target"]
9478
core = device.get_driver("core")
9579
if not (core and core["type"].startswith("cortex-m")):
9680
return False
9781

98-
module.add_option(
99-
NumericOption(
100-
name="frequency",
101-
description="Context switch frequency in Hz",
102-
minimum=1, maximum=1000,
103-
default=1000))
82+
if "m0" not in core:
83+
def validate_frequency(freq):
84+
if (1000 % freq > 1):
85+
raise ValueError("FreeRTOS frequency must cleanly divide 1kHz for modm::Clock!")
86+
module.add_option(
87+
NumericOption(
88+
name="frequency",
89+
description="Context switch frequency in Hz",
90+
validate=validate_frequency,
91+
minimum=4, maximum=1000,
92+
default=1000))
10493

10594
module.depends(
10695
":architecture:assert",
10796
":cmsis:device",
108-
":platform:clock")
97+
":platform:clock",
98+
":platform:heap")
10999

110100
module.add_submodule(FreeRTOS_TCP())
111101

@@ -114,7 +104,10 @@ def prepare(module, options):
114104
def build(env):
115105
# Figure out the core and correct path in portable/
116106
core = env[":target"].get_driver("core")["type"]
117-
env.substitutions = {"core": core}
107+
env.substitutions = {
108+
"core": core,
109+
"frequency": env.get("frequency", 1000)
110+
}
118111
path = core.replace("cortex-m", "ARM_CM").replace("+", "").replace("fd", "f").upper()
119112
path = path.replace("CM7F", "CM7/r0p1") # use subfolder for M7
120113
path = "freertos/FreeRTOS/Source/portable/GCC/{}".format(path)

repo.lb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ from os.path import normpath
2424

2525
# Check for miminum required lbuild version
2626
import lbuild
27-
min_lbuild_version = "1.16.1"
27+
min_lbuild_version = "1.17.0"
2828
if StrictVersion(getattr(lbuild, "__version__", "0.1.0")) < StrictVersion(min_lbuild_version):
2929
print("modm requires at least lbuild v{}, please upgrade!\n"
3030
" pip3 install -U lbuild".format(min_lbuild_version))

0 commit comments

Comments
 (0)