utils.h File Reference

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define EXTERN   extern

Functions

EXTERN unsigned short safeAdd (unsigned short, unsigned short)
 Add two unsigned shorts safely.
EXTERN unsigned short safeTrim (unsigned short, signed short)
 Add two unsigned shorts safely.
EXTERN unsigned short safeScale (unsigned short, unsigned short)
 Scale without overflow.
EXTERN void sleep (unsigned short) FPAGE_FE
 Sleep for X milli seconds.
EXTERN void sleepMicro (unsigned short) FPAGE_FE
 Sleep for X micro seconds.
EXTERN void adjustPWM (void) FPAGE_FE
 Demonstrate PWM.
EXTERN void setupPagedRAM (unsigned char) FPAGE_F8
 Setup tune switching.
EXTERN void resetToNonRunningState (void) FPAGE_F8
 Reset key state.
EXTERN void sampleEachADC (ADCArray *) FPAGE_F8
 Read ADCs one at a time.
EXTERN void sampleLoopADC (ADCArray *) FPAGE_F8
 Read ADCs in a loop.
EXTERN unsigned char checksum (unsigned char *, unsigned short) FPAGE_F8
 Simple checksum.
EXTERN unsigned short stringCopy (unsigned char *, unsigned char *) FPAGE_F8
 Homebrew strcpy().
EXTERN unsigned short compare (unsigned char *, unsigned char *, unsigned short)

Detailed Description

Definition in file utils.h.


Define Documentation

#define EXTERN   extern

Definition at line 47 of file utils.h.


Function Documentation

EXTERN void adjustPWM ( void   ) 

Demonstrate PWM.

Demonstrate basic PWM module usage by setting duty to scaled ADC inputs.

Author:
Fred Cooke

Definition at line 174 of file utils.c.

References ATD0DR0, ATD0DR1, ATD0DR2, ATD0DR3, ATD0DR4, ATD0DR5, ATD0DR6, ATD0DR7, PWMDTY0, PWMDTY1, PWMDTY2, PWMDTY3, PWMDTY4, PWMDTY5, PWMDTY6, and PWMDTY7.

Referenced by main().

00174                 {
00175     PWMDTY0 = ATD0DR0 >> 2; // scale raw adc to a duty
00176     PWMDTY1 = ATD0DR1 >> 2; // scale raw adc to a duty
00177     PWMDTY2 = ATD0DR2 >> 2; // scale raw adc to a duty
00178     PWMDTY3 = ATD0DR3 >> 2; // scale raw adc to a duty
00179     PWMDTY4 = ATD0DR4 >> 2; // scale raw adc to a duty
00180     PWMDTY5 = ATD0DR5 >> 2; // scale raw adc to a duty
00181     PWMDTY6 = ATD0DR6 >> 2; // scale raw adc to a duty
00182     PWMDTY7 = ATD0DR7 >> 2; // scale raw adc to a duty (user led instead at the moment, see init)
00183 }

EXTERN unsigned char checksum ( unsigned char *  block,
unsigned short  length 
)

Simple checksum.

Generate a simple additive checksum for a block of data.

Author:
Fred Cooke
Parameters:
block a pointer to a memory region to checksum.
length how large the memory region to checksum is.
Returns:
a simple additive checksum.

Definition at line 309 of file utils.c.

Referenced by checksumAndSend().

00309                                                                    {
00310     unsigned char sum = 0;
00311     while (length-- > 0){
00312         sum += *block++;
00313     }
00314     return sum;
00315 }

EXTERN unsigned short compare ( unsigned char *  original,
unsigned char *  toCheck,
unsigned short  length 
)
Returns:
a one based index of the failure point
Note:
this will return a positive result with bad data in the last position of a maximum sized block

Definition at line 343 of file utils.c.

Referenced by decodePacketAndRespond(), and initXgate().

00343                                                                                               {
00344     unsigned short i;
00345     for(i=0;i<length;i++){
00346         if(original[i] != toCheck[i]){
00347             return i + 1; // zero = success
00348         }
00349     }
00350     return 0;
00351 }

EXTERN void resetToNonRunningState ( void   ) 

Reset key state.

Reset all important variables to their non-running state.

Todo:
TODO bring this up to date and/or find a better way to do it.
Author:
Fred Cooke

Definition at line 151 of file utils.c.

References CLEAR_PRIMARY_SYNC, coreStatusA, engineCyclePeriod, RPM0, RPM1, and ticksPerCycleAtOneRPM.

Referenced by main().

