Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


Problems Using FreeRTOS-MPU on NXP K66

Posted by marcbe on March 23, 2017


I have some problems getting the FreeRTOS-MPU port for Cortex-M4 to work. I am using NXP's Kinetis Design Studio with SDK v2 for a Kinetis K66 (FRDM-K66F Platform) under GCC.

I first created a new SDK Project from FreeRTOS Hello World Example. Then replaced the FreeRTOS port (port.c, port.h), added mpu_wrappers.c and added some entries in Linker Script (see below).

I set up the parameters for hello_task (User-Mode, MPU region which allows read/write access to UART peripheral) and created the task with xTaskCreateRestricted.

Unfortunately there comes a HardFault when the task is trying to access the UART registers (read or write) so it seems my MPU setting is wrong. Starting the task as privileged task everything works fine.

Could someone provide help?

Below an excerpt of the linker script: ~~~

/* Variables used by FreeRTOS-MPU. */ PrivilegedFunctionsRegionSize = 32K; PrivilegedDataRegionSize = 512;

FLASHsegmentstart = ORIGIN( minterrupts ); __FLASHsegmentend__ = __FLASHsegmentstart__ + LENGTH( minterrupts ) + LENGTH( mflashconfig ) + LENGTH ( m_text );

privilegedfunctionsstart = ORIGIN( mtext ); __privilegedfunctionsend__ = __privilegedfunctionsstart__ + _PrivilegedFunctionsRegionSize;

SRAMsegmentstart = ORIGIN( mdata ); __SRAMsegmentend__ = __SRAMsegmentstart__ + LENGTH( mdata ) + LENGTH( mdata2 );

privilegeddatastart = ORIGIN( mdata ); __privilegeddataend__ = __privilegeddatastart__ + _PrivilegedDataRegionSize;

/* Define output sections / SECTIONS { / The startup code goes first into internal flash / .interrupts : { _VECTORTABLE = .; . = ALIGN(4); KEEP((.isrvector)) /* Startup code */ . = ALIGN(4);
} > minterrupts

.flashconfig : { . = ALIGN(4); KEEP((.FlashConfig)) / Flash Configuration Field (FCF) */ . = ALIGN(4); } > mflash_config

privilegedfunctions : { . = ALIGN(4); *(privilegedfunctions) . = ALIGN(4);

    /* Non privileged code is after _Privileged_Functions_Region_Size. */
    __privileged_functions_actual_end__ = .;
    . = _Privileged_Functions_Region_Size;
} > m_text


.interruptsram : { . = ALIGN(4); __VECTORRAM__ = .; interruptsramstart = .; /* Create a global symbol at data start / *(.minterruptsram) / This is a user defined section / . += MVECTORRAMSIZE; . = ALIGN(4); __interruptsram_end__ = .; / Define a global symbol at data end */ } > m_data

VECTOR_RAM = DEFINED(ramvectortable) ? _VECTORRAM : ORIGIN(minterrupts); __RAMVECTORTABLESIZEBYTES = DEFINED(__ramvectortable) ? (interruptsramend__ - __interruptsram_start__) : 0x0;

privilegeddata : { *(privilegeddata) /* Non kernel data is kept out of the first PrivilegedDataRegionSize bytes of SRAM. */ privilegeddataactual_end = .; . = PrivilegedDataRegionSize; } > m_data

.data : AT(_DATAROM) { ...


And here the task set up in freertos_hello.c:

~~~ static void hello_task(void *pvParameters);


/* GCC specifics. */

define mainALIGN_TO( x ) attribute((aligned(x)))

/* Task priorities. */

define hellotaskPRIORITY ((configMAX_PRIORITIES - 1))
define mainMPUDEBUGUART_SIZE (0x80)


static const TaskParameterst xHelloTaskParameters = { hellotask, "Hello", mainHELLOTASKSTACKSIZEWORDS, NULL, hellotaskPRIORITY, xHelloTaskStack, { { mainMPUDEBUGUARTBASEADDR, mainMPUDEBUGUARTSIZE, portMPUREGIONREADWRITE}, { 0x00, 0x00, 0x00 }, { 0x00, 0x00, 0x00 }




int main(void) { /* Init board hardware. */ BOARDInitPins(); BOARDBootClockRUN(); BOARD_InitDebugConsole(); xTaskCreateRestricted( &xHelloTaskParameters, NULL); vTaskStartScheduler(); for (;;) ; }

/*! * @brief Task responsible for printing of "Hello world." message. */ static void hello_task(void *pvParameters) { for (;;) { GETCHAR(); PRINTF("Hello world.rn"); vTaskSuspend(NULL); } }


Problems Using FreeRTOS-MPU on NXP K66

Posted by rtel on March 23, 2017

Can you confirm the task starts, and the fault only occurs when you attempt to access the UART? If so, what is the address of the data access that causes the fault?

What is BOARDDEBUGUARTBASEADDR set to? The Cortex-M hardware restricts MPU region start addresses to power of 2 boundaries. Likewise the length (mainMPUDEBUGUARTSIZE) must be a power of 2 (from memory).

Problems Using FreeRTOS-MPU on NXP K66

Posted by marcbe on March 24, 2017

Problems Using FreeRTOS-MPU on NXP K66

Posted by marcbe on March 27, 2017

BOARDDEBUGUART_BASEADDR is set to the address of UART0 which is (0x4006A000u). The size of the region is set to 0x80. If I understood the reference manual right, these settings should be correct for the MPU?

The task starts correctly and by stepping through the debugger I found out the following line in function UART_ReadBlocking leads to the HardFault:


    while (!base->RCFIFO)


With base pointing to address 0x4006a000 (which is UART0) and RCFIFO at Offset 0x0016.

Problems Using FreeRTOS-MPU on NXP K66

Posted by marcbe on March 31, 2017

Anybody there who could give some hints? I am still not able to get the MPU port run :-(

Problems Using FreeRTOS-MPU on NXP K66

Posted by rtel on March 31, 2017

The base address and region size look to conform with the requirements (region size is binary power of 2 and base address is a multiple of the region size). Are there any additional configuration items required to allow access to the peripherals?

Problems Using FreeRTOS-MPU on NXP K66

Posted by marcbe on April 3, 2017

After some more debugging I found out the HardFault Exception was not caused by the MPU. The exact fault was a BusFault and not a MemManage Fault as I expected it to be.

The reference manual says, the peripherals connected to eiter AIPS-Lite0 or AIPS-Lite1 Peripheral Bridge are not protected by the MPU but have their own protection included.

So the solution was to make the peripheral accessible from user mode by clearing the corresponding Supervisor Protect Bit in AIPSx_PACRn Register:

~~~ AIPS0->PACRN &= ~(AIPSPACRNSP2_MASK); /* clear Supervisor Protect Bit */ ~~~

The MPU setting for this region is not used.

Thanks for your help.

Problems Using FreeRTOS-MPU on NXP K66

Posted by rtel on April 3, 2017

Thanks for taking the time to report the solution.

[ Back to the top ]    [ About FreeRTOS ]    [ Privacy ]    [ Sitemap ]    [ ]

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS.

Meet Richard Barry and learn about running FreeRTOS on RISC-V at FOSDEM 2019

Version 10.1.1 of the FreeRTOS kernel is available for immediate download. MIT licensed.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


FreeRTOS and other embedded software careers at AWS.

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers


STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner