From 12940c9e3507420f3cb4c2366db15775cb389489 Mon Sep 17 00:00:00 2001 From: dnguye14-adi Date: Fri, 11 Apr 2025 14:53:22 +0700 Subject: [PATCH] fix(PeriphDrivers): MSDK-1287: Fix MAX32655 MXC_Delay on RISC-V shows different behavior when not compiling with -O0 Signed-off-by: Dung Nguyen --- Libraries/PeriphDrivers/Source/SYS/mxc_delay.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Libraries/PeriphDrivers/Source/SYS/mxc_delay.c b/Libraries/PeriphDrivers/Source/SYS/mxc_delay.c index 363ce72f995..6da702ada12 100644 --- a/Libraries/PeriphDrivers/Source/SYS/mxc_delay.c +++ b/Libraries/PeriphDrivers/Source/SYS/mxc_delay.c @@ -42,8 +42,19 @@ int MXC_Delay(uint32_t us) CSR_SetPCER(1); // Enable counting of cycles CSR_SetPCMR(3); // Turn on counter + // define a ticks num for a delay less than 1 us but long enough to prevent the PCCR + uint32_t num_ticks_half_us = MXC_SYS_RiscVClockRate() / 1000000 / 3; + volatile uint32_t counter; + while (CSR_GetPCCR() < ticks) { - // Wait for counter to reach the tick count. + // Wait about 1 us + // and re-check if the counter reaches the tick count or not. + // Need this waiting to prevent CPU bothers PCCR too much + // to avoid PCCR hardware cannot maintain the correct timing. + counter = 0; + while (counter < num_ticks_half_us) { + counter++; + } } return E_NO_ERROR; }