targets/TARGET_STM/TARGET_STM32G4/flash_api.c
/** Erase one sector starting at defined address
 *
 * The address should be at sector boundary. This function does not do any check for address alignments
 * @param obj The flash object
 * @param address The sector starting address
 * @return 0 for success, -1 for error
 */
Erase one sector starting at defined address
The address should be at sector boundary. This function does not do any check for address alignments
@param obj The flash object
@param address The sector starting address
@return 0 for success, -1 for error
Erase one sector starting at defined address The address should be at sector boundary . This function does not do any check for address alignments @param obj The flash object @param address The sector starting address @return 0 for success - 1 for error
int32_t flash_erase_sector(flash_t *obj, uint32_t address)
{
    uint32_t FirstPage = 0, BankNumber = 0;
    uint32_t PAGEError = 0;
    FLASH_EraseInitTypeDef EraseInitStruct;
    int32_t status = 0;
    if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) {
        return -1;
    }
    if (flash_unlock() != HAL_OK) {
        return -1;
    }
    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);
    FirstPage = GetPage(address);
    BankNumber = GetBank(address);
    EraseInitStruct.TypeErase   = FLASH_TYPEERASE_PAGES;
    EraseInitStruct.Banks       = BankNumber;
    EraseInitStruct.Page        = FirstPage;
    EraseInitStruct.NbPages     = 1;
    if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK) {
        status = -1;
    }
    flash_lock();
    return status;
}
int32_t flash_erase_sector ( flash_t * obj , uint32_t address ) { uint32_t FirstPage = 0 , BankNumber = 0 ; uint32_t PAGEError = 0 ; FLASH_EraseInitTypeDef EraseInitStruct ; int32_t status = 0 ; if ( ( address >= ( FLASH_BASE + FLASH_SIZE ) ) || ( address < FLASH_BASE ) ) { return -1 ; } if ( flash_unlock ( ) != HAL_OK ) { return -1 ; } __HAL_FLASH_CLEAR_FLAG ( FLASH_FLAG_ALL_ERRORS ) ; FirstPage = GetPage ( address ) ; BankNumber = GetBank ( address ) ; EraseInitStruct . TypeErase = FLASH_TYPEERASE_PAGES ; EraseInitStruct . Banks = BankNumber ; EraseInitStruct . Page = FirstPage ; EraseInitStruct . NbPages = 1 ; if ( HAL_FLASHEx_Erase ( & EraseInitStruct , & PAGEError ) != HAL_OK ) { status = -1 ; } flash_lock ( ) ; return status ; }
Erase one sector starting at defined address
 The address should be at sector boundary.
Erase one sector starting at defined address The address should be at sector boundary .
/* Clear error programming flags */ /* Get the 1st page to erase */ /* MBED HAL erases 1 page  / sector at a time */ /* Get the bank */ /* Fill EraseInit structure*/ /* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache,
     you have to make sure that these data are rewritten before they are accessed during code
     execution. If this cannot be done safely, it is recommended to flush the caches by setting the
     DCRST and ICRST bits in the FLASH_CR register. */