TI-RTOS实时操作系统开发之串口应用

TI-RTOS实时操作系统之串口应用

适用 CC1310\ CC2640R2F等基于TI-RTOS的SDK开发

更新-20190622

重新定义API
1、外部传入回调函数,方便调用。
2、增加自定义printf
3、线程安全考虑,增加互斥信号量功能,防止多线程(多任务)同时调用时冲突
4、自定义printf的缓存使用的是动态分配内存,可以换成固定数据
5、自定义printf的打印默认结尾带换新行
6、两种打印API,1、MyUart_doSendArray():根据传入数据的长度打印输出。2、MyUart_doPrintf():根据fmt格式,重组字符串

应用实例

//串口接收回调
void myUartReadCallback(void *buf, size_t count)
{

}
//初始化串口
UART_Params uartParams;
uartParams.baudRate = 115200;
MyUart_open(&uartParams, myUartReadCallback);

源码

.c文件

/***** Includes *****/
#include "uartUnit.h"
#include 
#include 
#include 
#include 
#include 

#include 
#include 
#include 

/***** Semaphore信号 *****/

static Semaphore_Handle uartMutexSemHandle;


/***** Prototypes *****/

// uartData_ts uartTxData;

char uartRxDataTemp[UART_read_length];                                                                              

UART_Handle myUartHandle = NULL;  
static MyUart_callBack MyUart_recvCB;

void Uart_ReadCallback(UART_Handle handle, void *buf, size_t count)
{
  MyUart_recvCB(buf, count);
  UART_read(myUartHandle, uartRxDataTemp, UART_read_length); //等待下一次串口接收
}
UART_Handle MyUart_open(UART_Params *params, MyUart_callBack CB)
{
    UART_Params uartParams;
    
    UART_Params_init(&uartParams);
    Board_initUART();
    uartParams.baudRate  = params->baudRate;
    uartParams.writeMode = UART_MODE_BLOCKING;
    uartParams.readMode = UART_MODE_CALLBACK;
    uartParams.readCallback = Uart_ReadCallback;
    uartParams.writeDataMode = UART_DATA_BINARY;
    uartParams.readDataMode = UART_DATA_BINARY; 
    uartParams.readReturnMode = UART_RETURN_FULL;
    uartParams.readEcho = UART_ECHO_OFF;          //设置读取反馈开关,关闭

    uartMutexSemHandle = SemaphoreP_createBinary(1);
    if (uartMutexSemHandle == NULL)
    {
        // DebugP_log0("DisplayUart.c: Couldn't create semaphore");
        return NULL;
    }
    MyUart_recvCB = CB;
    // UART_init();
    myUartHandle = UART_open(Board_UART0, &uartParams);
    if (NULL == myUartHandle)
    {
        // DebugP_log0("DisplayUart.c: Couldn't open UART");
        return NULL;
    }
    UART_control(myUartHandle, UART_CMD_RESERVED, NULL); //开启自动超时检测
    /* Disable RX in case that is needed to save power */
    // UART_control(myUartHandle, UART_CMD_RXDISABLE, NULL);
    UART_read(myUartHandle, uartRxDataTemp, UART_read_length); //等待下一次串口接收
    return myUartHandle;
}
/**
 * 
 *@funtion:串口发送数据
 *
 *@param1:str=>发送数据指针,
 *@param2:strLength=>发送数据长度
*/
static void MyUart_doSendArray(char *str, uint8_t strLength)
{
  int writeEcho;
   if (myUartHandle == NULL) {
     return;
   }
  writeEcho = UART_write(myUartHandle, str, strLength); 
  switch(writeEcho){
    case UART_ERROR:
    break;
    case 0:

    break;
    default:

    break;
  }
}
uint8_t *pMsg;
#define mallocMemSize 128
void MyUart_doPrintf(char *fmt, ...)
{
    va_list va;
    
    uint32_t strSize = 0;

    if (SemaphoreP_pend(uartMutexSemHandle, (unsigned int)(-1)) == SemaphoreP_OK)
    {
        va_start(va, fmt);
        pMsg = ICall_malloc(mallocMemSize);
        SystemP_vsnprintf(pMsg, mallocMemSize, fmt, va);
        strSize = strlen(pMsg);
        pMsg[strSize++] = '\r';
        pMsg[strSize++] = '\n';

        UART_write(myUartHandle, pMsg, strSize);
        SemaphoreP_post(uartMutexSemHandle);
        va_end(va);
        ICall_free(pMsg);
    }
}

