[转]优雅地用宏实现环形缓冲区
2021-09-09 14:24:31 Author: blog.csdn.net(查看原文) 阅读量:42 收藏

点击下方名片,关注公众号

大家好,周末快乐,我是小麦,今天分享一下CBUF


循环缓冲区是嵌入式软件工程师在日常开发过程中的关键组件。

多年来,互联网上出现了许多不同的循环缓冲区实现和示例。我非常喜欢这个模块,可以GitHub上找到这个开源的 CBUF.h 模块。

地址:https://github.com/barraq/BRBrain/blob/master/firmware/CBUF.h

CBUF.h 模块使用宏实现循环缓冲区,具体源码如下所示;

#if !defined( CBUF_H )
#define CBUF_H       /**< Include Guard                          */

/* ---- Include Files ---------------------------------------------------- */

/* ---- Constants and Types ---------------------------------------------- */

/**
*   Initializes the circular buffer for use.
*/ 

#define CBUF_Init( cbuf )       cbuf.m_getIdx = cbuf.m_putIdx = 0

/**
*   Returns the number of elements which are currently contained in the 
 *  circular buffer.
*/

#define CBUF_Len( cbuf )        ((typeof( cbuf.m_putIdx ))(( cbuf.m_putIdx ) - ( cbuf.m_getIdx )))

/**
*   Appends an element to the end of the circular buffer
*/

#define CBUF_Push( cbuf, elem ) (cbuf.m_entry)[ cbuf.m_putIdx++ & (( cbuf##_SIZE ) - 1 )] = (elem)

/**
*   Retrieves an element from the beginning of the circular buffer
*/

#define CBUF_Pop( cbuf )        (cbuf.m_entry)[ cbuf.m_getIdx++ & (( cbuf##_SIZE ) - 1 )]

/**
*   Retrieves the i'th element from the beginning of the circular buffer
*/

#define CBUF_Get( cbuf, idx )        (cbuf.m_entry)[( cbuf.m_getIdx + idx ) & (( cbuf##_SIZE ) - 1 )]

/**
*   Retrieves the i'th element from the end of the circular buffer
*/

#define CBUF_GetEnd( cbuf, idx )        (cbuf.m_entry)[( cbuf.m_putIdx - idx - 1 ) & (( cbuf##_SIZE ) - 1 )]

/**
*   Determines if the circular buffer is empty
*/

#define CBUF_IsEmpty( cbuf )    ( CBUF_Len( cbuf ) == 0 )

/**
*   Determines if the circular buffer is full.
*/

#define CBUF_IsFull( cbuf )     ( CBUF_Len( cbuf ) == ( cbuf##_SIZE ))

/**
*   Determines if the circular buffer is currenly overflowed or underflowed.
*/

#define CBUF_Error( cbuf )      ( CBUF_Len( cbuf ) > cbuf##_SIZE )

#if defined( __cplusplus )

template < class IndexType, unsigned Size, class EntryType >
class CBUF
{
public:

    CBUF()
    {
        m_getIdx = m_putIdx = 0;
    }

    IndexType Len() const   { return m_putIdx - m_getIdx; }

    bool IsEmpty() const    { return Len() == 0; }
    bool IsFull() const     { return Len() == Size; }
    bool Error() const      { return Len() > Size; }

    void Push( EntryType val )   
    {
        m_entry[ m_putIdx++ & ( Size - 1 )] = val;
    }

    EntryType Pop()
    {
        return m_entry[ m_getIdx++ & ( Size - 1 )];
    }

private:

    volatile IndexType  m_getIdx;
    volatile IndexType  m_putIdx;
    EntryType           m_entry[ Size ];

};

#endif  // __cplusplus

/* ---- Variable Externs ------------------------------------------------- */
/* ---- Function Prototypes ---------------------------------------------- */

/** @} */

#endif // CBUF_H

现在一般我不喜欢以这种方式使用宏,但实现已被证明是快速、高效且工作相对良好的,这是很难争论的。

循环缓冲区的设置非常简单。首先,需要定义循环缓冲区的大小。这是通过定义宏 myQ_SIZE 来完成的,同时记住缓冲区大小需要是 2 的幂

然后通过创建一个 myQ 类型的变量来声明循环缓冲区。例如,如果 myQ_SIZE 定义为 64 字节,则可以定义 UART 的发送和接收缓冲区,如下面的图 1 所示。

图 1 – 定义循环缓冲区

在此示例中,myQ 被定义为静态以限制缓冲区的范围并声明为易失性,因为它们在中断内被修改。定义循环缓冲区只是第一步。为了分配缓冲区,必须将这些变量传递给 CBUF_INIT 宏,如下图 2 所示。

图 2 – 缓冲区初始化

除了这个初始设置之外,缓冲区相当简单且易于使用。例如,可以使用 CBUF_PUSH 将通过串行接口接收 UART接收的字符推送到循环缓冲区,如图 3 所示。

图 3 – 推入缓冲区

开发人员不仅希望将数据推送到循环缓冲区上,还希望从缓冲区弹出或获取数据。看到这一点的一个简单示例是需要获取字符并通过 UART 传输的串行发送器。图 4 中可以看到一个示例传输函数。

图 4 – 从缓冲区弹出数据

在健壮的应用程序中,还应检查循环缓冲区长度和溢出状态。CBUF 模块确实提供了能够检查这些重要指标的宏。

要记住的一个重要问题是,如果需要对 CBUF 本身进行任何调试,这是不可能的。无法为宏设置断点,因此如果出现问题,则需要对模块进行功能化以逐步执行和调试。

多年来使用这个模块虽然我没有发现任何问题。循环缓冲区是在嵌入式系统中与串行设备通信的一个重要方面。

循环缓冲区也很好理解,应该创建它们以便它们可以模块化并从一个应用程序到下一个应用程序重复使用。

到目前为止,CBUF 模块已被证明是这样一个模块,所以在这里,我强烈推荐一下这个模块。好了,今天的文章就到这里,我们下期再见。

—— The End ——

推荐好文  点击蓝色字体即可跳转

 函数指针,这6点你学会了吗?

☞ 5个实践技巧,助你设计出优秀的RTOS程序

☞ 赚钱了

☞ RingBuff在多核通讯之间的妙用

原创不易,欢迎转发、留言、点赞、分享给你的朋友,感谢您的支持!

长按识别二维码关注我

你点的每个好看,我都认真当成了喜欢


文章来源: https://blog.csdn.net/21cnbao/article/details/120213217
如有侵权请联系:admin#unsafe.sh