/*
|
* Copyright (c) 2019-2023 Mauna Kea Semiconductor Holdings.
|
* All rights reserved.
|
*
|
*/
|
|
#include "mk_flash.h"
|
#include "mk_trace.h"
|
#include "mk_calib.h"
|
#include "mk_wdt.h"
|
#include "mk_clock.h"
|
#include "libc_rom.h"
|
#include "board.h"
|
#include "AppConfig.h"
|
#include "menu.h"
|
#include "aes.h"
|
static const uint32_t fw_addr = 0x04004000;
|
static const uint32_t fw_bytes = 14712;
|
|
extern const VECTOR_TABLE_Type __VECTOR_TABLE[48];
|
#define TEST_UART_POLL_MODE 0
|
#define TEST_UART_INTERUPT_MODE 1
|
#define TEST_UART_DMA_MODE 2
|
#define TEST_UART_MODE TEST_UART_DMA_MODE
|
|
volatile unsigned long time32_incr;
|
volatile unsigned long time32_reset = 0;
|
static unsigned char buff[100];
|
uint8_t key[16]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
|
|
extern int32_t Receive_Byte (uint8_t *c, uint32_t timeout);
|
void usartdata_process( uint8_t data)
|
{
|
static uint8_t state=0;
|
switch(state)
|
{
|
case 0:
|
if(data==0x55)
|
{
|
state = 1;
|
}
|
break;
|
case 1:
|
if(data==0xAA)
|
{
|
state = 2;
|
}else{
|
state = 0;
|
}
|
break;
|
case 2:
|
if(data==0x03)
|
{
|
state = 3;
|
}else{
|
state = 0;
|
}
|
break;
|
case 3:
|
if(data==0x06)
|
{
|
state = 4;
|
}else{
|
state = 0;
|
}
|
break;
|
case 4:
|
if(data==0x02)
|
{
|
state = 5;
|
}else{
|
state = 0;
|
}
|
break;
|
case 5:
|
if(data==0x64)
|
{
|
state = 6;
|
}else{
|
state = 0;
|
}
|
break;
|
case 6:
|
if(data==0x01)
|
{
|
state = 7;
|
}else{
|
state = 0;
|
}
|
break;
|
case 7:
|
if(data==0x01)
|
{
|
state = 8;
|
}else{
|
state = 0;
|
}
|
break;
|
case 8:
|
if(data==0x8E)
|
{
|
state = 9;
|
}else{
|
state = 0;
|
}
|
break;
|
case 9:
|
if(data==0xFF)
|
{
|
uint16_t tmp = 0xAAAA,result;
|
// SPIFlash_Erase(SPI_FLASH_ID_0, APP_CONFIG_IAPFLAG_MAP,2, SPI_FLASH_SECTOR_ERASE_MODE_4K); MK8000ÐÞ¸Ä
|
// SPIFlash_Write(SPI_FLASH_ID_0, APP_CONFIG_IAPFLAG_MAP, (HIDO_UINT8*)&tmp, sizeof(tmp));
|
state = 0;
|
|
}
|
break;
|
}
|
|
}
|
unsigned short int updata_byte = 0;
|
uint8_t trx_buf[10] = {0};
|
static void app_wdt_callback(void *dev, uint32_t status)
|
{
|
ASSERT(status, "WDT TIMEOUT£¬³ÌÐò¸´Î»");
|
//LOG_INFO(TRACE_MODULE_APP, "³ÌÐò¿¨ËÀ£¬¿´ÃŹ·¸´Î»");
|
}
|
struct WDT_CFG_T app_wdt_cfg = {
|
.timeout = 32768 * 30,
|
.rst_en = true,
|
.int_en = true,
|
.callback = app_wdt_callback,
|
};
|
int main(void)
|
{ uint8_t c;
|
|
//sys_tick_callback_set(systic_time_callback);
|
board_clock_run();
|
board_pins_config();
|
//board_debug_console_open(TRACE_PORT_UART0);
|
// Chip calibration
|
calib_chip();
|
// Disable watchdog timer
|
wdt_close(WDT_ID0);
|
wdt_open(WDT_ID0,&app_wdt_cfg);//30s¼ì²âι¹·
|
//LOG_INFO(TRACE_MODULE_APP, "½øÈëBootloader´®¿ÚÉý¼¶\r\n");
|
gpio_open();
|
board_configure();
|
|
struct UART_CFG_T test_uart_cfg =
|
{
|
.parity = UART_PARITY_NONE,
|
.stop = UART_STOP_BITS_1,
|
.data = UART_DATA_BITS_8,
|
.flow = UART_FLOW_CONTROL_NONE,
|
.rx_level = UART_RXFIFO_CHAR_1,
|
.tx_level = UART_TXFIFO_EMPTY,
|
.baud = BAUD_115200,
|
#if (TEST_UART_MODE == TEST_UART_POLL_MODE)
|
.dma_en = false,
|
.int_rx = false,
|
.int_tx = false,
|
#elif (TEST_UART_MODE == TEST_UART_INTERUPT_MODE)
|
.dma_en = false,
|
.int_rx = true,
|
.int_tx = true,
|
#elif (TEST_UART_MODE == TEST_UART_DMA_MODE)
|
.dma_en = true,
|
.int_rx = false,
|
.int_tx = false,
|
#endif
|
};
|
flash_open(FLASH_ID0, NULL);
|
uart_open(UART_ID1, &test_uart_cfg);
|
//uart_open(UART_ID0, &test_uart_cfg);
|
//uart_receive(UART_ID1, trx_buf, 1, uart_receive_callback);
|
SerialPutString("MK8000-BootLoader-V1.1Æô¶¯\r\n");
|
//Serial0PutString("MK8000-BootLoader-V1.0Æô¶¯\r\n");
|
while(1)
|
{
|
wdt_ping(WDT_ID0);//ι¹·
|
uint16_t tmp = 0;
|
tmp=0xAAAA;//²âÊÔÓÃ
|
//flash_erase(FLASH_ID0, APP_CONFIG_IAPFLAG_SECTOR_ADDR, FLASH_SECTOR_SIZE);
|
//flash_write_nbytes(FLASH_ID0,APP_CONFIG_IAPFLAG_MAP,(uint8_t*)&tmp,2);//²âÊÔÓÃ
|
// SPIFlash_Read(SPI_FLASH_ID_0, (HIDO_UINT8*)&tmp, APP_CONFIG_IAPFLAG_MAP, sizeof(tmp)); MK8000ÐÞ¸Ä
|
|
flash_read(FLASH_ID0, APP_CONFIG_IAPFLAG_MAP, (uint8_t*)&tmp, 2);
|
while (flash_check_busy(FLASH_ID0))
|
{
|
}
|
updata_byte = tmp;//¶Áȡָ¶¨µØÖ·µÄÖµ
|
if(updata_byte == 0xAAAA)//Åжϴ˵ØÖ·µÄÖµÊÇ·ñΪ0xAAAA,Èç¹ûÊÇÔòÉý¼¶
|
{
|
SerialPutString("´®¿ÚÉý¼¶Æô¶¯\r\n");
|
|
Main_Menu();
|
}
|
else if(updata_byte == 0xBBBB)//Åжϴ˵ØÖ·µÄÖµÊÇ·ñΪ0xBBBB,Èç¹ûÊÇÔòÍøÂçÉý¼¶
|
{
|
unsigned int i = 0;
|
unsigned int spiflash_addr = APP_CONFIG_APP_DOWN_LOAD_SPIFLASH_ADDRESS;
|
unsigned int flashdestination = APP_CONFIG_APPLICATION_ADDRESS;
|
uint32_t JumpAddress;
|
pFunction Jump_To_Application;
|
|
SerialPutString("ÍøÂçÉý¼¶Æô¶¯Æô¶¯\r\n");
|
// Flash_Init();
|
// Flash_Erase(APP_CONFIG_APPLICATION_ADDRESS, APP_CONFIG_APPLICATION_SIZE);//MK8000ÐÞ¸Ä
|
flash_erase(FLASH_ID0, APP_CONFIG_APPLICATION_ADDRESS, APP_CONFIG_APPLICATION_SIZE);//²Á³ýAPP
|
aesDecInit();
|
/* 200KB */
|
while(spiflash_addr < (APP_CONFIG_APP_DOWN_LOAD_SPIFLASH_ADDRESS + APP_CONFIG_APP_DOWN_LOAD_SPIFLASH_SIZE))
|
{
|
/* ¶Áȡһ°üÊý¾Ý */
|
//SPIFlash_Read(SPI_FLASH_ID_0, buff, spiflash_addr, sizeof(buff));MK8000ÐÞ¸Ä
|
flash_read(FLASH_ID0, spiflash_addr, buff, sizeof(buff));
|
while (flash_check_busy(FLASH_ID0))
|
{
|
}
|
spiflash_addr += sizeof(buff);
|
|
for(i = 0; i < sizeof(buff); i += 16)
|
{
|
aesDecrypt(&buff[i],key);
|
}
|
|
flash_write(FLASH_ID0, flashdestination, buff, sizeof(buff));
|
while (flash_check_busy(FLASH_ID0))
|
{
|
}
|
flashdestination += sizeof(buff);
|
// Flash_Write(flashdestination, buff, sizeof(buff));
|
// flashdestination += sizeof(buff); //MK8000ÐÞ¸Ä
|
}
|
|
/* Çå±êÖ¾ */
|
tmp = 0xFFFF;
|
flash_erase(FLASH_ID0, APP_CONFIG_IAPFLAG_MAP, 2);//²Á³ýAPPFLAG
|
flash_write(FLASH_ID0, APP_CONFIG_IAPFLAG_MAP, (uint8_t*)&tmp, sizeof(tmp));
|
delay_ms(500);
|
NVIC_SystemReset();
|
// SPIFlash_Erase(SPI_FLASH_ID_0, APP_CONFIG_IAPFLAG_MAP,2, SPI_FLASH_SECTOR_ERASE_MODE_4K);
|
// SPIFlash_Write(SPI_FLASH_ID_0, APP_CONFIG_IAPFLAG_MAP, (HIDO_UINT8*)&tmp, sizeof(tmp)); //MK8000ÐÞ¸Ä
|
|
//HAL_Delay(500);
|
//SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; MK8000 ÐÞ¸Ä
|
|
}
|
else if(updata_byte == 0xCCCC)//OTA
|
{
|
SerialPutString("OTAÉý¼¶Æô¶¯\r\n");
|
IAP_JumpTo(SRAM_BASE);
|
}
|
else //²»ÊÇÔòÌø×ªµ½APP,×¢ÒâF0ºÍÆäËûϵÁеÄÌø×ªÓÐЩ²»Ò»Ñù,Ìø×ª²¿·Ö¿É²Î¿¼Ô×Ó¸ç´úÂë
|
{
|
//Delay_ms(1000);
|
/* Test if user code is programmed starting from address "APP_CONFIG_APPLICATION_ADDRESS" */
|
//Serial0PutString("Ìø×ªAPPhehe\r\n");
|
memcpy((void *)SRAM_BASE, (void *)APP_CONFIG_APPLICATION_ADDRESS, APP_CONFIG_APPLICATION_SIZE);//sizeÌ«´óµ¼Ö¸²¸Çû·¨Ìø×ª
|
__set_MSP(*(uint32_t *)SRAM_BASE);
|
typedef void (*p_entry_t)(void);
|
((p_entry_t) (*(uint32_t *)(SRAM_BASE + 4) + SRAM_BASE))();
|
//IAP_JumpTo(SRAM_BASE);
|
}
|
/* USER CODE BEGIN 3 */
|
|
}
|
/* USER CODE END 3 */
|
|
////////////////////////////////////////////////////////////////////
|
// do something as needed such as comms with pc //
|
////////////////////////////////////////////////////////////////////
|
|
// copy application to it's link address
|
// {
|
// flash_open(FLASH_ID0, NULL);
|
//
|
// memcpy((void *)SRAM_BASE, (void *)fw_addr, fw_bytes);
|
//
|
// flash_close(FLASH_ID0);
|
// }
|
//
|
//// ////////////////////////////////////////////////////////////////////
|
//// // do something like restoring the status of registers if needed //
|
//// ////////////////////////////////////////////////////////////////////
|
//
|
// __set_MSP(*(uint32_t *)SRAM_BASE);
|
//
|
// typedef void (*p_entry_t)(void);
|
// ((p_entry_t) (*(uint32_t *)(SRAM_BASE + 4) + SRAM_BASE))();
|
}
|