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! */
28 * @brief Utility functions only
30 * General purpose utility functions that are used in various places throughout
31 * the code base. Functions should only be placed here if they are not strongly
32 * related to any other set of functionality.
39 #include "inc/freeEMS.h"
40 #include "inc/commsISRs.h"
41 #include "inc/utils.h"
45 /** @brief Setup tune switching
47 * Place the correct set of tables in RAM based on a boolean parameter
49 * @todo TODO change parameter style to be a pointer to a register and a mask?
53 * @param bool which set of data to enable.
55 void setupPagedRAM(unsigned char bool){
57 currentFuelRPage
= RPAGE_FUEL_ONE
;
58 currentTimeRPage
= RPAGE_TIME_ONE
;
59 currentTuneRPage
= RPAGE_TUNE_ONE
;
61 currentFuelRPage
= RPAGE_FUEL_TWO
;
62 currentTimeRPage
= RPAGE_TIME_TWO
;
63 currentTuneRPage
= RPAGE_TUNE_TWO
;
66 RPAGE
= currentTuneRPage
;
70 /** @brief Reset key state
72 * Reset all important variables to their non-running state.
74 * @todo TODO bring this up to date and/or find a better way to do it.
78 void resetToNonRunningState(){
79 /* Reset RPM to zero */
83 /* Ensure tacho reads lowest possible value */
84 engineCyclePeriod
= ticksPerCycleAtOneRPM
;
86 /* Clear all sync flags to lost state */
87 //coreStatusA &= CLEAR_RPM_VALID;
88 coreStatusA
&= CLEAR_PRIMARY_SYNC
;
89 //coreStatusA &= CLEAR_SECONDARY_SYNC;
91 // TODO more stuff needs resetting here, but only critical things.
95 /** @brief Demonstrate PWM
97 * Demonstrate basic PWM module usage by setting duty to scaled ADC inputs.
102 PWMDTY0
= ATD0DR0
>> 2; // scale raw adc to a duty
103 PWMDTY1
= ATD0DR1
>> 2; // scale raw adc to a duty
104 PWMDTY2
= ATD0DR2
>> 2; // scale raw adc to a duty
105 PWMDTY3
= ATD0DR3
>> 2; // scale raw adc to a duty
106 PWMDTY4
= ATD0DR4
>> 2; // scale raw adc to a duty
107 PWMDTY5
= ATD0DR5
>> 2; // scale raw adc to a duty
108 PWMDTY6
= ATD0DR6
>> 2; // scale raw adc to a duty
109 PWMDTY7
= ATD0DR7
>> 2; // scale raw adc to a duty (user led instead at the moment, see init)
113 /** @brief Read ADCs one at a time
115 * Read ADCs into the correct bank one at a time by name.
119 * @param Arrays a pointer to an ADCArray struct to store ADC values in.
121 void sampleEachADC(ADCArray
*Arrays
){
123 Arrays
->IAT
= ATD0DR0
;
124 Arrays
->CHT
= ATD0DR1
;
125 Arrays
->TPS
= ATD0DR2
;
126 Arrays
->EGO
= ATD0DR3
;
127 Arrays
->MAP
= ATD0DR4
;
128 Arrays
->AAP
= ATD0DR5
;
129 Arrays
->BRV
= ATD0DR6
;
130 Arrays
->MAT
= ATD0DR7
;
133 Arrays
->EGO2
= ATD1DR0
;
134 Arrays
->IAP
= ATD1DR1
;
135 Arrays
->MAF
= ATD1DR2
;
136 Arrays
->SpareADC3
= ATD1DR3
;
137 Arrays
->SpareADC4
= ATD1DR4
;
138 Arrays
->SpareADC5
= ATD1DR5
;
139 Arrays
->SpareADC6
= ATD1DR6
;
140 Arrays
->SpareADC7
= ATD1DR7
;
144 /** @brief Read ADCs with memcpy()
146 * Read ADCs into the correct bank using two fixed calls to memcpy()
150 * @param Arrays a pointer to an ADCArray struct to store ADC values in.
152 void sampleBlockADC(ADCArray
*Arrays
){
153 memcpy(Arrays
, (void*)ATD0_BASE
, 16);
154 memcpy(Arrays
+16, (void*)ATD1_BASE
, 16);
158 /** @brief Sleep for X milli seconds
160 * Run in a nested loop repeatedly for X milli seconds.
164 * @param ms the number of milli seconds to kill
166 void sleep(unsigned short ms
){
173 /** @brief Sleep for X micro seconds
175 * Run in a nested loop repeatedly for X micro seconds.
177 * @note Very approximate...
181 * @param us the number of micro seconds to kill
183 void sleepMicro(unsigned short us
){
190 /** @brief Simple checksum
192 * Generate a simple additive checksum for a block of data.
196 * @param block a pointer to a memory region to checksum.
197 * @param length how large the memory region to checksum is.
199 * @return a simple additive checksum.
201 unsigned char checksum(unsigned char *block
, unsigned short length
){
202 unsigned char sum
= 0;
204 for(i
=0;i
<length
;i
++){
212 /** @brief Homebrew strcpy()
214 * strcpy() wouldn't compile for me for some reason so I wrote my own.
218 * @param dest where to copy the null terminated string to.
219 * @param source where to copy the null terminated string from.
221 * @return the length of the string copied.
223 unsigned short stringCopy(unsigned char* dest
, unsigned char* source
){
224 unsigned short length
= 0;