📄 stm8l15x_flash.c
字号:
/**
* @brief Erases one byte in the program or data EEPROM memory
* @param Address : Address of the byte to erase
* @retval None
*/
void FLASH_EraseByte(uint32_t Address)
{
/* Check parameter */
assert_param(IS_FLASH_ADDRESS(Address));
*(PointerAttr uint8_t*) (uint16_t)Address = FLASH_CLEAR_BYTE; /* Erase byte */
}
/**
* @brief Programs one word (4 bytes) in program or data EEPROM memory
* @param Address : The address where the data will be programmed
* @param Data : Value to be programmed
* @retval None
*/
void FLASH_ProgramWord(uint32_t Address, uint32_t Data)
{
/* Check parameters */
assert_param(IS_FLASH_ADDRESS(Address));
/* Enable Word Write Once */
FLASH->CR2 |= FLASH_CR2_WPRG;
/* Write one byte - from lowest address*/
*((PointerAttr uint8_t*)(uint16_t)Address) = *((uint8_t*)(&Data));
/* Write one byte*/
*(((PointerAttr uint8_t*)(uint16_t)Address) + 1) = *((uint8_t*)(&Data) + 1);
/* Write one byte*/
*(((PointerAttr uint8_t*)(uint16_t)Address) + 2) = *((uint8_t*)(&Data) + 2);
/* Write one byte - from higher address*/
*(((PointerAttr uint8_t*)(uint16_t)Address) + 3) = *((uint8_t*)(&Data) + 3);
}
/**
* @brief Reads one byte from flash memory
* @param Address : Address to read
* @retval Value of the byte
*/
uint8_t FLASH_ReadByte(uint32_t Address)
{
/* Read byte */
return(*(PointerAttr uint8_t *) (uint16_t)Address);
}
/**
* @}
*/
/** @defgroup FLASH_Group3 Option Bytes Programming functions
* @brief Option Bytes Programming functions
*
@verbatim
===============================================================================
Option Bytes Programming functions
===============================================================================
The FLASH_Option Bytes Programming_functions, includes the following functions:
- void FLASH_ProgramOptionByte(uint16_t Address, uint8_t Data);
- void FLASH_EraseOptionByte(uint16_t Address);
- FunctionalState FLASH_GetReadOutProtectionStatus(void);
- uint16_t FLASH_GetBootSize(void);
- uint16_t FLASH_GetCodeSize(void);
Any operation of erase or program should follow these steps:
1. Call the FLASH_Unlock(FLASH_MemType_Data); function to enable the Flash
option control register access
2. Call the desired function to erase or program data
- void FLASH_ProgramOptionByte(uint16_t Address, uint8_t Data); => to program
the option byte Address with the desired Data value.
- void FLASH_EraseOptionByte(uint16_t Address); => to erase the option byte
Address.
3. Once all needed option bytes to be programmed are correctly written, call the
FLASH_Lock(FLASH_MemType_Data) to disable the memory access ( It is recommended to
protect the FLASH memory against possible unwanted operation)
@endverbatim
* @{
*/
/**
* @brief Programs option byte
* @param Address : option byte address to program
* @param Data : Value to write
* @retval None
*/
void FLASH_ProgramOptionByte(uint16_t Address, uint8_t Data)
{
/* Check parameter */
assert_param(IS_OPTION_BYTE_ADDRESS(Address));
/* Enable write access to option bytes */
FLASH->CR2 |= FLASH_CR2_OPT;
/* Program option byte and his complement */
*((PointerAttr uint8_t*)Address) = Data;
FLASH_WaitForLastOperation(FLASH_MemType_Program);
/* Disable write access to option bytes */
FLASH->CR2 &= (uint8_t)(~FLASH_CR2_OPT);
}
/**
* @brief Erases option byte
* @param Address : Option byte address to erase
* @retval None
*/
void FLASH_EraseOptionByte(uint16_t Address)
{
/* Check parameter */
assert_param(IS_OPTION_BYTE_ADDRESS(Address));
/* Enable write access to option bytes */
FLASH->CR2 |= FLASH_CR2_OPT;
/* Erase option byte and his complement */
*((PointerAttr uint8_t*)Address) = FLASH_CLEAR_BYTE;
FLASH_WaitForLastOperation(FLASH_MemType_Program);
/* Disable write access to option bytes */
FLASH->CR2 &= (uint8_t)(~FLASH_CR2_OPT);
}
/**
* @brief Returns the FLASH Read Out Protection Status.
* @param None
* @retval FLASH Read Out Protection Status.
* This parameter can be a ENABLE or DISABLE
*/
FunctionalState FLASH_GetReadOutProtectionStatus(void)
{
FunctionalState state = DISABLE;
if (OPT->ROP == FLASH_READOUTPROTECTION_KEY)
{
/* The status of the Flash read out protection is enabled*/
state = ENABLE;
}
else
{
/* The status of the Flash read out protection is disabled*/
state = DISABLE;
}
return state;
}
/**
* @brief Returns the Boot memory size in bytes
* @param None
* @retval Boot memory size in bytes
*/
uint16_t FLASH_GetBootSize(void)
{
uint16_t temp = 0;
/* Calculates the number of bytes */
temp = (uint16_t)((uint16_t)OPT->UBC * (uint16_t)128);
/* Correction because size upper 8kb doesn't exist */
if (OPT->UBC > 0x7F)
{
temp = 8192;
}
/* Return value */
return(temp);
}
/**
*
* @brief Returns the Code Area size in bytes
* @param None
* @retval Code Area size in bytes
*/
uint16_t FLASH_GetCodeSize(void)
{
uint16_t temp = 0;
/* Calculates the number of bytes */
temp = (uint16_t)((uint16_t)OPT->PCODESIZE * (uint16_t)128);
/* Correction because size upper of 8kb doesn't exist */
if (OPT->PCODESIZE > 0x7F)
{
temp = 8192;
}
/* Return value */
return(temp);
}
/**
* @}
*/
/** @defgroup FLASH_Group4 Interrupts and flags management functions
* @brief Interrupts and flags management functions
*
@verbatim
===============================================================================
Interrupts and flags management functions
===============================================================================
@endverbatim
* @{
*/
/**
* @brief Enables or Disables the Flash interrupt mode
* @param NewState : The new state of the flash interrupt mode
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void FLASH_ITConfig(FunctionalState NewState)
{
/* Check parameter */
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enables the interrupt sources */
FLASH->CR1 |= FLASH_CR1_IE;
}
else
{
/* Disables the interrupt sources */
FLASH->CR1 &= (uint8_t)(~FLASH_CR1_IE);
}
}
/**
* @brief Checks whether the specified FLASH flag is set or not.
* @param FLASH_FLAG : specifies the Flash Flag to check.
* This parameter can be one of the following values:
* @arg FLASH_FLAG_HVOFF: End of high voltage
* @arg FLASH_FLAG_DUL: Data EEPROM unlocked
* @arg FLASH_FLAG_EOP: End of programming (write or erase operation)
* @arg FLASH_FLAG_PUL: Flash Program memory unlocked
* @arg FLASH_FLAG_WR_PG_DIS: Write attempted to protected page
* @retval Indicates the state of the Flash_FLAG.
* This parameter can be SET or RESET
*/
FlagStatus FLASH_GetFlagStatus(FLASH_FLAG_TypeDef FLASH_FLAG)
{
FlagStatus status = RESET;
assert_param(IS_FLASH_FLAGS(FLASH_FLAG));
/* Check the status of the specified flash flag*/
if ((FLASH->IAPSR & (uint8_t)FLASH_FLAG) != (uint8_t)RESET)
{
status = SET; /* Flash_FLAG is set*/
}
else
{
status = RESET; /* Flash_FLAG is reset*/
}
/* Return the Flash_FLAG status*/
return status;
}
/**
* @}
*/
/** @defgroup FLASH_Group5 Functions to be executed from RAM
* @brief Functions to be executed from RAM
*
@verbatim
===============================================================================
Functions to be executed from RAM
===============================================================================
All the functions defined below must be executed from RAM exclusively, except
for the FLASH_WaitForLastOperation function which can be executed from Flash.
Steps of the execution from RAM differs from one toolchain to another:
- For Cosmic Compiler:
1- Define a segment FLASH_CODE by the mean of " #pragma section (FLASH_CODE)".
This segment is defined in the stm8l15x_flash.c file.
2- Uncomment the "#define RAM_EXECUTION (1)" line in the stm8l15x.h file,
or define it in Cosmic compiler preprocessor to enable the FLASH_CODE segment
definition.
3- In STVD Select Project\Settings\Linker\Category "input" and in the RAM section
add the FLASH_CODE segment with "-ic" options.
4- In main.c file call the _fctcpy() function with first segment character as
parameter "_fctcpy('F');" to load the declared moveable code segment
(FLASH_CODE) in RAM before execution.
5- By default the _fctcpy function is packaged in the Cosmic machine library,
so the function prototype "int _fctcopy(char name);" must be added in main.c
file.
- For Raisonance Compiler
1- Use the inram keyword in the function declaration to specify that it can be
executed from RAM.
This is done within the stm8l15x_flash.c file, and it's conditioned by
RAM_EXECUTION definition.
2- Uncomment the "#define RAM_EXECUTION (1)" line in the stm8l15x.h file, or
define it in Raisonance compiler preprocessor to enable the access for the
inram functions.
3- An inram function code is copied from Flash to RAM by the C startup code.
In some applications, the RAM area where the code was initially stored may be
erased or corrupted, so it may be desirable to perform the copy again.
Depending on the application memory model, the memcpy() or fmemcpy() functions
should be used to perform the copy.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -