/**************************************************************************** Module SPIService.c Revision 1.0.1 Description This is a template file for implementing a simple service under the Gen2 Events and Services Framework. Notes History When Who What/Why -------------- --- -------- 01/16/12 09:58 jec began conversion from TemplateFSM.c ****************************************************************************/ /*----------------------------- Include Files -----------------------------*/ /* include header files for this state machine as well as any machines at the next lower level in the hierarchy that are sub-machines to this machine */ #include "ES_Configure.h" #include "ES_Framework.h" #include "SPIProjectService.h" #include "TopHSM.h" #include #include /*----------------------------- Module Defines ----------------------------*/ #define QueryTime 50 /*---------------------------- Module Functions ---------------------------*/ /* prototypes for private functions for this service.They should be functions relevant to the behavior of this service */ void interrupt _Vec_spi SPIResponse(void); /*---------------------------- Module Variables ---------------------------*/ // with the introduction of Gen2, we need a module level Priority variable static uint8_t MyPriority; static unsigned char commandNumber; static unsigned char commandType; static bool reloadStatus; static bool gameOverDeclared; static bool DarkNight; static unsigned char myScore; static unsigned char theirScore; static unsigned char matchStatus; static unsigned char sequence[4]; /*------------------------------ Module Code ------------------------------*/ bool InitSPIService ( uint8_t Priority ) { ES_Event ThisEvent; unsigned char dummy; MyPriority = Priority; printf("InitSPI"); SPIBR = 0b11111111; // BaudRate Divisor = 3 * 2^3 = 24 SPICR1 |= (_S12_CPHA | _S12_CPOL); //Initialize System With Master Select SPICR1 |= (_S12_MSTR | _S12_SPE); //Mode 3 and Slave Select Output Enable DDRS |= BIT7HI; if (SPISR & _S12_SPIF) { dummy = SPIDR; } SPICR1 |= (_S12_SPIE); //Both Interrupt Enables EnableInterrupts; commandType = 0x3F; commandNumber = 0; myScore = 0; theirScore = 0; gameOverDeclared = false; reloadStatus = false; matchStatus = 0x00; ES_Timer_InitTimer(SPI_TIMER, QueryTime); ThisEvent.EventType = ES_INIT; if (ES_PostToService( MyPriority, ThisEvent) == true) { return true; }else { return false; } } void interrupt _Vec_spi SPIResponse(void) { static unsigned char pastMotorState = 0xFF; static unsigned char currentReading; ES_Event SPIEvent; if (SPISR & _S12_SPIF) { currentReading = SPIDR; sequence[commandNumber] = currentReading; if ((commandType == 0x3f) && (commandNumber == 3) ) { if ( ( ((currentReading & BIT7HI) || (currentReading & BIT3HI) ) && currentReading!= 0xFF) && (gameOverDeclared == false)){ printf("Test1"); SPIEvent.EventType = ES_GAME_OVER; gameOverDeclared = true; PostMasterSM(SPIEvent); } if ( DarkNight && (currentReading & BIT6HI) ) { //printf("Dark Knight Reload Ready"); reloadStatus = true; } else if ((~DarkNight) && (currentReading & BIT4HI) ) { reloadStatus = true; //printf("Red Knight Reload Ready"); } else { reloadStatus = false; } currentReading = currentReading & 0b00000111; sequence[commandNumber] = currentReading; if (currentReading != pastMotorState) { if ((currentReading == 0x05) && (gameOverDeclared == false)) { gameOverDeclared = true; SPIEvent.EventType = ES_GAME_OVER; printf("Test2"); } else { SPIEvent.EventType = ES_CHANGE_STATUS; SPIEvent.EventParam = currentReading; } PostMasterSM(SPIEvent); printf("Send Command"); pastMotorState = currentReading; } } else if ((commandType == 0xC3) && (commandNumber ==2) ) { if (~DarkNight) { myScore = currentReading; } else { theirScore = currentReading; } } else if ((commandType == 0xC3) && (commandNumber ==3) ) { if (DarkNight) { myScore = currentReading; } else { theirScore = currentReading; } } if (commandNumber == 3) { PTS |= BIT7HI; commandNumber = 0; //printf("%d: %d,%d,%d,%d\n", commandType, sequence[0], sequence[1],sequence[2],sequence[3]); if (commandType == 0x3F) { commandType = 0xC3; } else { commandType = 0x3F; } } else { if (SPISR & _S12_SPTEF) { SPIDR = 0x00; commandNumber++; } } } } bool PostSPIService( ES_Event ThisEvent ) { return ES_PostToService( MyPriority, ThisEvent); } ES_Event RunSPIService( ES_Event ThisEvent ) { ES_Event ReturnEvent; ReturnEvent.EventType = ES_NO_EVENT; // assume no errors DarkNight = DarkNightPlayer(); //Indicates to SPI Which Player You are if (ThisEvent.EventType == ES_TIMEOUT) { ES_Timer_InitTimer(SPI_TIMER, QueryTime); if (SPISR & _S12_SPTEF) { PTS &= BIT7LO; SPIDR = commandType; } commandNumber = 0; } return ReturnEvent; } unsigned char QueryMyScore(void) { return myScore; } unsigned char QueryTheirScore(void) { return theirScore; } bool Ready2Reload(void) { return reloadStatus; } /*------------------------------- Footnotes -------------------------------*/ /*------------------------------ End of file ------------------------------*/