Tick.c

Go to the documentation of this file.
00001 // ////////////////////////////////////////////////////////////////////////////
00002 // ////////////////////////////////////////////////////////////////////////////
00021 /*********************************************************************
00022  * Software License Agreement
00023  *
00024  * Copyright (C) 2002-2008 Microchip Technology Inc.  All rights 
00025  * reserved.
00026  *
00027  * Microchip licenses to you the right to use, modify, copy, and 
00028  * distribute: 
00029  * (i)  the Software when embedded on a Microchip microcontroller or 
00030  *      digital signal controller product ("Device") which is 
00031  *      integrated into Licensee's product; or
00032  * (ii) ONLY the Software driver source files ENC28J60.c and 
00033  *      ENC28J60.h ported to a non-Microchip device used in 
00034  *      conjunction with a Microchip ethernet controller for the 
00035  *      sole purpose of interfacing with the ethernet controller. 
00036  *
00037  * You should refer to the license agreement accompanying this 
00038  * Software for additional information regarding your rights and 
00039  * obligations.
00040  *
00041  * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT 
00042  * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT 
00043  * LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A 
00044  * PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL 
00045  * MICROCHIP BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR 
00046  * CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF 
00047  * PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS 
00048  * BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE 
00049  * THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER 
00050  * SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT 
00051  * (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE.
00052  *
00053  *
00054  * Author               Date        Comment
00055  *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00056  * Nilesh Rajbharti     6/28/01     Original        (Rev 1.0)
00057  * Nilesh Rajbharti     2/9/02      Cleanup
00058  * Nilesh Rajbharti     5/22/02     Rev 2.0 (See version.log for detail)
00059  * Howard Schlunder     6/13/07     Changed to use timer without 
00060  *                                  writing for perfect accuracy.
00061 ********************************************************************/
00062 #define __TICK_C
00063 
00064 #include "TCPIP.h"
00065 
00066 // Internal counter to store Ticks.
00067 static DWORD dwInternalTicks = 0;
00068 
00069 // 6-byte value to store Ticks.  Allows for use over longer periods of time.
00070 static BYTE vTickReading[6];
00071 
00072 static void GetTickCopy(void);
00073 void HighISR(void);
00074 
00075 /*****************************************************************************
00076   Function:
00077     void TickInit(void)
00078 
00079   Summary:
00080     Initializes the Tick manager module.
00081 
00082   Description:
00083     Configures the Tick module and any necessary hardware resources.
00084 
00085   Precondition:
00086     None
00087 
00088   Parameters:
00089     None
00090 
00091   Returns:
00092     None
00093     
00094   Remarks:
00095     This function is called only one during lifetime of the application.
00096   ***************************************************************************/
00097 void TickInit(void)
00098 {
00099     // Use Timer0
00100     // Initialize the time
00101     TMR0H = 0;
00102     TMR0L = 0;
00103 
00104     // Set up the timer interrupt
00105     INTCON2bits.TMR0IP = 1;     // High priority (MODIFIX: From low priority)
00106     INTCONbits.TMR0IF = 0;
00107     INTCONbits.TMR0IE = 1;      // Enable interrupt
00108 
00109     // Timer0 on, 16-bit, internal timer, 1:256 prescaler
00110     T0CON = 0x87;
00111 }
00112 
00113 /*****************************************************************************
00114   Function:
00115     static void GetTickCopy(void)
00116 
00117   Summary:
00118     Reads the tick value.
00119 
00120   Description:
00121     This function performs an interrupt-safe and synchronized read of the 
00122     48-bit Tick value.
00123 
00124   Precondition:
00125     None
00126 
00127   Parameters:
00128     None
00129 
00130   Returns:
00131     None
00132   ***************************************************************************/
00133 static void GetTickCopy(void)
00134 {
00135     // Perform an Interrupt safe and synchronized read of the 48-bit 
00136     // tick value
00137     do
00138     {
00139         INTCONbits.TMR0IE = 1;      // Enable interrupt
00140         Nop();
00141         INTCONbits.TMR0IE = 0;      // Disable interrupt
00142         vTickReading[0] = TMR0L;
00143         vTickReading[1] = TMR0H;
00144         *((DWORD*)&vTickReading[2]) = dwInternalTicks;
00145     } while(INTCONbits.TMR0IF);
00146     INTCONbits.TMR0IE = 1;          // Enable interrupt
00147 }
00148 
00149 
00150 /*****************************************************************************
00151   Function:
00152     DWORD TickGet(void)
00153 
00154   Summary:
00155     Obtains the current Tick value.
00156 
00157   Description:
00158     This function retrieves the current Tick value, allowing timing and
00159     measurement code to be written in a non-blocking fashion.  This function
00160     retrieves the least significant 32 bits of the internal tick counter, 
00161     and is useful for measuring time increments ranging from a few 
00162     microseconds to a few hours.  Use TickGetDiv256 or TickGetDiv64K for
00163     longer periods of time.
00164 
00165   Precondition:
00166     None
00167 
00168   Parameters:
00169     None
00170 
00171   Returns:
00172     Lower 32 bits of the current Tick value.
00173   ***************************************************************************/
00174 DWORD TickGet(void)
00175 {
00176     GetTickCopy();
00177     return *((DWORD*)&vTickReading[0]);
00178 }
00179 
00180 /*****************************************************************************
00181   Function:
00182     DWORD TickGetDiv256(void)
00183 
00184   Summary:
00185     Obtains the current Tick value divided by 256.
00186 
00187   Description:
00188     This function retrieves the current Tick value, allowing timing and
00189     measurement code to be written in a non-blocking fashion.  This function
00190     retrieves the middle 32 bits of the internal tick counter, 
00191     and is useful for measuring time increments ranging from a few 
00192     minutes to a few weeks.  Use TickGet for shorter periods or TickGetDiv64K
00193     for longer ones.
00194 
00195   Precondition:
00196     None
00197 
00198   Parameters:
00199     None
00200 
00201   Returns:
00202     Middle 32 bits of the current Tick value.
00203   ***************************************************************************/
00204 DWORD TickGetDiv256(void)
00205 {
00206     DWORD_VAL ret;
00207 
00208     GetTickCopy();
00209     ret.v[0] = vTickReading[1]; // Note: This copy must be done one 
00210     ret.v[1] = vTickReading[2]; // byte at a time to prevent misaligned 
00211     ret.v[2] = vTickReading[3]; // memory reads, which will reset the PIC.
00212     ret.v[3] = vTickReading[4];
00213     
00214     return ret.Val;
00215 }
00216 
00217 // MODIFIX: Removed the function TickGetDiv64K because not needed by eWicht.
00218 // MODIFIX: Removed the function TickConvertToMilliseconds because not needed 
00219 //          by eWicht. The ticks of eWicht are milliseconds!
00220 
00221 
00222 // MODIFIX: The TickUpdate function is replaced by a high prior ISR routine.
00223 
00225 // High prior Interrupt Service Routine
00226 
00227 #pragma code high_vector=0x08
00228 void high_interrupt (void)
00229 {
00230     _asm GOTO HighISR _endasm
00231 }
00232 #pragma code
00233 
00234 #pragma interrupt HighISR
00235 void HighISR(void)
00236 {
00237     // Increment internal high tick counter
00238     dwInternalTicks++;
00239 
00240     // Reset interrupt flag
00241     INTCONbits.TMR0IF = 0;
00242 }
00243 
00244 // MODIFIX: Removed the function _T1Interrupt because not needed by eWicht.

Generated on Sun Nov 27 20:02:39 2011 for eWicht by  doxygen 1.5.5