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

📄 net_tmr.c

📁 AT91SAM7X256
💻 C
📖 第 1 页 / 共 3 页
字号:
/*
*********************************************************************************************************
*                                              uC/TCP-IP
*                                      The Embedded TCP/IP Suite
*
*                          (c) Copyright 2003-2006; Micrium, Inc.; Weston, FL
*
*                   All rights reserved.  Protected by international copyright laws.
*                   Knowledge of the source code may not be used to write a similar
*                   product.  This file may only be used in accordance with a license
*                   and should not be redistributed in any way.
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*
*                                       NETWORK TIMER MANAGEMENT
*
* Filename      : net_tmr.c
* Version       : V1.86
* Programmer(s) : ITJ
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                            INCLUDE FILES
*********************************************************************************************************
*/

#define    NET_TMR_MODULE
#include  <net.h>


/*
*********************************************************************************************************
*                                            LOCAL DEFINES
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                           LOCAL CONSTANTS
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                          LOCAL DATA TYPES
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                            LOCAL TABLES
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                       LOCAL GLOBAL VARIABLES
*********************************************************************************************************
*/


/*$PAGE*/
/*
*********************************************************************************************************
*                                      LOCAL FUNCTION PROTOTYPES
*********************************************************************************************************
*/

#if (NET_DBG_CFG_MEM_CLR_EN == DEF_ENABLED)
static  void  NetTmr_Clr           (NET_TMR  *ptmr);
#endif

#if (NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED)
static  void  NetTmr_Discard       (NET_TMR  *ptmr);

static  void  NetTmr_DiscardTaskTmr(void);
#endif


/*
*********************************************************************************************************
*                                     LOCAL CONFIGURATION ERRORS
*********************************************************************************************************
*/


/*$PAGE*/
/*
*********************************************************************************************************
*                                            NetTmr_Init()
*
* Description : (1) Initialize Network Timer Management Module :
*
*                   (a) Perform Timer Module/OS initialization
*                   (b) Initialize timer pool
*                   (c) Initialize timer table
*                   (d) Initialize timer task list pointer
*                   (e) Initialize timer error counters
*
*
* Argument(s) : perr        Pointer to variable that will receive the return error code from this function :
*
*                               NET_TMR_ERR_NONE                Network timer module successfully initialized.
*
*                                                               ------- RETURNED BY NetOS_Tmr_Init() : -------
*                               NET_OS_ERR_INIT_TMR_TASK        Network timer task      NOT successfully
*                                                                   initialized.
*                               NET_OS_ERR_INIT_TMR_TASK_NAME   Network timer task name NOT successfully
*                                                                   configured.
*
* Return(s)   : none.
*
* Caller(s)   : Net_Init().
*
* Note(s)     : (2) Timer pool MUST be initialized PRIOR to initializing the pool with pointers to timers.
*********************************************************************************************************
*/
/*$PAGE*/
void  NetTmr_Init (NET_ERR  *perr)
{
    NET_TMR      *ptmr;
    NET_TMR_QTY   i;
    NET_ERR       err;


                                                                /* --------------- PERFORM TMR/OS INIT ---------------- */
    NetOS_Tmr_Init(perr);                                       /* Create Tmr Task.                                     */
    if (*perr != NET_OS_ERR_NONE) {
         return;
    }


                                                                /* ------------------ INIT TMR POOL ------------------- */
    NetTmr_PoolPtr = (NET_TMR *)0;                              /* See Note #2.                                         */

    NetStat_PoolInit((NET_STAT_POOL   *)&NetTmr_PoolStat,
                     (NET_STAT_POOL_QTY) NET_TMR_CFG_NBR_TMR,
                     (NET_ERR         *)&err);


                                                                /* ------------------ INIT TMR TBL -------------------- */
    ptmr = &NetTmr_Tbl[0];
    for (i = 0; i < NET_TMR_CFG_NBR_TMR; i++) {
        ptmr->Type  =  NET_TMR_TYPE_TMR;                        /* Init each tmr type/id--NEVER modify.                 */
        ptmr->ID    = (NET_BUF_QTY)i;

        ptmr->Flags =  NET_TMR_FLAG_NONE;                       /* Init each tmr as NOT used.                           */

#if (NET_DBG_CFG_MEM_CLR_EN == DEF_ENABLED)
        NetTmr_Clr(ptmr);
#endif

        ptmr->NextPtr  = (void *)NetTmr_PoolPtr;                /* Free each tmr to tmr pool (see Note #2).             */
        NetTmr_PoolPtr =  ptmr;

        ptmr++;
    }


                                                                /* -------------- INIT TMR TASK LIST PTR -------------- */
    NetTmr_TaskListHead         = (NET_TMR *)0;
    NetTmr_TaskListPtr          = (NET_TMR *)0;


                                                                /* ---------------- INIT TMR ERR CTRS ----------------- */
#if (NET_CTR_CFG_ERR_EN         == DEF_ENABLED)
    NetTmr_ErrNoneAvailCtr      =  0;

#if (NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED)
    NetTmr_ErrNullPtrCtr        =  0;
    NetTmr_ErrNullObjCtr        =  0;
    NetTmr_ErrNullFnctCtr       =  0;
    NetTmr_ErrNotUsedCtr        =  0;

    NetTmr_ErrInvalidTypeCtr    =  0;
#endif
#endif


   *perr = NET_TMR_ERR_NONE;
}


/*$PAGE*/
/*
*********************************************************************************************************
*                                         NetTmr_TaskHandler()
*
* Description : Decrement every timer in Timer Task List & execute timer's expiration function for any
*               timer that expires.
*
*               (1) (a) Timers are managed in a doubly-linked Timer List.
*
*                       (1) 'NetTmr_TaskListHead' points to the head of the Timer List.
*
*                       (2) Timers' 'PrevPtr' & 'NextPtr' doubly-link each timer to form the Timer List.
*
*                   (b) New timers are added at the head of the Timer List.
*
*                   (c) As timers are added into the list, older timers migrate to the tail of the Timer
*                       List.  Once a timer expires or is discarded, it is removed from the Timer List.
*
*
*                                        |                                               |
*                                        |<-------------- List of Timers --------------->|
*                                        |                (see Note #1a)                 |
*
*                                      New Timers
*                                   inserted at head                         Oldest Timer in List
*                                    (see Note #1b)                            (see Note #1c)
*
*                                           |                 NextPtr                 |
*                                           |             (see Note #1a2)             |
*                                           v                    |                    v
*                                                                |
*                        Head of         -------       -------   v   -------       -------
*                       Timer List  ---->|     |------>|     |------>|     |------>|     |
*                                        |     |<------|     |<------|     |<------|     |
*                    (see Note #1a1)     -------       -------   ^   -------       -------
*                                                                |
*                                                                |
*                                                             PrevPtr
*                                                         (see Note #1a2)
*
*
* Argument(s) : none.
*
* Return(s)   : none.
*
* Caller(s)   : NetOS_Tmr_Task() [see 'net_os.c'].
*
* Note(s)     : (2) NetTmr_TaskHandler() blocked until network initialization completes.
*
*               (3) NetTmr_TaskHandler() blocks ALL other network protocol tasks by pending on & acquiring
*                   the global network lock (see 'net.h  Note #2').
*
*               (4) (a) NetTmr_TaskHandler() handles all valid timers in Timer Task List, up to the first
*                       corrupted timer in the Timer Task List, if any.
*
*                   (b) #### If ANY timer(s) in Timer Task List are corrupted :
*
*                       (1) Discard/unlink current Timer Task timer.
*                           (A) Consequently, any remaining valid timers in Timer Task List are :
*                               (1) Unlinked from Timer Task List, ...
*                               (2) NOT handled.
*
*                       (2) Timer Task is aborted.
*
*               (5) Since NetTmr_TaskHandler() is asynchronous to NetTmr_Free() [via execution of certain
*                   timer expiration functions], the Timer Task List timer ('NetTmr_TaskListPtr') MUST be
*                   coordinated with NetTmr_Free() to avoid Timer Task List corruption :
*
*                   (a) (1) Timer Task List timer is typically advanced by NetTmr_TaskHandler() to the next 
*                           timer in the Timer Task List.
*
*                       (2) However, whenever the Timer Task List timer is freed by an asynchronous timer
*                           expiration function, the Timer Task List timer MUST be advanced to the next
*                           valid & available timer in the Timer Task List.
*
*                           See also 'NetTmr_Free()  Note #3a'.
*
*                   (b) Timer Task List timer MUST be cleared after handling the Timer Task List.
*
*                       (1) However, Timer Task List timer is implicitly cleared after handling the
*                           Timer Task List.
*
*                       See also 'NetTmr_DiscardTaskTmr()  Note #1c'.
*
*               (6) Since NetTmr_TaskHandler() is asynchronous to ANY timer Get/Set, one additional tick
*                   is added to each timer's count-down so that the requested timeout is ALWAYS satisfied.
*                   This additional tick is added by NOT checking for zero ticks after decrementing; any
*                   timer that expires is recognized at the next tick.
*
*               (7) When a network timer expires, the timer SHOULD be freed PRIOR to executing the timer
*                   expiration function.  This ensures that at least one timer is available if the timer
*                   expiration function requires a timer.
*********************************************************************************************************
*/
/*$PAGE*/
void  NetTmr_TaskHandler (void)
{
#if ((NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED)                    && \
     (NET_CTR_CFG_ERR_EN         == DEF_ENABLED)                    && \
     (CPU_CFG_CRITICAL_METHOD    == CPU_CRITICAL_METHOD_STATUS_LOCAL))
    CPU_SR         cpu_sr;
#endif
#if  (NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED)
    CPU_BOOLEAN    used;
#endif
    NET_TMR       *ptmr;
    void          *obj;
    CPU_FNCT_PTR   fnct;
    NET_ERR        err;


    if (Net_InitDone != DEF_YES) {                                      /* If init NOT complete, ...                    */
        NetOS_InitWait(&err);                                           /* ... wait on net init (see Note #2).          */
        if (err != NET_OS_ERR_NONE) {
            return;
        }
    }


    NetOS_Lock(&err);                                                   /* Acquire net lock (see Note #3).              */
    if (err != NET_OS_ERR_NONE) {
        return;
    }

    NetTmr_TaskListPtr = NetTmr_TaskListHead;                           /* Start @ Tmr Task List head.                  */
    while (NetTmr_TaskListPtr != (NET_TMR *)0) {                        /* Handle  Tmr Task List tmrs (see Note #4a).   */
#if  (NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED)
                                                                        /* -------------- VALIDATE TYPE --------------- */
        if (NetTmr_TaskListPtr->Type != NET_TMR_TYPE_TMR) {             /* If tmr type invalid;                     ... */
            NetTmr_DiscardTaskTmr();                                    /* ... discard tmr task tmr (see Note #4b1) ... */ 
            NET_CTR_ERR_INC(NetTmr_ErrInvalidTypeCtr);
            NetOS_Unlock();
            return;                                                     /* ... & abort tmr task     (see Note #4b2).    */
        }
                                                                        /* ------------ VALIDATE TMR USED ------------- */

⌨️ 快捷键说明

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