SAMV71 Xplained Ultra Software Package 1.5

timetick.c

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