PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
esos_pic24_tick.c
Go to the documentation of this file.
1 /*
2  * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")"
3  * All rights reserved.
4  * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University)
5  * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University)
6  * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University)
7  *
8  * Permission to use, copy, modify, and distribute this software and its
9  * documentation for any purpose, without fee, and without written agreement is
10  * hereby granted, provided that the above copyright notice, the following
11  * two paragraphs and the authors appear in all copies of this software.
12  *
13  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
15  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
16  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17  *
18  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
23  *
24  * Please maintain this header in its entirety when copying/modifying
25  * these files.
26  *
27  *
28  */
29 
30 // Documentation for this file. If the \file tag isn't present,
31 // this file won't be documented.
32 /**
33 * \file
34 * Hardware-specific file (Microchip PIC24 Family) to generate
35 * the ESOS system tick functions.
36 */
37 
38 //#include "esos_irq.h"
39 #include "esos_pic24.h"
40 
41 volatile uint32 esos_tick_count;
42 volatile uint8 sub_tick;
43 
44 // prototype for the ESOS timer service function
45 extern void __esos_tmrSvcsExecute(void);
46 
47 /****************************************************/
48 /*
49 * \brief Increments the ESOS system tick
50 *
51 * \pre TMR1 is initialized with __esos_hw_InitSystemTick
52 *
53 * This ISR provides the mechanism for incrementing ESOS's
54 * 1.0ms system tick. Using Timer 1, we can call this ISR
55 * every 1.0ms and increment esos_tick_count. Or, we can
56 * have T1 call this ISR every X ms, and increment the
57 * value esos_tick_count by X
58 *
59 * \note The ESOS system tick will overflow in 2^32 msec
60 ********************************************************/
61 void _ISRFAST _T1Interrupt (void) {
62  esos_tick_count++; // increment the ESOS system tick by
63  _T1IF = 0; // clear the timer interrupt bit
64  __esos_tmrSvcsExecute(); // let ESOS implement the S/w tmr service
65 }
66 
67 /****************************************************/
68 /*
69 * \brief Initializes the ESOS system tick.
70 *
71 * \pre None assumed
72 *
73 * \post Sets up the PIC24 MCU's Timer1 to generate the 1.0ms tick
74 * required by ESOS.
75 *
76 * The (platform-independent) ESOS initialization code will
77 * call this function to setup and init the hardware (PIC24
78 * MCU, in this case) to create the required IRQs to generate
79 * the 1.0ms ESOS system tick.
80 *
81 * \note We can either generate an IRQ every 1.0ms or longer period,
82 * we just need to make sure that ISR that increments the tick
83 * count is consistent.
84 ********************************************************/
85 void __esos_hw_InitSystemTick(void) {
86 
87  // FOR NOW, we will init our usual PIC24 development setup here.
88  // THIS REALLY DOESN'T BELONG HERE!!!!!!!
89  configClock(); //config clock
90 
91  /* Configure Timer1 to:
92  ** operate during IDLE, use a 64x prescaler, and the internal clock
93  */
94  T1CON = T1_IDLE_CON + T1_PS_1_64 + T1_SOURCE_INT;
95 
96  PR1 = MS_TO_TICKS(1, 64); // 1 ms interrupt interval
97  TMR1 = 0; // clear T1's count
98  _T1IF = 0; // clear interrupt flag
99 
100  /* set T1's priority to be the highest possible for a PIC24 user IRQ
101  ** User IRQs (if used) will be a lower IRQ priority, so the tick will
102  ** get serviced in a timely manner
103  */
104  _T1IP = 7;
105  _T1IE = 1; // enable the interrupt
106  T1CONbits.TON = 1; // turn on the timer
107 
108 } // end __esos_hw_InitSystemTick()
109 
110 /****************************************************/
111 /*
112 * \brief Returns the ESOS system tick count.
113 *
114 * \pre ESOS system tick is running/working.
115 *
116 * \return A 32-bit value of the number of ESOS system ticks
117 * since the system has booted.
118 *
119 ********************************************************/
120 uint32 __esos_hw_GetSystemTickCount(void) {
121  return esos_tick_count;
122 } // end __esos_hw_GetSystemTickCount()
123