天天看點

用連結清單實作fifo功能緩存和拼接資料功能

fifo.h

#ifndef  __LIST_QUEUE_H__
#define  __LIST_QUEUE_H__
#include <stdio.h>
#include <stdlib.h>
#include"xil_types.h"
#include<stdint.h>

typedef struct Node
{
	uint8_t data;
	struct Node* next;
}Node;
typedef struct
{
	Node* head;
	Node* tail;
}Queue;
Queue queue;
#define REAL_LEN  12
void push(Queue* pq,uint8_t data);
uint8_t empty(Queue* pq);
uint8_t full(Queue* pq);
void travel(Queue* pq);
int size(Queue* pq);
uint8_t get_head(Queue* pq);
uint8_t get_tail(Queue* pq);
uint8_t pop(Queue* pq);
void clear(Queue* pq);
uint8_t  judge_256head(Queue* pq,int cnt);
uint8_t  judge_128head(Queue* pq,int cnt);
void insert_arr(uint8_t arr[254],int len);
void require_arr(uint8_t brr[256],int len);
void queue_init();
uint8_t  judge_define_head(Queue* pq,int cnt);
#endif 
           

fifo.c

#include"list_queue.h"
uint8_t  head_256[4]={0xeb,0x90,0x33,0x44};
uint8_t  head_128[4]={0xeb,0x90,0x00,0x01};
void clear(Queue* pq)
{
	while(-1 != pop(pq));
}

uint8_t pop(Queue* pq)
{
	if(empty(pq))
	{
		return -1;
	}
	Node* pt = pq->head;
	pq->head = pt->next;
	if(NULL == pq->head)
	{
		pq->tail = NULL;
	}
	uint8_t temp = pt->data;
	free(pt);
	pt = NULL;
	return temp;
}

int size(Queue* pq)
{
	int cnt = 0;
	Node* pt = pq->head;
	while(pt != NULL)
	{
		cnt++;
		pt = pt->next;
	}
	return cnt;
}

uint8_t get_head(Queue* pq)
{
	return empty(pq)?-1:pq->head->data;
}

uint8_t get_tail(Queue* pq)
{
	return empty(pq)?-1:pq->tail->data;
}

void travel(Queue* pq)
{
	Node* pt = pq->head;
	while(pt != NULL)
	{
		//printf("%02x ",pt->data);
		pt = pt->next;
	}
}

uint8_t empty(Queue* pq)
{
	return NULL == pq->head;
}


uint8_t full(Queue* pq)
{
	return 0;
}

void push(Queue* pq,uint8_t data)
{

	Node* pn = (Node*)malloc(sizeof(Node));
	pn->data = data;
	pn->next = NULL;

	if(empty(pq))
	{
		pq->head = pn;
	}
	else
	{
		pq->tail->next = pn;
	}
	pq->tail = pn;
}
u8 head_define[2]={0x11,0x22};
uint8_t  judge_define_head(Queue* pq,int cnt)
{
	Node* pt = pq->head;
	int  ct_head_len=0;
	while(pt != NULL && cnt)
	{
		if(head_define[ct_head_len]==pt->data)
        {
			ct_head_len++;
		}
		 pt = pt->next;
		 cnt--;
	}
    if(ct_head_len==2)
    {
	   return 1;
	}
	else
	{
	   return 0;
	}
    cnt=4;
}
uint8_t  judge_128head(Queue* pq,int cnt)
{
	Node* pt = pq->head;
	int  ct=0;
	while(pt != NULL && cnt)
	{    
		if(head_128[ct]==pt->data)
        {
			 ct++;
		}
		 pt = pt->next;	
		 cnt--;
	}
    if(ct==4)
    {
	   return 1;
	}
	else
	{
	   return 0;
	}   
}
void queue_init()
{
	queue.head = NULL;
	queue.tail = NULL;
}

uint8_t  judge_256head(Queue* pq,int cnt)
{
	Node* pt = pq->head;
	int  ct=0;
	while(pt != NULL && cnt)
	{    
		if(head_256[ct]==pt->data)
        {
			 ct++;
		}
		//鎸囧悜涓嬩竴涓妭鐐?
		 pt = pt->next;	
		 cnt--;
	}
    if(ct==4)
    {
	   return 1;
	}
	else
	{
	   return 0;
	}   
    cnt=4;
}
void insert_arr(uint8_t arr[],int len)
{
    for(int i = 0; i < len; i++)
    {
       push(&queue,arr[i]);  
    }
}
void require_arr(uint8_t brr[],int len)
{
    for(int i = 0; i < len; i++)
    {
       brr[i]=pop(&queue);
    }
}


           

test.c

queue_init();//初始化連結清單
   
 
			   insert_arr(&PP[3],PP[2]);
			   //修改REAL_LEN作為真實的幀長,修改head_define用來修改真實的幀頭,幀頭位元組數量也可以修改1-128長度都是幀率頭都可以
			   if(size(&queue)>(REAL_LEN-1))
			   {
				    if(judge_define_head(&queue,2) )
				    {
					    require_arr(&uart_data_pl[3],REAL_LEN);
						Send_FpgaData_LY(uart_data_pl,2);
				    }
				    else
				    {
					    //下面部分用來相容長度出錯
						while(1)
						{
								  if(judge_define_head(&queue,2) )
								  {
										 break;
								  }
								  if( size(&queue)<REAL_LEN)
								  {
									     break;
								  }
								  pop(&queue);
						}
				    }
			   }






 
					  insert_arr(my_temp,254);
					  while(1)
					  {
							 if(judge_256head(&queue,4))
							 {
								 if(size(&queue)>255)
								 {
									require_arr(send_256data,256);
								 }
								  break; 
							 }
							 if(judge_128head(&queue,4) &&(size(&queue)>127))
							 {
									 require_arr(send_128data,128);
									 if(judge_128head(&queue,4) &&(size(&queue)>127))
									 {
									   require_arr(send_128data,128);
									 }
									  break; 
							 }					
							 pop(&queue);
					  }
           

繼續閱讀