2016-09-16 10:10:18 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Flash api for NodeMCU
|
|
|
|
* NodeMCU Team
|
|
|
|
* 2014-12-31
|
|
|
|
*******************************************************************************/
|
|
|
|
#include "flash_api.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2016-09-23 09:39:06 +02:00
|
|
|
#include "rom/spi_flash.h"
|
2016-09-16 10:10:18 +02:00
|
|
|
|
2016-11-16 07:59:06 +01:00
|
|
|
#include "esp_image_format.h"
|
2016-09-22 06:56:40 +02:00
|
|
|
|
|
|
|
#define FLASH_HDR_ADDR 0x1000
|
|
|
|
|
2016-11-12 03:17:17 +01:00
|
|
|
static inline esp_image_header_t flash_load_rom_header (void)
|
2016-09-22 06:56:40 +02:00
|
|
|
{
|
2016-11-12 03:17:17 +01:00
|
|
|
esp_image_header_t hdr;
|
2016-09-22 06:56:40 +02:00
|
|
|
if (ESP_OK !=
|
|
|
|
spi_flash_read (FLASH_HDR_ADDR, (uint32_t *)&hdr, sizeof (hdr)))
|
|
|
|
{
|
|
|
|
NODE_ERR("Failed to load flash header block!\n");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return hdr;
|
|
|
|
}
|
|
|
|
|
2016-09-23 09:39:06 +02:00
|
|
|
#define IRAM_SECTION __attribute__((section(".iram1")))
|
|
|
|
static void IRAM_SECTION do_flash_cfg_workaround (uint32_t sz)
|
|
|
|
{
|
|
|
|
// workaround: configure SPI flash size manually (2nd argument)
|
|
|
|
SPIParamCfg (0x1540ef, sz, 64*1024, 4096, 256, 0xffff);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t __attribute__((section(".iram1"))) flash_detect_size_byte(void)
|
2016-09-22 06:56:40 +02:00
|
|
|
{
|
|
|
|
#define DETECT_SZ 32
|
|
|
|
uint32_t detected_size = FLASH_SIZE_1MBYTE;
|
|
|
|
uint8_t data_orig[DETECT_SZ] PLATFORM_ALIGNMENT = {0};
|
|
|
|
uint8_t data_new[DETECT_SZ] PLATFORM_ALIGNMENT = {0};
|
|
|
|
// Detect read failure or wrap-around on flash read to find end of flash
|
|
|
|
if (ESP_OK == spi_flash_read (0, (uint32_t *)data_orig, DETECT_SZ))
|
|
|
|
{
|
2016-09-23 09:39:06 +02:00
|
|
|
do_flash_cfg_workaround (FLASH_SIZE_16MBYTE);
|
2016-09-22 06:56:40 +02:00
|
|
|
while ((detected_size < FLASH_SIZE_16MBYTE) &&
|
|
|
|
(ESP_OK == spi_flash_read (
|
|
|
|
detected_size, (uint32_t *)data_new, DETECT_SZ)) &&
|
|
|
|
(0 != memcmp(data_orig, data_new, DETECT_SZ)))
|
2016-09-16 10:10:18 +02:00
|
|
|
{
|
2016-09-22 06:56:40 +02:00
|
|
|
detected_size *= 2;
|
|
|
|
}
|
2016-09-23 09:39:06 +02:00
|
|
|
do_flash_cfg_workaround (detected_size);
|
2016-09-22 06:56:40 +02:00
|
|
|
};
|
|
|
|
return detected_size;
|
2016-09-16 10:10:18 +02:00
|
|
|
#undef FLASH_BUFFER_SIZE_DETECT
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t flash_safe_get_size_byte(void)
|
|
|
|
{
|
2016-09-22 06:56:40 +02:00
|
|
|
static uint32_t flash_size = 0;
|
|
|
|
if (flash_size == 0)
|
|
|
|
{
|
|
|
|
flash_size = flash_detect_size_byte();
|
|
|
|
}
|
|
|
|
return flash_size;
|
2016-09-16 10:10:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-22 06:56:40 +02:00
|
|
|
uint16_t flash_safe_get_sec_num(void)
|
2016-09-16 10:10:18 +02:00
|
|
|
{
|
2016-09-22 06:56:40 +02:00
|
|
|
return (flash_safe_get_size_byte() / (SPI_FLASH_SEC_SIZE));
|
2016-09-16 10:10:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t flash_rom_get_size_byte(void)
|
|
|
|
{
|
2016-09-22 06:56:40 +02:00
|
|
|
static uint32_t flash_size = 0;
|
|
|
|
if (flash_size == 0)
|
|
|
|
{
|
|
|
|
switch (flash_load_rom_header ().spi_size)
|
2016-09-16 10:10:18 +02:00
|
|
|
{
|
2016-09-22 06:56:40 +02:00
|
|
|
default: // Unknown flash size, fall back mode.
|
2016-11-12 03:17:17 +01:00
|
|
|
case ESP_IMAGE_FLASH_SIZE_1MB: flash_size = FLASH_SIZE_1MBYTE; break;
|
|
|
|
case ESP_IMAGE_FLASH_SIZE_2MB: flash_size = FLASH_SIZE_2MBYTE; break;
|
|
|
|
case ESP_IMAGE_FLASH_SIZE_4MB: flash_size = FLASH_SIZE_4MBYTE; break;
|
|
|
|
case ESP_IMAGE_FLASH_SIZE_8MB: flash_size = FLASH_SIZE_8MBYTE; break;
|
|
|
|
case ESP_IMAGE_FLASH_SIZE_16MB: flash_size = FLASH_SIZE_16MBYTE; break;
|
2016-09-16 10:10:18 +02:00
|
|
|
}
|
2016-09-22 06:56:40 +02:00
|
|
|
}
|
|
|
|
return flash_size;
|
2016-09-16 10:10:18 +02:00
|
|
|
}
|
|
|
|
|
2016-09-22 06:56:40 +02:00
|
|
|
static bool flash_rom_set_size_type(uint8_t size_code)
|
2016-09-16 10:10:18 +02:00
|
|
|
{
|
|
|
|
// Dangerous, here are dinosaur infested!!!!!
|
|
|
|
// Reboot required!!!
|
|
|
|
// If you don't know what you're doing, your nodemcu may turn into stone ...
|
|
|
|
NODE_DBG("\nBEGIN SET FLASH HEADER\n");
|
2016-11-12 03:17:17 +01:00
|
|
|
esp_image_header_t *hdr = (esp_image_header_t *)malloc (SPI_FLASH_SEC_SIZE);
|
2016-09-22 06:56:40 +02:00
|
|
|
if (!hdr)
|
2016-09-16 10:10:18 +02:00
|
|
|
return false;
|
|
|
|
|
2016-09-22 06:56:40 +02:00
|
|
|
if (ESP_OK == spi_flash_read (FLASH_HDR_ADDR, (uint32_t *)hdr, SPI_FLASH_SEC_SIZE))
|
2016-09-16 10:10:18 +02:00
|
|
|
{
|
2016-09-22 06:56:40 +02:00
|
|
|
hdr->spi_size = size_code;
|
|
|
|
if (ESP_OK == spi_flash_erase_sector (FLASH_HDR_ADDR / SPI_FLASH_SEC_SIZE))
|
|
|
|
{
|
|
|
|
NODE_DBG("\nERASE SUCCESS\n");
|
|
|
|
}
|
|
|
|
if (ESP_OK == spi_flash_write(FLASH_HDR_ADDR, (uint32_t *)hdr, SPI_FLASH_SEC_SIZE))
|
|
|
|
{
|
|
|
|
NODE_DBG("\nWRITE SUCCESS, %u\n", size_code);
|
|
|
|
}
|
2016-09-16 10:10:18 +02:00
|
|
|
}
|
2016-09-22 06:56:40 +02:00
|
|
|
free (hdr);
|
2016-09-16 10:10:18 +02:00
|
|
|
NODE_DBG("\nEND SET FLASH HEADER\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-22 06:56:40 +02:00
|
|
|
|
2016-09-16 10:10:18 +02:00
|
|
|
bool flash_rom_set_size_byte(uint32_t size)
|
|
|
|
{
|
|
|
|
// Dangerous, here are dinosaur infested!!!!!
|
|
|
|
// Reboot required!!!
|
2016-09-22 06:56:40 +02:00
|
|
|
bool ok = true;
|
|
|
|
uint8_t size_code = 0;
|
2016-09-16 10:10:18 +02:00
|
|
|
switch (size)
|
|
|
|
{
|
2016-11-12 03:17:17 +01:00
|
|
|
case FLASH_SIZE_1MBYTE: size_code = ESP_IMAGE_FLASH_SIZE_1MB; break;
|
|
|
|
case FLASH_SIZE_2MBYTE: size_code = ESP_IMAGE_FLASH_SIZE_2MB; break;
|
|
|
|
case FLASH_SIZE_4MBYTE: size_code = ESP_IMAGE_FLASH_SIZE_4MB; break;
|
|
|
|
case FLASH_SIZE_8MBYTE: size_code = ESP_IMAGE_FLASH_SIZE_8MB; break;
|
|
|
|
case FLASH_SIZE_16MBYTE: size_code = ESP_IMAGE_FLASH_SIZE_16MB; break;
|
2016-09-22 06:56:40 +02:00
|
|
|
default: ok = false; break;
|
2016-09-16 10:10:18 +02:00
|
|
|
}
|
2016-09-22 06:56:40 +02:00
|
|
|
if (ok)
|
|
|
|
ok = flash_rom_set_size_type (size_code);
|
|
|
|
return ok;
|
2016-09-16 10:10:18 +02:00
|
|
|
}
|
|
|
|
|
2016-09-22 06:56:40 +02:00
|
|
|
|
2016-09-16 10:10:18 +02:00
|
|
|
uint16_t flash_rom_get_sec_num(void)
|
|
|
|
{
|
2016-09-22 06:56:40 +02:00
|
|
|
return ( flash_rom_get_size_byte() / (SPI_FLASH_SEC_SIZE) );
|
2016-09-16 10:10:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-22 06:56:40 +02:00
|
|
|
uint8_t flash_rom_get_mode(void)
|
2016-09-16 10:10:18 +02:00
|
|
|
{
|
2016-09-22 06:56:40 +02:00
|
|
|
return flash_load_rom_header ().spi_mode;
|
2016-09-16 10:10:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-22 06:56:40 +02:00
|
|
|
uint32_t flash_rom_get_speed(void)
|
2016-09-16 10:10:18 +02:00
|
|
|
{
|
2016-09-22 06:56:40 +02:00
|
|
|
switch (flash_load_rom_header ().spi_speed)
|
|
|
|
{
|
2016-11-12 03:17:17 +01:00
|
|
|
case ESP_IMAGE_SPI_SPEED_40M: return 40000000;
|
|
|
|
case ESP_IMAGE_SPI_SPEED_26M: return 26700000; // TODO: verify 26.7MHz
|
|
|
|
case ESP_IMAGE_SPI_SPEED_20M: return 20000000;
|
|
|
|
case ESP_IMAGE_SPI_SPEED_80M: return 80000000;
|
2016-09-22 06:56:40 +02:00
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
return 0;
|
2016-09-16 10:10:18 +02:00
|
|
|
}
|