2020-05-03 14:35:01 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2018 Inria
|
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2020-05-03 17:17:54 +02:00
|
|
|
* @ingroup cpu_stm32
|
2020-05-03 14:35:01 +02:00
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief Low-level flash lock/unlock implementation
|
|
|
|
*
|
|
|
|
* @author Alexandre Abadie <alexandre.abadie@inria.fr>
|
|
|
|
* @author Oleg Artamonov <oleg@unwds.com>
|
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cpu.h"
|
|
|
|
|
2020-10-22 11:34:00 +02:00
|
|
|
#define ENABLE_DEBUG 0
|
2020-05-03 14:35:01 +02:00
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
#if defined(CPU_FAM_STM32L0) || defined(CPU_FAM_STM32L1)
|
|
|
|
/* Data EEPROM and control register unlock keys */
|
|
|
|
#define FLASH_KEY1 ((uint32_t)0x89ABCDEF)
|
|
|
|
#define FLASH_KEY2 ((uint32_t)0x02030405)
|
|
|
|
#define CNTRL_REG (FLASH->PECR)
|
|
|
|
#define CNTRL_REG_LOCK (FLASH_PECR_PELOCK)
|
|
|
|
#define KEY_REG (FLASH->PEKEYR)
|
2021-12-15 15:53:53 +01:00
|
|
|
#elif defined(CPU_FAM_STM32L5) || defined(CPU_FAM_STM32U5)
|
2020-09-28 15:58:30 +02:00
|
|
|
#define FLASH_KEY1 ((uint32_t)0x45670123)
|
|
|
|
#define FLASH_KEY2 ((uint32_t)0xCDEF89AB)
|
|
|
|
#define CNTRL_REG (FLASH->NSCR)
|
2021-12-15 15:53:53 +01:00
|
|
|
#if defined(CPU_FAM_STM32U5)
|
|
|
|
#define CNTRL_REG_LOCK (FLASH_NSCR_LOCK)
|
|
|
|
#define KEY_REG (FLASH->NSKEYR)
|
|
|
|
#define FLASH_SR_EOP (FLASH_NSSR_EOP)
|
|
|
|
#else
|
2020-09-28 15:58:30 +02:00
|
|
|
#define CNTRL_REG_LOCK (FLASH_NSCR_NSLOCK)
|
|
|
|
#define KEY_REG (FLASH->NSKEYR)
|
|
|
|
#define FLASH_SR_EOP (FLASH_NSSR_NSEOP)
|
2021-12-15 15:53:53 +01:00
|
|
|
#endif
|
2020-05-03 14:35:01 +02:00
|
|
|
#else
|
2020-05-23 17:26:54 +02:00
|
|
|
#if defined(CPU_FAM_STM32L4) || defined(CPU_FAM_STM32WB) || \
|
2020-11-10 15:07:35 +01:00
|
|
|
defined(CPU_FAM_STM32G4) || defined(CPU_FAM_STM32G0) || \
|
|
|
|
defined(CPU_FAM_STM32F2) || defined(CPU_FAM_STM32F4) || \
|
2024-01-25 22:45:17 +01:00
|
|
|
defined(CPU_FAM_STM32F7) || defined(CPU_FAM_STM32WL) || \
|
|
|
|
defined(CPU_FAM_STM32C0)
|
2020-05-03 14:35:01 +02:00
|
|
|
#define FLASH_KEY1 ((uint32_t)0x45670123)
|
|
|
|
#define FLASH_KEY2 ((uint32_t)0xCDEF89AB)
|
|
|
|
#endif
|
|
|
|
#define CNTRL_REG (FLASH->CR)
|
|
|
|
#define CNTRL_REG_LOCK (FLASH_CR_LOCK)
|
|
|
|
#define KEY_REG (FLASH->KEYR)
|
|
|
|
#endif
|
|
|
|
|
2024-01-25 22:45:17 +01:00
|
|
|
#if defined(CPU_FAM_STM32G0) || defined(CPU_FAM_STM32C0)
|
2020-05-03 22:22:10 +02:00
|
|
|
#define FLASH_SR_BSY (FLASH_SR_BSY1)
|
|
|
|
#endif
|
|
|
|
|
2020-09-28 15:58:30 +02:00
|
|
|
#if defined(CPU_FAM_STM32L5)
|
|
|
|
#define FLASH_SR_BSY (FLASH_NSSR_NSBSY)
|
|
|
|
#define FLASH_SR_REG (FLASH->NSSR)
|
2021-12-15 15:53:53 +01:00
|
|
|
#elif defined(CPU_FAM_STM32U5)
|
|
|
|
#define FLASH_SR_BSY (FLASH_NSSR_BSY)
|
|
|
|
#define FLASH_SR_REG (FLASH->NSSR)
|
2020-09-28 15:58:30 +02:00
|
|
|
#else
|
|
|
|
#define FLASH_SR_REG (FLASH->SR)
|
|
|
|
#endif
|
|
|
|
|
2020-05-03 14:35:01 +02:00
|
|
|
void _unlock(void)
|
|
|
|
{
|
|
|
|
if (CNTRL_REG & CNTRL_REG_LOCK) {
|
|
|
|
DEBUG("[flash-common] unlocking the flash module\n");
|
|
|
|
KEY_REG = FLASH_KEY1;
|
|
|
|
KEY_REG = FLASH_KEY2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _lock(void)
|
|
|
|
{
|
|
|
|
if (!(CNTRL_REG & CNTRL_REG_LOCK)) {
|
|
|
|
DEBUG("[flash-common] locking the flash module\n");
|
|
|
|
CNTRL_REG |= CNTRL_REG_LOCK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _wait_for_pending_operations(void)
|
|
|
|
{
|
2020-09-28 15:58:30 +02:00
|
|
|
if (FLASH_SR_REG & FLASH_SR_BSY) {
|
2020-05-03 14:35:01 +02:00
|
|
|
DEBUG("[flash-common] waiting for any pending operation to finish\n");
|
2020-09-28 15:58:30 +02:00
|
|
|
while (FLASH_SR_REG & FLASH_SR_BSY) {}
|
2020-05-03 14:35:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear 'end of operation' bit in status register, for other STM32 boards
|
|
|
|
this bit is set only if EOPIE is set, which is currently not done */
|
|
|
|
#if defined(CPU_FAM_STM32F0) || defined(CPU_FAM_STM32F1) || \
|
|
|
|
defined(CPU_FAM_STM32F3) || defined(CPU_FAM_STM32L0) || \
|
|
|
|
defined(CPU_FAM_STM32L1)
|
2020-09-28 15:58:30 +02:00
|
|
|
FLASH_SR_REG |= FLASH_SR_EOP;
|
2020-05-03 14:35:01 +02:00
|
|
|
#endif
|
|
|
|
}
|