串口初始化代码如下:
#include "cc2640_uart.h" // 串口部分 #define MAX_NUM_RX_BYTES100// Maximum RX bytes to receive in one go #define MAX_NUM_TX_BYTES100// 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 // 回调函數 static void readCallback(UART_Handle handle, void *rxBuf, size_t size); // 串口全局變量 UART_Handle handle; UART_Params params; void cc2640_uart_init(void) {// Init UART and specify non-default parametersUART_Params_init(¶ms);params.baudRate= 115200;params.writeDataMode = UART_DATA_BINARY;params.readMode= UART_MODE_CALLBACK;params.readCallback = readCallback;// Open the UART and initiate the first readhandle = UART_open(Board_UART, ¶ms);wantedRxBytes = 1;int rxBytes = UART_read(handle, rxBuf, wantedRxBytes); } static void readCallback(UART_Handle handle, void *rxBuf, size_t size) {// Copy bytes from RX buffer to TX bufferfor(size_t i = 0; i < size; i++)txBuf[i] = ((uint8_t*)rxBuf)[i];// Echo the bytes received back to transmitterUART_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); }
使用串口调试助手对2640定时发送数据,开始时可以收到数据,但是收着收着程序就死掉了,不知道为什么,如下图所示:
本来应该是发送数据和接收数据相等的,现在是收到479个字节数据程序就死掉了然后无法继续接收数据。找了很久不知道为什么,请问这是什么原因,谢谢!
猜猜我是谁:
没有人知道为什么吗?我改用任务的方式试了还是这样,串口收数据,程序跑着跑着就挂掉了…
代码如下:
#include "uart_test.h" #include "stdint.h" #include <Board.h> #include <ti/drivers/UART.h> #include <ti/sysbios/knl/Task.h> #include <ti/drivers/uart/UARTCC26XX.h>// Task configuration #define UART_TASK_PRIORITY1#ifndef UART_TASK_STACK_SIZE #define UART_TASK_STACK_SIZE644 #endif// Task configuration Task_Struct uartTask; Char uartTaskStack[UART_TASK_STACK_SIZE];#define MAX_NUM_RX_BYTES100// Maximum RX bytes to receive in one go #define MAX_NUM_TX_BYTES100// Maximum TX bytes to send in one gouint32_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/******************************************************************************** LOCAL FUNCTIONS*/ static void taskStartFxn(UArg a0, UArg a1); static void readCallback(UART_Handle handle, void *rxBuf, size_t size);/******************************************************************************** @fnUart_createTask** @briefTask creation function for the uart.** @paramNone.** @returnNone.*/ void Uart_createTask(void) {Task_Params taskParams;// Configure taskTask_Params_init(&taskParams);taskParams.stack = uartTaskStack;taskParams.stackSize = UART_TASK_STACK_SIZE;taskParams.priority = UART_TASK_PRIORITY;Task_construct(&uartTask, taskStartFxn, &taskParams, NULL); }/********************************************************************** @fntaskStartFxn** @briefApplication task entry point for the uart test.** @parama0, a1 - not used.** @returnNone.*/ static void taskStartFxn(UArg a0, UArg a1){UART_Handle handle;UART_Params params;// Init UART and specify non-default parametersUART_Params_init(¶ms);params.baudRate= 9600;params.writeDataMode = UART_DATA_BINARY;params.readMode= UART_MODE_CALLBACK;params.readDataMode= UART_DATA_BINARY;params.readCallback= readCallback;// Open the UART and initiate the first readhandle = UART_open(Board_UART, ¶ms);// Enable RETURN_PARTIALUART_control(handle, UARTCC26XX_CMD_RETURN_PARTIAL_ENABLE, NULL);wantedRxBytes = MAX_NUM_RX_BYTES;int rxBytes = UART_read(handle, rxBuf, wantedRxBytes);Task_exit(); }// Callback function static void readCallback(UART_Handle handle, void *rxBuf, size_t size) {// Copy bytes from RX buffer to TX bufferfor(size_t i = 0; i < size; i++)txBuf[i] = ((uint8_t*)rxBuf)[i];// Echo the bytes received back to transmitterUART_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); }
cedar_xuesong:
回复 猜猜我是谁:
哪一行挂了???
y s:
回复 cedar_xuesong:
可以使用npi-uart。
Rugooo Zhang:
别再readcallback中调用UART_write
da qin zheng sheng:
想办法用中断工作模式接收。
user4587069:
readcallback是在read中断里处理的,再次调用write会使程序异常。使用队列或信号量处理下就可以了
user5291981:
回复 Rugooo Zhang:
我通过串口写字库,可是数据只能接收到一部分是为什么?感谢