码迷,mamicode.com
首页 > 其他好文 > 详细

一个好用的fifo

时间:2016-10-20 21:51:47      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:

改造的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

标签:

原文地址:http://www.cnblogs.com/dong1/p/5982208.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!