fix(ulp_riscv): Fix a delay function to handle small delays correctly

Fixed ulp_riscv_delay_cycles function

Closes https://github.com/espressif/esp-idf/issues/16891
This commit is contained in:
Konstantin Kondrashov
2025-07-18 14:19:24 +03:00
parent a0f071f7ff
commit e9cae6c805

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 * 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 * @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 * @param cycles Number of cycles to busy wait
*/ */
void static inline ulp_riscv_delay_cycles(uint32_t cycles) void static inline ulp_riscv_delay_cycles(uint32_t cycles)
{ {
uint32_t start = ULP_RISCV_GET_CCOUNT(); if (cycles <= 20) { // the estimate of cycles for this function
/* Off with an estimate of cycles in this function to improve accuracy */ return;
uint32_t end = start + cycles - 20; }
/* Off with the estimate of cycles to improve accuracy */
while (ULP_RISCV_GET_CCOUNT() < end) { uint32_t end = ULP_RISCV_GET_CCOUNT() + cycles - 20;
while (ULP_RISCV_GET_CCOUNT() < end) {
/* Wait */ /* Wait */
} }
} }