?? stm32l1xx_flash.c
字號(hào):
/**
******************************************************************************
* @file stm32l1xx_flash.c
* @author MCD Application Team
* @version V1.0.0
* @date 31-December-2010
* @brief This file provides all the Flash firmware functions. These functions
* can be executed from Internal FLASH or Internal SRAM memories.
* The functions that should be called from SRAM are defined inside
* the "stm32l1xx_flash_ramfunc.c" file.
* This file provides firmware functions to manage the following
* functionalities of the FLASH peripheral:
* - FLASH Interface configuration
* - FLASH Memory Programming
* - DATA EEPROM Programming
* - Option Bytes Programming
* - Interrupts and flags management
*
* @verbatim
*
* ===================================================================
* How to use this driver
* ===================================================================
*
* This driver provides functions to configure and program the Flash
* memory of all STM32L1xx devices
* These functions are split in 5 groups
*
* 1. FLASH Interface configuration functions: this group includes
* the management of following features:
* - Set the latency
* - Enable/Disable the prefetch buffer
* - Enable/Disable the 64 bit Read Access
* - Enable/Disable the RUN PowerDown mode
* - Enable/Disable the SLEEP PowerDown mode
*
* 2. FLASH Memory Programming functions: this group includes all
* needed functions to erase and program the main memory:
* - Lock and Unlock the Flash interface.
* - Erase function: Erase Page.
* - Program functions: Fast Word and Half Page(should be
* executed from internal SRAM).
*
* 3. DATA EEPROM Programming functions: this group includes all
* needed functions to erase and program the DATA EEPROM memory:
* - Lock and Unlock the DATA EEPROM interface.
* - Erase function: Erase Word, erase Double Word (should be
* executed from internal SRAM).
* - Program functions: Fast Program Byte, Fast Program Half-Word,
* FastProgramWord, Program Byte, Program Half-Word,
* Program Word and Program Double-Word (should be executed
* from internal SRAM).
*
* 4. FLASH Option Bytes Programming functions: this group includes
* all needed functions to:
* - Lock and Unlock the Flash Option bytes.
* - Set/Reset the write protection
* - Set the Read protection Level
* - Set the BOR level
* - Program the user option Bytes
* - Launch the Option Bytes loader
* - Get the Write protection
* - Get the read protection status
* - Get the BOR level
* - Get the user option bytes
*
* 5. FLASH Interrupts and flag management functions: this group
* includes all needed functions to:
* - Enable/Disable the flash interrupt sources
* - Get flags status
* - Clear flags
* - Get Flash operation status
* - Wait for last flash operation
*
* @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 2010 STMicroelectronics</center></h2>
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32l1xx_flash.h"
/** @addtogroup STM32L1xx_StdPeriph_Driver
* @{
*/
/** @defgroup FLASH
* @brief FLASH driver modules
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* FLASH Mask */
#define RDPRT_MASK ((uint32_t)0x00000002)
#define WRP01_MASK ((uint32_t)0x0000FFFF)
#define WRP23_MASK ((uint32_t)0xFFFF0000)
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/** @defgroup FLASH_Private_Functions
* @{
*/
/** @defgroup FLASH_Group1 FLASH Interface configuration functions
* @brief FLASH Interface configuration functions
*
@verbatim
===============================================================================
FLASH Interface configuration functions
===============================================================================
FLASH_Interface configuration_Functions, includes the following functions:
- void FLASH_SetLatency(uint32_t FLASH_Latency):
To correctly read data from Flash memory, the number of wait states (LATENCY)
must be correctly programmed according to the frequency of the CPU clock
(HCLK) and the supply voltage of the device.
----------------------------------------------------------------
| Wait states | HCLK clock frequency (MHz) |
| |------------------------------------------------|
| (Latency) | voltage range | voltage range |
| | 1.65 V - 3.6 V | 2.0 V - 3.6 V |
| |----------------|---------------|---------------|
| | VCORE = 1.2 V | VCORE = 1.5 V | VCORE = 1.8 V |
|-------------- |----------------|---------------|---------------|
|0WS(1CPU cycle)|0 < HCLK <= 2 |0 < HCLK <= 8 |0 < HCLK <= 16 |
|---------------|----------------|---------------|---------------|
|1WS(2CPU cycle)|2 < HCLK <= 4 |8 < HCLK <= 16 |16 < HCLK <= 32|
----------------------------------------------------------------
- void FLASH_PrefetchBufferCmd(FunctionalState NewState);
- void FLASH_ReadAccess64Cmd(FunctionalState NewState);
- void FLASH_RUNPowerDownCmd(FunctionalState NewState);
- void FLASH_SLEEPPowerDownCmd(FunctionalState NewState);
- void FLASH_ITConfig(uint32_t FLASH_IT, FunctionalState NewState);
Here below the allowed configuration of Latency, 64Bit access and prefetch buffer
--------------------------------------------------------------------------------
| | ACC64 = 0 | ACC64 = 1 |
| Latency |----------------|---------------|---------------|---------------|
| | PRFTEN = 0 | PRFTEN = 1 | PRFTEN = 0 | PRFTEN = 1 |
|---------------|----------------|---------------|---------------|---------------|
|0WS(1CPU cycle)| YES | NO | YES | YES |
|---------------|----------------|---------------|---------------|---------------|
|1WS(2CPU cycle)| NO | NO | YES | YES |
--------------------------------------------------------------------------------
All these functions don't need the unlock sequence.
@endverbatim
* @{
*/
/**
* @brief Sets the code latency value.
* @param FLASH_Latency: specifies the FLASH Latency value.
* This parameter can be one of the following values:
* @arg FLASH_Latency_0: FLASH Zero Latency cycle
* @arg FLASH_Latency_1: FLASH One Latency cycle
* @retval None
*/
void FLASH_SetLatency(uint32_t FLASH_Latency)
{
uint32_t tmpreg = 0;
/* Check the parameters */
assert_param(IS_FLASH_LATENCY(FLASH_Latency));
/* Read the ACR register */
tmpreg = FLASH->ACR;
/* Sets the Latency value */
tmpreg &= (uint32_t) (~((uint32_t)FLASH_ACR_LATENCY));
tmpreg |= FLASH_Latency;
/* Write the ACR register */
FLASH->ACR = tmpreg;
}
/**
* @brief Enables or disables the Prefetch Buffer.
* @param NewState: new state of the FLASH prefetch buffer.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void FLASH_PrefetchBufferCmd(FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
if(NewState != DISABLE)
{
FLASH->ACR |= FLASH_ACR_PRFTEN;
}
else
{
FLASH->ACR &= (uint32_t)(~((uint32_t)FLASH_ACR_PRFTEN));
}
}
/**
* @brief Enables or disables read access to flash by 64 bits.
* @param NewState: new state of the FLASH read access mode.
* This parameter can be: ENABLE or DISABLE.
* @note - If this bit is set, the Read access 64 bit is used.
* - If this bit is reset, the Read access 32 bit is used.
* @note - This bit cannot be written at the same time as the LATENCY and
* PRFTEN bits.
* - To reset this bit, the LATENCY should be zero wait state and the
* prefetch off.
* @retval None
*/
void FLASH_ReadAccess64Cmd(FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
if(NewState != DISABLE)
{
FLASH->ACR |= FLASH_ACR_ACC64;
}
else
{
FLASH->ACR &= (uint32_t)(~((uint32_t)FLASH_ACR_ACC64));
}
}
/**
* @brief Enable or disable the power down mode during Sleep mode.
* @note This function is used to power down the FLASH when the system is in SLEEP LP mode.
* @param NewState: new state of the power down mode during sleep mode.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void FLASH_SLEEPPowerDownCmd(FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Set the SLEEP_PD bit to put Flash in power down mode during sleep mode */
FLASH->ACR |= FLASH_ACR_SLEEP_PD;
}
else
{
/* Clear the SLEEP_PD bit in to put Flash in idle mode during sleep mode */
FLASH->ACR &= (uint32_t)(~((uint32_t)FLASH_ACR_SLEEP_PD));
}
}
/**
* @}
*/
/** @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_Unlock(void);
- void FLASH_Lock(void);
- FLASH_Status FLASH_ErasePage(uint32_t Page_Address);
- FLASH_Status FLASH_FastProgramWord(uint32_t Address, uint32_t Data);
Any operation of erase or program should follow these steps:
1. Call the FLASH_Unlock() function to enable the flash control register and
program memory access
2. Call the desired function to erase page or program data
3. Call the FLASH_Lock() to disable the flash program memory access
(recommended to protect the FLASH memory against possible unwanted operation)
@endverbatim
* @{
*/
/**
* @brief Unlocks the FLASH control register and program memory access.
* @param None
* @retval None
*/
void FLASH_Unlock(void)
{
if((FLASH->PECR & FLASH_PECR_PRGLOCK) != RESET)
{
/* Unlocking the data memory and FLASH_PECR register access */
DATA_EEPROM_Unlock();
/* Unlocking the program memory access */
FLASH->PRGKEYR = FLASH_PRGKEY1;
FLASH->PRGKEYR = FLASH_PRGKEY2;
}
}
/**
* @brief Locks the Program memory access.
* @param None
* @retval None
*/
void FLASH_Lock(void)
{
/* Set the PRGLOCK Bit to lock the program memory access */
FLASH->PECR |= FLASH_PECR_PRGLOCK;
}
/**
* @brief Erases a specified page in program memory.
* @note - To correctly run this function, the FLASH_Unlock() function
* must be called before.
* - Call the FLASH_Lock() to disable the flash memory access
* (recommended to protect the FLASH memory against possible unwanted operation)
* @param Page_Address: The page address in program memory to be erased.
* @note A Page is erased in the Program memory only if the address to load
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -