Skip to content

Commit e3dc5e9

Browse files
committed
RISC-V tasks now context switching to each other using taskYIELD() - not fully tested yet.
1 parent 2bcb1ab commit e3dc5e9

File tree

3 files changed

+121
-106
lines changed

3 files changed

+121
-106
lines changed

FreeRTOS/Source/portable/GCC/RISC-V-RV32/port.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ static void prvTaskExitError( void );
5050

5151
/* Used to program the machine timer compare register. */
5252
static uint64_t ullNextTime = 0ULL;
53-
static volatile uint64_t * const pullMachineTimerCompareRegister = ( volatile uint64_t * const ) 0x2004000;
53+
static volatile uint64_t * const pullMachineTimerCompareRegister = ( volatile uint64_t * const ) ( configCTRL_BASE + 0x4000 );
5454

5555
/*-----------------------------------------------------------*/
5656

5757
void prvTaskExitError( void )
5858
{
5959
volatile uint32_t ulx = 0;
60-
#warning prvTaskExitError not used yet.
60+
6161
/* A function that implements a task must not exit or attempt to return to
6262
its caller as there is nothing to return to. If a task wants to exit it
6363
should instead call vTaskDelete( NULL ).
@@ -154,6 +154,8 @@ StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t px
154154
// pxTopOfStack--;
155155
// *pxTopOfStack = ( StackType_t ) 2; /* Stack pointer. */
156156
// pxTopOfStack--;
157+
*pxTopOfStack = ( StackType_t ) prvTaskExitError;
158+
pxTopOfStack--;
157159
*pxTopOfStack = ( StackType_t ) pxCode;
158160

159161
return pxTopOfStack;
@@ -163,8 +165,8 @@ StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t px
163165
void vPortSetupTimerInterrupt( void )
164166
{
165167
uint32_t ulCurrentTimeHigh, ulCurrentTimeLow;
166-
volatile uint32_t * const pulTimeHigh = ( volatile uint32_t * const ) 0x200BFF8;
167-
volatile uint32_t * const pulTimeLow = ( volatile uint32_t * const ) 0x200BFFc;
168+
volatile uint32_t * const pulTimeHigh = ( volatile uint32_t * const ) ( configCTRL_BASE + 0xBFF8 );
169+
volatile uint32_t * const pulTimeLow = ( volatile uint32_t * const ) ( configCTRL_BASE + 0xBFFc );
168170

169171
do
170172
{
@@ -185,9 +187,13 @@ volatile uint32_t * const pulTimeLow = ( volatile uint32_t * const ) 0x200BFFc;
185187

186188
void Software_IRQHandler( void )
187189
{
188-
vTaskSwitchContext();
189-
}
190+
volatile uint32_t * const ulSoftInterrupt = ( uint32_t * ) configCTRL_BASE;
190191

192+
vTaskSwitchContext();
191193

194+
/* Clear software interrupt. */
195+
*ulSoftInterrupt = 0UL;
196+
}
197+
/*-----------------------------------------------------------*/
192198

193199

FreeRTOS/Source/portable/GCC/RISC-V-RV32/portASM.S

Lines changed: 108 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -47,80 +47,83 @@
4747

4848
.align 8
4949
xPortStartScheduler:
50-
lw sp, pxCurrentTCB /* Load pxCurrentTCB. */
51-
lw sp, 0( sp ) /* Read sp from first TCB member. */
52-
lw x1, 0( sp )
53-
lw x5, 1 * WORD_SIZE( sp ) /* t0 */
54-
lw x6, 2 * WORD_SIZE( sp ) /* t1 */
55-
lw x7, 3 * WORD_SIZE( sp ) /* t2 */
56-
lw x8, 4 * WORD_SIZE( sp ) /* s0/fp */
57-
lw x9, 5 * WORD_SIZE( sp ) /* s1 */
58-
lw x10, 6 * WORD_SIZE( sp ) /* a0 */
59-
lw x11, 7 * WORD_SIZE( sp ) /* a1 */
60-
lw x12, 8 * WORD_SIZE( sp ) /* a2 */
61-
lw x13, 9 * WORD_SIZE( sp ) /* a3 */
62-
lw x14, 10 * WORD_SIZE( sp ) /* a4 */
63-
lw x15, 11 * WORD_SIZE( sp ) /* a5 */
64-
lw x16, 12 * WORD_SIZE( sp ) /* a6 */
65-
lw x17, 13 * WORD_SIZE( sp ) /* a7 */
66-
lw x18, 14 * WORD_SIZE( sp ) /* s2 */
67-
lw x19, 15 * WORD_SIZE( sp ) /* s3 */
68-
lw x20, 16 * WORD_SIZE( sp ) /* s4 */
69-
lw x21, 17 * WORD_SIZE( sp ) /* s5 */
70-
lw x22, 18 * WORD_SIZE( sp ) /* s6 */
71-
lw x23, 19 * WORD_SIZE( sp ) /* s7 */
72-
lw x24, 20 * WORD_SIZE( sp ) /* s8 */
73-
lw x25, 21 * WORD_SIZE( sp ) /* s9 */
74-
lw x26, 22 * WORD_SIZE( sp ) /* s10 */
75-
lw x27, 23 * WORD_SIZE( sp ) /* s11 */
76-
lw x28, 24 * WORD_SIZE( sp ) /* t3 */
77-
lw x29, 25 * WORD_SIZE( sp ) /* t4 */
78-
lw x30, 26 * WORD_SIZE( sp ) /* t5 */
79-
lw x31, 27 * WORD_SIZE( sp ) /* t6 */
80-
addi sp, sp, CONTEXT_SIZE
81-
csrs mie, 8 /* Enable soft interrupt. */
82-
csrs mstatus, 8 /* Enable interrupts. */
83-
ret
50+
lw sp, pxCurrentTCB /* Load pxCurrentTCB. */
51+
lw sp, 0( sp ) /* Read sp from first TCB member. */
52+
53+
lw x1, 0( sp ) /* Note for starting the scheduler the exception return address is used as the function return address. */
54+
lw x5, 2 * WORD_SIZE( sp ) /* t0 */
55+
lw x6, 3 * WORD_SIZE( sp ) /* t1 */
56+
lw x7, 4 * WORD_SIZE( sp ) /* t2 */
57+
lw x8, 5 * WORD_SIZE( sp ) /* s0/fp */
58+
lw x9, 6 * WORD_SIZE( sp ) /* s1 */
59+
lw x10, 7 * WORD_SIZE( sp ) /* a0 */
60+
lw x11, 8 * WORD_SIZE( sp ) /* a1 */
61+
lw x12, 9 * WORD_SIZE( sp ) /* a2 */
62+
lw x13, 10 * WORD_SIZE( sp ) /* a3 */
63+
lw x14, 11 * WORD_SIZE( sp ) /* a4 */
64+
lw x15, 12 * WORD_SIZE( sp ) /* a5 */
65+
lw x16, 13 * WORD_SIZE( sp ) /* a6 */
66+
lw x17, 14 * WORD_SIZE( sp ) /* a7 */
67+
lw x18, 15 * WORD_SIZE( sp ) /* s2 */
68+
lw x19, 16 * WORD_SIZE( sp ) /* s3 */
69+
lw x20, 17 * WORD_SIZE( sp ) /* s4 */
70+
lw x21, 18 * WORD_SIZE( sp ) /* s5 */
71+
lw x22, 19 * WORD_SIZE( sp ) /* s6 */
72+
lw x23, 20 * WORD_SIZE( sp ) /* s7 */
73+
lw x24, 21 * WORD_SIZE( sp ) /* s8 */
74+
lw x25, 22 * WORD_SIZE( sp ) /* s9 */
75+
lw x26, 23 * WORD_SIZE( sp ) /* s10 */
76+
lw x27, 24 * WORD_SIZE( sp ) /* s11 */
77+
lw x28, 25 * WORD_SIZE( sp ) /* t3 */
78+
lw x29, 26 * WORD_SIZE( sp ) /* t4 */
79+
lw x30, 27 * WORD_SIZE( sp ) /* t5 */
80+
lw x31, 28 * WORD_SIZE( sp ) /* t6 */
81+
addi sp, sp, CONTEXT_SIZE
82+
csrs mstatus, 8 /* Enable machine interrupts. */
83+
csrs mie, 8 /* Enable soft interrupt. */
84+
ret
8485

8586
/*-----------------------------------------------------------*/
8687

8788
.align 8
8889
vPortTrapHandler:
8990
addi sp, sp, -CONTEXT_SIZE
90-
sw x1, 0( sp )
91-
sw x5, 1 * WORD_SIZE( sp )
92-
sw x6, 2 * WORD_SIZE( sp )
93-
sw x7, 3 * WORD_SIZE( sp )
94-
sw x8, 4 * WORD_SIZE( sp )
95-
sw x9, 5 * WORD_SIZE( sp )
96-
sw x10, 6 * WORD_SIZE( sp )
97-
sw x11, 7 * WORD_SIZE( sp )
98-
sw x12, 8 * WORD_SIZE( sp )
99-
sw x13, 9 * WORD_SIZE( sp )
100-
sw x14, 10 * WORD_SIZE( sp )
101-
sw x15, 11 * WORD_SIZE( sp )
102-
sw x16, 12 * WORD_SIZE( sp )
103-
sw x17, 13 * WORD_SIZE( sp )
104-
sw x18, 14 * WORD_SIZE( sp )
105-
sw x19, 15 * WORD_SIZE( sp )
106-
sw x20, 16 * WORD_SIZE( sp )
107-
sw x21, 17 * WORD_SIZE( sp )
108-
sw x22, 18 * WORD_SIZE( sp )
109-
sw x23, 19 * WORD_SIZE( sp )
110-
sw x24, 20 * WORD_SIZE( sp )
111-
sw x25, 21 * WORD_SIZE( sp )
112-
sw x26, 22 * WORD_SIZE( sp )
113-
sw x27, 23 * WORD_SIZE( sp )
114-
sw x28, 24 * WORD_SIZE( sp )
115-
sw x29, 25 * WORD_SIZE( sp )
116-
sw x30, 26 * WORD_SIZE( sp )
117-
sw x31, 27 * WORD_SIZE( sp )
91+
sw x1, 1( sp )
92+
sw x5, 2 * WORD_SIZE( sp )
93+
sw x6, 3 * WORD_SIZE( sp )
94+
sw x7, 4 * WORD_SIZE( sp )
95+
sw x8, 5 * WORD_SIZE( sp )
96+
sw x9, 6 * WORD_SIZE( sp )
97+
sw x10, 7 * WORD_SIZE( sp )
98+
sw x11, 8 * WORD_SIZE( sp )
99+
sw x12, 9 * WORD_SIZE( sp )
100+
sw x13, 10 * WORD_SIZE( sp )
101+
sw x14, 11 * WORD_SIZE( sp )
102+
sw x15, 12 * WORD_SIZE( sp )
103+
sw x16, 13 * WORD_SIZE( sp )
104+
sw x17, 14 * WORD_SIZE( sp )
105+
sw x18, 15 * WORD_SIZE( sp )
106+
sw x19, 16 * WORD_SIZE( sp )
107+
sw x20, 17 * WORD_SIZE( sp )
108+
sw x21, 18 * WORD_SIZE( sp )
109+
sw x22, 19 * WORD_SIZE( sp )
110+
sw x23, 20 * WORD_SIZE( sp )
111+
sw x24, 21 * WORD_SIZE( sp )
112+
sw x25, 22 * WORD_SIZE( sp )
113+
sw x26, 23 * WORD_SIZE( sp )
114+
sw x27, 24 * WORD_SIZE( sp )
115+
sw x28, 25 * WORD_SIZE( sp )
116+
sw x29, 26 * WORD_SIZE( sp )
117+
sw x30, 27 * WORD_SIZE( sp )
118+
sw x31, 28 * WORD_SIZE( sp )
119+
120+
/* Save exception return address. */
121+
csrr t0, mepc
122+
sw t0, 0( sp )
123+
118124
lw t0, pxCurrentTCB /* Load pxCurrentTCB. */
119125
sw sp, 0( t0 ) /* Write sp from first TCB member. */
120126

121-
csrr t0, mepc
122-
sw t0, 31 * WORD_SIZE( sp )
123-
124127
csrr a0, mcause
125128
csrr a1, mepc
126129
mv a2, sp
@@ -131,37 +134,43 @@ vPortTrapHandler:
131134
li t0, 0x00001800 /* MSTATUS MPP */
132135
csrs mstatus, t0
133136

134-
/* Cut and past restore code from xPortStartScheduler - can be made a macro
135-
but that makes debugging harder. */
136-
lw sp, pxCurrentTCB /* Load pxCurrentTCB. */
137-
lw sp, 0( sp ) /* Read sp from first TCB member. */
138-
lw x1, 0( sp )
139-
lw x5, 1 * WORD_SIZE( sp ) /* t0 */
140-
lw x6, 2 * WORD_SIZE( sp ) /* t1 */
141-
lw x7, 3 * WORD_SIZE( sp ) /* t2 */
142-
lw x8, 4 * WORD_SIZE( sp ) /* s0/fp */
143-
lw x9, 5 * WORD_SIZE( sp ) /* s1 */
144-
lw x10, 6 * WORD_SIZE( sp ) /* a0 */
145-
lw x11, 7 * WORD_SIZE( sp ) /* a1 */
146-
lw x12, 8 * WORD_SIZE( sp ) /* a2 */
147-
lw x13, 9 * WORD_SIZE( sp ) /* a3 */
148-
lw x14, 10 * WORD_SIZE( sp ) /* a4 */
149-
lw x15, 11 * WORD_SIZE( sp ) /* a5 */
150-
lw x16, 12 * WORD_SIZE( sp ) /* a6 */
151-
lw x17, 13 * WORD_SIZE( sp ) /* a7 */
152-
lw x18, 14 * WORD_SIZE( sp ) /* s2 */
153-
lw x19, 15 * WORD_SIZE( sp ) /* s3 */
154-
lw x20, 16 * WORD_SIZE( sp ) /* s4 */
155-
lw x21, 17 * WORD_SIZE( sp ) /* s5 */
156-
lw x22, 18 * WORD_SIZE( sp ) /* s6 */
157-
lw x23, 19 * WORD_SIZE( sp ) /* s7 */
158-
lw x24, 20 * WORD_SIZE( sp ) /* s8 */
159-
lw x25, 21 * WORD_SIZE( sp ) /* s9 */
160-
lw x26, 22 * WORD_SIZE( sp ) /* s10 */
161-
lw x27, 23 * WORD_SIZE( sp ) /* s11 */
162-
lw x28, 24 * WORD_SIZE( sp ) /* t3 */
163-
lw x29, 25 * WORD_SIZE( sp ) /* t4 */
164-
lw x30, 26 * WORD_SIZE( sp ) /* t5 */
165-
lw x31, 27 * WORD_SIZE( sp ) /* t6 */
166-
addi sp, sp, CONTEXT_SIZE
167-
mret
137+
lw sp, pxCurrentTCB /* Load pxCurrentTCB. */
138+
lw sp, 0( sp ) /* Read sp from first TCB member. */
139+
140+
/* Load mret with the address of the first task. */
141+
lw t0, 0( sp )
142+
csrw mepc, t0
143+
144+
lw x1, 1( sp )
145+
lw x5, 2 * WORD_SIZE( sp ) /* t0 */
146+
lw x6, 3 * WORD_SIZE( sp ) /* t1 */
147+
lw x7, 4 * WORD_SIZE( sp ) /* t2 */
148+
lw x8, 5 * WORD_SIZE( sp ) /* s0/fp */
149+
lw x9, 6 * WORD_SIZE( sp ) /* s1 */
150+
lw x10, 7 * WORD_SIZE( sp ) /* a0 */
151+
lw x11, 8 * WORD_SIZE( sp ) /* a1 */
152+
lw x12, 9 * WORD_SIZE( sp ) /* a2 */
153+
lw x13, 10 * WORD_SIZE( sp ) /* a3 */
154+
lw x14, 11 * WORD_SIZE( sp ) /* a4 */
155+
lw x15, 12 * WORD_SIZE( sp ) /* a5 */
156+
lw x16, 13 * WORD_SIZE( sp ) /* a6 */
157+
lw x17, 14 * WORD_SIZE( sp ) /* a7 */
158+
lw x18, 15 * WORD_SIZE( sp ) /* s2 */
159+
lw x19, 16 * WORD_SIZE( sp ) /* s3 */
160+
lw x20, 17 * WORD_SIZE( sp ) /* s4 */
161+
lw x21, 18 * WORD_SIZE( sp ) /* s5 */
162+
lw x22, 19 * WORD_SIZE( sp ) /* s6 */
163+
lw x23, 20 * WORD_SIZE( sp ) /* s7 */
164+
lw x24, 21 * WORD_SIZE( sp ) /* s8 */
165+
lw x25, 22 * WORD_SIZE( sp ) /* s9 */
166+
lw x26, 23 * WORD_SIZE( sp ) /* s10 */
167+
lw x27, 24 * WORD_SIZE( sp ) /* s11 */
168+
lw x28, 25 * WORD_SIZE( sp ) /* t3 */
169+
lw x29, 26 * WORD_SIZE( sp ) /* t4 */
170+
lw x30, 27 * WORD_SIZE( sp ) /* t5 */
171+
lw x31, 28 * WORD_SIZE( sp ) /* t6 */
172+
addi sp, sp, CONTEXT_SIZE
173+
174+
mret
175+
176+

FreeRTOS/Source/portable/GCC/RISC-V-RV32/portmacro.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ not need to be guarded with a critical section. */
7070

7171

7272
/* Scheduler utilities. */
73-
#define portYIELD() { volatile uint32_t * const ulSoftInterrupt = ( uint32_t * ) 0x2000000; *ulSoftInterrupt = 1UL; }
73+
#define portYIELD() { volatile uint32_t * const ulSoftInterrupt = ( uint32_t * ) configCTRL_BASE; *ulSoftInterrupt = 1UL; }
7474
#define portEND_SWITCHING_ISR( xSwitchRequired ) if( xSwitchRequired ) vPortYield()
7575
#define portYIELD_FROM_ISR( x ) portEND_SWITCHING_ISR( x )
7676
/*-----------------------------------------------------------*/

0 commit comments

Comments
 (0)