init.c File Reference

Initialise the devices state. More...

#include "inc/freeEMS.h"
#include "inc/interrupts.h"
#include "inc/utils.h"
#include "inc/commsISRs.h"
#include "inc/pagedLocationBuffers.h"
#include "inc/init.h"
#include <string.h>

Include dependency graph for init.c:

Go to the source code of this file.

Defines

#define INIT_C

Functions

void init ()
 The main top level init.
void initPLL ()
 Set the PLL clock frequency.
void initIO ()
void initLookupAddresses ()
 Buffer lookup tables addresses.
void initFuelAddresses ()
 Buffer fuel tables addresses.
void initPagedRAMFuel (void)
 Copy fuel tables to RAM.
void initTimingAddresses ()
 Buffer timing tables addresses.
void initPagedRAMTime ()
 Copy timing tables to RAM.
void initTunableAddresses ()
 Buffer tunable tables addresses.
void initPagedRAMTune ()
void initAllPagedAddresses ()
 Buffer addresses of paged data.
void initAllPagedRAM ()
 Copies paged flash to RAM.
void initVariables ()
void initFlash ()
 Flash module setup.
void initECTTimer ()
void initPITTimer ()
void initSCIStuff ()
void initConfiguration ()
void initInterrupts ()


Detailed Description

Initialise the devices state.

Setup, configure and initialise all aspects of the devices state including but not limited to:

Author:
Fred Cooke

Definition in file init.c.


Define Documentation

#define INIT_C

Definition at line 45 of file init.c.


Function Documentation

void init ( void   ) 

The main top level init.

The main init function to be called from main.c before entering the main loop. This function is simply a delegator to the finer grained special purpose init functions.

Author:
Fred Cooke

Definition at line 63 of file init.c.

References ATOMIC_END, ATOMIC_START, initAllPagedRAM(), initConfiguration(), initECTTimer(), initFlash(), initInterrupts(), initIO(), initPITTimer(), initPLL(), initSCIStuff(), and initVariables().

Referenced by main().

00063            {
00064     ATOMIC_START();             /* Disable ALL interrupts while we configure the board ready for use */
00065     initPLL();                  /* Set up the PLL and use it */
00066     initIO();                   /* TODO make this config dependent. Set up all the pins and modules to be in low power harmless states */
00067     initAllPagedRAM();          /* Copy table and config blocks of data from flash to the paged ram blocks for fast data lookup */
00068     initVariables();            /* Initialise the rest of the running variables etc */
00069     initFlash();                /* TODO, finalise this */
00070     initECTTimer();             /* TODO move this to inside config in an organised way. Set up the timer module and its various aspects */
00071     initPITTimer();             /* TODO ditto... */
00072     initSCIStuff();             /* Setup the sci module(s) that we will use. */
00073     initConfiguration();        /* TODO Set user/feature/config up here! */
00074     initInterrupts();           /* still last, reset timers, enable interrupts here TODO move this to inside config in an organised way. Set up the rest of the individual interrupts */
00075     ATOMIC_END();               /* Re-enable any configured interrupts */
00076 }

Here is the call graph for this function:

void initAllPagedAddresses (  ) 

Buffer addresses of paged data.

Save the paged memory addresses to variables such that we can access them from another paged block with no warnings.

If you try to access paged data from the wrong place you get nasty warnings. These calls to functions that live in the same page that they are addressing prevent those warnings.

Note:
Many thanks to Jean Bélanger for the inspiration/idea to do this!
Author:
Fred Cooke

Definition at line 400 of file init.c.

References initFuelAddresses(), initLookupAddresses(), initTimingAddresses(), and initTunableAddresses().

Referenced by initAllPagedRAM().

00400                             {
00401     /* Setup pointers to lookup tables */
00402     initLookupAddresses();
00403     /* Setup pointers to the main tables */
00404     initFuelAddresses();
00405     initTimingAddresses();
00406     initTunableAddresses();
00407 }

Here is the call graph for this function:

void initAllPagedRAM (  ) 

Copies paged flash to RAM.

Take the tables and config from flash up to RAM to allow live tuning.

For the main tables and other paged config we need to adjust the RPAGE value to the appropriate one before copying up.

This function is simply a delegator to the ones for each flash page. Each one lives in the same paged space as the data it is copying up.

Author:
Fred Cooke

Definition at line 422 of file init.c.

References initAllPagedAddresses(), initPagedRAMFuel(), initPagedRAMTime(), initPagedRAMTune(), setupPagedRAM(), and TRUE.

Referenced by init().

00422                       {
00423     /* Setup the flash block pointers before copying flash to RAM using them */
00424     initAllPagedAddresses();
00425 
00426     /* Copy the tables up to their paged RAM blocks through the window from flash */
00427     initPagedRAMFuel();
00428     initPagedRAMTime();
00429     initPagedRAMTune();
00430 
00431     /* Default to page one for now, perhaps read the configured port straight out of reset in future? TODO */
00432     setupPagedRAM(TRUE); // probably something like (PORTA & TableSwitchingMask)
00433 }

Here is the call graph for this function:

void initConfiguration (  ) 

Definition at line 623 of file init.c.

