Page 1 of 1

Context switch

Posted: Thu Dec 20, 2018 4:54 pm
by steve64
I'm wondering if it's feasible to implement with pure Oberon code (of course Astrobe-specific) a simple cooperative multitasking
for the STM32 M7 platform based on following assumptions:
1) each task is an Oberon procedure but with its own stack
2) each task explicitly gives control to other tasks (if any) through a Yield() call
3) the Yield is a custom procedure triggering a software interrupt
4) an interrupt handler is installed for the sw interrupt defined in point 3;
it saves the current context (HW registers), selects a new task, then restores the context of such new task

I have found most of the required pieces inside Astrobe:

SYSTEM.PUT(MCU.STIR, N) is supposed to trigger the sw interrupt N
Traps.Assign(address, P) is supposed to install an handler P in the vector <address> for IRQ N
SYSTEM.REG(X) and SYSTEM.LDREG(X, value) allow to save and restore HW registers

but it's quite difficult to put them together, so far I'm just getting Hard Faults...

Any help/suggestion/reference code is welcome!

Re: Context switch

Posted: Fri Dec 21, 2018 10:46 am
by cfbsoftware
Astrobe for Cortex-M is designed to be used for dedicated single-tasking applications - what you are attempting is out of scope. However if you are determined to give it a try you could use the ASSERT function to generate your software interrupts (SVC) and modify the SVCTrap procedure in the Traps module to switch to another task. Currently it terminates in an infinite loop but there's nothing to stop you from getting it to restart somewhere else instead. It would be a tricky job to get it working right - I wouldn't recommend spending too much time going in that direction.

The proper way to do this would be to use a real-time operating system to handle the task switching and manage shared resources. If I were you I'd use the Project Oberon operating system on FPGA hardware using Astrobe for RISC5. Virtually all of the hard work has already been done for you.

There was a discussion a few weeks ago on the ETH Oberon mailing list where you might get some more ideas:

Oberon as (embedded) rtos

Re: Context switch

Posted: Fri Dec 21, 2018 4:57 pm
by steve64
I will investigate the ASSERT/ SVCTrap road. Thanks for the suggestion.

I need to stay with a standard core like Cortex-M and a RTOS is not applicable in my target scenario
for both memory constraints and context switch speed requirements.
Also I would like to use Oberon.
What I need is a solution for a kind of co-routines, so no need to manage resource access in mutual exclusion.
My main problem now is how to start N procedures, each one with its own stack.

Re: Context switch

Posted: Fri Dec 21, 2018 10:35 pm
by cfbsoftware
Oberon's predecessor, the language Modula-2, includes support for coroutines as part of the language. A detailed description of how this was implemented can be found in ETH Dissertation No 7195: Code Generation and The Lilith Architecture by Christian Jacobi. That should give you a few clues on what to watch out for. You can download a copy from the Modula-2 page on our website:

http://www.cfbsoftware.com/modula2/

Re: Context switch

Posted: Mon Dec 24, 2018 2:43 pm
by steve64
Thanks, I will look at it.