Skip to content

Commit

Permalink
Merge pull request #450 from dnc40085/dev096_revert_commit_5261fb1
Browse files Browse the repository at this point in the history
Revert "Update spiffs to latest." on dev096 branch
  • Loading branch information
vowstar committed May 29, 2015
2 parents f7a6183 + 01d90f1 commit fcde022
Show file tree
Hide file tree
Showing 20 changed files with 183 additions and 743 deletions.
91 changes: 47 additions & 44 deletions app/platform/flash_api.c
Expand Up @@ -4,8 +4,8 @@
* 2014-12-31
*******************************************************************************/
#include "user_config.h"
#include "osapi.h"
#include "flash_api.h"
#include "spi_flash.h"
#include "c_stdio.h"

static volatile const uint8_t flash_init_data[128] ICACHE_STORE_ATTR ICACHE_RODATA_ATTR =
Expand Down Expand Up @@ -103,40 +103,43 @@ uint8_t flash_rom_get_size_type(void)
uint32_t flash_rom_get_size_byte(void)
{
static uint32_t flash_size = 0;
switch (flash_rom_getinfo().size)
if (flash_size == 0)
{
case SIZE_2MBIT:
// 2Mbit, 256kByte
flash_size = 256 * 1024;
break;
case SIZE_4MBIT:
// 4Mbit, 512kByte
flash_size = 512 * 1024;
break;
case SIZE_8MBIT:
// 8Mbit, 1MByte
flash_size = 1 * 1024 * 1024;
break;
case SIZE_16MBIT:
// 16Mbit, 2MByte
flash_size = 2 * 1024 * 1024;
break;
case SIZE_32MBIT:
// 32Mbit, 4MByte
flash_size = 4 * 1024 * 1024;
break;
case SIZE_64MBIT:
// 64Mbit, 8MByte
flash_size = 8 * 1024 * 1024;
break;
case SIZE_128MBIT:
// 128Mbit, 16MByte
flash_size = 16 * 1024 * 1024;
break;
default:
// Unknown flash size, fall back mode.
flash_size = 512 * 1024;
break;
switch (flash_rom_getinfo().size)
{
case SIZE_2MBIT:
// 2Mbit, 256kByte
flash_size = 256 * 1024;
break;
case SIZE_4MBIT:
// 4Mbit, 512kByte
flash_size = 512 * 1024;
break;
case SIZE_8MBIT:
// 8Mbit, 1MByte
flash_size = 1 * 1024 * 1024;
break;
case SIZE_16MBIT:
// 16Mbit, 2MByte
flash_size = 2 * 1024 * 1024;
break;
case SIZE_32MBIT:
// 32Mbit, 4MByte
flash_size = 4 * 1024 * 1024;
break;
case SIZE_64MBIT:
// 64Mbit, 8MByte
flash_size = 8 * 1024 * 1024;
break;
case SIZE_128MBIT:
// 128Mbit, 16MByte
flash_size = 16 * 1024 * 1024;
break;
default:
// Unknown flash size, fall back mode.
flash_size = 512 * 1024;
break;
}
}
return flash_size;
}
Expand All @@ -146,21 +149,21 @@ bool flash_rom_set_size_type(uint8_t size)
// Dangerous, here are dinosaur infested!!!!!
// Reboot required!!!
// If you don't know what you're doing, your nodemcu may turn into stone ...
FLASH_DEBUG("\nBEGIN SET FLASH HEADER\n");
NODE_DBG("\nBEGIN SET FLASH HEADER\n");
uint8_t data[SPI_FLASH_SEC_SIZE] ICACHE_STORE_ATTR;
if (SPI_FLASH_RESULT_OK == spi_flash_read(0, (uint32 *)data, SPI_FLASH_SEC_SIZE))
{
((SPIFlashInfo *)(&data[0]))->size = size;
if (SPI_FLASH_RESULT_OK == spi_flash_erase_sector(0 * SPI_FLASH_SEC_SIZE))
{
FLASH_DEBUG("\nERASE SUCCESS\n");
NODE_DBG("\nERASE SUCCESS\n");
}
if (SPI_FLASH_RESULT_OK == spi_flash_write(0, (uint32 *)data, SPI_FLASH_SEC_SIZE))
{
FLASH_DEBUG("\nWRITE SUCCESS, %u\n", size);
NODE_DBG("\nWRITE SUCCESS, %u\n", size);
}
}
FLASH_DEBUG("\nEND SET FLASH HEADER\n");
NODE_DBG("\nEND SET FLASH HEADER\n");
return true;
}

