首页 > 代码库 > 一个好用的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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。