📄 stm8l15x_flash.c
字号:
/**
******************************************************************************
* @file stm8l15x_flash.c
* @author MCD Application Team
* @version V1.5.0
* @date 13-May-2011
* @brief This file provides all the Flash firmware functions. These functions
* can be executed from Internal FLASH or Internal RAM memories.
* - FLASH program and Data EEPROM memories interface configuration
* - FLASH program and Data EEPROM memories Programming
* - Option Bytes Programming
* - Interrupts and flags management
* - Functions to be executed from RAM
*
* @verbatim
*
* ===================================================================
* How to use this driver
* ===================================================================
*
* This driver provides functions to configure and program the Flash
* memory of all STM8L15x devices
* These functions are split in 4 groups
*
* 1. FLASH program and Data EEPROM memories interface configuration
* functions: this group includes the management of the following
* features:
* - Set the fixed programming time
* - Configure the power state during WFI mode
* - Configure the power state during run, low power run and
* WFE modes (should be executed from internal RAM)
*
* 2. FLASH program and Data EEPROM memories Programming functions:
* this group includes all needed functions to erase and program
* the FLASH program or the Data EEPROM memory.
* - Lock and Unlock the Flash program interface.
* - Lock and Unlock the Data EEPROM interface.
* - Erase function: Erase Byte, Erase Word and Erase Block
* (should be executed from internal RAM).
* - Program functions: Program Byte, Program Word,
* Program Block (should be executed from internal RAM)
* and Fast Program Block (should be executed from internal
* RAM).
*
* 3. FLASH Option Bytes Programming functions: this group includes
* all needed functions to:
* - Program/erase the user option Bytes
* - Get the Read Out Protection status (ROP option byte)
* - Get the User Boot Code size (UBC option byte)
* - Get the Protected Code size (PCODE option byte)
*
* Note: Option byte programming is very similar to data EEPROM byte
* programming.
*
* 4. FLASH Interrupts and flag management functions: this group
* includes all needed functions to:
* - Enable/Disable the flash interrupt sources
* - Get flags status
* - Wait for last flash operation(can be executed from
* internal RAM)
*
* 5. Functions to be executed from RAM: this group includes the functions
* that should be executed from RAM and provides description on how
* to handle this with the different supported toolchains
*
* The table below lists the functions that can be executed from RAM.
*
* +--------------------------------------------------------------------------------|
* | Functions prototypes | RAM execution | Comments |
* ---------------------------------------------------------------------------------|
* | | Mandatory in case of block | Can be executed |
* | FLASH_WaitForLastOperation | Operation: | from Flash in case |
* | | - Block programming | of byte and word |
* | | - Block erase | Operations |
* |--------------------------------------------------------------------------------|
* | FLASH_PowerRunModeConfig | Exclusively | useless from Flash |
* |--------------------------------------------------------------------------------|
* | FLASH_GetPowerStatus | Exclusively | useless from Flash |
* |--------------------------------------------------------------------------------|
* | FLASH_ProgramBlock | Exclusively | useless from Flash |
* |--------------------------------------------------------------------------------|
* | FLASH_EraseBlock | Exclusively | useless from Flash |
* |--------------------------------------------------------------------------------|
*
* To be able to execute functions from RAM several steps have to be followed.
* These steps may differ from one toolchain to another.
* A detailed description is available below within this driver.
* You can also refer to the Flash_DataProgram example provided within the
* STM8L15x_StdPeriph_Lib package.
*
* @endverbatim
*
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm8l15x_flash.h"
/** @addtogroup STM8L15x_StdPeriph_Driver
* @{
*/
/** @defgroup FLASH
* @brief FLASH driver modules
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/** @defgroup FLASH_Private_Define
* @{
*/
#define FLASH_CLEAR_BYTE ((uint8_t)0x00)
#define FLASH_SET_BYTE ((uint8_t)0xFF)
#define OPERATION_TIMEOUT ((uint16_t)0xFFFF)
/**
* @}
*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/** @defgroup FLASH_Private_Functions
* @{
*/
/** @defgroup FLASH_Group1 FLASH program and Data EEPROM memories Interface
* configuration functions
* @brief FLASH Interface configuration functions
*
@verbatim
===============================================================================
FLASH program and Data EEPROM memories interface configuration functions
===============================================================================
The FLASH program and Data EEPROM memories interface configuration functions,
includes the following functions:
- FLASH_ProgramTime_TypeDef FLASH_GetProgrammingTime(void);
- void FLASH_SetProgrammingTime(FLASH_ProgramTime_TypeDef FLASH_ProgTime);
- void FLASH_PowerWaitModeConfig(FLASH_Power_TypeDef FLASH_Power);
These functions don't need the unlock sequence.
@endverbatim
* @{
*/
/**
* @brief Sets the fixed programming time
* @param FLASH_ProgTime : Indicates the programming time to be fixed
* This parameter can be one of the following values:
* @arg FLASH_ProgramTime_Standard: Standard programming time fixed at 1/2 tprog
* @arg FLASH_ProgramTime_TProg: Programming time fixed at tprog
* @retval None
*/
void FLASH_SetProgrammingTime(FLASH_ProgramTime_TypeDef FLASH_ProgTime)
{
/* Check parameter */
assert_param(IS_FLASH_PROGRAM_TIME(FLASH_ProgTime));
FLASH->CR1 &= (uint8_t)(~FLASH_CR1_FIX);
FLASH->CR1 |= (uint8_t)FLASH_ProgTime;
}
/**
* @brief Returns the fixed programming time
* @param None
* @retval Fixed programming time value
*/
FLASH_ProgramTime_TypeDef FLASH_GetProgrammingTime(void)
{
return((FLASH_ProgramTime_TypeDef)(FLASH->CR1 & FLASH_CR1_FIX));
}
/**
* @brief Configures the power state for Flash program and data EEPROM during
* wait for interrupt mode
* @param FLASH_Power: The power state for Flash program and data EEPROM during
* wait for interrupt mode
* This parameter can be one of the following values:
* @arg FLASH_Power_IDDQ: Flash program and data EEPROM in IDDQ
* @arg FLASH_Power_On: Flash program and data EEPROM not in IDDQ
* @retval None
*/
void FLASH_PowerWaitModeConfig(FLASH_Power_TypeDef FLASH_Power)
{
/* Check parameter */
assert_param(IS_FLASH_POWER(FLASH_Power));
/* Flash program and data EEPROM in IDDQ during wait for interrupt mode*/
if (FLASH_Power != FLASH_Power_On)
{
FLASH->CR1 |= (uint8_t)FLASH_CR1_WAITM;
}
/* Flash program and data EEPROM not in IDDQ during wait for interrupt mode*/
else
{
FLASH->CR1 &= (uint8_t)(~FLASH_CR1_WAITM);
}
}
/**
* @}
*/
/** @defgroup FLASH_Group2 FLASH Memory Programming functions
* @brief FLASH Memory Programming functions
*
@verbatim
===============================================================================
FLASH Memory Programming functions
===============================================================================
The FLASH Memory Programming functions, includes the following functions:
- void FLASH_DeInit(void);
- void FLASH_Unlock(FLASH_MemType_TypeDef FLASH_MemType);
- void FLASH_Lock(FLASH_MemType_TypeDef FLASH_MemType);
- void FLASH_ProgramByte(uint32_t Address, uint8_t Data);
- void FLASH_EraseByte(uint32_t Address);
- void FLASH_ProgramWord(uint32_t Address, uint32_t Data);
- uint8_t FLASH_ReadByte(uint32_t Address);
Any operation of erase or program should follow these steps:
1. Call the FLASH_Unlock(FLASH_MemType) function to enable the memory access
2. Call the desired function to erase or program data
3. Call the FLASH_Lock() function to disable the memory access
(it is recommended to protect the FLASH memory against possible unwanted operation)
@endverbatim
* @{
*/
/**
* @brief Deinitializes the FLASH registers to their default reset values.
* @param None
* @retval None
*/
void FLASH_DeInit(void)
{
FLASH->CR1 = FLASH_CR1_RESET_VALUE;
FLASH->CR2 = FLASH_CR2_RESET_VALUE;
FLASH->IAPSR = FLASH_IAPSR_RESET_VALUE;
(void) FLASH->IAPSR; /* Reading of this register causes the clearing of status flags */
}
/**
* @brief Unlocks the program or data EEPROM memory
* @param FLASH_MemType : Memory type to unlock
* This parameter can be one of the following values:
* @arg FLASH_MemType_Program: Program memory
* @arg FLASH_MemType_Data: Data EEPROM memory
* @retval None
*/
void FLASH_Unlock(FLASH_MemType_TypeDef FLASH_MemType)
{
/* Check parameter */
assert_param(IS_FLASH_MEMORY_TYPE(FLASH_MemType));
/* Unlock program memory */
if (FLASH_MemType == FLASH_MemType_Program)
{
FLASH->PUKR = FLASH_RASS_KEY1;
FLASH->PUKR = FLASH_RASS_KEY2;
}
/* Unlock data memory */
if (FLASH_MemType == FLASH_MemType_Data)
{
FLASH->DUKR = FLASH_RASS_KEY2; /* Warning: keys are reversed on data memory !!! */
FLASH->DUKR = FLASH_RASS_KEY1;
}
}
/**
* @brief Locks the program or data EEPROM memory
* @param FLASH_MemType : Memory type
* This parameter can be one of the following values:
* @arg FLASH_MemType_Program: Program memory
* @arg FLASH_MemType_Data: Data EEPROM memory
* @retval None
*/
void FLASH_Lock(FLASH_MemType_TypeDef FLASH_MemType)
{
/* Check parameter */
assert_param(IS_FLASH_MEMORY_TYPE(FLASH_MemType));
/* Lock memory */
FLASH->IAPSR &= (uint8_t)FLASH_MemType;
}
/**
* @brief Programs one byte in program or data EEPROM memory
* @param Address : Address where the byte will be programmed
* @param Data : Value to be programmed
* @retval None
*/
void FLASH_ProgramByte(uint32_t Address, uint8_t Data)
{
/* Check parameters */
assert_param(IS_FLASH_ADDRESS(Address));
*(PointerAttr uint8_t*) (uint16_t)Address = Data;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -