freertos with endless loop in main instead of tasks

kafna wrote on Saturday, January 06, 2018:

I’m working on a port of freertos to raspberry pi , I want to eventually port it to pi3 on 64 bit.

The code of pxPortInitialiseStack , portRESTORE_CONTEXT and portSAVE_CONTEXT is identical to the port for GCC/ARM7_AT91FR40008

I added uart driver with an intrrupt handler that works , and I want to start by porting only the uart driver. So I don’t want to enable the timer interrupt and the scheduler, instead I want to run an endless loop in main function.

I wrote a function xInitPxCurrentTCB
which initialize pxCurrentTCB since it is used to save the sp register in context switch.

The main function initializes the uart, then calls xInitPxCurrentTCB and then enables the interrupts.
It also initialize the spsr register to 0x1f as this is the initial value set in the function pxPortInitialiseStack

it then enters an endless loop where it turn the green led on and off.

The code for main.

I have two bugs that I couldn’t figure them out:

  1. If I don’t initialize the spsr register, then upon sending rx to the uart, the main function then starts from it’s beginning after the interrupt handler returns instead of continuing from somewhere in the endless loop where the interrupt fired.
  2. If do initalize the spsr in the main function with:
__asm volatile("nop");
__asm volatile ("push {r0}");
__asm volatile ("mov r0, #31");
__asm volatile ("msr spsr_cxsf, r0");
__asm volatile ("pop {r0}");

Then it seems that there is a contraption , as the green led stop blinking.

richard_damon wrote on Saturday, January 06, 2018:

Many of the FreeRTOS API functions aren’t supposed to be used (especially the 'FromISR ones) until after the schedule has been started, so you are going to be fighting FreeRTOS all the way with this plan.

Rather than trying to make things work with an endless loop in main, create a single task, and put the endless loop in that. Run it at task priority 0 so the Idle task will get time (not so important now, but later when you split off other functions, it may be needed), and then you won’t have any of these sort of issues.