[CSR8] 分享一个CSR8670串口收发程序

来源:互联网 发布:淘宝 大麻暗语 编辑:程序博客网 时间:2024/06/07 02:00

http://www.eeskill.com/group/topic/id/4413

[CSR8] 分享一个CSR8670串口收发程序

最近在调试csr8670串口收发程序,为了方便大家使用,下面就介绍下怎么使用8670的串口。

介绍前,先吐槽下CSR,也许是以前没用过CSR芯片,尼玛资料太少了。
使用uart的步骤:

1.工程属性中将Transport改为raw ,还需确认mak中的Transport也为raw
CS-207483-UGP8ImplementingStreamsinBlueCoreApplications.pdf文档里面有介绍serial port类型就是raw。

2.在工程中加入以下代码

#define uart_RECV_BUF_SIZE                20
static TaskData uart;
static uint8 uartl_recv_buf[UART_RECV_BUF_SIZE];

void uart_init(void)
{
        uart.handler = uart_handler;
        
        StreamConfigure(VM_STREAM_UART_CONFIG, VM_STREAM_UART_THROUGHPUT);
        StreamUartConfigure(VM_UART_RATE_115K2,VM_UART_STOP_ONE,VM_UART_PARITY_NONE);        
        MessageSinkTask(StreamUartSink(), &uart);
}

void uart_send(uint8 *buf, uint16 len)
{
        uint16 offset;
        uint8 *dest;

        /*get the sink for the uart, panic if not available*/
        Sink sink = StreamUartSink();
        PanicNull(sink);

        /*claim space in the sink, getting the offset to it*/
        offset = SinkClaim(sink, len);
        if(offset == 0xFFFF) Panic(); /*space not available*/

        /*Map the sink into memory space*/
        dest = SinkMap(sink);
        (void) PanicNull(dest);

        /*copy the string into the claimed space*/
        memcpy(dest + offset, buf, len);

        /*Flush the data out to the uart*/
        PanicZero(SinkFlush(sink, len));
}

static void uart_recv(void)
{
        Source src;
        uint8 size, i;
        uint8 *buf;

        /*get the uart source header*/
        src = StreamUartSource();
        size = SourceSize(src);
        buf = (uint8 *)SourceMap(src);
        if(size > UART_RECV_BUF_SIZE)
        {
                SourceDrop(src, size);
                return;
        }
        
        memcpy(uart_recv_buf, buf, size);
        SourceDrop(src, size);
}
static void uart_handler(Task task, MessageId id, Message message)
{
        switch(id)
        {
                case MESSAGE_MORE_DATA:
                        uart_recv();
                        break;

                default:
                        break;
        }
}

3.在pstool中设置host_interface为VM access to the uart,实际根据需要去选择,这里我选VM access to the uart。

4.在pstool中设置uart configuration when under VM control为0880,这里我选择不需要流控制,实际根据需要去设置。

不过在实际测试中,只要接收的数据大于2,都会分包接收,搞得很蛋疼,也许是因为没有使用流控制的原因,所以实际使用中最好加入流控制,方便接收。

但想想为什么没有流控制的时候会分包呢?开始以为message消息中会有接收成功的标志,不过测试了一番发现没有!!!

猜想可能是csr 的OS原因引起的,估计是中断接收到串口数据了,会先发一个包过去告诉Task有数据过来,去接收,但并没有全部把数据发过来,这样做可能是想及时响应中断的缘故。

大家知道中断程序尽量少做事,以免引起阻塞,于是将剩下的数据等到OS空闲的时候再发送过来告诉Task去读取,不过没法去求证,希望大神解答。


0 0
原创粉丝点击