From 2fe3c61c267e058939b5197f2c4459627ddadcac Mon Sep 17 00:00:00 2001 From: Hesham Almatary Date: Wed, 18 Feb 2026 07:37:52 +0000 Subject: [PATCH] RISC-V: add configMTIME_INIT_IN_BSP Add a new configMTIME_INIT_IN_BSP configuration macro to allow BSPs and demo applications to initialise the MMIO MTIME pointers within the CLINT. This enables BSPs to provide custom addresses or properly typed pointer values, rather than relying on plain integer constants coming from macros as is currently the case. This change does not affect existing assumptions and usage, but allows architectures that differentiate between integers and pointers to work. Signed-off-by: Hesham Almatary --- portable/GCC/RISC-V/port.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/portable/GCC/RISC-V/port.c b/portable/GCC/RISC-V/port.c index 44f6ab384..2892e6692 100644 --- a/portable/GCC/RISC-V/port.c +++ b/portable/GCC/RISC-V/port.c @@ -93,6 +93,9 @@ const size_t uxTimerIncrementsForOneTick = ( size_t ) ( ( configCPU_CLOCK_HZ ) / UBaseType_t const ullMachineTimerCompareRegisterBase = configMTIMECMP_BASE_ADDRESS; volatile uint64_t * pullMachineTimerCompareRegister = NULL; +volatile uint32_t * pulTimeHigh = ( volatile uint32_t * const ) ( ( configMTIME_BASE_ADDRESS ) + 4UL ); /* 8-byte type so high 32-bit word is 4 bytes up. */ +volatile uint32_t * pulTimeLow = ( volatile uint32_t * const ) ( configMTIME_BASE_ADDRESS ); + /* Holds the critical nesting value - deliberately non-zero at start up to * ensure interrupts are not accidentally enabled before the scheduler starts. */ size_t xCriticalNesting = ( size_t ) 0xaaaaaaaa; @@ -130,13 +133,13 @@ ReturnFunctionType_t xTaskReturnAddress = ( ReturnFunctionType_t ) portTASK_RETU void vPortSetupTimerInterrupt( void ) { uint32_t ulCurrentTimeHigh, ulCurrentTimeLow; - volatile uint32_t * const pulTimeHigh = ( volatile uint32_t * const ) ( ( configMTIME_BASE_ADDRESS ) + 4UL ); /* 8-byte type so high 32-bit word is 4 bytes up. */ - volatile uint32_t * const pulTimeLow = ( volatile uint32_t * const ) ( configMTIME_BASE_ADDRESS ); - volatile uint32_t ulHartId; - - __asm volatile ( "csrr %0, mhartid" : "=r" ( ulHartId ) ); - - pullMachineTimerCompareRegister = ( volatile uint64_t * ) ( ullMachineTimerCompareRegisterBase + ( ulHartId * sizeof( uint64_t ) ) ); + #ifndef configMTIME_INIT_IN_BSP + { + volatile uint32_t ulHartId; + __asm volatile ( "csrr %0, mhartid" : "=r" ( ulHartId ) ); + pullMachineTimerCompareRegister = ( volatile uint64_t * ) ( ullMachineTimerCompareRegisterBase + ( ulHartId * sizeof( uint64_t ) ) ); + } + #endif do {