FIFO

                   FIFO

在数据结构中,有一项内容叫队列。它是先进先出的一种结构,在操作系统中普遍使用并形成了消息队列这样强大的功能,对于有些信息,CPU没办法及时处理,就可以通过队列来做为缓存。这里要说的是它最基本的结构——先进先出。用过STM32的人会发现,在硬件上有一个叫FIFO的数据缓冲区,其实就是个队列,只不过它是硬件上处理的,不需要CPU处理。而很多单片机并没有硬件FIFO资源,那我们可以用软件来写一个。

一、这是我从一个官方例程中看到的fifo结构

 1 #include "fifo.h"
 2 
 3 static uint16_t FifoNext( tFifo *fifo, uint16_t index )
 4 {
 5     return ( index + 1 ) % fifo->Size;
 6 }
 7 
 8 void FifoInit( tFifo *fifo, uint16_t *buffer, uint16_t size )
 9 {
10     fifo->Begin = 0;
11     fifo->End = 0;
12     fifo->Data = buffer;
13     fifo->Size = size;
14 }
15 
16 void FifoPush( tFifo *fifo, uint16_t data )
17 {
18     fifo->End = FifoNext( fifo, fifo->End );
19     fifo->Data[fifo->End] = data;
20 }
21 
22 uint16_t FifoPop( tFifo *fifo )
23 {
24     uint16_t data = fifo->Data[FifoNext( fifo, fifo->Begin )];
25 
26     fifo->Begin = FifoNext( fifo, fifo->Begin );
27     return data;
28 }
29 
30 void FifoFlush( tFifo *fifo )
31 {
32     fifo->Begin = 0;
33     fifo->End = 0;
34 }
35 
36 bool IsFifoEmpty( tFifo *fifo )
37 {
38     return ( fifo->Begin == fifo->End );
39 }
40 
41 bool IsFifoFull( tFifo *fifo )
42 {
43     return ( FifoNext( fifo, fifo->End ) == fifo->Begin );
44 }
 1 #ifndef __FIFO_H__
 2 #define __FIFO_H__
 3 
 4 #include <stdbool.h>
 5 
 6 
 7 typedef struct sFifo
 8 {
 9     uint16_t Begin;
10     uint16_t End;
11     uint16_t *Data;
12     uint16_t Size;
13 }tFifo;
14 
15 void FifoInit( tFifo *fifo, uint16_t *buffer, uint16_t size );
16 
17 void FifoPush( tFifo *fifo, uint16_t data );
18 
19 uint16_t FifoPop( tFifo *fifo );
20 
21 void FifoFlush( tFifo *fifo );
22 
23 bool IsFifoEmpty( tFifo *fifo );
24 
25 bool IsFifoFull( tFifo *fifo );
26 
27 #endif // __FIFO_H__

二、这是我从网上见到的并修改正在使用的FIFO结构 1 #include "fifo.h"

 2 
 3 
 4 
 5 /************************************************************** 
 6 * 函数名  : FIFO_Init
 7 * 描述    : fifo初始化
 8 * 输入    : fifo寄存器
 9 * 输出    : None
10 *************************************************************/
11 void FIFO_Init(FifoTypedef *Fifo) 
12 {
13     Fifo->front = Fifo->rear;            //初始化时队列头尾相连
14     Fifo->count = 0;                           //队列数为0
15 }
16 
17 
18 
19 /************************************************************** 
20 * 函数名  : FIFO_In
21 * 描述    : 数据进fifo
22 * 输入    : *Fifo:fifo寄存器
23                         *sdat:队列内容
24                         len:数据长度
25 * 输出    : 操作结果
26 *************************************************************/
27 unsigned char FIFO_In(FifoTypedef *Fifo, unsigned char *sdat, unsigned int len)
28 {
29     unsigned int i;    
30     
31     if((Fifo->front == Fifo->rear) && (Fifo->count == FIFO_Size))
32     {                   
33         return FIFO_Full;       //队列已满
34     }
35     else
36     {   
37         memcpy(Fifo->dat[Fifo->rear], sdat, len);    //拷入队列41         Fifo->rear = (Fifo->rear + 1) % FIFO_Size;
42         Fifo->count = Fifo->count + 1;
43         return FIFO_OperateOk; 
44     }
45 }
46 
47 
48 
49 /************************************************************** 
50 * 函数名  : FIFO_Out
51 * 描述    : 读出fifo数据
52 * 输入    : *Fifo:fifo寄存器
53                         *sdat:读出数据存放地址
54                         len:数据长度
55 * 输出    : 操作结果
56 *************************************************************/
57 unsigned char FIFO_Out(FifoTypedef *Fifo, unsigned char *sdat, unsigned int len)   
58 {
59     unsigned int i;    
60     
61     if((Fifo->front == Fifo->rear) && (Fifo->count == 0))
62     {
63         return FIFO_Empty;        //队列为空
64     }
65     else
66     {   

       memcpy(sdat, Fifo->dat[Fifo->rear], len);    //输出队列内容
       memset(Fifo->dat[Fifo->front], 0, len);
72         Fifo->front = (Fifo->front + 1) % FIFO_Size;
73         Fifo->count = Fifo->count - 1;
74         return FIFO_OperateOk;
75     }
76 }
 1 #ifndef __FIFO_H      
 2 #define __FIFO_H
 3  
 4 
 5 #define FIFO_Dat       129             //fifo队列内容大小
 6 #define FIFO_Size      15                 //fifo队列大小
 7 #define FIFO_Full      0              //fifo已满
 8 #define FIFO_Empty     1        //fifo有空
 9 #define FIFO_OperateOk 2              //队列操作完成
10 
11 typedef struct 
12 {
13     unsigned short front;             //队列头
14     unsigned short rear;            //队列尾
15     unsigned short count;              //队列计数
16     unsigned char dat[FIFO_Size][FIFO_Dat];    //队列内容
17 }FifoTypedef;
18 
24 
25 extern void FIFO_Init(FifoTypedef *Fifo);            //fifo初始化
26 extern unsigned char FIFO_In(FifoTypedef *Fifo, unsigned char *sdat, unsigned int len);        //数据进fifo
27 extern unsigned char FIFO_Out(FifoTypedef *Fifo, unsigned char *sdat, unsigned int len);    //数据出fifo
28
29
#endif


 

原文地址:https://www.cnblogs.com/wcw12580/p/11277463.html