00151                              {
00152     /* Reset RPM to zero */
00153     RPM0 = 0;
00154     RPM1 = 0;
00155 
00156     /* Ensure tacho reads lowest possible value */
00157     engineCyclePeriod = ticksPerCycleAtOneRPM;
00158 
00159     /* Clear all sync flags to lost state */
00160     //coreStatusA &= CLEAR_RPM_VALID;
00161     coreStatusA &= CLEAR_PRIMARY_SYNC;
00162     //coreStatusA &= CLEAR_SECONDARY_SYNC;
00163 
00164     // TODO more stuff needs resetting here, but only critical things.
00165 }

EXTERN unsigned short safeAdd ( unsigned short  addend1,
unsigned short  addend2 
)

Add two unsigned shorts safely.

This will either return short max or the sum of the two arguments.

Author:
Fred Cooke
Parameters:
addend1 
addend2 

Definition at line 55 of file utils.c.

References SHORTMAX.

Referenced by calculateFuelAndIgnition().

00055                                                                       {
00056     if((SHORTMAX - addend1) > addend2){
00057         return addend1 + addend2;
00058     }else{
00059         return SHORTMAX;
00060     }
00061 }

EXTERN unsigned short safeScale ( unsigned short  baseValue,
unsigned short  scaler 
)

Scale without overflow.

Takes a base value and a scaler where 0x8000/32768 means 100%, 0 means 0% and 0xFFFF/65535 means 200%, and returns the baseValue multiplied, in effect, by the resulting percentage figure.

Author:
Fred Cooke
Parameters:
baseValue 
scaler 

Definition at line 104 of file utils.c.

References SHORTHALF, and SHORTMAX.

Referenced by calculateFuelAndIgnition().

00104                                                                          {
00105     /* Perform the scaling */
00106     unsigned short scaled = ((unsigned long)baseValue * scaler) / SHORTHALF;
00107 
00108     /* If the trim is greater than 100% then the trimmedPW MUST be larger */
00109     /* If it's less than 100% it can't have overflowed */        /* If it's not larger, it overflowed */
00110     if((scaler > SHORTHALF) && (baseValue > scaled)){
00111         return SHORTMAX;
00112     }else{
00113         return scaled;
00114     }
00115 }

EXTERN unsigned short safeTrim ( unsigned short  addend1,
signed short  addend2 
)

Add two unsigned shorts safely.

This will either return short max or the sum of the two arguments.

Author:
Fred Cooke
Parameters:
addend1 
addend2 

Definition at line 73 of file utils.c.

References SHORTMAX.

Referenced by calculateFuelAndIgnition().

00073                                                                      {
00074 
00075     if(addend2 < 0){
00076         if(addend1 > -addend2){
00077             return addend1 + addend2;
00078         }else{
00079             return 0;
00080         }
00081     }else if(addend2 > 0){
00082         if(addend2 < (SHORTMAX - addend1)){
00083             return addend1 + addend2;
00084         }else{
00085             return SHORTMAX;
00086         }
00087     }else{
00088         return addend1;
00089     }
00090 }

EXTERN void sampleEachADC ( ADCArray Arrays  ) 

Read ADCs one at a time.

Read ADCs into the correct bank one at a time by name.

Author:
Fred Cooke
Parameters:
Arrays a pointer to an ADCArray struct to store ADC values in.

Definition at line 194 of file utils.c.

References ADCArray::AAP, ATD0DR0, ATD0DR1, ATD0DR2, ATD0DR3, ATD0DR4, ATD0DR5, ATD0DR6, ATD0DR7, ATD1DR0, ATD1DR1, ATD1DR2, ATD1DR3, ATD1DR4, ATD1DR5, ATD1DR6, ATD1DR7, ADCArray::BRV, ADCArray::CHT, ADCArray::EGO, ADCArray::EGO2, ADCArray::IAP, ADCArray::IAT, ADCArray::MAF, ADCArray::MAP, ADCArray::MAT, ADCArray::SpareADC3, ADCArray::SpareADC4, ADCArray::SpareADC5, ADCArray::SpareADC6, ADCArray::SpareADC7, and ADCArray::TPS.

Referenced by main(), and PrimaryRPMISR().

