Skip to content

Commit 50013cf

Browse files
committed
riscv: add generic way to detect if inside interrupt context
1 parent 8be86d4 commit 50013cf

4 files changed

Lines changed: 33 additions & 2 deletions

File tree

portable/GCC/RISC-V/port.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ volatile uint64_t * pullMachineTimerCompareRegister = NULL;
9797
size_t xCriticalNesting = ( size_t ) 0xaaaaaaaa;
9898
size_t * pxCriticalNesting = &xCriticalNesting;
9999

100+
/*
101+
* Counts the interrupt nesting depth.
102+
*/
103+
size_t xInterruptNesting = 0;
104+
size_t * pxInterruptNesting = &xInterruptNesting;
105+
100106
/* Used to catch tasks that attempt to return from their implementing function. */
101107
size_t xTaskReturnAddress = ( size_t ) portTASK_RETURN_ADDRESS;
102108

@@ -205,3 +211,9 @@ void vPortEndScheduler( void )
205211
}
206212
}
207213
/*-----------------------------------------------------------*/
214+
215+
BaseType_t xPortIsInsideInterrupt( void )
216+
{
217+
return ( xInterruptNesting == 0 ) ? pdFALSE : pdTRUE;
218+
}
219+
/*-----------------------------------------------------------*/

portable/GCC/RISC-V/portASM.S

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ definitions. */
162162
* where the global and thread pointers are currently assumed to be constant so
163163
* are not saved:
164164
*
165+
* xInterruptNesting
165166
* xCriticalNesting
166167
* x31
167168
* x30
@@ -198,6 +199,8 @@ definitions. */
198199
* pxCode
199200
*/
200201
pxPortInitialiseStack:
202+
addi a0, a0, -portWORD_SIZE /* Space for interrupt nesting count. */
203+
201204
addi a0, a0, -portWORD_SIZE /* Space for critical nesting count. */
202205
store_x x0, 0(a0) /* Critical nesting count starts at 0 for every task. */
203206

portable/GCC/RISC-V/portContext.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,13 @@
5757
* specific version of freertos_risc_v_chip_specific_extensions.h. See the
5858
* notes at the top of portASM.S file. */
5959
#ifdef __riscv_32e
60-
#define portCONTEXT_SIZE ( 15 * portWORD_SIZE )
60+
#define portCONTEXT_SIZE ( 16 * portWORD_SIZE )
6161
#define portCRITICAL_NESTING_OFFSET 14
62+
#define portINTERRUPT_NESTING_OFFSET 15
6263
#else
63-
#define portCONTEXT_SIZE ( 31 * portWORD_SIZE )
64+
#define portCONTEXT_SIZE ( 32 * portWORD_SIZE )
6465
#define portCRITICAL_NESTING_OFFSET 30
66+
#define portINTERRUPT_NESTING_OFFSET 31
6567
#endif
6668

6769
#if ( configENABLE_FPU == 1 )
@@ -122,6 +124,8 @@
122124
.extern xISRStackTop
123125
.extern xCriticalNesting
124126
.extern pxCriticalNesting
127+
.extern xInterruptNesting
128+
.extern pxInterruptNesting
125129
/*-----------------------------------------------------------*/
126130

127131
.macro portcontexSAVE_FPU_CONTEXT
@@ -306,6 +310,12 @@ store_x x15, 13 * portWORD_SIZE( sp )
306310
load_x t0, xCriticalNesting /* Load the value of xCriticalNesting into t0. */
307311
store_x t0, portCRITICAL_NESTING_OFFSET * portWORD_SIZE( sp ) /* Store the critical nesting value to the stack. */
308312

313+
load_x t0, xInterruptNesting /* Load the value of xInterruptNesting into t0. */
314+
store_x t0, portINTERRUPT_NESTING_OFFSET * portWORD_SIZE( sp ) /* Store the interrupt nesting value to the stack. */
315+
addi t0, t0, 1
316+
load_x t1, pxInterruptNesting /* Load the address of xInterruptNesting into t1. */
317+
store_x t0, 0( t1 ) /* Increment xInterruptNesting by 1 */
318+
309319
#if( configENABLE_FPU == 1 )
310320
csrr t0, mstatus
311321
srl t1, t0, MSTATUS_FS_OFFSET
@@ -425,6 +435,10 @@ csrw mstatus, t3
425435
6:
426436
#endif /* ifdef portasmSTORE_FPU_CONTEXT */
427437

438+
load_x t0, portINTERRUPT_NESTING_OFFSET * portWORD_SIZE( sp ) /* Obtain saved xInterruptNesting value from task's stack. */
439+
load_x t1, pxInterruptNesting /* Load the address of xInterruptNesting into t1. */
440+
store_x t0, 0 ( t1 ) /* Restore the interrupt nesting value. */
441+
428442
load_x t0, portCRITICAL_NESTING_OFFSET * portWORD_SIZE( sp ) /* Obtain xCriticalNesting value for this task from task's stack. */
429443
load_x t1, pxCriticalNesting /* Load the address of xCriticalNesting into t1. */
430444
store_x t0, 0 ( t1 ) /* Restore the critical nesting value for this task. */

portable/GCC/RISC-V/portmacro.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ extern void vTaskSwitchContext( void );
106106
} \
107107
} while( 0 )
108108
#define portYIELD_FROM_ISR( x ) portEND_SWITCHING_ISR( x )
109+
110+
BaseType_t xPortIsInsideInterrupt( void );
109111
/*-----------------------------------------------------------*/
110112

111113
/* Critical section management. */

0 commit comments

Comments
 (0)