From d84a24fc9a4758c5f0154d994c3335b2fbfea49d Mon Sep 17 00:00:00 2001 From: Johny Mattsson Date: Tue, 8 Dec 2015 10:25:05 +1100 Subject: [PATCH] Proper cached-flash to phys address translation. --- app/platform/common.c | 2 +- app/platform/cpu_esp8266.h | 5 +++++ app/platform/platform.c | 11 +++++++++++ app/platform/platform.h | 10 ++++++++++ 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/app/platform/common.c b/app/platform/common.c index bb10fb45..a977c30c 100644 --- a/app/platform/common.c +++ b/app/platform/common.c @@ -149,7 +149,7 @@ uint32_t platform_flash_get_first_free_block_address( uint32_t *psect ) uint32_t start, end, sect; NODE_DBG("_flash_used_end:%08x\n", (uint32_t)_flash_used_end); if(_flash_used_end>0){ // find the used sector - sect = flashh_find_sector( ( uint32_t )_flash_used_end - INTERNAL_FLASH_MAPPED_ADDRESS - 1, NULL, &end ); + sect = flashh_find_sector( platform_flash_mapped2phys ( (uint32_t)_flash_used_end - 1), NULL, &end ); if( psect ) *psect = sect + 1; return end + 1; diff --git a/app/platform/cpu_esp8266.h b/app/platform/cpu_esp8266.h index ef618dea..20e01015 100644 --- a/app/platform/cpu_esp8266.h +++ b/app/platform/cpu_esp8266.h @@ -68,4 +68,9 @@ #define flash_read spi_flash_read #endif // defined(FLASH_SAFE_API) +#define CACHE_FLASH_CTRL_REG 0x3ff0000c +#define CACHE_FLASH_ACTIVE 0x00000100 +#define CACHE_FLASH_MAPPED0 0x02000000 +#define CACHE_FLASH_MAPPED1 0x00010000 + #endif // #ifndef __CPU_ESP8266_H__ diff --git a/app/platform/platform.c b/app/platform/platform.c index 1a45d5b1..95fa2fc9 100755 --- a/app/platform/platform.c +++ b/app/platform/platform.c @@ -579,3 +579,14 @@ int platform_flash_erase_sector( uint32_t sector_id ) system_soft_wdt_feed (); return flash_erase( sector_id ) == SPI_FLASH_RESULT_OK ? PLATFORM_OK : PLATFORM_ERR; } + +uint32_t platform_flash_mapped2phys (uint32_t mapped_addr) +{ + uint32_t cache_ctrl = READ_PERI_REG(CACHE_FLASH_CTRL_REG); + if (!(cache_ctrl & CACHE_FLASH_ACTIVE)) + return -1; + bool b0 = (cache_ctrl & CACHE_FLASH_MAPPED0) ? 1 : 0; + bool b1 = (cache_ctrl & CACHE_FLASH_MAPPED1) ? 1 : 0; + uint32_t meg = (b1 << 1) | b0; + return mapped_addr - INTERNAL_FLASH_MAPPED_ADDRESS + meg * 0x100000; +} diff --git a/app/platform/platform.h b/app/platform/platform.h index b4d3a00a..47f0b5a7 100644 --- a/app/platform/platform.h +++ b/app/platform/platform.h @@ -237,6 +237,16 @@ uint32_t platform_s_flash_read( void *to, uint32_t fromaddr, uint32_t size ); uint32_t platform_flash_get_num_sectors(void); int platform_flash_erase_sector( uint32_t sector_id ); +/** + * Translated a mapped address to a physical flash address, based on the + * current flash cache mapping. + * @param mapped_addr Address to translate (>= INTERNAL_FLASH_MAPPED_ADDRESS) + * @return the corresponding physical flash address, or -1 if flash cache is + * not currently active. + * @see Cache_Read_Enable. + */ +uint32_t platform_flash_mapped2phys (uint32_t mapped_addr); + // ***************************************************************************** // Allocator support