diff --git a/src/config.c b/src/config.c index fb5be65..ea278c0 100644 --- a/src/config.c +++ b/src/config.c @@ -1,10 +1,11 @@ #include "config.h" -#include "../iflytop_microcontroller/sdk/stm32/stm32sdk.h" #include + +#include "../iflytop_microcontroller/sdk/stm32/stm32sdk.h" +#include "lwip/api.h" #include "lwip/opt.h" #include "lwip/sys.h" -#include "lwip/api.h" #include "protocol.h" #include "src/zflash.h" #include "zboard.h" @@ -27,6 +28,12 @@ typedef uint32_t u32; typedef int64_t i64; typedef uint64_t u64; +const uint8_t g_text_buf[] = {"STM32 FLASH TEST"}; +#define TEXT_LENTH sizeof(g_text_buf) /* 数组长度 */ +#define SIZE TEXT_LENTH / 4 + ((TEXT_LENTH % 4) ? 1 : 0) +#define FLASH_SAVE_ADDR \ + 0x08010000 /* 设置FLASH 保存地址(必须为4的整数倍,且其值要大于本代码所占用FLASH的大小 + 0X08000000) */ + // #define INIT_CONFIG(type, name, markname) \ // type name = 0; \ // err = nvs_get_##type(s_nvs_handle, markname, &name); \ @@ -58,6 +65,7 @@ typedef uint64_t u64; // } void config_init() { + uint8_t g_text_rx_buf[100]; #if 1 IP4_ADDR((ip4_addr_t *)&s_config.ip, 192, 168, 1, 10); IP4_ADDR((ip4_addr_t *)&s_config.gw, 192, 168, 1, 1); @@ -67,8 +75,13 @@ void config_init() { s_config.mask = 1; // #endif + stmflash_write(FLASH_SAVE_ADDR, (uint32_t *)g_text_buf, SIZE); + stmflash_read(FLASH_SAVE_ADDR, (uint32_t *)g_text_rx_buf, SIZE); + printf("%s\r\n", g_text_rx_buf); - // stmflash_write_config(STM32_FLASH_BASE, &s_config, flash_write_lenght(config_t)); + // if (stmflash_write_config(STM32_FLASH_BASE, &s_config, flash_write_lenght(config_t)) < 0) { + // ZLOGE(TAG, "flash write config error\r\n"); + // } // esp_err_t err = nvs_flash_init(); diff --git a/src/zflash.c b/src/zflash.c index bc91fef..78963f7 100644 --- a/src/zflash.c +++ b/src/zflash.c @@ -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 */