2014-05-03 18:27:38 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2017-07-03 03:37:22 -04:00
|
|
|
#include "py/mpconfig.h"
|
2015-11-24 08:35:24 -05:00
|
|
|
#include "py/misc.h"
|
2020-06-17 23:35:53 -04:00
|
|
|
#include "py/mphal.h"
|
2017-07-03 03:37:22 -04:00
|
|
|
#include "flash.h"
|
2014-03-17 09:31:35 -04:00
|
|
|
|
2020-11-03 23:29:31 -05:00
|
|
|
#if MICROPY_HW_STM32WB_FLASH_SYNCRONISATION
|
|
|
|
// See WB55 specific documentation in AN5289 Rev 3, and in particular, Figure 10.
|
|
|
|
|
|
|
|
#include "rfcore.h"
|
|
|
|
#include "stm32wbxx_ll_hsem.h"
|
|
|
|
|
|
|
|
// Protects all flash registers.
|
|
|
|
#define SEMID_FLASH_REGISTERS (2)
|
|
|
|
// Used by CPU1 to prevent CPU2 from writing/erasing data in flash memory.
|
|
|
|
#define SEMID_FLASH_CPU1 (6)
|
|
|
|
// Used by CPU2 to prevent CPU1 from writing/erasing data in flash memory.
|
|
|
|
#define SEMID_FLASH_CPU2 (7)
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2016-03-22 12:49:39 -04:00
|
|
|
typedef struct {
|
|
|
|
uint32_t base_address;
|
|
|
|
uint32_t sector_size;
|
|
|
|
uint32_t sector_count;
|
|
|
|
} flash_layout_t;
|
|
|
|
|
2018-05-28 04:10:53 -04:00
|
|
|
#if defined(STM32F0)
|
|
|
|
|
|
|
|
static const flash_layout_t flash_layout[] = {
|
|
|
|
{ FLASH_BASE, FLASH_PAGE_SIZE, (FLASH_BANK1_END + 1 - FLASH_BASE) / FLASH_PAGE_SIZE },
|
|
|
|
};
|
|
|
|
|
|
|
|
#elif defined(STM32F4)
|
2016-03-22 12:49:39 -04:00
|
|
|
|
|
|
|
static const flash_layout_t flash_layout[] = {
|
|
|
|
{ 0x08000000, 0x04000, 4 },
|
|
|
|
{ 0x08010000, 0x10000, 1 },
|
|
|
|
{ 0x08020000, 0x20000, 3 },
|
|
|
|
#if defined(FLASH_SECTOR_8)
|
|
|
|
{ 0x08080000, 0x20000, 4 },
|
|
|
|
#endif
|
|
|
|
#if defined(FLASH_SECTOR_12)
|
|
|
|
{ 0x08100000, 0x04000, 4 },
|
|
|
|
{ 0x08110000, 0x10000, 1 },
|
|
|
|
{ 0x08120000, 0x20000, 7 },
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2018-03-16 19:42:50 -04:00
|
|
|
#elif defined(STM32F7)
|
2015-08-03 18:07:20 -04:00
|
|
|
|
2015-07-28 14:13:33 -04:00
|
|
|
// FLASH_FLAG_PGSERR (Programming Sequence Error) was renamed to
|
|
|
|
// FLASH_FLAG_ERSERR (Erasing Sequence Error) in STM32F7
|
|
|
|
#define FLASH_FLAG_PGSERR FLASH_FLAG_ERSERR
|
2015-07-30 12:50:58 -04:00
|
|
|
|
2019-07-18 20:52:48 -04:00
|
|
|
#if defined(STM32F722xx) || defined(STM32F723xx) || defined(STM32F732xx) || defined(STM32F733xx)
|
|
|
|
static const flash_layout_t flash_layout[] = {
|
|
|
|
{ 0x08000000, 0x04000, 4 },
|
|
|
|
{ 0x08010000, 0x10000, 1 },
|
|
|
|
{ 0x08020000, 0x20000, 3 },
|
|
|
|
};
|
|
|
|
#else
|
2020-06-18 01:12:25 -04:00
|
|
|
// This is for dual-bank mode disabled
|
2016-03-22 12:49:39 -04:00
|
|
|
static const flash_layout_t flash_layout[] = {
|
|
|
|
{ 0x08000000, 0x08000, 4 },
|
|
|
|
{ 0x08020000, 0x20000, 1 },
|
2020-06-18 01:12:25 -04:00
|
|
|
#if FLASH_SECTOR_TOTAL == 8
|
2016-03-22 12:49:39 -04:00
|
|
|
{ 0x08040000, 0x40000, 3 },
|
2020-06-18 01:12:25 -04:00
|
|
|
#else
|
|
|
|
{ 0x08040000, 0x40000, 7 },
|
|
|
|
#endif
|
2016-03-22 12:49:39 -04:00
|
|
|
};
|
2019-07-18 20:52:48 -04:00
|
|
|
#endif
|
2015-07-30 12:50:58 -04:00
|
|
|
|
2019-07-17 02:33:31 -04:00
|
|
|
#elif defined(STM32L0) || defined(STM32L4) || defined(STM32WB)
|
2016-03-22 12:49:39 -04:00
|
|
|
|
|
|
|
static const flash_layout_t flash_layout[] = {
|
|
|
|
{ (uint32_t)FLASH_BASE, (uint32_t)FLASH_PAGE_SIZE, 512 },
|
|
|
|
};
|
2015-07-30 12:50:58 -04:00
|
|
|
|
2018-02-22 07:05:00 -05:00
|
|
|
#elif defined(STM32H7)
|
|
|
|
|
|
|
|
static const flash_layout_t flash_layout[] = {
|
2018-05-29 19:54:51 -04:00
|
|
|
{ 0x08000000, 0x20000, 16 },
|
2018-02-22 07:05:00 -05:00
|
|
|
};
|
|
|
|
|
2015-11-24 08:35:24 -05:00
|
|
|
#else
|
2016-03-22 12:49:39 -04:00
|
|
|
#error Unsupported processor
|
2015-07-30 12:50:58 -04:00
|
|
|
#endif
|
|
|
|
|
2018-11-30 20:38:29 -05:00
|
|
|
#if (defined(STM32L4) && defined(SYSCFG_MEMRMP_FB_MODE)) || defined(STM32H7)
|
2016-03-22 12:49:39 -04:00
|
|
|
|
|
|
|
// get the bank of a given flash address
|
|
|
|
static uint32_t get_bank(uint32_t addr) {
|
2018-02-22 07:05:00 -05:00
|
|
|
#if defined(STM32H7)
|
|
|
|
if (READ_BIT(FLASH->OPTCR, FLASH_OPTCR_SWAP_BANK) == 0) {
|
|
|
|
#else
|
2016-03-22 12:49:39 -04:00
|
|
|
if (READ_BIT(SYSCFG->MEMRMP, SYSCFG_MEMRMP_FB_MODE) == 0) {
|
2020-02-26 23:36:53 -05:00
|
|
|
#endif
|
2016-03-22 12:49:39 -04:00
|
|
|
// no bank swap
|
|
|
|
if (addr < (FLASH_BASE + FLASH_BANK_SIZE)) {
|
|
|
|
return FLASH_BANK_1;
|
|
|
|
} else {
|
|
|
|
return FLASH_BANK_2;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// bank swap
|
|
|
|
if (addr < (FLASH_BASE + FLASH_BANK_SIZE)) {
|
|
|
|
return FLASH_BANK_2;
|
|
|
|
} else {
|
|
|
|
return FLASH_BANK_1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 20:38:29 -05:00
|
|
|
#if (defined(STM32L4) && defined(SYSCFG_MEMRMP_FB_MODE))
|
2016-03-22 12:49:39 -04:00
|
|
|
// get the page of a given flash address
|
|
|
|
static uint32_t get_page(uint32_t addr) {
|
|
|
|
if (addr < (FLASH_BASE + FLASH_BANK_SIZE)) {
|
|
|
|
// bank 1
|
|
|
|
return (addr - FLASH_BASE) / FLASH_PAGE_SIZE;
|
|
|
|
} else {
|
|
|
|
// bank 2
|
|
|
|
return (addr - (FLASH_BASE + FLASH_BANK_SIZE)) / FLASH_PAGE_SIZE;
|
|
|
|
}
|
|
|
|
}
|
2018-02-22 07:05:00 -05:00
|
|
|
#endif
|
2016-03-22 12:49:39 -04:00
|
|
|
|
2019-07-17 02:33:31 -04:00
|
|
|
#elif (defined(STM32L4) && !defined(SYSCFG_MEMRMP_FB_MODE)) || defined(STM32WB)
|
2019-05-02 00:53:26 -04:00
|
|
|
|
|
|
|
static uint32_t get_page(uint32_t addr) {
|
|
|
|
return (addr - FLASH_BASE) / FLASH_PAGE_SIZE;
|
|
|
|
}
|
|
|
|
|
2016-03-22 12:49:39 -04:00
|
|
|
#endif
|
2014-03-17 09:31:35 -04:00
|
|
|
|
2020-06-18 01:12:25 -04:00
|
|
|
bool flash_is_valid_addr(uint32_t addr) {
|
|
|
|
uint8_t last = MP_ARRAY_SIZE(flash_layout) - 1;
|
|
|
|
uint32_t end_of_flash = flash_layout[last].base_address +
|
|
|
|
flash_layout[last].sector_count * flash_layout[last].sector_size;
|
|
|
|
return flash_layout[0].base_address <= addr && addr < end_of_flash;
|
|
|
|
}
|
|
|
|
|
2020-06-30 02:33:32 -04:00
|
|
|
int32_t flash_get_sector_info(uint32_t addr, uint32_t *start_addr, uint32_t *size) {
|
2016-03-22 12:49:39 -04:00
|
|
|
if (addr >= flash_layout[0].base_address) {
|
|
|
|
uint32_t sector_index = 0;
|
|
|
|
for (int i = 0; i < MP_ARRAY_SIZE(flash_layout); ++i) {
|
|
|
|
for (int j = 0; j < flash_layout[i].sector_count; ++j) {
|
|
|
|
uint32_t sector_start_next = flash_layout[i].base_address
|
|
|
|
+ (j + 1) * flash_layout[i].sector_size;
|
|
|
|
if (addr < sector_start_next) {
|
|
|
|
if (start_addr != NULL) {
|
|
|
|
*start_addr = flash_layout[i].base_address
|
|
|
|
+ j * flash_layout[i].sector_size;
|
|
|
|
}
|
|
|
|
if (size != NULL) {
|
|
|
|
*size = flash_layout[i].sector_size;
|
|
|
|
}
|
|
|
|
return sector_index;
|
2014-03-17 09:31:35 -04:00
|
|
|
}
|
2016-03-22 12:49:39 -04:00
|
|
|
++sector_index;
|
2014-03-17 09:31:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-30 02:33:32 -04:00
|
|
|
return -1;
|
2014-03-17 09:31:35 -04:00
|
|
|
}
|
|
|
|
|
2020-06-17 23:35:53 -04:00
|
|
|
int flash_erase(uint32_t flash_dest, uint32_t num_word32) {
|
2014-04-16 18:08:36 -04:00
|
|
|
// check there is something to write
|
|
|
|
if (num_word32 == 0) {
|
2020-06-17 23:35:53 -04:00
|
|
|
return 0;
|
2014-04-16 18:08:36 -04:00
|
|
|
}
|
|
|
|
|
2020-11-03 23:29:31 -05:00
|
|
|
#if MICROPY_HW_STM32WB_FLASH_SYNCRONISATION
|
|
|
|
// Acquire lock on the flash peripheral.
|
|
|
|
while (LL_HSEM_1StepLock(HSEM, SEMID_FLASH_REGISTERS)) {
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-06-17 23:35:53 -04:00
|
|
|
// Unlock the flash for erase.
|
2014-04-16 18:08:36 -04:00
|
|
|
HAL_FLASH_Unlock();
|
|
|
|
|
2020-11-03 23:29:31 -05:00
|
|
|
#if MICROPY_HW_STM32WB_FLASH_SYNCRONISATION
|
|
|
|
// Tell the HCI controller stack we're starting an erase, so it
|
|
|
|
// avoids radio activity for a while.
|
|
|
|
rfcore_start_flash_erase();
|
|
|
|
// Wait for PES.
|
|
|
|
while (LL_FLASH_IsActiveFlag_OperationSuspended()) {
|
|
|
|
}
|
|
|
|
// Wait for flash lock.
|
|
|
|
while (LL_HSEM_1StepLock(HSEM, SEMID_FLASH_CPU2)) {
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-06-17 23:35:53 -04:00
|
|
|
// Clear pending flags (if any) and set up EraseInitStruct.
|
2016-03-22 12:49:39 -04:00
|
|
|
|
2020-06-17 23:35:53 -04:00
|
|
|
FLASH_EraseInitTypeDef EraseInitStruct;
|
2018-05-28 04:10:53 -04:00
|
|
|
#if defined(STM32F0)
|
|
|
|
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR);
|
2020-02-26 23:36:53 -05:00
|
|
|
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
|
2018-05-28 04:10:53 -04:00
|
|
|
EraseInitStruct.PageAddress = flash_dest;
|
2020-02-26 23:36:53 -05:00
|
|
|
EraseInitStruct.NbPages = (4 * num_word32 + FLASH_PAGE_SIZE - 4) / FLASH_PAGE_SIZE;
|
2019-07-05 03:24:59 -04:00
|
|
|
#elif defined(STM32L0)
|
|
|
|
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR);
|
2020-02-26 23:36:53 -05:00
|
|
|
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
|
2019-07-05 03:24:59 -04:00
|
|
|
EraseInitStruct.PageAddress = flash_dest;
|
2020-02-26 23:36:53 -05:00
|
|
|
EraseInitStruct.NbPages = (4 * num_word32 + FLASH_PAGE_SIZE - 4) / FLASH_PAGE_SIZE;
|
2019-07-17 02:33:31 -04:00
|
|
|
#elif (defined(STM32L4) && !defined(SYSCFG_MEMRMP_FB_MODE)) || defined(STM32WB)
|
2018-11-30 20:38:29 -05:00
|
|
|
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);
|
2020-02-26 23:36:53 -05:00
|
|
|
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
|
|
|
|
EraseInitStruct.Page = get_page(flash_dest);
|
|
|
|
EraseInitStruct.NbPages = (4 * num_word32 + FLASH_PAGE_SIZE - 4) / FLASH_PAGE_SIZE;
|
2018-05-28 04:10:53 -04:00
|
|
|
#elif defined(STM32L4)
|
2016-03-22 12:49:39 -04:00
|
|
|
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);
|
|
|
|
// The sector returned by flash_get_sector_info can not be used
|
|
|
|
// as the flash has on each bank 0/1 pages 0..255
|
2020-02-26 23:36:53 -05:00
|
|
|
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
|
|
|
|
EraseInitStruct.Banks = get_bank(flash_dest);
|
|
|
|
EraseInitStruct.Page = get_page(flash_dest);
|
|
|
|
EraseInitStruct.NbPages = get_page(flash_dest + 4 * num_word32 - 1) - EraseInitStruct.Page + 1;
|
2016-03-22 12:49:39 -04:00
|
|
|
#else
|
2020-06-17 23:35:53 -04:00
|
|
|
|
2018-02-22 07:05:00 -05:00
|
|
|
#if defined(STM32H7)
|
|
|
|
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS_BANK1 | FLASH_FLAG_ALL_ERRORS_BANK2);
|
|
|
|
#else
|
2014-04-16 18:08:36 -04:00
|
|
|
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
|
2020-02-26 23:36:53 -05:00
|
|
|
FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
|
2018-02-22 07:05:00 -05:00
|
|
|
#endif
|
2014-04-16 18:08:36 -04:00
|
|
|
|
|
|
|
EraseInitStruct.TypeErase = TYPEERASE_SECTORS;
|
2021-09-15 09:08:16 -04:00
|
|
|
#if defined(FLASH_CR_PSIZE)
|
2014-04-16 18:08:36 -04:00
|
|
|
EraseInitStruct.VoltageRange = VOLTAGE_RANGE_3; // voltage range needs to be 2.7V to 3.6V
|
2021-09-15 09:08:16 -04:00
|
|
|
#else
|
|
|
|
EraseInitStruct.VoltageRange = 0; // unused parameter on STM32H7A3/B3
|
|
|
|
#endif
|
2018-02-22 07:05:00 -05:00
|
|
|
#if defined(STM32H7)
|
|
|
|
EraseInitStruct.Banks = get_bank(flash_dest);
|
|
|
|
#endif
|
2014-04-16 18:08:36 -04:00
|
|
|
EraseInitStruct.Sector = flash_get_sector_info(flash_dest, NULL, NULL);
|
|
|
|
EraseInitStruct.NbSectors = flash_get_sector_info(flash_dest + 4 * num_word32 - 1, NULL, NULL) - EraseInitStruct.Sector + 1;
|
2020-06-17 23:35:53 -04:00
|
|
|
|
2016-03-22 12:49:39 -04:00
|
|
|
#endif
|
|
|
|
|
2020-06-17 23:35:53 -04:00
|
|
|
// Erase the sectors.
|
2014-04-16 18:08:36 -04:00
|
|
|
uint32_t SectorError = 0;
|
2020-06-17 23:35:53 -04:00
|
|
|
HAL_StatusTypeDef status = HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError);
|
|
|
|
|
2020-11-03 23:29:31 -05:00
|
|
|
#if MICROPY_HW_STM32WB_FLASH_SYNCRONISATION
|
|
|
|
// Release flash lock.
|
|
|
|
while (__HAL_FLASH_GET_FLAG(FLASH_FLAG_CFGBSY)) {
|
|
|
|
}
|
|
|
|
LL_HSEM_ReleaseLock(HSEM, SEMID_FLASH_CPU2, 0);
|
|
|
|
// Tell HCI controller that erase is over.
|
|
|
|
rfcore_end_flash_erase();
|
|
|
|
#endif
|
|
|
|
|
2020-06-17 23:35:53 -04:00
|
|
|
// Lock the flash after erase.
|
|
|
|
HAL_FLASH_Lock();
|
|
|
|
|
2020-11-03 23:29:31 -05:00
|
|
|
#if MICROPY_HW_STM32WB_FLASH_SYNCRONISATION
|
|
|
|
// Release lock on the flash peripheral.
|
|
|
|
LL_HSEM_ReleaseLock(HSEM, SEMID_FLASH_REGISTERS, 0);
|
|
|
|
#endif
|
|
|
|
|
2020-06-17 23:35:53 -04:00
|
|
|
return mp_hal_status_to_neg_errno(status);
|
2014-04-16 18:08:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
// erase the sector using an interrupt
|
2018-05-01 23:11:56 -04:00
|
|
|
void flash_erase_it(uint32_t flash_dest, uint32_t num_word32) {
|
2014-04-16 18:08:36 -04:00
|
|
|
// check there is something to write
|
|
|
|
if (num_word32 == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// unlock
|
|
|
|
HAL_FLASH_Unlock();
|
|
|
|
|
|
|
|
// Clear pending flags (if any)
|
|
|
|
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
|
|
|
|
FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR);
|
|
|
|
|
|
|
|
// erase the sector(s)
|
|
|
|
FLASH_EraseInitTypeDef EraseInitStruct;
|
|
|
|
EraseInitStruct.TypeErase = TYPEERASE_SECTORS;
|
|
|
|
EraseInitStruct.VoltageRange = VOLTAGE_RANGE_3; // voltage range needs to be 2.7V to 3.6V
|
|
|
|
EraseInitStruct.Sector = flash_get_sector_info(flash_dest, NULL, NULL);
|
|
|
|
EraseInitStruct.NbSectors = flash_get_sector_info(flash_dest + 4 * num_word32 - 1, NULL, NULL) - EraseInitStruct.Sector + 1;
|
|
|
|
if (HAL_FLASHEx_Erase_IT(&EraseInitStruct) != HAL_OK) {
|
|
|
|
// error occurred during sector erase
|
|
|
|
HAL_FLASH_Lock(); // lock the flash
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2020-06-17 23:35:53 -04:00
|
|
|
int flash_write(uint32_t flash_dest, const uint32_t *src, uint32_t num_word32) {
|
2020-11-03 23:29:31 -05:00
|
|
|
#if MICROPY_HW_STM32WB_FLASH_SYNCRONISATION
|
|
|
|
// Acquire lock on the flash peripheral.
|
|
|
|
while (LL_HSEM_1StepLock(HSEM, SEMID_FLASH_REGISTERS)) {
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-06-17 23:35:53 -04:00
|
|
|
// Unlock the flash for write.
|
|
|
|
HAL_FLASH_Unlock();
|
|
|
|
|
2020-11-03 23:29:31 -05:00
|
|
|
#if MICROPY_HW_STM32WB_FLASH_SYNCRONISATION
|
|
|
|
// Wait for PES.
|
|
|
|
while (LL_FLASH_IsActiveFlag_OperationSuspended()) {
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-06-17 23:35:53 -04:00
|
|
|
HAL_StatusTypeDef status = HAL_OK;
|
|
|
|
|
2019-07-17 02:33:31 -04:00
|
|
|
#if defined(STM32L4) || defined(STM32WB)
|
2016-03-22 12:49:39 -04:00
|
|
|
|
|
|
|
// program the flash uint64 by uint64
|
|
|
|
for (int i = 0; i < num_word32 / 2; i++) {
|
2020-02-26 23:36:53 -05:00
|
|
|
uint64_t val = *(uint64_t *)src;
|
2020-11-03 23:29:31 -05:00
|
|
|
|
|
|
|
#if MICROPY_HW_STM32WB_FLASH_SYNCRONISATION
|
|
|
|
// Wait for flash lock.
|
|
|
|
while (LL_HSEM_1StepLock(HSEM, SEMID_FLASH_CPU2)) {
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-06-17 23:35:53 -04:00
|
|
|
status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, flash_dest, val);
|
2020-11-03 23:29:31 -05:00
|
|
|
|
|
|
|
#if MICROPY_HW_STM32WB_FLASH_SYNCRONISATION
|
|
|
|
// Release flash lock.
|
|
|
|
LL_HSEM_ReleaseLock(HSEM, SEMID_FLASH_CPU2, 0);
|
|
|
|
while (__HAL_FLASH_GET_FLAG(FLASH_FLAG_CFGBSY)) {
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-06-17 23:35:53 -04:00
|
|
|
if (status != HAL_OK) {
|
|
|
|
num_word32 = 0; // don't write any odd word after this loop
|
|
|
|
break;
|
2016-03-22 12:49:39 -04:00
|
|
|
}
|
|
|
|
flash_dest += 8;
|
|
|
|
src += 2;
|
|
|
|
}
|
|
|
|
if ((num_word32 & 0x01) == 1) {
|
2020-02-26 23:36:53 -05:00
|
|
|
uint64_t val = *(uint64_t *)flash_dest;
|
2016-03-22 12:49:39 -04:00
|
|
|
val = (val & 0xffffffff00000000uL) | (*src);
|
2020-11-03 23:29:31 -05:00
|
|
|
|
|
|
|
#if MICROPY_HW_STM32WB_FLASH_SYNCRONISATION
|
|
|
|
// Wait for flash lock.
|
|
|
|
while (LL_HSEM_1StepLock(HSEM, SEMID_FLASH_CPU2)) {
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-06-17 23:35:53 -04:00
|
|
|
status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, flash_dest, val);
|
2020-11-03 23:29:31 -05:00
|
|
|
|
|
|
|
#if MICROPY_HW_STM32WB_FLASH_SYNCRONISATION
|
|
|
|
// Release flash lock.
|
|
|
|
LL_HSEM_ReleaseLock(HSEM, SEMID_FLASH_CPU2, 0);
|
|
|
|
while (__HAL_FLASH_GET_FLAG(FLASH_FLAG_CFGBSY)) {
|
|
|
|
}
|
|
|
|
#endif
|
2016-03-22 12:49:39 -04:00
|
|
|
}
|
|
|
|
|
2018-02-22 07:05:00 -05:00
|
|
|
#elif defined(STM32H7)
|
|
|
|
|
|
|
|
// program the flash 256 bits at a time
|
|
|
|
for (int i = 0; i < num_word32 / 8; i++) {
|
2020-06-17 23:35:53 -04:00
|
|
|
status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD, flash_dest, (uint64_t)(uint32_t)src);
|
|
|
|
if (status != HAL_OK) {
|
|
|
|
break;
|
2018-02-22 07:05:00 -05:00
|
|
|
}
|
|
|
|
flash_dest += 32;
|
|
|
|
src += 8;
|
|
|
|
}
|
|
|
|
|
2016-03-22 12:49:39 -04:00
|
|
|
#else
|
|
|
|
|
2014-04-16 18:08:36 -04:00
|
|
|
// program the flash word by word
|
|
|
|
for (int i = 0; i < num_word32; i++) {
|
2020-06-17 23:35:53 -04:00
|
|
|
status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, flash_dest, *src);
|
|
|
|
if (status != HAL_OK) {
|
|
|
|
break;
|
2014-04-16 18:08:36 -04:00
|
|
|
}
|
|
|
|
flash_dest += 4;
|
|
|
|
src += 1;
|
|
|
|
}
|
|
|
|
|
2016-03-22 12:49:39 -04:00
|
|
|
#endif
|
|
|
|
|
2020-06-17 23:35:53 -04:00
|
|
|
// Lock the flash after write.
|
2014-04-16 18:08:36 -04:00
|
|
|
HAL_FLASH_Lock();
|
2020-06-17 23:35:53 -04:00
|
|
|
|
2020-11-03 23:29:31 -05:00
|
|
|
#if MICROPY_HW_STM32WB_FLASH_SYNCRONISATION
|
|
|
|
// Release lock on the flash peripheral.
|
|
|
|
LL_HSEM_ReleaseLock(HSEM, SEMID_FLASH_REGISTERS, 0);
|
|
|
|
#endif
|
|
|
|
|
2020-06-17 23:35:53 -04:00
|
|
|
return mp_hal_status_to_neg_errno(status);
|
2014-04-16 18:08:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
use erase, then write
|
|
|
|
void flash_erase_and_write(uint32_t flash_dest, const uint32_t *src, uint32_t num_word32) {
|
2014-03-17 09:31:35 -04:00
|
|
|
// check there is something to write
|
|
|
|
if (num_word32 == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// unlock
|
|
|
|
HAL_FLASH_Unlock();
|
|
|
|
|
|
|
|
// Clear pending flags (if any)
|
|
|
|
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
|
|
|
|
FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR);
|
|
|
|
|
|
|
|
// erase the sector(s)
|
|
|
|
FLASH_EraseInitTypeDef EraseInitStruct;
|
|
|
|
EraseInitStruct.TypeErase = TYPEERASE_SECTORS;
|
|
|
|
EraseInitStruct.VoltageRange = VOLTAGE_RANGE_3; // voltage range needs to be 2.7V to 3.6V
|
|
|
|
EraseInitStruct.Sector = flash_get_sector_info(flash_dest, NULL, NULL);
|
|
|
|
EraseInitStruct.NbSectors = flash_get_sector_info(flash_dest + 4 * num_word32 - 1, NULL, NULL) - EraseInitStruct.Sector + 1;
|
|
|
|
uint32_t SectorError = 0;
|
|
|
|
if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK) {
|
|
|
|
// error occurred during sector erase
|
2014-04-16 18:08:36 -04:00
|
|
|
HAL_FLASH_Lock(); // lock the flash
|
2014-03-17 09:31:35 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// program the flash word by word
|
|
|
|
for (int i = 0; i < num_word32; i++) {
|
|
|
|
if (HAL_FLASH_Program(TYPEPROGRAM_WORD, flash_dest, *src) != HAL_OK) {
|
|
|
|
// error occurred during flash write
|
2014-04-16 18:08:36 -04:00
|
|
|
HAL_FLASH_Lock(); // lock the flash
|
2014-03-17 09:31:35 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
flash_dest += 4;
|
|
|
|
src += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// lock the flash
|
|
|
|
HAL_FLASH_Lock();
|
|
|
|
}
|
2014-04-16 18:08:36 -04:00
|
|
|
*/
|