201 lines
5.4 KiB
C
201 lines
5.4 KiB
C
|
|
/*
|
||
|
|
* bootloader.c
|
||
|
|
*
|
||
|
|
* Created on: Oct 22, 2023
|
||
|
|
* Author: Brian.Bailey
|
||
|
|
*/
|
||
|
|
|
||
|
|
#include <Loader/bootloader.h>
|
||
|
|
#include <stdio.h>
|
||
|
|
#include <stdint.h>
|
||
|
|
#include <stdbool.h>
|
||
|
|
|
||
|
|
#include "lcd.h"
|
||
|
|
#include "Fonts/fontLibrary.h"
|
||
|
|
#include "timer.h"
|
||
|
|
#include "utils.h"
|
||
|
|
#include "keys.h"
|
||
|
|
#include "init.h"
|
||
|
|
#include "usbComms.h"
|
||
|
|
|
||
|
|
#include "fsl_power.h"
|
||
|
|
#include "virtual_com.h"
|
||
|
|
#include "sha256.h"
|
||
|
|
|
||
|
|
// #include "bootloader.h"
|
||
|
|
#include "Loader/iap_jump.h"
|
||
|
|
#include "eeprom.h"
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
/*******************************************************************************
|
||
|
|
* Definitions
|
||
|
|
******************************************************************************/
|
||
|
|
|
||
|
|
|
||
|
|
/*******************************************************************************
|
||
|
|
* Variables
|
||
|
|
******************************************************************************/
|
||
|
|
|
||
|
|
BL_DATA_t blData;
|
||
|
|
|
||
|
|
extern uint8_t tempString[40];
|
||
|
|
/*******************************************************************************
|
||
|
|
* Static Function Declarations
|
||
|
|
******************************************************************************/
|
||
|
|
|
||
|
|
|
||
|
|
/*******************************************************************************
|
||
|
|
* Static Functions
|
||
|
|
******************************************************************************/
|
||
|
|
|
||
|
|
|
||
|
|
/*******************************************************************************
|
||
|
|
* Public Functions
|
||
|
|
******************************************************************************/
|
||
|
|
|
||
|
|
void BL_Run(void)
|
||
|
|
{
|
||
|
|
Power_ON_OFF(ON); // Hold power on.
|
||
|
|
Select_Estop(ON); // Ensure output is ISOLATED from connections
|
||
|
|
SPI0_Chip_Select(CS_HIGH,E2PROM); // Set EEPROM as SPI CS
|
||
|
|
KEY_Init();
|
||
|
|
SPI_Init();
|
||
|
|
|
||
|
|
//Read bootloader EEPROM data to the blData structure
|
||
|
|
//Read the EEPROM
|
||
|
|
// blData.version = 1;
|
||
|
|
// blData.loaderRunRequest = 0;
|
||
|
|
// blData.transmitterProgrammed = 1;
|
||
|
|
#if 0 //for testing blank EEPROM
|
||
|
|
blData.version = 0xffffffff;
|
||
|
|
blData.loaderRunRequest = 0xffffffff;
|
||
|
|
blData.transmitterProgrammed = 0xffffffff;
|
||
|
|
M95512_WriteMemoryUINT32(BL_VERSION_EEPROM_ADDR, &blData.version);
|
||
|
|
M95512_WriteMemoryUINT32(BL_LOADERRUNREQUEST_EEPROM_ADDR, &blData.loaderRunRequest);
|
||
|
|
M95512_WriteMemoryUINT32(BL_PROGRAMMED_EEPROM_ADDR, &blData.transmitterProgrammed);
|
||
|
|
|
||
|
|
#else
|
||
|
|
EE_ReadMemoryUINT32(BL_VERSION_EEPROM_ADDR, &blData.version);
|
||
|
|
EE_ReadMemoryUINT32(BL_LOADERRUNREQUEST_EEPROM_ADDR, &blData.loaderRunRequest);
|
||
|
|
EE_ReadMemoryUINT32(BL_PROGRAMMED_EEPROM_ADDR, &blData.transmitterProgrammed);
|
||
|
|
|
||
|
|
|
||
|
|
if(blData.version > BL_MAX_VERSION) //Check for bootloader FLASH uninitialized. if uninitialized, this will be 0xffff
|
||
|
|
{
|
||
|
|
//Zero out Bootloader EEPROM data
|
||
|
|
blData.loaderRunRequest = 0;
|
||
|
|
blData.transmitterProgrammed = 0;
|
||
|
|
EE_WriteMemoryUINT32(BL_LOADERRUNREQUEST_EEPROM_ADDR, &blData.loaderRunRequest);
|
||
|
|
EE_WriteMemoryUINT32(BL_PROGRAMMED_EEPROM_ADDR, &blData.transmitterProgrammed);
|
||
|
|
//Set correct BL VERSION in EEPROM
|
||
|
|
blData.version = BL_VERSION;
|
||
|
|
EE_WriteMemoryUINT32(BL_VERSION_EEPROM_ADDR, &blData.version);
|
||
|
|
|
||
|
|
//TODO: Maybe need to read back,verify, and add error information
|
||
|
|
EE_ReadMemoryUINT32(BL_VERSION_EEPROM_ADDR, &blData.version);
|
||
|
|
EE_ReadMemoryUINT32(BL_LOADERRUNREQUEST_EEPROM_ADDR, &blData.loaderRunRequest);
|
||
|
|
EE_ReadMemoryUINT32(BL_PROGRAMMED_EEPROM_ADDR, &blData.transmitterProgrammed);
|
||
|
|
}
|
||
|
|
#endif
|
||
|
|
|
||
|
|
if(!blData.transmitterProgrammed) //If NOT programmed, return to run loader
|
||
|
|
{
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if(blData.loaderRunRequest) //If loaderRunRequest, return to run loader
|
||
|
|
{
|
||
|
|
//Clear the loader run request
|
||
|
|
blData.loaderRunRequest = 0;
|
||
|
|
EE_WriteMemoryUINT32(BL_LOADERRUNREQUEST_EEPROM_ADDR, &blData.loaderRunRequest);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
//if FREQ and UP keys are held, return to run the loader screen
|
||
|
|
if(KEY_GetUpKeyHeld() && KEY_GetFrequencyKeyHeld())
|
||
|
|
{
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
//Jump to the application at BL_APP_IMAGE_ADDR
|
||
|
|
IAP_JumpToApp(BL_APP_IMAGE_ADDR);
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
void BL_ShowScreen(void)
|
||
|
|
{
|
||
|
|
float32_t timer = 0.0;
|
||
|
|
|
||
|
|
//Init some things
|
||
|
|
timer_init(); //Needed for Delay_ticks
|
||
|
|
Init_Ports(); //make sure port expander outputs are in a safe state
|
||
|
|
LCD_Init();
|
||
|
|
|
||
|
|
|
||
|
|
//Update screen @ 10Hz and listen for USB data
|
||
|
|
while(1)
|
||
|
|
{
|
||
|
|
uint32_t xText = 20;
|
||
|
|
uint32_t yText = 30;
|
||
|
|
uint32_t yInc = 10;
|
||
|
|
|
||
|
|
LCD_Clear();
|
||
|
|
|
||
|
|
//Draw Text to LCD
|
||
|
|
sprintf(tempString, "TX Loader v%d", BL_VERSION);
|
||
|
|
FL_DrawString(tempString, LCD_X_MID, 0, font12Bold, LCD_DRAW_SET, FL_ALIGN_CENTER);
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
//TX Programmed
|
||
|
|
if(true == blData.transmitterProgrammed)
|
||
|
|
{
|
||
|
|
FL_DrawString("TX Programmed", xText, yText, font12Bold, LCD_DRAW_SET, FL_ALIGN_LEFT);
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
FL_DrawString("TX NOT Programmed", xText, yText, font12Bold, LCD_DRAW_SET, FL_ALIGN_LEFT);
|
||
|
|
}
|
||
|
|
yText += yInc;
|
||
|
|
yText += yInc;
|
||
|
|
|
||
|
|
FL_DrawString("Ready for Programming...", xText, yText, font12Bold, LCD_DRAW_SET, FL_ALIGN_LEFT);
|
||
|
|
yText += yInc;
|
||
|
|
|
||
|
|
//show time
|
||
|
|
timer += 0.1;
|
||
|
|
sprintf(tempString, "%0.1fs", timer);
|
||
|
|
FL_DrawString(tempString, 0, LCD_Y_MAX - FL_GetFontHeight(font12Bold), font12Bold, LCD_DRAW_SET, FL_ALIGN_LEFT);
|
||
|
|
|
||
|
|
LCD_Update();
|
||
|
|
|
||
|
|
//Power off if user presses power key
|
||
|
|
if(KEY_GetPowerKeyHeld())
|
||
|
|
{
|
||
|
|
Power_ON_OFF(OFF);
|
||
|
|
while(1); //wait for power off
|
||
|
|
}
|
||
|
|
|
||
|
|
Delay_Ticks(10); //100mS delay
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/*
|
||
|
|
* Read bootloader data from EEPROM
|
||
|
|
* Have seen issue where first EEPROM read is garbage. throw the first read away.
|
||
|
|
*/
|
||
|
|
void BL_ReadInfo(void)
|
||
|
|
{
|
||
|
|
EE_ReadMemoryUINT32(BL_VERSION_EEPROM_ADDR, &blData.version);
|
||
|
|
EE_ReadMemoryUINT32(BL_LOADERRUNREQUEST_EEPROM_ADDR, &blData.loaderRunRequest);
|
||
|
|
EE_ReadMemoryUINT32(BL_PROGRAMMED_EEPROM_ADDR, &blData.transmitterProgrammed);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|