References bootFuelConst, sensorRange::BRVMinimum, sensorRange::BRVRange, fixedConfig1::engineSettings, fixedConfigs1, fixedConfigs2, engineSetting::injectorFlow, masterFuelConstant, ONES16, engineSetting::perCylinderVolume, PORTS_BA, fixedConfig2::sensorRanges, sleep(), engineSetting::stoichiometricAFR, TPSADCRange, sensorRange::TPSClosedMAP, TPSMAPRange, sensorRange::TPSMaximumADC, sensorRange::TPSMinimumADC, and sensorRange::TPSOpenMAP.

Referenced by init().

00623                         {
00624 //  // TODO Calc TPS ADC range on startup or every time? this depends on whether we ensure that things work without a re init or reset or not.
00625 
00626 
00627     /* Add in tunable physical parameters at boot time TODO move to init.c TODO duplicate for secondary fuel? or split somehow?
00628      *nstant = ((masterConst * perCylinderVolume) / (stoichiometricAFR * injectorFlow));
00629      *nstant = ((139371764   * 16384            ) / (15053             * 4096        ));
00630      * OR
00631      *nstant = ((masterConst / injectorFlow) * perCylinderVolume) / stoichiometricAFR;
00632      *nstant = ((139371764   / 4096        ) * 16384            ) / 15053            ;
00633      * http://www.google.com/search?hl=en&safe=off&q=((139371764++%2F+4096+++++)+*+16384+++)+%2F+15053++++&btnG=Search */
00634     bootFuelConst = ((unsigned long)(masterFuelConstant / fixedConfigs1.engineSettings.injectorFlow) * fixedConfigs1.engineSettings.perCylinderVolume) / fixedConfigs1.engineSettings.stoichiometricAFR;
00635 
00636     /* The MAP range used to convert fake TPS from MAP and vice versa */
00637     TPSMAPRange = fixedConfigs2.sensorRanges.TPSOpenMAP - fixedConfigs2.sensorRanges.TPSClosedMAP;
00638 
00639     /* The ADC range used to generate TPS percentage */
00640     TPSADCRange = fixedConfigs2.sensorRanges.TPSMaximumADC - fixedConfigs2.sensorRanges.TPSMinimumADC;
00641 
00642 
00643     /* Use like flags for now, just add one for each later */
00644     unsigned char cumulativeConfigErrors = 0;
00645 
00646     /* Check various aspects of config which will cause problems */
00647 
00648     /* BRV max bigger than variable that holds it */
00649     if(((unsigned long)fixedConfigs2.sensorRanges.BRVMinimum + fixedConfigs2.sensorRanges.BRVRange) > 65535){
00650         //sendError(BRV_MAX_TOO_LARGE);
00651         cumulativeConfigErrors++;
00652     }
00653 
00654     // TODO check all critical variables here!
00655 
00656     /*
00657      * check ignition settings for range etc, possibly check some of those on the fly too
00658      * check fuel settings for being reasonable
00659      * check all variable tables for correct sizing
00660      * etc
00661      */
00662 
00663     while(cumulativeConfigErrors > 0){
00664         sleep(1000);
00665         PORTS_BA ^= ONES16; // flash leds
00666         //send("There were ");
00667         //sendUC(cumulativeConfigErrors);
00668         //send(" config errors, init aborted!");
00669     } // TODO ensure that we can recieve config and settings via serial while this is occuring! If not a bad config will lock us out all together.
00670 }

Here is the call graph for this function:

void initECTTimer (  ) 

Definition at line 513 of file init.c.

References fixedConfigs1, MCCNT, MCCTL, MCFLG, ONES, ONES16, PTMCPSR, PTPSR, fixedConfig1::tachoSettings, tachoSetting::tachoTickFactor, TCTL1, TCTL2, TCTL3, TCTL4, TFLG, TFLGOF, TIE, TIOS, TSCR1, TSCR2, and ZEROS.

Referenced by init().

00513                    {
00514 
00515     // TODO rearrange the order of this stuff and pull enable and interrupt enable out to the last function call of init.
00516 
00517 
00518 #ifndef NO_INIT
00519     /* Timer channel interrupts */
00520     TIE = 0x03; /* 0,1 IC interrupts enabled for reading engine position and RPM, 6 OC channels disabled such that no injector switching happens till scheduled */
00521     TFLG = ONES; /* Clear all the flags such that we are up and running before they first occur */
00522     TFLGOF = ONES; /* Clear all the flags such that we are up and running before they first occur */
00523 
00524     /* TODO Turn the timer on and set the rate and overflow interrupt */
00525 //  DLYCT = 0xFF; /* max noise filtering as experiment for volvo this will come from flash config */ // just hiding a wiring/circuit issue...
00526     TSCR1 = 0x88; /* 0b_1000_1000 Timer enabled, and precision timer turned on */
00527     TSCR2 = 0x87; /* 0b_1000_0111 Overflow interrupt enable, divide by 256 if precision turned off */
00528 //  PTPSR = 0x03; /* 4 prescaler gives .1uS resolution and max period of 7ms measured */
00529     PTPSR = 0x1F; /* 32 prescaler gives 0.8uS resolution and max period of 52.4288ms measured */
00530 //  PTPSR = 0x3F; /* 64 prescaler gives 1.6uS resolution and max period of 105ms measured */
00531 //  PTPSR = 0xFF; /* 256 prescaler gives 6.4uS resolution and max period of 400ms measured */
00532 //  PTPSR = 0x7F; /* 128 prescaler gives 3.2uS resolution and max period of 200ms measured */
00533     /* http://www.google.com/search?hl=en&safe=off&q=1+%2F+%2840MHz+%2F+32+%29&btnG=Search */
00534     /* http://www.google.com/search?hl=en&safe=off&q=1+%2F+%2840MHz+%2F+32+%29+*+2%5E16&btnG=Search */
00535     /* www.mecheng.adelaide.edu.au/robotics_novell/WWW_Devs/Dragon12/LM4_Timer.pdf */
00536 
00537     /* Initial actions */
00538     TIOS = 0xFC; /* 0b_1111_1100 0 and 1 are input capture, 2 through 7 are output compare */
00539     TCTL1 = ZEROS; /* Set disabled at startup time, use these and other flags to switch fueling on and off inside the decoder */
00540     TCTL2 = ZEROS; /* 0,1 have compare turned off regardless as they are in IC mode. */
00541     TCTL3 = ZEROS; /* Capture off for 4 - 7 */
00542     TCTL4 = 0x0F; /* Capture on both edges of two pins for IC (0,1), capture off for 2,3 */
00543 
00544     // TODO setup delay counters on 0 and 1 to filter noise (nice feature!)
00545     //DLYCT = ??; built in noise filter
00546 
00547     /* Configurable tachometer output */
00548     PTMCPSR = fixedConfigs1.tachoSettings.tachoTickFactor - 1; // Precision prescaler - fastest is 1 represented by 0, slowest/longest possible is 256 represented by 255 or 0xFF
00549     MCCNT = ONES16; // init to slowest possible, first
00550     MCCTL = 0xC4; // turn on and setup the mod down counter
00551     MCFLG = 0x80; // clear the flag up front
00552 #endif
00553 }

