FIFO程序设计解析(队列指针)
typedef struct { QUEUE_DATA_TYPE *Out; QUEUE_DATA_TYPE *In; QUEUE_DATA_TYPE *End; u16 NData; u16 MaxData; u8 (* ReadEmpty)(); u8 (* WriteFull)(); QUEUE_DATA_TYPE Buf[1]; u32 SizeOfBuf, u8 (* ReadEmpty)(), u8 (* WriteFull)() ) DataQueue *Queue; if (Buf != NULL && SizeOfBuf >= (sizeof(DataQueue))) { Queue = (DataQueue *)Buf;//强制转换成数据队列类型 NVIC_SETPRIMASK(); //disable all system interrupts Queue->MaxData = (SizeOfBuf - (u32)(((DataQueue *)0)->Buf)) / sizeof(QUEUE_DATA_TYPE); Queue->End = Queue->Buf + Queue->MaxData; Queue->Out = Queue->Buf; Queue->In = Queue->Buf; Queue->NData = 0; Queue->ReadEmpty = ReadEmpty; Queue->WriteFull = WriteFull; NVIC_RESETPRIMASK(); //enable all system interrupts return QUEUE_OK; } else { return NOT_OK; } u8 err; DataQueue *Queue; err = NOT_OK; if (Buf != NULL) { Queue = (DataQueue *)Buf; NVIC_SETPRIMASK(); //disable all system interrupts if (Queue->NData > 0) { *Ret = Queue->Out[0]; if (Queue->Out >= Queue->End) { Queue->Out = Queue->Buf; } Queue->NData--; err = QUEUE_OK; } else { err = QUEUE_EMPTY; if (Queue->ReadEmpty != NULL) { err = Queue->ReadEmpty(Ret, Queue); } } NVIC_RESETPRIMASK(); //enable all system interrupts } return err; u8 err; DataQueue *Queue; err = NOT_OK; if (Buf != NULL) { Queue = (DataQueue *)Buf; NVIC_SETPRIMASK(); //disable all system interrupts if (Queue->NData < Queue->MaxData) { Queue->In[0] = Data; Queue->In++; if (Queue->In >= Queue->End) { Queue->In = Queue->Buf; } Queue->NData++; err = QUEUE_OK; } else { err = QUEUE_FULL; if (Queue->WriteFull != NULL) { err = Queue->WriteFull(Queue, Data, Q_WRITE_MODE); } } NVIC_RESETPRIMASK(); //enable all system interrupts } return err; u8 err; DataQueue *Queue; err = NOT_OK; if (Buf != NULL) { Queue = (DataQueue *)Buf; if (Queue->NData < Queue->MaxData) { Queue->Out--; if (Queue->Out < Queue->Buf) { Queue->Out = Queue->End - 1; } Queue->Out[0] = Data; Queue->NData++; err = QUEUE_OK; } else { err = QUEUE_FULL; if (Queue->WriteFull != NULL) { err = Queue->WriteFull(Queue, Data, Q_WRITE_FRONT_MODE); } } } return err; u16 temp; temp = 0; NVIC_SETPRIMASK(); //disable all system interrupts if (Buf != NULL) { temp = ((DataQueue *)Buf)->NData; } NVIC_RESETPRIMASK(); //enable all system interrupts return temp; u16 temp; temp = 0; if (Buf != NULL) { temp = ((DataQueue *)Buf)->MaxData; } return temp; DataQueue *Queue; if (Buf != NULL) { Queue = (DataQueue *)Buf; Queue->Out = Queue->Buf; Queue->In = Queue->Buf; Queue->NData = 0; }
} DataQueue;
u8 QueueCreate(void *Buf,
{
}
u8 QueueRead(QUEUE_DATA_TYPE *Ret, void *Buf)
{
Queue->Out++;
}
#ifndef EN_QUEUE_WRITE
#define EN_QUEUE_WRITE 1
#endif
#if EN_QUEUE_WRITE > 0
u8 QueueWrite(void *Buf, QUEUE_DATA_TYPE Data)
{
}
#endif
#ifndef EN_QUEUE_WRITE_FRONT
#define EN_QUEUE_WRITE_FRONT 0
#endif
#if EN_QUEUE_WRITE_FRONT > 0
u8 QueueWriteFront(void *Buf, QUEUE_DATA_TYPE Data)
{
}
#endif
#ifndef EN_QUEUE_NDATA
#define EN_QUEUE_NDATA 1
#endif
#if EN_QUEUE_NDATA > 0
u16 QueueNData(void *Buf)
{
}
#endif
#ifndef EN_QUEUE_SIZE
#define EN_QUEUE_SIZE 1
#endif
#if EN_QUEUE_SIZE > 0
u16 QueueSize(void *Buf)
{
}
#endif
#ifndef EN_QUEUE_FLUSH
#define EN_QUEUE_FLUSH 1
#endif
#if EN_QUEUE_FLUSH > 0
void QueueFlush(void *Buf)
{
}
#endif
/*********************************************************************************************************
**
关键词: FIFO程序设计队列指

加入微信
获取电子行业最新资讯
搜索微信公众号:EEPW
或用微信扫描左侧二维码