Skip to content

STM32: Lock / Unlock flash for each operation #5026

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 36 additions & 10 deletions targets/TARGET_STM/TARGET_STM32F4/flash_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ static uint32_t GetSector(uint32_t Address);
static uint32_t GetSectorSize(uint32_t Sector);

int32_t flash_init(flash_t *obj)
{
return 0;
}

int32_t flash_free(flash_t *obj)
{
return 0;
}

static int32_t flash_unlock(void)
{
/* Allow Access to Flash control registers and user Falsh */
if (HAL_FLASH_Unlock()) {
Expand All @@ -48,28 +58,34 @@ int32_t flash_init(flash_t *obj)
return 0;
}
}
int32_t flash_free(flash_t *obj)

static int32_t flash_lock(void)
{
/* Disable the Flash option control register access (recommended to protect
/* Disable the Flash option control register access (recommended to protect
the option Bytes against possible unwanted operations) */
if (HAL_FLASH_Lock()) {
return -1;
} else {
return 0;
}
}

int32_t flash_erase_sector(flash_t *obj, uint32_t address)
{
/*Variable used for Erase procedure*/
static FLASH_EraseInitTypeDef EraseInitStruct;
uint32_t FirstSector;
uint32_t SectorError = 0;

int32_t status = 0;

if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) {
return -1;
}

if (flash_unlock() != HAL_OK) {
return -1;
}

/* Get the 1st sector to erase */
FirstSector = GetSector(address);

Expand All @@ -79,19 +95,26 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address)
EraseInitStruct.Sector = FirstSector;
EraseInitStruct.NbSectors = 1;
if(HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK){
return -1;
} else {
return 0;
status = -1;
}

flash_lock();

return status;
}

int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, uint32_t size)
{
int32_t status = 0;

if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) {
return -1;
}

if (flash_unlock() != HAL_OK) {
return -1;
}

/* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache,
you have to make sure that these data are rewritten before they are accessed during code
execution. If this cannot be done safely, it is recommended to flush the caches by setting the
Expand All @@ -105,16 +128,19 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
__HAL_FLASH_INSTRUCTION_CACHE_ENABLE();
__HAL_FLASH_DATA_CACHE_ENABLE();

while (size > 0) {
while ((size > 0) && (status == 0)) {
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, address, (uint64_t)*data) != HAL_OK) {
return -1;
status = -1;
} else {
size--;
address++;
data++;
}
}
return 0;

flash_lock();

return status;
}

uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address)
Expand Down
43 changes: 35 additions & 8 deletions targets/TARGET_STM/TARGET_STM32F7/flash_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,24 @@ static uint32_t GetSectorSize(uint32_t Sector);

int32_t flash_init(flash_t *obj)
{
/* Allow Access to Flash control registers and user Flash */
return 0;
}
int32_t flash_free(flash_t *obj)
{
return 0;
}

static int32_t flash_unlock(void)
{
/* Allow Access to Flash control registers and user Falsh */
if (HAL_FLASH_Unlock()) {
return -1;
} else {
return 0;
}
}
int32_t flash_free(flash_t *obj)

static int32_t flash_lock(void)
{
/* Disable the Flash option control register access (recommended to protect
the option Bytes against possible unwanted operations) */
Expand All @@ -55,18 +65,24 @@ int32_t flash_free(flash_t *obj)
return 0;
}
}

int32_t flash_erase_sector(flash_t *obj, uint32_t address)
{
/* Variable used for Erase procedure */
FLASH_EraseInitTypeDef EraseInitStruct;
FLASH_OBProgramInitTypeDef OBInit;
uint32_t SectorId;
uint32_t SectorError = 0;
int32_t status = 0;

if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) {
return -1;
}

if (flash_unlock() != HAL_OK) {
return -1;
}

/* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache,
you have to make sure that these data are rewritten before they are accessed during code
execution. If this cannot be done safely, it is recommended to flush the caches by setting the
Expand Down Expand Up @@ -102,19 +118,27 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address)
EraseInitStruct.NbSectors = 1;

if(HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK){
return -1;
} else {
return 0;
status = -1;
}

flash_lock();

return status;
}

int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
uint32_t size)
{
int32_t status = 0;

if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) {
return -1;
}

if (flash_unlock() != HAL_OK) {
return -1;
}

/* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache,
you have to make sure that these data are rewritten before they are accessed during code
execution. If this cannot be done safely, it is recommended to flush the caches by setting the
Expand All @@ -123,17 +147,20 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
__HAL_FLASH_ART_RESET();
__HAL_FLASH_ART_ENABLE();

while (size > 0) {
while ((size > 0) && (status == 0)) {
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE,
address, (uint64_t)*data) != HAL_OK) {
return -1;
status = -1;
} else {
size--;
address++;
data++;
}
}
return 0;

flash_lock();

return status;
}

uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address)
Expand Down
57 changes: 44 additions & 13 deletions targets/TARGET_STM/TARGET_STM32L0/flash_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,51 @@

int32_t flash_init(flash_t *obj)
{
/* Unlock the Flash to enable the flash control register access *************/
HAL_FLASH_Unlock();
return 0;
}

int32_t flash_free(flash_t *obj)
{
/* Lock the Flash to disable the flash control register access (recommended
* to protect the FLASH memory against possible unwanted operation) *********/
HAL_FLASH_Lock();
return 0;
}

static int32_t flash_unlock(void)
{
/* Allow Access to Flash control registers and user Falsh */
if (HAL_FLASH_Unlock()) {
return -1;
} else {
return 0;
}
}

static int32_t flash_lock(void)
{
/* Disable the Flash option control register access (recommended to protect
the option Bytes against possible unwanted operations) */
if (HAL_FLASH_Lock()) {
return -1;
} else {
return 0;
}
}

int32_t flash_erase_sector(flash_t *obj, uint32_t address)
{
uint32_t FirstPage = 0;
uint32_t PAGEError = 0;
FLASH_EraseInitTypeDef EraseInitStruct;
int32_t status = 0;

if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) {

return -1;
}

if (flash_unlock() != HAL_OK) {
return -1;
}

/* Clear OPTVERR bit set on virgin samples */
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);

Expand All @@ -65,16 +86,20 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address)
DCRST and ICRST bits in the FLASH_CR register. */

if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK) {
return -1;
} else {
return 0;
status = -1;
}

flash_lock();

return status;

}

int32_t flash_program_page(flash_t *obj, uint32_t address,
const uint8_t *data, uint32_t size)
{
uint32_t StartAddress = 0;
int32_t status = 0;

if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) {
return -1;
Expand All @@ -85,14 +110,18 @@ int32_t flash_program_page(flash_t *obj, uint32_t address,
return -1;
}

if (flash_unlock() != HAL_OK) {
return -1;
}

/* Program the user Flash area word by word */
StartAddress = address;

/* HW needs an aligned address to program flash, which data
* parameters doesn't ensure */
if ((uint32_t) data % 4 != 0) {
volatile uint32_t data32;
while (address < (StartAddress + size)) {
while ((address < (StartAddress + size)) && (status == 0)) {
for (uint8_t i =0; i < 4; i++) {
*(((uint8_t *) &data32) + i) = *(data + i);
}
Expand All @@ -101,21 +130,23 @@ int32_t flash_program_page(flash_t *obj, uint32_t address,
address = address + 4;
data = data + 4;
} else {
return -1;
status = -1;
}
}
} else { /* case where data is aligned, so let's avoid any copy */
while (address < (StartAddress + size)) {
while ((address < (StartAddress + size)) && (status == 0)) {
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, *((uint32_t*) data)) == HAL_OK) {
address = address + 4;
data = data + 4;
} else {
return -1;
status = -1;
}
}
}

return 0;
flash_lock();

return status;
}

uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address) {
Expand Down
Loading