void initFlash (  ) 

Flash module setup.

Initialise configuration registers for the flash module to allow burning of non-volatile flash memory from within the firmware.

The FCLKDIV register can be written once only after reset, thus the lower seven bits and the PRDIV8 bit must be set at the same time.

We want to put the flash clock as high as possible between 150kHz and 200kHz

The oscillator clock is 16MHz and because that is above 12.8MHz we will set the PRDIV8 bit to further divide by 8 bits as per the manual.

16MHz = 16000KHz which pre-divided by 8 is 2000kHz

2000kHz / 200kHz = 10 thus we want to set the divide register to 10 or 0x0A

Combining 0x0A with PRDIV8 gives us 0x4A (0x0A | 0x40 = 0x4A) so we use that

Author:
Sean Keys
Note:
If you use a different crystal lower than 12.8MHz PRDIV8 should not be set.
Warning:
If the frequency you end up with is outside 150kHz - 200kHz you may damage your flash module or get corrupt data written to it.

Definition at line 505 of file init.c.

References ACCERR, FCLKDIV, FPROT, FSTAT, and PVIOL.

Referenced by init().

00505                 {
00506     FCLKDIV = 0x4A;                     /* Set the flash clock frequency    */
00507     FPROT = 0xFF;                       /* Disable all flash protection     */
00508     FSTAT = FSTAT | (PVIOL | ACCERR);   /* Clear any errors                 */
00509 }

void initFuelAddresses (  ) 

void initInterrupts (  ) 

Definition at line 674 of file init.c.

References CRGFLG, CRGINT, IVBR, ONES, PIEH, PIFH, PPSH, RTICTL, VREGCTRL, and ZEROS.

Referenced by init().

00674                      {
00675     /* IMPORTANT : Set the s12x vector base register (Thanks Karsten!!) */
00676     IVBR = 0xF7; /* Without this the interrupts will never find your code! */
00677 
00678     /* Set up the Real Time Interrupt */
00679     RTICTL = 0x81; /* 0b_1000_0001 0.125ms/125us period http://www.google.com/search?hl=en&safe=off&q=1+%2F+%2816MHz+%2F+%282+*+10%5E3%29+%29&btnG=Search */
00680 //  RTICTL = 0xF9; /* 0b_1111_1001 0.125s/125ms period http://www.google.com/search?hl=en&safe=off&q=1+%2F+%2816MHz+%2F+%282*10%5E6%29+%29&btnG=Search */
00681     CRGINT |= 0x80; /* Enable the RTI */
00682     CRGFLG = 0x80; /* Clear the RTI flag */
00683 
00684 #ifndef NO_INIT
00685     // set up port H for testing
00686     PPSH = ZEROS; // falling edge/pull up for all
00687     PIEH = ONES; // enable all pins interrupts
00688     PIFH = ONES; // clear all port H interrupt flags
00689 #endif
00690 
00691     // TODO set up irq and xirq for testing
00692     // IRQCR for IRQ
00693     //
00694 
00695     /* VReg API setup (only for wait mode? i think so) */
00696 //  VREGAPIR = 0x09C3; /* For 500ms period : (500ms - 0.2ms) / 0.2ms = 0b100111000011 = 2499 */
00697 //  VREGAPICL = 0x02; /* Enable the interrupt */
00698 //  VREGAPICL = 0x04; /* Start the counter running */
00699     /* Writing a one to the flag will set it if it is unset, so best not to mess with it here as it probably starts off unset */
00700 
00701     /* LVI Low Voltage Interrupt enable */
00702     VREGCTRL = 0x02; // Counts bad power events for diagnosis reasons
00703 }

void initIO (  ) 

Definition at line 112 of file init.c.

