|
|
@ -76,41 +76,39 @@ uint8_t stmflash_get_flash_sector(uint32_t addr) { |
|
|
|
* @param length : 要写入的 字(32位)数(就是要写入的32位数据的个数) |
|
|
|
* @retval 无 |
|
|
|
*/ |
|
|
|
void stmflash_write(uint32_t start_address, uint32_t *pbuf, uint32_t length) { |
|
|
|
void stmflash_write(uint32_t waddr, uint32_t *pbuf, uint32_t length) { |
|
|
|
FLASH_EraseInitTypeDef flasheraseinit; |
|
|
|
HAL_StatusTypeDef FlashStatus = HAL_OK; |
|
|
|
|
|
|
|
uint32_t flash_write_start_address = 0; |
|
|
|
uint32_t flash_write_end_address = 0; |
|
|
|
uint32_t addrx = 0; |
|
|
|
uint32_t endaddr = 0; |
|
|
|
uint32_t sectorerror = 0; |
|
|
|
|
|
|
|
flash_write_start_address = start_address; /* 写入的起始地址 */ |
|
|
|
flash_write_end_address = start_address + length * 4; /* 写入的结束地址 */ |
|
|
|
addrx = waddr; /* 写入的起始地址 */ |
|
|
|
endaddr = waddr + length * 4; /* 写入的结束地址 */ |
|
|
|
|
|
|
|
if (!stmflash_address_legal(flash_write_start_address, flash_write_end_address)) { |
|
|
|
return; |
|
|
|
} |
|
|
|
stmflash_address_legal(addrx, endaddr); |
|
|
|
|
|
|
|
HAL_FLASH_Unlock(); /* 解锁 */ |
|
|
|
FLASH->ACR &= ~(1 << 10); /* FLASH擦除期间,必须禁止数据缓存!!! */ |
|
|
|
|
|
|
|
if (flash_write_start_address < 0X1FFF0000) /* 只有主存储区,才需要执行擦除操作!! */ |
|
|
|
if (addrx < 0X1FFF0000) /* 只有主存储区,才需要执行擦除操作!! */ |
|
|
|
{ |
|
|
|
while (flash_write_start_address < flash_write_end_address) /* 扫清一切障碍.(对非FFFFFFFF的地方,先擦除) */ |
|
|
|
while (addrx < endaddr) /* 扫清一切障碍.(对非FFFFFFFF的地方,先擦除) */ |
|
|
|
{ |
|
|
|
if (stmflash_read_word(flash_write_start_address) != 0XFFFFFFFF) /* 有非0XFFFFFFFF的地方,要擦除这个扇区 */ |
|
|
|
if (stmflash_read_word(addrx) != 0XFFFFFFFF) /* 有非0XFFFFFFFF的地方,要擦除这个扇区 */ |
|
|
|
{ |
|
|
|
flasheraseinit.TypeErase = FLASH_TYPEERASE_SECTORS; /* 擦除类型,扇区擦除 */ |
|
|
|
flasheraseinit.Sector = stmflash_get_flash_sector(flash_write_start_address); /* 要擦除的扇区 */ |
|
|
|
flasheraseinit.NbSectors = 1; /* 一次只擦除一个扇区 */ |
|
|
|
flasheraseinit.VoltageRange = FLASH_VOLTAGE_RANGE_3; /* 电压范围,VCC=2.7~3.6V之间!! */ |
|
|
|
flasheraseinit.TypeErase = FLASH_TYPEERASE_SECTORS; /* 擦除类型,扇区擦除 */ |
|
|
|
flasheraseinit.Sector = stmflash_get_flash_sector(addrx); /* 要擦除的扇区 */ |
|
|
|
flasheraseinit.NbSectors = 1; /* 一次只擦除一个扇区 */ |
|
|
|
flasheraseinit.VoltageRange = FLASH_VOLTAGE_RANGE_3; /* 电压范围,VCC=2.7~3.6V之间!! */ |
|
|
|
|
|
|
|
if (HAL_FLASHEx_Erase(&flasheraseinit, §orerror) != HAL_OK) { |
|
|
|
break; /* 发生错误了 */ |
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
flash_write_start_address += 4; |
|
|
|
addrx += 4; |
|
|
|
} |
|
|
|
FLASH_WaitForLastOperation(FLASH_WAITETIME); /* 等待上次操作完成 */ |
|
|
|
} |
|
|
@ -119,14 +117,14 @@ void stmflash_write(uint32_t start_address, uint32_t *pbuf, uint32_t length) { |
|
|
|
FlashStatus = FLASH_WaitForLastOperation(FLASH_WAITETIME); /* 等待上次操作完成 */ |
|
|
|
|
|
|
|
if (FlashStatus == HAL_OK) { |
|
|
|
while (start_address < flash_write_end_address) /* 写数据 */ |
|
|
|
while (waddr < endaddr) /* 写数据 */ |
|
|
|
{ |
|
|
|
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, start_address, *pbuf) != HAL_OK) /* 写入数据 */ |
|
|
|
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, waddr, *pbuf) != HAL_OK) /* 写入数据 */ |
|
|
|
{ |
|
|
|
break; /* 写入异常 */ |
|
|
|
} |
|
|
|
|
|
|
|
start_address += 4; |
|
|
|
waddr += 4; |
|
|
|
pbuf++; |
|
|
|
} |
|
|
|
} |
|
|
@ -171,16 +169,20 @@ int8_t stmflash_write_config(uint32_t start_address, config_t *config, uint32_t |
|
|
|
uint32_t flash_write_end_address = 0; |
|
|
|
uint32_t sectorerror = 0; |
|
|
|
|
|
|
|
flash_write_start_address = start_address; /* 写入的起始地址 */ |
|
|
|
flash_write_end_address = start_address + length * 4; /* 写入的结束地址 */ |
|
|
|
|
|
|
|
if (!stmflash_address_legal(flash_write_start_address, flash_write_end_address)) { |
|
|
|
if (start_address < STM32_FLASH_BASE || |
|
|
|
start_address % 4 || /* 写入地址小于 STM32_FLASH_BASE, 或不是4的整数倍, 非法. */ |
|
|
|
start_address > |
|
|
|
(STM32_FLASH_BASE + STM32_FLASH_SIZE)) /* 写入地址大于 STM32_FLASH_BASE + STM32_FLASH_SIZE, 非法. */ |
|
|
|
{ |
|
|
|
return -1; |
|
|
|
} |
|
|
|
|
|
|
|
HAL_FLASH_Unlock(); /* 解锁 */ |
|
|
|
FLASH->ACR &= ~(1 << 10); /* FLASH擦除期间,必须禁止数据缓存!!! */ |
|
|
|
|
|
|
|
flash_write_start_address = start_address; /* 写入的起始地址 */ |
|
|
|
flash_write_end_address = start_address + length * 4; /* 写入的结束地址 */ |
|
|
|
|
|
|
|
if (flash_write_start_address < 0X1FFF0000) /* 只有主存储区,才需要执行擦除操作!! */ |
|
|
|
{ |
|
|
|
while (flash_write_start_address < flash_write_end_address) /* 扫清一切障碍.(对非FFFFFFFF的地方,先擦除) */ |
|
|
@ -206,16 +208,16 @@ int8_t stmflash_write_config(uint32_t start_address, config_t *config, uint32_t |
|
|
|
FlashStatus = FLASH_WaitForLastOperation(FLASH_WAITETIME); /* 等待上次操作完成 */ |
|
|
|
|
|
|
|
if (FlashStatus == HAL_OK) { |
|
|
|
ZFLASH_WRITE_DATA(&start_address, config->ip) { return -1; } |
|
|
|
END(); |
|
|
|
ZFLASH_WRITE_DATA(&start_address, config->gw) { return -2; } |
|
|
|
END(); |
|
|
|
ZFLASH_WRITE_DATA(&start_address, config->netmask) { return -3; } |
|
|
|
END(); |
|
|
|
ZFLASH_WRITE_DATA(&start_address, config->localport) { return -4; } |
|
|
|
END(); |
|
|
|
ZFLASH_WRITE_DATA(&start_address, config->obtaining_ip_mode) { return -5; } |
|
|
|
END(); |
|
|
|
// ZFLASH_WRITE_DATA(&start_address, config->ip) { return -1; } |
|
|
|
// END(); |
|
|
|
// ZFLASH_WRITE_DATA(&start_address, config->gw) { return -2; } |
|
|
|
// END(); |
|
|
|
// ZFLASH_WRITE_DATA(&start_address, config->netmask) { return -3; } |
|
|
|
// END(); |
|
|
|
// ZFLASH_WRITE_DATA(&start_address, config->localport) { return -4; } |
|
|
|
// END(); |
|
|
|
// ZFLASH_WRITE_DATA(&start_address, config->obtaining_ip_mode) { return -5; } |
|
|
|
// END(); |
|
|
|
} |
|
|
|
|
|
|
|
FLASH->ACR |= 1 << 10; /* FLASH擦除结束,开启数据fetch */ |
|
|
|