1 /* FreeEMS - the open source engine management system
3 * Copyright 2008, 2009 Fred Cooke, Philip L Johnson
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!
29 * @brief Utility functions only
31 * General purpose utility functions that are used in various places throughout
32 * the code base. Functions should only be placed here if they are not strongly
33 * related to any other set of functionality.
40 #include "inc/freeEMS.h"
41 #include "inc/commsISRs.h"
42 #include "inc/utils.h"
46 /** @brief Setup tune switching
48 * Place the correct set of tables in RAM based on a boolean parameter
50 * @todo TODO change parameter style to be a pointer to a register and a mask?
54 * @param bool which set of data to enable.
56 void setupPagedRAM(unsigned char bool){
58 currentFuelRPage
= RPAGE_FUEL_ONE
;
59 currentTimeRPage
= RPAGE_TIME_ONE
;
60 currentTuneRPage
= RPAGE_TUNE_ONE
;
62 currentFuelRPage
= RPAGE_FUEL_TWO
;
63 currentTimeRPage
= RPAGE_TIME_TWO
;
64 currentTuneRPage
= RPAGE_TUNE_TWO
;
67 RPAGE
= currentTuneRPage
;
71 /** @brief Reset key state
73 * Reset all important variables to their non-running state.
75 * @todo TODO bring this up to date and/or find a better way to do it.
79 void resetToNonRunningState(){
80 /* Reset RPM to zero */
84 /* Ensure tacho reads lowest possible value */
85 engineCyclePeriod
= ticksPerCycleAtOneRPM
;
87 /* Clear all sync flags to lost state */
88 //coreStatusA &= CLEAR_RPM_VALID;
89 coreStatusA
&= CLEAR_PRIMARY_SYNC
;
90 //coreStatusA &= CLEAR_SECONDARY_SYNC;
92 // TODO more stuff needs resetting here, but only critical things.
96 /** @brief Demonstrate PWM
98 * Demonstrate basic PWM module usage by setting duty to scaled ADC inputs.
103 PWMDTY0
= ATD0DR0
>> 2; // scale raw adc to a duty
104 PWMDTY1
= ATD0DR1
>> 2; // scale raw adc to a duty
105 PWMDTY2
= ATD0DR2
>> 2; // scale raw adc to a duty
106 PWMDTY3
= ATD0DR3
>> 2; // scale raw adc to a duty
107 PWMDTY4
= ATD0DR4
>> 2; // scale raw adc to a duty
108 PWMDTY5
= ATD0DR5
>> 2; // scale raw adc to a duty
109 PWMDTY6
= ATD0DR6
>> 2; // scale raw adc to a duty
110 PWMDTY7
= ATD0DR7
>> 2; // scale raw adc to a duty (user led instead at the moment, see init)
114 /** @brief Read ADCs one at a time
116 * Read ADCs into the correct bank one at a time by name.
120 * @param Arrays a pointer to an ADCArray struct to store ADC values in.
122 void sampleEachADC(ADCArray
*Arrays
){
124 Arrays
->IAT
= ATD0DR0
;
125 Arrays
->CHT
= ATD0DR1
;
126 Arrays
->TPS
= ATD0DR2
;
127 Arrays
->EGO
= ATD0DR3
;
128 Arrays
->MAP
= ATD0DR4
;
129 Arrays
->AAP
= ATD0DR5
;
130 Arrays
->BRV
= ATD0DR6
;
131 Arrays
->MAT
= ATD0DR7
;
134 Arrays
->EGO2
= ATD1DR0
;
135 Arrays
->IAP
= ATD1DR1
;
136 Arrays
->MAF
= ATD1DR2
;
137 Arrays
->SpareADC3
= ATD1DR3
;
138 Arrays
->SpareADC4
= ATD1DR4
;
139 Arrays
->SpareADC5
= ATD1DR5
;
140 Arrays
->SpareADC6
= ATD1DR6
;
141 Arrays
->SpareADC7
= ATD1DR7
;
145 /** @brief Read ADCs with memcpy()
147 * Read ADCs into the correct bank using two fixed calls to memcpy()
151 * @param Arrays a pointer to an ADCArray struct to store ADC values in.
153 void sampleBlockADC(ADCArray
*Arrays
){
154 memcpy(Arrays
, (void*)ATD0_BASE
, 16);
155 memcpy(Arrays
+16, (void*)ATD1_BASE
, 16);
159 /** @brief Sleep for X milli seconds
161 * Run in a nested loop repeatedly for X milli seconds.
165 * @param ms the number of milli seconds to kill
167 void sleep(unsigned short ms
){
176 /** @brief Sleep for X micro seconds
178 * Run in a nested loop repeatedly for X micro seconds.
180 * @note Very approximate...
184 * @param us the number of micro seconds to kill
186 void sleepMicro(unsigned short us
){
195 /** @brief Simple checksum
197 * Generate a simple additive checksum for a block of data.
201 * @param block a pointer to a memory region to checksum.
202 * @param length how large the memory region to checksum is.
204 * @return a simple additive checksum.
206 unsigned char checksum(unsigned char *block
, unsigned short length
){
207 unsigned char sum
= 0;
208 while (length
-- > 0){
215 /** @brief Homebrew strcpy()
217 * strcpy() wouldn't compile for me for some reason so I wrote my own.
221 * @param dest where to copy the null terminated string to.
222 * @param source where to copy the null terminated string from.
224 * @return the length of the string copied.
226 unsigned short stringCopy(unsigned char* dest
, unsigned char* source
){
231 } while(*(source
-1) != 0);
232 return (unsigned short) length
;