环形缓冲区

.h文件

#ifndef _DRIVER_BUFFER_H_
#define _DRIVER_BUFFER_H_

#include "stm32f1xx_hal.h"
#include "stdlib.h"
#include "string.h"

/*******************************************
 * Structure name: RingBuffer ; *ptRingBuffer
 * member        : fifo     -> Defines a pointer to the first address of the ring buffer
                   pw/pr    -> Read / write pointer (equivalent to the subscript of the array), defined as short type
                               If it is defined as char type, it can only store 128 bytes at most, which may not be enough
                   buf_size -> Size of ring buffer
*******************************************/
typedef struct
{
    uint8_t *fifo;  /* Defines a pointer to the first address of the ring buffer */
    uint16_t pw;    
    uint16_t pr;        
    volatile uint16_t buf_size;
}RingBuffer, *ptRingBuffer;

/* Function declaration */
int DriverBuffer_Init(ptRingBuffer buffer, uint16_t size);
int DriverBuffer_Write(ptRingBuffer buffer, const uint8_t data);
int DriverBuffer_Read(ptRingBuffer buffer, uint8_t *data);
int DriverBuffer_WriteBytes(ptRingBuffer buffer, const uint8_t *data_stream, uint8_t len);
int DriverBuffer_ReadBytes(ptRingBuffer buffer, uint8_t *data_stream, uint8_t len);
int DriverBuffer_Clean(ptRingBuffer buffer);

#endif

.c文件

#include "ring_buffer.h"
#include "stdio.h"

/*******************************************
 * function name : DriverBuffer_Init
 * description   : Initialize the ring buffer, allocate space for it, 
                   and clear the read / write pointer
 * input params  : buffer -> Points to the ring buffer that needs to be initialized
                   size   -> The size of space allocated for the ring buffer
 * return type   : int
 * return value  : Success -> return 0
                   Failed  -> return -1
*******************************************/
int DriverBuffer_Init(ptRingBuffer buffer, uint16_t size)
{
    /* Parameter judgment */
    if(buffer == NULL || size == 0) return -1;
    /* Allocate space to the ring buffer */
    if(buffer->fifo == NULL)
    {
        buffer->fifo = (uint8_t *)malloc(size);
        if(buffer->fifo == NULL)    return -1;
    }
    /* Clear read / write pointer */
    buffer->pr = buffer->pw = 0;
    buffer->buf_size = size;
    return 0;
}

/*******************************************
 * function name : DriverBuffer_Write
 * description   : Writes a character to the specified ring buffer
 * input params  : buffer -> Struct pointer to the target ring buffer
                   data   -> Characters written
 * return type   : int
 * return value  : Success -> return 0
                   Failed  -> return -1
*******************************************/
int DriverBuffer_Write(ptRingBuffer buffer, const uint8_t data)
{
    /* Parameter judgment */
    if(buffer == NULL || buffer->fifo == NULL)  return -1;
    /* Determine whether the ring buffer is full */
    int i = (buffer->pw + 1) % buffer->buf_size;
    if(buffer->pr != i)     /* Not full */
    {
        buffer->fifo[buffer->pw] = data;    /* Write one byte of data */
        buffer->pw = i;
        return 0;
    }
    return -1;
}

/*******************************************
 * function name : DriverBuffer_Read
 * description   : Reads a character from the specified ring buffer
 * input params  : buffer -> Struct pointer to the target ring buffer
                   data   -> Characters written
 * return type   : int
 * return value  : Success -> return 0
                   Failed  -> return -1
*******************************************/
int DriverBuffer_Read(ptRingBuffer buffer, uint8_t *data)
{
    /* Parameter judgment(buffer cannot be NULL, buffer->fifo cannot be null too) */
    if(buffer == NULL || buffer->fifo == NULL || data == NULL)  return -1;
    /* Determine whether the ring buffer is not empty */
    if(buffer->pr != buffer->pw)
    {
        *data = buffer->fifo[buffer->pr];    /* Read one byte of data */
        buffer->pr = (buffer->pr + 1) % buffer->buf_size;
        return 0;
    }
    return -1;
}

/*******************************************
 * function name : DriverBuffer_WriteBytes
 * description   : Writes multiple bytes of data to the specified ring buffer
 * input params  : buffer      -> Struct pointer to the target ring buffer
                   data_stream -> Written string
                   len         -> How many bytes are written
 * return type   : int
 * return value  : i -> The number of characters successfully written. 
                        If it is equal to the parameter len, 
                        the writing is complete. 
                        If it is not equal, the writing is incomplete
*******************************************/
int DriverBuffer_WriteBytes(ptRingBuffer buffer, const uint8_t *data_stream, uint8_t len)
{
    /* Parameter judgment */
    if(buffer == NULL || buffer->fifo == NULL || data_stream == NULL || len == 0)  return -1;
    
    int i;
    for(i = 0; i < len; i++)
    {
        if(DriverBuffer_Write(buffer, data_stream[i]) != 0) break;   /* Full, break */
    }
    return i;
}

/*******************************************
 * function name : DriverBuffer_ReadBytes
 * description   : Reads multiple bytes of data from the specified ring buffer
 * input params  : buffer      -> Struct pointer to the target ring buffer
                   data_stream -> Used to store the read data
                   len         -> How many bytes of data are you going to read
 * return type   : int
 * return value  : i -> The size of the read data, the caller only needs to 
                        judge whether it is equal to the parameter len. 
                        If it is equal, the reading is completed. 
                        If it is not equal, the reading is not completed
*******************************************/
int DriverBuffer_ReadBytes(ptRingBuffer buffer, uint8_t *data_stream, uint8_t len)
{
    /* Parameter judgment */
    if(data_stream == NULL || len == 0)  return -1;

    int i = 0;
    for(i = 0; i < len; i++)
    {   /* The ring buffer is empty, break */
        if(DriverBuffer_Read(buffer, &data_stream[i]) != 0) break; 
    }
    return i;
}

/*******************************************
 * function name : DriverBuffer_Clean
 * description   : Clears the specified ring buffer
 * input params  : buffer -> Struct pointer to the target ring buffer
 * return type   : int
 * return value  : Success -> return 0
                   Failed  -> return -1
*******************************************/
int DriverBuffer_Clean(ptRingBuffer buffer)
{
    /* Parameter judgment */
    if(buffer == NULL || buffer->fifo == NULL)  return -1;
    /* Clear its contents, Clear the read / write pointer to zero */
    memset(buffer->fifo, 0, buffer->buf_size);
    buffer->pr = buffer->pw = 0;
    
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值