Updated copyright notice to include Phil.
[freeems-vanilla.git] / src / utils.c
blobd5e16f92ceb1befc4ca7f2ce2fd07fe6f742e8fc
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!
27 /** @file utils.c
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.
35 * @author Fred Cooke
39 #define UTILS_C
40 #include "inc/freeEMS.h"
41 #include "inc/commsISRs.h"
42 #include "inc/utils.h"
43 #include <string.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?
52 * @author Fred Cooke
54 * @param bool which set of data to enable.
56 void setupPagedRAM(unsigned char bool){
57 if(bool){
58 currentFuelRPage = RPAGE_FUEL_ONE;
59 currentTimeRPage = RPAGE_TIME_ONE;
60 currentTuneRPage = RPAGE_TUNE_ONE;
61 }else{
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.
77 * @author Fred Cooke
79 void resetToNonRunningState(){
80 /* Reset RPM to zero */
81 RPM0 = 0;
82 RPM1 = 0;
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.
100 * @author Fred Cooke
102 void adjustPWM(){
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.
118 * @author Fred Cooke
120 * @param Arrays a pointer to an ADCArray struct to store ADC values in.
122 void sampleEachADC(ADCArray *Arrays){
123 /* ATD0 */
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;
133 /* ATD1 */
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()
149 * @author Fred Cooke
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.
163 * @author Fred Cooke
165 * @param ms the number of milli seconds to kill
167 void sleep(unsigned short ms){
168 unsigned short j, k;
169 for(j=0;j<ms;j++){
170 for(k=0;k<5714;k++){
176 /** @brief Sleep for X micro seconds
178 * Run in a nested loop repeatedly for X micro seconds.
180 * @note Very approximate...
182 * @author Fred Cooke
184 * @param us the number of micro seconds to kill
186 void sleepMicro(unsigned short us){
187 unsigned short j, k;
188 for(j=0;j<us;j++){
189 for(k=0;k<6;k++){
195 /** @brief Simple checksum
197 * Generate a simple additive checksum for a block of data.
199 * @author Fred Cooke
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){
209 sum += *block++;
211 return sum;
215 /** @brief Homebrew strcpy()
217 * strcpy() wouldn't compile for me for some reason so I wrote my own.
219 * @author Fred Cooke
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){
227 short length = -1;
228 do {
229 *dest++ = *source++;
230 length++;
231 } while(*(source-1) != 0);
232 return (unsigned short) length;