lpcware

Minimalist Threading on LPC800

Discussion created by lpcware Employee on Jun 15, 2016
Content originally posted in LPCWare by MarcVonWindscooting on Sun Jan 12 09:05:13 MST 2014
Hi folks,

today I finished porting my simple context switcher from LPC2100 (ARM instruction set used) to the LM800 (THUMB).
To my surprise, it worked at the first try - a rather rare experience when programming! THUMB is NO FUN for such things, because of only a few instructions for high registers, yet I have to obey the AAPCS-2.09 (ARM IHI 0042E, Nov 30, 2012). I wasn't too sure about callee preserving of R12 = IP. I do not preserve it, because I'm programming -ffreestanding (no dynamic linker) and I haven't found a single use of IP by GCC 4.8.1 in any of my libs that indicates the need of preserving it. In fact most occurences seem total nonsense with a pattern like:

mov IP,r1
..
adds r0,r0,r1
..
mov r1,IP

(r1 not destroyed, but 'restored'  :~ )

I don't implement a scheduler, I provide context switching, only. Scheduling is something different. And the most simple implementation will do the job very often - see the example program multitask.c .

It is based on the idea of cooperative multitasking. Functions may be written sequentially but their yield control at the points where tlYield() is called. Similarly, main() yields control, whenever tlCall() is executed.

It's even possible, that an ISR calls tlCall(), provided the 'invoked' thread and the ISR are a 1:1 relationship. In fact, that was my motivation, when I first implemented it. The multithreading was only a by-product  8-)

What do you think about it?

Original Attachment has been moved to: tl_0.h.zip

Original Attachment has been moved to: multitask.c.zip

Original Attachment has been moved to: tl.s_3.txt.zip

Outcomes