Expand Down Expand Up @@ -278,7 +281,7 @@ bool flash_rom_set_speed(uint32_t speed)
// Dangerous, here are dinosaur infested!!!!!
// Reboot required!!!
// If you don't know what you're doing, your nodemcu may turn into stone ...
FLASH_DEBUG("\nBEGIN SET FLASH HEADER\n");
NODE_DBG("\nBEGIN SET FLASH HEADER\n");
uint8_t data[SPI_FLASH_SEC_SIZE] ICACHE_STORE_ATTR;
uint8_t speed_type = SPEED_40MHZ;
if (speed < 26700000)
Expand All @@ -302,14 +305,14 @@ bool flash_rom_set_speed(uint32_t speed)
((SPIFlashInfo *)(&data[0]))->speed = speed_type;
if (SPI_FLASH_RESULT_OK == spi_flash_erase_sector(0 * SPI_FLASH_SEC_SIZE))
{
FLASH_DEBUG("\nERASE SUCCESS\n");
NODE_DBG("\nERASE SUCCESS\n");
}
if (SPI_FLASH_RESULT_OK == spi_flash_write(0, (uint32 *)data, SPI_FLASH_SEC_SIZE))
{
FLASH_DEBUG("\nWRITE SUCCESS, %u\n", speed_type);
NODE_DBG("\nWRITE SUCCESS, %u\n", speed_type);
}
}
FLASH_DEBUG("\nEND SET FLASH HEADER\n");
NODE_DBG("\nEND SET FLASH HEADER\n");
return true;
}

