目录
Uart功能模块实现
默认4.1的adk中是不包含uart收发的应用层逻辑,所以需要自己实现
在src/lib目录下新建uart目录,添加uart.c与uart.h文件后编译库文件,如下所示:
uart.c
#include "uart.h"
#define MAX_G_BUFF_SIZE 32
static Sink sUartSink = 0;
Sink UartInit(Task task)
{
if(!(sUartSink = StreamUartSink()))
return 0;
/* Configure sink not to send MESSAGE_MORE_SPACE */
PanicFalse(SinkConfigure(sUartSink, VM_SINK_MESSAGES, VM_MESSAGES_NONE));
StreamConfigure(VM_STREAM_UART_CONFIG, VM_STREAM_UART_LATENCY);
MessageSinkTask(StreamUartSink(), task);
return sUartSink;
}
bool UartSendData(uint8 *data, uint16 size)
{
if(!sUartSink){
return FALSE;
}
if (!data || size == 0){
return FALSE;
}
if(SinkClaim(sUartSink, size) != 0xFFFF){
memmove(SinkMap(sUartSink), data, size);
(void) PanicZero(SinkFlush(sUartSink, size));
return TRUE;
}
return FALSE;
}
bool UartSendChar(uint8 c )
{
if(!sUartSink) {
return FALSE;
}
if(SinkClaim(sUartSink, 1) != 0xFFFF) {
memcpy(SinkMap(sUartSink), &c, 1);
(void) PanicZero(SinkFlush(sUartSink, 1));
return TRUE;
}
return FALSE;
}
bool UartSendStr(char* str)
{
if(!str)
return FALSE;
if(strlen(str) > 0)
return UartSendData((uint8*)str, strlen(str));
return FALSE;
}
uart.h
#ifndef _UART_LIB_H_
#define _UART_LIB_H_
#include <panic.h>
#include <sink.h>
#include <message.h>
#include <message_.h>
#include <stream.h>
#include <string.h>
#include <source.h>
#include <csrtypes.h>
#include <stdio.h>
Sink UartInit(Task task);
bool UartSendChar(uint8 c);
bool UartSendStr(char* str);
bool UartSendData(uint8 *data, uint16 size);
#endif
上面接口的描述在doc/support/adkdocs/CS-207483-UG.pdf中可以找到。有兴趣可以自行查阅
将uart库导入工程中
重新编译库文件,上面的uart模块也会生成对应的uart库,我们需要将该库添加到我们的工程中
如果在编译过程中出现无法找到uart库,请在工程目录下xide/bin/运行xide.exe,然后再project->open workspace选择当前工程打开
添加应用层逻辑
然后再apps/sink/目录下添加sink_custom_uart.h sink_custom_uart.c上层应用程序文件,如下所示
sink_custom_uart.c
#include <message.h>
#include <pio.h>
#include "sink_custom_uart.h"
#define MESSAGE_CUSTOM_TEST 0x0001
UARTStreamTaskData theUARTTask;
void app_uart_handler(Task t, MessageId id, Message payload)
{
UARTStreamTaskData *app = &theUARTTask;
switch(id){
case MESSAGE_MORE_DATA:
Source src=StreamSourceFromSink(app->uart_sink);
const uint8 * s = (uint8*)SourceMap(src);
const uint8 * e = s + SourceSize(src);
const uint8 * p = NULL;
p = UartparseData(s, e, &app->task);
SourceDrop(src, p-s);
break;
case MESSAGE_CUSTOM_TEST:
/*test usrt task*/
UartSendStr("MESSAGE_CUSTOM_TEST\r\n");
break;
default:
UartSendStr("ERROR\r\n");
break;
}
}
void custom_uart_init(Task client)
{
theUARTTask.task.handler = app_uart_handler;
theUARTTask.client = client;
theUARTTask.uart_sink = UartInit(&theUARTTask.task);
MessageSendLater(&theUARTTask.task, MESSAGE_CUSTOM_TEST, NULL, 1000);
}
void UarthandleUnrecognised(const uint8 *data, uint16 length, Task task)
{
UartSendStr("ERROR\r\n");
}
sink_custom_uart.h
#ifndef SINK_CUSTOM_UART_H
#define SINK_CUSTOM_UART_H
#include <sink.h>
void app_uart_handler(Task t, MessageId id, Message msg);
void custom_uart_init(Task client);
typedef struct
{
TaskData task;
Task client;
Sink uart_sink;
}UARTStreamTaskData;
extern UARTStreamTaskData theUARTTask;
void UartSendInt(int32 data);
#endif
使用
在main.c的主函数中对uart进行初始化,包含uart.h即可在任意位置,使用UartSendData()和UartSendStr()进行数据发送
如果不能再串口工具中接收到数据,一般都是波特率与流控配置有问题,请打开PSTool.exe(注:该工具上篇文章中我们已经介绍过)连接设备,可以检查如下几个psr配置项:
PSKEY_UART_BITRATE(0x01ea)
PSKEY_UART_CONFIG_USR(0x01c2)
上面我只贴出了部分代码,详细源码及相关的工具资料等,请到我的博客<csr867x入门笔记(序)>最下面的网盘链接或者github链接中下载。