回调函数写成这种形式:void Uart_ReadCallback(UART_Handle handle, void *rxBuf, size_t size)
我想知道 rxBuf和size这两个参数分别代表什么含义?
在接收测试中,分别用两种write,发一个字节都没问题,但是我同时发两个字节就会卡死,真是非常纳闷。
#include <stdint.h> #include <stddef.h> /* Driver Header files */ #include <ti/drivers/GPIO.h> #include <ti/drivers/UART.h> /* Example/Board Header files */ #include "Board.h" uint8_t a = 0; static UART_Handle uart; void Uart_ReadCallback(UART_Handle handle, void *rxBuf, size_t size) {UART_write(handle, (uint8_t*)rxBuf, 1);//UART_write(handle, &size, 1);UART_read(uart, &a, 1); } /* * ======== mainThread ======== */ void *mainThread(void *arg0) {charinput;const char echoPrompt[] = "Echoing characters:\r\n";UART_Params uartParams;/* Call driver init functions */GPIO_init();UART_init();/* Configure the LED pin */GPIO_setConfig(Board_GPIO_LED0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);/* Turn on user LED */GPIO_write(Board_GPIO_LED0, Board_GPIO_LED_ON);/* Create a UART with data processing off. */UART_Params_init(&uartParams);uartParams.writeDataMode = UART_DATA_BINARY;uartParams.readDataMode = UART_DATA_BINARY;uartParams.readMode = UART_MODE_CALLBACK;uartParams.readCallback = Uart_ReadCallback;uartParams.readReturnMode = UART_RETURN_FULL;uartParams.readEcho = UART_ECHO_OFF;uartParams.baudRate = 115200;uart = UART_open(Board_UART0, &uartParams);if (uart == NULL) {/* UART_open() failed */while (1);}UART_write(uart, echoPrompt, sizeof(echoPrompt));UART_read(uart, &input, 1);/* Loop forever echoing */while (1) {//UART_read(uart, &input, 1);//UART_write(uart, &input, 1);} }
Felix ZF:
可以阅读下面的文档,详细了解。
我按照文档中的代码段,进行了测试,可以实现多个字节的发送。
测试代码如下:
/*
* ======== uartecho.c ========
*/
#include <stdint.h>
#include <stddef.h>/* Driver Header files */
#include <ti/drivers/GPIO.h>
#include <ti/drivers/UART.h>/* Example/Board Header files */
#include "Board.h"#define MAX_NUM_RX_BYTES 1000 // Maximum RX bytes to receive in one go
#define MAX_NUM_TX_BYTES 1000 // Maximum TX bytes to send in one go
uint32_t wantedRxBytes; // Number of bytes received so far
uint8_t rxBuf[MAX_NUM_RX_BYTES]; // Receive buffer
uint8_t txBuf[MAX_NUM_TX_BYTES]; // Transmit buffer
// Read callback function
static void readCallback(UART_Handle handle, void *rxBuf, size_t size)
{
size_t i = 0;// Make sure we received all expected bytes
if (size == wantedRxBytes) {
// Copy bytes from RX buffer to TX buffer
for(i = 0; i < size; i++)
txBuf[i] = ((uint8_t*)rxBuf)[i];
// Echo the bytes received back to transmitter
UART_write(handle, txBuf, size);
// Start another read, with size the same as it was during first call to
// UART_read()
UART_read(handle, rxBuf, wantedRxBytes);
}
else {
// Handle error or call to UART_readCancel()
}
}
// Write callback function
static void writeCallback(UART_Handle handle, void *rxBuf, size_t size)
{
// Do nothing
}/*
* ======== mainThread ========
*/
void *mainThread(void *arg0)
{
UART_Handle handle;
UART_Params params;
// Init UART
UART_init();
// Specify non-default parameters
UART_Params_init(¶ms);
params.baudRate = 115200;
params.writeMode = UART_MODE_CALLBACK;
params.writeDataMode = UART_DATA_BINARY;
params.writeCallback = writeCallback;
params.readMode = UART_MODE_CALLBACK;
params.readDataMode = UART_DATA_BINARY;
params.readCallback = readCallback;
// Open the UART and initiate the first read
handle = UART_open(Board_UART0, ¶ms);
wantedRxBytes = 6;
int rxBytes = UART_read(handle, rxBuf, wantedRxBytes);
while(true); // Wait forever
}
user4959119:
回复 Felix ZF:
你好FZ,文档我看了还是不理解,我看咱们回调函数里面的参数size还可以配合for循环使用,按中断来说来一个字节中断一次,size应该永远为1吧,在for里面看起来就像一次可以接收多个数据然后再处理,这个问题请解释一下吧。。。
user4959119:
回复 Felix ZF:
哦买嘎,了然,thank you!
user5322468:
回复 Felix ZF:
我的串口接收到数据,进入处理,处理里面有flash操作,flash 会导致串口的接收回调再也无法接收到中断数据。但是串口却在正常运行(串口在发送数据,无法接收中断回调)。