在上一次的基础上添加了不同需求缓冲区大小可变的更改。
1 /* 2 * 串口的FIFO简单读取实现 3 * 功能,实现串口的FIFO实现 4 * 使用方法: 5 * 更新时间:2017.9.26 6 * 版本:v2.0.0 7 * 编写:ZhangPeng 8 */ 9 #include "stdio.h" 10 #include <stdlib.h> 11 #include "df_fifo.h" 12 #include <memory.h> 13 /* 14 * 函数功能:Fifo出队操作 15 * 入口参数 16 参数1:缓冲区句柄 17 参数2:要出队的指针地址 18 参数3:出队的个数 19 * 出口参数:是否出队成功 20 * 注意事项:该函数只从头指针开始读取,length个字节,不会影响缓冲区数据 21 */ 22 Df_StateTypeDef Df_FifoOutput(Df_FifoPointTypeDef kfifo, uint8_t * buf, uint8_t length) 23 { 24 Df_FifoStructTypeDef Pfifo; 25 Pfifo = *kfifo; 26 if (Pfifo.Count - length < 0)//缓冲区没有足够的数据 27 { 28 return Df_StateOutOver;//读数据越界 29 } 30 while (length--) 31 { 32 *buf = Pfifo.buffer[Pfifo.read]; 33 buf++; 34 Pfifo.read++;//读取指针自加 35 if (Pfifo.read == Pfifo.length) 36 { 37 Pfifo.read = 0; 38 } 39 } 40 return Df_StateOk;//数据读取成功 41 } 42 43 /* 44 * 函数功能:Fifo入队操作 45 * 入口参数 46 参数1:缓冲区句柄 47 参数2:要入队的指针地址 48 参数3:入队的个数 49 * 出口参数:是否入队成功 50 * 注意事项:该函数在缓冲区满的时候会返回Over 51 */ 52 Df_StateTypeDef Df_FifoInput(Df_FifoPointTypeDef Pfifo, uint8_t * buf, uint8_t length)// 53 { 54 if (Pfifo->Count + length > Pfifo->length)//写入的数据超过缓冲区 55 { 56 return Df_StateInOver;//写数据越界 57 } 58 while (length--) 59 { 60 Pfifo->buffer[Pfifo->write] = *buf;//赋值给缓冲区 61 buf++;//缓冲区地址加一 62 Pfifo->Count++; 63 Pfifo->write++;// 64 if (Pfifo->write == Pfifo->length) 65 { 66 Pfifo->write = 0; 67 } 68 } 69 return Df_StateOk;//数据读取成功 70 } 71 72 /* 73 * 函数功能:缓冲区擦除操作 74 * 入口参数 75 参数1:缓冲区句柄 76 参数2:擦除的数据长度 77 * 出口参数:是否擦除 78 * 注意事项:该函数会将缓冲区头开始的length个数据从缓冲区擦除 79 */ 80 Df_StateTypeDef Df_FifoErase(Df_FifoPointTypeDef Pfifo, uint8_t length)// 81 { 82 if (Pfifo->Count - length > Pfifo->length)//写入的数据超过缓冲区 83 { 84 return Df_StateEaserOver;//写数据越界 85 } 86 while (length--) 87 { 88 Pfifo->Count--; 89 Pfifo->read++;// 90 if (Pfifo->read == Pfifo->length) 91 { 92 Pfifo->read = 0; 93 } 94 } 95 return Df_StateOk;//数据读取成功 96 } 97 98 /* 99 * 函数功能:重置缓冲区 100 * 入口参数 101 参数1:缓冲区句柄 102 参数2:缓冲区数组首地址 103 参数3:缓冲区的大小 104 * 出口参数:是否重置成功 105 * 注意事项:length不能小于buf 的长度否则会导致内存泄漏 106 */ 107 Df_StateTypeDef Df_FifoReset(Df_FifoPointTypeDef Pfifo,uint8_t * buf,uint16_t length) 108 { 109 printf("[%s]the buffer length is %d\n", __FUNCTION__, sizeof(buf)); 110 Pfifo->buffer = buf;//缓冲区数组 111 Pfifo->length = length;//缓冲区大小 112 Pfifo->Count = 0;//数据为空 113 Pfifo->read = 0;//读指针为0 114 Pfifo->write = 0;//写指针为0 115 return Df_StateOk; 116 } 117 118 /* 119 * 函数功能:Fifo出队操作 120 * 入口参数 121 参数1:缓冲区句柄 122 参数2:要出队的指针地址 123 参数3:出队的个数 124 * 出口参数:是否出队成功 125 * 注意事项:该函数只从头指针开始读取,length个字节,同时擦除该部分数据 126 */ 127 Df_StateTypeDef Df_FifoOutEaser(Df_FifoPointTypeDef Pfifo, uint8_t * buf, uint8_t length) 128 { 129 if (Pfifo->Count - length < 0)//缓冲区没有足够的数据 130 { 131 return Df_StateOutOver;//读数据越界 132 } 133 while (length--) 134 { 135 *buf = Pfifo->buffer[Pfifo->read]; 136 buf++; 137 Pfifo->Count--; 138 Pfifo->read++;//读取指针自加 139 if (Pfifo->read == Pfifo->length) 140 { 141 Pfifo->read = 0; 142 } 143 } 144 return Df_StateOk;//数据读取成功 145 } 146 #define debug 1//是否打印缓冲区状态 147 /*测试*/ 148 int main() 149 { 150 Df_FifoStructTypeDef Fifostructdefine; 151 uint8_t buffer[100];//定义缓冲区 152 memset(buffer, 0, 100 * sizeof(uint8_t));//初始化缓冲区 153 uint8_t data[100];//定义应