00001 /* FreeEMS - the open source engine management system 00002 00003 Copyright 2008 Fred Cooke 00004 00005 This file is part of the FreeEMS project. 00006 00007 FreeEMS software is free software: you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation, either version 3 of the License, or 00010 (at your option) any later version. 00011 00012 FreeEMS software is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with any FreeEMS software. If not, see http://www.gnu.org/licenses/ 00019 00020 We ask that if you make any changes to this file you email them upstream to 00021 us at admin(at)diyefi(dot)org or, even better, fork the code on github.com! 00022 00023 Thank you for choosing FreeEMS to run your engine! */ 00024 00025 00038 #define UTILS_C 00039 #include "inc/freeEMS.h" 00040 #include "inc/commsISRs.h" 00041 #include "inc/utils.h" 00042 #include <string.h> 00043 00044 00055 void setupPagedRAM(unsigned char bool){ 00056 if(bool){ 00057 currentFuelRPage = RPAGE_FUEL_ONE; 00058 currentTimeRPage = RPAGE_TIME_ONE; 00059 currentTuneRPage = RPAGE_TUNE_ONE; 00060 }else{ 00061 currentFuelRPage = RPAGE_FUEL_TWO; 00062 currentTimeRPage = RPAGE_TIME_TWO; 00063 currentTuneRPage = RPAGE_TUNE_TWO; 00064 } 00065 00066 RPAGE = currentTuneRPage; 00067 } 00068 00069 00078 void resetToNonRunningState(){ 00079 /* Reset RPM to zero */ 00080 RPM0 = 0; 00081 RPM1 = 0; 00082 00083 /* Ensure tacho reads lowest possible value */ 00084 engineCyclePeriod = ticksPerCycleAtOneRPM; 00085 00086 /* Clear all sync flags to lost state */ 00087 //coreStatusA &= CLEAR_RPM_VALID; 00088 coreStatusA &= CLEAR_PRIMARY_SYNC; 00089 //coreStatusA &= CLEAR_SECONDARY_SYNC; 00090 00091 // TODO more stuff needs resetting here, but only critical things. 00092 } 00093 00094 00101 void adjustPWM(){ 00102 PWMDTY0 = ATD0DR0 >> 2; // scale raw adc to a duty 00103 PWMDTY1 = ATD0DR1 >> 2; // scale raw adc to a duty 00104 PWMDTY2 = ATD0DR2 >> 2; // scale raw adc to a duty 00105 PWMDTY3 = ATD0DR3 >> 2; // scale raw adc to a duty 00106 PWMDTY4 = ATD0DR4 >> 2; // scale raw adc to a duty 00107 PWMDTY5 = ATD0DR5 >> 2; // scale raw adc to a duty 00108 PWMDTY6 = ATD0DR6 >> 2; // scale raw adc to a duty 00109 PWMDTY7 = ATD0DR7 >> 2; // scale raw adc to a duty (user led instead at the moment, see init) 00110 } 00111 00112 00121 void sampleEachADC(ADCArray *Arrays){ 00122 /* ATD0 */ 00123 Arrays->IAT = ATD0DR0; 00124 Arrays->CHT = ATD0DR1; 00125 Arrays->TPS = ATD0DR2; 00126 Arrays->EGO = ATD0DR3; 00127 Arrays->MAP = ATD0DR4; 00128 Arrays->AAP = ATD0DR5; 00129 Arrays->BRV = ATD0DR6; 00130 Arrays->MAT = ATD0DR7; 00131 00132 /* ATD1 */ 00133 Arrays->EGO2 = ATD1DR0; 00134 Arrays->IAP = ATD1DR1; 00135 Arrays->MAF = ATD1DR2; 00136 Arrays->SpareADC3 = ATD1DR3; 00137 Arrays->SpareADC4 = ATD1DR4; 00138 Arrays->SpareADC5 = ATD1DR5; 00139 Arrays->SpareADC6 = ATD1DR6; 00140 Arrays->SpareADC7 = ATD1DR7; 00141 } 00142 00143 00152 void sampleBlockADC(ADCArray *Arrays){ 00153 memcpy(Arrays, (void*)ATD0_BASE, 16); 00154 memcpy(Arrays+16, (void*)ATD1_BASE, 16); 00155 } 00156 00157 00166 void sleep(unsigned short ms){ 00167 unsigned short j, k; 00168 for(j=0;j<ms;j++) 00169 for(k=0;k<5714;k++); 00170 } 00171 00172 00183 void sleepMicro(unsigned short us){ 00184 unsigned short j, k; 00185 for(j=0;j<us;j++) 00186 for(k=0;k<6;k++); 00187 } 00188 00189 00201 unsigned char checksum(unsigned char *block, unsigned short length){ 00202 unsigned char sum = 0; 00203 unsigned short i; 00204 for(i=0;i<length;i++){ 00205 sum += *block; 00206 block++; 00207 } 00208 return sum; 00209 } 00210 00211 00223 unsigned short stringCopy(unsigned char* dest, unsigned char* source){ 00224 unsigned short length = 0; 00225 while(*source != 0){ 00226 *dest = *source; 00227 dest++; 00228 source++; 00229 length++; 00230 } 00231 *dest = 0; 00232 return length; 00233 }