From 3c6ebea059c00773a9123afc1dd235cd6fa6c2b5 Mon Sep 17 00:00:00 2001 From: HuangRui Date: Tue, 6 Jan 2015 19:32:17 +0800 Subject: [PATCH] Use safe flash options. --- app/platform/flash_api.c | 42 +++++++++++++++++++++++++--------------- app/platform/flash_api.h | 22 +++++++++++++++++++++ app/user/user_main.c | 5 ++++- 3 files changed, 52 insertions(+), 17 deletions(-) diff --git a/app/platform/flash_api.c b/app/platform/flash_api.c index 11dba464..d042ef44 100644 --- a/app/platform/flash_api.c +++ b/app/platform/flash_api.c @@ -71,10 +71,10 @@ bool flash_set_size(uint8_t size) // Reboot required!!! // If you don't know what you're doing, your nodemcu may turn into stone ... uint8_t data[SPI_FLASH_SEC_SIZE] ICACHE_STORE_ATTR; - SPIRead(0, data, sizeof(data)); + spi_flash_read(0, (uint32 *)data, sizeof(data)); SPIFlashInfo *p_spi_flash_info = (SPIFlashInfo *)(data); p_spi_flash_info->size = size; - SPIEraseSector(0); + spi_flash_erase_sector(0); spi_flash_write(0, (uint32 *)data, sizeof(data)); //p_spi_flash_info = flash_get_info(); //p_spi_flash_info->size = size; @@ -175,13 +175,13 @@ uint32_t flash_get_speed(void) bool flash_init_data_written(void) { // FLASH SEC - 4 - // Dangerous, here are dinosaur infested!!!!! - // Reboot required!!! - // It will init system data to default! uint32_t data[2] ICACHE_STORE_ATTR; - SPIRead((flash_get_sec_num() - 4) * SPI_FLASH_SEC_SIZE, data, sizeof(data)); - if(data[0] == 0xFFFFFFFF && data[1] == 0xFFFFFFFF) { - return false; + if (SPI_FLASH_RESULT_OK == spi_flash_read((flash_get_sec_num() - 4) * SPI_FLASH_SEC_SIZE, (uint32 *)data, sizeof(data))) + { + if (data[0] == 0xFFFFFFFF && data[1] == 0xFFFFFFFF) + { + return false; + } } return true; } @@ -192,10 +192,15 @@ bool flash_init_data_default(void) // Dangerous, here are dinosaur infested!!!!! // Reboot required!!! // It will init system data to default! - - SPIEraseSector((flash_get_sec_num() - 4)); - spi_flash_write((flash_get_sec_num() - 4) * SPI_FLASH_SEC_SIZE, (uint32 *)flash_init_data, 128); - return true; + bool result = false; + if (SPI_FLASH_RESULT_OK == spi_flash_erase_sector((flash_get_sec_num() - 4))) + { + if (SPI_FLASH_RESULT_OK == spi_flash_write((flash_get_sec_num() - 4) * SPI_FLASH_SEC_SIZE, (uint32 *)flash_init_data, 128)) + { + result = true; + } + } + return result; } bool flash_init_data_blank(void) @@ -204,14 +209,19 @@ bool flash_init_data_blank(void) // Dangerous, here are dinosaur infested!!!!! // Reboot required!!! // It will init system config to blank! - SPIEraseSector((flash_get_sec_num() - 2)); - SPIEraseSector((flash_get_sec_num() - 1)); - return true; + bool result = false; + if ((SPI_FLASH_RESULT_OK == spi_flash_erase_sector((flash_get_sec_num() - 2))) && + (SPI_FLASH_RESULT_OK == spi_flash_erase_sector((flash_get_sec_num() - 1)))) + { + result = true; + } + + return result ; } bool flash_self_destruct(void) { - // Erase your flash. Good bye! + // Dangerous, Erase your flash. Good bye! SPIEraseChip(); return true; } diff --git a/app/platform/flash_api.h b/app/platform/flash_api.h index 593741c8..96452e74 100644 --- a/app/platform/flash_api.h +++ b/app/platform/flash_api.h @@ -5,6 +5,28 @@ #include "cpu_esp8266.h" #define FLASH_MAP_START_ADDRESS (INTERNAL_FLASH_START_ADDRESS) +/****************************************************************************** + * ROM Function definition + * Note: It is unsafe to use ROM function, but it may efficient. + * SPIEraseSector + * unknown SPIEraseSector(uint16 sec); + * The 1st parameter is flash sector number. + + * SPIRead (Unsafe) + * unknown SPIRead(uint32_t src_addr, uint32_t *des_addr, uint32_t size); + * The 1st parameter is source addresses. + * The 2nd parameter is destination addresses. + * The 3rd parameter is size. + * Note: Sometimes it have no effect, may be need a delay or other option(lock or unlock, etc.) with known reason. + + * SPIWrite (Unsafe) + * unknown SPIWrite(uint32_t des_addr, uint32_t *src_addr, uint32_t size); + * The 1st parameter is destination addresses. + * The 2nd parameter is source addresses. + * The 3rd parameter is size. + * Note: Sometimes it have no effect, may be need a delay or other option(lock or unlock, etc.) with known reason. +*******************************************************************************/ + typedef struct { uint8_t unknown0; diff --git a/app/user/user_main.c b/app/user/user_main.c index bc044cd5..77c7939e 100644 --- a/app/user/user_main.c +++ b/app/user/user_main.c @@ -83,9 +83,12 @@ void user_init(void) return; } - if(!flash_init_data_written()){ + if( !flash_init_data_written() ){ NODE_ERR("Restore init data.\n"); + // Flash init data at FLASHSIZE - 0x04000 Byte. flash_init_data_default(); + // Flash blank data at FLASHSIZE - 0x02000 Byte. + flash_init_data_blank(); } #if defined( BUILD_WOFS )