PIC24 Support Libraries
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_t esos_tick_count;
42 volatile uint8_t 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  // FOR NOW, we will init our usual PIC24 development setup here.
87  // THIS REALLY DOESN'T BELONG HERE!!!!!!!
88  configClock(); //config clock
89 
90  // TODO: testing an experimental tick using an external watch xtal
91  // on the PIC24 secondary oscillator (SOSC) pins.
92 #if 1
93  /**********************************************
94  * USE FCY (instruction clock) to drive T1
95  * operate during IDLE, use a 64x prescaler,
96  * and the internal clock
97  ********************************************************/
98  T1CON = T1_IDLE_CON + T1_PS_1_64 + T1_SOURCE_INT;
99  PR1 = MS_TO_TICKS(1, 64); // 1 ms interrupt interval
100 #else
101  /**********************************************
102  * USE 32.768kHz watch xtal (SOSC) to drive T1
103  * operate during IDLE, use a 1x prescaler,
104  * and the secondary oscillator (must enable
105  * it via LPOSCEN in the OSCCON register)
106  * Not exactly 1ms tick (but neither is the
107  * instruction clock version)
108  ********************************************************/
109  // a local copy of OSCCON register to manipulate
110  OSCCONBITS OSCCONBITS_copy;
111  asm("DISI #0x3FFF"); // Disable interrupts for a long time
112  OSCCONBITS_copy = OSCCONbits; // Copy OSCCON register bits
113  OSCCONBITS_copy.LPOSCEN = 1; // ENABLE secondary oscillator
114  // First write high byte, containing new clock source NOSC
115  __builtin_write_OSCCONH(BITS2BYTEH(OSCCONBITS_copy));
116  // Then write low byte, requesting clock switch with OSWEN
117  __builtin_write_OSCCONL(BITS2BYTEL(OSCCONBITS_copy));
118  asm("DISI #0"); // Re-enable IRQs at the next instruction
119 
120  T1CON = T1_IDLE_CON + T1_PS_1_1 + T1_SOURCE_EXT + T1_SYNC_EXT_OFF;
121  PR1 = 32; // 32/32.768 = 0.9765625ms interrupt
122 #endif
123 
124  TMR1 = 0; // clear T1's count
125  _T1IF = 0; // clear interrupt flag
126 
127  /* set T1's priority to be the highest possible for a PIC24 user IRQ
128  ** User IRQs (if used) will be a lower IRQ priority, so the tick will
129  ** get serviced in a timely manner
130  */
131  _T1IP = 7;
132  _T1IE = 1; // enable the interrupt
133  T1CONbits.TON = 1; // turn on the timer
134 
135 } // end __esos_hw_InitSystemTick()
136 
137 /****************************************************/
138 /*
139 * \brief Returns the ESOS system tick count.
140 *
141 * \pre ESOS system tick is running/working.
142 *
143 * \return A 32-bit value of the number of ESOS system ticks
144 * since the system has booted.
145 *
146 ********************************************************/
147 uint32_t __esos_hw_GetSystemTickCount(void) {
148  return esos_tick_count;
149 } // end __esos_hw_GetSystemTickCount()
static void configClock()
This is the master include file for implementing ESOS on Microchip PIC24 MCUs.
#define BITS2BYTEL(sfrBitfield)
Return the low byte (as a uint8_t) of a bitfield.
Definition: pic24_util.h:57
#define BITS2BYTEH(sfrBitfield)
Return the high byte (as a uint8_t) of a bitfield.
Definition: pic24_util.h:59
unsigned char uint8_t
An abbreviation for an 8-bit unsigned integer.
Definition: dataXferImpl.h:194