Skip to content

Commit

Permalink
Update FreeRTOS/README.md & NOTES_architecture_and_best_practices.md
Browse files Browse the repository at this point in the history
  • Loading branch information
ElectricRCAircraftGuy committed Jan 26, 2024
1 parent 768d48d commit 28c1248
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
49 changes: 45 additions & 4 deletions FreeRTOS/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ See:

These replacements can be done either programmatically at the command line, or manually in MS VSCode, for instance, using the global "Find and Replace" feature with regexes enabled.


## 1. Mass `xTaskCreate()` to `xTaskCreateStatic()` conversions

1. Find and replace all instances of `xTaskCreate()` with `xTaskCreateStatic()`
1. Example 1:

Expand Down Expand Up @@ -48,7 +51,7 @@ These replacements can be done either programmatically at the command line, or m
```
Replace with:
```
$1static StackType_t taskStack[$6];\n$1static StaticTask_t taskControlBlock;\n$1$11 = xTaskCreateStatic($3,$4, $6,$7,$8, taskStack, &taskControlBlock);
$1static StackType_t $3Stack[$6];\n$1static StaticTask_t $3ControlBlock;\n$1$11 = xTaskCreateStatic($3,$4, $6,$7,$8, $3Stack, &$3ControlBlock);
```
1. Step 2: replace all instances of `NULL = xTaskCreateStatic(` (which happened in Step 1 cases where the last parameter passed to `xTaskCreate()` was `NULL`) with just `xTaskCreateStatic(`
Expand All @@ -75,7 +78,45 @@ These replacements can be done either programmatically at the command line, or m
```c
TaskHandle_t myTaskHandle = NULL;
static StackType_t taskStack[configMINIMAL_STACK_SIZE];
static StaticTask_t taskControlBlock;
myTaskHandle = xTaskCreateStatic(myTask, "My Task", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, taskStack, &taskControlBlock);
static StackType_t myTaskStack[configMINIMAL_STACK_SIZE];
static StaticTask_t myTaskControlBlock;
myTaskHandle = xTaskCreateStatic(myTask, "My Task", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, myTaskStack, &myTaskControlBlock);
```
## 2. General fixes
To convert this:
```c
static StackType_t taskStack[configMINIMAL_STACK_SIZE];
static StaticTask_t taskControlBlock;
myTaskHandle = xTaskCreateStatic(myTask, "My Task", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, taskStack, &taskControlBlock);
```

to this:
```c
#define myTask_STACK_SIZE (configMINIMAL_STACK_SIZE)
static StackType_t taskStack[myTask_STACK_SIZE];
static StaticTask_t taskControlBlock;
myTaskHandle = xTaskCreateStatic(myTask, "My Task", myTask_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, taskStack, &taskControlBlock);
```
WORK-IN-PROGRESS:
Find:
```
^(.*)(static StackType_t )(.*)\[(.*)\];(.*)\n^(.*)(static StaticTask_t )(.*);(.*)\n

TODO: finish this regex
^(.*)(xTaskCreate\()(.*),(.*),( )*(.*),(.*),(.*),( )*(&)?(.*)(\));(.*)


^(.*)(xTaskCreate\()(.*),(.*),( )*(.*),(.*),(.*),( )*(&)?(.*)(\));(.*)
```
Replace with:
```
TODO: finish this regex
$1static StackType_t $3Stack[$6];\n$1static StaticTask_t $3ControlBlock;\n$1$11 = xTaskCreateStatic($3,$4, $6,$7,$8, $3Stack, &$3ControlBlock);
```

4 changes: 4 additions & 0 deletions NOTES_architecture_and_best_practices.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,7 @@ These could also be part of a "style guide" or "coding standards" document.
1. https://www.freertos.org/FreeRTOS-Software-Timer-API-Functions.html
1. Task creation `malloc`s (dynamically allocates memory), which is non-deterministic and can lead to memory fragmentation and run-time crashes. That's nuts. Instead, use timers for what timers are for!
1. Note: using `xTaskCreateStatic()` is far better than `xTaskCreate()`, because it does not dynamically allocate memory. But, it is still bad.

1. When allocating stack sizes, use `configMINIMAL_STACK_SIZE` as a multiplier, not an adder. Ex: use `configMINIMAL_STACK_SIZE*10`, for instance, not `configMINIMAL_STACK_SIZE + 500` or whatever.
1. This allows easy scaling across the whole system.
1. This way, stack sizes can be seen in relative sizes to each other. And, to increase the stack size proportionally for the whole system all at once, you just increase the value of `configMINIMAL_STACK_SIZE`, which is the base multiplier.

0 comments on commit 28c1248

Please sign in to comment.