1.15 FreeRTOS with MPU

FreeRTOS with MPU enables 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

Description

This demonstration enables MPU support in FreeRTOS. It then creates three tasks as shown below:

  • Task1: rdonly_task

    This task runs in un-privileged (user) mode. It has read-only access to the ucSharedMemory memory buffer and read-write access to the fault_testing memory.

  • Task2: rw_task

    This task runs in un-privileged (user) mode. It has read-write access to the ucSharedMemory memory buffer.

  • Task3: priv_task

    This task runs in privileged mode and has access to all the memory regions.

priv_task is created in privileged mode and it drives the menu on the serial terminal. Since it is a privileged task, it has access to all the memory regions. Based on the user input, the rdonly_task writes to the ucSharedMemory[1] by temprarily switching itself to the privileged mode. Once the ucSharedMemory[1] is updated, it switches itself back to the user mode. Optionally, based on the user input, it tries to write to the ucSharedMemory[1] region in user mode where it does not have write access (only read access). This results in a memory management fault. Along with other details, the memory management fault handler prints the memory address that caused the memory management fault. This address is same as the ucSharedMemory[1] address. The rw_task runs in user mode, but it has been configured to have full (read/write) access to the ucSharedMemory buffer. Every time it runs, it increments the ucSharedMemory[0] memory location. Based on the user input, the value of ucSharedMemory[0] can be printed on the console.

Downloading and Building the Application

To clone or download this application from Github, go to the main page of this repository and then click Clone button to clone this repository or download as zip file. This content can also be downloaded using content manager by following these instructions.

Path of the application within the repository is apps/rtos/freertos/freertos_mpu/firmware.

To build the application, refer to the following table and open the project using its IDE.

Project NameDescription
sam_e54_xpro.XMPLABX project for SAM E54 Xplained Pro Evaluation Kit

Setting Up the Hardware

The following table shows the target hardware for the application projects.

Project NameDescription
sam_e54_xpro.XSAM E54 Xplained Pro Evaluation Kit

Setting Up SAM E54 Xplained Pro Evaluation Kit

  • Connect the Debug USB port on the board to the computer using a micro USB cable

Running the Application

  1. Open the Terminal application (Ex.:Tera Term) on the computer.
  2. Connect to the EDBG Virtual COM port and configure the serial settings as follows:
    • Baud : 115200
    • Data : 8 Bits
    • Parity : None
    • Stop : 1 Bit
    • Flow Control : None
  3. Build and program the application using its IDE.
  4. Observe the following output on the terminal. Notice the address of ucSharedMemory[1].
  5. Enter '1' to force the rdonly_task to write to ucSharedMemory[1] to which it only has read only access in user mode. This should generate a MPU fault and print the MPU fault address as shown below.
  6. Enter '2' to temporarily switch the rdonly_task to privileged mode and write to ucSharedMemory[1]. Once the write is done, it switches itself back to the user mode. Observe the value at ucSharedMemory[1] increment by 1 everytime this option is excercised.
  7. Enter '3' to print the value of ucSharedMemory[0] which is incremented by the priv_task that has access to the entire memory region.