/****************************************************************************** * Function: void main(void) * * PreCondition: None * * Input: None * * Output: None * * Side Effects: None * * Overview: This is the first code that executes during boot up of * the microcontroller. This code checks to see if execution * should stay in the "bootloader" mode, or if it should jump * into the "application" (non-bootloder) execution mode. * No other unrelated code should be added to this function. * * Note: THIS FUNCTION EXECUTES PRIOR TO INITIALIZATION OF THE C * STACK. NO C INITIALIZATION OF STATIC VARIABLES OR RESOURCES * WILL OCCUR, PRIOR TO EXECUTING THIS FUNCTION. THEREFORE, * THE CODE IN THIS FUNCTION MUST NOT CALL OTHER FUNCTIONS OR * PERFORM ANY OPERATIONS THAT WILL REQUIRE C INITIALIZED * BEHAVIOR. *****************************************************************************/ void main(void) { //Assuming the I/O pin check entry method is enabled, check the I/O pin value //to see if we should stay in bootloader mode, or jump to normal applicaiton //execution mode. #ifdef ENABLE_IO_PIN_CHECK_BOOTLOADER_ENTRY //Need to make sure the I/O pin is configured for digital mode so we //can sense the digital level on the input pin. mInitSwitch2(); //Check Bootload Mode Entry Condition from the I/O pin (ex: place a //pushbutton and pull up resistor on the pin) if(sw2 == 1) { //If we get to here, the user is not pressing the pushbutton. We //should default to jumping into application run mode in this case. //Restore default "reset" value of registers we may have modified temporarily. mDeInitSwitch2(); //Before going to application image however, make sure the image //is properly signed and is intact. goto DoFlashSignatureCheck; } else { //User is pressing the pushbutton. We should stay in bootloader mode BootMain(); } #endif //#ifdef ENABLE_IO_PIN_CHECK_BOOTLOADER_ENTRY DoFlashSignatureCheck: //Check if the application region flash signature is valid if(*(ROM unsigned int*)APP_SIGNATURE_ADDRESS == APP_SIGNATURE_VALUE) { //The flash signature was valid, implying the previous //erase/program/verify operation was a success. //Also make sure the first WORD of program memory in the app space //is not blank, meaning there is an application image programmed into the device. if(*(ROM unsigned int*)REMAPPED_APPLICATION_RESET_VECTOR != 0xFFFF) { //Go ahead and jump out of bootloader mode into the application run mode #ifdef __XC8__ #asm goto REMAPPED_APPLICATION_RESET_VECTOR #endasm #else //Must be C18 instead _asm goto REMAPPED_APPLICATION_RESET_VECTOR _endasm #endif } } //else the application image is missing or corrupt. In this case, we //need to stay in the bootloader mode, so the user has the ability to //try (again) to re-program a valid application image into the device. //We should stay in bootloader mode BootMain(); }//end UninitializedMain
/****************************************************************************** * Function: void UninitializedMain(void) * * PreCondition: None * * Input: None * * Output: None * * Side Effects: None * * Overview: This is the first code that executes during boot up of * the microcontroller. This code checks to see if execution * should stay in the "bootloader" mode, or if it should jump * into the "application" (non-bootloder) execution mode. * No other unrelated code should be added to this function. * * Note: THIS FUNCTION EXECUTES PRIOR TO INITIALIZATION OF THE C * STACK. NO C INITIALIZATION OF STATIC VARIABLES OR RESOURCES * WILL OCCUR, PRIOR TO EXECUTING THIS FUNCTION. THEREFORE, * THE CODE IN THIS FUNCTION MUST NOT CALL OTHER FUNCTIONS OR * PERFORM ANY OPERATIONS THAT WILL REQUIRE C INITIALIZED * BEHAVIOR. *****************************************************************************/ void UninitializedMain(void) { //Assuming the I/O pin check entry method is enabled, check the I/O pin value //to see if we should stay in bootloader mode, or jump to normal applicaiton //execution mode. #ifdef ENABLE_IO_PIN_CHECK_BOOTLOADER_ENTRY //Need to make sure the I/O pin is configured for digital mode so we //can sense the digital level on the input pin. mInitSwitch2(); //Check Bootload Mode Entry Condition from the I/O pin (ex: place a //pushbutton and pull up resistor on the pin) if(sw2 == 1) { //If we get to here, the user is not pressing the pushbutton. We //should default to jumping into application run mode in this case. //Restore default "reset" value of registers we may have modified temporarily. mDeInitSwitch2(); //Before going to application image however, make sure the image //is properly signed and is intact. goto DoFlashSignatureCheck; } else { //User is pressing the pushbutton. We should stay in bootloader mode _asm goto BootMain _endasm } #endif //#ifdef ENABLE_IO_PIN_CHECK_BOOTLOADER_ENTRY DoFlashSignatureCheck: //Check if the application region flash signature is valid #ifdef ENABLE_FLASH_SIGNATURE_VERIFICATION if(*(rom unsigned int*)APP_SIGNATURE_ADDRESS == APP_SIGNATURE_VALUE) { //The flash signature was valid, implying the previous //erase/program/verify operation was a success. //Go ahead and jump out of bootloader mode into the application run mode _asm goto REMAPPED_APPLICATION_RESET_VECTOR _endasm }