📄 stm8s_flash.c
字号:
FLASH->CR1 |= (u8)LPMode; /* Sets the new mode */
}
/**
* @brief Sets the fixed programming time
* @param[in] ProgTime Indicates the programming time to be fixed
* This parameter can be any of the @ref FLASH_ProgramTime_TypeDef values.
* @retval None
*/
void FLASH_SetProgrammingTime(FLASH_ProgramTime_TypeDef ProgTime)
{
/* Check parameter */
assert_param(IS_FLASH_PROGRAM_TIME_OK(ProgTime));
FLASH->CR1 &= (u8)(~FLASH_CR1_FIX);
FLASH->CR1 |= (u8)ProgTime;
}
/**
* @brief Returns the Flash behaviour type in low power mode
* @par Parameters:
* None
* @retval FLASH_LPMode_TypeDef Flash behaviour type in low power mode
*/
FLASH_LPMode_TypeDef FLASH_GetLowPowerMode(void)
{
return((FLASH_LPMode_TypeDef)(FLASH->CR1 & (FLASH_CR1_HALT | FLASH_CR1_AHALT)));
}
/**
* @brief Returns the fixed programming time
* @par Parameters:
* None
* @retval FLASH_ProgramTime_TypeDef Fixed programming time value
*/
FLASH_ProgramTime_TypeDef FLASH_GetProgrammingTime(void)
{
return((FLASH_ProgramTime_TypeDef)(FLASH->CR1 & FLASH_CR1_FIX));
}
/**
* @brief Returns the Boot memory size in bytes
* @par Parameters:
* None
* @retval u32 Boot memory size in bytes
*/
u32 FLASH_GetBootSize(void)
{
u32 temp = 0;
/* Calculates the number of bytes */
temp = (u32)((u32)FLASH->FPR * (u32)512);
/* Correction because size of 127.5 kb doesn't exist */
if (FLASH->FPR == 0xFF)
{
temp += 512;
}
/* Return value */
return(temp);
}
/**
* @brief Checks whether the specified SPI flag is set or not.
* @param[in] FLASH_FLAG : Specifies the flag to check.
* This parameter can be any of the @ref FLASH_Flag_TypeDef enumeration.
* @retval FlagStatus : Indicates the state of FLASH_FLAG.
* This parameter can be any of the @ref FlagStatus enumeration.
* @par Required preconditions:
* This function clears the EOP, WR_PG_DIS flags in the IAPSR register.
*/
FlagStatus FLASH_GetFlagStatus(FLASH_Flag_TypeDef FLASH_FLAG)
{
FlagStatus status = RESET;
/* Check parameters */
assert_param(IS_FLASH_FLAGS_OK(FLASH_FLAG));
/* Check the status of the specified FLASH flag */
if ((FLASH->IAPSR & (u8)FLASH_FLAG) != (u8)RESET)
{
status = SET; /* FLASH_FLAG is set */
}
else
{
status = RESET; /* FLASH_FLAG is reset*/
}
/* Return the FLASH_FLAG status */
return status;
}
/**
* @brief Wait for a Flash operation to complete.
* @par Parameters:
* @param[in] MemType Memory type
* @retval FLASH_Status_TypeDef State of the last operation
* @par Required preconditions:
* The FLASH_WaitForLastOperation function should be executed from RAM.
* This function return the FLASH status and clears the EOP, WR_PG_DIS flags in the IAPSR register.
*/
FLASH_Status_TypeDef FLASH_WaitForLastOperation(FLASH_MemType_TypeDef MemType)
{
u8 flagstatus = 0x00;
u16 timeout = OPERATION_TIMEOUT;
/* Wait until operation completion or write protected page occured */
#if defined (STM8S208) || defined(STM8S207) || defined(STM8S105)
if (MemType == FLASH_MEMTYPE_PROG)
{
while ((flagstatus == 0x00) && (timeout != 0x00))
{
flagstatus = (u8)(FLASH->IAPSR & (FLASH_IAPSR_EOP |
FLASH_IAPSR_WR_PG_DIS));
timeout--;
}
}
else
{
while ((flagstatus == 0x00) && (timeout != 0x00))
{
flagstatus = (u8)(FLASH->IAPSR & (FLASH_IAPSR_HVOFF |
FLASH_IAPSR_WR_PG_DIS));
timeout--;
}
}
#else /*STM8S103, STM8S903*/
while ((flagstatus == 0x00) && (timeout != 0x00))
{
flagstatus = (u8)(FLASH->IAPSR & (FLASH_IAPSR_EOP | FLASH_IAPSR_WR_PG_DIS));
timeout--;
}
#endif /* STM8S208, STM8S207, STM8S105 */
if (timeout == 0x00 )
{
flagstatus = FLASH_STATUS_TIMEOUT;
}
return((FLASH_Status_TypeDef)flagstatus);
}
/* *****************************************************************************************/
/* Uncomment the line below to use these functions */
/* */
/* *****************************************************************************************/
/* LINKER SECTIONS DEFINITION FOR THIS FILE ONLY */
#ifdef USE_COSMIC_SECTIONS
#pragma section (FLASH_CODE)
#pragma section const {FLASH_CONST}
#pragma section @near [FLASH_URAM]
#pragma section @near {FLASH_IRAM}
#pragma section @tiny [FLASH_UZRAM]
#pragma section @tiny {FLASH_IZRAM}
#endif
/**
* @brief Erases a block in the program or data memory.
* @param[in] MemType Memory type
* @param[in] BlockNum Indicates the block number to erase
* @retval
* None.
* @par Required preconditions:
* The FLASH_EraseBlock function should be executed from RAM.
* 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_EraseBlock(u16 BlockNum, FLASH_MemType_TypeDef MemType)
{
u16 timeout = OPERATION_TIMEOUT;
#ifdef PointerAttr_Far
u32 StartAddress = 0;
#else /* PointerAttr_Near */
PointerAttr u32 *pwFlash;
u32 StartAddress = 0;
#endif /*PointerAttr_Far*/
/* Check parameters */
assert_param(IS_MEMORY_TYPE_OK(MemType));
if (MemType == FLASH_MEMTYPE_PROG)
{
assert_param(IS_FLASH_PROG_BLOCK_NUMBER_OK(BlockNum));
StartAddress = FLASH_PROG_START_PHYSICAL_ADDRESS;
}
else
{
assert_param(IS_FLASH_DATA_BLOCK_NUMBER_OK(BlockNum));
StartAddress = FLASH_DATA_START_PHYSICAL_ADDRESS;
}
#ifdef PointerAttr_Far
/* Point to the first block address */
StartAddress = StartAddress + ((u32)BlockNum * FLASH_BLOCK_SIZE);
/* Enable erase block mode */
FLASH->CR2 |= FLASH_CR2_ERASE;
FLASH->NCR2 &= (u8)(~FLASH_NCR2_NERASE);
*((PointerAttr u8*) StartAddress) = FLASH_CLEAR_BYTE;
*((PointerAttr u8*) StartAddress + 1) = FLASH_CLEAR_BYTE;
*((PointerAttr u8*) StartAddress + 2) = FLASH_CLEAR_BYTE;
*((PointerAttr u8*) StartAddress + 3) = FLASH_CLEAR_BYTE;
#else /*PointerAttr_Near*/
/* Point to the first block address */
pwFlash = (PointerAttr u32 *)(StartAddress + ((u32)BlockNum * FLASH_BLOCK_SIZE));
/* Enable erase block mode */
FLASH->CR2 |= FLASH_CR2_ERASE;
FLASH->NCR2 &= (u8)(~FLASH_NCR2_NERASE);
*pwFlash = (u32)0;
#endif /*PointerAttr_Far*/
#if defined (STM8S208) || defined(STM8S207) || defined(STM8S105)
/* Waiting until High voltage flag is cleared*/
while ((FLASH->IAPSR & FLASH_IAPSR_HVOFF) != 0x00 || (timeout == 0x00))
{
timeout--;
}
#endif /* STM8S208, STM8S207, STM8S105 */
}
/**
*@brief Programs a memory block
* @param[in] MemType The type of memory to program
* @param[in] BlockNum The block number
* @param[in] ProgMode The programming mode.
* @param[in] Buffer The buffer address of source data.
* @retval
* None.
* @par Required preconditions:
* The FLASH_ProgramBlock function should be executed from RAM.
* 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_ProgramBlock(u16 BlockNum, FLASH_MemType_TypeDef MemType, FLASH_ProgramMode_TypeDef ProgMode, u8 *Buffer)
{
u16 Count = 0;
u32 StartAddress = 0;
u16 timeout = OPERATION_TIMEOUT;
/* Check parameters */
assert_param(IS_MEMORY_TYPE_OK(MemType));
assert_param(IS_FLASH_PROGRAM_MODE_OK(ProgMode));
if (MemType == FLASH_MEMTYPE_PROG)
{
assert_param(IS_FLASH_PROG_BLOCK_NUMBER_OK(BlockNum));
StartAddress = FLASH_PROG_START_PHYSICAL_ADDRESS;
}
else
{
assert_param(IS_FLASH_DATA_BLOCK_NUMBER_OK(BlockNum));
StartAddress = FLASH_DATA_START_PHYSICAL_ADDRESS;
}
/* Point to the first block address */
StartAddress = StartAddress + ((u32)BlockNum * FLASH_BLOCK_SIZE);
/* Selection of Standard or Fast programming mode */
if (ProgMode == FLASH_PROGRAMMODE_STANDARD)
{
/* Standard programming mode */ /*No need in standard mode */
FLASH->CR2 |= FLASH_CR2_PRG;
FLASH->NCR2 &= (u8)(~FLASH_NCR2_NPRG);
}
else
{
/* Fast programming mode */
FLASH->CR2 |= FLASH_CR2_FPRG;
FLASH->NCR2 &= (u8)(~FLASH_NCR2_NFPRG);
}
/* Copy data bytes from RAM to FLASH memory */
for (Count = 0; Count < FLASH_BLOCK_SIZE; Count++)
{
*((PointerAttr u8*)StartAddress + Count) = ((u8)(Buffer[Count]));
}
#if defined (STM8S208) || defined(STM8S207) || defined(STM8S105)
if (MemType == FLASH_MEMTYPE_DATA)
{
/* Waiting until High voltage flag is cleared*/
while ((FLASH->IAPSR & FLASH_IAPSR_HVOFF) != 0x00 || (timeout == 0x00))
{
timeout--;
}
}
#endif /* STM8S208, STM8S207, STM8S105 */
}
/**
* @}
*/
/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -