Esempio n. 1
0
/******************************************************************************
 * 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
Esempio n. 2
0
/******************************************************************************
 * 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	
        }    
/******************************************************************************
 * 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:            
 *****************************************************************************/
void main(void)
{
    unsigned int i;

    //Note: If you disable MLCR (so it gets used as RA3 general purpose input)
    //and then use RA3 as the bootloader entry check I/O pin, it is recommended
    //to have some bootup delay prior to checking the RA3 pin to see if entry
    //into the bootloader or application should occur.  This is useful if there
    //is capacitance on RA3 and the pull up is weak enought that the RA3 rises
    //slower than VDD.  Without a bootup delay here, this could allow the
    //microcontroller to execute and check the I/O pin when it has not yet reached
    //the proper value.  This is also possible immediately after exiting ICSP programming
    //mode, as it may take some time for the programmer to tri-state the MCLR/RA3
    //pin and for the pull up resistor to overcome any capacitance on the pin.
    for(i = 0; i < 1000; i++);    //Make sure this delay is long enough for the sw2 pin can
                                //fully settle to the correct value based on the PCB circuit,
                                //prior to doing the I/O pin bootloader entry check.

    
    //Perform startup check of I/O pin to see if we should stay in bootloader
    //mode (ex: because the user is pressing the pushbutton on the board)
    #if defined(ENABLE_IO_PIN_CHECK_BOOTLOADER_ENTRY)   //See usb_config.h
        mInitSwitch2();
        if(sw2 == 0)
        {
            //Statup check of I/O pin detected low state.  Stay in bootloader mode.
            BootMain();
        }
    #endif
    
    //If we get to here, that means the user is not trying to enter bootloader
    //mode by I/O pin.  However, we still need to stay in bootloader mode
    //if no application firmware image is present at all, or if the last
    //erase/program/verify sequence was interrupted, causing the flash memory
    //signature word location to contain the wrong value.

    //Check to verify the flash memory has the correct signature word
    //value, indicating that the application space firmware image is intact
    //and was successfully erased/programmed/verified/signed when last
    //reprogrammed.
    PMCON1bits.CFGS = 0;
    PMADR = APP_SIGNATURE_ADDRESS;
    PMCON1bits.RD = 1;  //Initiate flash memory read operation
    Nop();              //2 Nops() required, see datasheet
    Nop();
    if((PMDATL == APP_SIGNATURE_VALUE) && (PMDATH == RETLW_OPCODE_MSB))
    {
        //The signature word was valid.  Now make one more check to ensure
        //0x900 has been programmed with a non blank value (ex: != 0x3FFF),
        //indicating that an application firmware image has been programmed
        //into the device.
        PMADR = APP_SPACE_RESET_VECTOR;
        PMCON1bits.RD = 1;  //Initiate flash memory read operation
        Nop();              //2 Nops() required, see datasheet
        Nop();
        //Check for completely blank value or not.  If the location is not
        //blank, this means we have an application firmware image programmed
        //in the device, and we should jump to it now.
        if(PMDAT != BLANK_FLASH_WORD_VALUE)
        {
            //Jump out of this bootloader firmware and into the main
            //application firmware entry point at APP_SPACE_START_ADDRESS.
            #asm
                movlp APP_SPACE_START_HI_BYTE
                goto APP_SPACE_START_LOWER_11BITS
            #endasm
        }