1、Modifying Flash Partitions:

Bootloader:0x60000000-0x6007FFFF 512K
   APP:0x60100000-0x601FFFFF 1024K
   Backup:0x60300000-0x603FFFFF 1024K
   Download:0x60500000-0x605FFFFF 1024K
   OTAFlag:0x60700000-0x6071FFFF 128K
2、add four flash function:flash_erase,flash_write,flash_read and flash_copy
This commit is contained in:
wgzAIIT 2023-04-11 11:10:00 +08:00
parent 669ef1eb2b
commit d615793063
6 changed files with 148 additions and 12 deletions

View File

@ -22,7 +22,7 @@
MEMORY
{
flash (rx) : ORIGIN = 0x60040000, LENGTH = 0x00100000
flash (rx) : ORIGIN = 0x60100000, LENGTH = 0x00100000
sram (rwx) : ORIGIN = 0x20200000, LENGTH = 0x00080000
itcm (rwx) : ORIGIN = 0x00000000, LENGTH = 0x00020000
dtcm (rwx) : ORIGIN = 0x20000000, LENGTH = 0x00020000

View File

@ -53,8 +53,8 @@ STACK_SIZE = 0x4000;
/* Specify the memory areas */
MEMORY
{
m_interrupts (RX) : ORIGIN = 0x60040000, LENGTH = 0x00000400
m_text (RX) : ORIGIN = 0x60040400, LENGTH = 0x000FFC00
m_interrupts (RX) : ORIGIN = 0x60100000, LENGTH = 0x00000400
m_text (RX) : ORIGIN = 0x60100400, LENGTH = 0x000FFC00
m_data (RW) : ORIGIN = 0x20000000, LENGTH = 0x00020000
m_data2 (RW) : ORIGIN = 0x20200000, LENGTH = 0x00060000

View File

@ -58,7 +58,7 @@ MEMORY
/*bootloader*/
m_interrupts (RX) : ORIGIN = 0x60002000, LENGTH = 0x00000400
m_text (RX) : ORIGIN = 0x60002400, LENGTH = 0x0003DC00
m_text (RX) : ORIGIN = 0x60002400, LENGTH = 0x0007DC00
m_data (RW) : ORIGIN = 0x20000000, LENGTH = 0x00020000
m_data2 (RW) : ORIGIN = 0x20200000, LENGTH = 0x00060000

View File

@ -13,18 +13,20 @@
* @date 2023-04-03
*/
#include <stdio.h>
#include <xs_base.h>
#include "flash.h"
#include "stdio.h"
#include "xs_base.h"
/*******************************************************************************
* Definitions
******************************************************************************/
#define APP_FLASH_SIZE 0x100000 //Application package size is limited to 1M
#define FLASH_PAGE_SIZE 256 //每次写入256个字节
/*******************************************************************************
* Prototypes
******************************************************************************/
uint8_t buffer[FLASH_PAGE_SIZE]; //256 bytes buffer cache
/*******************************************************************************
* Variables
******************************************************************************/
@ -301,4 +303,134 @@ status_t FLASH_Read(uint32_t addr, const uint8_t *buf, uint32_t len)
__enable_irq();
return status;
}
/**
* @brief flash指定地址指定长度的空间
* @param start_addr:
* @param byte_cnt :
* @retval kStatus_Success
*/
status_t flash_erase(uint32_t start_addr, uint32_t byte_cnt)
{
uint32_t addr;
status_t status;
addr = start_addr;
while(addr < (byte_cnt + start_addr))
{
status = FLASH_EraseSector(addr);
if(status != kStatus_Success)
{
return status;
}
addr += FLASH_GetSectorSize();
}
return status;
}
/**
* @brief flash指定地址写入指定长度的数据
* @param start_addr:
* @param buf : buffer
* @param byte_cnt :
* @retval kStatus_Success
*/
status_t flash_write(uint32_t start_addr, uint8_t *buf, uint32_t byte_cnt)
{
return FLASH_WritePage(start_addr, buf, byte_cnt);
}
/**
* @brief flash指定开始读取一定长度的数据到缓存中
* @param start_addr:
* @param buf : buffer
* @param byte_cnt :
* @retval kStatus_Success
*/
status_t flash_read(uint32_t addr, uint8_t *buf, uint32_t len)
{
/* For FlexSPI Memory ReadBack, use IP Command instead of AXI command for security */
if((addr >= 0x60000000) && (addr < 0x70000000))
{
return FLASH_Read(addr, buf, len);
}
else
{
void* result = memcpy(buf, (void*)addr, len);
if(result == NULL)
{
return (status_t)kStatus_Fail;
}
else
{
return (status_t)kStatus_Success;
}
}
}
/**
* @brief flash地址空间之间的数据拷贝
* @param srcAddr: flash的起始地址
* @param dstAddr : flash的起始地址
* @param imageSize : flash空间大小,
* @retval kStatus_Success
*/
status_t flash_copy(uint32_t srcAddr,uint32_t dstAddr, uint32_t imageSize)
{
uint32_t PageNum, Remain, i;
status_t status;
if((srcAddr == dstAddr) || imageSize > (APP_FLASH_SIZE + 1))
{
return (status_t)kStatus_Fail;
}
status = flash_erase(dstAddr,imageSize);
if(status != kStatus_Success)
{
KPrintf("Erase flash 0x%08x failure !\r\n",dstAddr);
return status;
}
PageNum = imageSize/FLASH_PAGE_SIZE;
Remain = imageSize%FLASH_PAGE_SIZE;
for(i=0;i<PageNum;i++)
{
memset(buffer, 0, sizeof(buffer));
status = flash_read(srcAddr + i*FLASH_PAGE_SIZE, buffer, sizeof(buffer));
if(status != kStatus_Success)
{
KPrintf("Read flash 0x%08x failure !\r\n", srcAddr + i*FLASH_PAGE_SIZE);
return status;
}
status = flash_write(dstAddr+ i*FLASH_PAGE_SIZE, buffer, FLASH_PAGE_SIZE);
if(status != kStatus_Success)
{
KPrintf("Write flash 0x%08x failure !\r\n", dstAddr + i*FLASH_PAGE_SIZE);
return status;
}
}
if(Remain)
{
memset(buffer, 0, sizeof(buffer));
status = flash_read(srcAddr + i*FLASH_PAGE_SIZE, buffer, Remain);
if(status != kStatus_Success)
{
KPrintf("Read flash 0x%08x failure !\r\n", srcAddr + i*FLASH_PAGE_SIZE);
return status;
}
status = flash_write(dstAddr+ i*FLASH_PAGE_SIZE, buffer, Remain);
if(status != kStatus_Success)
{
KPrintf("Write flash 0x%08x failure !\r\n", dstAddr + i*FLASH_PAGE_SIZE);
return status;
}
}
return (status_t)kStatus_Success;
}

View File

@ -20,11 +20,11 @@
#ifdef MCUBOOT_BOOTLOADER
static void JumpToApp(void)
{
asm volatile("LDR R0, = 0x60040000");
asm volatile("LDR R0, = 0x60100000");
asm volatile("LDR R0, [R0]");
asm volatile("MOV SP, R0");
asm volatile("LDR R0, = 0x60040000+4");
asm volatile("LDR R0, = 0x60100000+4");
asm volatile("LDR R0, [R0]");
asm volatile("BX R0");
}
@ -79,8 +79,8 @@ void BootLoaderJumpApp(void)
UartConfig();
SerialPutString("BOOTLOADER START AND JUMP TO APP[0x60040000]\n");
SCB->VTOR = (uint32_t)0x60040000;
SerialPutString("BOOTLOADER START AND JUMP TO APP[0x60100000]\n");
SCB->VTOR = (uint32_t)0x60100000;
JumpToApp();
}
#endif

View File

@ -27,6 +27,10 @@ status_t FLASH_EraseSector(uint32_t addr);
status_t FLASH_Read(uint32_t addr, const uint8_t *buf, uint32_t len);
uint32_t FLASH_Test(uint32_t startAddr, uint32_t len);
uint32_t FLASH_GetProgramCmd(void);
status_t flash_erase(uint32_t start_addr, uint32_t byte_cnt);
status_t flash_write(uint32_t start_addr, uint8_t *buf, uint32_t byte_cnt);
status_t flash_read(uint32_t addr, uint8_t *buf, uint32_t len);
status_t flash_copy(uint32_t srcAddr,uint32_t dstAddr, uint32_t imageSize);
#endif