Context switch

General discussions about working with the Astrobe IDE and programming ARM Cortex-M3, M4 and M7 microcontrollers.

Context switch

Postby steve64 » Thu Dec 20, 2018 4:54 pm

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!
steve64
 
Posts: 40
Joined: Mon Jul 09, 2018 8:56 am
Location: Italy

Re: Context switch

Postby cfbsoftware » Fri Dec 21, 2018 10:46 am

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
cfbsoftware
Site Admin
 
Posts: 350
Joined: Fri Dec 31, 2010 12:30 pm

Re: Context switch

Postby steve64 » Fri Dec 21, 2018 4:57 pm

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.
steve64
 
Posts: 40
Joined: Mon Jul 09, 2018 8:56 am
Location: Italy

Re: Context switch

Postby cfbsoftware » Fri Dec 21, 2018 10:35 pm

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/
cfbsoftware
Site Admin
 
Posts: 350
Joined: Fri Dec 31, 2010 12:30 pm

Re: Context switch

Postby steve64 » Mon Dec 24, 2018 2:43 pm

Thanks, I will look at it.
steve64
 
Posts: 40
Joined: Mon Jul 09, 2018 8:56 am
Location: Italy


Return to Astrobe for ARM Cortex-M3, M4 and M7

cron