如何为STM32L475板交换FLASH内存中的两个区域?
How to swap two regions in FLASH memory for STM32L475 board?
我正在研究 STM32L475 物联网套件,这是一款 ARM M4 Cortex 设备。我想交换闪存的两个区域。我正在使用的电路板有两个闪存存储区,每个存储区的大小为 512KB.So 我有 1 MB 闪存。我读到要交换闪存的内容,你必须先解锁它,然后擦除它然后写入它并在操作结束后锁定闪存。
还有一个限制,一次只能复制2KB的内存,定义为一页。因此只能逐页复制内存。对于我的应用程序,如果满足某些条件,我必须交换存储在闪存中的应用程序 1 和 2。虽然这两个应用程序都被分配了 384 KB 的内存,但它们实际上使用的内存都比这少(例如 264 KB)。
我尝试按照上述步骤操作,但它不起作用。这是我试过的代码:-
#define APPLICATION_ADDRESS 0x0800F000
#define APPLICATION2_ADDRESS 0x0806F800
#define SWAP_ADDRESS 0x0806F000
boolean swap(void)
{
char *app_1=( char*) APPLICATION_ADDRESS;//points to the 1st address of application1
char *app_2=(char*) APPLICATION2_ADDRESS;//points to the 1st address of application2
int mem1 = getMemorySize((unsigned char*)APPLICATION_ADDRESS);//returns the number of bytes in Application1
int mem2 = getMemorySize((unsigned char*)APPLICATION2_ADDRESS);//returns the number of bytes in Application2
int limit;
if(mem1>mem2)
limit= mem1;
else
limit= mem2;
Unlock_FLASH();
int lm = limit/2048;
for(int i=1; i<=lm; i++,app_1+=2048,app_2+=2048)
{
int *swap = (int *)SWAP_ADDRESS;
Erase_FLASH(swap);
Write_FLASH(app_1, swap);
Erase_FLASH(app_1);
Write_FLASH(app_2, app_1);
Erase_FLASH(app_2);
Write_FLASH(swap, app_2);
}
Lock_FLASH();
return TRUE;
}
void Unlock_FLASH(void)
{
while ((FLASH->SR & FLASH_SR_BSY) != 0 );
// Check if the controller is unlocked already
if ((FLASH->CR & FLASH_CR_LOCK) != 0 ){
// Write the first key
FLASH->KEYR = FLASH_FKEY1;
// Write the second key
FLASH->KEYR = FLASH_FKEY2;
}
}
void Erase_FLASH(int *c)
{
FLASH->CR |= FLASH_CR_PER; // Page erase operation
FLASH->ACR = c; // Set the address to the page to be written
FLASH->CR |= FLASH_CR_STRT;// Start the page erase
// Wait until page erase is done
while ((FLASH->SR & FLASH_SR_BSY) != 0);
// If the end of operation bit is set...
if ((FLASH->SR & FLASH_SR_EOP) != 0){
// Clear it, the operation was successful
FLASH->SR |= FLASH_SR_EOP;
}
//Otherwise there was an error
else{
// Manage the error cases
}
// Get out of page erase mode
FLASH->CR &= ~FLASH_CR_PER;
}
void Write_FLASH(int *a, int *b)
{
for(int i=1;i<=2048;i++,a++,b++)
{
FLASH->CR |= FLASH_CR_PG; // Programing mode
*(__IO uint16_t*)(b) = *a; // Write data
// Wait until the end of the operation
while ((FLASH->SR & FLASH_SR_BSY) != 0);
// If the end of operation bit is set...
if ((FLASH->SR & FLASH_SR_EOP) != 0){
// Clear it, the operation was successful
FLASH->SR |= FLASH_SR_EOP;
}
//Otherwise there was an error
else{
// Manage the error cases
}
}
FLASH->CR &= ~FLASH_CR_PG;
}
void Lock_FLASH(void)
{
FLASH->CR |= FLASH_CR_LOCK;
}
这里swap buffer是用来临时存储每页(2KB)作为交换时的缓冲区。此外,变量 limit 存储应用程序 1 和 2 的最大大小,以便在如前所述的内存大小不相等的情况下交换时不会出现错误。所以基本上我是一页一页地交换,一次只有 2 KB。
谁能找出代码中的错误?
谢谢,
舍图
2K 是 2048 字节,而不是 2024。修复整个代码中的增量。
There is another restriction that at a time only 2KB of memory can be copied
还有一个,这些内存块必须对齐到 2KB。
这个地址
#define APPLICATION2_ADDRESS 0x08076400
未正确对齐,它的值应能被 2048 (0x800) 整除。
我正在研究 STM32L475 物联网套件,这是一款 ARM M4 Cortex 设备。我想交换闪存的两个区域。我正在使用的电路板有两个闪存存储区,每个存储区的大小为 512KB.So 我有 1 MB 闪存。我读到要交换闪存的内容,你必须先解锁它,然后擦除它然后写入它并在操作结束后锁定闪存。
还有一个限制,一次只能复制2KB的内存,定义为一页。因此只能逐页复制内存。对于我的应用程序,如果满足某些条件,我必须交换存储在闪存中的应用程序 1 和 2。虽然这两个应用程序都被分配了 384 KB 的内存,但它们实际上使用的内存都比这少(例如 264 KB)。
我尝试按照上述步骤操作,但它不起作用。这是我试过的代码:-
#define APPLICATION_ADDRESS 0x0800F000
#define APPLICATION2_ADDRESS 0x0806F800
#define SWAP_ADDRESS 0x0806F000
boolean swap(void)
{
char *app_1=( char*) APPLICATION_ADDRESS;//points to the 1st address of application1
char *app_2=(char*) APPLICATION2_ADDRESS;//points to the 1st address of application2
int mem1 = getMemorySize((unsigned char*)APPLICATION_ADDRESS);//returns the number of bytes in Application1
int mem2 = getMemorySize((unsigned char*)APPLICATION2_ADDRESS);//returns the number of bytes in Application2
int limit;
if(mem1>mem2)
limit= mem1;
else
limit= mem2;
Unlock_FLASH();
int lm = limit/2048;
for(int i=1; i<=lm; i++,app_1+=2048,app_2+=2048)
{
int *swap = (int *)SWAP_ADDRESS;
Erase_FLASH(swap);
Write_FLASH(app_1, swap);
Erase_FLASH(app_1);
Write_FLASH(app_2, app_1);
Erase_FLASH(app_2);
Write_FLASH(swap, app_2);
}
Lock_FLASH();
return TRUE;
}
void Unlock_FLASH(void)
{
while ((FLASH->SR & FLASH_SR_BSY) != 0 );
// Check if the controller is unlocked already
if ((FLASH->CR & FLASH_CR_LOCK) != 0 ){
// Write the first key
FLASH->KEYR = FLASH_FKEY1;
// Write the second key
FLASH->KEYR = FLASH_FKEY2;
}
}
void Erase_FLASH(int *c)
{
FLASH->CR |= FLASH_CR_PER; // Page erase operation
FLASH->ACR = c; // Set the address to the page to be written
FLASH->CR |= FLASH_CR_STRT;// Start the page erase
// Wait until page erase is done
while ((FLASH->SR & FLASH_SR_BSY) != 0);
// If the end of operation bit is set...
if ((FLASH->SR & FLASH_SR_EOP) != 0){
// Clear it, the operation was successful
FLASH->SR |= FLASH_SR_EOP;
}
//Otherwise there was an error
else{
// Manage the error cases
}
// Get out of page erase mode
FLASH->CR &= ~FLASH_CR_PER;
}
void Write_FLASH(int *a, int *b)
{
for(int i=1;i<=2048;i++,a++,b++)
{
FLASH->CR |= FLASH_CR_PG; // Programing mode
*(__IO uint16_t*)(b) = *a; // Write data
// Wait until the end of the operation
while ((FLASH->SR & FLASH_SR_BSY) != 0);
// If the end of operation bit is set...
if ((FLASH->SR & FLASH_SR_EOP) != 0){
// Clear it, the operation was successful
FLASH->SR |= FLASH_SR_EOP;
}
//Otherwise there was an error
else{
// Manage the error cases
}
}
FLASH->CR &= ~FLASH_CR_PG;
}
void Lock_FLASH(void)
{
FLASH->CR |= FLASH_CR_LOCK;
}
这里swap buffer是用来临时存储每页(2KB)作为交换时的缓冲区。此外,变量 limit 存储应用程序 1 和 2 的最大大小,以便在如前所述的内存大小不相等的情况下交换时不会出现错误。所以基本上我是一页一页地交换,一次只有 2 KB。
谁能找出代码中的错误?
谢谢,
舍图
2K 是 2048 字节,而不是 2024。修复整个代码中的增量。
There is another restriction that at a time only 2KB of memory can be copied
还有一个,这些内存块必须对齐到 2KB。
这个地址
#define APPLICATION2_ADDRESS 0x08076400
未正确对齐,它的值应能被 2048 (0x800) 整除。