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

📄 mailbox.c

📁 ucos2.85在vs2005上模拟环境
💻 C
字号:

#define RTOS_MAILBOX_C

#include "includes.h"

#define RTOS_MailboxIsValid(mailbox) \
        ((mailbox!=(RTOS_Mailbox)0)\
        &&(((OS_EVENT*)mailbox)->OSEventType==OS_EVENT_TYPE_MBOX))

#define RTOS_MessageIsValid(message) \
        (message!=(RTOS_Message)0)

/*
*******************************************************************************
**
** This function creates a new message mailbox handle and initializes it with 
** the given data address.
**
*******************************************************************************
*/
RTOS_Mailbox RTOS_MailboxCreate( RTOS_Message message )
{
    //if( ! RTOS_MessageIsValid( message ) )
    //    return( (RTOS_Mailbox)0 );
        
    return( (RTOS_Mailbox) OSMboxCreate( (void*) message ) );
}

/*
*******************************************************************************
**
** This function destroys a previously created message mailbox.
**
*******************************************************************************
*/
int RTOS_MailboxDestroy( RTOS_Mailbox mailbox )
{
    if( ! RTOS_MailboxIsValid( mailbox ) )
        return( -1 ); 
        
    return( 0 );
}

/*
*******************************************************************************
**
** This function simply queries a message mailbox without blocking if no
** message is available.
**
*******************************************************************************
*/
RTOS_Message RTOS_MailboxQuery( RTOS_Mailbox mailbox )
{
    OS_MBOX_DATA mboxdata;

    if( ! RTOS_MailboxIsValid( mailbox ) )
        return( (RTOS_Message)0 );
     
    if( OSMboxQuery( (OS_EVENT*)mailbox, &mboxdata ) != OS_NO_ERR )
        return( (RTOS_Message)0 );

    return( (RTOS_Message) mboxdata.OSMsg );
}

/*
*******************************************************************************
**
** This function waits until a message is available in the given mailbox. 
** The suspend argument controls whether this function will be in blocking 
** mode or not while waiting for a message. A waiting message will be cleared
**
*******************************************************************************
*/
RTOS_Message RTOS_MailboxWait( RTOS_Mailbox mailbox, bool suspend )
{
    INT8U error   = OS_NO_ERR;
    void* message = 0;

    if( ! RTOS_MailboxIsValid( mailbox ) )
        return( (RTOS_Message)0 );

    if( suspend )
        message = OSMboxPend( (OS_EVENT*)mailbox, (INT16U)0, &error );
    else
        message = OSMboxAccept( (OS_EVENT*)mailbox );    
           
    if( ( error != OS_NO_ERR ) || ( message == 0 ) )
        return( (RTOS_Message)0 );

    return( message );
}

/*
*******************************************************************************
**
** This function waits until a message is available in the given mailbox. 
** The suspend argument controls whether this function will be in blocking 
** mode or not while waiting for a message. A waiting message will be cleared
**
*******************************************************************************
*/
RTOS_Message RTOS_MailboxWaitTimeout( RTOS_Mailbox mailbox, INT16U msecs )
{
    INT16U ticks = msecs / (1000 / OS_TICKS_PER_SEC);
    INT8U  error   = OS_NO_ERR;
    void*  message = 0;

    if( ! RTOS_MailboxIsValid( mailbox ) )
        return( (RTOS_Message)0 );

    if( msecs == 0 )
        return( RTOS_MailboxWait( mailbox, false ) );
            
    if( msecs == (INT16U)-1)
        return( RTOS_MailboxWait( mailbox, true ) );

    message = OSMboxPend( (OS_EVENT*)mailbox, (INT16U)0, &error );
    if( ( error != OS_NO_ERR ) || ( message == 0 ) )
        return( (RTOS_Message)0 );

    return( message );
}

/*
*******************************************************************************
**
** This function sends a message to a given mailbox. 
**
*******************************************************************************
*/
int RTOS_MailboxSend( RTOS_Mailbox mailbox, RTOS_Message message )
{
    if( ! RTOS_MailboxIsValid( mailbox ) )
        return( -3 );
        
    if( ! RTOS_MessageIsValid( message ) )
        return( -2 );
        
    if( OSMboxPost( (OS_EVENT*)mailbox, (void*) message ) != OS_NO_ERR )
        return( -1 );
    
    return( 0 );
}

⌨️ 快捷键说明

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