References ATD0CTL2, ATD0CTL3, ATD0CTL5, ATD0DIEN, ATD1CTL0, ATD1CTL2, ATD1CTL3, ATD1CTL5, ATD1DIEN0, ATD1DIEN1, DDRA, DDRB, DDRC, DDRD, DDRE, DDRH, DDRJ, DDRK, DDRM, DDRP, DDRS, DDRT, ONES, PORTA, PORTB, PORTE, PORTH, PORTJ, PORTK, PORTM, PORTP, PORTS, PORTT, PWMCLK, PWMDTY0, PWMDTY1, PWMDTY2, PWMDTY3, PWMDTY4, PWMDTY5, PWMDTY6, PWMDTY7, PWME, PWMPER0, PWMPER1, PWMPER2, PWMPER3, PWMPER4, PWMPER5, PWMPER6, PWMPER7, PWMPRCLK, PWMSCLA, PWMSCLB, and ZEROS.

Referenced by init().

00112              {
00113     /* for now, hard code all stuff to be outputs as per Freescale documentation,   */
00114     /* later what to do will be pulled from flash configuration such that all       */
00115     /* things are setup at once, and not messed with thereafter. when the port      */
00116     /* something uses is changed via the tuning interface, the confuration will be  */
00117     /* done on the fly, and the value burned to flash such that next boot happens   */
00118     /* correctly and current running devices are used in that way.                  */
00119 
00120     /* Turn off and on and configure all the modules in an explicit way */
00121     // TODO set up and turn off all modules (CAN,SCI,SPI,IIC,etc)
00122 
00123     /* Turn off the digital input buffers on the ATD channels */
00124     ATD0DIEN = ZEROS; /* You are out of your mind if you waste this on digital Inputs */
00125     ATD1DIEN0 = ZEROS; /* You are out of your mind if you waste this on digital Inputs (NOT-bonded, can't use) */
00126     ATD1DIEN1 = ZEROS; /* You are out of your mind if you waste this on digital Inputs */
00127     /* TODO Second half of ATD1 - can we disable this somehow */
00128 
00129     /* And configure them all for analog input */
00130     ATD0CTL2 = 0x80; /* Turns on the ADC block. */
00131     ATD0CTL3 = 0x40; /* Set sequence length = 8 */
00132     ATD0CTL5 = 0xB0; /* Sets justification to right, multiplex and scan all channels. */
00133     // TODO find out if this is the default (i suspect it is)
00134     // TODO look into sampling techniques
00135 
00136     /* And configure them all for analog input */
00137     ATD1CTL0 = 0x07; /* Sets wrap on 8th ADC because we can't use the other 8 */
00138     ATD1CTL2 = 0x80; /* Turns on the ADC block. */
00139     ATD1CTL3 = 0x40; /* Set sequence length = 8 */
00140     ATD1CTL5 = 0xB0; /* Sets justification to right, multiplex and scan all channels. */
00141     // TODO find out if this is the default (i suspect it is)
00142     // TODO look into sampling techniques
00143 
00144 #ifndef NO_INIT
00145     /* Set up the PWM component and initialise its values to off */
00146     PWME = 0x7F; /* Turn on PWM 0 - 6 (7 is user LED on main board) */
00147     PWMCLK = ZEROS; /* The fastest we can go for all channels */
00148     PWMPRCLK = ZEROS; /* The fastest prescaler we can go for all channels */
00149     PWMSCLA = ZEROS; /* The fastest we can go */
00150     PWMSCLB = ZEROS; /* The fastest we can go */
00151     /* TODO PWM channel concatenation for high resolution */
00152     // join channel pairs together here (needs 16 bit regs enabled too)
00153     /* TODO Initialise pwm channels with frequency, and initial duty for real use */
00154     // initial PWM settings for testing
00155     /* PWM periods */
00156     PWMPER0 = 0xFF; // 255 for ADC0 testing
00157     PWMPER1 = 0xFF; // 255 for ADC1 testing
00158     PWMPER2 = 0xFF; // 255 for ADC1 testing
00159     PWMPER3 = 0xFF; // 255 for ADC1 testing
00160     PWMPER4 = 0xFF; // 255 for ADC1 testing
00161     PWMPER5 = 0xFF; // 255 for ADC1 testing
00162     PWMPER6 = 0xFF; // 255 for ADC1 testing
00163     PWMPER7 = 0xFF; // 255 for ADC1 testing
00164     /* PWM duties */
00165     PWMDTY0 = 0;
00166     PWMDTY1 = 0;
00167     PWMDTY2 = 0;
00168     PWMDTY3 = 0;
00169     PWMDTY4 = 0;
00170     PWMDTY5 = 0;
00171     PWMDTY6 = 0;
00172     PWMDTY7 = 0;
00173 
00174 
00175     /* Initialise the state of pins configured as output */
00176     /* Initialise to low such that transistor grounded things are all turned off by default. */
00177     PORTA = ZEROS; /* The serial monitor pin is on 0x40, and could cause problems if capacitance at the output is large when a reset occurs. */
00178     PORTB = ZEROS; /* Init the rest of the spark outputs as off */
00179     PORTE = 0x1F; /* 0b_0001_1111 : when not in use 0b_1001_1111 PE7 should be high PE5 and PE6 should be low, the rest high */
00180     PORTK = ZEROS;
00181     PORTS = ZEROS;
00182     PORTT = ZEROS; /* All pins in off state at boot up (only matters for 2 - 7) */
00183     PORTM = ZEROS;
00184     PORTP = ZEROS; // TODO hook these up to the adc channels such that you can vary the brightness of an led with a pot.
00185     PORTH = ZEROS;
00186     PORTJ = ZEROS;
00187     /* AD0PT1 You are out of your mind if you waste this on digital Inputs */
00188     /* AD1PT1 You are out of your mind if you waste this on digital Inputs */
00189 
00190     /* Initialise the Data Direction Registers */
00191     /* To outputs based on the note at the end of chapter 1.2.2 of MC9S12XDP512V2.pdf */
00192     DDRA = ONES; /* GPIO (8) */
00193     DDRB = ONES; /* GPIO (8) */
00194     DDRE = 0xFC; /* 0b_1111_1100 : Clock and mode pins PE0,PE1 are input only pins, the rest are GPIO */
00195     DDRK = ONES; /* Only 0,1,2,3,4,5,7, NOT 6 (7) */
00196     DDRS = ONES; /* SCI0, SCI1, SPI0 (8) */
00197     DDRT = 0xFC; /* 0b_1111_1100 set ECT pins 0,1 to IC and 2:7 to OC (8) */
00198     DDRM = ONES; /* CAN 0 - 3 (8) */
00199     DDRP = ONES; /* PWM pins (8) */
00200     DDRH = ZEROS; /* All pins configured as input for misc isrs (SPI1, SPI2) (8) */
00201     DDRJ = ONES; /* Only 0,1,6,7 are brought out on the 112 pin chip (4) */
00202     /* Configure the non bonded pins to output to avoid current drain (112 pin package) */
00203     DDRC = ONES; /* NON-bonded external data bus pins */
00204     DDRD = ONES; /* NON-bonded external data bus pins */
00205     /* AD0DDR1 You are out of your mind if you waste this on digital Inputs */
00206     /* AD1DDR1 You are out of your mind if you waste this on digital Inputs */
00207 #endif
00208 }

