Merge branch 'fix/ulp_riscv_delay_cycles_v5.5' into 'release/v5.5'

fix(ulp_riscv): Fix ulp_riscv_delay_cycles() to handle small delays correctly (v5.5)

See merge request espressif/esp-idf!41941
This commit is contained in:
Jiang Jiang Jian
2025-09-28 18:52:09 +08:00

View File

@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -92,15 +92,19 @@ void ulp_riscv_timer_resume(void);
/**
* @brief Makes the co-processor busy wait for a certain number of cycles
*
* @note This function is not accurate for delays shorter than 20 cycles,
* as the function's own overhead may exceed the requested delay.
*
* @param cycles Number of cycles to busy wait
*/
void static inline ulp_riscv_delay_cycles(uint32_t cycles)
{
uint32_t start = ULP_RISCV_GET_CCOUNT();
/* Off with an estimate of cycles in this function to improve accuracy */
uint32_t end = start + cycles - 20;
while (ULP_RISCV_GET_CCOUNT() < end) {
if (cycles <= 20) { // the estimate of cycles for this function
return;
}
/* Off with the estimate of cycles to improve accuracy */
uint32_t end = ULP_RISCV_GET_CCOUNT() + cycles - 20;
while (ULP_RISCV_GET_CCOUNT() < end) {
/* Wait */
}
}