Add PLL lock lost/gained interrupt handler, sync loss reason, ign/inj cuts, and flagg...
[freeems-vanilla.git] / src / main / miscISRs.c
blob07bf32f4a3c184a2ea00de5bcdd1be25b11e5b8e
1 /* FreeEMS - the open source engine management system
3 * Copyright 2008-2012 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!
27 /** @file
29 * @ingroup interruptHandlers
31 * @brief Miscellaneous Interrupt Handlers
33 * Various non-descript interrupt handlers that don't really fit anywhere else
34 * and aren't big enough to live on their own just yet.
38 #include "inc/freeEMS.h"
39 #include "inc/interrupts.h"
40 #include "inc/decoderInterface.h"
43 /** @brief Unimplemented Interrupt Handler
45 * Unimplemented interrupt service routine for calls we weren't expecting.
46 * Currently this simply counts bad calls like any other event type.
48 * @todo TODO Split this into its own file, create one for each, and clear flags for all, and increment shared counter as is.
49 * @todo TODO Currently not a problem, but as indirectly pointed out by johntramp, if no flag clearing is being done, then this code will run continuously, which is not a good idea...
51 void UISR(void){
52 DEBUG_TURN_PIN_ON(DECODER_BENCHMARKS, BIT7, PORTB);
53 FLAG_AND_INC_FLAGGABLE(FLAG_CALLS_TO_UISRS_OFFSET);
54 DEBUG_TURN_PIN_OFF(DECODER_BENCHMARKS, NBIT7, PORTB);
57 /** @brief PLL Lock Lost/Gained
59 * When the Phase Locked Loop is lost or gained, this is called.
61 void PLLLockISR(void){
62 // Clear the flag
63 CRGFLG = PLLLOCKIF;
64 DEBUG_TURN_PIN_ON(DECODER_BENCHMARKS, BIT7, PORTB);
65 // Check the state of PLL lock
66 if(CRGFLG & PLLLOCK){ // Recovered
67 // Re-enable outputs with return of accurate clock
68 ((ignitionCutFlags *)&KeyUserDebugs.ignitionCuts)->IgnLostPLL = 0;
69 ((injectionCutFlags *)&KeyUserDebugs.injectionCuts)->InjLostPLL = 0;
70 }else{ // Lock lost
71 // Record the loss of PLL lock
72 FLAG_AND_INC_FLAGGABLE(PHASE_LOCKED_LOOP_LOCK_LOST_OFFSET);
73 // Force sync loss with special code to prevent engine damage from incorrect timings
74 resetToNonRunningState(PLL_LOCK_LOST_PRECAUTIONARY);
75 // Disable outputs as a precaution with dodgy clock
76 ((ignitionCutFlags *)&KeyUserDebugs.ignitionCuts)->IgnLostPLL = 1;
77 ((injectionCutFlags *)&KeyUserDebugs.injectionCuts)->InjLostPLL = 1;
79 DEBUG_TURN_PIN_OFF(DECODER_BENCHMARKS, NBIT7, PORTB);
83 /** @brief Port P pins ISR
85 * Interrupt handler for edge events on port P pins. Not currently used.
87 void PortPISR(void){
88 /* Clear all port P flags (we only want one at a time) */
89 PIFP = ONES;
90 DEBUG_TURN_PIN_ON(DECODER_BENCHMARKS, BIT7, PORTB);
91 FLAG_AND_INC_FLAGGABLE(FLAG_CALLS_TO_UISRS_OFFSET);
92 DEBUG_TURN_PIN_OFF(DECODER_BENCHMARKS, NBIT7, PORTB);
96 /** @brief Port J pins ISR
98 * Interrupt handler for edge events on port J pins. Not currently used.
100 void PortJISR(void){
101 /* Clear all port H flags (we only want one at a time) */
102 PIFJ = ONES;
103 DEBUG_TURN_PIN_ON(DECODER_BENCHMARKS, BIT7, PORTB);
104 FLAG_AND_INC_FLAGGABLE(FLAG_CALLS_TO_UISRS_OFFSET);
105 DEBUG_TURN_PIN_OFF(DECODER_BENCHMARKS, NBIT7, PORTB);
109 /** @brief Port H pins ISR
111 * Interrupt handler for edge events on port H pins. Not currently used.
113 void PortHISR(void)
115 // // read the interrupt flags to a variable
116 // unsigned char portHFlags = PIFH;
117 // portHFlags &= 0xF8; // mask out the other bits
119 // /* Clear all port H flags (we only want one at a time) */
120 PIFH = ONES;
121 DEBUG_TURN_PIN_ON(DECODER_BENCHMARKS, BIT7, PORTB);
123 // Bump this for the time being as this should not be occurring.
124 FLAG_AND_INC_FLAGGABLE(FLAG_CALLS_TO_UISRS_OFFSET);
126 // // Toggle a LED so we can see if the code ran
127 // PO-don't use this-RTA ^= 0x80; // Fuel pump pin (A7)
129 // debounce
130 if(portHDebounce == 0){
131 portHDebounce = 2;
132 }else{
133 return;
136 // // find out which pin triggered it, clear the flag, perform the action.
137 // switch(portHFlags)
138 // {
139 // case 0x80 : // Increment cylinder count and set port count appropriately.
140 // switch (configs.combustionEventsPerEngineCycle) {
141 // case 1 :
142 // configs.combustionEventsPerEngineCycle = 2;
143 // configs.ports = 2;
144 // break;
145 // case 2 :
146 // configs.combustionEventsPerEngineCycle = 3;
147 // configs.ports = 3;
148 // break;
149 // case 3 :
150 // configs.combustionEventsPerEngineCycle = 4;
151 // configs.ports = 4;
152 // break;
153 // case 4 :
154 // configs.combustionEventsPerEngineCycle = 5;
155 // configs.ports = 5;
156 // break;
157 // case 5 :
158 // configs.combustionEventsPerEngineCycle = 6;
159 // configs.ports = 6;
160 // break;
161 // case 6 :
162 // configs.combustionEventsPerEngineCycle = 8;
163 // configs.ports = 4;
164 // break;
165 // case 8 :
166 // configs.combustionEventsPerEngineCycle = 10;
167 // configs.ports = 5;
168 // break;
169 // case 10 :
170 // configs.combustionEventsPerEngineCycle = 12;
171 // configs.ports = 6;
172 // break;
173 // case 12 :
174 // configs.combustionEventsPerEngineCycle = 1;
175 // configs.ports = 1;
176 // break;
177 // }
178 // break;
179 // case 0x40 : // Injection output enable/disable
180 // break;
181 // case 0x20 : // Ignition output enable/disable
182 // break;
183 // case 0x10 : // Staged injection enable/disable
184 // break;
185 // case 0x08 : // Staged injection start sched/fixed
186 // break;
187 // case 0x04 : // Staged injection end sched/fixed
188 // break;
189 // case 0x02 : // free input
190 // break;
191 // case 0x01 : // free input
192 // break;
193 // default : // Two or more pressed, nothing to do except wait for another button press
194 // break;
195 // }
196 DEBUG_TURN_PIN_OFF(DECODER_BENCHMARKS, NBIT7, PORTB);
200 /** @brief IRQ/PE1 pin ISR
202 * Interrupt handler for edge events on the IRQ/PE1 pin. Not currently used.
204 void IRQISR(void){
205 /* Clear the flag */
206 // ?? TODO
207 DEBUG_TURN_PIN_ON(DECODER_BENCHMARKS, BIT7, PORTB);
208 FLAG_AND_INC_FLAGGABLE(FLAG_CALLS_TO_UISRS_OFFSET);
209 DEBUG_TURN_PIN_OFF(DECODER_BENCHMARKS, NBIT7, PORTB);
213 /** @brief XIRQ/PE0 pin ISR
215 * Interrupt handler for edge events on the XIRQ/PE0 pin. Not currently used.
217 void XIRQISR(void){
218 /* Clear the flag */
219 // ?? TODO
220 DEBUG_TURN_PIN_ON(DECODER_BENCHMARKS, BIT7, PORTB);
221 FLAG_AND_INC_FLAGGABLE(FLAG_CALLS_TO_UISRS_OFFSET);
222 DEBUG_TURN_PIN_OFF(DECODER_BENCHMARKS, NBIT7, PORTB);
226 /** @brief Low Voltage Counter
228 * Count how often our voltage drops lower than it should without resetting.
230 void LowVoltageISR(void){
231 /* Clear the flag */
232 VREGCTRL |= 0x01;
233 DEBUG_TURN_PIN_ON(DECODER_BENCHMARKS, BIT6, PORTB);
234 FLAG_AND_INC_FLAGGABLE(FLAG_LOW_VOLTATE_CONDITION_OFFSET);
235 DEBUG_TURN_PIN_OFF(DECODER_BENCHMARKS, NBIT6, PORTB);