SAM9G20 Issues with FreeRTOS, possibly related to PIT and AIC peripherals

Moderator: nferre

Posts: 7
Joined: Thu Nov 12, 2020 11:19 am

SAM9G20 Issues with FreeRTOS, possibly related to PIT and AIC peripherals

Thu Mar 25, 2021 12:36 pm


My application running on a AT91SAM9G20-EK consists of a two-stage bootloader and a main application:

1. First stage bootloader on the on-board NAND-Flash executed in SRAM0. It is loaded by the ROM-Boot program of the SAM9G20
2. Second stage bootloader in NAND-Flash address 0x20000 loaded at SDRAM address 0x20100000 by the first-stage bootloader
3. Main application at NAND-Flash 0x40000 or on a SD-Card loaded at SDRAM address 0x20000000 by the second-stage bootloader

Boot mode select pin is 1 to use ROM-Boot and the ROM-Boot loads the first-stage bootloader into SRAM0 to execute it there. The second-stage bootloader and primary application are executed in SDRAM.

Some more information: Our AT91 library is based partly on sources provided by ISIS and on the softpack 1.5.
All of our software except proprietary libraries is public: ... m9g20/at91 ... bootloader

arm-none-eabi-gcc (xPack GNU Arm Embedded GCC, 64-bit) 9.3.1 20191025

I tried to use FreeRTOS in the second stage bootloader for some convenience, but this caused the main application to crash somewhere during the start of the first FreeRTOS task. This was not happening when not using FreeRTOS so thats the solution I chose.

I now found out that when I use the PIT and AIC peripherals of the SAM9G20 core to create a millisecond counter in the first-stage bootloader, the main application now shows a similar behaviour and crashes during start of the first task or very early in the first task. Might it be possible that this issue is related to those peripherals? I am disabling those interrupts before jumping from the first-stage bootloader to the second-stage bootloader but the problem persist.. Our target hardware uses a NOR-Flash boot instead and I wanted to use the PIT and AIC to create a time base, but now I am not sure whether I should do this anymore because PIT and AIC appear to cause some issues.. I am using the PIT and AIC already in the second-stage bootloader, so this is also a bit confusing because this apparently was not an issue.

This it the code I used to enable PIT and AIC in the first stage bootloader

Code: Select all

 * Setup the PIT to generate the tick interrupts at the required frequency.
void setup_timer_interrupt(void)
    // each 1 ms the pit throws an interrupt
    const unsigned long ulPeriodIn_uS = ( 1.0 /
            ( double ) TICK_RATE_HZ ) * port1SECOND_IN_uS;

    /* Setup the PIT for the required frequency. */
    PIT_Init( ulPeriodIn_uS, BOARD_MCK / port1MHz_IN_Hz );

    /* Setup the PIT interrupt. */
    AIC_DisableIT( AT91C_ID_SYS );
    AIC_ConfigureIT( AT91C_ID_SYS, TICK_SYSTEM_PRIORITY, ms_tick_isr);
    AIC_EnableIT( AT91C_ID_SYS );

void ms_tick_isr(void)
    volatile unsigned long ulDummy;
    /* Clear the PIT interrupt. */
    ulDummy = AT91C_BASE_PITC->PITC_PIVR;
    /* To remove compiler warning. */
    ( void ) ulDummy;
    /* The AIC is cleared in the asm wrapper, outside of this function. */
and to disable

Code: Select all

void disable_pit_aic() {
    // Unstack nested interrupts
    for (int i = 0; i < 8 ; i++) {
        AT91C_BASE_AIC->AIC_EOICR = 0;
    // Clear AIC and PIT interrupts and disable them.
    AIC_DisableIT( AT91C_ID_SYS );

Did anyone already try to use FreeRTOS in a bootloader or configure the PIT/AIC multiple times and have similar issues?

Also some weird behaviour: I am able to flash the second-stage bootloader to the SDRAM directly with a debugger probe. If I use FreeRTOS in the bootloader and immediately continue after the breakpoint in main, I get the issue that there is a crash in the main application. If I set a breakpoint where the jump happens, everything works. The code for the second-stage bootloader can be found here: ... ain_at91.c
Posts: 7
Joined: Thu Nov 12, 2020 11:19 am

Re: SAM9G20 Issues with FreeRTOS, possibly related to PIT and AIC peripherals

Fri Apr 09, 2021 9:44 am

Okay, it is not just the PIT and the AIC. Using the SPI peripheral DMA functionalities (which also cause an interrupt) in the bootloader cause similar issues..
Posts: 7
Joined: Thu Nov 12, 2020 11:19 am

Re: SAM9G20 Issues with FreeRTOS, possibly related to PIT and AIC peripherals

Thu Apr 29, 2021 8:06 pm

It might be possible that I found a solution but I am not entirely sure. In any case I will report back.

The instruction cache was enabled and used but not invalidated before disabling it and jumping to the main application.

This is now done with the following function:

cp15_asm.S file:

Code: Select all

.global _invalidateICache

        mov     r0, #0
        MCR     p15,0,r0,c7,c5,0
        bx      lr
and then call it with _invalidateICache();

Or just not use the instruction cache at all in the bootloader.

Kind Regards

Return to “SAM9-based”

Who is online

Users browsing this forum: No registered users and 4 guests