⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 queue.c

📁 C语言源代码及相关资料
💻 C
📖 第 1 页 / 共 2 页
字号:
/***********************************************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:		    中间件函数
**
**--------------------------------------------------------------------------------------------------------
** Created by:			    chengmingji
** Created date:		    2007-07-15
** Version:				    1.0
** Descriptions:		    
**
**--------------------------------------------------------------------------------------------------------
** Modified by:			    lixiaocheng
** Modified Date:		    2007-09-20
** Version:				    1.0
** Descriptions:		    对排版格式进行了调整
**
*********************************************************************************************************/
#include "config.h"

/*********************************************************************************************************
** Function name:           QueueCreate
** Descriptions:            初始化数据队列
** Input parameters:        Buf      :为队列分配的存储空间地址
**                          SizeOfBuf:为队列分配的存储空间大小(字节)
**                          ReadEmpty:为队列读空时处理程序
**                          WriteFull:为队列写满时处理程序
** Output parameters:       NONE
** Returned value:          NOT_OK  :参数错误
**                          QUEUE_OK:成功
*********************************************************************************************************/
int QueueCreate (void      *Buf,
                 uint32     SizeOfBuf,
                 uint8   (* ReadEmpty)(),
                 uint8   (* WriteFull)()
                 )
{
    DataQueue *Queue;
#if defined(QUEUE_SEMCONTROL)
    INT8U QueueErr;
#endif // end #if defined(QUEUE_SEMCONTROL)
    
    if (Buf != NULL && SizeOfBuf >= (sizeof(DataQueue))) {              /* 判断参数是否有效             */
        Queue = (DataQueue *)Buf;
#if defined(QUEUE_SEMCONTROL)
				Queue->pQueueSem = OSSemCreate(1);
				if ( Queue->pQueueSem!=0 ) {
	        OSSemPend(Queue->pQueueSem, 0, &QueueErr);
#else
	        //OS_ENTER_CRITICAL(); 
#endif // end #if defined(QUEUE_SEMCONTROL)
	                                                                        /* 初始化结构体数据             */
	        Queue->MaxData = (uint16)((SizeOfBuf - (uint32)(((DataQueue *)0)->Buf)) / 
	                         sizeof(QUEUE_DATA_TYPE));                      /* 计算队列可以存储的数据数目   */
	        Queue->End = Queue->Buf + Queue->MaxData;                       /* 计算数据缓冲的结束地址       */
	        Queue->Out = Queue->Buf;
	        Queue->In = Queue->Buf;
#if !defined(QUEUE_NEW_OPRT)
	        Queue->NData = 0;
#endif // end of #if !defined(QUEUE_NEW_OPRT)	        
	        Queue->ReadEmpty = ReadEmpty;
	        Queue->WriteFull = WriteFull;
	
#if defined(QUEUE_SEMCONTROL)
	        OSSemPost(Queue->pQueueSem);
#else
	        //OS_EXIT_CRITICAL();
#endif // end #if defined(QUEUE_SEMCONTROL)
	
	        return QUEUE_OK;
#if defined(QUEUE_SEMCONTROL)
      	} else {
      		return NOT_OK;
      	}
#endif // end #if defined(QUEUE_SEMCONTROL)
    } else {
        return NOT_OK;
    }
}


