Files
bia_microddr_fw/bootloader/src/main.c

83 lines
2.8 KiB
C
Raw Normal View History

/*
* File: main.c
* Author: justin
*
* Created on August 9, 2019, 6:14 PM
*/
#include <xc.h>
#include "usb.h"
#include "bootloader.h"
/* 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.
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.
// initialize the USB framework
USBDeviceInit();
USBDeviceAttach();
// initialize the bootloader
UserInit();
//Execute main loop
while(1)
{
//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)
USBDeviceTasks();
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
}