FreeEMS  0.2.0-SNAPSHOT-282-g9efc524
Macros | Functions
utils.c File Reference

Utility functions only. More...

#include "inc/freeEMS.h"
#include "inc/commsISRs.h"
#include "inc/utils.h"
#include <string.h>
Include dependency graph for utils.c:

Go to the source code of this file.

Macros

#define UTILS_C

Functions

unsigned short safeAdd (unsigned short addend1, unsigned short addend2)
 Add two unsigned shorts safely.
unsigned short safeTrim (unsigned short addend1, signed short addend2)
 Add signed short to an unsigned short safely.
unsigned short safeScale (unsigned short baseValue, unsigned short dividend, unsigned short divisor)
 Scale without overflow.
void setupPagedRAM (unsigned char bool)
 Setup tune switching.
void adjustPWM ()
 Demonstrate PWM.
void sampleEachADC (ADCBuffer *Arrays)
 Read ADCs one at a time.
void sampleLoopADC (ADCBuffer *Arrays)
 Read ADCs in a loop.
void sleep (unsigned short ms)
 Sleep for X milli seconds.
void sleepMicro (unsigned short us)
 Sleep for X micro seconds.
unsigned char checksum (unsigned char *block, unsigned short length)
 Simple checksum.
unsigned short stringCopy (unsigned char *dest, unsigned char *source)
 Homebrew strcpy()
unsigned short compare (unsigned char *original, unsigned char *toCheck, unsigned short length)

Detailed Description

Utility functions only.

General purpose utility functions that are used in various places throughout the code base. Functions should only be placed here if they are not strongly related to any other set of functionality.

Definition in file utils.c.

Macro Definition Documentation

#define UTILS_C

Definition at line 37 of file utils.c.

Function Documentation

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.

Parameters
addend1
addend2

Definition at line 51 of file utils.c.

References SHORTMAX.

Referenced by calculateFuelAndIgnition(), and main().

{
if((SHORTMAX - addend1) > addend2){
return addend1 + addend2;
}else{
return SHORTMAX;
}
}

Here is the caller graph for this function:

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

Add signed short to an unsigned short safely.

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

Parameters
addend1
addend2

Definition at line 67 of file utils.c.

References SHORTMAX.

Referenced by calculateFuelAndIgnition().

{
if(addend2 < 0){
if(addend1 > -addend2){
return addend1 + addend2;
}else{
return 0;
}
}else if(addend2 > 0){
if(addend2 < (SHORTMAX - addend1)){
return addend1 + addend2;
}else{
return SHORTMAX;
}
}else{
return addend1;
}
}

Here is the caller graph for this function:

unsigned short safeScale ( unsigned short  baseValue,
unsigned short  dividend,
unsigned short  divisor 
)

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.

Parameters
baseValue
dividend
divisor

Definition at line 97 of file utils.c.

References SHORTMAX.

Referenced by calculateFuelAndIgnition().

{
/* Perform the scaling */
unsigned short scaled = ((unsigned long)baseValue * dividend) / divisor;
/* If the trim is greater than 100% then the trimmedPW MUST be larger */
/* If it's less than 100% it can't have overflowed. If it's not larger, it overflowed */
if((dividend > divisor) && (baseValue > scaled)){
return SHORTMAX;
}else{
return scaled;
}
}

Here is the caller graph for this function:

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?
Parameters
boolwhich set of data to enable.

Definition at line 119 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().

Here is the caller graph for this function:

void adjustPWM ( void  )

Demonstrate PWM.

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

Definition at line 138 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().

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

Here is the caller graph for this function:

void sampleEachADC ( ADCBuffer Arrays)

Read ADCs one at a time.

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

Parameters
Arraysa pointer to an ADCBuffer struct to store ADC values in.

Definition at line 156 of file utils.c.

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

Referenced by main(), PrimaryRPMISR(), and SecondaryRPMISR().

{
/* ATD0 */
Arrays->IAT = ATD0DR0;
Arrays->CHT = ATD0DR1;
Arrays->TPS = ATD0DR2;
Arrays->EGO = ATD0DR3;
Arrays->MAP = ATD0DR4;
Arrays->AAP = ATD0DR5;
Arrays->BRV = ATD0DR6;
Arrays->MAT = ATD0DR7;
/* ATD1 */
Arrays->EGO2 = ATD1DR0;
Arrays->IAP = ATD1DR1;
Arrays->MAF = ATD1DR2;
Arrays->SpareADC3 = ATD1DR3;
Arrays->SpareADC4 = ATD1DR4;
Arrays->SpareADC5 = ATD1DR5;
Arrays->SpareADC6 = ATD1DR6;
Arrays->SpareADC7 = ATD1DR7;
}

Here is the caller graph for this function:

void sampleLoopADC ( ADCBuffer Arrays)

Read ADCs in a loop.

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

Parameters
Arraysa pointer to an ADCBuffer 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 185 of file utils.c.

References ATD0_BASE, ATD1_BASE, and DVUSP.

{
// get the address of the ADC array
unsigned short addr = (unsigned short)Arrays;
//sendUS(addr);
unsigned char loop;
/* (value of((address of ADCBuffers struct) + (offset to start of bank(0 or half struct length)) + (offset to particular ADC (loopcounter * 4)) + (offset to correct element(0 or 2)))) =
* (value of((address of ARRAY block) + (loop counter * 2))) */
for(loop=0;loop<16;loop += 2){
/* Do the first block */
DVUSP(addr + loop) = DVUSP(ATD0_BASE + loop);
/* Do the second block */
DVUSP(addr + 16 + loop) = DVUSP(ATD1_BASE + loop);
/// @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.
}
}
void sleep ( unsigned short  ms)

Sleep for X milli seconds.

Run in a nested loop repeatedly for X milli seconds.

Parameters
msthe number of milli seconds to kill

Definition at line 226 of file utils.c.

{
unsigned short j, k;
for(j=0;j<ms;j++){
for(k=0;k<5714;k++){
}
}
}
void sleepMicro ( unsigned short  us)

Sleep for X micro seconds.

Run in a nested loop repeatedly for X micro seconds.

Note
Very approximate...
Parameters
usthe number of micro seconds to kill

Definition at line 243 of file utils.c.

Referenced by main().

{
unsigned short j, k;
for(j=0;j<us;j++){
for(k=0;k<6;k++){
}
}
}

Here is the caller graph for this function:

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

Simple checksum.

Generate a simple additive checksum for a block of data.

Parameters
blocka pointer to a memory region to checksum.
lengthhow large the memory region to checksum is.
Returns
a simple additive checksum.

Definition at line 261 of file utils.c.

Referenced by decodePacketAndRespond(), and finaliseAndSend().

{
unsigned char sum = 0;
while (length-- > 0){
sum += *block++;
}
return sum;
}

Here is the caller graph for this function:

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.

Parameters
destwhere to copy the null terminated string to.
sourcewhere to copy the null terminated string from.
Returns
the length of the string copied, including the zero byte terminator.

Definition at line 279 of file utils.c.

Referenced by decodePacketAndRespond().

{
unsigned short length = 0;
do {
*dest++ = *source++;
length++;
} while(*(source-1) != 0);
return length;
}

Here is the caller graph for this function:

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 293 of file utils.c.

Referenced by decodePacketAndRespond().

{
unsigned short i;
for(i=0;i<length;i++){
if(original[i] != toCheck[i]){
return i + 1; // zero = success
}
}
return 0;
}

Here is the caller graph for this function: