/**************************************************************************** Module AccService.c Description This is a service for initializing and getting X, Y and Z readings from the accelerometer and calculating heading. Notes Private Definitions CPSDVSR SCR SSI_ENABLE_D01 SSI_ENABLE_D23 SSI_ENABLE ALL_SSI_PINS BW_RATE DATA_FORMAT POWER_CTL DATAX0 BYTES_TO_READ ACC_TIME SIZEOFBUFFER n MyPriority ReadBuffer[7] Initialization HeadingX HeadingY HeadingZ trash bufferX[SIZEOFBUFFER] bufferY[SIZEOFBUFFER] bufferZ[SIZEOFBUFFER] buffer_index x[n] y[n] alpha[n] Private Functions sendCommand startQuery Acc_SPI_Init readQuery AddToBufferX GetAvgX AddToBufferY GetAvgY AddToBufferZ GetAvgZ bool InitAccService(uint8_t Priority) ---------------------------------------- // post the initial transition event // start ACC_TIMER bool PostAccService(ES_Event_t ThisEvent) ------------------------------------ // post ThisEvent to AccService ES_Event_t RunAccService(ES_Event_t ThisEvent) ------------------------------- // process any events // if there is an ES_TIMEOUT event with ACC_TIMER as the parameter // initiate multi-byte read // add to ring buffer for X, Y and Z // restart ACC_TIMER // if there is an ACC_EOT event // check if the Initialization variable is true // set Initialization to false // make two garbage reads from accelerometer // otherwise // read X, Y, Z from accelerometer void Acc_EOT_ISR(void) -------------------------------------------------------- // clear interrupt // Post ACC_EOT to itself static void Acc_SPI_Init(void) ------------------------------------------------ // enable the clock to SSI3 module // enable clock to Port D // wait for the clock at SSI3 to be initialized // wait for the clock at Port D to be initialized // enable the GPIO pins PD0, PD1, PD2, PD3 as alternate function of SSI // enable PD0-PD3 as SSI pins // enable PD0-PD3 as digital output // enable PD2 as digital input // enable PD0 with a pull-up resistor // disable the SSI3 module before editing // set Tiva to master and set TXRIS interrupt to EOT // set SSI clock source as system clock // set CPSDVSR and SCR to set the SSI baud clock rate // set SSI to Mode 3 (SPO = 1, SPH = 1) // set data transfer size to 8 bits // enable NVIC // global enable // re-enable the SSI3 module static void sendCommand(uint8_t regAddr, uint8_t data) ------------------------ // send register address byte // send value to be set in register // allows for the interrupt to be triggered static void startQuery(void) -------------------------------------------------- // send first address to read from (0x32 = DATAX0) // send 6 garbage writes for multi-byte read // mask TXMIS for interrupt to be triggered static void readQuery(void) --------------------------------------------------- // read from accelerometer 7 times and save to ReadBuffer // combine DATAX0 and DATAX1 to get HeadingX // combine DATAY0 and DATAY1 to get HeadingY // combine DATAZ0 and DATAZ1 to get HeadingZ uint16_t getHeading(void) ----------------------------------------------------- // initialize return value to 0 // initialize previous norm to random max value // calculate average X and average Y (must be called after waiting 1.2 sec) // calculate difference between average and current x value 24 times // compute norm of differences in x and y // if norm is less than previous norm, set heading to alpha for these x and y // set previous norm to norm void AddToBufferX(int8_t NewItem) --------------------------------------------- // add NewItem to bufferX // increment buffer_index // if buffer_index reaches SIZEOFBUFFER // set buffer_index to 0 int16_t GetAvgX(void) --------------------------------------------------------- // set sum to 0 // go through all values in bufferX and add to sum // calculate mean - divide sum by SIZEOFBUFFER void AddToBufferY(int8_t NewItem) --------------------------------------------- // add NewItem to bufferY // increment buffer_index // if buffer_index reaches SIZEOFBUFFER // set buffer_index to 0 int16_t GetAvgY(void) --------------------------------------------------------- // set sum to 0 // go through all values in bufferY and add to sum // calculate mean - divide sum by SIZEOFBUFFER void AddToBufferZ(int8_t NewItem) --------------------------------------------- // add NewItem to bufferZ // increment buffer_index // if buffer_index reaches SIZEOFBUFFER // set buffer_index to 0 int16_t GetAvgZ(void) --------------------------------------------------------- // set sum to 0 // go through all values in bufferZ and add to sum // calculate mean - divide sum by SIZEOFBUFFER