FreeEMS  0.2.0-SNAPSHOT-285-g028e24c
counterTypes.h
Go to the documentation of this file.
1 /* FreeEMS - the open source engine management system
2  *
3  * Copyright 2008-2013 Fred Cooke
4  *
5  * This file is part of the FreeEMS project.
6  *
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.
11  *
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.
16  *
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/
19  *
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!
22  *
23  * Thank you for choosing FreeEMS to run your engine!
24  */
25 
26 
27 /** @file
28  *
29  * @ingroup allHeaders
30  * @ingroup globalHeaders
31  * @ingroup dataStructures
32  *
33  * @brief Counter struct typedefs
34  *
35  * This file contains all of the counter and statistic struct typedefs.
36  */
37 
38 
39 /* Header file multiple inclusion protection courtesy eclipse Header Template */
40 /* and http://gcc.gnu.org/onlinedocs/gcc-3.1.1/cpp/ C pre processor manual */
41 #ifndef FILE_COUNTER_TYPES_H_SEEN
42 #define FILE_COUNTER_TYPES_H_SEEN
43 
44 
45 // Some Fred Cooke cunning inspired by EssEss
46 #define FLAG_AND_INC_FLAGGABLE(OFFSET) \
47 (*(((unsigned char*)&Flaggables) + OFFSET))++; \
48 KeyUserDebugs.flaggableFlags |= (1 << OFFSET); // End of macro
49 
50 /// These should all stay at zero, thus they are incremented through a mechanism that also sets a flag in a special variable
51 typedef struct {
52  // Error conditions
53  unsigned char callsToUISRs; ///< to ensure we aren't accidentally triggering unused ISRs.
54  unsigned char lowVoltageConditions; ///< low voltage conditions.
55 #define FLAG_CALLS_TO_UISRS_OFFSET 0
56 #define FLAG_LOW_VOLTAGE_CONDITION_OFFSET 1
57 
58  // RPM/Position input
59  unsigned char decoderSyncLosses; ///< Number of times cam, crank or combustion sync is lost.
60  unsigned char decoderSyncsNotConfirmed; ///< How many times we cleared sync state without having yet accumulated enough confirmations.
61  unsigned char decoderSyncStateClears; ///< Sync loss called when not synced yet, thus discarding data and preventing sync.
62 #define FLAG_DECODER_SYNC_LOSSES_OFFSET 2
63 #define FLAG_DECODER_SYNCS_NOT_CONFIRMED_OFFSET 3
64 #define FLAG_DECODER_SYNC_STATE_CLEARS_OFFSET 4
65 
66  // If you're getting these, then your serial hardware sucks
67  unsigned char serialNoiseErrors; ///< Incremented when noise is detected
68  unsigned char serialFramingErrors; ///< Incremented when a framing error occurs
69  unsigned char serialParityErrors; ///< Incremented when a parity error occurs
70 #define FLAG_SERIAL_NOISE_ERRORS_OFFSET 5
71 #define FLAG_SERIAL_FRAMING_ERRORS_OFFSET 6
72 #define FLAG_SERIAL_PARITY_ERRORS_OFFSET 7
73 
74  // This is caused by heavy interrupt load delaying serial servicing, just a fact of life at high RPM.
75  unsigned char serialOverrunErrors; ///< Incremented when overrun occurs (duplicated in KeyUserDebug below)
76 #define FLAG_SERIAL_OVERRUN_ERRORS_OFFSET 8
77 
78  // These can be caused by noise, but if there is no noise, then it's a code issue with the PC side application
79  unsigned char serialEscapePairMismatches; ///< Incremented when an escape is found but not followed by an escapee
80  unsigned char serialStartsInsideAPacket; ///< Incremented when a start byte is found inside a packet
81  unsigned char serialPacketsOverLength; ///< Incremented when the buffer fills up before the end
82  unsigned char serialChecksumMismatches; ///< Incremented when calculated checksum did not match the received one
83  unsigned char serialPacketsUnderLength; ///< Incremented when a packet is found that is too short
84 #define FLAG_SERIAL_ESCAPE_PAIR_MISMATCHES_OFFSET 9
85 #define FLAG_SERIAL_STARTS_INSIDE_A_PACKET_OFFSET 10
86 #define FLAG_SERIAL_PACKETS_OVER_LENGTH_OFFSET 11
87 #define FLAG_SERIAL_CHECKSUM_MISMATCHES_OFFSET 12
88 #define FLAG_SERIAL_PACKETS_UNDER_LENGTH_OFFSET 13
89 
90  unsigned char phaseLockedLoopLockLost; ///< Incremented when PLL lock is lost
91  unsigned char selfClockModeEntered; ///< Incremented when the MCU loses main clock
92 #define FLAG_PHASE_LOCKED_LOOP_LOCK_LOST_OFFSET 14
93 #define FLAG_SELF_CLOCK_MODE_ENTERED_OFFSET 15
94 } Flaggable;
95 
96 
97 // Some more Fred Cooke cunning inspired by EssEss
98 #define FLAG_AND_INC_FLAGGABLE2(OFFSET) \
99 (*(((unsigned char*)&Flaggables2) + OFFSET))++; \
100 KeyUserDebugs.flaggableFlags2 |= (1 << OFFSET); // End of macro
101 
102 /// These should all stay at zero, thus they are incremented through a mechanism that also sets a flag in a special variable
103 typedef struct {
104  // More error conditions
105  unsigned char spuriousInterrupts; ///< ? TODO
106  unsigned char unimplementedOpcodes; ///< ? TODO
107  unsigned char RAMAccessViolations; ///< ? TODO
108  unsigned char XGATESoftwareErrors; ///< ? TODO
109 #define FLAG_SPURIOUS_INTERRUPTS_OFFSET 0
110 #define FLAG_UNIMPLEMENTED_OPCODES_OFFSET 1
111 #define FLAG_RAM_ACCESS_VIOLATIONS_OFFSET 2
112 #define FLAG_XGATE_SOFTWARE_ERRORS_OFFSET 3
113 
114  unsigned char spare4; ///< Spare flaggable.
115  unsigned char spare5; ///< Spare flaggable.
116  unsigned char spare6; ///< Spare flaggable.
117  unsigned char spare7; ///< Spare flaggable.
118  unsigned char spare8; ///< Spare flaggable.
119  unsigned char spare9; ///< Spare flaggable.
120  unsigned char spare10; ///< Spare flaggable.
121  unsigned char spare11; ///< Spare flaggable.
122  unsigned char spare12; ///< Spare flaggable.
123  unsigned char spare13; ///< Spare flaggable.
124  unsigned char spare14; ///< Spare flaggable.
125  unsigned char spare15; ///< Spare flaggable.
126 #define FLAG_SPARE_4_OFFSET 4
127 #define FLAG_SPARE_5_OFFSET 5
128 #define FLAG_SPARE_6_OFFSET 6
129 #define FLAG_SPARE_7_OFFSET 7
130 #define FLAG_SPARE_8_OFFSET 8
131 #define FLAG_SPARE_9_OFFSET 9
132 #define FLAG_SPARE_10_OFFSET 10
133 #define FLAG_SPARE_11_OFFSET 11
134 #define FLAG_SPARE_12_OFFSET 12
135 #define FLAG_SPARE_13_OFFSET 13
136 #define FLAG_SPARE_14_OFFSET 14
137 #define FLAG_SPARE_15_OFFSET 15
138 } Flaggable2;
139 
140 
141 /// Use this block to manage the various clocks kept.
142 typedef struct {
143  /* Real Time and other Clocks (all require init to zero) */
144  unsigned short realTimeClockMain; ///< Variable to count RTI executions, 0.125ms exactly
145  unsigned short realTimeClockMillis; ///< Variable to count milliseconds exactly
146  unsigned short realTimeClockTenths; ///< Variable to count tenths of a second exactly
147  unsigned short realTimeClockSeconds; ///< Variable to count seconds exactly
148  unsigned short realTimeClockMinutes; ///< Variable to count minutes exactly
149 
150  unsigned short millisToTenths; ///< Roll-over variable for counting tenths
151  unsigned short tenthsToSeconds; ///< Roll-over variable for counting seconds
152  unsigned short secondsToMinutes; ///< Roll-over variable for counting minutes
153 
154  unsigned short timeoutADCreadingClock; ///< Timeout clock/counter for synced ADC readings
155 } Clock;
156 
157 
158 /// Use this block to manage the execution count of various functions loops and ISRs etc. TODO break this up into smaller chunks
159 typedef struct {
160  // Scheduling
161  unsigned char normalSchedule; ///< times events were scheduled normally.
162  unsigned char timerStretchedToSchedule; ///< times timer needed to be extended to prevent not scheduling.
163 
164  // Decoder scheduling
165  unsigned char pinScheduledToGoHigh; ///< Behaviour chosen from decoder based on register and pin state.
166  unsigned char pinScheduledAlready; ///< @copydoc pinScheduledToGoHigh
167  unsigned char pinScheduledToSelfSchedule; ///< @copydoc pinScheduledToGoHigh
168  unsigned char pinScheduledAgainToStayOn; ///< @copydoc pinScheduledToGoHigh
169 
170  unsigned char pinScheduledToToggleError; ///< @copydoc pinScheduledToGoHigh
171  unsigned char pinScheduledToDoNothing; ///< @copydoc pinScheduledToGoHigh
172  unsigned char pinScheduledFromCold; ///< @copydoc pinScheduledToGoHigh
173  unsigned char pinScheduledWithTimerExtension; ///< @copydoc pinScheduledToGoHigh
174 
175  // Outputs, refactor to not say "injector" later.
176  unsigned char injectorSwitchOns; ///< number of times the injector switched on
177  unsigned char injectorSwitchOffs; ///< number of times the injector switched off to stay off
178  unsigned char injectorTimerExtensions; ///< number of times the injector ISR fired and rescheduled itself to fire and do nothing again
179  unsigned char injectorTimerExtensionFinals; ///< number of times the injector ISR fired and scheduled the injector to switch on
180 
181  unsigned char injectorSelfSchedules; ///< number of times the injector switched off and scheduled itself again
182  unsigned char injectorSelfScheduleExtensions; ///< number of times the injector switched off and scheduled itself again with timer extension
183 
184  unsigned char syncedADCreadings; ///< Incremented each time a synchronous ADC reading is taken
185  unsigned char timeoutADCreadings; ///< Incremented for each ADC reading in RTC because of timeout
186  unsigned char calculationsPerformed; ///< Incremented for each time the fuel and ign calcs are done
187 
188  unsigned char sparePadding; ///< Replace with something useful
189 } Counter;
190 
191 
192 #else
193  /* let us know if we are being untidy with headers */
194  #warning "Header file COUNTER_TYPES_H seen before, sort it out!"
195 /* end of the wrapper ifdef from the very top */
196 #endif