flash_diskio.c can write - but read crashes

This commit is contained in:
wilberforce 2017-03-21 00:31:12 +13:00
parent 017ccaff15
commit 0460845615
5 changed files with 190 additions and 73 deletions

View File

@ -312,8 +312,14 @@ SOURCES += \
libs/filesystem/fat_sd/sdio_diskio.c \
libs/filesystem/fat_sd/sdio_sdcard.c
else #USE_FILESYSTEM_SDIO
ifdef USE_FLASH_FILESYSTEM
DEFINES += -DUSE_FLASH_FILESYSTEM
SOURCES += \
libs/filesystem/fat_sd/flash_diskio.c
else
SOURCES += \
libs/filesystem/fat_sd/spi_diskio.c
endif #USE_FLASH_FILESYSTEM
endif #USE_FILESYSTEM_SDIO
endif #!LINUX
endif #USE_FILESYSTEM

View File

@ -0,0 +1,138 @@
// Use Flash memory with devices with large flash - ESP8266/ESP32
/*
* @author: Wilberforce - Rhys Williams
*
*/
#include "platform_config.h"
#include "jsinteractive.h"
#include "ff.h"
#include "diskio.h"
#define FS_SECTOR_SIZE 4096
#define FS_BLOCK_SIZE 1
#define FS_SECTOR_COUNT 256; // 1024*1024 / 4096 = 256
// Hardcode page of 4Mb memory
#define FS_FLASH_BASE 0x200000
/*--------------------------------------------------------------------------
Public Functions
---------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------*/
/* Initialize Disk Drive */
/*-----------------------------------------------------------------------*/
DSTATUS disk_initialize (
BYTE drv /* Physical drive number (0) */
)
{
NOT_USED(drv);
jsWarn("Flash Init - disk_initialize %d\n",drv);
return 0;
}
/*-----------------------------------------------------------------------*/
/* Get Disk Status */
/*-----------------------------------------------------------------------*/
DSTATUS disk_status (
BYTE drv /* Physical drive number (0) */
)
{
NOT_USED(drv);
jsWarn("Flash Init - disk_status %d\n",drv);
return 0;
}
/*-----------------------------------------------------------------------*/
/* Read Sector(s) */
/*-----------------------------------------------------------------------*/
DRESULT disk_read (
BYTE drv, /* Physical drive number (0) */
BYTE *buff, /* Pointer to the data buffer to store read data */
DWORD sector, /* Start sector number (LBA) */
UINT count /* Sector count */
)
{
uint16_t Transfer_Length;
uint32_t Memory_Offset;
Transfer_Length = count * FS_SECTOR_SIZE;
Memory_Offset = sector * FS_SECTOR_SIZE;
jsWarn("Flash disk_read sector: %d, buff: %d len: %d\n", sector, buff, Transfer_Length);
jshFlashRead( buff, FS_FLASH_BASE+Memory_Offset, Transfer_Length);
return RES_OK;
}
/*-----------------------------------------------------------------------*/
/* Write Sector(s) */
/*-----------------------------------------------------------------------*/
DRESULT disk_write (
BYTE drv, /* Physical drive number (0) */
const BYTE *buff, /* Pointer to the data to be written */
DWORD sector, /* Start sector number (LBA) */
UINT count /* Sector count */
)
{
uint16_t Transfer_Length;
uint32_t Memory_Offset;
Transfer_Length = count * FS_SECTOR_SIZE;
Memory_Offset = sector * FS_SECTOR_SIZE;
jsWarn("Flash disk_write %d %d\n", buff, Transfer_Length);
jshFlashErasePage(FS_FLASH_BASE+Memory_Offset);
jshFlashWrite( buff, FS_FLASH_BASE+Memory_Offset,Transfer_Length);
return RES_OK;
}
/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions */
/*-----------------------------------------------------------------------*/
DRESULT disk_ioctl (
BYTE drv, // Physical drive number (0)
BYTE ctrl, // Control code
void *buff // Buffer to send/receive control data
)
{
DRESULT res = RES_OK;
jsWarn("Flash disk_ioctl %d\n",ctrl);
switch (ctrl) {
case CTRL_SYNC : /// Make sure that no pending write process
res = RES_OK;
jsWarn("Flash disk_ioctl CTRL_SYNC\n");
break;
case GET_SECTOR_COUNT : // Get number of sectors on the disk (DWORD)
*(DWORD*)buff = FS_SECTOR_COUNT;
res = RES_OK;
break;
case GET_SECTOR_SIZE : // Get R/W sector size (WORD)
*(WORD*)buff = FS_SECTOR_SIZE;
res = RES_OK;
break;
case GET_BLOCK_SIZE : // Get erase block size in unit of sector (DWORD)
*(DWORD*)buff = FS_BLOCK_SIZE;
res = RES_OK;
break;
}
return res;
}

