📄 stm32l1xx_flash.c
字号:
* is the start address of a page (multiple of 256 bytes).
* @retval FLASH Status: The returned value can be:
* FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
*/
FLASH_Status FLASH_ErasePage(uint32_t Page_Address)
{
FLASH_Status status = FLASH_COMPLETE;
/* Check the parameters */
assert_param(IS_FLASH_PROGRAM_ADDRESS(Page_Address));
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
if(status == FLASH_COMPLETE)
{
/* If the previous operation is completed, proceed to erase the page */
/* Set the ERASE bit */
FLASH->PECR |= FLASH_PECR_ERASE;
/* Set PROG bit */
FLASH->PECR |= FLASH_PECR_PROG;
/* Write 00000000h to the first word of the program page to erase */
*(__IO uint32_t *)Page_Address = 0x00000000;
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
/* If the erase operation is completed, disable the ERASE and PROG bits */
FLASH->PECR &= (uint32_t)(~FLASH_PECR_PROG);
FLASH->PECR &= (uint32_t)(~FLASH_PECR_ERASE);
}
/* Return the Erase Status */
return status;
}
/**
* @brief Programs a word at a specified address 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 Address: specifies the address to be written.
* @param Data: specifies the data to be written.
* @retval FLASH Status: The returned value can be:
* FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
*/
FLASH_Status FLASH_FastProgramWord(uint32_t Address, uint32_t Data)
{
FLASH_Status status = FLASH_COMPLETE;
/* Check the parameters */
assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
if(status == FLASH_COMPLETE)
{
/* If the previous operation is completed, proceed to program the new word */
*(__IO uint32_t *)Address = Data;
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
}
/* Return the Write Status */
return status;
}
/**
* @}
*/
/** @defgroup FLASH_Group3 DATA EEPROM Programming functions
* @brief DATA EEPROM Programming functions
*
@verbatim
===============================================================================
DATA EEPROM Programming functions
===============================================================================
The DATA_EEPROM Programming_Functions, includes the following functions:
- void DATA_EEPROM_Unlock(void);
- void DATA_EEPROM_Lock(void);
- FLASH_Status DATA_EEPROM_EraseWord(uint32_t Address);
- FLASH_Status DATA_EEPROM_FastProgramByte(uint32_t Address, uint8_t Data);
- FLASH_Status DATA_EEPROM_FastProgramHalfWord(uint32_t Address, uint16_t Data);
- FLASH_Status DATA_EEPROM_FastProgramWord(uint32_t Address, uint32_t Data);
- FLASH_Status DATA_EEPROM_ProgramByte(uint32_t Address, uint8_t Data);
- FLASH_Status DATA_EEPROM_ProgramHalfWord(uint32_t Address, uint16_t Data);
- FLASH_Status DATA_EEPROM_ProgramWord(uint32_t Address, uint32_t Data);
Any operation of erase or program should follow these steps:
1. Call the DATA_EEPROM_Unlock() function to enable the data EEPROM access
and Flash program erase control register access.
2. Call the desired function to erase or program data
3. Call the DATA_EEPROM_Lock() to disable the data EEPROM access
and Flash program erase control register access(recommended
to protect the DATA_EEPROM against possible unwanted operation)
@endverbatim
* @{
*/
/**
* @brief Unlocks the data memory and FLASH_PECR register access.
* @param None
* @retval None
*/
void DATA_EEPROM_Unlock(void)
{
if((FLASH->PECR & FLASH_PECR_PELOCK) != RESET)
{
/* Unlocking the Data memory and FLASH_PECR register access*/
FLASH->PEKEYR = FLASH_PEKEY1;
FLASH->PEKEYR = FLASH_PEKEY2;
}
}
/**
* @brief Locks the Data memory and FLASH_PECR register access.
* @param None
* @retval None
*/
void DATA_EEPROM_Lock(void)
{
/* Set the PELOCK Bit to lock the data memory and FLASH_PECR register access */
FLASH->PECR |= FLASH_PECR_PELOCK;
}
/**
* @brief Enables or disables DATA EEPROM fixed Time programming (2*Tprog).
* @param NewState: new state of the DATA EEPROM fixed Time programming mode.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void DATA_EEPROM_FixedTimeProgramCmd(FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
if(NewState != DISABLE)
{
FLASH->PECR |= (uint32_t)FLASH_PECR_FTDW;
}
else
{
FLASH->PECR &= (uint32_t)(~((uint32_t)FLASH_PECR_FTDW));
}
}
/**
* @brief Erase a word in data memory.
* @param Address: specifies the address to be erased
* @note1 - A data memory word is erased in the data memory only if the address
* to load is the start address of a word (multiple of a word).
* @note2 - To correctly run this function, the DATA_EEPROM_Unlock() function
* must be called before.
* - Call the DATA_EEPROM_Lock() to he data EEPROM access
* and Flash program erase control register access(recommended to protect
* the DATA_EEPROM against possible unwanted operation)
* @retval FLASH Status: The returned value can be:
* FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
*/
FLASH_Status DATA_EEPROM_EraseWord(uint32_t Address)
{
FLASH_Status status = FLASH_COMPLETE;
/* Check the parameters */
assert_param(IS_FLASH_DATA_ADDRESS(Address));
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
if(status == FLASH_COMPLETE)
{
/* Write "00000000h" to valid address in the data memory" */
*(__IO uint32_t *) Address = 0x00000000;
}
/* Return the erase status */
return status;
}
/**
* @brief Write a Byte at a specified address in data memory.
* @note - To correctly run this function, the DATA_EEPROM_Unlock() function
* must be called before.
* - Call the DATA_EEPROM_Lock() to he data EEPROM access
* and Flash program erase control register access(recommended to protect
* the DATA_EEPROM against possible unwanted operation)
* @param Address: specifies the address to be written.
* @param Data: specifies the data to be written.
* @note This function assumes that the is data word is already erased.
* @retval FLASH Status: The returned value can be:
* FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
*/
FLASH_Status DATA_EEPROM_FastProgramByte(uint32_t Address, uint8_t Data)
{
FLASH_Status status = FLASH_COMPLETE;
uint32_t tmp = 0, tmpaddr = 0;
/* Check the parameters */
assert_param(IS_FLASH_DATA_ADDRESS(Address));
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
if(status == FLASH_COMPLETE)
{
/* Clear the FTDW bit */
FLASH->PECR &= (uint32_t)(~((uint32_t)FLASH_PECR_FTDW));
if(Data != (uint8_t)0x00)
{
/* If the previous operation is completed, proceed to write the new Data */
*(__IO uint8_t *)Address = Data;
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
}
else
{
tmpaddr = Address & 0xFFFFFFFC;
tmp = * (__IO uint32_t *) tmpaddr;
tmpaddr = 0xFF << ((uint32_t) (0x8 * (Address & 0x3)));
tmp &= ~tmpaddr;
status = DATA_EEPROM_EraseWord(Address & 0xFFFFFFFC);
status = DATA_EEPROM_FastProgramWord((Address & 0xFFFFFFFC), tmp);
}
}
/* Return the Write Status */
return status;
}
/**
* @brief Writes a half word at a specified address in data memory.
* @note - To correctly run this function, the DATA_EEPROM_Unlock() function
* must be called before.
* - Call the DATA_EEPROM_Lock() to he data EEPROM access
* and Flash program erase control register access(recommended to protect
* the DATA_EEPROM against possible unwanted operation)
* @param Address: specifies the address to be written.
* @param Data: specifies the data to be written.
* @note This function assumes that the is data word is already erased.
* @retval FLASH Status: The returned value can be:
* FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
*/
FLASH_Status DATA_EEPROM_FastProgramHalfWord(uint32_t Address, uint16_t Data)
{
FLASH_Status status = FLASH_COMPLETE;
uint32_t tmp = 0, tmpaddr = 0;
/* Check the parameters */
assert_param(IS_FLASH_DATA_ADDRESS(Address));
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
if(status == FLASH_COMPLETE)
{
/* Clear the FTDW bit */
FLASH->PECR &= (uint32_t)(~((uint32_t)FLASH_PECR_FTDW));
if(Data != (uint16_t)0x0000)
{
/* If the previous operation is completed, proceed to write the new data */
*(__IO uint16_t *)Address = Data;
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
}
else
{
if((Address & 0x3) != 0x3)
{
tmpaddr = Address & 0xFFFFFFFC;
tmp = * (__IO uint32_t *) tmpaddr;
tmpaddr = 0xFFFF << ((uint32_t) (0x8 * (Address & 0x3)));
tmp &= ~tmpaddr;
status = DATA_EEPROM_EraseWord(Address & 0xFFFFFFFC);
status = DATA_EEPROM_FastProgramWord((Address & 0xFFFFFFFC), tmp);
}
else
{
DATA_EEPROM_FastProgramByte(Address, 0x00);
DATA_EEPROM_FastProgramByte(Address + 1, 0x00);
}
}
}
/* Return the Write Status */
return status;
}
/**
* @brief Programs a word at a specified address in data memory.
* @note - To correctly run this function, the DATA_EEPROM_Unlock() function
* must be called before.
* - Call the DATA_EEPROM_Lock() to the data EEPROM access
* and Flash program erase control register access(recommended to protect
* the DATA_EEPROM against possible unwanted operation)
* @param Address: specifies the address to be written.
* @param Data: specifies the data to be written.
* @note This function assumes that the is data word is already erased.
* @retval FLASH Status: The returned value can be:
* FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
*/
FLASH_Status DATA_EEPROM_FastProgramWord(uint32_t Address, uint32_t Data)
{
FLASH_Status status = FLASH_COMPLETE;
/* Check the parameters */
assert_param(IS_FLASH_DATA_ADDRESS(Address));
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
if(status == FLASH_COMPLETE)
{
/* Clear the FTDW bit */
FLASH->PECR &= (uint32_t)(~((uint32_t)FLASH_PECR_FTDW));
/* If the previous operation is completed, proceed to program the new data */
*(__IO uint32_t *)Address = Data;
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
}
/* Return the Write Status */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -