#include <stdio.h> #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "driver/ledc.h" #include "esp_err.h" #include "string.h" #include "esp_log.h" #include "freertos/queue.h" #include "driver/uart.h" #define LEDC_TIMER LEDC_TIMER_0 // LEDC定时器 #define LEDC_MODE LEDC_LOW_SPEED_MODE // LEDC的速度模式 #define LEDC_OUTPUT_IO (12) // LEDC绑定引脚 #define LEDC_CHANNEL LEDC_CHANNEL_0 // LEDC通道 #define LEDC_DUTY_RES LEDC_TIMER_13_BIT // LEDC占空比分辨率 #define LEDC_DUTY (4095) // LEDC占空比 #define LEDC_FREQUENCY (5000) // LEDC频率 5 kHz static void example_ledc_init(void) { // LEDC定时器配置 ledc_timer_config_t ledc_timer = { .speed_mode = LEDC_MODE, // 设置定时器低速模式 .timer_num = LEDC_TIMER, // 设置定时器0 .duty_resolution = LEDC_DUTY_RES, // 设置定时器分辨率 13位 .freq_hz = LEDC_FREQUENCY, // 设置定时器频率 5kHz .clk_cfg = LEDC_AUTO_CLK // 设置定时器时钟选择 }; ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer)); // 设置定时器配置 // 配置LEDC通道 ledc_channel_config_t ledc_channel = { .speed_mode = LEDC_MODE, // 设置通道模式低速模式 .channel = LEDC_CHANNEL, // 设置LEDC通道0 .timer_sel = LEDC_TIMER, // 设置LEDC定时器 .intr_type = LEDC_INTR_DISABLE, // 设置LEDC中断类型 .gpio_num = LEDC_OUTPUT_IO, // 设置LEDCGPIO引脚 .duty = 0, // Set duty to 0% //设置占空比 .hpoint = 0 // LEDC通道hpoint值 }; ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel)); // 配置LEDC通道参数 } void cpu_task(void *ptr) { uint8_t cpu_info[400]; while (1) { memset(cpu_info, 0, 400); vTaskList((char *)&cpu_info); printf("%s", cpu_info); printf("--------------\r\n"); memset(cpu_info, 0, 400); vTaskGetRunTimeStats((char *)&cpu_info); printf("%s", cpu_info); printf("--------------\r\n"); vTaskDelay(5000 / portTICK_PERIOD_MS); } } QueueHandle_t test_queue_handle = NULL; void test_queue() { test_queue_handle = xQueueCreate(20, 50); if (test_queue_handle != NULL) { ESP_LOGI("YCP", "创建成功"); } else { ESP_LOGI("YCP", "空间不足"); } } char *str = "消息队列发送测试"; void test_queue_send(void *ptr) { while (1) { xQueueSend(test_queue_handle, (void *)str, 0); vTaskDelay(1000 / portTICK_PERIOD_MS); } } void test_queue_receive(void *ptr) { char queue_receive[50]; while (1) { int ret = xQueueReceive(test_queue_handle, (void *)queue_receive, 500); if (ret) { ESP_LOGE("消息队列接收", "%s", queue_receive); } else { ESP_LOGE("消息队列接收", "没收到"); } vTaskDelay(1000 / portTICK_PERIOD_MS); } } QueueHandle_t uart_handle = NULL; void uart_task(void *ptr) { const uart_config_t uart_config = { .baud_rate = 9600, /*!< UART baud rate*/ .data_bits = UART_DATA_8_BITS, /*!< UART byte size*/ .parity = UART_PARITY_DISABLE, /*!< UART parity mode*/ .stop_bits = UART_STOP_BITS_1, /*!< UART stop bits*/ .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, /*!< UART HW flow control mode (cts/rts)*/ // uint8_t rx_flow_ctrl_thresh; /*!< UART HW RTS threshold*/ .source_clk = UART_SCLK_DEFAULT, /*!< UART source clock selection */ }; int intr_alloc_flags = 0; uart_event_t uart_ev; #if CONFIG_UART_ISR_IN_IRAM intr_alloc_flags = ESP_INTR_FLAG_IRAM; #endif uart_param_config(UART_NUM_1, &uart_config); uart_driver_install( UART_NUM_1, // UART 编号 1024, // Rx 缓冲区大小 0, // Tx 缓冲区大小 20, // 事件队列长度(可以不要,此参数填 0,然后下一个参数填NULL) &uart_handle, //(QueueHandle_t*)接受被创建的句柄的变量指针,类型为FreeRTOS的队列 intr_alloc_flags // 中断分配标志,这里写 0 表示不想分配中断 ); uart_set_pin(UART_NUM_1, 21, 20, -1, -1); uint8_t *data = (uint8_t *)malloc(1024 * 8); // 活动内存 while (1) { int ret = xQueueReceive(uart_handle, &uart_ev, 500 / portTICK_PERIOD_MS); if (ret != 0) { switch (uart_ev.type) { case UART_DATA: { uart_read_bytes(UART_NUM_1, data, uart_ev.size, 500 / portTICK_PERIOD_MS); uart_write_bytes(UART_NUM_1, data, uart_ev.size); } break; default: break; } } } } void app_main(void) { test_queue(); // 配置LEDC外设 example_ledc_init(); // 设置占空比(LEDC的速度模式,LEDC通道,4095+1) 50% 2^13 = 8192 4096即占一半,占空比50% ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, LEDC_DUTY)); // 更新占空比 ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL)); /* xTaskCreate(cpu_task,"cpu_task_name",4096,NULL,1,NULL); xTaskCreate(test_queue_send,"test_queue_send_name",4096,NULL,1,NULL); xTaskCreate(test_queue_receive,"test_queue_receive_name",4096,NULL,1,NULL);*/ xTaskCreate(uart_task, "uart_task_name", 4096, NULL, 1, NULL); }
标签:UART,队列,void,ledc,queue,uart,LEDC,事件,串口 From: https://www.cnblogs.com/ycp666/p/17020424.html