/*********************************************************************************************************
** Function name:           QueueRead
** Descriptions:            获取队列中的数据
** Input parameters:        Ret:存储返回的消息的地址
**                          Buf:指向队列的指针
** Output parameters:       NONE
** Returned value:          NOT_OK     :参数错误
**                          QUEUE_OK   :收到消息
**                          QUEUE_EMPTY:队列空
*********************************************************************************************************/
int QueueRead (QUEUE_DATA_TYPE *Ret, void *Buf)
{
    int err;
    DataQueue *Queue;
#if defined(QUEUE_SEMCONTROL)
		INT8U QueueErr;
#endif // end #if defined(QUEUE_SEMCONTROL)
#if 1//defined(QUEUE_NEW_OPRT)
		uint16 shNumData = 0;
#endif // end of #if defined(QUEUE_NEW_OPRT)

    err = NOT_OK;
    if (Buf != NULL) {                                                  /* 队列是否有效                 */
        Queue = (DataQueue *)Buf;
        
#if defined(QUEUE_SEMCONTROL)
        OSSemPend(Queue->pQueueSem, 0, &QueueErr);
#else
        //OS_ENTER_CRITICAL();
#endif // end #if defined(QUEUE_SEMCONTROL)
        
#if 1//defined(QUEUE_NEW_OPRT)
#if 1
				if ( Queue->In >= Queue->Out ) {
					shNumData = ( Queue->In - Queue->Out );
				} else {
					shNumData = Queue->MaxData - ( Queue->Out - Queue->In );
				}
				if ( shNumData>0 ) {// Not Empty
#else
				if ( Queue->In != Queue->Out ) { // Not Empty
#endif
#else
        if (Queue->NData > 0) {                                         /* 队列是否为空                 */                                                               
#endif // end of #if defined(QUEUE_NEW_OPRT)
            *Ret = Queue->Out[0];                                       /* 数据出队                     */
            Queue->Out++;                                               
            
            if (Queue->Out >= Queue->End) {                             /* 调整出队指针                 */
                Queue->Out = Queue->Buf;
            }
            
#if !defined(QUEUE_NEW_OPRT)
            Queue->NData--;                                             /* 数据减少                     */
#endif // end of #if !defined(QUEUE_NEW_OPRT)
            err = QUEUE_OK;
        } else {                                                               
            err = QUEUE_EMPTY;
            
            if (Queue->ReadEmpty != NULL) {                             /* 空,调用用户处理函数         */
                err = Queue->ReadEmpty(Ret, Queue);
            }
        }

#if defined(QUEUE_SEMCONTROL)
        OSSemPost(Queue->pQueueSem);
#else
        //OS_EXIT_CRITICAL();
#endif // end #if defined(QUEUE_SEMCONTROL)
    }
    return err;
}

/*********************************************************************************************************
** Function name:           QueueWrite
** Descriptions:            FIFO方式发送数据
** Input parameters:        Buf :指向队列的指针
**                          Data:发送的数据
** Output parameters:       NONE
** Returned value:          NOT_OK    : 参数错误
**                          QUEUE_FULL: 队列满
**                          QUEUE_OK  : 发送成功
*********************************************************************************************************/
#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;
#if defined(QUEUE_SEMCONTROL)
    INT8U QueueErr;
#endif // end #if defined(QUEUE_SEMCONTROL)
#if 1//defined(QUEUE_NEW_OPRT)
		uint16 shNumData = 0;
#endif // end of #if defined(QUEUE_NEW_OPRT)

    err = NOT_OK;
    if (Buf != NULL) {                                                  /* 队列是否有效                 */
        Queue = (DataQueue *)Buf;
        
#if defined(QUEUE_SEMCONTROL)
        OSSemPend(Queue->pQueueSem, 0, &QueueErr);
#else
        //OS_ENTER_CRITICAL(); 
#endif // end #if defined(QUEUE_SEMCONTROL)
#if 1 //defined(QUEUE_NEW_OPRT)
#if 1
				if ( Queue->In >= Queue->Out ) {
					shNumData = ( Queue->In - Queue->Out );
				} else {
					shNumData = Queue->MaxData - ( Queue->Out - Queue->In );
				}
				if ( shNumData < Queue->MaxData ) {
					Queue->In[0] = Data;
					Queue->In++;
					if (Queue->In >= Queue->End) {
						Queue->In = Queue->Buf;
					}
					Queue->NData++;
					err = QUEUE_OK;
#else
				if ( Queue->In >= Queue->Out ) {
					if ( (Queue->In - Queue->Out) < Queue->MaxData ) {
						// Case 1: Stil have free space, in Index >= out Index
						err = QUEUE_OK;
					}
				} else {
					// Queue->In < Queue->Out
					// it means that ((Queue->Out - Queue->In) > 0), has free space
					// Case 2: Stil have free space, in Index < out Index
					err = QUEUE_OK;
				}
				if ( err == QUEUE_OK )
				{
					Queue->In[0] = Data;
					Queue->In++;
					if (Queue->In >= Queue->End) {
						Queue->In = Queue->Buf;
					}
#endif
#else
        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;
#endif // end #if defined(QUEUE_SEMCONTROL)            
        } else {                                                        /* 满                           */
            err = QUEUE_FULL;
            if (Queue->WriteFull != NULL) {                             /* 调用用户处理函数             */
                err = Queue->WriteFull(Queue, Data, Q_WRITE_MODE);
            }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -