William Kane / Generic

Dependents:   LaserioLib

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers abcc_timer.c Source File

abcc_timer.c

00001 /*******************************************************************************
00002 ********************************************************************************
00003 **                                                                            **
00004 ** ABCC Driver version 4.01.01 (2015-12-14)                                   **
00005 **                                                                            **
00006 ** Delivered with:                                                            **
00007 **    ABP         7.16.01 (2015-10-14)                                        **
00008 **                                                                            */
00009 /*******************************************************************************
00010 ********************************************************************************
00011 ** COPYRIGHT NOTIFICATION (c) 2013 HMS Industrial Networks AB                 **
00012 **                                                                            **
00013 ** This code is the property of HMS Industrial Networks AB.                   **
00014 ** The source code may not be reproduced, distributed, or used without        **
00015 ** permission. When used together with a product from HMS, permission is      **
00016 ** granted to modify, reproduce and distribute the code in binary form        **
00017 ** without any restrictions.                                                  **
00018 **                                                                            **
00019 ** THE CODE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. HMS DOES NOT    **
00020 ** WARRANT THAT THE FUNCTIONS OF THE CODE WILL MEET YOUR REQUIREMENTS, OR     **
00021 ** THAT THE OPERATION OF THE CODE WILL BE UNINTERRUPTED OR ERROR-FREE, OR     **
00022 ** THAT DEFECTS IN IT CAN BE CORRECTED.                                       **
00023 ********************************************************************************
00024 ********************************************************************************
00025 ** File Description:
00026 ** Timer implementation.
00027 ********************************************************************************
00028 ********************************************************************************
00029 */
00030 
00031 #include "abcc_timer.h"
00032 #include "abcc_debug_err.h"
00033 #include "abcc_port.h"
00034 
00035 /*******************************************************************************
00036 ** Constants
00037 ********************************************************************************
00038 */
00039 
00040 
00041 /*******************************************************************************
00042 ** Typedefs
00043 ********************************************************************************
00044 */
00045 /*
00046  * Timer resource structure
00047  */
00048 typedef struct ABCC_TimerTimeoutType
00049 {
00050    BOOL  fActive;
00051    BOOL  fTmoOccured;
00052    INT32 lTimeLeft;
00053    ABCC_TimerTimeoutCallbackType pnHandleTimeout;
00054 }
00055 ABCC_TimerTimeoutType;
00056 
00057 
00058 /*******************************************************************************
00059 ** Public Globals
00060 ********************************************************************************
00061 */
00062 
00063 
00064 /*******************************************************************************
00065 ** Private Globals
00066 ********************************************************************************
00067 */
00068 #define MAX_NUM_TIMERS 3
00069 static ABCC_TimerTimeoutType sTimer[ MAX_NUM_TIMERS ];
00070 static BOOL fTimerEnabled = FALSE;
00071 
00072 /*******************************************************************************
00073 ** Private Services
00074 ********************************************************************************
00075 */
00076 
00077 
00078 /*******************************************************************************
00079 ** Public Services
00080 ********************************************************************************
00081 */
00082 void ABCC_TimerInit( void )
00083 {
00084    ABCC_TimerHandle xHandle;
00085 
00086    for ( xHandle = 0; xHandle < MAX_NUM_TIMERS; xHandle++ )
00087    {
00088       sTimer[ xHandle ].pnHandleTimeout = NULL;
00089    }
00090    fTimerEnabled = TRUE;
00091 }
00092 
00093 
00094 /*
00095 ** ABCC_TimerCreateTimer()
00096 */
00097 ABCC_TimerHandle ABCC_TimerCreate( ABCC_TimerTimeoutCallbackType pnHandleTimeout )
00098 {
00099    ABCC_TimerHandle xHandle = ABCC_TIMER_NO_HANDLE;
00100    ABCC_PORT_UseCritical();
00101 
00102    ABCC_PORT_EnterCritical();
00103 
00104    for ( xHandle = 0; xHandle < MAX_NUM_TIMERS; xHandle++ )
00105    {
00106       if ( sTimer[ xHandle ].pnHandleTimeout == NULL )
00107       {
00108          sTimer[ xHandle ].fActive = FALSE;
00109          sTimer[ xHandle ].fTmoOccured = FALSE;
00110          sTimer[ xHandle ].pnHandleTimeout = pnHandleTimeout;
00111          break;
00112       }
00113    }
00114    ABCC_PORT_ExitCritical();
00115 
00116    if ( xHandle >=  MAX_NUM_TIMERS )
00117    {
00118       xHandle = ABCC_TIMER_NO_HANDLE;
00119    }
00120    return( xHandle );
00121 }
00122 
00123 BOOL ABCC_TimerStart( ABCC_TimerHandle xHandle,
00124                       UINT32 lTimeoutMs )
00125 {
00126    BOOL fTmo;
00127    ABCC_PORT_UseCritical();
00128 
00129    ABCC_ASSERT( sTimer[ xHandle ].pnHandleTimeout );
00130 
00131    ABCC_PORT_EnterCritical();
00132    fTmo = sTimer[ xHandle ].fTmoOccured;
00133    sTimer[ xHandle ].lTimeLeft = (INT32)lTimeoutMs;
00134    sTimer[ xHandle ].fTmoOccured = FALSE;
00135    sTimer[ xHandle ].fActive = TRUE;
00136 
00137    ABCC_PORT_ExitCritical();
00138    return( fTmo );
00139 }
00140 
00141 
00142 /*
00143 ** ABCC_TimerStopTimer()
00144 */
00145 BOOL ABCC_TimerStop( ABCC_TimerHandle xHandle )
00146 {
00147    BOOL fTmo;
00148    ABCC_PORT_UseCritical();
00149 
00150    ABCC_PORT_EnterCritical();
00151    fTmo = sTimer[ xHandle ].fTmoOccured;
00152 
00153    sTimer[ xHandle ].fActive = FALSE;
00154    sTimer[ xHandle ].fTmoOccured = FALSE;
00155 
00156    ABCC_PORT_ExitCritical();
00157    return( fTmo );
00158 }
00159 
00160 /*
00161 ** ABCC_TimerTick()
00162 */
00163 void ABCC_TimerTick(const INT16 iDeltaTimeMs)
00164 {
00165    ABCC_TimerHandle xHandle;
00166    ABCC_PORT_UseCritical();
00167 
00168    if( !fTimerEnabled )
00169    {
00170       return;
00171    }
00172 
00173    ABCC_PORT_EnterCritical();
00174 
00175    for ( xHandle = 0; xHandle < MAX_NUM_TIMERS; xHandle++ )
00176    {
00177        if ( ( sTimer[ xHandle ].pnHandleTimeout != NULL ) &&
00178              ( sTimer[ xHandle ].fActive == TRUE ) )
00179        {
00180           sTimer[ xHandle ].lTimeLeft -= (INT32)iDeltaTimeMs;
00181           if( sTimer[ xHandle ].lTimeLeft <= 0 )
00182           {
00183              sTimer[ xHandle ].fTmoOccured = TRUE;
00184              sTimer[ xHandle ].fActive = FALSE;
00185              sTimer[ xHandle ].pnHandleTimeout();
00186           }
00187        }
00188    }
00189 
00190    ABCC_PORT_ExitCritical();
00191 }
00192 
00193 void ABCC_TimerDisable( void )
00194 {
00195    fTimerEnabled = FALSE;
00196 }
00197 
00198 /*******************************************************************************
00199 ** Tasks
00200 ********************************************************************************
00201 */