1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-23 07:58:09 +00:00
trezor-firmware/embed/trezorhal/flash.c

65 lines
1.7 KiB
C
Raw Normal View History

2017-03-07 13:50:09 +00:00
#include STM32_HAL_H
2017-03-28 11:04:54 +00:00
int flash_init(void)
{
2017-03-07 14:52:19 +00:00
return 0;
2017-03-07 13:50:09 +00:00
}
#define WANTED_WRP (OB_WRP_SECTOR_0 | OB_WRP_SECTOR_1)
#define WANTED_RDP (OB_RDP_LEVEL_2)
#define WANTED_BOR (OB_BOR_LEVEL3)
void flash_set_option_bytes(void)
{
FLASH_OBProgramInitTypeDef opts;
HAL_FLASHEx_OBGetConfig(&opts);
opts.OptionType = 0;
if (opts.WRPSector != WANTED_WRP) {
opts.OptionType = OPTIONBYTE_WRP;
opts.WRPState = OB_WRPSTATE_ENABLE;
opts.WRPSector = WANTED_WRP;
opts.Banks = FLASH_BANK_1;
}
if (opts.RDPLevel != WANTED_RDP) {
opts.OptionType = OPTIONBYTE_RDP;
opts.RDPLevel = WANTED_RDP;
}
if (opts.BORLevel != WANTED_BOR) {
opts.OptionType = OPTIONBYTE_BOR;
opts.BORLevel = WANTED_BOR;
}
if (opts.OptionType != 0) {
HAL_FLASHEx_OBProgram(&opts);
}
}
2017-06-20 15:32:21 +00:00
int flash_erase_sectors(int start, int end, void (*progress)(uint16_t val))
2017-06-20 15:32:21 +00:00
{
HAL_FLASH_Unlock();
FLASH_EraseInitTypeDef EraseInitStruct;
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
EraseInitStruct.NbSectors = 1;
uint32_t SectorError = 0;
for (int i = start; i <= end; i++) {
EraseInitStruct.Sector = i;
if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK) {
HAL_FLASH_Lock();
return 0;
}
if (progress) {
progress(1000 * (i - start + 1) / (end - start + 1));
2017-06-20 15:32:21 +00:00
}
}
HAL_FLASH_Lock();
return 1;
}