CSR1010使用UART进行串口通信

CSR1010蓝牙芯片集成了UART串口通信的功能,能够十分方便的和MCU进行通信。
通过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;
}


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值