瀏覽代碼

Fix esp_init_data_default inconsistencies.

Make ADC vs readvdd33 option user_config.h configurable for the init data,
and made readvdd33 the default.
Johny Mattsson 8 年之前
父節點
當前提交
1e3c231630
共有 3 個文件被更改,包括 64 次插入32 次删除
  1. 5 3
      app/include/user_config.h
  2. 20 23
      app/platform/flash_api.c
  3. 39 6
      app/user/user_main.c

+ 5 - 3
app/include/user_config.h

@@ -11,11 +11,13 @@
 // #define FLASH_16M
 #define FLASH_AUTOSIZE
 #define FLASH_SAFE_API
-// #define DEVELOP_VERSION
-#define FULL_VERSION_FOR_USER
 
-#define USE_OPTIMIZE_PRINTF
+// Byte 107 of esp_init_data_default, only one of these 3 can be picked
+#define ESP_INIT_DATA_ENABLE_READVDD33
+//#define ESP_INIT_DATA_ENABLE_READADC
+//#define ESP_INIT_DATA_FIXED_VDD33_VALUE 33
 
+// #define DEVELOP_VERSION
 #ifdef DEVELOP_VERSION
 #define NODE_DEBUG
 #define COAP_DEBUG

+ 20 - 23
app/platform/flash_api.c

@@ -8,7 +8,17 @@
 #include "spi_flash.h"
 #include "c_stdio.h"
 
-static volatile const uint8_t flash_init_data[128] ICACHE_STORE_ATTR ICACHE_RODATA_ATTR =
+#if defined(ESP_INIT_DATA_ENABLE_READVDD33)
+# define INIT_107 0xff
+#elif defined(ESP_INIT_DATA_ENABLE_READADC)
+# define INIT_107 0x00
+#elif defined(ESP_INIT_DATA_FIXED_VDD33_VALUE)
+# define INIT_107 ESP_INIT_DATA_FIXED_VDD33_VALUE
+#else
+# define INIT_107 0xff
+#endif
+
+static const uint8_t flash_init_data[128] =
 {
     0x05, 0x00, 0x04, 0x02, 0x05, 0x05, 0x05, 0x02, 0x05, 0x00, 0x04, 0x05, 0x05, 0x04, 0x05, 0x05,
     0x04, 0xFE, 0xFD, 0xFF, 0xF0, 0xF0, 0xF0, 0xE0, 0xE0, 0xE0, 0xE1, 0x0A, 0xFF, 0xFF, 0xF8, 0x00,
@@ -16,7 +26,7 @@ static volatile const uint8_t flash_init_data[128] ICACHE_STORE_ATTR ICACHE_RODA
     0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     0xE1, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x93, 0x43, 0x00, 0x00, 0x00,
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, INIT_107, 0x00, 0x00, 0x00, 0x00,
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
 };
 
@@ -316,26 +326,13 @@ bool flash_rom_set_speed(uint32_t speed)
     return true;
 }
 