void initLookupAddresses (  ) 

Buffer lookup tables addresses.

Save pointers to the lookup tables which live in paged flash.

Note:
Many thanks to Jean Bélanger for the inspiration/idea to do this!
Author:
Fred Cooke

Definition at line 219 of file init.c.

References CHTTransferTable, CHTTransferTableLocation, IATTransferTable, IATTransferTableLocation, MAFTransferTable, MAFTransferTableLocation, TestTransferTable, and TestTransferTableLocation.

Referenced by initAllPagedAddresses().

void initPagedRAMFuel ( void   ) 

Copy fuel tables to RAM.

Initialises the fuel tables in RAM by copying them up from flash.

Author:
Fred Cooke

Definition at line 254 of file init.c.

References LambdaTableFlash2Location, LambdaTableFlashLocation, MAINTABLE_SIZE, RPAGE, RPAGE_FUEL_ONE, RPAGE_FUEL_TWO, VETableMainFlash2Location, VETableMainFlashLocation, VETableSecondaryFlash2Location, VETableSecondaryFlashLocation, VETableTertiaryFlash2Location, and VETableTertiaryFlashLocation.

Referenced by initAllPagedRAM().

00254                            {
00255     /* Copy the tables from flash to RAM */
00256     RPAGE = RPAGE_FUEL_ONE;
00257     memcpy((void*)&TablesA, VETableMainFlashLocation,       MAINTABLE_SIZE);
00258     memcpy((void*)&TablesB, VETableSecondaryFlashLocation,  MAINTABLE_SIZE);
00259     memcpy((void*)&TablesC, VETableTertiaryFlashLocation,   MAINTABLE_SIZE);
00260     memcpy((void*)&TablesD, LambdaTableFlashLocation,       MAINTABLE_SIZE);
00261     RPAGE = RPAGE_FUEL_TWO;
00262     memcpy((void*)&TablesA, VETableMainFlash2Location,      MAINTABLE_SIZE);
00263     memcpy((void*)&TablesB, VETableSecondaryFlash2Location, MAINTABLE_SIZE);
00264     memcpy((void*)&TablesC, VETableTertiaryFlash2Location,  MAINTABLE_SIZE);
00265     memcpy((void*)&TablesD, LambdaTableFlash2Location,      MAINTABLE_SIZE);
00266 }

void initPagedRAMTime (  ) 

void initPagedRAMTune (  ) 

Definition at line 372 of file init.c.

References MAINTABLE_SIZE, RPAGE, RPAGE_TUNE_ONE, RPAGE_TUNE_TWO, SmallTablesAFlash2Location, SmallTablesAFlashLocation, SmallTablesBFlash2Location, SmallTablesBFlashLocation, SmallTablesCFlash2Location, SmallTablesCFlashLocation, SmallTablesDFlash2Location, and SmallTablesDFlashLocation.

Referenced by initAllPagedRAM().

00372                        {
00373     /* Copy the tables from flash to RAM */
00374     RPAGE = RPAGE_TUNE_ONE;
00375     memcpy((void*)&TablesA, SmallTablesAFlashLocation,  MAINTABLE_SIZE);
00376     memcpy((void*)&TablesB, SmallTablesBFlashLocation,  MAINTABLE_SIZE);
00377     memcpy((void*)&TablesC, SmallTablesCFlashLocation,  MAINTABLE_SIZE);
00378     memcpy((void*)&TablesD, SmallTablesDFlashLocation,  MAINTABLE_SIZE);
00379     RPAGE = RPAGE_TUNE_TWO;
00380     memcpy((void*)&TablesA, SmallTablesAFlash2Location, MAINTABLE_SIZE);
00381     memcpy((void*)&TablesB, SmallTablesBFlash2Location, MAINTABLE_SIZE);
00382     memcpy((void*)&TablesC, SmallTablesCFlash2Location, MAINTABLE_SIZE);
00383     memcpy((void*)&TablesD, SmallTablesDFlash2Location, MAINTABLE_SIZE);
00384 }

