1
6
7
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
42
43
44
45
46
49
55
56
57
58
59
60
64
65
66
67
68
77
78
79
80
81
84
85
86
87
88
89
90
94
95
96
97
98
99
100
101
102
103
119
/* ... */
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "esp_private/esp_clk.h"
#include "driver/mcpwm_cap.h"
#include "driver/gpio.h"6 includes
const static char *TAG = "example";
#define HC_SR04_TRIG_GPIO 0
#define HC_SR04_ECHO_GPIO 2
static bool hc_sr04_echo_callback(mcpwm_cap_channel_handle_t cap_chan, const mcpwm_capture_event_data_t *edata, void *user_data)
{
static uint32_t cap_val_begin_of_sample = 0;
static uint32_t cap_val_end_of_sample = 0;
TaskHandle_t task_to_notify = (TaskHandle_t)user_data;
BaseType_t high_task_wakeup = pdFALSE;
if (edata->cap_edge == MCPWM_CAP_EDGE_POS) {
cap_val_begin_of_sample = edata->cap_value;
cap_val_end_of_sample = cap_val_begin_of_sample;
}{...} else {
cap_val_end_of_sample = edata->cap_value;
uint32_t tof_ticks = cap_val_end_of_sample - cap_val_begin_of_sample;
xTaskNotifyFromISR(task_to_notify, tof_ticks, eSetValueWithOverwrite, &high_task_wakeup);
}{...}
return high_task_wakeup == pdTRUE;
}{ ... }
/* ... */
static void gen_trig_output(void)
{
gpio_set_level(HC_SR04_TRIG_GPIO, 1);
esp_rom_delay_us(10);
gpio_set_level(HC_SR04_TRIG_GPIO, 0);
}{ ... }
void app_main(void)
{
ESP_LOGI(TAG, "Install capture timer");
mcpwm_cap_timer_handle_t cap_timer = NULL;
mcpwm_capture_timer_config_t cap_conf = {
.clk_src = MCPWM_CAPTURE_CLK_SRC_DEFAULT,
.group_id = 0,
}{...};
ESP_ERROR_CHECK(mcpwm_new_capture_timer(&cap_conf, &cap_timer));
ESP_LOGI(TAG, "Install capture channel");
mcpwm_cap_channel_handle_t cap_chan = NULL;
mcpwm_capture_channel_config_t cap_ch_conf = {
.gpio_num = HC_SR04_ECHO_GPIO,
.prescale = 1,
.flags.neg_edge = true,
.flags.pos_edge = true,
.flags.pull_up = true,
}{...};
ESP_ERROR_CHECK(mcpwm_new_capture_channel(cap_timer, &cap_ch_conf, &cap_chan));
ESP_LOGI(TAG, "Register capture callback");
TaskHandle_t cur_task = xTaskGetCurrentTaskHandle();
mcpwm_capture_event_callbacks_t cbs = {
.on_cap = hc_sr04_echo_callback,
}{...};
ESP_ERROR_CHECK(mcpwm_capture_channel_register_event_callbacks(cap_chan, &cbs, cur_task));
ESP_LOGI(TAG, "Enable capture channel");
ESP_ERROR_CHECK(mcpwm_capture_channel_enable(cap_chan));
ESP_LOGI(TAG, "Configure Trig pin");
gpio_config_t io_conf = {
.mode = GPIO_MODE_OUTPUT,
.pin_bit_mask = 1ULL << HC_SR04_TRIG_GPIO,
}{...};
ESP_ERROR_CHECK(gpio_config(&io_conf));
ESP_ERROR_CHECK(gpio_set_level(HC_SR04_TRIG_GPIO, 0));
ESP_LOGI(TAG, "Enable and start capture timer");
ESP_ERROR_CHECK(mcpwm_capture_timer_enable(cap_timer));
ESP_ERROR_CHECK(mcpwm_capture_timer_start(cap_timer));
uint32_t tof_ticks;
while (1) {
gen_trig_output();
if (xTaskNotifyWait(0x00, ULONG_MAX, &tof_ticks, pdMS_TO_TICKS(1000)) == pdTRUE) {
float pulse_width_us = tof_ticks * (1000000.0 / esp_clk_apb_freq());
if (pulse_width_us > 35000) {
continue;
}{...}
float distance = (float) pulse_width_us / 58;
ESP_LOGI(TAG, "Measured distance: %.2fcm", distance);
}{...}
vTaskDelay(pdMS_TO_TICKS(500));
}{...}
}{ ... }