CC2541 添加串口功能

来源:互联网 发布:聚划算和淘宝的关系 编辑:程序博客网 时间:2024/05/29 08:18

转载请注明出处:http://blog.csdn.net/haozi0_0

最近重新写了 CC2541 的程序,这里把添加串口的流程介绍一下。

CC2541 历程里面有一个 npi.c 文件,是用来操作串口的,但是我没有直接使用这个文件,反正也就是初始化的操作而已,自己写就好了。

1. 先在 simpleBLEPeripheral.c 文件中添加头文件

#ifdef UART_DEBUG#include "hal_uart.h"#endif


别忘了在文件的的开头加上函数声明

void UartCallBack(uint8 port, uint8 event);void UartCofig(void);

然后添加配置函数和回调函数,回调函数中可以收到输入的串口数据哦

#ifdef UART_DEBUGuint8 Buffer[128];uint16 Counter;//----- UART 收到資料的 CallBack Code-----------------------------------------void UartCallBack(uint8 port, uint8 event) //call back use{  switch(event)    {        case HAL_UART_RX_TIMEOUT:          {                uint16 count;count = Hal_UART_RxBufLen(HAL_UART_PORT_0) ;// 把 UART 資料存到 BufferHalUARTRead(HAL_UART_PORT_0, &Buffer[Counter], count);serialCounter = count + serialCounter;                //在此处添加处理函数,处理收到的串口数据          }       break;       default:break;    }}void UartCofig(void){  halUARTCfg_t uartConfig;  // configure UART  uartConfig.configured           = TRUE;  uartConfig.baudRate             = HAL_UART_BR_115200;  uartConfig.flowControl          = FALSE;  uartConfig.flowControlThreshold = 48;  uartConfig.rx.maxBufSize        = 128;  uartConfig.tx.maxBufSize        = 128;  uartConfig.idleTimeout          = 6;  uartConfig.intEnable            = TRUE;  uartConfig.callBackFunc         = UartCallBack;  // start UART  // Note: Assumes no issue opening UART port.  (void)HalUARTOpen( HAL_UART_PORT_0, &uartConfig );  return;}#endif


2. 在 SimpleBLEPeripheral_Init() 函数中加入 UartCofig(),配置串口


3. 在 hal_uart.c 文件中加入 print_msg 函数,别忘了在 hal_uart.h 中声明函数,extern void print_msg(const char *fmt, ...); 然后就可以使用 print_msg 函数串口信息了。

#ifdef UART_DEBUG#include "stdio.h"#define PRINT_MSG_SKIP  0//1//0#define MSG_SKIP_VALUE  191#define FIRST_MSG_PRINT 3//Skip message is FIRST_MSG_PRINT+1 ~ MSG_SKIP_VALUE => 4 ~ 191int print_msg_skip_cnt = MSG_SKIP_VALUE;void print_msg(const char *fmt, ...){    uint8 i;    char buf[64];    va_list args;    #if (PRINT_MSG_SKIP)    if (print_msg_skip_cnt > MSG_SKIP_VALUE-FIRST_MSG_PRINT)    {        print_msg_skip_cnt--;    }    else if (print_msg_skip_cnt>0 )    {        print_msg_skip_cnt--;        return;    }    #endif    #if 0 //printf sign for different printf    buf[0] = '^'; // in different H/W use different begin signal    buf[1] = '^';    HalUARTWrite(HAL_UART_PORT_0, (uint8*)buf, 2);    #endif    va_start(args, fmt);    vsprintf(buf, fmt, args);    va_end(args);    for(i=0; i<64; i++)    {        if(buf[i] == NULL)        break;        if(buf[i] == '\n')        {            char buf2[2];            buf2[0] = 0x0d;            buf2[1] = 0x0a;            HalUARTWrite(HAL_UART_PORT_0, (uint8*)buf2, 2);        }        else        {            HalUARTWrite(HAL_UART_PORT_0, (uint8*)&buf[i], 1);        }    }}#endif

4.在 Preprocessor 中添加如下宏定义,打开串口功能

HAL_UART=TRUE
HAL_UART_DMA=1
HAL_UART_TX_BY_ISR=1
UART_DEBUG

如果想使用其他扣得 Uart,设置这些宏定义就好了,这里我设置成了 DMA 方式。

 





0 0
原创粉丝点击