初始提交
This commit is contained in:
@@ -0,0 +1,304 @@
|
||||
#include "main.h"
|
||||
#include "cmsis_os.h"
|
||||
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "string.h"
|
||||
#include "stm32h7xx_hal.h"
|
||||
#include "stm32h7xx_it.h"
|
||||
|
||||
|
||||
#include "W25QXX/w25qxx.h"
|
||||
#include "Param/param.h"
|
||||
|
||||
#include "IAP/iap.h"
|
||||
#include "CPY_PROT/cpy_prot.h"
|
||||
|
||||
typedef enum {
|
||||
IAP_CMD_ERASE = 0x01,
|
||||
IAP_CMD_WRITE,
|
||||
IAP_CMD_CHECK,
|
||||
} IAP_CMD_DEF;
|
||||
|
||||
#define MAX_PACKET_PARAM_NUM 4
|
||||
|
||||
#pragma pack(1)
|
||||
|
||||
typedef struct __iapPacketDef {
|
||||
uint8_t ucCmd;
|
||||
uint32_t aulParams[MAX_PACKET_PARAM_NUM];
|
||||
uint8_t data[];
|
||||
} iapPacketDef;
|
||||
|
||||
#pragma pack()
|
||||
|
||||
static int init_ok = 0;
|
||||
|
||||
|
||||
uint16_t getBoardTemp(void);
|
||||
|
||||
void systemReboot(void)
|
||||
{
|
||||
__disable_irq();
|
||||
HAL_NVIC_SystemReset();
|
||||
while (1) {
|
||||
__asm("nop");
|
||||
}
|
||||
}
|
||||
|
||||
#define APPLICATION_ADDRESS 0x30000000
|
||||
|
||||
void copyFirmware(void)
|
||||
{
|
||||
uint32_t ulDstAddr = APPLICATION_ADDRESS;
|
||||
uint32_t ulSrcAddr = IAP_START_SECTOR * 4096;
|
||||
uint32_t ullen = 128 * 1024;
|
||||
|
||||
while (ullen >= 1024) {
|
||||
W25QXX_Read((uint8_t *)ulDstAddr, ulSrcAddr, 1024);
|
||||
ulDstAddr += 1024;
|
||||
ulSrcAddr += 1024;
|
||||
ullen -= 1024;
|
||||
}
|
||||
|
||||
if (ullen > 0) {
|
||||
W25QXX_Read((uint8_t *)ulDstAddr, ulSrcAddr, ullen);
|
||||
}
|
||||
ulDstAddr = APPLICATION_ADDRESS;
|
||||
printf("0x%08X \n", ((uint32_t *)ulDstAddr)[0]);
|
||||
}
|
||||
|
||||
void bootJump(void)
|
||||
{
|
||||
static uint32_t JumpAddress;
|
||||
typedef void (*pFunction)(void);
|
||||
static pFunction JumpToApplication;
|
||||
printf("disable irq\n");
|
||||
__disable_irq();
|
||||
SysTick->CTRL = 0x00;
|
||||
|
||||
/* execute the new program */
|
||||
JumpAddress = *(__IO uint32_t*) (0x08000000 + 4);
|
||||
/* Jump to user application */
|
||||
JumpToApplication = (pFunction) JumpAddress;
|
||||
/* Initialize user application's Stack Pointer */
|
||||
printf("set msp\n");
|
||||
__set_MSP(*(__IO uint32_t*) 0x08000000);
|
||||
__set_PSP(*(__IO uint32_t*) 0x08000000);
|
||||
printf("jump\n");
|
||||
JumpToApplication();
|
||||
}
|
||||
|
||||
void appJump(void)
|
||||
{
|
||||
|
||||
printf("jumpto addr : %0X \r\n",*(__IO uint32_t*) (APPLICATION_ADDRESS + 4));
|
||||
|
||||
__disable_irq();
|
||||
|
||||
HAL_DeInit();
|
||||
|
||||
SysTick->CTRL = 0x00;
|
||||
|
||||
/* execute the new program */
|
||||
static uint32_t JumpAddress = 0;
|
||||
JumpAddress = *(__IO uint32_t*) (APPLICATION_ADDRESS + 4);
|
||||
/* Jump to user application */
|
||||
typedef void (*pFunction)(void);
|
||||
static pFunction JumpToApplication;
|
||||
JumpToApplication = (pFunction) JumpAddress;
|
||||
/* Initialize user application's Stack Pointer */
|
||||
__set_PSP(*(volatile unsigned int*) APPLICATION_ADDRESS);
|
||||
__set_CONTROL(0);
|
||||
__set_MSP(*(__IO uint32_t*) APPLICATION_ADDRESS);
|
||||
JumpToApplication();
|
||||
}
|
||||
|
||||
int checkApp(void)
|
||||
{
|
||||
uint32_t addr;
|
||||
|
||||
copyFirmware();
|
||||
|
||||
addr = *(__IO uint32_t*) (APPLICATION_ADDRESS);
|
||||
if ((addr > 0x24000000) && (addr < (0x24000000 + 0x80000))) {
|
||||
printf("MSP addr : %0X \r\n",addr);
|
||||
|
||||
} else {
|
||||
printf("MSP addr : %0X \r\n",addr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
addr = *(__IO uint32_t*) (APPLICATION_ADDRESS + 4);
|
||||
if ((addr > 0x30000000) && (addr < (0x30000000 + 0x20000))) {
|
||||
printf("Reset addr : %0X \r\n",addr);
|
||||
} else {
|
||||
printf("Reset addr : %0X \r\n",addr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int FLASH_If_Erase(uint32_t size)
|
||||
{
|
||||
int start_sector = IAP_START_SECTOR;
|
||||
int num = size / 4096;
|
||||
if (size % 4096) {
|
||||
num++;
|
||||
}
|
||||
for (int i = 0; i < num; ++i) {
|
||||
W25QXX_Erase_Sector((start_sector + i) * 4096);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int FLASH_If_verify(uint32_t addr, uint8_t *pucSrc, uint32_t ulLen)
|
||||
{
|
||||
static uint8_t aucbuf[1024];
|
||||
int start_sector = IAP_START_SECTOR;
|
||||
|
||||
addr = addr + start_sector * 4096;
|
||||
|
||||
while (ulLen >= 256) {
|
||||
W25QXX_Read(aucbuf, addr, 256);
|
||||
if (memcmp(aucbuf, pucSrc, 256) != 0) {
|
||||
printf("verify failed at 0x%08X\n", addr);
|
||||
return -1;
|
||||
}
|
||||
pucSrc += 256;
|
||||
addr += 256;
|
||||
ulLen -= 256;
|
||||
}
|
||||
|
||||
if (ulLen > 0) {
|
||||
W25QXX_Read(aucbuf, addr, ulLen);
|
||||
if (memcmp(aucbuf, pucSrc, ulLen) != 0) {
|
||||
printf("verify failed at 0x%08X\n", addr);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int FLASH_If_Write(uint32_t addr, uint8_t *pucSrc, uint32_t ulLen)
|
||||
{
|
||||
int start_sector = IAP_START_SECTOR;
|
||||
|
||||
addr = addr + start_sector * 4096;
|
||||
|
||||
while (ulLen >= 256) {
|
||||
W25QXX_Page_Program(pucSrc, addr, 256);
|
||||
pucSrc += 256;
|
||||
addr += 256;
|
||||
ulLen -= 256;
|
||||
}
|
||||
|
||||
if (ulLen > 0) {
|
||||
W25QXX_Page_Program(pucSrc, addr, ulLen);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int iapPacketHandler(const uint8_t *packet)
|
||||
{
|
||||
iapPacketDef *pkt = (iapPacketDef *)packet;
|
||||
switch (pkt->ucCmd) {
|
||||
case IAP_CMD_ERASE:
|
||||
{
|
||||
uint32_t len = pkt->aulParams[0];
|
||||
if (len > 0) {
|
||||
FLASH_If_Erase(len);
|
||||
char *str = "erase success";
|
||||
daq_send_packet( CPY_PROT_CMD_IAP | CPY_PROT_CMD_ACK, (uint8_t *)str, strlen(str));
|
||||
} else {
|
||||
char *str = "erase failed";
|
||||
daq_send_packet( CPY_PROT_CMD_IAP | CPY_PROT_CMD_ERROR, (uint8_t *)str, strlen(str));
|
||||
}
|
||||
}
|
||||
break;
|
||||
case IAP_CMD_WRITE:
|
||||
{
|
||||
uint32_t addr = pkt->aulParams[0];
|
||||
uint32_t len = pkt->aulParams[1];
|
||||
int ret = FLASH_If_Write(addr, pkt->data, len);
|
||||
ret = FLASH_If_verify(addr, pkt->data, len);
|
||||
if (ret == 0) {
|
||||
printf("write success at %d\n", addr);
|
||||
char *str = "write success";
|
||||
daq_send_packet( CPY_PROT_CMD_IAP | CPY_PROT_CMD_ACK, (uint8_t *)str, strlen(str));
|
||||
} else {
|
||||
printf("write failed at %d\n", addr);
|
||||
char *str = "write failed";
|
||||
daq_send_packet( CPY_PROT_CMD_IAP | CPY_PROT_CMD_ERROR, (uint8_t *)str, strlen(str));
|
||||
}
|
||||
}
|
||||
break;
|
||||
case IAP_CMD_CHECK:
|
||||
{
|
||||
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef BOOTLOADER
|
||||
static int bootBlockingFlag = 0;
|
||||
void blockInBootloader(int block)
|
||||
{
|
||||
if (block) {
|
||||
bootBlockingFlag = 1;
|
||||
} else {
|
||||
bootBlockingFlag = 0;
|
||||
}
|
||||
|
||||
static uint32_t val;
|
||||
W25QXX_Read((uint8_t *)&val, IAP_START_SECTOR * 4096, 4);
|
||||
printf("0x%08X \n", val);
|
||||
}
|
||||
#endif
|
||||
|
||||
int giIapThreadStatus = 0;
|
||||
|
||||
static void iapThread(void *arg)
|
||||
{
|
||||
|
||||
printf("iapThread running ... \r\n");
|
||||
|
||||
osDelay(3000);
|
||||
|
||||
#ifdef BOOTLOADER
|
||||
while(1) {
|
||||
if (bootBlockingFlag == 0) {
|
||||
if (checkApp()) {
|
||||
appJump();
|
||||
}
|
||||
}
|
||||
osDelay(1000);
|
||||
}
|
||||
#else
|
||||
while(1) {
|
||||
osDelay(1000);
|
||||
}
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
void iapThreadInit(void)
|
||||
{
|
||||
const osThreadAttr_t Task_attributes = {
|
||||
.name = "iap_thread",
|
||||
.stack_size = 4096 * 4,
|
||||
.priority = (osPriority_t) osPriorityNormal,
|
||||
};
|
||||
if (osThreadNew(iapThread, NULL, &Task_attributes) == NULL)
|
||||
{
|
||||
printf("failed to create iap_thread\n");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#ifndef IAP_H
|
||||
#define IAP_H
|
||||
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
/* Exported types ------------------------------------------------------------*/
|
||||
|
||||
#define IAP_START_SECTOR 16
|
||||
|
||||
void systemReboot(void);
|
||||
int iapPacketHandler(const uint8_t *packet);
|
||||
void blockInBootloader(int block);
|
||||
void iapThreadInit(void);
|
||||
|
||||
|
||||
#endif // IAP_H
|
||||
Reference in New Issue
Block a user