void initPITTimer (  ) 

Definition at line 557 of file init.c.

References PITCFLMT, PITMTLD0, and PITMTLD1.

Referenced by init().

00557                    {
00558 #ifndef NO_INIT
00559     /*  */
00560     // set micro periods
00561     PITMTLD0 = 0x1F; /* 32 prescaler gives 0.8uS resolution and max period of 52.4288ms measured */
00562     PITMTLD1 = 0x1F; /* ditto */
00563     /* http://www.google.com/search?hl=en&safe=off&q=1+%2F+%2840MHz+%2F+32+%29 Exactly the same as for ECT */
00564 
00565     // set timers running
00566 //  PITLD0 = dwellPeriod;
00567     // enable module
00568     PITCFLMT = 0x80;
00569     // enable channels
00570     //PITCE = 0x03;
00571     // enable interrupt
00572 //  PITINTE = 0x01;
00573     // clear flags
00574     //PITFLT = ONES;
00575 #endif
00576 }

void initPLL (  ) 

Set the PLL clock frequency.

Set the Phase Locked Loop to our desired frequency (80MHz) and switch to using it for clock (40MHz bus speed).

Author:
Fred Cooke

Definition at line 90 of file init.c.

References CLKSEL, CRGFLG, PLLCTL, PLLDIVISOR, PLLLOCK, PLLMULTIPLIER, PLLOFF, PLLON, PLLSELOFF, PLLSELON, REFDV, and SYNR.

Referenced by init().

00090               {
00091     CLKSEL &= PLLSELOFF;    /* Switches to base external OSCCLK to ensure PLL is not being used (off out of reset, but not sure if the monitor turns it on before passing control or not) */
00092     PLLCTL &= PLLOFF;       /* Turn the PLL device off to adjust its speed (on by default out of reset) */
00093     REFDV = PLLDIVISOR;     /* 16MHz / (3 + 1) = 4MHz Bus frequency */
00094     SYNR = PLLMULTIPLIER;   /* 4MHz * (9 + 1) = 40MHz Bus frequency */
00095     PLLCTL |= PLLON;        /* Turn the PLL device back on again at 80MHz */
00096 
00097     while (!(CRGFLG & PLLLOCK)){
00098         /* Do nothing while we wait till the PLL loop locks onto the target frequency. */
00099         /* Target frequency is given by (2 * (crystal frequency / (REFDV + 1)) * (SYNR + 1)) */
00100         /* Bus frequency is half PLL frequency and given by ((crystal frequency / (REFDV + 1)) * (SYNR + 1)) */
00101     }
00102 
00103     CLKSEL = PLLSELON;      /* Switches to PLL clock for internal bus frequency */
00104     /* from MC9S12XDP512V2.pdf Section 2.4.1.1.2 page 101 Third paragraph       */
00105     /* "This takes a MAXIMUM of 4 OSCCLK clock cylces PLUS 4 PLL clock cycles"  */
00106     /* "During this time ALL clocks freeze, and CPU activity ceases"            */
00107     /* Therefore there is no point waiting for this to occur, we already are... */
00108 }

void initSCIStuff (  ) 

Definition at line 579 of file init.c.

References serialSetting::baudDivisor, fixedConfigs1, SCI0BD, SCI0CR1, SCI0CR2, and fixedConfig1::serialSettings.

Referenced by init().

00579                    {
00580     /* The alternative register set selector defaults to zero */
00581 
00582     // set the baud/data speed
00583     SCI0BD = fixedConfigs1.serialSettings.baudDivisor;
00584 
00585     // etc
00586 
00587     /* Switch to alternative register set? */
00588 
00589     // etc
00590 
00591     /* Switch back again? */
00592 
00593     /*
00594      * 0 = LOOPS (normal two wire operation)
00595      * 0 = SCISWAI (Wait mode on)
00596      * 0 = RSRC (if loops=1, int/ext wiring)
00597      * 1 = M MODE (9 bit operation)
00598      * 0 = WAKE (idle line wakeup)
00599      * 0 = ILT (idle line type count start pos)
00600      * 1 = PE (parity on)
00601      * 1 = PT (odd parity) (minicom defaults to no parity)
00602      *
00603      * 00010011 = 0x13
00604      */
00605     SCI0CR1 = 0x13;
00606 
00607     /*
00608      * 0 = TIE (tx data empty isr disabled)
00609      * 0 = TCIE (tx complete isr disabled)
00610      * 1 = RIE (rx full isr enabled)
00611      * 0 = ILIE (idle line isr disabled)
00612      * 1 = TE (transmit enabled)
00613      * 1 = RE (receive enabled)
00614      * 0 = RWU (rx wake up normal)
00615      * 0 = SBK (send break off)
00616      *
00617      * 00101100 = 0x2C
00618      */
00619     SCI0CR2 = 0x2C;
00620 }

void initTimingAddresses (  ) 

Buffer timing tables addresses.

Save pointers to the timing tables which live in paged flash.

