CC2640 UART使用回调方式进行数据接收时程序经常死掉
时间:10-02
整理:3721RD
点击:
串口初始化代码如下:
#include "cc2640_uart.h"
// 串口部分
#define MAX_NUM_RX_BYTES 100 // Maximum RX bytes to receive in one go
#define MAX_NUM_TX_BYTES 100 // 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 parameters
UART_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 read
handle = 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 buffer
for(size_t 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);
}
使用串口调试助手对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_PRIORITY 1
#ifndef UART_TASK_STACK_SIZE
#define UART_TASK_STACK_SIZE 644
#endif
// Task configuration
Task_Struct uartTask;
Char uartTaskStack[UART_TASK_STACK_SIZE];
#define MAX_NUM_RX_BYTES 100 // Maximum RX bytes to receive in one go
#define MAX_NUM_TX_BYTES 100 // 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
/*******************************************************************************
* LOCAL FUNCTIONS
*/
static void taskStartFxn(UArg a0, UArg a1);
static void readCallback(UART_Handle handle, void *rxBuf, size_t size);
/*******************************************************************************
* @fn Uart_createTask
*
* @brief Task creation function for the uart.
*
* @param None.
*
* @return None.
*/
void Uart_createTask(void)
{
Task_Params taskParams;
// Configure task
Task_Params_init(&taskParams);
taskParams.stack = uartTaskStack;
taskParams.stackSize = UART_TASK_STACK_SIZE;
taskParams.priority = UART_TASK_PRIORITY;
Task_construct(&uartTask, taskStartFxn, &taskParams, NULL);
}
/*********************************************************************
* @fn taskStartFxn
*
* @brief Application task entry point for the uart test.
*
* @param a0, a1 - not used.
*
* @return None.
*/
static void taskStartFxn(UArg a0, UArg a1)
{
UART_Handle handle;
UART_Params params;
// Init UART and specify non-default parameters
UART_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 read
handle = UART_open(Board_UART, ¶ms);
// Enable RETURN_PARTIAL
UART_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 buffer
for(size_t 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);
}
哪一行挂了?
可以使用npi-uart。
别再readcallback中调用UART_write
想办法用中断工作模式接收。
我遇到过这个问题,已经解决了。
一开始我不太清楚回调函数的作用,回调函数相当于串口中断的入口,一旦串口收到数据,就会执行这个被注册了的callback回调函数;
回调函数里不要放很多处理程序,回调函数只要发送一个equeue消息给主程序,让主程序慢慢处理;
如果数据一来一直进入中断,而中断又需要处理你写的一堆任务,就会卡死。
如果不清楚可以给我发邮件zhengxinkang@qq.com
readcallback是在read中断里处理的,再次调用write会使程序异常。使用队列或信号量处理下就可以了