-bool flash_init_data_written(void)
-{
-    // FLASH SEC - 4
-    uint32_t data[2] ICACHE_STORE_ATTR;
-#if defined(FLASH_SAFE_API)
-    if (SPI_FLASH_RESULT_OK == flash_safe_read((flash_safe_get_sec_num() - 4) * SPI_FLASH_SEC_SIZE, (uint32 *)data, sizeof(data)))
-#else
-    if (SPI_FLASH_RESULT_OK == spi_flash_read((flash_rom_get_sec_num() - 4) * SPI_FLASH_SEC_SIZE, (uint32 *)data, sizeof(data)))
-#endif // defined(FLASH_SAFE_API)
-    {
-        if (data[0] == 0xFFFFFFFF && data[1] == 0xFFFFFFFF)
-        {
-            return false;
-        }
-    }
-    return true;
-}
-
 bool flash_init_data_default(void)
 {
+    /* Can't copy directly from flash (which is where the default data lives)
+     * due to it being unmapped during the write, so bounce via ram buffer. */
+    uint8_t init_data[128];
+    os_memcpy (init_data, flash_init_data, 128);
+
     // FLASH SEC - 4
     // Dangerous, here are dinosaur infested!!!!!
     // Reboot required!!!
@@ -344,7 +341,7 @@ bool flash_init_data_default(void)
 #if defined(FLASH_SAFE_API)
     if (SPI_FLASH_RESULT_OK == flash_safe_erase_sector((flash_safe_get_sec_num() - 4)))
     {
-        if (SPI_FLASH_RESULT_OK == flash_safe_write((flash_safe_get_sec_num() - 4) * SPI_FLASH_SEC_SIZE, (uint32 *)flash_init_data, 128))
+        if (SPI_FLASH_RESULT_OK == flash_safe_write((flash_safe_get_sec_num() - 4) * SPI_FLASH_SEC_SIZE, (uint32 *)init_data, 128))
         {
             result = true;
         }
@@ -352,7 +349,7 @@ bool flash_init_data_default(void)
 #else
     if (SPI_FLASH_RESULT_OK == spi_flash_erase_sector((flash_rom_get_sec_num() - 4)))
     {
-        if (SPI_FLASH_RESULT_OK == spi_flash_write((flash_rom_get_sec_num() - 4) * SPI_FLASH_SEC_SIZE, (uint32 *)flash_init_data, 128))
+        if (SPI_FLASH_RESULT_OK == spi_flash_write((flash_rom_get_sec_num() - 4) * SPI_FLASH_SEC_SIZE, (uint32 *)init_data, 128))
         {
             result = true;
         }
@@ -438,4 +435,4 @@ uint16_t word_of_aligned_array(const uint16_t *aligned_array, uint32_t index)
 // uint8_t flash_rom_calc_checksum(void)
 // {
 //     return 0;
-// }
+// }

+ 39 - 6
app/user/user_main.c

@@ -30,7 +30,14 @@
 #define SIG_LUA 0
 #define SIG_UARTINPUT 1
 #define TASK_QUEUE_LEN 4
-os_event_t *taskQueue;
+
+static os_event_t *taskQueue;
+
+/* Important: no_init_data CAN NOT be left as zero initialised, as that
+ * initialisation will happen after user_start_trampoline, but before
+ * the user_init, thus clobbering our state!
+ */
+static uint8_t no_init_data = 0xff;
 
 
 /* Note: the trampoline *must* be explicitly put into the .text segment, since
@@ -47,6 +54,30 @@ void TEXT_SECTION_ATTR user_start_trampoline (void)
   // is where the cpu clock actually gets bumped to 80MHz.
   rtctime_early_startup ();
 #endif
+
+
+  /* Minimal early detection of missing esp_init_data.
+   * If it is missing, the SDK will write its own and thus we'd end up
+   * using that unless the flash size field is incorrect. This then leads
+   * to different esp_init_data being used depending on whether the user
+   * flashed with the right flash size or not (and the better option would
+   * be to flash with an *incorrect* flash size, counter-intuitively).
+   * To avoid that mess, we read out the flash size and do a test for
+   * esp_init_data based on that size. If it's missing, flag for later.
+   * If the flash size was incorrect, we'll end up fixing it all up
+   * anyway, so this ends up solving the conundrum. Only remaining issue
+   * is lack of spare code bytes in iram, so this is deliberately quite
+   * terse and not as readable as one might like.
+   */
+  SPIFlashInfo sfi;
+  SPIRead (0, &sfi, sizeof (sfi)); // Cache read not enabled yet, safe to use
+  if (sfi.size < 2) // Compensate for out-of-order 4mbit vs 2mbit values
+    sfi.size ^= 1;
+  uint32_t flash_end_addr = (256 * 1024) << sfi.size;
+  uint32_t init_data_hdr = 0xffffffff;
+  SPIRead (flash_end_addr - 4 * SPI_FLASH_SEC_SIZE, &init_data_hdr, sizeof (init_data_hdr));
+  no_init_data = (init_data_hdr == 0xffffffff);
+
   call_user_start ();
 }
 
@@ -96,10 +127,9 @@ void nodemcu_init(void)
         NODE_ERR("Self adjust flash size.\n");
         // Fit hardware real flash size.
         flash_rom_set_size_byte(flash_safe_get_size_byte());
-        // Flash init data at FLASHSIZE - 0x04000 Byte.
-        flash_init_data_default();
-        // Flash blank data at FLASHSIZE - 0x02000 Byte.
-        flash_init_data_blank();
+        // Write out init data at real location.
+        no_init_data = true;
+
         if( !fs_format() )
         {
             NODE_ERR( "\ni*** ERROR ***: unable to format. FS might be compromised.\n" );
@@ -112,12 +142,15 @@ void nodemcu_init(void)
     }
 #endif // defined(FLASH_SAFE_API)
 
-    if( !flash_init_data_written() ){
+    if (no_init_data)
+    {
         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();
+        // Reboot to make the new data come into effect
+        system_restart ();
     }
 
 #if defined( BUILD_WOFS )