Browse Source

Fixed strange SPI flash API error. Added word_of_aligned_array.

Vowstar 9 years ago
parent
commit
749e71d9cb
2 changed files with 16 additions and 1 deletions
  1. 15 1
      app/platform/flash_api.c
  2. 1 0
      app/platform/flash_api.h

+ 15 - 1
app/platform/flash_api.c

@@ -396,11 +396,25 @@ uint8_t byte_of_aligned_array(const uint8_t *aligned_array, uint32_t index)
         NODE_DBG("aligned_array is not 4-byte aligned.\n");
         return 0;
     }
-    uint32_t v = ((uint32_t *)aligned_array)[ index / 4 ];
+    volatile uint32_t v = ((uint32_t *)aligned_array)[ index / 4 ];
     uint8_t *p = (uint8_t *) (&v);
     return p[ (index % 4) ];
 }
 
+uint16_t word_of_aligned_array(const uint16_t *aligned_array, uint32_t index)
+{
+    if ( (((uint32_t)aligned_array) % 4) != 0 )
+    {
+        NODE_DBG("aligned_array is not 4-byte aligned.\n");
+        return 0;
+    }
+    volatile uint32_t v = ((uint32_t *)aligned_array)[ index / 2 ];
+    uint16_t *p = (uint16_t *) (&v);
+    return (index % 2 == 0) ? p[ 0 ] : p[ 1 ];
+    // return p[ (index % 2) ]; // -- why error???
+    // (byte_of_aligned_array((uint8_t *)aligned_array, index * 2 + 1) << 8) | byte_of_aligned_array((uint8_t *)aligned_array, index * 2);
+}
+
 // uint8_t flash_rom_get_checksum(void)
 // {
 //     // SPIFlashInfo spi_flash_info ICACHE_STORE_ATTR = flash_rom_getinfo();

+ 1 - 0
app/platform/flash_api.h

@@ -107,6 +107,7 @@ bool flash_init_data_default(void);
 bool flash_init_data_blank(void);
 bool flash_self_destruct(void);
 uint8_t byte_of_aligned_array(const uint8_t* aligned_array, uint32_t index);
+uint16_t word_of_aligned_array(const uint16_t *aligned_array, uint32_t index);
 // uint8_t flash_rom_get_checksum(void);
 // uint8_t flash_rom_calc_checksum(void);