00001 /* realtimeISRs.c 00002 00003 Copyright 2008 Fred Cooke 00004 00005 This file is part of the FreeEMS project. 00006 00007 FreeEMS software is free software: you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation, either version 3 of the License, or 00010 (at your option) any later version. 00011 00012 FreeEMS software is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with any FreeEMS software. If not, see <http://www.gnu.org/licenses/>. 00019 00020 We ask that if you make any changes to this file you send them upstream to us at admin@diyefi.org 00021 00022 Thank you for choosing FreeEMS to run your engine! */ 00023 00024 #define REALTIMEISRS_C 00025 #include "inc/freeEMS.h" 00026 #include "inc/interrupts.h" 00027 #include "inc/commsISRs.h" 00028 #include "inc/realtimeISRs.h" 00029 00030 /* Real Time Interrupt Interrupt Service Routine */ 00031 /* Handles time keeping and generic periodic tasks that run quickly and must be done on time. */ 00032 void RTIISR(void) 00033 { 00034 /* Clear the RTI flag */ 00035 CRGFLG = 0x80; 00036 00037 /* Record time stamp for code run time reporting */ 00038 unsigned short startTimeRTI = TCNT; 00039 00040 /* Increment the counter */ 00041 Clocks.realTimeClockMain++; 00042 00043 /* 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 */ 00044 00045 // TODO add content to eighths of a milli RTC ? 00046 00047 /* Every 8th RTI execution is one milli */ 00048 if(Clocks.realTimeClockMain % 8 == 0){ 00049 /* Increment the milli counter */ 00050 Clocks.realTimeClockMillis++; 00051 00052 /* Increment the milli roll over variable */ 00053 Clocks.millisToTenths++; 00054 00055 /* Perform all tasks that are once per millisecond here or preferably main */ 00056 Clocks.timeoutADCreadingClock++; 00057 if(Clocks.timeoutADCreadingClock > fixedConfigs2.readingTimeout){ 00058 /* Set force read adc flag */ 00059 coreStatusA |= FORCE_READING; 00060 Clocks.timeoutADCreadingClock = 0; 00061 } 00062 00063 /* Every 100 millis is one tenth */ 00064 if(Clocks.millisToTenths % 100 == 0){ 00065 /* Increment the tenths counter */ 00066 Clocks.realTimeClockTenths++; 00067 00068 /* Increment the tenths roll over variable */ 00069 Clocks.tenthsToSeconds++; 00070 00071 /* Reset the millis roll over variable */ 00072 Clocks.millisToTenths = 0; 00073 00074 /* Perform all tasks that are once per tenth of a second here or preferably main */ 00075 // decrement port H debounce variable till it's zero again. 00076 if(portHDebounce != 0){ 00077 portHDebounce -= 1; 00078 } 00079 00080 /* Every 10 tenths is one second */ 00081 if(Clocks.tenthsToSeconds % 10 == 0){ 00082 /* Increment the seconds counter */ 00083 Clocks.realTimeClockSeconds++; 00084 00085 /* Increment the seconds roll over variable */ 00086 Clocks.secondsToMinutes++; 00087 00088 /* Reset the tenths roll over variable */ 00089 Clocks.tenthsToSeconds = 0; 00090 /* Perform all tasks that are once per second here or preferably main */ 00091 00092 // temp throttling for log due to tuner performance issues (in the bedroom) 00093 ShouldSendLog = TRUE; 00094 /* Flash the user LED as a "heartbeat" to let new users know it's alive */ 00095 PORTP ^= 0x80; 00096 00097 /* Every 60 seconds is one minute, 65535 minutes is enough for us :-) */ 00098 if(Clocks.secondsToMinutes % 60 == 0){ 00099 /* Increment the minutes counter */ 00100 Clocks.realTimeClockMinutes++; 00101 00102 /* Potentially put an hours field in here and below, but that would be excessive */ 00103 // TODO add hours RTC ? 00104 00105 /* Reset the seconds roll over variable */ 00106 Clocks.secondsToMinutes = 0; 00107 00108 /* Perform all tasks that are once per minute here or preferably main */ 00109 // TODO add content in minutes RTC ? 00110 00111 /* Hours if statement here if we do hours which we probably won't */ 00112 } 00113 } 00114 } 00115 } 00116 RuntimeVars.RTCRuntime = TCNT - startTimeRTI; 00117 } 00118 00119 /* Tachometer pulse generator function */ 00120 void ModDownCtrISR(void) 00121 { 00122 /* Clear the modulus down counter interrupt flag */ 00123 MCFLG = 0x80; 00124 00125 /* If the rpm isn't genuine go ultra slow */ 00126 if(engineCyclePeriod == ticksPerCycleAtOneRPM){ 00127 tachoPeriod = 65535; 00128 }else{ 00129 /* Use engine cycle period to setup the frequency of this counter and thereby act as a tacho out */ 00130 tachoPeriod = (unsigned long)engineCyclePeriod / fixedConfigs2.tachoTotalFactor; 00131 } 00132 /* Set the count down value */ 00133 MCCNT = tachoPeriod; 00134 00135 /* Bit bang the output port */ 00136 PORTA ^= 0x40; // SM pin (A6) 00137 } 00138 00139 /* When the ECT free running timer hits 65535 and rolls over, this is run */ 00140 void TimerOverflow(void){ 00141 /* Increment the timer extension variable */ 00142 timerExtensionClock++; 00143 00144 /* Clear the timer overflow interrupt flag */ 00145 TFLGOF = 0x80; 00146 } 00147 00148 /* Generic periodic interrupt (This only works from wait mode...) */ 00149 //void VRegAPIISR(void){ 00150 // /* Clear the flag needs check because writing a 1 can set this one */ 00151 // //if(VREGAPICL & 0x01){ // if the flag is set... 00152 // VREGAPICL |= 0x01; // clear it... 00153 // //} // and not otherwise! 00154 // 00155 // // DO stuff 00156 //}