Add auto generated location id list.
[freeems-vanilla.git] / src / realtimeISRs.c
blob43d6df4cd13224c6f9f3bf178370599d7eb099b7
1 /* FreeEMS - the open source engine management system
3 Copyright 2008 Fred Cooke
5 This file is part of the FreeEMS project.
7 FreeEMS software is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 FreeEMS software is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with any FreeEMS software. If not, see http://www.gnu.org/licenses/
20 We ask that if you make any changes to this file you email them upstream to
21 us at admin(at)diyefi(dot)org or, even better, fork the code on github.com!
23 Thank you for choosing FreeEMS to run your engine! */
26 /** @file realtimeISRs.c
27 * @ingroup interruptHandlers
29 * @brief Real time interrupts
31 * This file contains real time interrupt handlers. Mainly it holds the RTI
32 * handler itself, however the modulus down counter and ETC timer overflow
33 * functions are here too.
35 * @author Fred Cooke
39 #define REALTIMEISRS_C
40 #include "inc/freeEMS.h"
41 #include "inc/interrupts.h"
42 #include "inc/commsISRs.h"
45 /** @brief Real Time Interrupt Handler
47 * Handles time keeping, including all internal clocks, and generic periodic
48 * tasks that run quickly and must be done on time.
50 * @author Fred Cooke
52 void RTIISR(){
53 /* Clear the RTI flag */
54 CRGFLG = 0x80;
56 /* Record time stamp for code run time reporting */
57 unsigned short startTimeRTI = TCNT;
59 /* Increment the counter */
60 Clocks.realTimeClockMain++;
62 /* This function could be performed without the extra variables by rolling over the main ones at the largest multiples of the next ones, but I'm not sure thats better */
64 // TODO add content to eighths of a milli RTC ?
66 /* Every 8th RTI execution is one milli */
67 if(Clocks.realTimeClockMain % 8 == 0){
68 /* Increment the milli counter */
69 Clocks.realTimeClockMillis++;
71 /* Increment the milli roll over variable */
72 Clocks.millisToTenths++;
74 /* Perform all tasks that are once per millisecond here or preferably main */
75 Clocks.timeoutADCreadingClock++;
76 if(Clocks.timeoutADCreadingClock > fixedConfigs2.sensorSettings.readingTimeout){
77 /* Set force read adc flag */
78 coreStatusA |= FORCE_READING;
79 Clocks.timeoutADCreadingClock = 0;
82 /* Every 100 millis is one tenth */
83 if(Clocks.millisToTenths % 100 == 0){
84 /* Increment the tenths counter */
85 Clocks.realTimeClockTenths++;
87 /* Increment the tenths roll over variable */
88 Clocks.tenthsToSeconds++;
90 /* Reset the millis roll over variable */
91 Clocks.millisToTenths = 0;
93 /* Perform all tasks that are once per tenth of a second here or preferably main */
94 // decrement port H debounce variable till it's zero again.
95 if(portHDebounce != 0){
96 portHDebounce -= 1;
99 /* Every 10 tenths is one second */
100 if(Clocks.tenthsToSeconds % 10 == 0){
101 /* Increment the seconds counter */
102 Clocks.realTimeClockSeconds++;
104 /* Increment the seconds roll over variable */
105 Clocks.secondsToMinutes++;
107 /* Reset the tenths roll over variable */
108 Clocks.tenthsToSeconds = 0;
109 /* Perform all tasks that are once per second here or preferably main */
111 // temp throttling for log due to tuner performance issues (in the bedroom)
112 ShouldSendLog = TRUE;
113 /* Flash the user LED as a "heartbeat" to let new users know it's alive */
114 PORTP ^= 0x80;
116 /* Every 60 seconds is one minute, 65535 minutes is enough for us :-) */
117 if(Clocks.secondsToMinutes % 60 == 0){
118 /* Increment the minutes counter */
119 Clocks.realTimeClockMinutes++;
121 /* Potentially put an hours field in here and below, but that would be excessive */
122 // TODO add hours RTC ?
124 /* Reset the seconds roll over variable */
125 Clocks.secondsToMinutes = 0;
127 /* Perform all tasks that are once per minute here or preferably main */
128 // TODO add content in minutes RTC ?
130 /* Hours if statement here if we do hours which we probably won't */
135 RuntimeVars.RTCRuntime = TCNT - startTimeRTI;
139 /** @brief Tacho pulse generator
141 * Currently this is being used to generate a variable frequency tachometer
142 * output. Although this is a bit of a waste of a timer resource it does allow
143 * tachometers that were intended for engines with a different cylinder count
144 * to be used where it would otherwise not be possible.
146 * @author Fred Cooke
148 void ModDownCtrISR(){
149 /* Clear the modulus down counter interrupt flag */
150 MCFLG = 0x80;
152 /* If the rpm isn't genuine go ultra slow */
153 if(engineCyclePeriod == ticksPerCycleAtOneRPM){
154 tachoPeriod = 65535;
155 }else{
156 /* Use engine cycle period to setup the frequency of this counter and thereby act as a tacho out */
157 tachoPeriod = (unsigned long)engineCyclePeriod / fixedConfigs1.tachoSettings.tachoTotalFactor;
159 /* Set the count down value */
160 MCCNT = tachoPeriod;
162 /* Bit bang the output port */
163 PORTA ^= 0x40; // SM pin (A6)
167 /** @brief ECT overflow handler
169 * When the ECT free running timer hits 65535 and rolls over, this is run. Its
170 * job is to extend the timer to an effective 32 bits for longer measuring much
171 * longer periods with the same resolution.
173 * @author Fred Cooke
175 void TimerOverflow(){
176 /* Increment the timer extension variable */
177 timerExtensionClock++;
179 /* Clear the timer overflow interrupt flag */
180 TFLGOF = 0x80;
184 /** @todo TODO This could be useful in future once sleeping is implemented.
185 // Generic periodic interrupt (This only works from wait mode...)
186 void VRegAPIISR(){
187 // Clear the flag needs check because writing a 1 can set this one
188 //if(VREGAPICL & 0x01){ // if the flag is set...
189 VREGAPICL |= 0x01; // clear it...
190 //} // and not otherwise!
192 // DO stuff