diff --git a/components/esp8266/include/esp8266/rom_functions.h b/components/esp8266/include/esp8266/rom_functions.h index c1f20370..6aeacb2b 100644 --- a/components/esp8266/include/esp8266/rom_functions.h +++ b/components/esp8266/include/esp8266/rom_functions.h @@ -17,7 +17,6 @@ uint32_t Wait_SPI_Idle(); void uart_div_modify(uint32_t uart_no, uint32_t baud_div); -void ets_delay_us(uint32_t us); int ets_io_vprintf(int (*putc)(int), const char* fmt, va_list ap); void system_soft_wdt_feed(); diff --git a/components/esp8266/include/rom/ets_sys.h b/components/esp8266/include/rom/ets_sys.h index 4d176d92..b5cbf0a5 100644 --- a/components/esp8266/include/rom/ets_sys.h +++ b/components/esp8266/include/rom/ets_sys.h @@ -97,6 +97,16 @@ extern uint32_t WDEV_INTEREST_EVENT; */ void os_delay_us(uint16_t us); +/** + * @brief CPU do while loop for some time. + * In FreeRTOS task, please call FreeRTOS apis. + * + * @param uint32_t us : Delay time in us. + * + * @return None + */ +void ets_delay_us(uint32_t us); + /** * @brief Register the print output function. * diff --git a/components/newlib/newlib/port/time.c b/components/newlib/newlib/port/time.c index fc65f33c..59684d09 100644 --- a/components/newlib/newlib/port/time.c +++ b/components/newlib/newlib/port/time.c @@ -19,6 +19,7 @@ #include "esp_system.h" #include "esp_timer.h" +#include "rom/ets_sys.h" #include "FreeRTOS.h" #include "task.h" @@ -141,3 +142,26 @@ clock_t _times_r(struct _reent *r, struct tms *tms) return 0; } + +int usleep(useconds_t us) +{ + const int us_per_tick = portTICK_PERIOD_MS * 1000; + + if (us < us_per_tick) { + ets_delay_us((uint32_t) us); + } else { + /* since vTaskDelay(1) blocks for anywhere between 0 and portTICK_PERIOD_MS, + * round up to compensate. + */ + vTaskDelay((us + us_per_tick - 1) / us_per_tick); + } + + return 0; +} + +unsigned int sleep(unsigned int seconds) +{ + vTaskDelay(seconds * (1000 / portTICK_PERIOD_MS)); + + return 0; +}