/* Wait for channel to open */ static void UserAppSM_WaitChannelOpen(void) { /* Monitor the channel status to check if channel is opened */ if(AntRadioStatus() == ANT_OPEN) { #ifdef MPG1 LedOn(GREEN); #endif /* MPG1 */ #ifdef MPG2 LedOn(GREEN0); #endif /* MPG2 */ UserApp_StateMachine = UserAppSM_ChannelOpen; } /* Check for timeout */ if( IsTimeUp(&UserApp_u32Timeout, TIMEOUT_VALUE) ) { AntCloseChannel(); #ifdef MPG1 LedOff(GREEN); LedOn(YELLOW); #endif /* MPG1 */ #ifdef MPG2 LedOn(RED0); LedOn(GREEN0); #endif /* MPG2 */ UserApp_StateMachine = UserAppSM_Idle; } } /* end UserAppSM_WaitChannelOpen() */
/* Wait for channel to close */ static void UserAppSM_WaitChannelClose(void) { /* Monitor the channel status to check if channel is closed */ if(AntRadioStatus() == ANT_CLOSED) { #ifdef MPG1 LedOff(GREEN); LedOn(YELLOW); #endif /* MPG1 */ #ifdef MPG2 LedOn(GREEN0); LedOn(RED0); #endif /* MPG2 */ UserApp_StateMachine = UserAppSM_Idle; } /* Check for timeout */ if( IsTimeUp(&UserApp_u32Timeout, TIMEOUT_VALUE) ) { #ifdef MPG1 LedOff(GREEN); LedOff(YELLOW); LedBlink(RED, LED_4HZ); #endif /* MPG1 */ #ifdef MPG2 LedBlink(RED0, LED_4HZ); LedOff(GREEN0); #endif /* MPG2 */ UserApp_StateMachine = UserAppSM_Error; } } /* end UserAppSM_WaitChannelClose() */
/* Send all configuration messages to ANT to fully assign the channel. The selected channel has already been verified unconfigured, and all required setup messages are ready to go. */ static void AntApiSM_AssignChannel(void) { static u8 u8CurrentMessageToSend = 0; static u8 u8CurrentMesssageId = 0; static bool bMessageInProgress = FALSE; /* Queue the next message if it's time */ if(bMessageInProgress == FALSE) { u8CurrentMesssageId = *(AntApi_apu8AntAssignChannel[u8CurrentMessageToSend] + BUFFER_INDEX_MESG_ID); AntQueueOutgoingMessage(AntApi_apu8AntAssignChannel[u8CurrentMessageToSend]); bMessageInProgress = TRUE; } /* Check message status */ if( u8CurrentMesssageId == G_stMessageResponse.u8MessageNumber ) { if(G_stMessageResponse.u8ResponseCode == RESPONSE_NO_ERROR) { /* Increment message pointer and check if complete */ u8CurrentMessageToSend++; if(u8CurrentMessageToSend == ANT_ASSIGN_MESSAGES) { /* Print OK message and update the channel flags */ G_au8AntMessageAssign[12] = G_stMessageResponse.u8Channel; DebugPrintf(G_au8AntMessageAssign); DebugPrintf(G_au8AntMessageOk); G_asAntChannelConfiguration[G_stMessageResponse.u8Channel].AntFlags |= _ANT_FLAGS_CHANNEL_CONFIGURED; /* Clean up and exit this state */ u8CurrentMessageToSend = 0; AntApi_StateMachine = AntApiSM_Idle; } } else { /* Report the error and return. Channel flags will remain clear for application to check. */ DebugPrintf(G_au8AntMessageAssign); DebugPrintf(G_au8AntMessageFail); AntApi_StateMachine = AntApiSM_Idle; } /* In either case, clean up the following: */ bMessageInProgress = FALSE; } /* end if( u8CurrentMesssageId == G_stMessageResponse.u8MessageNumber ) */ /* Check for timeout */ if(IsTimeUp(&AntApi_u32Timeout, ANT_ACTIVITY_TIME_COUNT) ) { /* Report the error and return. Channel flags will remain clear for application to check. */ DebugPrintf(G_au8AntMessageAssign); DebugPrintf(G_au8AntMessageFail); AntApi_StateMachine = AntApiSM_Idle; } } /* end AntApiSM_AssignChannel() */
/*-------------------------------------------------------------------------------------------------------------------- Function: CheckTimeout Description: Checks on timeout and updates the state machine if required. Requires: - State machine is running through states where timeouts are frequently checked and where the result of a timeout should be a timeout error and redirection to the error state. - u32Time_ is ms count for timeout - SD_u32Timeout is the reference time Promises: - if the timeout has occured, sets the erorr code and directs the SM to SdError state */ void CheckTimeout(u32 u32Time_) { if( IsTimeUp(&SD_u32Timeout, u32Time_) ) { SD_u8ErrorCode = SD_ERROR_TIMEOUT; SD_pfnStateMachine = SdError; } } /* end CheckTimeout() */
/*------------------------------------------------------------------------------ Function: LcdInitialize Description: Initializes the LCD task and manually sends a message to the LCD Requires: - Promises: - LCD task Setup and LCD functions can now be called */ void LcdInitialize(void) { u8 au8Commands[] = { LCD_FUNCTION_CMD, LCD_FUNCTION2_CMD, LCD_BIAS_CMD, LCD_CONTRAST_CMD, LCD_DISPLAY_SET_CMD, LCD_FOLLOWER_CMD }; /* "012345567890123456789" */ u8 au8Welcome[] = "RAZOR SAM3U2 ASCII "; /* State to Idle */ Lcd_StateMachine = LcdSM_Idle; /* Turn on LCD wait 40 ms for it to setup */ AT91C_BASE_PIOB->PIO_SODR = PB_09_LCD_RST; Lcd_u32Timer = G_u32SystemTime1ms; while( !IsTimeUp(&Lcd_u32Timer, LCD_STARTUP_DELAY) ); /* Send Control Command */ TWI0WriteByte(LCD_ADDRESS, LCD_CONTROL_COMMAND, NO_STOP); /* Send Control Commands */ TWI0WriteData(LCD_ADDRESS, NUM_CONTROL_CMD, &au8Commands[0], NO_STOP); /* Wait for 200 ms */ Lcd_u32Timer = G_u32SystemTime1ms; while( !IsTimeUp(&Lcd_u32Timer, LCD_CONTROL_COMMAND_DELAY) ); /* Send Final Command to turn it on */ TWI0WriteByte(LCD_ADDRESS, LCD_DISPLAY_CMD | LCD_DISPLAY_ON, STOP); /* Blacklight - White */ LedOn(LCD_RED); LedOn(LCD_GREEN); LedOn(LCD_BLUE); TWI0WriteByte(LCD_ADDRESS, LCD_CONTROL_DATA, NO_STOP); TWI0WriteData(LCD_ADDRESS, 20, &au8Welcome[0], STOP); Lcd_u32Timer = G_u32SystemTime1ms; G_u32ApplicationFlags |= _APPLICATION_FLAGS_LCD; } /* end LcdInitialize */
/*---------------------------------------------------------------------------------------------------------------------- Function: SspManualMode Description: Runs a transmit cycle of the SSP application to clock out a message. This function is used only during initialization. Requires: - SSP application has been initialized. Promises: - All bytes currently in the SSP Tx FIFO are sent out and thus all the expected received bytes are read into the application receive circular buffer. */ static void SspManualMode(void) { SSP_u32Flags |=_SSP_INIT_MODE; SSP_u32Timer = G_u32SystemTime1ms; while(SSP_u32Flags &_SSP_INIT_MODE) { G_SspStateMachine(); IsTimeUp(&G_u32SystemTime1ms, &SSP_u32Timer, 1, RESET_TARGET_TIMER); } } /* end SspManualMode() */
/* Kill time before checking SSP availability again */ static void SdCardWaitSSP(void) { if( IsTimeUp(&SD_u32Timeout, SD_SPI_WAIT_TIME_MS) ) { /* Make sure error light is off if we exitted through here from SD_Error */ //SD_CardStatusLed.eBlinkRate = LED_OFF; //LedRequest(&SD_CardStatusLed); SD_pfnStateMachine = SD_WaitReturnState; } } /* end SdCardWaitSSP() */
/*---------------------------------------------------------------------------------------------------------------------- Function: SspManualMode Description: Runs a transmit cycle of the SSP application to clock out a message. This function is used only during initialization. Requires: - SSP application has been initialized. Promises: - All bytes currently in the SSP Tx FIFO are sent out and thus all the expected received bytes are read into the application receive circular buffer. */ void SspManualMode(void) { /* Set up for manual mode */ SSP_u32Flags |= _SSP_MANUAL_MODE; SSP_psCurrentSsp = &SSP_Peripheral0; /* Run a full cycle of the SSP state machine so all SSP peripherals send their current message */ while(SSP_u32Flags & _SSP_MANUAL_MODE) { Ssp_pfnStateMachine(); SSP_u32Timer = G_u32SystemTime1ms; IsTimeUp(&SSP_u32Timer, 1); } } /* end SspManualMode() */
void TestSM_Idle(void) { static u8 u8Bar = 0; static u8 au8Data1[] = { 0x00 }; static u8 au8Data2[] = { 0x38, 0x39, 0x14, 0x72, 0x5E, 0x6D }; static u8 au8Data3[] = { 0x0F }; static u8 au8Data4[] = { 0x40, 0x48, 0x65, 0x6C, 0x6C, 0x6F }; if(u8Bar == 0) { AT91C_BASE_PIOB->PIO_SODR |= PB_09_LCD_RST; Request.TWIPeripheral = TWI0; Request.u8DeviceAddress = 0x3C; Request.pu8RxBufferAddress = &Test_Buffer[0]; Request.u32RxBufferSize = 20; Request.pu8RxNextByte = &Request.pu8RxBufferAddress; Control = TWIRequest(&Request); u32Timer = G_u32SystemTime1ms; u8Bar++; } if( (u8Bar == 1) && IsTimeUp(&u32Timer, 50) ) { u32Return = TWIWriteData(Control, 1, au8Data1); u32Timer = G_u32SystemTime1ms; u8Bar++; } if( (u8Bar == 2) && /*IsTimeUp(&u32Timer, 1)*/ ) { u32Return = TWIWriteData(Control, 1, &au8Data2[0]); u32Timer = G_u32SystemTime1ms; u8Bar++; }
static void SdCardWaitCommand(void) { static u8 u8Retries = SD_CMD_RETRIES; /* Check to see if the SSP peripheral has sent the command */ if( QueryMessageStatus(SD_u32CurrentMsgToken) == COMPLETE ) { /* If no response but retries left, queue another read */ if( (*SD_pu8RxBufferParser & BIT7) && (u8Retries != 0) ) { u8Retries--; SD_u32CurrentMsgToken = SspReadByte(SD_Ssp); if( !SD_u32CurrentMsgToken ) { /* We didn't get a return token, so abort */ SD_u8ErrorCode = SD_ERROR_NO_TOKEN; SD_pfnStateMachine = SdError; } AdvanceSD_pu8RxBufferParser(1); } else { /* Otherwise return now */ u8Retries = SD_CMD_RETRIES; SD_pfnStateMachine = SD_WaitReturnState; } } if( IsTimeUp(&SD_u32Timeout, SD_WAIT_TIME) ) { u8Retries = SD_CMD_RETRIES; SD_u8ErrorCode = SD_ERROR_TIMEOUT; SD_pfnStateMachine = SdError; } } /* end SdCardWaitCommand() */
/*---------------------------------------------------------------------------------------------------------------------- Function: LedInitialize Description: Initialization of LED system paramters and visual LED check. Requires: - G_u32SystemTime1ms ticking - All LEDs already initialized to LED_NORMAL_MODE mode ON Promises: - All LEDs in LED_NORMAL_MODE mode with OFF */ void LedInitialize(void) { u32 u32Timer; // u8* pu8Parser; u32 u32Buzzer1Frequency = 4000; u32 u32Buzzer2Frequency = 500; u32 u32StepSize = (u32Buzzer1Frequency - u32Buzzer2Frequency) / 20; static u8 au8LedStartupMsg[] = "LED functions ready\n\r"; #if MPGL2 /* Test code for checking LEDs */ #if 0 LedOn(RED0); LedOn(RED1); LedOn(RED2); LedOn(RED3); LedOn(BLUE0); LedOn(BLUE1); LedOn(BLUE2); LedOn(BLUE3); LedOn(GREEN0); LedOn(GREEN1); LedOn(GREEN2); LedOn(GREEN3); #endif #endif /* MPGL2 */ /* Turn all LEDs on full, then fade them out over a few seconds */ for(u8 i = 20; i > 0; i--) { #if STARTUP_SOUND /* Configure Buzzers to provide some audio during start up */ PWMAudioSetFrequency(BUZZER1, u32Buzzer1Frequency); PWMAudioOn(BUZZER1); #ifdef MPGL1 PWMAudioSetFrequency(BUZZER2, u32Buzzer2Frequency); PWMAudioOn(BUZZER2); #endif /* MPGL1 */ #endif /* STARTUP_SOUND */ /* Spend 40ms in each level of intensity */ for(u16 j = 40; j > 0; j--) { u32Timer = G_u32SystemTime1ms; while( !IsTimeUp(&u32Timer, 1) ); LedUpdate(); } /* Pause for a bit on the first iteration to show the LEDs on for little while */ if(i == 20) { while( !IsTimeUp(&u32Timer, 1500) ); } /* Set the LED intensity for the next iteration */ for(u8 j = 0; j < TOTAL_LEDS; j++) { Leds_asLedArray[j].eRate = (LedRateType)(i - 1); } /* Set the buzzer frequency for the next iteration */ u32Buzzer1Frequency -= u32StepSize; u32Buzzer2Frequency += u32StepSize; } /* Final update to set last state, hold for a short period */ LedUpdate(); while( !IsTimeUp(&u32Timer, 200) ); #if STARTUP_SOUND /* Turn off the buzzers */ PWMAudioOff(BUZZER1); #ifdef MPGL1 PWMAudioOff(BUZZER2); #endif /* MPGL1 */ #endif /* STARTUP_SOUND */ /* Exit with Leds off, NORMAL mode, and the backlight on (white) */ for(u8 i = 0; i < TOTAL_LEDS; i++) { Leds_asLedArray[0].eMode = LED_NORMAL_MODE; } #ifdef MPGL1 LedOn(LCD_RED); LedOn(LCD_GREEN); LedOn(LCD_BLUE); #endif #ifdef MPGL2 LedOn(LCD_BL); #endif /* Final setup and report that LED system is ready */ G_u32ApplicationFlags |= _APPLICATION_FLAGS_LED; DebugPrintf(au8LedStartupMsg); } /* end LedInitialize() */