首页 > 代码库 > 一个好用的fifo

一个好用的fifo

改造的Nordic库中的 E:\nRF52_SDK_0.9.2_dbc28c9\components\libraries\fifo

 

sensor_fifo.c

#include "sensor_fifo.h"static __INLINE uint32_t fifo_length(sensor_t * p_fifo){  uint32_t tmp = p_fifo->read_pos;  return p_fifo->write_pos - tmp;}#define FIFO_LENGTH fifo_length(p_fifo)  uint32_t sensor_init(sensor_t * p_fifo, int16_t * p_buf, uint16_t buf_size){        if (p_buf == NULL)    {        return 0;    }    p_fifo->p_buf         = p_buf;    p_fifo->buf_size_mask = buf_size - 1;    p_fifo->read_pos      = 0;    p_fifo->write_pos     = 0;    return 1;}uint32_t sensor_put(sensor_t * p_fifo, int16_t byte){    if (FIFO_LENGTH <= p_fifo->buf_size_mask)    {        p_fifo->p_buf[p_fifo->write_pos & p_fifo->buf_size_mask] = byte;        p_fifo->write_pos++;        return 1;    }    return 0;}uint32_t sensor_get(sensor_t * p_fifo, int16_t * p_byte){    if (FIFO_LENGTH != 0)    {        *p_byte = p_fifo->p_buf[p_fifo->read_pos & p_fifo->buf_size_mask];        p_fifo->read_pos++;        return 1;    }    return 0;}uint32_t sensor_flush(sensor_t * p_fifo){    p_fifo->read_pos = p_fifo->write_pos;    return 1;}

 

sensor_fifo.h

#ifndef SENSOR_FIFO_H__#define SENSOR_FIFO_H__#include <stdint.h>#include <stdlib.h>typedef struct{    int16_t *          p_buf;               uint16_t           buf_size_mask;       volatile uint32_t  read_pos;            volatile uint32_t  write_pos;       } sensor_fifo_t;uint32_t sensor_fifo_init(sensor_fifo_t * p_fifo, uint8_t * p_buf, uint16_t buf_size);uint32_t sensor_fifo_get(sensor_fifo_t * p_fifo, uint8_t * p_byte);uint32_t sensor_fifo_flush(sensor_fifo_t * p_fifo);#endif 

 

一个好用的fifo