utils.c

Go to the documentation of this file.
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 #include <string.h>
00029 
00030 
00031 /* Do table switching based on boolean parameter */
00032 void setupPagedRAM(unsigned char bool){
00033         if(bool){
00034                 currentFuelRPage = RPAGE_FUEL_ONE;
00035                 currentTimeRPage = RPAGE_TIME_ONE;
00036                 currentTuneRPage = RPAGE_TUNE_ONE;
00037         }else{
00038                 currentFuelRPage = RPAGE_FUEL_TWO;
00039                 currentTimeRPage = RPAGE_TIME_TWO;
00040                 currentTuneRPage = RPAGE_TUNE_TWO;
00041         }
00042 
00043         RPAGE = currentTuneRPage;
00044 }
00045 
00046 
00047 /* Reset all state to non running */
00048 void resetToNonRunningState(){
00049         /* Reset RPM to zero */
00050         RPM0 = 0;
00051         RPM1 = 0;
00052 
00053         /* Ensure tacho reads lowest possible value */
00054         engineCyclePeriod = ticksPerCycleAtOneRPM;
00055 
00056         /* Clear all sync flags to lost state */
00057         //coreStatusA &= CLEAR_RPM_VALID;
00058         coreStatusA &= CLEAR_PRIMARY_SYNC;
00059         //coreStatusA &= CLEAR_SECONDARY_SYNC;
00060 
00061         // TODO more stuff needs resetting here, but only critical things.
00062 }
00063 
00064 
00065 /* Demonstrate basic PWM usage */
00066 void adjustPWM(){
00067         PWMDTY0 = ATD0DR0 >> 2; // scale raw adc to a duty
00068         PWMDTY1 = ATD0DR1 >> 2; // scale raw adc to a duty
00069         PWMDTY2 = ATD0DR2 >> 2; // scale raw adc to a duty
00070         PWMDTY3 = ATD0DR3 >> 2; // scale raw adc to a duty
00071         PWMDTY4 = ATD0DR4 >> 2; // scale raw adc to a duty
00072         PWMDTY5 = ATD0DR5 >> 2; // scale raw adc to a duty
00073         PWMDTY6 = ATD0DR6 >> 2; // scale raw adc to a duty
00074         PWMDTY7 = ATD0DR7 >> 2; // scale raw adc to a duty (user led instead at the moment, see init)
00075 }
00076 
00077 
00078 /* Read ADCs into the correct bank one at a time linearly */
00079 void sampleEachADC(ADCArray *Arrays){
00080         /* ATD0 */
00081         Arrays->IAT = ATD0DR0;
00082         Arrays->CHT = ATD0DR1;
00083         Arrays->TPS = ATD0DR2;
00084         Arrays->EGO = ATD0DR3;
00085         Arrays->MAP = ATD0DR4;
00086         Arrays->AAP = ATD0DR5;
00087         Arrays->BRV = ATD0DR6;
00088         Arrays->MAT = ATD0DR7;
00089 
00090         /* ATD1 */
00091         Arrays->EGO2 = ATD1DR0;
00092         Arrays->IAP = ATD1DR1;
00093         Arrays->MAF = ATD1DR2;
00094         Arrays->SpareADC3 = ATD1DR3;
00095         Arrays->SpareADC4 = ATD1DR4;
00096         Arrays->SpareADC5 = ATD1DR5;
00097         Arrays->SpareADC6 = ATD1DR6;
00098         Arrays->SpareADC7 = ATD1DR7;
00099 }
00100 
00101 
00102 /* Read ADCs into the correct bank in a loop using pointers */
00103 void sampleBlockADC(ADCArray *Arrays){
00104         memcpy(Arrays, (void*)ATD0_BASE, 16);
00105         memcpy(Arrays+16, (void*)ATD1_BASE, 16);
00106 }
00107 
00108 
00109 /* Loop repeatedly for X milli seconds. */
00110 void sleep(unsigned short ms){
00111         unsigned short j, k;
00112         for(j=0;j<ms;j++)
00113                 for(k=0;k<5714;k++);
00114 }
00115 
00116 
00117 /* Loop repeatedly for X micro seconds. */
00118 void sleepMicro(unsigned short us){ /* Very approximate... */
00119         unsigned short j, k;
00120         for(j=0;j<us;j++)
00121                 for(k=0;k<6;k++);
00122 }
00123 
00124 
00125 /* Generate a checksum for a block of data */
00126 unsigned char checksum(unsigned char *block, unsigned short length){
00127         unsigned char sum = 0;
00128         unsigned short i;
00129         for(i=0;i<length;i++){
00130                 sum += *block;
00131                 block++;
00132         }
00133         return sum;
00134 }
00135 
00136 unsigned short stringCopy(unsigned char* dest, unsigned char* source){
00137         unsigned short length = 0;
00138         while(*source != 0){
00139                 *dest = *source;
00140                 dest++;
00141                 source++;
00142                 length++;
00143         }
00144         *dest = 0;
00145         return length;
00146 }

Generated on Mon Dec 22 21:29:19 2008 for freeems by  doxygen 1.5.2