Note:
Many thanks to Jean Bélanger for the inspiration/idea to do this!
Author:
Fred Cooke

Definition at line 277 of file init.c.

References IgnitionAdvanceTableMainFlash, IgnitionAdvanceTableMainFlash2, IgnitionAdvanceTableMainFlash2Location, IgnitionAdvanceTableMainFlashLocation, IgnitionAdvanceTableSecondaryFlash, IgnitionAdvanceTableSecondaryFlash2, IgnitionAdvanceTableSecondaryFlash2Location, IgnitionAdvanceTableSecondaryFlashLocation, InjectionAdvanceTableMainFlash, InjectionAdvanceTableMainFlash2, InjectionAdvanceTableMainFlash2Location, InjectionAdvanceTableMainFlashLocation, InjectionAdvanceTableSecondaryFlash, InjectionAdvanceTableSecondaryFlash2, InjectionAdvanceTableSecondaryFlash2Location, and InjectionAdvanceTableSecondaryFlashLocation.

Referenced by initAllPagedAddresses().

void initTunableAddresses (  ) 

Buffer tunable tables addresses.

Save pointers to the tunable tables which live in paged flash and their sub-sections too.

Note:
Many thanks to Jean Bélanger for the inspiration/idea to do this!
Author:
Fred Cooke

Definition at line 320 of file init.c.

References SmallTables1::dwellDesiredVersusVoltageTable, dwellDesiredVersusVoltageTable2Location, dwellDesiredVersusVoltageTableLocation, SmallTables1::dwellMaxVersusRPMTable, dwellMaxVersusRPMTable2Location, dwellMaxVersusRPMTableLocation, SmallTables1::engineTempEnrichmentTableFixed, engineTempEnrichmentTableFixed2Location, engineTempEnrichmentTableFixedLocation, SmallTables1::engineTempEnrichmentTablePercent, engineTempEnrichmentTablePercent2Location, engineTempEnrichmentTablePercentLocation, SmallTables4::filler, SmallTables3::filler, SmallTables2::filler, SmallTables1::filler, fillerA2Location, fillerALocation, fillerB2Location, fillerBLocation, fillerC2Location, fillerCLocation, fillerD2Location, fillerDLocation, SmallTables1::injectorDeadTimeTable, injectorDeadTimeTable2Location, injectorDeadTimeTableLocation, SmallTables2::perCylinderFuelTrims, perCylinderFuelTrims2Location, perCylinderFuelTrimsLocation, SmallTables1::postStartEnrichmentTable, postStartEnrichmentTable2Location, postStartEnrichmentTableLocation, SmallTables1::primingVolumeTable, primingVolumeTable2Location, primingVolumeTableLocation, SmallTablesAFlash, SmallTablesAFlash2, SmallTablesAFlash2Location, SmallTablesAFlashLocation, SmallTablesBFlash, SmallTablesBFlash2, SmallTablesBFlash2Location, SmallTablesBFlashLocation, SmallTablesCFlash, SmallTablesCFlash2, SmallTablesCFlash2Location, SmallTablesCFlashLocation, SmallTablesDFlash, SmallTablesDFlash2, SmallTablesDFlash2Location, and SmallTablesDFlashLocation.

Referenced by initAllPagedAddresses().

00320                            {
00321     /* Setup addresses within the page to avoid warnings */
00322     SmallTablesAFlashLocation   = (void*)&SmallTablesAFlash;
00323     SmallTablesBFlashLocation   = (void*)&SmallTablesBFlash;
00324     SmallTablesCFlashLocation   = (void*)&SmallTablesCFlash;
00325     SmallTablesDFlashLocation   = (void*)&SmallTablesDFlash;
00326     SmallTablesAFlash2Location  = (void*)&SmallTablesAFlash2;
00327     SmallTablesBFlash2Location  = (void*)&SmallTablesBFlash2;
00328     SmallTablesCFlash2Location  = (void*)&SmallTablesCFlash2;
00329     SmallTablesDFlash2Location  = (void*)&SmallTablesDFlash2;
00330 
00331     /* TablesA */
00332     dwellDesiredVersusVoltageTableLocation    = (void*)&SmallTablesAFlash.dwellDesiredVersusVoltageTable;
00333     dwellDesiredVersusVoltageTable2Location   = (void*)&SmallTablesAFlash2.dwellDesiredVersusVoltageTable;
00334     injectorDeadTimeTableLocation             = (void*)&SmallTablesAFlash.injectorDeadTimeTable;
00335     injectorDeadTimeTable2Location            = (void*)&SmallTablesAFlash2.injectorDeadTimeTable;
00336     postStartEnrichmentTableLocation          = (void*)&SmallTablesAFlash.postStartEnrichmentTable;
00337     postStartEnrichmentTable2Location         = (void*)&SmallTablesAFlash2.postStartEnrichmentTable;
00338     engineTempEnrichmentTableFixedLocation    = (void*)&SmallTablesAFlash.engineTempEnrichmentTableFixed;
00339     engineTempEnrichmentTableFixed2Location   = (void*)&SmallTablesAFlash2.engineTempEnrichmentTableFixed;
00340     primingVolumeTableLocation                = (void*)&SmallTablesAFlash.primingVolumeTable;
00341     primingVolumeTable2Location               = (void*)&SmallTablesAFlash2.primingVolumeTable;
00342     engineTempEnrichmentTablePercentLocation  = (void*)&SmallTablesAFlash.engineTempEnrichmentTablePercent;
00343     engineTempEnrichmentTablePercent2Location = (void*)&SmallTablesAFlash2.engineTempEnrichmentTablePercent;
00344     dwellMaxVersusRPMTableLocation            = (void*)&SmallTablesAFlash.dwellMaxVersusRPMTable;
00345     dwellMaxVersusRPMTable2Location           = (void*)&SmallTablesAFlash2.dwellMaxVersusRPMTable;
00346 
00347     /* TablesB */
00348     perCylinderFuelTrimsLocation  = (void*)&SmallTablesBFlash.perCylinderFuelTrims;
00349     perCylinderFuelTrims2Location = (void*)&SmallTablesBFlash2.perCylinderFuelTrims;
00350 
00351     /* TablesC */
00352     // TODO
00353 
00354     /* TablesD */
00355     // TODO
00356 
00357     /* filler defs */
00358     fillerALocation  = (void*)&SmallTablesAFlash.filler;
00359     fillerA2Location = (void*)&SmallTablesAFlash2.filler;
00360     fillerBLocation  = (void*)&SmallTablesBFlash.filler;
00361     fillerB2Location = (void*)&SmallTablesBFlash2.filler;
00362     fillerCLocation  = (void*)&SmallTablesCFlash.filler;
00363     fillerC2Location = (void*)&SmallTablesCFlash2.filler;
00364     fillerDLocation  = (void*)&SmallTablesDFlash.filler;
00365     fillerD2Location = (void*)&SmallTablesDFlash2.filler;
00366 }

