📄 stm8s_flash.c
字号:
/**
******************************************************************************
* @file stm8s_flash.c
* @brief This file contains all the functions for the FLASH peripheral.
* @author STMicroelectronics - MCD Application Team
* @version V1.1.1
* @date 06/05/2009
******************************************************************************
*
* 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 2009 STMicroelectronics</center></h2>
* @image html logo.bmp
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm8s_flash.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define FLASH_CLEAR_BYTE (u8)0x00;
#define FLASH_SET_BYTE (u8)0xFF;
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
#define OPERATION_TIMEOUT ((u16)0x1000)
/* Private function prototypes -----------------------------------------------*/
/* Private Constants ---------------------------------------------------------*/
/** @addtogroup FLASH_Public_functions
* @{
*/
/**
* @brief Unlocks the program or data EEPROM memory
* @param[in] MemType Memory type to unlock
* This parameter can be any of the @ref FLASH_MemType_TypeDef values.
* @retval None
*/
void FLASH_Unlock(FLASH_MemType_TypeDef MemType)
{
/* Check parameter */
assert_param(IS_MEMORY_TYPE_OK(MemType));
/* Unlock program memory */
if (MemType == FLASH_MEMTYPE_PROG)
{
FLASH->PUKR = FLASH_RASS_KEY1;
FLASH->PUKR = FLASH_RASS_KEY2;
}
/* Unlock data memory */
else
{
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[in] MemType Memory type
* @retval
* None.
*/
void FLASH_Lock(FLASH_MemType_TypeDef MemType)
{
/* Check parameter */
assert_param(IS_MEMORY_TYPE_OK(MemType));
/* Lock program memory */
if (MemType == FLASH_MEMTYPE_PROG)
{
FLASH->IAPSR = (u8)(~FLASH_IAPSR_PUL);
}
/* Lock data memory */
else
{
FLASH->IAPSR = (u8)(~FLASH_IAPSR_DUL);
}
}
/**
* @brief Deinitializes the FLASH peripheral registers to their default reset values.
* @par Parameters:
* None.
* @retval None
*/
void FLASH_DeInit(void)
{
u8 temp = 0;
FLASH->CR1 = FLASH_CR1_RESET_VALUE;
FLASH->CR2 = FLASH_CR2_RESET_VALUE;
FLASH->NCR2 = FLASH_NCR2_RESET_VALUE;
FLASH->IAPSR &= (u8)(~FLASH_IAPSR_DUL);
FLASH->IAPSR &= (u8)(~FLASH_IAPSR_PUL);
temp = FLASH->IAPSR; /* Reading of this register causes the clearing of status flags */
}
/**
* @brief Enables or Disables the Flash interrupt mode
* @param[in] NewState The new state of the flash interrupt mode
* @retval None
*/
void FLASH_ITConfig(FunctionalState NewState)
{
if (NewState != DISABLE)
{
FLASH->CR1 |= FLASH_CR1_IE; /* Enables the interrupt sources */
}
else
{
FLASH->CR1 &= (u8)(~FLASH_CR1_IE); /* Disables the interrupt sources */
}
}
/**
* @brief Erases one byte in the program or data EEPROM memory
* @param[in] Address Address of the byte to erase
* @retval
* None.
* @par Required preconditions:
* PointerAttr define is declared in the stm8s.h file to select if pointer will be declared
* as near (2 bytes) or far (3 bytes).
*/
void FLASH_EraseByte(u32 Address)
{
/* Check parameter */
assert_param(IS_FLASH_ADDRESS_OK(Address));
*((PointerAttr u8*) Address) = FLASH_CLEAR_BYTE; /* Erase byte */
}
/**
* @brief Programs one byte in program or data EEPROM memory
* @param[in] Address Adress where the byte is written
* @param[in] Data Value to be written
* @retval
* None.
* @par Required preconditions:
* PointerAttr define is declared in the stm8s.h file to select if pointer will be declared
* as near (2 bytes) or far (3 bytes).
*/
void FLASH_ProgramByte(u32 Address, u8 Data)
{
/* Check parameters */
assert_param(IS_FLASH_ADDRESS_OK(Address));
*((PointerAttr u8*) Address) = Data;
}
/**
* @brief Reads any byte from flash memory
* @param[in] Address Address to read
* @retval u8 Value read
* @par Required preconditions:
* PointerAttr define is declared in the stm8s.h file to select if pointer will be declared
* as near (2 bytes) or far (3 bytes).
*/
u8 FLASH_ReadByte(u32 Address)
{
/* Check parameter */
assert_param(IS_FLASH_ADDRESS_OK(Address));
return(*((PointerAttr u8*) Address)); /* Read byte */
}
/**
* @brief Programs one word (4 bytes) in program or data EEPROM memory
* @param[in] Address Adress where the byte is written
* @param[in] Data Value to be written
* @retval
* None.
* @par Required preconditions:
* PointerAttr define is declared in the stm8s.h file to select if pointer will be declared
* as near (2 bytes) or far (3 bytes).
*/
void FLASH_ProgramWord(u32 Address, u32 Data)
{
/* Check parameters */
assert_param(IS_FLASH_ADDRESS_OK(Address));
/* Enable Word Write Once */
FLASH->CR2 |= FLASH_CR2_WPRG;
FLASH->NCR2 &= (u8)(~FLASH_NCR2_NWPRG);
*((PointerAttr u8*)Address) = *((u8*)(&Data)); /* Write one byte - from lowest address*/
*(((PointerAttr u8*)Address) + 1) = *((u8*)(&Data)+1); /* Write one byte*/
*(((PointerAttr u8*)Address) + 2) = *((u8*)(&Data)+2); /* Write one byte*/
*(((PointerAttr u8*)Address) + 3) = *((u8*)(&Data)+3); /* Write one byte - from higher address*/
}
/**
* @brief Programs an option byte
* @param[in] Address option byte address to program
* @param[in] Data Value to write
* @retval
* None
*/
void FLASH_ProgramOptionByte(u16 Address, u8 Data)
{
/* Check parameter */
assert_param(IS_OPTION_BYTE_ADDRESS_OK(Address));
/* Enable write access to option bytes */
FLASH->CR2 |= FLASH_CR2_OPT;
FLASH->NCR2 &= (u8)(~FLASH_NCR2_NOPT);
/* Program option byte and his complement */
*((NEAR u8*)Address) = Data;
*((NEAR u8*)(Address + 1)) = (u8)(~Data);
FLASH_WaitForLastOperation(FLASH_MEMTYPE_DATA);
/* Disable write access to option bytes */
FLASH->CR2 &= (u8)(~FLASH_CR2_OPT);
FLASH->NCR2 |= FLASH_NCR2_NOPT;
}
/**
* @brief Erases an option byte
* @param[in] Address Option byte address to erase
* @retval
* None.
*/
void FLASH_EraseOptionByte(u16 Address)
{
/* Check parameter */
assert_param(IS_OPTION_BYTE_ADDRESS_OK(Address));
/* Enable write access to option bytes */
FLASH->CR2 |= FLASH_CR2_OPT;
FLASH->NCR2 &= (u8)(~FLASH_NCR2_NOPT);
/* Erase option byte and his complement */
*((NEAR u8*)Address) = FLASH_CLEAR_BYTE;
*((NEAR u8*)(Address + 1 )) = FLASH_SET_BYTE;
FLASH_WaitForLastOperation(FLASH_MEMTYPE_DATA);
/* Disable write access to option bytes */
FLASH->CR2 &= (u8)(~FLASH_CR2_OPT);
FLASH->NCR2 |= FLASH_NCR2_NOPT;
}
/**
* @brief Reads one option byte
* @param[in] Address option byte address to read.
* @retval u16 res_value (Value read + complement value read.)
*/
u16 FLASH_ReadOptionByte(u16 Address)
{
u8 value_optbyte, value_optbyte_complement = 0;
u16 res_value = 0;
/* Check parameter */
assert_param(IS_OPTION_BYTE_ADDRESS_OK(Address));
value_optbyte = *((NEAR u8*)Address); /* Read option byte */
value_optbyte_complement = *(((NEAR u8*)Address) + 1); /* Read option byte complement*/
if (value_optbyte == (u8)(~value_optbyte_complement))
{
res_value = (u16)((u16)value_optbyte << 8);
res_value = res_value | (u16)value_optbyte_complement;
}
else
{
res_value = FLASH_OPTIONBYTE_ERROR;
}
return(res_value);
}
/**
* @brief Select the Flash behaviour in low power mode
* @param[in] LPMode Low power mode selection
* This parameter can be any of the @ref FLASH_LPMode_TypeDef values.
* @retval None
*/
void FLASH_SetLowPowerMode(FLASH_LPMode_TypeDef LPMode)
{
/* Check parameter */
assert_param(IS_FLASH_LOW_POWER_MODE_OK(LPMode));
FLASH->CR1 &= (u8)(~(FLASH_CR1_HALT | FLASH_CR1_AHALT)); /* Clears the two bits */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -