Updated docs and gpl header titles.
[freeems-vanilla.git] / src / ignitionISRs.c
blob53af42dec07eb1672c1d8550f54fc44e5eb5683d
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 ignitionISRs.c
27 * @ingroup interruptHandlers
29 * @brief Turn ignition channels on and off
31 * This currently semi-working but broken code is intended to one day provide
32 * multi-channel ignition capabilities. The basic method will be to turn a pin
33 * or set of pins on or another pin or set of pins off during each run of the
34 * appropriate handler. Each run will be triggered either by the scheduler and
35 * possibly this code itself as well. Currently it does not work correctly and
36 * isn't suitable for actual use as an ignition control solution.
40 #define IGNITIONISRS_C
41 #include "inc/freeEMS.h"
42 #include "inc/interrupts.h"
45 /* Summary of intended ignition timing scheme
47 * Set TWO PIT timers based on a reference point (probably single cam trigger or optionally toggle bit based on missing tooth on crank)
48 * Arm one PIT timer interrupt to trigger when we need to start dwelling
49 * Arm the other PIT timer interrupt to trigger when we need to fire
50 * Configure a variable to point to the next pin to turn on for dwell
51 * Configure a variable to point to the next pin to turn off to fire
52 * On PIT interrupt for dwell check to see that spark should have finished
53 * If so, turn on coil to dwell
54 * If not, reset dwell timer interrupt for end of spark event
55 * On PIT interrupt for spark, turn off coil (amything else?)
56 * Repeat for each cylinder.
59 /* Further consideration to spark duration and dwell starting possibly needs to be done. */
61 /* further reading : ftp://ftp-sop.inria.fr/esterel/pub/papers/FDL99-camready.pdf section 4.1 has a nice diagram */
64 /** @brief Ignition dwell control
66 * This function turns ignition pins on to dwell when required.
68 * @author Fred Cooke
70 * @todo TODO make this actually work.
72 void IgnitionDwellISR(void)
74 // clear flag
75 PITTF = DWELL_ENABLE;
77 // start dwelling asap
78 PORTS_BA |= dwellStartMasks[nextDwellChannel];
80 if(dwellQueueLength == 0){
81 // turn off the int
82 PITINTE &= DWELL_DISABLE;
84 // disable channels
85 PITCE &= DWELL_DISABLE;
86 }else{
87 // reduce queue length by one
88 dwellQueueLength--;
90 // increment channel counter to next channel
91 if(nextDwellChannel < (fixedConfigs2.combustionEventsPerEngineCycle - 1)){
92 nextDwellChannel++; // if not the last channel, increment
93 }else{
94 nextDwellChannel = 0; // if the last channel, reset to zero
97 // if the queue length after decrement is greater than 0 then we need to load the timer, if it is zero and we decremented, the timer was already loaded.
98 if(dwellQueueLength > 0){
99 if(dwellQueueLength > 8){ // TODO ???? why 8 ???? 12 or combustion events per... or ?
100 //throw a nasty error of some sort for index out of range issue that should never occur (for now just light a LED)
101 PORTS |= 0x20;
102 }else{
103 // load the timer if the index is good
104 PITLD0 = queuedDwellOffsets[dwellQueueLength - 1];
109 // blink a led
110 PORTS ^= 0x80;
114 /** @brief Ignition discharge control
116 * This function turns ignition pins off to discharge when required.
118 * @author Fred Cooke
120 * @todo TODO make this actually work.
122 void IgnitionFireISR(void)
124 // clear flag
125 PITTF = IGNITION_ENABLE;
127 // fire the coil asap
128 PORTS_BA &= ignitionMasks[nextIgnitionChannel];
130 if(ignitionQueueLength == 0){
131 // turn off the int
132 PITINTE &= IGNITION_DISABLE;
134 // disable channels
135 PITCE &= IGNITION_DISABLE ;
136 }else{
137 // reduce queue length by one
138 ignitionQueueLength--;
140 // increment channel counter to next channel
141 if(nextIgnitionChannel < (fixedConfigs2.combustionEventsPerEngineCycle - 1)){
142 nextIgnitionChannel++; // if not the last channel, increment
143 }else{
144 nextIgnitionChannel = 0; // if the last channel, reset to zero
147 // if the queue length after decrement is greater than 0 then we need to load the timer, if it is zero and we decremented, the timer was already loaded.
148 if(ignitionQueueLength > 0){
149 if(ignitionQueueLength > fixedConfigs2.combustionEventsPerEngineCycle){ // TODO as above!!!!!!!!!!
150 //throw a nasty error of some sort for index out of range issue that should never occur (for now just light a LED)
151 PORTS |= 0x10;
152 }else{
153 // load the timer if the index is good
154 PITLD0 = queuedIgnitionOffsets[ignitionQueueLength - 1];
159 // blink a led
160 PORTS ^= 0x40;