multitasking on cortex m 0 class mcu
play

Multitasking on Cortex-M(0) class MCU A deepdive into the - PowerPoint PPT Presentation

Multitasking on Cortex-M(0) class MCU A deepdive into the Chromium-EC scheduler $whoami Embedded Software Engineer at National Instruments We just finished our first product using Chromium-EC and future ones to come Other stuff I


  1. Multitasking on Cortex-M(0) class MCU A deepdive into the Chromium-EC scheduler

  2. $whoami Embedded Software Engineer at National Instruments ● We just finished our first product using Chromium-EC ● and future ones to come Other stuff I do: ● fpga-mgr framework (co)maintainer for the linux kernel ○ random drive-by contributions to other projects ○

  3. r0 r1 r2 Cortex-M0 registers r3 r4 r5 Cortex-M0 has 16 general purpose ● r6 registers Low registers ( r0-r7 ) ○ xPSR r7 High registers ( r8-r15 ) ○ Undefined status at reset ○ r8 PRIMASK Limited size in thumb, often only low regs ○ r9 r13 is stack pointer for current context ● CONTROL stack r10 It is banked, eigher msp or psp ○ r11 xPSR is depending on mode (later) ● APSR (application program status register) ○ r12 msp EPSR (exception program status register) ○ r13/ sp IPSR (interrupt program status register) ○ psp PRIMASK (later) ● r14 / lr CONTROL (later) ● r15 / pc

  4. sp 0x20000000 0x1ffffffc Stack pointer (sp/r13) 0x1ffffff8 0x1ffffff4 r7 = 0x42 Used for accessing stack memory via push ● and pop instructions sp = 0x20000000 Can be modified / accessed like any other ● reg via ldr, str, subs, adds , ... push r7 Either r13 or sp will work ● It is banked (later more) ● 0x42 0x20000000 Always word aligned, i.e. lowest two bits will ● sp always read 0 0x1ffffffc Stack is full descending ● 0x1ffffff8 0x1ffffff4 r7 = 0x42 sp = 0x1ffffffc

  5. Link Register (r14) & Program Counter (r15) Link register lr is used with subroutine calls Reading it will give current instruction + 4 ● ● and exceptions (later) (pipeline) During subroutine call (using bl / blx ), pc[0] should be zero, however bx/blx ● ● sequentially next pc value is loaded into lr require it to be set, to make sure we stay in lr[0] set indicates a return to Thumb state Thumb ● Some instructions need lr[0] to be set ●

  6. Combined Program Status Register 31 30 29 28 27:0 the xPSR is a combined register, where apsr ● is the application program status registers N Z C V reserved Bits 31:0 are the ALU flags ● Not ○ Zero ○ 31:6 5:0 Carry ○ Overflow ○ Exception The ipsr contains the exception number in ● reserved Number the lower bits 5:0 if 0, then thread mode (later) ○ The epsr is the exception program status ● 31:24 23:0 24 register All of them can be accessed with msr / mrs ● reserved T reserved instructions

  7. Calling convention r0-r3 are the argument and scratch ● registers r0-r1 are also the result registers ( r1 if ● result > word size) r4-r8 are callee-save registers (i.e. ● callee gotta restore them on return) r9 might be a callee-save register or ● not (on some variants of AAPCS it is a special register, ignore that) r10-r11 are callee-save registers ● r12-r15 are special registers ( r12 is ● intra procedure scratch register)

  8. PRIMASK PRIMASK ● If set no exceptions with ○ 31:1 0 programmable priority entered If not set, no effect ○ reserved PRIMASK

  9. CONTROL Only one privilege level in ● Cortex-M0 31:2 1 0 CONTROL[0] is reserved (priv. In ● M3) CONTROL[1] ● reserved if set sp = psp ○ if not set sp = msp ○ Using CONTROL[1] one can switch ● between psp and msp reserved Active stack

  10. Thumb State (overview) ● Two modes Handler ○ Handler mode mode Exception Exception ○ Thread mode sp == msp return return ● Handler mode always uses msp as stack ● Thread mode usage of CONTROL[1] = 0 stack depends on setting in control register ● After reset start out in Thread Thread Thread mode with msp Exception mode mode active sp == msp sp == psp ● Thread to Handler mode transition via Exception CONTROL[1] = 1 Reset

  11. Exceptions Event that changes program flow ● Suspends current code, run handler, resume ● (some) exceptions on Cortex-M0 have fixed priorities ● Reset ○ NMI ○ Hard Fault ○ some exceptions have programmable priority ● SysTick (later) ○ PendSV (later) ○ IRQs ○ 0 is the highest priority ● PRIMASK can be used to mask interrupts ● Interrupts can be pending ●

  12. Exceptions: Vectors Cortex-M0 processors support vectored exceptions ● Table contains addresses of handlers, processor fetches address on exception ● Processor jumps to correct handler, instead of having single handler ● Make sure to set a default one ●

  13. Exceptions (Stacking with MSP) Cortex-M(0) comes with hardware features ● that make dealing with exceptions easier sp sp xPSR xPSR On exception entry some registers are ● pc pc lr lr pushed onto the stack (depending on current r12 r12 mode) r3 r3 These registers form the Exception Context ● r2 r2 r0-r3, r12, lr, pc, xPSR ○ r1 r1 Stacking happens on current sp (example is if ● r0 r0 sp sp psp is not used) Makes nesting possible ● Handler Unstacking happens based on lr ● stacking unstacking Thread

  14. Exceptions (Stacking with PSP) msp sp sp msp == msp Cortex-M(0) comes with hardware features ● sp that make dealing with exceptions easier sp xPSR xPSR == On exception entry some registers are ● pc pc psp lr lr pushed onto the stack (depending on current r12 r12 mode) r3 r3 These registers form the Exception Context ● r2 r2 r0-r3, r12, lr, pc, xPSR ○ r1 r1 Stacking happens on current sp ● r0 r0 psp psp Makes nesting possible ● Unstacking happens based on lr ● Handler stacking unstacking Thread

  15. Exceptions (Tail Chaining) Speeds up exception ● Exception Exception servicing Return Return On completion, if there is ● a pending exception, unstacking is skipped Handler Handler A Handler B New handler runs ● stacking unstacking Thread Exception Exception A B

  16. Exceptions (Late arrival) Exception Exception If higher priority Return Return ● exception arrives before execution of handler, but Handler after stacking Handler B Handler a Stacking gets reused ● stacking unstacking Thread Exception Exception A B

  17. Exceptions (Nested) If higher priority ● exception arrives after Exception Exception handler starts Return Higher Prio handler ● preempted Exception Priority Afterwards lower priority ● Handler B Return handler finished stacking unstacking Handler Han dler A unstacking stacking Thread Exception Exception A B

  18. Exceptions (EXC_RETURN) Exception Stacking (nested) using psp Handler Depending on ● mode value of the lr sp == msp Stacking different paths using will be taken msp if lr ● 0xfffffffD 0xfffffff1 ○ 0xfffffff1 Unstacking unstacking Unstacking Exception using psp msp to using msp Return handler mode 0xfffffff9 ○ 0xfffffff9 Exception unstacking Exception msp to thread mode Thread Thread 0xfffffffd ○ mode mode unstacking sp == msp Setting / sp == psp psp to thread Clearing mode CONTROL[1]

  19. Startup code / Getting to main() On reset execution starts at reset vector ● Do a bunch of stuff before we can run ● ‘normal’ C Make sure we’re in the right state (MSP, ● Thumb, Privileged, …) Initialize bss section to zero ● Copy exception vectors to SRAM ● Copy initialized data section to SRAM ● e.g. global variables ○ Set initial stack pointer ● Jump to main() ●

  20. Getting to main() in Chromium-EC reset: ● After reset make sure CONTROL = 0 ldr r0,_ro_end movs r0, #0 ldr r1,_data_start msr control, r0 ● Write bss to zero ldr r2,_data_end isb ● Copy over vectors movs r0, #0 ● Set vector table to SRAM data_loop: ldr r1,_bss_start ldr r3, [r0] ● Copy initialized data ldr r2,_bss_end adds r0, #4 bss_loop: ● Go! str r3, [r1] str r0, [r1] adds r1, #4 adds r1, #4 cmp r1, r2 cmp r1, r2 blt data_loop blt bss_loop ldr r1, =vectors ldr r0, =stack_end ldr r2, =sram_vtable mov sp, r0 movs r0, #0 vtable_loop: bl main ldr r3, [r1] fini_loop: b fini_loop str r3, [r2] adds r1, #4 adds r2, #4 adds r0, #1 cmp r0, #48 blt vtable_loop movs r0, #3 ldr r1, =0x40010000 str r0, [r1]

  21. Multitasking - Context Switching General idea: Run tasks in a way that each of ● them can use processor exclusively As opposed to cooperative approaches, tasks ● don’t need to be aware of each other To make that work, each of them will need ● state, i.e. context to be restored O O O Task A Task B Task C As seen before, Context: registers + stack S S S ● OS decides who goes next ● time

  22. Multitasking - Stack layout OS Need one stack per task ● Stack Need one stack for OS ● OS stack needs to be large enough to deal ● with all Exceptions Task A Task doesn’t need to know it’s own stack ● Stack OS takes care of dealing with stack pointers ● Heap (malloc, free …) is optional ● Task B Stack Task C Stack Heap

  23. Multitasking - Systick Use a timer as periodic event source ● Tick Tick Tick Use these events to run scheduler ● Correct prioritization is required ● So common that ARM provides (optional) ● one in ARMv6 O O O Task A Task B Task C S S S time

Recommend


More recommend