From 749e71d9cb2c1442f25fc2281cef378e13abfe13 Mon Sep 17 00:00:00 2001 From: Vowstar Date: Tue, 5 May 2015 23:59:30 +0800 Subject: [PATCH] Fixed strange SPI flash API error. Added word_of_aligned_array. --- app/platform/flash_api.c | 16 +++++++++++++++- app/platform/flash_api.h | 1 + 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/app/platform/flash_api.c b/app/platform/flash_api.c index a96619e9..5f9772f6 100644 --- a/app/platform/flash_api.c +++ b/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(); diff --git a/app/platform/flash_api.h b/app/platform/flash_api.h index ff276a83..0063ee1c 100644 --- a/app/platform/flash_api.h +++ b/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);