1、Ping Pong Buffer 原理分析
基本原理如上图所示,当设备有数据来时,先放入缓冲区1
然后将缓冲区1的数据放入缓冲区2,这时缓冲区1可接收下次数据。工作区可从缓冲区2拿数据
2、C++代码实现
相关结构体创建
typedef struct
{
void *buffer[2];
volatile uint8_t writeIndex;
volatile uint8_t readIndex;
volatile uint8_t readAvaliable[2];
} PingPongBuffer_t;
void *buffer[2] 无类型指针,指向不同数据类型的缓冲Buff
volatile uint8_t writeIndex 缓冲写索引
volatile uint8_t readIndex 缓冲读索引
volatile uint8_t readAvaliable[2] 可读标志位
相关函数实现
/**
* @brief Ping-pong buffer initialization
* @param ppbuf: Pointer to the ping-pong buffer structure
* @param buf0: Pointer to the first buffer
* @param buf1: Pointer to the second buffer
* @retval None
*/
void PingPongBuffer_Init(PingPongBuffer_t *ppbuf, void *buf0, void *buf1)
{
memset(ppbuf, 0, sizeof(PingPongBuffer_t));
ppbuf->buffer[0] = buf0;
ppbuf->buffer[1] = buf1;
}
/**
* @brief Get a readable buffer
* @param ppbuf: Pointer to the ping-pong buffer structure
* @param pReadBuf: Pointer to the pointer to the buffer to be read
* @retval Returns true if there is a buffer to be read
*/
bool PingPongBuffer_GetReadBuf(PingPongBuffer_t *ppbuf, void **pReadBuf)
{
if (ppbuf->readAvaliable[0])
{
ppbuf->readIndex = 0;
}
else if (ppbuf->readAvaliable[1])
{
ppbuf->readIndex = 1;
}
else
{
return false;
}
*pReadBuf = ppbuf->buffer[ppbuf->readIndex];
return true;
}
/**
* @brief Notify buffer read completion
* @param ppbuf: Pointer to the ping-pong buffer structure
* @retval None
*/
void PingPongBuffer_SetReadDone(PingPongBuffer_t *ppbuf)
{
ppbuf->readAvaliable[ppbuf->readIndex] = false;
}
/**
* @brief Get writable buffer
* @param ppbuf: Pointer to the ping-pong buffer structure
* @param pWriteBuf: Pointer to the pointer to the buffer to be wriye
* @retval None
*/
void PingPongBuffer_GetWriteBuf(PingPongBuffer_t *ppbuf, void **pWriteBuf)
{
if (ppbuf->writeIndex == ppbuf->readIndex)
{
ppbuf->writeIndex = !ppbuf->readIndex;
}
*pWriteBuf = ppbuf->buffer[ppbuf->writeIndex];
}
/**
* @brief Notify buffer write completion
* @param ppbuf: Pointer to the ping-pong buffer structure
* @retval None
*/
void PingPongBuffer_SetWriteDone(PingPongBuffer_t *ppbuf)
{
ppbuf->readAvaliable[ppbuf->writeIndex] = true;
ppbuf->writeIndex = !ppbuf->writeIndex;
}