CSR1010蓝牙芯片集成了UART串口通信的功能,能够十分方便的和MCU进行通信。
通过SDK提供的函数库,能够很容易的进行通信程序的编写。
为编写UART通信程序,需要做如下了解:
1、头文件<debug.h>和<uart.h>两个,<uart.h>提供了最基本的配置接口和发送数据的接口,<debug.h>封装了一些能够直接使用和发送数据的函数。
示例使用头文件<debug.h>。
2、初始化函数。
(2)、第二个参数为接受数据的回调函数入口,不存在时设为NULL。回调函数格式定义如下:
通过SDK提供的函数库,能够很容易的进行通信程序的编写。
为编写UART通信程序,需要做如下了解:
1、头文件<debug.h>和<uart.h>两个,<uart.h>提供了最基本的配置接口和发送数据的接口,<debug.h>封装了一些能够直接使用和发送数据的函数。
示例使用头文件<debug.h>。
2、初始化函数。
void DebugInit (uint16 rx_threshold, uart_data_in_fn rx_event_handler, uart_data_out_fn tx_event_handler) ;
(1)、第一个参数设置为1,表示保持通信。
(2)、第二个参数为接受数据的回调函数入口,不存在时设为NULL。回调函数格式定义如下:
static uint16 UartRxCallBack(void *Rx_Buffer, uint16 Rx_Length, uint16 *If_Packed);
(3)、第三个参数为发送数据的回调函数入口,不存在时设为NULL。回调函数格式如下:
static void UartTxCallBack(void);
3、可使用如下函数直接发送字符串:
void DebugWriteString (const char *string);
下面贴上uart通信源码,通信格式设置为波特率19200,数据8bit,停止1,无校验:
#include <main.h>
#include <debug.h>
#include <ls_app_if.h>
#define QUEUE_MAX_SIZE 100
uint8 queue_uart[QUEUE_MAX_SIZE];
uint8 q_head = 0;
uint8 q_free = 0;
uint8 q_foot = 0;
static uint16 UartRxCallBack(void *Rx_Buffer, uint16 Rx_Length, uint16 *If_Packed);
static void UartTxCallBack(void);
bool GetQueueByte(uint8 *data);
bool PutQueueByte(uint8 data);
void SendPendingData(void);
void QueueAddBytes(const char *data, uint8 length);
void AppPowerOnReset(void)
{
}
void AppInit(sleep_state last_sleep_state)
{
DebugInit(1, UartRxCallBack, UartTxCallBack);
UartConfig(0x004e, 0x00);
UartEnable(TRUE);
UartRead(1, 0);
SendPendingData();
}
void AppProcessSystemEvent(sys_event_id id, void *data)
{
}
bool AppProcessLmEvent(lm_event_code event_code, LM_EVENT_T *event_data)
{
return TRUE;
}
static uint16 UartRxCallBack(void *Rx_Buffer, uint16 Rx_Length, uint16 *If_Packed)
{
if (Rx_Length > 0) QueueAddBytes(Rx_Buffer, Rx_Length);
SendPendingData();
*If_Packed = (uint16)0x01;
return Rx_Length;
}
static void UartTxCallBack(void)
{
SendPendingData();
}
void SendPendingData()
{
uint8 s_data;
while (GetQueueByte(&s_data))
UartWrite(&s_data, 1);
}
void QueueAddBytes(const char *data, uint8 length)
{
uint8 i = 0;
while (i < length)
{
if (!PutQueueByte(data[i]))
{
i--;
SendPendingData();
}
i++;
}
}
bool PutQueueByte(uint8 data)
{
if (q_free > 1)
{
queue_uart[q_foot] = data;
q_free--;
q_foot = ((q_foot + 1) % QUEUE_MAX_SIZE);
return TRUE;
}
else return FALSE;
}
bool GetQueueByte(uint8 *data)
{
if (q_free < QUEUE_MAX_SIZE)
{
*data = queue_uart[q_head];
q_free++;
q_head = ((q_head + 1) % QUEUE_MAX_SIZE);
if (q_free == QUEUE_MAX_SIZE)
{
q_head = q_foot = 0;
}
return TRUE;
}
else return FALSE;
}