2019-08-10 15:30:49 -04:00
|
|
|
/*
|
|
|
|
|
* File: main.c
|
|
|
|
|
* Author: justin
|
|
|
|
|
*
|
|
|
|
|
* Created on August 9, 2019, 6:14 PM
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <xc.h>
|
|
|
|
|
#include "usb.h"
|
2019-08-11 13:48:04 -04:00
|
|
|
#include "bootloader.h"
|
2019-08-10 15:30:49 -04:00
|
|
|
|
2019-08-11 21:39:11 -04:00
|
|
|
/* Private prototypes */
|
|
|
|
|
void main(void);
|
|
|
|
|
void BootMain(void);
|
|
|
|
|
|
|
|
|
|
const unsigned int FlashSignatureWord __at(APP_SIGNATURE_ADDRESS) = APP_SIGNATURE_VALUE;
|
|
|
|
|
|
|
|
|
|
void main(void)
|
|
|
|
|
{
|
|
|
|
|
//BootMain();
|
|
|
|
|
/**
|
|
|
|
|
* TODO: implement hardware IO-based method for forcing entry into bootloader
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// normal operation: verify the firmware signature is valid. if it isn't enter
|
|
|
|
|
// bootloader (fw update) mode so valid firmware can be flashed
|
|
|
|
|
//DoFlashSignatureCheck:
|
|
|
|
|
//Check if the application region flash signature is valid
|
|
|
|
|
if(*(const 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(*(const unsigned int*)REMAPPED_APPLICATION_RESET_VECTOR != 0xFFFF)
|
|
|
|
|
{
|
|
|
|
|
//Go ahead and jump out of bootloader mode into the application run mode
|
|
|
|
|
#asm
|
|
|
|
|
goto REMAPPED_APPLICATION_RESET_VECTOR
|
|
|
|
|
#endasm
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//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.
|
2019-08-11 13:48:04 -04:00
|
|
|
|
2019-08-11 21:39:11 -04:00
|
|
|
BootMain();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BootMain(void) __at(0x30)
|
|
|
|
|
{
|
|
|
|
|
//Make sure interrupts are disabled for this code (could still be on,
|
|
|
|
|
//if the application firmware jumped into the bootloader via software methods)
|
|
|
|
|
INTCON = 0x00;
|
|
|
|
|
|
|
|
|
|
//Clear the stack pointer, in case the user application jumped into
|
|
|
|
|
//bootloader mode with excessive junk on the call stack
|
|
|
|
|
STKPTR = 0x00;
|
|
|
|
|
|
|
|
|
|
// End of the important parts of the C initializer. This bootloader firmware does not use
|
|
|
|
|
// any C initialized user variables (idata memory sections). Therefore, the above is all
|
|
|
|
|
// the initialization that is required.
|
|
|
|
|
|
2019-08-10 15:30:49 -04:00
|
|
|
// initialize the USB framework
|
|
|
|
|
USBDeviceInit();
|
|
|
|
|
USBDeviceAttach();
|
|
|
|
|
|
2019-08-11 21:39:11 -04:00
|
|
|
// initialize the bootloader
|
|
|
|
|
UserInit();
|
|
|
|
|
|
|
|
|
|
//Execute main loop
|
2019-08-10 15:30:49 -04:00
|
|
|
while(1)
|
|
|
|
|
{
|
2019-08-11 21:39:11 -04:00
|
|
|
//Need to call USBDeviceTasks() periodically. This function takes care of
|
|
|
|
|
//processing non-USB application related USB packets (ex: "Chapter 9"
|
|
|
|
|
//packets associated with USB enumeration)
|
2019-08-10 15:30:49 -04:00
|
|
|
USBDeviceTasks();
|
|
|
|
|
|
2019-08-11 21:39:11 -04:00
|
|
|
ProcessIO(); //This is where all the actual bootloader related data transfer/self programming takes
|
|
|
|
|
//place see ProcessIO() function in the BootPIC[xxxx].c file.
|
|
|
|
|
}//end while
|
|
|
|
|
}
|