esp32调试
#if 0//led_t
/* Blink Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "led_strip.h"
#include "sdkconfig.h"
#define CONFIG_BLINK_GPIO 25
static const char *TAG = "example";
/* Use project configuration menu (idf.py menuconfig) to choose the GPIO to blink,
or you can edit the following line and set a number here.
*/
#define BLINK_GPIO CONFIG_BLINK_GPIO
static uint8_t s_led_state = 0;
#ifdef CONFIG_BLINK_LED_RMT
static led_strip_t *pStrip_a;
static void blink_led(void)
{
/* If the addressable LED is enabled */
if (s_led_state) {
/* Set the LED pixel using RGB from 0 (0%) to 255 (100%) for each color */
pStrip_a->set_pixel(pStrip_a, 0, 16, 16, 16);
/* Refresh the strip to send data */
pStrip_a->refresh(pStrip_a, 100);
} else {
/* Set all LED off to clear all pixels */
pStrip_a->clear(pStrip_a, 50);
}
}
static void configure_led(void)
{
ESP_LOGI(TAG, "Example configured to blink addressable LED!");
/* LED strip initialization with the GPIO and pixels number*/
pStrip_a = led_strip_init(CONFIG_BLINK_LED_RMT_CHANNEL, BLINK_GPIO, 1);
/* Set all LED off to clear all pixels */
pStrip_a->clear(pStrip_a, 50);
}
#elif CONFIG_BLINK_LED_GPIO
static void blink_led(void)
{
/* Set the GPIO level according to the state (LOW or HIGH)*/
gpio_set_level(BLINK_GPIO, s_led_state);
}
static void configure_led(void)
{
ESP_LOGI(TAG, "Example configured to blink GPIO LED!");
gpio_reset_pin(BLINK_GPIO);
/* Set the GPIO as a push/pull output */
gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
}
#endif
void app_main(void)
{
/* Configure the peripheral according to the LED type */
configure_led();
while (1) {
ESP_LOGI(TAG, "Turning the LED %s!", s_led_state == true ? "ON" : "OFF");
blink_led();
/* Toggle the LED state */
s_led_state = !s_led_state;
vTaskDelay(CONFIG_BLINK_PERIOD / portTICK_PERIOD_MS);
}
}
#endif
#if 0 // 简单收发
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/gpio.h"
#include "driver/timer.h"
#include "esp_timer.h"
#include "driver/uart.h"
#define TXD1_PIN (GPIO_NUM_17) //串口1的发送数据引脚
#define RXD1_PIN (GPIO_NUM_16) //串口1的接收数据引脚
#define BUF_SIZE (1024) //接收数据缓存大小,该大小需要大于内部FIFO大小:UART_FIFO_LEN(128)
/*串口任务*/
static void uart_task(void *arg)
{
/*配置串口参数*/
uart_config_t uart_config = {
.baud_rate = 115200,//波特率
.data_bits = UART_DATA_8_BITS,//数据位8位
.parity = UART_PARITY_DISABLE,//无奇偶校验
.stop_bits = UART_STOP_BITS_1,//停止位1位
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,//不使用硬件流控
.source_clk = UART_SCLK_APB,//串口使用的时钟
};
/*初始化串口1*/
uart_driver_install(UART_NUM_1,
BUF_SIZE, //串口1接收缓存大小
0,//100, //不使用发送缓存(发送数据的时候便会阻塞发送)
0, //队列大小为0;没有使用freertos内部缓存管理
NULL, //不使用QueueHandle_t 内部缓存管理,设置为空
0 //设置串口中断优先级,设置为0意味着让系统从1-3级中自动选择一个
);
/*设置串口参数*/
uart_param_config(UART_NUM_1, &uart_config);
/*设置串口的TX,RX,RTS,DTR引脚*/ //不使用RTS,DTR
uart_set_pin(UART_NUM_1, TXD1_PIN, RXD1_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
/*申请一块内存,用于临时存储接收的数据*/
uint8_t *data = (uint8_t *) malloc(BUF_SIZE);
while (1) {
//接收串口数据 //每隔10ms判断一次,可以写成portMAX_DELAY(一直判断)
int len = uart_read_bytes(UART_NUM_1, data, BUF_SIZE, 10 / portTICK_RATE_MS);
//把接收的数据发送出去
uart_write_bytes(UART_NUM_1, (const char *) data, len);
}
}
void app_main(void)
{
xTaskCreate(uart_task, "uart_task", 2048, NULL, 10, NULL);
}
#endif
#if 1 //带消息队列的收发
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/gpio.h"
#include "driver/timer.h"
#include "esp_timer.h"
#include "driver/uart.h"
#define TXD1_PIN (GPIO_NUM_17) //串口1的发送数据引脚
#define RXD1_PIN (GPIO_NUM_16) //串口1的接收数据引脚
#define BUF_SIZE (1024) //接收数据缓存大小,该大小需要大于内部FIFO大小:UART_FIFO_LEN(128)
#define BUF_SEND_SIZE (1024) //发送数据缓存大小,该大小需要大于内部FIFO大小:UART_FIFO_LEN(128)
static QueueHandle_t QueueHandle_t_uart1;
/*串口任务*/
static void uart_task(void *arg)
{
/*配置串口参数*/
uart_config_t uart_config = {
.baud_rate = 115200,//波特率
.data_bits = UART_DATA_8_BITS,//数据位8位
.parity = UART_PARITY_DISABLE,//无奇偶校验
.stop_bits = UART_STOP_BITS_1,//停止位1位
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,//不使用硬件流控
.source_clk = UART_SCLK_APB,//串口使用的时钟
};
/*初始化串口1*/
uart_driver_install(UART_NUM_1,
BUF_SIZE, //串口1接收缓存大小
BUF_SEND_SIZE, //串口1发送缓存大小
10, //队列大小为10
&QueueHandle_t_uart1, //缓存管理
0 //设置串口中断优先级,设置为0意味着让系统从1-3级中自动选择一个
);
/*设置串口参数*/
uart_param_config(UART_NUM_1, &uart_config);
/*设置串口的TX,RX,RTS,DTR引脚*/ //不使用RTS,DTR
uart_set_pin(UART_NUM_1, TXD1_PIN, RXD1_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
/*申请一块内存,用于临时存储接收的数据*/
uint8_t *data = (uint8_t *) malloc(BUF_SIZE);
uart_event_t event;
while (1) {
if(xQueueReceive(QueueHandle_t_uart1, (void * )&event, portMAX_DELAY))
{
switch(event.type) {
case UART_DATA://接收到数据
//读取接收的数据
uart_read_bytes(UART_NUM_1, data, event.size, portMAX_DELAY);
//返回接收的数据
uart_write_bytes(UART_NUM_1, (const char*) data, event.size);
break;
case UART_FIFO_OVF://FIFO溢出(建议加上数据流控制)
uart_flush_input(UART_NUM_1);
xQueueReset(QueueHandle_t_uart1);
break;
case UART_BUFFER_FULL://接收缓存满(建议加大缓存 BUF_SIZE)
uart_flush_input(UART_NUM_1);
xQueueReset(QueueHandle_t_uart1);
break;
case UART_BREAK://检测到接收数据中断
break;
case UART_PARITY_ERR://数据校验错误
break;
case UART_FRAME_ERR://数据帧错误
break;
case UART_PATTERN_DET://接收到相匹配的字符(没用到)
break;
default:
break;
}
}
}
free(data);
data = NULL;
vTaskDelete(NULL);
}
void app_main(void)
{
xTaskCreate(uart_task, "uart_task", 2048, NULL, 10, NULL);
}
#endif