00001 /* utils.c 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 send them upstream to us at admin@diyefi.org 00021 00022 Thank you for choosing FreeEMS to run your engine! */ 00023 00024 #define UTILS_C 00025 #include "inc/freeEMS.h" 00026 #include "inc/commsISRs.h" 00027 #include "inc/utils.h" 00028 00029 00030 /* Do table switching based on boolean parameter */ 00031 void setupPagedRAM(unsigned char bool){ 00032 if(bool){ 00033 currentFuelRPage = RPAGE_FUEL_ONE; 00034 currentTimeRPage = RPAGE_TIME_ONE; 00035 currentTuneRPage = RPAGE_TUNE_ONE; 00036 }else{ 00037 currentFuelRPage = RPAGE_FUEL_TWO; 00038 currentTimeRPage = RPAGE_TIME_TWO; 00039 currentTuneRPage = RPAGE_TUNE_TWO; 00040 } 00041 00042 RPAGE = currentTuneRPage; 00043 } 00044 00045 /* Reset all state to non running */ 00046 void resetToNonRunningState(){ 00047 /* Reset RPM to zero */ 00048 RPM[recordADCBank] = 0; 00049 00050 /* Ensure tacho reads lowest possible value */ 00051 engineCyclePeriod = ticksPerCycleAtOneRPM; 00052 00053 /* Clear all sync flags to lost state */ 00054 //coreStatusA &= CLEAR_RPM_VALID; 00055 coreStatusA &= CLEAR_PRIMARY_SYNC; 00056 //coreStatusA &= CLEAR_SECONDARY_SYNC; 00057 00058 // TODO more stuff needs resetting here, but only critical things. 00059 } 00060 00061 00062 /* Demonstrate basic PWM usage */ 00063 void adjustPWM(){ 00064 PWMDTY0 = ATD0DR0 >> 2; // scale raw adc to a duty 00065 PWMDTY1 = ATD0DR1 >> 2; // scale raw adc to a duty 00066 PWMDTY2 = ATD0DR2 >> 2; // scale raw adc to a duty 00067 PWMDTY3 = ATD0DR3 >> 2; // scale raw adc to a duty 00068 PWMDTY4 = ATD0DR4 >> 2; // scale raw adc to a duty 00069 //PWMDTY5 = ATD0DR5 >> 2; // scale raw adc to a duty 00070 //PWMDTY6 = ATD0DR6 >> 2; // scale raw adc to a duty 00071 //PWMDTY7 = ATD0DR7 >> 2; // scale raw adc to a duty (user led instead) 00072 } 00073 00074 00075 /* Read ADCs into the correct bank one at a time linearly */ 00076 void sampleEachADC(ADCArray *Arrays){ 00077 /* ATD0 */ 00078 Arrays->IAT[recordADCBank] = ATD0DR0; 00079 Arrays->CHT[recordADCBank] = ATD0DR1; 00080 Arrays->TPS[recordADCBank] = ATD0DR2; 00081 Arrays->EGO[recordADCBank] = ATD0DR3; 00082 Arrays->MAP[recordADCBank] = ATD0DR4; 00083 Arrays->AAP[recordADCBank] = ATD0DR5; 00084 Arrays->BRV[recordADCBank] = ATD0DR6; 00085 Arrays->MAT[recordADCBank] = ATD0DR7; 00086 00087 /* ATD1 */ 00088 Arrays->EGO2[recordADCBank] = ATD1DR0; 00089 Arrays->IAP[recordADCBank] = ATD1DR1; 00090 Arrays->MAF[recordADCBank] = ATD1DR2; 00091 Arrays->SpareADC3[recordADCBank] = ATD1DR3; 00092 Arrays->SpareADC4[recordADCBank] = ATD1DR4; 00093 Arrays->SpareADC5[recordADCBank] = ATD1DR5; 00094 Arrays->SpareADC6[recordADCBank] = ATD1DR6; 00095 Arrays->SpareADC7[recordADCBank] = ATD1DR7; 00096 } 00097 00098 00099 /* Read ADCs into the correct bank in a loop using pointers */ 00100 void sampleLoopADC(ADCArray *Arrays){ 00101 // get the address of the ADC array 00102 unsigned short addr = (unsigned short)Arrays; 00103 00104 //sendUS(addr); 00105 unsigned char loop; 00106 /* (value of((address of ADCArrays struct) + (offset to start of bank(0 or half struct length)) + (offset to particular ADC (loopcounter * 4)) + (offset to correct element(0 or 2)))) = 00107 * (value of((address of ARRAY block) + (loop counter * 2))) */ 00108 00109 for(loop=0;loop<ADCS_IN_ADC0;loop++){ 00110 /* Do the first block */ 00111 DVUSP(addr + /*(0) +*/ (loop * 4) + (recordADCBank * 2)) = DVUSP(ATD0_BASE + (loop * 2)); 00112 00113 /* Do the second block */ 00114 DVUSP(addr + 32 + (loop * 4) + (recordADCBank * 2)) = DVUSP(ATD1_BASE + (loop * 2)); 00115 } 00116 } 00117 00118 00119 /* Loop repeatedly for X milli seconds. */ 00120 void sleep(unsigned short ms){ 00121 unsigned short j, k; 00122 for(j=0;j<ms;j++) 00123 for(k=0;k<5714;k++); 00124 } 00125 00126 00127 /* Loop repeatedly for X micro seconds. */ 00128 void sleepMicro(unsigned short us){ /* Very approximate... */ 00129 unsigned short j, k; 00130 for(j=0;j<us;j++) 00131 for(k=0;k<6;k++); 00132 } 00133 00134 00135 /* Generate a checksum for a block of data */ 00136 unsigned char checksum(unsigned char *block, unsigned short length){ 00137 unsigned char sum = 0; 00138 unsigned short i; 00139 for(i=0;i<length;i++){ 00140 sum += *block; 00141 block++; 00142 } 00143 return sum; 00144 } 00145 00146 unsigned short stringCopy(unsigned char* dest, unsigned char* source){ 00147 unsigned short length = 0; 00148 while(*source != 0){ 00149 *dest = *source; 00150 dest++; 00151 source++; 00152 length++; 00153 } 00154 *dest = 0; 00155 return length; 00156 }