understanding portable code

kafna wrote on Saturday, December 09, 2017:

Hi,
I don’t have a lot of background in RT , I want to understand few things.

  1. regarding the function pxPortInitialiseStack, it is wrriten Initialise the stack of a task to look exactly as if a call to portSAVE_CONTEXT had been called. So does it mean that pxPortInitialiseStack and portSAVE_CONTEXT are identical ? If I want to implement a new port, I should just make sure that those functions and also portRESTORE_CONTEXT are consistent with each other?
    For example, I can decide the order for which to push the registers values to the stack as long as it’s the same order for the pxPortInitialiseStack and portSAVE_CONTEXT functions?

  2. Is the function portRESTORE_CONTEXT equivalent to the function __switch_to in the linux kernel ?

  3. I want to add a new port to dragonboard410c, which is arm64 cortext A53, which I see a directory in freertos portable/GCC/ARM_CA53_64_BIT is already implemented. Can I use the code there as is or there is a possibilty that I might need to adjust things ?

Thanks

rtel wrote on Monday, December 11, 2017:

regarding the function |pxPortInitialiseStack|, it is wrriten
|Initialise the stack of a task to look exactly as if a call to
portSAVE_CONTEXT had been called|. So does it mean that
|pxPortInitialiseStack| and |portSAVE_CONTEXT| are identical ? 

No - perhaps too obviously if you look at the code, so maybe you don’t
mean literally - one is a function that sets the initial stack frame for
a new task, and the other is [normally, but depends on port] a macro
that saves the CPU context to the stack of a task.

So portSAVE_CONTEXT saves the CPU context onto the stack of the task,
and portRESTORE_CONTEXT restores the CPU context from the stack of the
task. It is the job of pxPortInitialiseStack() to create a stack frame
that is identical to the one created by portSAVE_CONTEXT so when
portRESTORE_CONTEXT is called to run a newly created task for the first
time everything works as expected.

If I
want to implement a new port, I should just make sure that those
functions and also |portRESTORE_CONTEXT| are consistent with each other?

Yes.

For example, I can decide the order for which to push the registers
values to the stack as long as it's the same order for the
|pxPortInitialiseStack| and |portSAVE_CONTEXT| functions?

Almost - you also have to take into account the way the compiler uses
the stack. For example, a parameter can be passed into a task so you
need to know where the code generated by the compiler expects to find
that parameter - be it in a particular register (r0 in the case of most
ARM MCUs), or on the stack.

Is the function |portRESTORE_CONTEXT| equivalent to the function
|__switch_to| in the linux kernel ?

Sorry, don’t know :o)

I want to add a new port to dragonboard410c
<https://developer.qualcomm.com/hardware/dragonboard-410c>, which is
arm64 cortext A53, which I see a directory in freertos
|portable/GCC/ARM_CA53_64_BIT| is already implemented. Can I use the
code there as is or there is a possibilty that I might need to
adjust things ?

No doubt the port in that directory will be a good starting point, and
maybe everything you need. It mainly depends on the interrupt
controller used. The port was written for the Zynq Ultrascale, which (I
think) uses the standard ARM interrupt controller. If your port uses
the same interrupt controller then you may not need to change anything
(or maybe just a few register addresses).