?? queue.c
字號:
/***********************************************Copyright (c)*********************************************
** Guangzou ZLG-MCU Development Co.,LTD.
**
** http://www.zlgmcu.com
**
**--------------File Info---------------------------------------------------------------------------------
** File name: queue.c
** Last modified Date: 2007-09-20
** Last Version: 1.0
** Descriptions: 中間件函數(shù)
**
**--------------------------------------------------------------------------------------------------------
** Created by: chengmingji
** Created date: 2007-07-15
** Version: 1.0
** Descriptions:
**
**--------------------------------------------------------------------------------------------------------
** Modified by: lixiaocheng
** Modified Date: 2007-09-20
** Version: 1.0
** Descriptions: 對排版格式進行了調(diào)整
**
*********************************************************************************************************/
#include "config.h"
/*********************************************************************************************************
** Function name: QueueCreate
** Descriptions: 初始化數(shù)據(jù)隊列
** Input parameters: Buf :為隊列分配的存儲空間地址
** SizeOfBuf:為隊列分配的存儲空間大小(字節(jié))
** ReadEmpty:為隊列讀空時處理程序
** WriteFull:為隊列寫滿時處理程序
** Output parameters: NONE
** Returned value: NOT_OK :參數(shù)錯誤
** QUEUE_OK:成功
*********************************************************************************************************/
int QueueCreate (void *Buf,
uint32 SizeOfBuf,
uint8 (* ReadEmpty)(),
uint8 (* WriteFull)()
)
{
DataQueue *Queue;
if (Buf != NULL && SizeOfBuf >= (sizeof(DataQueue))) { /* 判斷參數(shù)是否有效 */
Queue = (DataQueue *)Buf;
OS_ENTER_CRITICAL();
/* 初始化結構體數(shù)據(jù) */
Queue->MaxData = (uint16)((SizeOfBuf - (uint32)(((DataQueue *)0)->Buf)) /
sizeof(QUEUE_DATA_TYPE)); /* 計算隊列可以存儲的數(shù)據(jù)數(shù)目 */
Queue->End = Queue->Buf + Queue->MaxData; /* 計算數(shù)據(jù)緩沖的結束地址 */
Queue->Out = Queue->Buf;
Queue->In = Queue->Buf;
Queue->NData = 0;
Queue->ReadEmpty = ReadEmpty;
Queue->WriteFull = WriteFull;
OS_EXIT_CRITICAL();
return QUEUE_OK;
} else {
return NOT_OK;
}
}
/*********************************************************************************************************
** Function name: QueueRead
** Descriptions: 獲取隊列中的數(shù)據(jù)
** Input parameters: Ret:存儲返回的消息的地址
** Buf:指向隊列的指針
** Output parameters: NONE
** Returned value: NOT_OK :參數(shù)錯誤
** QUEUE_OK :收到消息
** QUEUE_EMPTY:隊列空
*********************************************************************************************************/
int QueueRead (QUEUE_DATA_TYPE *Ret, void *Buf)
{
int err;
DataQueue *Queue;
err = NOT_OK;
if (Buf != NULL) { /* 隊列是否有效 */
Queue = (DataQueue *)Buf;
OS_ENTER_CRITICAL();
if (Queue->NData > 0) { /* 隊列是否為空 */
*Ret = Queue->Out[0]; /* 數(shù)據(jù)出隊 */
Queue->Out++;
if (Queue->Out >= Queue->End) { /* 調(diào)整出隊指針 */
Queue->Out = Queue->Buf;
}
Queue->NData--; /* 數(shù)據(jù)減少 */
err = QUEUE_OK;
} else {
err = QUEUE_EMPTY;
if (Queue->ReadEmpty != NULL) { /* 空,調(diào)用用戶處理函數(shù) */
err = Queue->ReadEmpty(Ret, Queue);
}
}
OS_EXIT_CRITICAL();
}
return err;
}
/*********************************************************************************************************
** Function name: QueueWrite
** Descriptions: FIFO方式發(fā)送數(shù)據(jù)
** Input parameters: Buf :指向隊列的指針
** Data:發(fā)送的數(shù)據(jù)
** Output parameters: NONE
** Returned value: NOT_OK : 參數(shù)錯誤
** QUEUE_FULL: 隊列滿
** QUEUE_OK : 發(fā)送成功
*********************************************************************************************************/
#ifndef EN_QUEUE_WRITE
#define EN_QUEUE_WRITE 1
#endif
#if EN_QUEUE_WRITE > 0
int QueueWrite (void *Buf, QUEUE_DATA_TYPE Data)
{
int err;
DataQueue *Queue;
err = NOT_OK;
if (Buf != NULL) { /* 隊列是否有效 */
Queue = (DataQueue *)Buf;
OS_ENTER_CRITICAL();
if (Queue->NData < Queue->MaxData) { /* 隊列是否滿 */
Queue->In[0] = Data; /* 數(shù)據(jù)入隊 */
Queue->In++;
if (Queue->In >= Queue->End) { /* 調(diào)整入隊指針 */
Queue->In = Queue->Buf;
}
Queue->NData++; /* 數(shù)據(jù)增加 */
err = QUEUE_OK;
} else { /* 滿 */
err = QUEUE_FULL;
if (Queue->WriteFull != NULL) { /* 調(diào)用用戶處理函數(shù) */
err = Queue->WriteFull(Queue, Data, Q_WRITE_MODE);
}
}
OS_EXIT_CRITICAL();
}
return err;
}
#endif /* EN_QUEUE_WRITE */
/*********************************************************************************************************
** Function name: QueueWriteFront
** Descriptions: LIFO方式發(fā)送數(shù)據(jù)
** Input parameters: Buf :指向隊列的指針
** Data:發(fā)送的數(shù)據(jù)
** Output parameters: NONE
** Returned value: NOT_OK : 參數(shù)錯誤
** QUEUE_FULL: 隊列滿
** QUEUE_OK : 發(fā)送成功
*********************************************************************************************************/
#ifndef EN_QUEUE_WRITE_FRONT
#define EN_QUEUE_WRITE_FRONT 0
#endif
#if EN_QUEUE_WRITE_FRONT > 0
uint8 QueueWriteFront (void *Buf, QUEUE_DATA_TYPE Data)
{
uint8 err;
DataQueue *Queue;
err = NOT_OK;
if (Buf != NULL) { /* 隊列是否有效 */
Queue = (DataQueue *)Buf;
OS_ENTER_CRITICAL();
if (Queue->NData < Queue->MaxData) { /* 隊列是否滿 */
Queue->Out--;
if (Queue->Out < Queue->Buf) { /* 調(diào)整出隊指針 */
Queue->Out = Queue->End - 1;
}
Queue->Out[0] = Data; /* 數(shù)據(jù)入隊 */
Queue->NData++; /* 數(shù)據(jù)數(shù)目增加 */
err = QUEUE_OK;
} else { /* 滿 */
err = QUEUE_FULL;
if (Queue->WriteFull != NULL) { /* 調(diào)用用戶處理函數(shù) */
err = Queue->WriteFull(Queue, Data, Q_WRITE_FRONT_MODE);
}
}
OS_EXIT_CRITICAL();
}
return err;
}
#endif /* EN_QUEUE_WRITE_FRONT */
/*********************************************************************************************************
** Function name: QueueNData
** Descriptions: 取得隊列中數(shù)據(jù)數(shù)
** Input parameters: Buf :指向隊列的指針
** Output parameters: NONE
** Returned value: 隊列包含數(shù)據(jù)數(shù)
*********************************************************************************************************/
#ifndef EN_QUEUE_NDATA
#define EN_QUEUE_NDATA 0
#endif
#if EN_QUEUE_NDATA > 0
uint16 QueueNData (void *Buf)
{
uint16 temp;
temp = 0; /* 隊列無效返回0 */
if (Buf != NULL) {
OS_ENTER_CRITICAL();
temp = ((DataQueue *)Buf)->NData;
OS_EXIT_CRITICAL();
}
return temp;
}
#endif /* EN_QUEUE_NDATA */
/*********************************************************************************************************
** Function name: QueueSize
** Descriptions: 取得隊列總容量
** Input parameters: Buf :指向隊列的指針
** Output parameters: NONE
** Returned value: 隊列總容量
*********************************************************************************************************/
#ifndef EN_QUEUE_SIZE
#define EN_QUEUE_SIZE 0
#endif
#if EN_QUEUE_SIZE > 0
uint16 QueueSize (void *Buf)
{
uint16 temp;
temp = 0; /* 隊列無效返回0 */
if (Buf != NULL) {
OS_ENTER_CRITICAL();
temp = ((DataQueue *)Buf)->MaxData;
OS_EXIT_CRITICAL();
}
return temp;
}
#endif /* EN_QUEUE_SIZE */
/*********************************************************************************************************
** Function name: OSQFlush
** Descriptions: 清空隊列
** Input parameters: Buf :指向隊列的指針
** Output parameters: NONE
** Returned value: NONE
*********************************************************************************************************/
#ifndef EN_QUEUE_FLUSH
#define EN_QUEUE_FLUSH 1
#endif
#if EN_QUEUE_FLUSH > 0
void QueueFlush (void *Buf)
{
DataQueue *Queue;
if (Buf != NULL) { /* 隊列是否有效 */
Queue = (DataQueue *)Buf;
OS_ENTER_CRITICAL();
Queue->Out = Queue->Buf;
Queue->In = Queue->Buf;
Queue->NData = 0; /* 數(shù)據(jù)數(shù)目為0 */
OS_EXIT_CRITICAL();
}
}
#endif /* EN_QUEUE_FLUSH */
/*********************************************************************************************************
** End Of File
********************************************************************************************************/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -