.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;
}