HAL_FLASHEx_Erase函数代码及解析
时间: 2023-08-07 14:02:51 浏览: 164
STM32 HAL 库延时函数 HAL_Delay 解析
5星 · 资源好评率100%
以下是HAL_FLASHEx_Erase函数的代码实现及解析,代码基于STM32Cube HAL库:
```c
HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *SectorError)
{
uint32_t NbSectorsToErase;
uint32_t SectorErrorTmp;
uint32_t SECTORErrorMask;
HAL_StatusTypeDef status = HAL_OK;
/* Check the parameters */
assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->Banks));
assert_param(IS_FLASH_NBPAGES(pEraseInit->NbPages));
/* Get the number of sectors to erase from 1st sector*/
NbSectorsToErase = (pEraseInit->NbPages * FLASH_PAGE_SIZE) / FLASH_SECTOR_SIZE;
/* Check if there are any sectors to erase */
if (NbSectorsToErase == 0U)
{
return HAL_ERROR;
}
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
if (status != HAL_OK)
{
return status;
}
/* Initialize SECTORErrorMask to 0 */
SECTORErrorMask = 0;
/* Erase by sector by sector to have a higher granularity than the page erase */
while (NbSectorsToErase > 0U)
{
/* Clear OPTVERR bit set on virgin samples */
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
if (status != HAL_OK)
{
break;
}
/* FLASH_SECTOR_0 to FLASH_SECTOR_11 are used for STM32F103xE, STM32F103xG, STM32F101xC and STM32F101xG devices */
/* FLASH_SECTOR_0 to FLASH_SECTOR_23 are used for STM32F103xB, STM32F103xC, STM32F103xD, STM32F103xE and STM32F100xC devices */
/* Get the sector to erase */
uint32_t sectornb = FLASH_SECTOR_0;
for (uint32_t i = 0; i < FLASH_SECTOR_NUMBER; i++)
{
if (pEraseInit->Sector & (1UL << i))
{
sectornb = i;
break;
}
}
/* Fill EraseInit structure*/
pEraseInit->TypeErase = FLASH_TYPEERASE_SECTORS;
pEraseInit->Sector = (uint32_t)(1U << sectornb);
pEraseInit->NbSectors = 1;
/* Call the erase module */
HAL_FLASHEx_Erase_IT(pEraseInit);
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
/* If the erase operation is completed, disable the SER Bit */
FLASH->CR &= CR_SPARE_MASK;
if (status != HAL_OK)
{
/* In case of error, stop erase procedure and return the faulty sector */
SECTORErrorMask |= pEraseInit->Sector;
/* Save the error code */
SectorErrorTmp |= status;
}
else
{
/*
No error occurred in sector erase operation
SECTORErrorMask is reset
*/
SECTORErrorMask = 0;
}
/* Decrement number of sectors to erase */
NbSectorsToErase--;
/* Point on next sector */
pEraseInit->Sector = (uint32_t)(pEraseInit->Sector << 1U);
}
/* Return the Erase Status */
if (SectorError != NULL)
{
*SectorError = SectorErrorTmp;
}
if (SECTORErrorMask != 0U)
{
status = HAL_ERROR;
}
/* Return the erase status */
return status;
}
```
HAL_FLASHEx_Erase函数的实现过程如下:
1. 检查输入参数pEraseInit是否合法。该结构体包含了要擦除的Flash扇区的相关信息,如起始地址、扇区数量和擦除方式等。
2. 计算需要擦除的Flash扇区数量,并且检查是否有扇区需要擦除。如果扇区数量为0,则直接返回错误。
3. 等待上一次Flash操作(如写入或擦除)完成,并且清除OPTVERR标志位。如果等待超时,则返回错误。
4. 循环执行Flash扇区擦除操作,直到所有需要擦除的扇区都被擦除。具体的操作包括:
- 获取需要擦除的扇区编号,该编号是一个0到11(或23)之间的整数。
- 将需要擦除的扇区信息填入FLASH_EraseInitTypeDef结构体中,并且调用HAL_FLASHEx_Erase_IT函数执行擦除操作。
- 等待Flash擦除操作完成,并且检查是否有错误发生。如果发生了错误,则保存错误信息,但是继续执行擦除操作直到所有需要擦除的扇区都被擦除。
- 将擦除完成的扇区数量减1,继续执行下一个扇区的擦除操作。
5. 返回擦除操作的结果。如果有错误发生,则返回错误代码;否则,返回HAL_OK表示操作成功。
需要注意的是,该函数执行期间会禁止中断,并且解锁Flash,并且在函数执行完毕之后会恢复中断,并且重新锁定Flash。因此,在调用该函数之前,需要确保中断不会影响到Flash的操作,并且需要适当地设置Flash的保护等级以避免意外修改Flash。
阅读全文