コード例 #1
0
ファイル: usbd_flash_if.c プロジェクト: PencilDH/firmware
/*******************************************************************************
* Function Name  : FLASH_If_Erase
* Description    : Erase sector
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
uint16_t FLASH_If_Erase(uint32_t Add)
{
  /* Unlock the internal flash */
  FLASH_Unlock();

  FLASH_ClearFlags();

#if defined (STM32F2XX) || defined (STM32F4XX)
  /* Disable sector write protection if it's already protected */
  uint16_t OB_WRP_Sector = FLASH_SectorToWriteProtect(FLASH_INTERNAL, Add);
  FLASH_WriteProtection_Disable(OB_WRP_Sector);

  /* Check which sector has to be erased */
  uint16_t FLASH_Sector = FLASH_SectorToErase(FLASH_INTERNAL, Add);
  FLASH_Status status = FLASH_EraseSector(FLASH_Sector, VoltageRange_3);

#elif defined(STM32F10X_CL)
  /* Call the standard Flash erase function */
  FLASH_ErasePage(Add);
#endif /* STM32F2XX */

  /* Lock the internal flash */
  FLASH_Lock();

  if (status != FLASH_COMPLETE) return MAL_FAIL;

  return MAL_OK;
}
コード例 #2
0
ファイル: flash_mal.c プロジェクト: kbowerma/particle
bool FLASH_WriteProtectMemory(flash_device_t flashDeviceID, uint32_t startAddress, uint32_t length, bool protect)
{
    if (FLASH_CheckValidAddressRange(flashDeviceID, startAddress, length) != true)
    {
        return false;
    }

    if (flashDeviceID == FLASH_INTERNAL)
    {
        /* Get the first OB_WRP_Sector */
        uint16_t OB_WRP_Sector = FLASH_SectorToWriteProtect(FLASH_INTERNAL, startAddress);
        uint16_t end = FLASH_SectorToWriteProtect(FLASH_INTERNAL, startAddress+length-1)<<1;

        if (OB_WRP_Sector < OB_WRP_Sector_0)
        {
            return false;
        }

        while (!(OB_WRP_Sector & end))
        {
            if (protect)
            {
                FLASH_WriteProtection_Enable(OB_WRP_Sector);
            }
            else
            {
                FLASH_WriteProtection_Disable(OB_WRP_Sector);
            }
            OB_WRP_Sector <<= 1;
        }

        return true;
    }

    return false;
}