打印

求一份代码?关于环形缓存对串口收发数据的处理代码?

[复制链接]
1128|6
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
hanfei08131106|  楼主 | 2017-5-11 15:34 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
如题:
整理代码时需要写环形缓存对串口收发数据的处理函数;函数
接口如下:求一份代码参考参考,谢谢!
//#include "RingBufferUtils.h"

#define UART_RX_BUFFER_LEN 2048

static ring_buffer_t  uart_ring_buffer;
static UINT8  uart_ring_data[UART_RX_BUFFER_LEN] = {0};


  • /*
  •     send uart data
  • */
  • INT32 aliyun_iot_uart_send(INT8 *pSendBuffer, INT32 bufferLen);
  • /*
  •     receive uart data
  • */
  • INT32 aliyun_iot_uart_recv(INT8 *pRecvBuffer, INT32 bufferLen, UINT32 timeOutMs);
  • /*
  •     initialize the baud rate of the uart
  • */
  • INT32 aliyun_iot_uart_init();
  • /*
  •      the length of the data readable inside uart buffer
  • */
  • INT32 aliyun_iot_uart_buffer_len();



相关帖子

沙发
宇容创行| | 2017-5-11 19:37 | 只看该作者
这个不同单片机写法是不一样的,一般原厂demo里面都有

使用特权

评论回复
板凳
hanfei08131106|  楼主 | 2017-5-11 21:16 | 只看该作者
宇容创行 发表于 2017-5-11 19:37
这个不同单片机写法是不一样的,一般原厂demo里面都有

就是用在单片机上的

使用特权

评论回复
地板
@若水| | 2017-5-12 16:46 | 只看该作者
宇容创行 发表于 2017-5-11 19:37
这个不同单片机写法是不一样的,一般原厂demo里面都有

如果用标准的C写,所有单片机都一样的吧?

使用特权

评论回复
5
宇容创行| | 2017-5-12 19:10 | 只看该作者
/* Code adapted from Atmel AVR Application Note AVR306
* Interrupt mode driver for UART.
*/
#include <io8515v.h>
#include <macros.h>
#include "uart.h"

/* IMPORTANT: these vector numbers are for 8515! If you use other devices
* you must change them to the different set of numbers.
*
* UART_RX_interrupt set to UART, Rx Complete
* UART_TX_interrupt set to UART Data Register Empty
*/

#pragma interrupt_handler UART_RX_interrupt:10 UART_TX_interrupt:11

/* UART Buffer Defines */
#define UART_RX_BUFFER_SIZE 128 /* 1,2,4,8,16,32,64,128 or 256 bytes */
#define UART_RX_BUFFER_MASK ( UART_RX_BUFFER_SIZE - 1 )
#define UART_TX_BUFFER_SIZE 128 /* 1,2,4,8,16,32,64,128 or 256 bytes */
#define UART_TX_BUFFER_MASK ( UART_TX_BUFFER_SIZE - 1 )

#if ( UART_RX_BUFFER_SIZE & UART_RX_BUFFER_MASK )
#error RX buffer size is not a power of 2
#endif

/* Static Variables */
static unsigned char UART_RxBuf[UART_RX_BUFFER_SIZE];
static volatile unsigned char UART_RxHead;
static volatile unsigned char UART_RxTail;
static unsigned char UART_TxBuf[UART_TX_BUFFER_SIZE];
static volatile unsigned char UART_TxHead;
static volatile unsigned char UART_TxTail;

/* initialize UART */
void InitUART( unsigned char baudrate )
        {
        unsigned char x;
        UBRR = baudrate; /* set the baud rate */
        /* enable UART receiver and transmitter, and
        receive interrupt */
        UCR = ( (1<<RXCIE) | (1<<RXEN) | (1<<TXEN) );
        x = 0; /* flush receive buffer */
        UART_RxTail = x;
        UART_RxHead = x;
        UART_TxTail = x;
        UART_TxHead = x;
        }

/* interrupt handlers */
void UART_RX_interrupt( void )
{
        unsigned char data;
        unsigned char tmphead;
        data = UDR; /* read the received data */
        /* calculate buffer index */
        tmphead = ( UART_RxHead + 1 ) & UART_RX_BUFFER_MASK;
        UART_RxHead = tmphead; /* store new index */
        if ( tmphead == UART_RxTail )
                {
                /* ERROR! Receive buffer overflow */
                }
        UART_RxBuf[tmphead] = data; /* store received data in buffer */
}

void UART_TX_interrupt( void )
{
        unsigned char tmptail;

        /* check if all data is transmitted */
        if ( UART_TxHead != UART_TxTail )
                {
                /* calculate buffer index */
                tmptail = ( UART_TxTail + 1 ) & UART_TX_BUFFER_MASK;
                UART_TxTail = tmptail; /* store new index */
                UDR = UART_TxBuf[tmptail]; /* start transmition */
                }
        else
                {
                UCR &= ~(1<<UDRIE); /* disable UDRE interrupt */
                }
        }

/* Read and write functions */
unsigned char ReceiveByte( void )
        {
        unsigned char tmptail;

        while ( UART_RxHead == UART_RxTail ) /* wait for incomming data */
                ;
        tmptail = ( UART_RxTail + 1 ) & UART_RX_BUFFER_MASK;/* calculate buffer index */
        UART_RxTail = tmptail; /* store new index */
        return UART_RxBuf[tmptail]; /* return data */
        }

void TransmitByte( unsigned char data )
        {
        unsigned char tmphead;
        /* calculate buffer index */
        tmphead = ( UART_TxHead + 1 ) & UART_TX_BUFFER_MASK;
                /* wait for free space in buffer */

        while ( tmphead == UART_TxTail )
                ;
        UART_TxBuf[tmphead] = data; /* store data in buffer */
        UART_TxHead = tmphead; /* store new index */
        UCR |= (1<<UDRIE); /* enable UDRE interrupt */
        }

unsigned char DataInReceiveBuffer( void )
        {
        return ( UART_RxHead != UART_RxTail );
                /* return 0 (FALSE) if the receive buffer is empty */
        }

#ifdef TEST
/* main - a simple test program*/
void main( void )
        {
        InitUART( 25 ); /* set the baudrate to 9600 bps using a 4MHz crystal */
        _SEI(); /* enable interrupts => enable UART interrupts */
        while ( 1 ) /* forever */
                {
                TransmitByte( ReceiveByte() ); /* echo the received character */
                }
        }
#endif

使用特权

评论回复
6
宇容创行| | 2017-5-12 19:12 | 只看该作者
这段代码环形缓存大小固定为2^n,因此算法可以很巧妙精简

使用特权

评论回复
7
Prry| | 2017-5-12 20:17 | 只看该作者
宇容创行 发表于 2017-5-11 19:37
这个不同单片机写法是不一样的,一般原厂demo里面都有

怎会不一样呢,我自己的可以用在各种单片机上和PC上都可以,封装起来做公共函数库用

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:适应环境,不要让环境去适应你、、

5

主题

144

帖子

1

粉丝