rp2040

RP2040 Programming without SDK
Log | Files | Refs

commit c49d611c72bb8695818395f8fde47c1dbd5dbaa7
parent 8a347c5c0da05100a62c21f461cd4cc4e0e343ab
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Mon, 18 Mar 2024 13:02:55 +0900

contex switch, wip

Diffstat:
Mex3/main.c | 35++++++++++++++++-------------------
Mex3/proc.s | 8+++++---
2 files changed, 21 insertions(+), 22 deletions(-)

diff --git a/ex3/main.c b/ex3/main.c @@ -63,24 +63,9 @@ main(void) { p1.r[15] = 0x20000101; // pc p1.next = NULL; - for (;;) { - wfi(); - for (int i = 0; i < 17; i++) { - printh(ptab.head->r[i]); - puts(""); - } - } - return 0; -} + proc0(); -// Save_context saves the context starting at sp to the current proc struct. -void -save_context(unsigned int r[17]) -{ - int i; - for (i = 0; i < 17; i++) { - ptab.head->r[i] = r[i]; - } + return 0; } void @@ -88,15 +73,27 @@ scheduler(void) { puts("scheduler called"); } -void -switch_context(void) { +// Switch_context saves registers r to the currently executing proc struct +// and then move it to the tail of task queue. +// It returns the next PC of the next task in the queue +unsigned int +switch_context(unsigned int r[17]) { proc *p; + + // save context + int i; + for (i = 0; i < 17; i++) { + ptab.head->r[i] = r[i]; + } + // update task queue p = ptab.head; ptab.head = ptab.head->next; ptab.tail->next = p; ptab.tail = p; ptab.tail->next = NULL; restore_context(ptab.head->r); + + return ptab.head->r[15]; } void diff --git a/ex3/proc.s b/ex3/proc.s @@ -4,6 +4,7 @@ proc0: ldr r0, =msg0 bl puts + wfi b proc0 .align 2 @@ -16,6 +17,7 @@ msg0: proc1: ldr r0, =msg1 bl puts + wfi b proc1 .align 2 @@ -99,8 +101,6 @@ isr_alarm: add r3, r3, #4 ldr r0, [r1, r2] str r0, [r1, r3] // ReturnAddress(ExceptionType): The next pc at interrupt. - ldr r0, =0x20000101 // The entry point of proc1 - str r0, [r1, r2] add r2, r2, #4 add r3, r3, #4 @@ -108,7 +108,9 @@ isr_alarm: str r0, [r1, r3] mov r0, r1 - bl save_context + bl switch_context + sub r2, r2, #4 + str r0, [r1, r2] add sp, sp, #(17*4) ldr r0, =(1000 * 1000)