void initVariables (  ) 

Definition at line 438 of file init.c.

References ADCArrays, ADCArrays0, ADCArrays1, ADCArraysRecord, asyncADCArrays, asyncADCArrays0, asyncADCArrays1, asyncADCArraysRecord, configuredBasicDatalogLength, CoreVars, CoreVars0, currentDwell0, currentDwell1, currentDwellMath, currentDwellRealtime, DerivedVars, DerivedVars0, injectorMainControlRegisters, injectorMainPulseWidths0, injectorMainPulseWidths1, injectorMainPulseWidthsMath, injectorMainPulseWidthsRealtime, injectorMainTimeRegisters, injectorStagedPulseWidths0, injectorStagedPulseWidths1, injectorStagedPulseWidthsMath, injectorStagedPulseWidthsRealtime, ISRLatencyVars, mathSampleTimeStamp, ISRLatencyVar::mathSampleTimeStamp0, ISRLatencyVar::mathSampleTimeStamp1, mathSampleTimeStampRecord, maxBasicDatalogLength, RPM, RPM0, RPM1, RPMRecord, TC2_ADDR, TC3_ADDR, TC4_ADDR, TC5_ADDR, TC6_ADDR, TC7_ADDR, TCTL1_ADDR, and TCTL2_ADDR.

Referenced by init().

00438                     {
00439     /* And the opposite for the other halves */
00440     CoreVars = &CoreVars0;
00441     DerivedVars = &DerivedVars0;
00442     ADCArrays = &ADCArrays0;
00443     ADCArraysRecord = &ADCArrays1;
00444     asyncADCArrays = &asyncADCArrays0;
00445     asyncADCArraysRecord = &asyncADCArrays1;
00446     currentDwellMath = &currentDwell0;
00447     currentDwellRealtime = &currentDwell1;
00448 
00449     injectorMainPulseWidthsMath = injectorMainPulseWidths0;
00450     injectorMainPulseWidthsRealtime = injectorMainPulseWidths1;
00451     injectorStagedPulseWidthsMath = injectorStagedPulseWidths0;
00452     injectorStagedPulseWidthsRealtime = injectorStagedPulseWidths1;
00453 
00454     mathSampleTimeStamp = &ISRLatencyVars.mathSampleTimeStamp0; // TODO temp, remove
00455     mathSampleTimeStampRecord = &ISRLatencyVars.mathSampleTimeStamp1; // TODO temp, remove
00456     RPM = &RPM0; // TODO temp, remove
00457     RPMRecord = &RPM1; // TODO temp, remove
00458 
00459     /* Setup the pointers to the registers for fueling use, this does NOT work if done in global.c, I still don't know why. */
00460     injectorMainTimeRegisters[0] = TC2_ADDR;
00461     injectorMainTimeRegisters[1] = TC3_ADDR;
00462     injectorMainTimeRegisters[2] = TC4_ADDR;
00463     injectorMainTimeRegisters[3] = TC5_ADDR;
00464     injectorMainTimeRegisters[4] = TC6_ADDR;
00465     injectorMainTimeRegisters[5] = TC7_ADDR;
00466     injectorMainControlRegisters[0] = TCTL2_ADDR;
00467     injectorMainControlRegisters[1] = TCTL2_ADDR;
00468     injectorMainControlRegisters[2] = TCTL1_ADDR;
00469     injectorMainControlRegisters[3] = TCTL1_ADDR;
00470     injectorMainControlRegisters[4] = TCTL1_ADDR;
00471     injectorMainControlRegisters[5] = TCTL1_ADDR;
00472 
00473     configuredBasicDatalogLength = maxBasicDatalogLength;
00474 
00475     // TODO perhaps read from the ds1302 once at start up and init the values or different ones with the actual time and date then update them in RTI
00476 }


Generated on Wed May 26 03:59:56 2010 for FreeEMS by  doxygen 1.5.6