00001 /* ---------------------------------------------------------------------------- 00002 * SAM Software Package License 00003 * ---------------------------------------------------------------------------- 00004 * Copyright (c) 2013, 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 * Headers 00032 *----------------------------------------------------------------------------*/ 00033 00034 #include "board.h" 00035 #include "sys_arch.h" 00036 00037 /*---------------------------------------------------------------------------- 00038 * Variables 00039 *----------------------------------------------------------------------------*/ 00040 00041 /** clock tick count */ 00042 static volatile uint32_t dwClockTick; 00043 00044 /*---------------------------------------------------------------------------- 00045 * Exported functions 00046 *----------------------------------------------------------------------------*/ 00047 00048 /** 00049 * Interrupt handler for TC0 interrupt. 00050 */ 00051 void TC0_Handler( void ) 00052 { 00053 volatile uint32_t dwDummy ; 00054 /* Clear status bit to acknowledge interrupt */ 00055 dwDummy = TC0->TC_CHANNEL[0].TC_SR ; 00056 00057 /* Increase tick */ 00058 dwClockTick ++; 00059 } 00060 00061 00062 /** 00063 * Initialize for timing operation 00064 */ 00065 void sys_init_timing(void) 00066 { 00067 uint32_t div; 00068 uint32_t tcclks; 00069 00070 /* Clear tick value */ 00071 dwClockTick = 0; 00072 00073 /* Enable peripheral clock. */ 00074 PMC_EnablePeripheral( ID_TC0 ) ; 00075 00076 /* Configure TC for a CLOCK_CONF_SECOND frequency. */ 00077 TC_FindMckDivisor( CLOCK_CONF_SECOND, BOARD_MCK, &div, &tcclks, BOARD_MCK ); 00078 TC_Configure( TC0, 0, tcclks | TC_CMR_CPCTRG ); 00079 TC0->TC_CHANNEL[0].TC_RC = BOARD_MCK / (CLOCK_CONF_SECOND * div); 00080 00081 /* Configure and enable interrupt on RC compare */ 00082 NVIC_ClearPendingIRQ(TC0_IRQn); 00083 NVIC_EnableIRQ(TC0_IRQn); 00084 00085 TC0->TC_CHANNEL[ 0 ].TC_IER = TC_IER_CPCS ; 00086 /* Start timer */ 00087 TC_Start( TC0, 0 ); 00088 } 00089 00090 /** 00091 * Read for clock time (ms) 00092 */ 00093 unsigned int sys_get_ms(void) 00094 { 00095 return dwClockTick; 00096 } 00097