1
1
/*
2
- * Copyright (c) 2021-2022, RT-Thread Development Team
2
+ * Copyright (c) 2021-2024 RT-Thread Development Team
3
3
*
4
4
* SPDX-License-Identifier: Apache-2.0
5
5
*
6
6
* Change Logs:
7
7
* Date Author Notes
8
8
* 2022-06-02 supperthomas first version
9
+ * 2024-12-08 wumingzi support rt_hw_us_delay
9
10
*/
10
11
11
12
#include <rtthread.h>
13
+ #include "rttypes.h"
12
14
#include "hal/systimer_hal.h"
13
15
#include "hal/systimer_ll.h"
14
16
#include "esp_private/panic_internal.h"
15
17
#include "esp_private/systimer.h"
16
18
#include "esp_private/periph_ctrl.h"
17
19
#include "esp_intr_alloc.h"
18
20
#include "esp_attr.h"
21
+ #include "esp_timer.h"
22
+ #include "driver/gptimer.h"
19
23
20
24
static systimer_hal_context_t systimer_hal ;
21
25
IRAM_ATTR void rt_SysTickIsrHandler (void * arg )
@@ -57,3 +61,31 @@ void rt_hw_board_init(void)
57
61
rt_console_set_device (RT_CONSOLE_DEVICE_NAME );
58
62
#endif
59
63
}
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