View File

@ -12,18 +12,8 @@
#include "sdio_sdcard.h"
#ifdef FLASH_FS
#define FS_SECTOR_SIZE 4096
#define FS_BLOCK_SIZE 1
#define FS_SECTOR_COUNT 256; // 1024*1024 / 4096 = 256
// Hardcode last page of 4Mb memory
#define FS_FLASH_BASE 0x200000
#else
#define FS_SECTOR_SIZE 512
#define FS_BLOCK_SIZE 32
#define FS_SECTOR_COUNT 131072; // 4*1024*32 = 131072
#endif
SD_CardInfo SDCardInfo2;
/*--------------------------------------------------------------------------
@ -36,10 +26,6 @@
/* Initialize Disk Drive */
/*-----------------------------------------------------------------------*/
#ifndef FLASH_FS
SD_CardInfo SDCardInfo2;
DSTATUS disk_initialize (
BYTE drv /* Physical drive number (0) */
)
@ -63,19 +49,10 @@ DSTATUS disk_initialize (
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//NAND_Init();
return 0;
}
#else
DSTATUS disk_initialize (
BYTE drv /* Physical drive number (0) */
)
{
jsiConsolePrint("SD_Init Flash Init\n");
return 0;
}
#endif
/*-----------------------------------------------------------------------*/
/* Get Disk Status */
@ -103,19 +80,16 @@ DRESULT disk_read (
uint16_t Transfer_Length;
uint32_t Memory_Offset;
Transfer_Length = count * FS_SECTOR_SIZE;
Memory_Offset = sector * FS_SECTOR_SIZE;
Transfer_Length = count * 512;
Memory_Offset = sector * 512;
#ifdef FLASH_FS
jsiConsolePrint("Flash disk_read %d %d\n", buff, Transfer_Length);
jshFlashRead(FS_FLASH_BASE+addr, buff, Transfer_Length);
#else
SD_ReadBlock(Memory_Offset, (uint32_t *)buff, Transfer_Length);
#endif
//NAND_Read(Memory_Offset, (uint32_t *)buff, Transfer_Length);
return RES_OK;
}
/*-----------------------------------------------------------------------*/
/* Write Sector(s) */
/*-----------------------------------------------------------------------*/
@ -130,16 +104,11 @@ DRESULT disk_write (
uint16_t Transfer_Length;
uint32_t Memory_Offset;
Transfer_Length = count * FS_SECTOR_SIZE;
Memory_Offset = sector * FS_SECTOR_SIZE;
Transfer_Length = count * 512;
Memory_Offset = sector * 512;
#ifdef FLASH_FS
jsiConsolePrint("Flash disk_write %d %d\n", buff, Transfer_Length);
jshFlashErasePage(FS_FLASH_BASE+addr);
jshFlashWrite(FS_FLASH_BASE+addr, buff, Transfer_Length);
#else
SD_WriteBlock(Memory_Offset, (uint32_t *)buff, Transfer_Length);
#endif
//NAND_Write(Memory_Offset, (uint32_t *)buff, Transfer_Length);
return RES_OK;
}
@ -159,36 +128,33 @@ DRESULT disk_ioctl (
uint32_t status = SD_NO_TRANSFER;
//uint32_t status = NAND_READY;
switch (ctrl) {
case CTRL_SYNC : /// Make sure that no pending write process
#ifdef FLASH_FS
res = RES_OK;
jsiConsolePrint("Flash disk_ioctl CTRL_SYNC\n");
#else
status = SD_GetTransferState();
if (status == SD_NO_TRANSFER)
//status = FSMC_NAND_GetStatus();
//if (status == NAND_READY)
{res = RES_OK;}
else{res = RES_ERROR;}
#endif
break;
case GET_SECTOR_COUNT : // Get number of sectors on the disk (DWORD)
*(DWORD*)buff = FS_SECTOR_COUNT;
*(DWORD*)buff = 131072; // 4*1024*32 = 131072
res = RES_OK;
break;
case GET_SECTOR_SIZE : // Get R/W sector size (WORD)
*(WORD*)buff = FS_SECTOR_SIZE;
*(WORD*)buff = 512;
res = RES_OK;
break;
case GET_BLOCK_SIZE : // Get erase block size in unit of sector (DWORD)
*(DWORD*)buff = FS_BLOCK_SIZE;
*(DWORD*)buff = 32;
res = RES_OK;
break;
}
return res;
}
}

View File

@ -1,3 +1,4 @@
/*
/*
* This file is part of Espruino, a JavaScript interpreter for Microcontrollers
*
@ -19,7 +20,7 @@
#define JS_FS_DATA_NAME JS_HIDDEN_CHAR_STR"FSd" // the data in each file
#define JS_FS_OPEN_FILES_NAME JS_HIDDEN_CHAR_STR"FSo" // the list of open files
#if !defined(LINUX) && !defined(USE_FILESYSTEM_SDIO)
#if !defined(LINUX) && !defined(USE_FILESYSTEM_SDIO) && !defined(USE_FLASH_FILESYSTEM)
#define SD_CARD_ANYWHERE
#endif
@ -27,6 +28,7 @@
#ifndef LINUX
FATFS jsfsFAT;
bool fat_initialised = false;
bool use_flash_fs=false;
#endif
#ifdef SD_CARD_ANYWHERE
@ -69,6 +71,7 @@ void jsfsReportError(const char *msg, FRESULT res) {
bool jsfsInit() {
#ifndef LINUX
if (!fat_initialised) {
if ( !use_flash_fs ) {
#ifdef SD_CARD_ANYWHERE
if (!isSdSPISetup()) {
#ifdef SD_SPI
@ -88,12 +91,13 @@ bool jsfsInit() {
#endif
}
#endif
}
FRESULT res;
if ((res = f_mount(&jsfsFAT, "", 1/*immediate*/)) != FR_OK) {
jsfsReportError("Unable to mount SD card", res);
return false;
}
jsWarn("jsfsInit - appears to f_mount!");
fat_initialised = true;
}
#endif
@ -560,8 +564,7 @@ void jswrap_file_skip_or_seek(JsVar* parent, int nBytes, bool is_skip) {
Pipe this file to a stream (an object with a 'write' method)
*/
#ifdef FLASH_FS
#endif
#ifdef USE_FLASH_FILESYSTEM
/*JSON{
"type" : "staticmethod",
@ -575,19 +578,25 @@ Pipe this file to a stream (an object with a 'write' method)
]
}
Setup the flash filesystem so that subsequent calls to `E.openFile` and `require('fs').*` will use the flash area.
ESP8266 aqnd ESP32.
ESP8266 and ESP32 only.
```
E.flashFatFs(0x200000,true); // Set flash address and format the file system (needed first time)
console.log(require("fs").readdirSync());
E.flashFatFs(0x200000,0);
dd if=/dev/zero of=fat.fs bs=1024 count=1024
mkfs.vfat -v -S 4096 fat.fs.img
dd if=/dev/zero of=fat.fs.img bs=1024 count=1024
mkfs.vfat -v -F 16 -S 4096 -s 1 fat.fs.img
f=require("Flash");
f.read(10,0x200000);
var files = require("fs").readdirSync();
require("fs").writeFileSync("hello.txt", "Hello World");
console.log(require("fs").readFileSync("hello.txt")); // prints "Hello World"
```
*/
@ -595,20 +604,17 @@ f.read(10,0x200000);
#define FS_SECTOR_SIZE 4096
void jswrap_E_flashFatFs(int addr, int format) {
jsError("E.flashFatFs addr: %d format: %d\r\n", addr, format);
fat_initialised = true;
jsError("E.flashFatFs addr: %d format: %d\r\n", addr,format);
use_flash_fs=true;
if ( format == 1 ) {
jsError("E.flashFatFs formatting...");
FRESULT res = f_mkfs("/", 1, FS_SECTOR_SIZE);
if (res != FR_OK) {
jsError("[f_mkfs] Error %d\r\n", res);
jsfsReportError("Format error:",res);
}
}
FRESULT res;
if ((res = f_mount(&jsfsFAT, "", 0) != FR_OK) ) {
jsfsReportError("Unable to mount flash FS", res);
return;
}
if ( format == 1 ) {
jsError("E.flashFatFs formatting...");
FRESULT res = f_mkfs("/", 1, FS_SECTOR_SIZE);
if (res != FR_OK) {
jsError("[f_mkfs] Error %d\r\n", res);
jsfsReportError("Format error:",res);
}
}
jsError("jsfsInit: %d", jsfsInit());
}
#endif

View File

@ -74,5 +74,6 @@ size_t jswrap_file_write(JsVar* parent, JsVar* buffer);
JsVar *jswrap_file_read(JsVar* parent, int length);
void jswrap_file_skip_or_seek(JsVar* parent, int length, bool is_skip);
void jswrap_file_close(JsVar* parent);
void jswrap_E_flashFatFs(int addr, int format);
#ifdef USE_FLASH_FILESYSTEM
void jswrap_E_flashFatFs(int addr, int format);
#endif