1 改写原因
原例程为“uart_events”。
例程是基于从机方式的,即接收响应的,打算实现一个基于主机方式的,即发送等待应答(带超时处理,失败自动重发2次)。
2 改写内容
硬件上:将串口改为串口2,发送脚GPIO19,接收脚GPIO18。
将例程中串口内容,复制并做部分修改移植到新建的“uart_master.c”和“uart_master.h”文件中。
“uart_master.c”文件内容如下:
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/uart.h"
#include "esp_log.h"
#include "uart_master.h"
static const char *TAG = "uart_events";
static QueueHandle_t rx_queue;
static uint8_t send_buf[1024]={1,2,3,4,5,6,7,8};
static uint8_t ack_ok = 0;
static void uart_rx_task(void *pvParameters)
{
uart_event_t event;
uint8_t* dtmp = (uint8_t*) malloc(1024);
for(;;) {
//Waiting for UART event.
if(xQueueReceive(rx_queue, (void * )&event, (portTickType)portMAX_DELAY)) {
bzero(dtmp, 1024);
ESP_LOGI(TAG, "uart[%d] event:", UART_NUM_2);
switch(event.type) {
//Event of UART receving data
/*We'd better handler data event fast, there would be much more data events than
other types of events. If we take too much time on data event, the queue might
be full.*/
case UART_DATA:

本文介绍如何将原基于从机的UART_events例程修改为支持主机模式,使用GPIO19发送,GPIO18接收,并实现发送数据后等待应答的超时重发功能。内容涉及UART_master.c和.h文件的修改,以及初始化、任务创建和错误处理。
最低0.47元/天 解锁文章
8078

被折叠的 条评论
为什么被折叠?