Expand Down Expand Up @@ -390,7 +393,7 @@ uint8_t byte_of_aligned_array(const uint8_t *aligned_array, uint32_t index)
{
if ( (((uint32_t)aligned_array) % 4) != 0 )
{
FLASH_DEBUG("aligned_array is not 4-byte aligned.\n");
NODE_DBG("aligned_array is not 4-byte aligned.\n");
return 0;
}
volatile uint32_t v = ((uint32_t *)aligned_array)[ index / 4 ];
Expand All @@ -402,7 +405,7 @@ uint16_t word_of_aligned_array(const uint16_t *aligned_array, uint32_t index)
{
if ( (((uint32_t)aligned_array) % 4) != 0 )
{
FLASH_DEBUG("aligned_array is not 4-byte aligned.\n");
NODE_DBG("aligned_array is not 4-byte aligned.\n");
return 0;
}
volatile uint32_t v = ((uint32_t *)aligned_array)[ index / 2 ];
Expand Down
8 changes: 3 additions & 5 deletions app/platform/flash_api.h
@@ -1,8 +1,8 @@
#ifndef __FLASH_API_H__
#define __FLASH_API_H__
#include "ets_sys.h"
#include "user_config.h"
#include "user_interface.h"
#include "spi_flash.h"
#include "cpu_esp8266.h"

#define FLASH_ADDRESS_START_MAP (INTERNAL_FLASH_START_ADDRESS)

Expand All @@ -22,8 +22,6 @@
#define FLASH_SIZE_8MBYTE (FLASH_SIZE_64MBIT / 8)
#define FLASH_SIZE_16MBYTE (FLASH_SIZE_128MBIT/ 8)

#define FLASH_DEBUG

#define FLASH_SAFEMODE_ENTER() \
do { \
extern SpiFlashChip * flashchip; \
Expand Down Expand Up @@ -87,7 +85,7 @@ typedef struct
} size : 4;
uint32_t entry_point;
uint32_t memory_offset;
uint32_t segment_size;
uint32_t segment_size;
} ICACHE_STORE_TYPEDEF_ATTR SPIFlashInfo;

uint32_t flash_detect_size_byte(void);
Expand Down
2 changes: 1 addition & 1 deletion app/spiffs/docs/INTEGRATION
Expand Up @@ -303,4 +303,4 @@ Having these figures you can disable SPIFFS_BUFFER_HELP again to save flash.

* HOW TO CONFIG

TODO
TODO
16 changes: 1 addition & 15 deletions app/spiffs/docs/TODO
@@ -1,15 +1 @@
* When mending lost pages, also see if they fit into length specified in object index header

SPIFFS2 thoughts

* Instead of exact object id:s in the object lookup tables, use a hash of span index and object id.
Eg. object id xor:ed with bit-reversed span index.
This should decrease number of actual pages that needs to be visited when looking thru the obj lut.

* Logical number of each block. When moving stuff in a garbage collected page, the free
page is assigned the same number as the garbage collected. Thus, object index pages do not have to
be rewritten.

* Steal one page, use as a bit parity page. When starting an fs modification operation, write one bit
as zero. When ending, write another bit as zero. On mount, if number of zeroes in page is uneven, a
check is automatically run.
* When mending lost pages, also see if they fit into length specified in object index header
45 changes: 13 additions & 32 deletions app/spiffs/spiffs.c
Expand Up @@ -4,7 +4,7 @@

spiffs fs;

#define LOG_PAGE_SIZE (256*2)
#define LOG_PAGE_SIZE 256

static u8_t spiffs_work_buf[LOG_PAGE_SIZE*2];
static u8_t spiffs_fds[32*4];
Expand Down Expand Up @@ -49,9 +49,9 @@ void myspiffs_mount() {
cfg.phys_addr &= 0xFFFFC000; // align to 4 sector.
cfg.phys_size = INTERNAL_FLASH_SIZE - ( ( u32_t )cfg.phys_addr - INTERNAL_FLASH_START_ADDRESS );
cfg.phys_erase_block = INTERNAL_FLASH_SECTOR_SIZE; // according to datasheet
cfg.log_block_size = INTERNAL_FLASH_SECTOR_SIZE * 16; // let us not complicate things
cfg.log_block_size = INTERNAL_FLASH_SECTOR_SIZE; // let us not complicate things
cfg.log_page_size = LOG_PAGE_SIZE; // as we said
SPIFFS_DBG("fs.start:%x,max:%x\n",cfg.phys_addr,cfg.phys_size);
NODE_DBG("fs.start:%x,max:%x\n",cfg.phys_addr,cfg.phys_size);

cfg.hal_read_f = my_spiffs_read;
cfg.hal_write_f = my_spiffs_write;
Expand All @@ -66,7 +66,7 @@ void myspiffs_mount() {
sizeof(spiffs_cache),
// myspiffs_check_callback);
0);
SPIFFS_DBG("mount res: %i\n", res);
NODE_DBG("mount res: %i\n", res);
}

void myspiffs_unmount() {
Expand All @@ -77,35 +77,20 @@ void myspiffs_unmount() {
// Returns 1 if OK, 0 for error
int myspiffs_format( void )
{
#if 1
myspiffs_unmount();
myspiffs_mount();
myspiffs_unmount();
if(0 == SPIFFS_format(&fs))
{
myspiffs_mount();
return 1;
}
else
{
return 0;
}
#else
SPIFFS_unmount(&fs);
u32_t sect_first, sect_last;
sect_first = ( u32_t )platform_flash_get_first_free_block_address( NULL );
sect_first += 0x3000;
sect_first &= 0xFFFFC000; // align to 4 sector.
sect_first = platform_flash_get_sector_of_address(sect_first);
sect_last = INTERNAL_FLASH_SIZE + INTERNAL_FLASH_START_ADDRESS - 4;
sect_last = platform_flash_get_sector_of_address(sect_last);
SPIFFS_DBG("sect_first: %x, sect_last: %x\n", sect_first, sect_last);
NODE_DBG("sect_first: %x, sect_last: %x\n", sect_first, sect_last);
while( sect_first <= sect_last )
if( platform_flash_erase_sector( sect_first ++ ) == PLATFORM_ERR )
return 0;
myspiffs_mount();

return 1;
#endif
}

int myspiffs_check( void )
Expand All @@ -117,11 +102,7 @@ int myspiffs_check( void )
}

int myspiffs_open(const char *name, int flags){
int res = SPIFFS_open(&fs, (char *)name, (spiffs_flags)flags, 0);
if (res < 0) {
SPIFFS_DBG("open errno %i\n", SPIFFS_errno(&fs));
}
return res;
return (int)SPIFFS_open(&fs, (char *)name, (spiffs_flags)flags, 0);
}

int myspiffs_close( int fd ){
Expand All @@ -137,15 +118,15 @@ size_t myspiffs_write( int fd, const void* ptr, size_t len ){
#endif
int res = SPIFFS_write(&fs, (spiffs_file)fd, (void *)ptr, len);
if (res < 0) {
SPIFFS_DBG("write errno %i\n", SPIFFS_errno(&fs));
NODE_DBG("write errno %i\n", SPIFFS_errno(&fs));
return 0;
}
return res;
}
size_t myspiffs_read( int fd, void* ptr, size_t len){
int res = SPIFFS_read(&fs, (spiffs_file)fd, ptr, len);
if (res < 0) {
SPIFFS_DBG("read errno %i\n", SPIFFS_errno(&fs));
NODE_DBG("read errno %i\n", SPIFFS_errno(&fs));
return 0;
}
return res;
Expand All @@ -165,7 +146,7 @@ int myspiffs_getc( int fd ){
if(!myspiffs_eof(fd)){
res = SPIFFS_read(&fs, (spiffs_file)fd, &c, 1);
if (res != 1) {
SPIFFS_DBG("getc errno %i\n", SPIFFS_errno(&fs));
NODE_DBG("getc errno %i\n", SPIFFS_errno(&fs));
return (int)EOF;
} else {
return (int)c;
Expand Down Expand Up @@ -198,13 +179,13 @@ void test_spiffs() {
// Surely, I've mounted spiffs before entering here

spiffs_file fd = SPIFFS_open(&fs, "my_file", SPIFFS_CREAT | SPIFFS_TRUNC | SPIFFS_RDWR, 0);
if (SPIFFS_write(&fs, fd, (u8_t *)"Hello world", 12) < 0) SPIFFS_DBG("errno %i\n", SPIFFS_errno(&fs));
if (SPIFFS_write(&fs, fd, (u8_t *)"Hello world", 12) < 0) NODE_DBG("errno %i\n", SPIFFS_errno(&fs));
SPIFFS_close(&fs, fd);

fd = SPIFFS_open(&fs, "my_file", SPIFFS_RDWR, 0);
if (SPIFFS_read(&fs, fd, (u8_t *)buf, 12) < 0) SPIFFS_DBG("errno %i\n", SPIFFS_errno(&fs));
if (SPIFFS_read(&fs, fd, (u8_t *)buf, 12) < 0) NODE_DBG("errno %i\n", SPIFFS_errno(&fs));
SPIFFS_close(&fs, fd);

SPIFFS_DBG("--> %s <--\n", buf);
NODE_DBG("--> %s <--\n", buf);
}
#endif

0 comments on commit fcde022

Please sign in to comment.