11/*
2- * Copyright (c) 2021-2022, RT-Thread Development Team
2+ * Copyright (c) 2021-2024 RT-Thread Development Team
33 *
44 * SPDX-License-Identifier: Apache-2.0
55 *
66 * Change Logs:
77 * Date Author Notes
88 * 2022-06-02 supperthomas first version
9+ * 2024-12-08 wumingzi support rt_hw_us_delay
910 */
1011
1112#include <rtthread.h>
13+ #include "rttypes.h"
1214#include "hal/systimer_hal.h"
1315#include "hal/systimer_ll.h"
1416#include "esp_private/panic_internal.h"
1517#include "esp_private/systimer.h"
1618#include "esp_private/periph_ctrl.h"
1719#include "esp_intr_alloc.h"
1820#include "esp_attr.h"
21+ #include "esp_timer.h"
22+ #include "driver/gptimer.h"
1923
2024static systimer_hal_context_t systimer_hal ;
2125IRAM_ATTR void rt_SysTickIsrHandler (void * arg )
@@ -57,3 +61,31 @@ void rt_hw_board_init(void)
5761 rt_console_set_device (RT_CONSOLE_DEVICE_NAME );
5862#endif
5963}
64+
65+ static gptimer_handle_t gptimer_hw_us = NULL ;
66+
67+ static int delay_us_init (void )
68+ {
69+ gptimer_config_t timer_config = {
70+ .clk_src = GPTIMER_CLK_SRC_DEFAULT ,
71+ .direction = GPTIMER_COUNT_UP ,
72+ .resolution_hz = 1 * 1000 * 1000 , /* 1MHz, 1 tick = 1us*/
73+ };
74+ ESP_ERROR_CHECK (gptimer_new_timer (& timer_config , & gptimer_hw_us ));
75+ ESP_ERROR_CHECK (gptimer_enable (gptimer_hw_us ));
76+ return RT_EOK ;
77+ }
78+ INIT_DEVICE_EXPORT (delay_us_init );
79+
80+ void rt_hw_us_delay (rt_uint32_t us )
81+ {
82+ uint64_t count = 0 ;
83+ ESP_ERROR_CHECK (gptimer_start (gptimer_hw_us ));
84+ ESP_ERROR_CHECK (gptimer_set_raw_count (gptimer_hw_us , 0 ));
85+ /* Retrieve the timestamp at anytime*/
86+ while (count < (uint64_t )us )
87+ {
88+ ESP_ERROR_CHECK (gptimer_get_raw_count (gptimer_hw_us , & count ));
89+ }
90+ ESP_ERROR_CHECK (gptimer_stop (gptimer_hw_us ));
91+ }
0 commit comments