当前位置:网站首页>FIFO code implemented in C language
FIFO code implemented in C language
2022-06-26 00:59:00 【To, violet】
Recently, when I was working on a small project, I needed to use FIFO As a cache of data . But the chip used by the author has no hardware FIFO Manager , So we need to use C Language to use software to make a FIFO Management of .
I began to quote some code of the great God
(5 Bar message ) C Language implementation standard FIFO_jdh99 The column -CSDN Blog _c fifo
https://blog.csdn.net/jdh99/article/details/83789431?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164501280416780366574110%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=164501280416780366574110&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-2-83789431.pc_search_insert_ulrmf&utm_term=C%E8%AF%AD%E8%A8%80FIFO&spm=1018.2226.3001.4187 But the code has some BUG,BUG Specifically, : For example, it defines 10 Bytes of FIFO, write in 10 Bytes , read out 6 Bytes , Write again 6 An overflow occurs after bytes , Cause data error . So the author modified the code on the basis of the original author , Revised as follows :
FIFO.c file
#include "FIFO.h"
/**
* @brief fifo structure
*/
typedef struct
{
int ptr_write;// The amount of writing
int ptr_read;// Read volume
bool is_full;//FIFO state
int item_sum;// fifo Number of elements stored in , Not byte size
int item_size;// Element size . Company : byte
void *fifo_ptr; //FIFO Address
} Fifo;
/**
* @brief establish fifo
* @param item_sum:fifo Number of elements in . Note that it's not the number of bytes
* @param item_size: Element size . Company : byte
* @return fifo Indexes
*/
int fifo_create(int item_sum, int item_size)
{
Fifo *fifo = (Fifo *)malloc(sizeof(Fifo));
fifo->item_sum = item_sum;
fifo->item_size = item_size;
fifo->ptr_write = 0;
fifo->ptr_read = 0;
fifo->is_full = false;
fifo->fifo_ptr = (void *)malloc(item_sum * item_size);
return (int)fifo;
}
/**
* @brief Delete fifo
* @param fifo_index: fifo Indexes
*/
void fifo_delete(int fifo_index)
{
Fifo *fifo = (Fifo *)fifo_index;
free(fifo->fifo_ptr);
fifo->fifo_ptr = NULL;
free(fifo);
fifo = NULL;
}
/**
* @brief fifo Check whether it can be written
* @param fifo_index: fifo Indexes
* @retval false: Can't write .true: Can write
*/
bool fifo_writeable(int fifo_index)
{
Fifo *fifo = (Fifo *)fifo_index;
return !fifo->is_full;
}
/**
* @brief fifo write in
* @param fifo_index: fifo Indexes
* @param frame: Write element pointer
* @return false: Failure .true: success
*/
bool fifo_write(int fifo_index, void *data)
{
Fifo *fifo = (Fifo *)fifo_index;
if (fifo->is_full)// Can I write
{
return false;
}
memcpy((char *)(fifo->fifo_ptr) + fifo->ptr_write * fifo->item_size, data, fifo->item_size);// Copy the data to FIFO in
fifo->ptr_write++;// The amount of writing +1
if (fifo->ptr_write >= fifo->item_sum)
{
fifo->is_full = true;//FIFO full
}
return true;
}
/**
* @brief fifo Batch write
* @param fifo_index: fifo Indexes
* @param data: Write element pointer
* @param item_num: Number of elements written
* @return false: Failure .true: success
*/
bool fifo_write_batch(int fifo_index, void *data, int item_num)
{
Fifo *fifo = (Fifo *)fifo_index;
if (fifo_writeable_item_count((int)fifo) < item_num)
{
return false;
}
memcpy((char *)(fifo->fifo_ptr) + fifo->ptr_write * fifo->item_size, data, fifo->item_size * item_num);
fifo->ptr_write += item_num;
if (fifo->ptr_write >= fifo->item_sum)
{
fifo->is_full = true;//FIFO full
}
return true;
}
/**
* @brief fifo Check if it is possible to read
* @param fifo_index: fifo Indexes
* @return false: Not readable .true: Can read
*/
bool fifo_readable(int fifo_index)
{
Fifo *fifo = (Fifo *)fifo_index;
if (fifo->ptr_write == 0)// The amount of writing is 0 Unreadable
{
return false;
}
return true;
}
/**
* @brief fifo Read
* @param fifo_index: fifo Indexes
* @param data: Read data
* @return false: Failure .true: success
*/
bool fifo_read(int fifo_index, void *data)
{
Fifo *fifo = (Fifo *)fifo_index;
if (fifo->ptr_write == fifo->ptr_read && !fifo->is_full)
{
return false;
}
memcpy(data, (char *)(fifo->fifo_ptr), fifo->item_size);
fifo->ptr_read ++;
//-----------------------------------------//
memcpy((char *)(fifo->fifo_ptr),(char *)(fifo->fifo_ptr) + fifo->ptr_read * fifo->item_size,(fifo->item_sum - fifo->ptr_read));// The overall data is aligned to the left
fifo->ptr_write -= fifo->ptr_read;// The amount of data written -= The amount of data read out
fifo->ptr_read = 0;// Read quantity reset
//========================================//
fifo->is_full = false;
return true;
}
/**
* @brief fifo Bulk read
* @param fifo_index: fifo Indexes
* @param data: Read data
* @param item_num: Number of elements read
* @return false: Failure .true: success
*/
bool fifo_read_batch(int fifo_index, void *data, int item_num)
{
Fifo *fifo = (Fifo *)fifo_index;
if (fifo_readable_item_count((int)fifo) < item_num)
{
return false;
}
memcpy(data, (char *)(fifo->fifo_ptr), fifo->item_size * item_num);
fifo->ptr_read += item_num;
//-----------------------------------------//
memcpy((char *)(fifo->fifo_ptr),(char *)(fifo->fifo_ptr) + fifo->ptr_read * fifo->item_size,(fifo->item_sum - fifo->ptr_read));// The overall data is aligned to the left
fifo->ptr_write -= fifo->ptr_read;// The amount of data written -= The amount of data read out
fifo->ptr_read = 0;// Read quantity reset
//========================================//
fifo->is_full = false;
return true;
}
/**
* @brief fifo Number of readable elements
* @param fifo_index: fifo Indexes
* @return Number of elements
*/
int fifo_readable_item_count(int fifo_index)
{
Fifo *fifo = (Fifo *)fifo_index;
if (fifo->is_full)
{
return fifo->item_sum;
}
else
{
return fifo->ptr_write;
}
}
/**
* @brief fifo Number of elements that can be written
* @param fifo_index: fifo Indexes
* @return Number of elements
*/
int fifo_writeable_item_count(int fifo_index)
{
Fifo *fifo = (Fifo *)fifo_index;
if (fifo->is_full)
{
return 0;
}
else
{
return fifo->item_sum - fifo->ptr_write;
}
}
int FIFO1;
int Count;
bool error;
unsigned char linshi[] = "lingshishuju";
unsigned char ceshi[]="1234567890";
unsigned char g_cEE[11];
void TestFifo()
{
FIFO1 = fifo_create(10,1);// establish FIFO depth 10 byte 1
error = fifo_writeable(FIFO1);// Whether it can be written
error = fifo_readable(FIFO1);// Is it readable?
Count = fifo_writeable_item_count(FIFO1);// Amount of data that can be written
fifo_write(FIFO1, ceshi);// Write data
fifo_write(FIFO1, &ceshi[1]);// Write data
error = fifo_readable(FIFO1);// Is it readable?
Count = fifo_readable_item_count(FIFO1);// Amount of readable data
fifo_read(FIFO1,g_cEE);// read
fifo_write(FIFO1, &ceshi[2]);// Write data
fifo_read(FIFO1,&g_cEE[1]);// read
fifo_read(FIFO1,&g_cEE[2]);// read
// Batch
Count = fifo_writeable_item_count(FIFO1);// Amount of data that can be written
error = fifo_write_batch(FIFO1,linshi,Count);// Batch write
error = fifo_read_batch(FIFO1,g_cEE,3);// Read in bulk
error = fifo_writeable(FIFO1);// Whether it can be written
Count = fifo_writeable_item_count(FIFO1);// Amount of data that can be written
error = fifo_write_batch(FIFO1,linshi,Count);// Batch write
Count = fifo_readable_item_count(FIFO1);// Amount of readable data
error = fifo_read_batch(FIFO1,g_cEE,Count);// Read in bulk
error = fifo_writeable(FIFO1);// Whether it can be written
error = fifo_readable(FIFO1);// Is it readable?
}
FIFO.h file
#ifndef _FIFO_H_
#define _FIFO_H_
#include "stdlib.h"
#include "stdio.h"
#include "stdarg.h"
#include "stdbool.h"
#include "string.h"
#include "math.h"
/**
* @brief establish fifo
* @param item_sum:fifo Number of elements in . Note that it's not the number of bytes
* @param item_size: Element size . Company : byte
* @return fifo Indexes
*/
extern int fifo_create(int item_sum, int item_size);
/**
* @brief Delete fifo
* @param fifo_index: fifo Indexes
*/
extern void fifo_delete(int fifo_index);
/**
* @brief fifo Check whether it can be written
* @param fifo_index: fifo Indexes
* @retval false: Can't write .true: Can write
*/
extern bool fifo_writeable(int fifo_index);
/**
* @brief fifo write in
* @param fifo_index: fifo Indexes
* @param frame: Write element pointer
* @return false: Failure .true: success
*/
extern bool fifo_write(int fifo_index, void *data);
/**
* @brief fifo Batch write
* @param fifo_index: fifo Indexes
* @param data: Write element pointer
* @param @param item_num: Number of elements written
* @return false: Failure .true: success
*/
extern bool fifo_write_batch(int fifo_index, void *data, int item_num);
/**
* @brief fifo Check if it is possible to read
* @param fifo_index: fifo Indexes
* @return false: Not readable .true: Can read
*/
extern bool fifo_readable(int fifo_index);
/**
* @brief fifo Read
* @param fifo_index: fifo Indexes
* @param data: Read data
* @return false: Failure .true: success
*/
extern bool fifo_read(int fifo_index, void *data);
/**
* @brief fifo Bulk read
* @param fifo_index: fifo Indexes
* @param data: Read data
* @param item_num: Number of elements read
* @return false: Failure .true: success
*/
extern bool fifo_read_batch(int fifo_index, void *data, int item_num);
/**
* @brief fifo Number of readable elements
* @param fifo_index: fifo Indexes
* @return Number of elements
*/
extern int fifo_readable_item_count(int fifo_index);
/**
* @brief fifo Number of elements that can be written
* @param fifo_index: fifo Indexes
* @return Number of elements
*/
extern int fifo_writeable_item_count(int fifo_index);
extern void TestFifo(void);
#endif
In the above code ,TestFifo() Function is FIFO Function test code of .
Last , Thanks to the original author of the code , It saves a lot of time for my follow-up development .
边栏推荐
- C another new class is ICO? And app Use of config
- Solve STM32 operation μ Solution to sudden failure of task scheduling in c/os-ii system
- react + router 框架下的路由跳转后缓存页面存初始参数
- 认识map
- C thread pool control semaphore
- 安卓缓存使用工具类
- Idea view unit test coverage
- Px4 system terminal for pixhawk
- Permission design = function permission + Data permission
- Establish a j-link GDB cross debugging environment for Px4
猜你喜欢

Preorder and middle order traversal of forest

渗透工具-Burpsuite

1-9network configuration in VMWare

返回值为Object型方法调用equals()

Analyze the five root causes of product development failure

Balanced binary tree AVL

Return value is object type method call equals()

Ad20 (Altium designer) PCB highlight network

C # operate with MySQL

ciscn_2019_en_2
随机推荐
Return value is object type method call equals()
How product managers control the progress of product development
STM32 uses SPI mode to drive TFT-LCD optimization code of hx8347 scheme
简单 deepclone
ASP. Net cache cache usage
MySQL custom function instance
jarvisoj_ level2_ x64
Nacos注册中心
Dynamic verification code
Template engine - FreeMarker first experience
How to design the product roadmap?
AD20(Altium Designer) PCB 高亮网络
Solve STM32 operation μ Solution to sudden failure of task scheduling in c/os-ii system
Web学习之TypeScript
返回值为Object型方法调用equals()
Sqlserver is case sensitive
Chapter VI exercises (678) [microcomputer principles] [exercises]
C IO stream (I) basic concept_ Basic definition
事物/现象/事情/东西/情况/表象
Kylin