SAMV71 Xplained Ultra Software Package 1.4

timetick.c

Go to the documentation of this file.
00001 /* ----------------------------------------------------------------------------
00002  *         SAM Software Package License 
00003  * ----------------------------------------------------------------------------
00004  * Copyright (c) 2014, Atmel Corporation
00005  *
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *
00011  * - Redistributions of source code must retain the above copyright notice,
00012  * this list of conditions and the disclaimer below.
00013  *
00014  * Atmel's name may not be used to endorse or promote products derived from
00015  * this software without specific prior written permission.
00016  *
00017  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00019  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
00020  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00022  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00023  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00024  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00025  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00026  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  * ----------------------------------------------------------------------------
00028  */
00029 
00030 /**
00031  *  \file
00032  *  Implement the System Timer.
00033  */
00034 
00035 /*----------------------------------------------------------------------------
00036  *         Headers
00037  *----------------------------------------------------------------------------*/
00038 
00039 #include "board.h"
00040 #include <assert.h>
00041 /*----------------------------------------------------------------------------
00042  *         Local variables
00043  *----------------------------------------------------------------------------*/
00044 #define MAX_TIMER   4
00045 /** Tick Counter united by ms */
00046 static volatile uint32_t _dwTickCount = 0 ;
00047 static uint8_t SysTickConfigured = 0 ;
00048 
00049 static volatile uint32_t _dwTickTimer = 0 ;
00050 static TimeEvent *pTimeEventList = 0;
00051 
00052 SyTickDelayCounter_t DelayTimer;
00053 
00054 /*----------------------------------------------------------------------------
00055  *         Exported Functions
00056  *----------------------------------------------------------------------------*/
00057 
00058 
00059 /**
00060  *  \brief Handler for System Tick interrupt.
00061  *
00062  *  Process System Tick Event
00063  *  Increments the time-stamp counter.
00064  */
00065 void SysTick_Handler( void )
00066 {
00067      TimeEvent *pEvent;
00068     pEvent = pTimeEventList;
00069     _dwTickCount ++;
00070     if(_dwTickTimer)
00071       _dwTickTimer --;
00072     while(pEvent) {
00073         if(pEvent->time_start && pEvent->occur == 0) {
00074             pEvent->time_tick--;
00075             if(pEvent->time_tick == 0) {
00076                pEvent->time_start = 0;
00077                pEvent->occur = 1;
00078             }
00079         }
00080         pEvent = pEvent->pNextEvent;
00081     }
00082 }
00083 
00084 void SetTimeEvent(TimeEvent* pEvent)
00085 {
00086     pTimeEventList = pEvent;
00087 }
00088 
00089 /**
00090  *  \brief Configures the System Timer.
00091  *  Systick interrupt handler will generates 1ms interrupt and increase a
00092  *  tickCount.
00093  *  \note IRQ handler must be configured before invoking this function.
00094  */
00095 uint32_t TimeTick_Configure( void )
00096 {
00097     uint8_t Mdiv_Val;
00098     uint32_t Pck;
00099     _dwTickCount = 0 ;
00100 
00101     TRACE_INFO( "Configure system tick to get 1ms tick period.\n\r" ) ;
00102     /* check if there is MDIV value */
00103     Mdiv_Val = ( (PMC->PMC_MCKR & PMC_MCKR_MDIV_Msk) >> PMC_MCKR_MDIV_Pos);
00104 
00105     if(Mdiv_Val == 0) {
00106       Pck = BOARD_MCK;
00107     } else if(Mdiv_Val == 3 ) {
00108       Pck = BOARD_MCK * Mdiv_Val;
00109     } else {
00110       Pck = BOARD_MCK * (Mdiv_Val*2);
00111     }
00112 
00113      DelayTimer.pTimer1 = NULL; DelayTimer.pTimer1=NULL;
00114     /* Configure SysTick for 1 ms. */
00115     if ( SysTick_Config( Pck/1000 ) ) {
00116         TRACE_ERROR("SysTick configuration error\n\r" ) ;
00117         SysTickConfigured = 0;
00118         return 1;
00119     }
00120     SysTickConfigured = 1;
00121     return 0;
00122 }
00123 
00124 /**
00125  * \brief Get Delayed number of tick
00126  * \param startTick Start tick point.
00127  * \param endTick   End tick point.
00128  */
00129 uint32_t GetDelayInTicks(uint32_t startTick, uint32_t endTick)
00130 {
00131     assert(SysTickConfigured);
00132     
00133     if (endTick >= startTick) return (endTick - startTick);
00134     return (endTick + (0xFFFFFFFF - startTick) + 1);
00135     
00136 }
00137 
00138 /**
00139  * \brief Get Delayed number of tick
00140  * \param startTick Start tick point.
00141  * \param endTick   End tick point.
00142  */
00143 uint32_t GetTicks(void)
00144 {
00145     assert(SysTickConfigured);
00146     
00147     return _dwTickCount;
00148 }
00149 
00150 /**
00151  *  \brief Sync Wait for several ms
00152  *  \param dwMs    Waiting time in ms.
00153  */
00154 void Wait( volatile uint32_t dwMs )
00155 {
00156     uint32_t dwStart , dwEnd;
00157     
00158     assert(SysTickConfigured);
00159     
00160     dwStart = _dwTickCount ;
00161     dwEnd = _dwTickCount;
00162     while(GetDelayInTicks(dwStart, dwEnd) < dwMs ){
00163         dwEnd = _dwTickCount;
00164     }
00165 }
00166 
00167 /**
00168  *  \brief Sync Sleep for several ms
00169  *  \param dwMs    Sleeping time in ms.
00170  */
00171 void Sleep( volatile uint32_t dwMs )
00172 {
00173     uint32_t dwStart , dwEnd;
00174      
00175     assert(SysTickConfigured);
00176    
00177     __ASM("CPSIE   I");
00178     dwStart = _dwTickCount ;
00179     dwEnd = _dwTickCount;
00180     do {
00181         if (GetDelayInTicks(dwStart, dwEnd) < dwMs ) {
00182             break ;
00183         }
00184         dwEnd = _dwTickCount;
00185         __ASM("WFI");
00186     } while( 1 ) ;
00187 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines