Memory and FreeRtos

Memory management is important in a multi-threaded environment, as threads (or tasks) can quickly consume all of the allocated memory, causing bizarre effects such as overwritten memory locations or random processor resets.

The solution to the challenge in the video can be found here: https:www.digikey.comenmaker/proj…

In this video, we look at various ways to allocate memory to a task and how to monitor memory usage in FreeRTOS.

Most C programs rely on 3 different types of memory. Static memory is set aside prior to program execution and used for things like static variables, constants, and global variables. Stack is allowed to grow dynamically and consists of local variables declared at the function level. Finally, heap may also grow dynamically and must be specifically allocated and deallocated by the programmer (e.g. using the malloc() and free() functions, respectively).

Whenever we create a new task in FreeRTOS, we must assign it a set amount of stack memory to use out of the global, available heap. Note that each task also requires another section of heap for the Task Control Block (TCB), which stores various attributes about the task, such as its state and priority.

We demonstrate ways to monitor the stack in each thread as well as the total amount of heap available to the system. We also show what happens when you overrun the stack or heap!

Protection

In general memeory in freertos is linear (no virtalisation) and no protection. A standard “C” pointer can access all memory.

In nordjysk: could be better

MMU

Memory proctection and virtualisation is not part of FreeRtos. It seems eom one has done some non open source implmentation

MPU

Memory Protection Unit (MPU) From - freertos link:

FreeRTOS MPU ports enable microcontroller applications to be more robust and more secure by: first, enabling tasks to run in either privileged or unprivileged mode; and second by restricting access to resources such as RAM, executable code, peripherals, and memory beyond the limit of a task's stack. Huge benefit is gained from, for example, preventing code from ever executing from RAM. Doing so will protect against many attack vectors such as buffer overflow exploits or the execution of malicious code loaded into RAM.

Use of an MPU necessarily makes application design more complex because: first the MPU's memory region restrictions must be determined and described to the RTOS; and second the MPU restricts what application tasks can and cannot do.

FreeRTOS provides official Memory Protection Unit (MPU) support on ARMv7-M (Cortex-M3, Cortex-M4 and Cortex-M7 microcontrollers) and ARMv8-M (Cortex-M23 and Cortex-M33 microcontroller) cores:

There are two FreeRTOS ports for ARMv7-M cores, one that includes MPU support and one that doesn't. There is only one FreeRTOS port for ARMv8-M cores as MPU support is a compile time option.

SO BEWARE

So please code as a responsible person