00194                                     {
00195     /* ATD0 */
00196     Arrays->IAT = ATD0DR0;
00197     Arrays->CHT = ATD0DR1;
00198     Arrays->TPS = ATD0DR2;
00199     Arrays->EGO = ATD0DR3;
00200     Arrays->MAP = ATD0DR4;
00201     Arrays->AAP = ATD0DR5;
00202     Arrays->BRV = ATD0DR6;
00203     Arrays->MAT = ATD0DR7;
00204 
00205     /* ATD1 */
00206     Arrays->EGO2 = ATD1DR0;
00207     Arrays->IAP = ATD1DR1;
00208     Arrays->MAF = ATD1DR2;
00209     Arrays->SpareADC3 = ATD1DR3;
00210     Arrays->SpareADC4 = ATD1DR4;
00211     Arrays->SpareADC5 = ATD1DR5;
00212     Arrays->SpareADC6 = ATD1DR6;
00213     Arrays->SpareADC7 = ATD1DR7;
00214 }

EXTERN void sampleLoopADC ( ADCArray Arrays  ) 

Read ADCs in a loop.

Read ADCs into the correct bank in a loop using pointers.

Author:
Fred Cooke
Parameters:
Arrays a pointer to an ADCArray struct to store ADC values in.

Todo:
TODO this needs to be split into two loops one for the small block and one for the big one for the future chips.

Definition at line 225 of file utils.c.

References ATD0_BASE, ATD1_BASE, and DVUSP.

00225                                     {
00226     // get the address of the ADC array
00227     unsigned short addr = (unsigned short)Arrays;
00228 
00229     //sendUS(addr);
00230     unsigned char loop;
00231     /* (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)))) =
00232      * (value of((address of ARRAY block) + (loop counter * 2))) */
00233 
00234     for(loop=0;loop<16;loop += 2){
00235         /* Do the first block */
00236         DVUSP(addr + loop) = DVUSP(ATD0_BASE + loop);
00237 
00238         /* Do the second block */
00239         DVUSP(addr + 16 + loop) = DVUSP(ATD1_BASE + loop);
00241     }
00242 }

EXTERN void setupPagedRAM ( unsigned char  bool  ) 

Setup tune switching.

Place the correct set of tables in RAM based on a boolean parameter

Todo:
TODO change parameter style to be a pointer to a register and a mask?
Author:
Fred Cooke
Parameters:
bool which set of data to enable.

Definition at line 128 of file utils.c.

References currentFuelRPage, currentTimeRPage, currentTuneRPage, RPAGE, RPAGE_FUEL_ONE, RPAGE_FUEL_TWO, RPAGE_TIME_ONE, RPAGE_TIME_TWO, RPAGE_TUNE_ONE, and RPAGE_TUNE_TWO.

Referenced by initAllPagedRAM().

00128                                       {
00129     if(bool){
00130         currentFuelRPage = RPAGE_FUEL_ONE;
00131         currentTimeRPage = RPAGE_TIME_ONE;
00132         currentTuneRPage = RPAGE_TUNE_ONE;
00133     }else{
00134         currentFuelRPage = RPAGE_FUEL_TWO;
00135         currentTimeRPage = RPAGE_TIME_TWO;
00136         currentTuneRPage = RPAGE_TUNE_TWO;
00137     }
00138 
00139     RPAGE = currentTuneRPage;
00140 }

EXTERN void sleep ( unsigned short  ms  ) 

Sleep for X milli seconds.

Run in a nested loop repeatedly for X milli seconds.

Author:
Fred Cooke
Parameters:
ms the number of milli seconds to kill

Definition at line 270 of file utils.c.

Referenced by initConfiguration().

00270                              {
00271     unsigned short j, k;
00272     for(j=0;j<ms;j++){
00273         for(k=0;k<5714;k++){
00274         }
00275     }
00276 }

EXTERN void sleepMicro ( unsigned short  us  ) 

Sleep for X micro seconds.

Run in a nested loop repeatedly for X micro seconds.

Note:
Very approximate...
Author:
Fred Cooke
Parameters:
us the number of micro seconds to kill

Definition at line 289 of file utils.c.

Referenced by main().

00289                                   {
00290     unsigned short j, k;
00291     for(j=0;j<us;j++){
00292         for(k=0;k<6;k++){
00293         }
00294     }
00295 }

EXTERN unsigned short stringCopy ( unsigned char *  dest,
unsigned char *  source 
)

Homebrew strcpy().

strcpy() wouldn't compile for me for some reason so I wrote my own.

Author:
Fred Cooke
Parameters:
dest where to copy the null terminated string to.
source where to copy the null terminated string from.
Returns:
the length of the string copied.

Definition at line 329 of file utils.c.

Referenced by sendDebugInternal().

00329                                                                      {
00330     short length = -1;
00331     do {
00332         *dest++ = *source++;
00333         length++;
00334     } while(*(source-1) != 0);
00335     return (unsigned short) length;
00336 }

Generated on Sat Oct 16 21:29:23 2010 for FreeEMS by  doxygen 1.6.3