Removed old s19 files and installed new ones.
[freeems-vanilla.git] / src / utils.c
blobb574b6382014600461a2a2f2250a14f9bdc486c5
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 utils.c
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.
34 * @author Fred Cooke
38 #define UTILS_C
39 #include "inc/freeEMS.h"
40 #include "inc/commsISRs.h"
41 #include "inc/utils.h"
42 #include <string.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?
51 * @author Fred Cooke
53 * @param bool which set of data to enable.
55 void setupPagedRAM(unsigned char bool){
56 if(bool){
57 currentFuelRPage = RPAGE_FUEL_ONE;
58 currentTimeRPage = RPAGE_TIME_ONE;
59 currentTuneRPage = RPAGE_TUNE_ONE;
60 }else{
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.
76 * @author Fred Cooke
78 void resetToNonRunningState(){
79 /* Reset RPM to zero */
80 RPM0 = 0;
81 RPM1 = 0;
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.
99 * @author Fred Cooke
101 void adjustPWM(){
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.
117 * @author Fred Cooke
119 * @param Arrays a pointer to an ADCArray struct to store ADC values in.
121 void sampleEachADC(ADCArray *Arrays){
122 /* ATD0 */
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;
132 /* ATD1 */
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()
148 * @author Fred Cooke
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.
162 * @author Fred Cooke
164 * @param ms the number of milli seconds to kill
166 void sleep(unsigned short ms){
167 unsigned short j, k;
168 for(j=0;j<ms;j++)
169 for(k=0;k<5714;k++);
173 /** @brief Sleep for X micro seconds
175 * Run in a nested loop repeatedly for X micro seconds.
177 * @note Very approximate...
179 * @author Fred Cooke
181 * @param us the number of micro seconds to kill
183 void sleepMicro(unsigned short us){
184 unsigned short j, k;
185 for(j=0;j<us;j++)
186 for(k=0;k<6;k++);
190 /** @brief Simple checksum
192 * Generate a simple additive checksum for a block of data.
194 * @author Fred Cooke
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;
203 unsigned short i;
204 for(i=0;i<length;i++){
205 sum += *block;
206 block++;
208 return sum;
212 /** @brief Homebrew strcpy()
214 * strcpy() wouldn't compile for me for some reason so I wrote my own.
216 * @author Fred Cooke
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;
225 while(*source != 0){
226 *dest = *source;
227 dest++;
228 source++;
229 length++;
231 *dest = 0;
232 return length;