STM32g单片机HAL库函数HAL_FLASHEx_Erase代码及解析
时间: 2023-08-04 19:04:29 浏览: 200
STM32 HAL库 Flash读写函数
以下是STM32G单片机HAL库函数HAL_FLASHEx_Erase的代码实现及解析:
```c
HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *SectorError)
{
uint32_t nbSectorsToErase = 0U;
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_BANK_EXCLUSIVE(pEraseInit->Banks));
assert_param(IS_FLASH_NBSECTORS(pEraseInit->NbSectors));
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
if (status != HAL_OK)
{
return status;
}
/* Clear OPTVERR bit set on virgin samples */
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);
/* Get the number of sectors to erase from 1st sector*/
nbSectorsToErase = pEraseInit->NbSectors;
/* Check if there are any sectors to erase */
if (nbSectorsToErase == 0U)
{
return HAL_ERROR;
}
/* Initialize sectorErrorMask to 0 */
sectorErrorMask = 0U;
/* Erase by sector by sector to have a higher granularity than the page erase */
while (nbSectorsToErase > 0U)
{
/* Fill EraseInit structure*/
pEraseInit->TypeErase = FLASH_TYPEERASE_SECTORS;
/* Select the bank to erase */
if (pEraseInit->Banks == FLASH_BANK_1)
{
pEraseInit->Bank = FLASH_BANK_1;
}
#if defined(FLASH_BANK_2)
else if (pEraseInit->Banks == FLASH_BANK_2)
{
pEraseInit->Bank = FLASH_BANK_2;
}
#endif /* FLASH_BANK_2 */
/* 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;
}
}
pEraseInit->Sector = sectorNb;
/* 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 */
CLEAR_BIT(FLASH->CR, FLASH_CR_SER);
if (status != HAL_OK)
{
/* In case of error, stop erase procedure and return the faulty sector */
sectorErrorMask |= (1UL << sectorNb);
/* Save the error code */
sectorErrorTmp |= status;
}
else
{
/*
No error occurred in sector erase operation
sectorErrorMask is reset
*/
sectorErrorMask = 0U;
}
/* 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 status;
}
```
HAL_FLASHEx_Erase函数的实现过程如下:
1. 检查输入参数pEraseInit是否合法。该结构体包含了要擦除的Flash扇区的相关信息,如起始地址、扇区数量和擦除方式等。
2. 等待上一次Flash操作(如写入或擦除)完成,并且清除OPTVERR标志位。如果等待超时,则返回错误。
3. 计算需要擦除的Flash扇区数量,并且检查是否有扇区需要擦除。如果扇区数量为0,则直接返回错误。
4. 循环执行Flash扇区擦除操作,直到所有需要擦除的扇区都被擦除。具体的操作包括:
- 将需要擦除的扇区信息填入FLASH_EraseInitTypeDef结构体中,并且调用HAL_FLASHEx_Erase_IT函数执行擦除操作。
- 等待Flash擦除操作完成,并且检查是否有错误发生。如果发生了错误,则保存错误信息,但是继续执行擦除操作直到所有需要擦除的扇区都被擦除。
- 将擦除完成的扇区数量减1,继续执行下一个扇区的擦除操作。
5. 返回擦除操作的结果。如果有错误发生,则返回错误代码;否则,返回HAL_OK表示操作成功。
需要注意的是,该函数执行期间会禁止中断,并且解锁Flash,并且在函数执行完毕之后会恢复中断,并且重新锁定Flash。因此,在调用该函数之前,需要确保中断不会影响到Flash的操作,并且需要适当地设置Flash的保护等级以避免意外修改Flash。
阅读全文