void closeUartUnit(void)
{
  if(myUartHandle){  
    myUartHandle = NULL;
    UART_close(myUartHandle);
  }
}

#ifndef __uartUnit_H_
#define __uartUnit_H_
#define uartRxBufferMaxIndex 10
#define uartRxBufferMaxLength 25
#define uartRxSubPackageLength uartRxBufferMaxLength

#define UART_read_length 256
typedef struct{
    unsigned char length;
    unsigned char isEmpty;
    unsigned char buffer[uartRxBufferMaxLength];
}uartData_ts;

typedef void (*MyUart_callBack)(void *buf, size_t count);

UART_Handle MyUart_doOpen(UART_Params *params, MyUart_callBack CB);
void MyUart_doSendArray(char *str, uint8_t strLength);
void MyUart_doPrintf(char *fmt, ...);

#define MyUart_Printf     MyUart_doPrintf
#define MyUart_open(params, CB)    MyUart_doOpen(params, CB)
#define MyUart_sendArray(str, lenth)    MyUart_doSendArray(str, lenth)


#endif /* TASKS_NODETASK_H_ */

---------------------------------------------------------------分割线--------------------------------------------------

配置

  • 注意:
    1、UART_read(mUartHandle, uartRxDataTemp, UART_read_length);
    UART_read_length:表示能接收到的数据长度,如果接收到的数据长度超过该参数,会造成串口宕机
    2、UART_control(mUartHandle, UART_CMD_RESERVED, NULL);
    开启硬件超时检测,超时后会自动调用Uart_ReadCallback
/**
 * @funtion: 串口参数配置
 * 
 * @param1:baudRate=>波特率,最大=3Mbps
 * @param2:dataLength=>数据单元长度=5,6,7,8
 * @param3:stopBits=>停止位=1位,2位
 * @param4:parityType=>校验位=无校验,偶校验,奇校验,总为0,总为1
 * @ret: =1 配置成功,=0 配置失败
*/
char config_uart(uint32_t baudRate, UART_LEN dataLength, UART_STOP stopBits,UART_PAR parityType)
{
  if(mUartHandle != NULL){
    UART_close(mUartHandle);
    mUartHandle = NULL;
  }else{
    initUartRxBuffer();
    // setTagPriority("peripheral", 0);
    Board_initUART();
    /* Create a mUartHandle with data processing off. */
    UART_Params_init(&uartParams);                //加载默认参数
  }
  uartParams.readCallback = Uart_ReadCallback;  //获取串口读取回调函数
  // uartParams.writeCallback = Uart_WriteCallback;  //获取串口写回调函数

  uartParams.readMode = UART_MODE_CALLBACK;     //设置串口读模式:回调模式
  // uartParams.writeMode = UART_MODE_BLOCKING; //设置串口写模式:默认为阻塞模式

  uartParams.writeDataMode = UART_DATA_BINARY;
  uartParams.readDataMode = UART_DATA_BINARY;

  uartParams.readReturnMode = UART_RETURN_FULL;
  uartParams.readEcho = UART_ECHO_OFF;          //设置读取反馈开关,关闭

  uartParams.baudRate   = baudRate;       //波特率
  uartParams.dataLength = dataLength;   //数据长度
  uartParams.stopBits   = stopBits;       //停止位
  uartParams.parityType = parityType;   //奇偶检验    

  // uartParams.readTimeout = getRxTimeOutTm();
  // uartParams.readTimeout = UART_WAIT_FOREVER;    //设置读取超时时间,默认总是等待,
                                                    //readMode=UART_MODE_CALLBACK时一定要设置为forever
  mUartHandle = UART_open(Board_UART0, &uartParams);
  if (mUartHandle == NULL) {
      return 0;
  }else{
    UART_control(mUartHandle, UART_CMD_RESERVED, NULL); //开启自动超时检测
    UART_read(mUartHandle, uartRxDataTemp, UART_read_length);
    return 1;
  }

}

接收回调

回调函数中尽量不要处理耗时工作,每次读取完成后都要调用一次UART_read();
参数:void *bufUART_read(mUartHandle, uartRxDataTemp, UART_read_length);传入的缓存地址

void Uart_ReadCallback(UART_Handle handle, void *buf, size_t count)
{

  UART_read(mUartHandle, uartRxDataTemp, UART_read_length); //等待下一次串口接收
}

你可能感兴趣的:(CC1310,CC2640R2F,TI-RTOS)