Add FatFs and SD card support (#1397)
* Add FatFs * enable BUILD_FATFS for all-module build * push vfs into rest of firmware * align maximum filename length * increase timeout for acmd41 during card initialization * switch from DOS to Unix path semantics chdrive() is substituted by chdir() * update to fatfs R.012a incl. patches 1-6 * add callback for rtc provisioning in file * update docs
This commit is contained in:
parent
99cd2177bb
commit
ecf8bd98d6
|
@ -43,6 +43,7 @@ SUBDIRS= \
|
|||
tsl2561 \
|
||||
net \
|
||||
http \
|
||||
fatfs \
|
||||
websocket
|
||||
|
||||
endif # } PDIR
|
||||
|
@ -83,6 +84,7 @@ COMPONENTS_eagle.app.v6 = \
|
|||
ucglib/ucglib.a \
|
||||
smart/smart.a \
|
||||
spiffs/spiffs.a \
|
||||
fatfs/libfatfs.a \
|
||||
cjson/libcjson.a \
|
||||
crypto/libcrypto.a \
|
||||
dhtlib/libdhtlib.a \
|
||||
|
|
|
@ -41,6 +41,7 @@ STD_CFLAGS=-std=gnu11 -Wimplicit
|
|||
INCLUDES := $(INCLUDES) -I $(PDIR)include
|
||||
INCLUDES += -I ./
|
||||
INCLUDES += -I ../libc
|
||||
INCLUDES += -I ../platform
|
||||
PDIR := ../$(PDIR)
|
||||
sinclude $(PDIR)Makefile
|
||||
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
* SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
#include "vfs.h"
|
||||
#include "digests.h"
|
||||
#include "user_config.h"
|
||||
#include "rom.h"
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
typedef void (*create_ctx_fn)(void *ctx);
|
||||
typedef void (*update_ctx_fn)(void *ctx, const uint8_t *msg, int len);
|
||||
typedef void (*finalize_ctx_fn)(uint8_t *digest, void *ctx);
|
||||
typedef size_t ( *read_fn )(int fd, void *ptr, size_t len);
|
||||
typedef sint32_t ( *read_fn )(int fd, void *ptr, size_t len);
|
||||
|
||||
/**
|
||||
* Description of a message digest mechanism.
|
||||
|
|
187
app/driver/spi.c
187
app/driver/spi.c
|
@ -1,5 +1,12 @@
|
|||
#include "driver/spi.h"
|
||||
|
||||
typedef union {
|
||||
uint32 word[2];
|
||||
uint64 dword;
|
||||
} spi_buf_t;
|
||||
|
||||
static uint32_t spi_clkdiv[2];
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
* FunctionName : spi_lcd_mode_init
|
||||
|
@ -59,6 +66,45 @@ void spi_lcd_9bit_write(uint8 spi_no,uint8 high_bit,uint8 low_8bit)
|
|||
SET_PERI_REG_MASK(SPI_CMD(spi_no), SPI_USR); //transmission start
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
* FunctionName : spi_set_clkdiv
|
||||
* Description : Set the clock divider
|
||||
* Parameters : uint8 spi_no - SPI module number, Only "SPI" and "HSPI" are valid
|
||||
* uint32 clock_div - new clock divider
|
||||
* Returns : uint32 - previous clock divider
|
||||
*******************************************************************************/
|
||||
uint32_t spi_set_clkdiv(uint8 spi_no, uint32_t clock_div)
|
||||
{
|
||||
uint32_t tmp_clkdiv;
|
||||
|
||||
if (spi_no > 1) return 0; //handle invalid input number
|
||||
tmp_clkdiv = spi_clkdiv[spi_no];
|
||||
|
||||
if (clock_div > 1) {
|
||||
uint8 i, k;
|
||||
i = (clock_div / 40) ? (clock_div / 40) : 1;
|
||||
k = clock_div / i;
|
||||
WRITE_PERI_REG(SPI_CLOCK(spi_no),
|
||||
(((i - 1) & SPI_CLKDIV_PRE) << SPI_CLKDIV_PRE_S) |
|
||||
(((k - 1) & SPI_CLKCNT_N) << SPI_CLKCNT_N_S) |
|
||||
((((k + 1) / 2 - 1) & SPI_CLKCNT_H) << SPI_CLKCNT_H_S) |
|
||||
(((k - 1) & SPI_CLKCNT_L) << SPI_CLKCNT_L_S)); //clear bit 31,set SPI clock div
|
||||
} else {
|
||||
WRITE_PERI_REG(SPI_CLOCK(spi_no), SPI_CLK_EQU_SYSCLK); // 80Mhz speed
|
||||
}
|
||||
|
||||
if(spi_no==SPI){
|
||||
WRITE_PERI_REG(PERIPHS_IO_MUX, 0x005 | (clock_div <= 1 ? 0x100 : 0));
|
||||
}
|
||||
else if(spi_no==HSPI){
|
||||
WRITE_PERI_REG(PERIPHS_IO_MUX, 0x105 | (clock_div <= 1 ? 0x200 : 0));
|
||||
}
|
||||
|
||||
spi_clkdiv[spi_no] = clock_div;
|
||||
|
||||
return tmp_clkdiv;
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
* FunctionName : spi_master_init
|
||||
* Description : SPI master initial function for common byte units transmission
|
||||
|
@ -96,28 +142,15 @@ void spi_master_init(uint8 spi_no, unsigned cpol, unsigned cpha, uint32_t clock_
|
|||
//clear Dual or Quad lines transmission mode
|
||||
CLEAR_PERI_REG_MASK(SPI_CTRL(spi_no), SPI_QIO_MODE|SPI_DIO_MODE|SPI_DOUT_MODE|SPI_QOUT_MODE);
|
||||
|
||||
if (clock_div > 1) {
|
||||
uint8 i, k;
|
||||
i = (clock_div / 40) ? (clock_div / 40) : 1;
|
||||
k = clock_div / i;
|
||||
WRITE_PERI_REG(SPI_CLOCK(spi_no),
|
||||
(((i - 1) & SPI_CLKDIV_PRE) << SPI_CLKDIV_PRE_S) |
|
||||
(((k - 1) & SPI_CLKCNT_N) << SPI_CLKCNT_N_S) |
|
||||
((((k + 1) / 2 - 1) & SPI_CLKCNT_H) << SPI_CLKCNT_H_S) |
|
||||
(((k - 1) & SPI_CLKCNT_L) << SPI_CLKCNT_L_S)); //clear bit 31,set SPI clock div
|
||||
} else {
|
||||
WRITE_PERI_REG(SPI_CLOCK(spi_no), SPI_CLK_EQU_SYSCLK); // 80Mhz speed
|
||||
}
|
||||
spi_set_clkdiv(spi_no, clock_div);
|
||||
|
||||
if(spi_no==SPI){
|
||||
WRITE_PERI_REG(PERIPHS_IO_MUX, 0x005 | (clock_div <= 1 ? 0x100 : 0));
|
||||
PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CLK_U, 1);//configure io to spi mode
|
||||
PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CMD_U, 1);//configure io to spi mode
|
||||
PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_DATA0_U, 1);//configure io to spi mode
|
||||
PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_DATA1_U, 1);//configure io to spi mode
|
||||
}
|
||||
else if(spi_no==HSPI){
|
||||
WRITE_PERI_REG(PERIPHS_IO_MUX, 0x105 | (clock_div <= 1 ? 0x200 : 0));
|
||||
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, 2);//configure io to spi mode
|
||||
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, 2);//configure io to spi mode
|
||||
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTMS_U, 2);//configure io to spi mode
|
||||
|
@ -125,6 +158,44 @@ void spi_master_init(uint8 spi_no, unsigned cpol, unsigned cpha, uint32_t clock_
|
|||
}
|
||||
}
|
||||
|
||||
void spi_mast_byte_order(uint8 spi_no, uint8 order)
|
||||
{
|
||||
if(spi_no > 1)
|
||||
return;
|
||||
|
||||
if (order == SPI_ORDER_MSB) {
|
||||
SET_PERI_REG_MASK(SPI_USER(spi_no), SPI_RD_BYTE_ORDER | SPI_WR_BYTE_ORDER);
|
||||
} else if (order == SPI_ORDER_LSB) {
|
||||
CLEAR_PERI_REG_MASK(SPI_USER(spi_no), SPI_RD_BYTE_ORDER | SPI_WR_BYTE_ORDER);
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
* FunctionName : spi_mast_blkset
|
||||
* Description : Copy a block of data to the MOSI FIFO
|
||||
* Parameters : uint8 spi_no - SPI module number, Only "SPI" and "HSPI" are valid
|
||||
* size_t bitlen - number of bits to copy, multiple of 8
|
||||
* uint8 *data - pointer to data buffer
|
||||
*******************************************************************************/
|
||||
void spi_mast_blkset(uint8 spi_no, size_t bitlen, const uint8 *data)
|
||||
{
|
||||
while(READ_PERI_REG(SPI_CMD(spi_no)) & SPI_USR);
|
||||
os_memcpy((void *)SPI_W0(spi_no), (const void *)data, bitlen >> 3);
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
* FunctionName : spi_mast_blkget
|
||||
* Description : Copy a block of data from the MISO FIFO
|
||||
* Parameters : uint8 spi_no - SPI module number, Only "SPI" and "HSPI" are valid
|
||||
* size_t bitlen - number of bits to copy, multiple of 8
|
||||
* uint8 *data - pointer to data buffer
|
||||
*******************************************************************************/
|
||||
void spi_mast_blkget(uint8 spi_no, size_t bitlen, uint8 *data)
|
||||
{
|
||||
while(READ_PERI_REG(SPI_CMD(spi_no)) & SPI_USR);
|
||||
os_memcpy((void *)data, (void *)SPI_W0(spi_no), bitlen >> 3);
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
* FunctionName : spi_mast_set_mosi
|
||||
* Description : Enter provided data into MOSI buffer.
|
||||
|
@ -137,48 +208,36 @@ void spi_master_init(uint8 spi_no, unsigned cpol, unsigned cpha, uint32_t clock_
|
|||
*******************************************************************************/
|
||||
void spi_mast_set_mosi(uint8 spi_no, uint16 offset, uint8 bitlen, uint32 data)
|
||||
{
|
||||
uint8 wn, wn_offset, wn_bitlen;
|
||||
uint32 wn_data;
|
||||
uint8 wn, shift;
|
||||
spi_buf_t spi_buf;
|
||||
|
||||
if (spi_no > 1)
|
||||
return; // handle invalid input number
|
||||
if (bitlen > 32)
|
||||
return; // handle invalid input number
|
||||
|
||||
while(READ_PERI_REG(SPI_CMD(spi_no)) & SPI_USR);
|
||||
|
||||
// determine which SPI_Wn register is addressed
|
||||
wn = offset >> 5;
|
||||
if (wn > 15)
|
||||
if (wn > 15) {
|
||||
return; // out of range
|
||||
wn_offset = offset & 0x1f;
|
||||
if (32 - wn_offset < bitlen)
|
||||
{
|
||||
// splitting required
|
||||
wn_bitlen = 32 - wn_offset;
|
||||
wn_data = data >> (bitlen - wn_bitlen);
|
||||
}
|
||||
else
|
||||
{
|
||||
wn_bitlen = bitlen;
|
||||
wn_data = data;
|
||||
}
|
||||
|
||||
do
|
||||
{
|
||||
// write payload data to SPI_Wn
|
||||
SET_PERI_REG_BITS(REG_SPI_BASE(spi_no) +0x40 + wn*4, BIT(wn_bitlen) - 1, wn_data, 32 - (wn_offset + wn_bitlen));
|
||||
while(READ_PERI_REG(SPI_CMD(spi_no)) & SPI_USR);
|
||||
|
||||
// prepare writing of dangling data part
|
||||
wn += 1;
|
||||
wn_offset = 0;
|
||||
if (wn <= 15)
|
||||
bitlen -= wn_bitlen;
|
||||
else
|
||||
bitlen = 0; // force abort
|
||||
wn_bitlen = bitlen;
|
||||
wn_data = data;
|
||||
} while (bitlen > 0);
|
||||
// transfer Wn to buf
|
||||
spi_buf.word[1] = READ_PERI_REG(SPI_W0(spi_no) + wn*4);
|
||||
if (wn < 15) {
|
||||
spi_buf.word[0] = READ_PERI_REG(SPI_W0(spi_no) + (wn+1)*4);
|
||||
}
|
||||
|
||||
shift = 64 - (offset & 0x1f) - bitlen;
|
||||
spi_buf.dword &= ~((1ULL << bitlen)-1 << shift);
|
||||
spi_buf.dword |= (uint64)data << shift;
|
||||
|
||||
if (wn < 15) {
|
||||
WRITE_PERI_REG(SPI_W0(spi_no) + (wn+1)*4, spi_buf.word[0]);
|
||||
}
|
||||
WRITE_PERI_REG(SPI_W0(spi_no) + wn*4, spi_buf.word[1]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
@ -194,46 +253,30 @@ void spi_mast_set_mosi(uint8 spi_no, uint16 offset, uint8 bitlen, uint32 data)
|
|||
*******************************************************************************/
|
||||
uint32 spi_mast_get_miso(uint8 spi_no, uint16 offset, uint8 bitlen)
|
||||
{
|
||||
uint8 wn, wn_offset, wn_bitlen;
|
||||
uint32 wn_data = 0;
|
||||
uint8 wn;
|
||||
spi_buf_t spi_buf;
|
||||
uint32 result;
|
||||
|
||||
if (spi_no > 1)
|
||||
return 0; // handle invalid input number
|
||||
|
||||
while(READ_PERI_REG(SPI_CMD(spi_no)) & SPI_USR);
|
||||
|
||||
// determine which SPI_Wn register is addressed
|
||||
wn = offset >> 5;
|
||||
if (wn > 15)
|
||||
return 0; // out of range
|
||||
wn_offset = offset & 0x1f;
|
||||
|
||||
if (bitlen > (32 - wn_offset))
|
||||
{
|
||||
// splitting required
|
||||
wn_bitlen = 32 - wn_offset;
|
||||
}
|
||||
else
|
||||
{
|
||||
wn_bitlen = bitlen;
|
||||
while(READ_PERI_REG(SPI_CMD(spi_no)) & SPI_USR);
|
||||
|
||||
// transfer Wn to buf
|
||||
spi_buf.word[1] = READ_PERI_REG(SPI_W0(spi_no) + wn*4);
|
||||
if (wn < 15) {
|
||||
spi_buf.word[0] = READ_PERI_REG(SPI_W0(spi_no) + (wn+1)*4);
|
||||
}
|
||||
|
||||
do
|
||||
{
|
||||
wn_data |= (READ_PERI_REG(REG_SPI_BASE(spi_no) +0x40 + wn*4) >> (32 - (wn_offset + wn_bitlen))) & (BIT(wn_bitlen) - 1);
|
||||
result = (uint32)(spi_buf.dword >> (64 - ((offset & 0x1f) + bitlen)));
|
||||
result &= (1UL << bitlen)-1;
|
||||
|
||||
// prepare reading of dangling data part
|
||||
wn_data <<= bitlen - wn_bitlen;
|
||||
wn += 1;
|
||||
wn_offset = 0;
|
||||
if (wn <= 15)
|
||||
bitlen -= wn_bitlen;
|
||||
else
|
||||
bitlen = 0; // force abort
|
||||
wn_bitlen = bitlen;
|
||||
} while (bitlen > 0);
|
||||
|
||||
return wn_data;
|
||||
return result;
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
|
|
|
@ -0,0 +1,267 @@
|
|||
----------------------------------------------------------------------------
|
||||
Revision history of FatFs module
|
||||
----------------------------------------------------------------------------
|
||||
|
||||
R0.00 (February 26, 2006)
|
||||
|
||||
Prototype.
|
||||
|
||||
|
||||
|
||||
R0.01 (April 29, 2006)
|
||||
|
||||
The first release.
|
||||
|
||||
|
||||
|
||||
R0.02 (June 01, 2006)
|
||||
|
||||
Added FAT12 support.
|
||||
Removed unbuffered mode.
|
||||
Fixed a problem on small (<32M) partition.
|
||||
|
||||
|
||||
|
||||
R0.02a (June 10, 2006)
|
||||
|
||||
Added a configuration option (_FS_MINIMUM).
|
||||
|
||||
|
||||
|
||||
R0.03 (September 22, 2006)
|
||||
|
||||
Added f_rename().
|
||||
Changed option _FS_MINIMUM to _FS_MINIMIZE.
|
||||
|
||||
|
||||
|
||||
R0.03a (December 11, 2006)
|
||||
|
||||
Improved cluster scan algorithm to write files fast.
|
||||
Fixed f_mkdir() creates incorrect directory on FAT32.
|
||||
|
||||
|
||||
|
||||
R0.04 (February 04, 2007)
|
||||
|
||||
Added f_mkfs().
|
||||
Supported multiple drive system.
|
||||
Changed some interfaces for multiple drive system.
|
||||
Changed f_mountdrv() to f_mount().
|
||||
|
||||
|
||||
|
||||
R0.04a (April 01, 2007)
|
||||
|
||||
Supported multiple partitions on a physical drive.
|
||||
Added a capability of extending file size to f_lseek().
|
||||
Added minimization level 3.
|
||||
Fixed an endian sensitive code in f_mkfs().
|
||||
|
||||
|
||||
|
||||
R0.04b (May 05, 2007)
|
||||
|
||||
Added a configuration option _USE_NTFLAG.
|
||||
Added FSINFO support.
|
||||
Fixed DBCS name can result FR_INVALID_NAME.
|
||||
Fixed short seek (<= csize) collapses the file object.
|
||||
|
||||
|
||||
|
||||
R0.05 (August 25, 2007)
|
||||
|
||||
Changed arguments of f_read(), f_write() and f_mkfs().
|
||||
Fixed f_mkfs() on FAT32 creates incorrect FSINFO.
|
||||
Fixed f_mkdir() on FAT32 creates incorrect directory.
|
||||
|
||||
|
||||
|
||||
R0.05a (February 03, 2008)
|
||||
|
||||
Added f_truncate() and f_utime().
|
||||
Fixed off by one error at FAT sub-type determination.
|
||||
Fixed btr in f_read() can be mistruncated.
|
||||
Fixed cached sector is not flushed when create and close without write.
|
||||
|
||||
|
||||
|
||||
R0.06 (April 01, 2008)
|
||||
|
||||
Added fputc(), fputs(), fprintf() and fgets().
|
||||
Improved performance of f_lseek() on moving to the same or following cluster.
|
||||
|
||||
|
||||
|
||||
R0.07 (April 01, 2009)
|
||||
|
||||
Merged Tiny-FatFs as a configuration option. (_FS_TINY)
|
||||
Added long file name feature. (_USE_LFN)
|
||||
Added multiple code page feature. (_CODE_PAGE)
|
||||
Added re-entrancy for multitask operation. (_FS_REENTRANT)
|
||||
Added auto cluster size selection to f_mkfs().
|
||||
Added rewind option to f_readdir().
|
||||
Changed result code of critical errors.
|
||||
Renamed string functions to avoid name collision.
|
||||
|
||||
|
||||
|
||||
R0.07a (April 14, 2009)
|
||||
|
||||
Septemberarated out OS dependent code on reentrant cfg.
|
||||
Added multiple sector size feature.
|
||||
|
||||
|
||||
|
||||
R0.07c (June 21, 2009)
|
||||
|
||||
Fixed f_unlink() can return FR_OK on error.
|
||||
Fixed wrong cache control in f_lseek().
|
||||
Added relative path feature.
|
||||
Added f_chdir() and f_chdrive().
|
||||
Added proper case conversion to extended character.
|
||||
|
||||
|
||||
|
||||
R0.07e (November 03, 2009)
|
||||
|
||||
Septemberarated out configuration options from ff.h to ffconf.h.
|
||||
Fixed f_unlink() fails to remove a sub-directory on _FS_RPATH.
|
||||
Fixed name matching error on the 13 character boundary.
|
||||
Added a configuration option, _LFN_UNICODE.
|
||||
Changed f_readdir() to return the SFN with always upper case on non-LFN cfg.
|
||||
|
||||
|
||||
|
||||
R0.08 (May 15, 2010)
|
||||
|
||||
Added a memory configuration option. (_USE_LFN = 3)
|
||||
Added file lock feature. (_FS_SHARE)
|
||||
Added fast seek feature. (_USE_FASTSEEK)
|
||||
Changed some types on the API, XCHAR->TCHAR.
|
||||
Changed .fname in the FILINFO structure on Unicode cfg.
|
||||
String functions support UTF-8 encoding files on Unicode cfg.
|
||||
|
||||
|
||||
|
||||
R0.08a (August 16, 2010)
|
||||
|
||||
Added f_getcwd(). (_FS_RPATH = 2)
|
||||
Added sector erase feature. (_USE_ERASE)
|
||||
Moved file lock semaphore table from fs object to the bss.
|
||||
Fixed f_mkfs() creates wrong FAT32 volume.
|
||||
|
||||
|
||||
|
||||
R0.08b (January 15, 2011)
|
||||
|
||||
Fast seek feature is also applied to f_read() and f_write().
|
||||
f_lseek() reports required table size on creating CLMP.
|
||||
Extended format syntax of f_printf().
|
||||
Ignores duplicated directory separators in given path name.
|
||||
|
||||
|
||||
|
||||
R0.09 (September 06, 2011)
|
||||
|
||||
f_mkfs() supports multiple partition to complete the multiple partition feature.
|
||||
Added f_fdisk().
|
||||
|
||||
|
||||
|
||||
R0.09a (August 27, 2012)
|
||||
|
||||
Changed f_open() and f_opendir() reject null object pointer to avoid crash.
|
||||
Changed option name _FS_SHARE to _FS_LOCK.
|
||||
Fixed assertion failure due to OS/2 EA on FAT12/16 volume.
|
||||
|
||||
|
||||
|
||||
R0.09b (January 24, 2013)
|
||||
|
||||
Added f_setlabel() and f_getlabel().
|
||||
|
||||
|
||||
|
||||
R0.10 (October 02, 2013)
|
||||
|
||||
Added selection of character encoding on the file. (_STRF_ENCODE)
|
||||
Added f_closedir().
|
||||
Added forced full FAT scan for f_getfree(). (_FS_NOFSINFO)
|
||||
Added forced mount feature with changes of f_mount().
|
||||
Improved behavior of volume auto detection.
|
||||
Improved write throughput of f_puts() and f_printf().
|
||||
Changed argument of f_chdrive(), f_mkfs(), disk_read() and disk_write().
|
||||
Fixed f_write() can be truncated when the file size is close to 4GB.
|
||||
Fixed f_open(), f_mkdir() and f_setlabel() can return incorrect value on error.
|
||||
|
||||
|
||||
|
||||
R0.10a (January 15, 2014)
|
||||
|
||||
Added arbitrary strings as drive number in the path name. (_STR_VOLUME_ID)
|
||||
Added a configuration option of minimum sector size. (_MIN_SS)
|
||||
2nd argument of f_rename() can have a drive number and it will be ignored.
|
||||
Fixed f_mount() with forced mount fails when drive number is >= 1. (appeared at R0.10)
|
||||
Fixed f_close() invalidates the file object without volume lock.
|
||||
Fixed f_closedir() returns but the volume lock is left acquired. (appeared at R0.10)
|
||||
Fixed creation of an entry with LFN fails on too many SFN collisions. (appeared at R0.07)
|
||||
|
||||
|
||||
|
||||
R0.10b (May 19, 2014)
|
||||
|
||||
Fixed a hard error in the disk I/O layer can collapse the directory entry.
|
||||
Fixed LFN entry is not deleted on delete/rename an object with lossy converted SFN. (appeared at R0.07)
|
||||
|
||||
|
||||
|
||||
R0.10c (November 09, 2014)
|
||||
|
||||
Added a configuration option for the platforms without RTC. (_FS_NORTC)
|
||||
Changed option name _USE_ERASE to _USE_TRIM.
|
||||
Fixed volume label created by Mac OS X cannot be retrieved with f_getlabel(). (appeared at R0.09b)
|
||||
Fixed a potential problem of FAT access that can appear on disk error.
|
||||
Fixed null pointer dereference on attempting to delete the root direcotry. (appeared at R0.08)
|
||||
|
||||
|
||||
|
||||
R0.11 (February 09, 2015)
|
||||
|
||||
Added f_findfirst(), f_findnext() and f_findclose(). (_USE_FIND)
|
||||
Fixed f_unlink() does not remove cluster chain of the file. (appeared at R0.10c)
|
||||
Fixed _FS_NORTC option does not work properly. (appeared at R0.10c)
|
||||
|
||||
|
||||
|
||||
R0.11a (September 05, 2015)
|
||||
|
||||
Fixed wrong media change can lead a deadlock at thread-safe configuration.
|
||||
Added code page 771, 860, 861, 863, 864, 865 and 869. (_CODE_PAGE)
|
||||
Removed some code pages actually not exist on the standard systems. (_CODE_PAGE)
|
||||
Fixed errors in the case conversion teble of code page 437 and 850 (ff.c).
|
||||
Fixed errors in the case conversion teble of Unicode (cc*.c).
|
||||
|
||||
|
||||
|
||||
R0.12 (April 12, 2016)
|
||||
|
||||
Added support for exFAT file system. (_FS_EXFAT)
|
||||
Added f_expand(). (_USE_EXPAND)
|
||||
Changed some members in FINFO structure and behavior of f_readdir().
|
||||
Added an option _USE_CHMOD.
|
||||
Removed an option _WORD_ACCESS.
|
||||
Fixed errors in the case conversion table of Unicode (cc*.c).
|
||||
|
||||
|
||||
|
||||
R0.12a (July 10, 2016)
|
||||
|
||||
Added support for creating exFAT volume with some changes of f_mkfs().
|
||||
Added a file open method FA_OPEN_APPEND. An f_lseek() following f_open() is no longer needed.
|
||||
f_forward() is available regardless of _FS_TINY.
|
||||
Fixed f_mkfs() creates wrong volume.
|
||||
Fixed compilation fails at some configurations, _USE_FASTSEEK and _USE_FORWARD.
|
||||
Fixed wrong memory read in create_name().
|
||||
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
FatFs Module Source Files R0.12a
|
||||
|
||||
|
||||
FILES
|
||||
|
||||
00readme.txt This file.
|
||||
history.txt Revision history.
|
||||
ffconf.h Configuration file for FatFs module.
|
||||
ff.h Common include file for FatFs and application module.
|
||||
ff.c FatFs module.
|
||||
diskio.h Common include file for FatFs and disk I/O module.
|
||||
diskio.c An example of glue function to attach existing disk I/O module to FatFs.
|
||||
integer.h Integer type definitions for FatFs.
|
||||
option Optional external functions.
|
||||
|
||||
|
||||
Low level disk I/O module is not included in this archive because the FatFs
|
||||
module is only a generic file system layer and not depend on any specific
|
||||
storage device. You have to provide a low level disk I/O module that written
|
||||
to control the target storage device.
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
|
||||
#############################################################
|
||||
# Required variables for each makefile
|
||||
# Discard this section from all parent makefiles
|
||||
# Expected variables (with automatic defaults):
|
||||
# CSRCS (all "C" files in the dir)
|
||||
# SUBDIRS (all subdirs with a Makefile)
|
||||
# GEN_LIBS - list of libs to be generated ()
|
||||
# GEN_IMAGES - list of images to be generated ()
|
||||
# COMPONENTS_xxx - a list of libs/objs in the form
|
||||
# subdir/lib to be extracted and rolled up into
|
||||
# a generated lib/image xxx.a ()
|
||||
#
|
||||
ifndef PDIR
|
||||
|
||||
UP_EXTRACT_DIR = ..
|
||||
GEN_LIBS = libfatfs.a
|
||||
COMPONENTS_libfatfs = option/liboption.a
|
||||
|
||||
endif
|
||||
|
||||
ifndef FATFS_INC_DIR
|
||||
FATFS_INC_DIR = ./
|
||||
endif
|
||||
|
||||
STD_CFLAGS=-std=gnu11 -Wimplicit -imacros $(FATFS_INC_DIR)fatfs_prefix_lib.h
|
||||
|
||||
#############################################################
|
||||
# Configuration i.e. compile options etc.
|
||||
# Target specific stuff (defines etc.) goes in here!
|
||||
# Generally values applying to a tree are captured in the
|
||||
# makefile at its root level - these are then overridden
|
||||
# for a subtree within the makefile rooted therein
|
||||
#
|
||||
#DEFINES +=
|
||||
|
||||
#############################################################
|
||||
# Recursion Magic - Don't touch this!!
|
||||
#
|
||||
# Each subtree potentially has an include directory
|
||||
# corresponding to the common APIs applicable to modules
|
||||
# rooted at that subtree. Accordingly, the INCLUDE PATH
|
||||
# of a module can only contain the include directories up
|
||||
# its parent path, and not its siblings
|
||||
#
|
||||
# Required for each makefile to inherit from the parent
|
||||
#
|
||||
|
||||
INCLUDES := $(INCLUDES) -I $(PDIR)include
|
||||
INCLUDES += -I ./
|
||||
INCLUDES += -I ../platform
|
||||
INCLUDES += -I ../libc
|
||||
INCLUDES += -I ../lua
|
||||
PDIR := ../$(PDIR)
|
||||
sinclude $(PDIR)Makefile
|
||||
|
|
@ -0,0 +1,115 @@
|
|||
/*-----------------------------------------------------------------------*/
|
||||
/* Low level disk I/O module skeleton for FatFs (C)ChaN, 2016 */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* If a working storage control module is available, it should be */
|
||||
/* attached to the FatFs via a glue function rather than modifying it. */
|
||||
/* This is an example of glue functions to attach various exsisting */
|
||||
/* storage control modules to the FatFs module with a defined API. */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
#include "diskio.h" /* FatFs lower layer API */
|
||||
#include "sdcard.h"
|
||||
|
||||
static DSTATUS m_status = STA_NOINIT;
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Get Drive Status */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
DSTATUS disk_status (
|
||||
BYTE pdrv /* Physical drive nmuber to identify the drive */
|
||||
)
|
||||
{
|
||||
return m_status;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Inidialize a Drive */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
DSTATUS disk_initialize (
|
||||
BYTE pdrv /* Physical drive nmuber to identify the drive */
|
||||
)
|
||||
{
|
||||
int result;
|
||||
|
||||
if (platform_sdcard_init( 1, pdrv )) {
|
||||
m_status &= ~STA_NOINIT;
|
||||
}
|
||||
|
||||
return m_status;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Read Sector(s) */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
DRESULT disk_read (
|
||||
BYTE pdrv, /* Physical drive nmuber to identify the drive */
|
||||
BYTE *buff, /* Data buffer to store read data */
|
||||
DWORD sector, /* Sector address in LBA */
|
||||
UINT count /* Number of sectors to read */
|
||||
)
|
||||
{
|
||||
if (count == 1) {
|
||||
if (! platform_sdcard_read_block( pdrv, sector, buff )) {
|
||||
return RES_ERROR;
|
||||
}
|
||||
} else {
|
||||
if (! platform_sdcard_read_blocks( pdrv, sector, count, buff )) {
|
||||
return RES_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
return RES_OK;
|
||||
}
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Write Sector(s) */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
DRESULT disk_write (
|
||||
BYTE pdrv, /* Physical drive nmuber to identify the drive */
|
||||
const BYTE *buff, /* Data to be written */
|
||||
DWORD sector, /* Sector address in LBA */
|
||||
UINT count /* Number of sectors to write */
|
||||
)
|
||||
{
|
||||
if (count == 1) {
|
||||
if (! platform_sdcard_write_block( pdrv, sector, buff )) {
|
||||
return RES_ERROR;
|
||||
}
|
||||
} else {
|
||||
if (! platform_sdcard_write_blocks( pdrv, sector, count, buff )) {
|
||||
return RES_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
return RES_OK;
|
||||
}
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Miscellaneous Functions */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
DRESULT disk_ioctl (
|
||||
BYTE pdrv, /* Physical drive nmuber (0..) */
|
||||
BYTE cmd, /* Control code */
|
||||
void *buff /* Buffer to send/receive control data */
|
||||
)
|
||||
{
|
||||
switch (cmd) {
|
||||
case CTRL_TRIM: /* no-op */
|
||||
case CTRL_SYNC: /* no-op */
|
||||
return RES_OK;
|
||||
|
||||
default: /* anything else throws parameter error */
|
||||
return RES_PARERR;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,80 @@
|
|||
/*-----------------------------------------------------------------------/
|
||||
/ Low level disk interface modlue include file (C)ChaN, 2014 /
|
||||
/-----------------------------------------------------------------------*/
|
||||
|
||||
#ifndef _DISKIO_DEFINED
|
||||
#define _DISKIO_DEFINED
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "integer.h"
|
||||
|
||||
|
||||
/* Status of Disk Functions */
|
||||
typedef BYTE DSTATUS;
|
||||
|
||||
/* Results of Disk Functions */
|
||||
typedef enum {
|
||||
RES_OK = 0, /* 0: Successful */
|
||||
RES_ERROR, /* 1: R/W Error */
|
||||
RES_WRPRT, /* 2: Write Protected */
|
||||
RES_NOTRDY, /* 3: Not Ready */
|
||||
RES_PARERR /* 4: Invalid Parameter */
|
||||
} DRESULT;
|
||||
|
||||
|
||||
/*---------------------------------------*/
|
||||
/* Prototypes for disk control functions */
|
||||
|
||||
|
||||
DSTATUS disk_initialize (BYTE pdrv);
|
||||
DSTATUS disk_status (BYTE pdrv);
|
||||
DRESULT disk_read (BYTE pdrv, BYTE* buff, DWORD sector, UINT count);
|
||||
DRESULT disk_write (BYTE pdrv, const BYTE* buff, DWORD sector, UINT count);
|
||||
DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff);
|
||||
|
||||
|
||||
/* Disk Status Bits (DSTATUS) */
|
||||
|
||||
#define STA_NOINIT 0x01 /* Drive not initialized */
|
||||
#define STA_NODISK 0x02 /* No medium in the drive */
|
||||
#define STA_PROTECT 0x04 /* Write protected */
|
||||
|
||||
|
||||
/* Command code for disk_ioctrl fucntion */
|
||||
|
||||
/* Generic command (Used by FatFs) */
|
||||
#define CTRL_SYNC 0 /* Complete pending write process (needed at _FS_READONLY == 0) */
|
||||
#define GET_SECTOR_COUNT 1 /* Get media size (needed at _USE_MKFS == 1) */
|
||||
#define GET_SECTOR_SIZE 2 /* Get sector size (needed at _MAX_SS != _MIN_SS) */
|
||||
#define GET_BLOCK_SIZE 3 /* Get erase block size (needed at _USE_MKFS == 1) */
|
||||
#define CTRL_TRIM 4 /* Inform device that the data on the block of sectors is no longer used (needed at _USE_TRIM == 1) */
|
||||
|
||||
/* Generic command (Not used by FatFs) */
|
||||
#define CTRL_POWER 5 /* Get/Set power status */
|
||||
#define CTRL_LOCK 6 /* Lock/Unlock media removal */
|
||||
#define CTRL_EJECT 7 /* Eject media */
|
||||
#define CTRL_FORMAT 8 /* Create physical format on the media */
|
||||
|
||||
/* MMC/SDC specific ioctl command */
|
||||
#define MMC_GET_TYPE 10 /* Get card type */
|
||||
#define MMC_GET_CSD 11 /* Get CSD */
|
||||
#define MMC_GET_CID 12 /* Get CID */
|
||||
#define MMC_GET_OCR 13 /* Get OCR */
|
||||
#define MMC_GET_SDSTAT 14 /* Get SD status */
|
||||
#define ISDIO_READ 55 /* Read data form SD iSDIO register */
|
||||
#define ISDIO_WRITE 56 /* Write data to SD iSDIO register */
|
||||
#define ISDIO_MRITE 57 /* Masked write data to SD iSDIO register */
|
||||
|
||||
/* ATA/CF specific ioctl command */
|
||||
#define ATA_GET_REV 20 /* Get F/W revision */
|
||||
#define ATA_GET_MODEL 21 /* Get model name */
|
||||
#define ATA_GET_SN 22 /* Get serial number */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -0,0 +1,24 @@
|
|||
|
||||
#define f_chdir fatfslib_f_chdir
|
||||
#define f_chdrive fatfslib_f_chdrive
|
||||
#define f_chmod fatfslib_f_chmod
|
||||
#define f_close fatfslib_f_close
|
||||
#define f_closedir fatfslib_f_closedir
|
||||
#define f_getcwd fatfslib_f_getcwd
|
||||
#define f_getfree fatfslib_f_getfree
|
||||
#define f_getlabel fatfslib_f_getlabel
|
||||
#define f_lseek fatfslib_f_lseek
|
||||
#define f_mkdir fatfslib_f_mkdir
|
||||
#define f_mount fatfslib_f_mount
|
||||
#define f_open fatfslib_f_open
|
||||
#define f_opendir fatfslib_f_opendir
|
||||
#define f_read fatfslib_f_read
|
||||
#define f_readdir fatfslib_f_readdir
|
||||
#define f_rename fatfslib_f_rename
|
||||
#define f_setlabel fatfslib_f_setlabel
|
||||
#define f_stat fatfslib_f_stat
|
||||
#define f_sync fatfslib_f_sync
|
||||
#define f_truncate fatfslib_f_truncate
|
||||
#define f_unlink fatfslib_f_unlink
|
||||
#define f_utime fatfslib_f_utime
|
||||
#define f_write fatfslib_f_write
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,366 @@
|
|||
/*----------------------------------------------------------------------------/
|
||||
/ FatFs - Generic FAT file system module R0.12a /
|
||||
/-----------------------------------------------------------------------------/
|
||||
/
|
||||
/ Copyright (C) 2016, ChaN, all right reserved.
|
||||
/
|
||||
/ FatFs module is an open source software. Redistribution and use of FatFs in
|
||||
/ source and binary forms, with or without modification, are permitted provided
|
||||
/ that the following condition is met:
|
||||
|
||||
/ 1. Redistributions of source code must retain the above copyright notice,
|
||||
/ this condition and the following disclaimer.
|
||||
/
|
||||
/ This software is provided by the copyright holder and contributors "AS IS"
|
||||
/ and any warranties related to this software are DISCLAIMED.
|
||||
/ The copyright owner or contributors be NOT LIABLE for any damages caused
|
||||
/ by use of this software.
|
||||
/----------------------------------------------------------------------------*/
|
||||
|
||||
|
||||
#ifndef _FATFS
|
||||
#define _FATFS 80186 /* Revision ID */
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "integer.h" /* Basic integer types */
|
||||
#include "ffconf.h" /* FatFs configuration options */
|
||||
|
||||
#if _FATFS != _FFCONF
|
||||
#error Wrong configuration file (ffconf.h).
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/* Definitions of volume management */
|
||||
|
||||
#if _MULTI_PARTITION /* Multiple partition configuration */
|
||||
typedef struct {
|
||||
BYTE pd; /* Physical drive number */
|
||||
BYTE pt; /* Partition: 0:Auto detect, 1-4:Forced partition) */
|
||||
} PARTITION;
|
||||
extern PARTITION VolToPart[]; /* Volume - Partition resolution table */
|
||||
#define LD2PD(vol) (VolToPart[vol].pd) /* Get physical drive number */
|
||||
#define LD2PT(vol) (VolToPart[vol].pt) /* Get partition index */
|
||||
|
||||
#else /* Single partition configuration */
|
||||
#define LD2PD(vol) (BYTE)(vol) /* Each logical drive is bound to the same physical drive number */
|
||||
#define LD2PT(vol) 0 /* Find first valid partition or in SFD */
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/* Type of path name strings on FatFs API */
|
||||
|
||||
#if _LFN_UNICODE /* Unicode (UTF-16) string */
|
||||
#if _USE_LFN == 0
|
||||
#error _LFN_UNICODE must be 0 at non-LFN cfg.
|
||||
#endif
|
||||
#ifndef _INC_TCHAR
|
||||
typedef WCHAR TCHAR;
|
||||
#define _T(x) L ## x
|
||||
#define _TEXT(x) L ## x
|
||||
#endif
|
||||
#else /* ANSI/OEM string */
|
||||
#ifndef _INC_TCHAR
|
||||
typedef char TCHAR;
|
||||
#define _T(x) x
|
||||
#define _TEXT(x) x
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/* Type of file size variables */
|
||||
|
||||
#if _FS_EXFAT
|
||||
#if _USE_LFN == 0
|
||||
#error LFN must be enabled when enable exFAT
|
||||
#endif
|
||||
typedef QWORD FSIZE_t;
|
||||
#else
|
||||
typedef DWORD FSIZE_t;
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/* File system object structure (FATFS) */
|
||||
|
||||
typedef struct {
|
||||
BYTE fs_type; /* File system type (0:N/A) */
|
||||
BYTE drv; /* Physical drive number */
|
||||
BYTE n_fats; /* Number of FATs (1 or 2) */
|
||||
BYTE wflag; /* win[] flag (b0:dirty) */
|
||||
BYTE fsi_flag; /* FSINFO flags (b7:disabled, b0:dirty) */
|
||||
WORD id; /* File system mount ID */
|
||||
WORD n_rootdir; /* Number of root directory entries (FAT12/16) */
|
||||
WORD csize; /* Cluster size [sectors] */
|
||||
#if _MAX_SS != _MIN_SS
|
||||
WORD ssize; /* Sector size (512, 1024, 2048 or 4096) */
|
||||
#endif
|
||||
#if _USE_LFN != 0
|
||||
WCHAR* lfnbuf; /* LFN working buffer */
|
||||
#endif
|
||||
#if _FS_EXFAT
|
||||
BYTE* dirbuf; /* Directory entry block scratchpad buffer */
|
||||
#endif
|
||||
#if _FS_REENTRANT
|
||||
_SYNC_t sobj; /* Identifier of sync object */
|
||||
#endif
|
||||
#if !_FS_READONLY
|
||||
DWORD last_clst; /* Last allocated cluster */
|
||||
DWORD free_clst; /* Number of free clusters */
|
||||
#endif
|
||||
#if _FS_RPATH != 0
|
||||
DWORD cdir; /* Current directory start cluster (0:root) */
|
||||
#if _FS_EXFAT
|
||||
DWORD cdc_scl; /* Containing directory start cluster (invalid when cdir is 0) */
|
||||
DWORD cdc_size; /* b31-b8:Size of containing directory, b7-b0: Chain status */
|
||||
DWORD cdc_ofs; /* Offset in the containing directory (invalid when cdir is 0) */
|
||||
#endif
|
||||
#endif
|
||||
DWORD n_fatent; /* Number of FAT entries (number of clusters + 2) */
|
||||
DWORD fsize; /* Size of an FAT [sectors] */
|
||||
DWORD volbase; /* Volume base sector */
|
||||
DWORD fatbase; /* FAT base sector */
|
||||
DWORD dirbase; /* Root directory base sector/cluster */
|
||||
DWORD database; /* Data base sector */
|
||||
DWORD winsect; /* Current sector appearing in the win[] */
|
||||
BYTE win[_MAX_SS]; /* Disk access window for Directory, FAT (and file data at tiny cfg) */
|
||||
} FATFS;
|
||||
|
||||
|
||||
|
||||
/* Object ID and allocation information (_FDID) */
|
||||
|
||||
typedef struct {
|
||||
FATFS* fs; /* Pointer to the owner file system object */
|
||||
WORD id; /* Owner file system mount ID */
|
||||
BYTE attr; /* Object attribute */
|
||||
BYTE stat; /* Object chain status (b1-0: =0:not contiguous, =2:contiguous (no data on FAT), =3:got flagmented, b2:sub-directory stretched) */
|
||||
DWORD sclust; /* Object start cluster (0:no cluster or root directory) */
|
||||
FSIZE_t objsize; /* Object size (valid when sclust != 0) */
|
||||
#if _FS_EXFAT
|
||||
DWORD n_cont; /* Size of coutiguous part, clusters - 1 (valid when stat == 3) */
|
||||
DWORD c_scl; /* Containing directory start cluster (valid when sclust != 0) */
|
||||
DWORD c_size; /* b31-b8:Size of containing directory, b7-b0: Chain status (valid when c_scl != 0) */
|
||||
DWORD c_ofs; /* Offset in the containing directory (valid when sclust != 0) */
|
||||
#endif
|
||||
#if _FS_LOCK != 0
|
||||
UINT lockid; /* File lock ID origin from 1 (index of file semaphore table Files[]) */
|
||||
#endif
|
||||
} _FDID;
|
||||
|
||||
|
||||
|
||||
/* File object structure (FIL) */
|
||||
|
||||
typedef struct {
|
||||
_FDID obj; /* Object identifier */
|
||||
BYTE flag; /* File status flags */
|
||||
BYTE err; /* Abort flag (error code) */
|
||||
FSIZE_t fptr; /* File read/write pointer (Zeroed on file open) */
|
||||
DWORD clust; /* Current cluster of fpter (invalid when fprt is 0) */
|
||||
DWORD sect; /* Sector number appearing in buf[] (0:invalid) */
|
||||
#if !_FS_READONLY
|
||||
DWORD dir_sect; /* Sector number containing the directory entry */
|
||||
BYTE* dir_ptr; /* Pointer to the directory entry in the win[] */
|
||||
#endif
|
||||
#if _USE_FASTSEEK
|
||||
DWORD* cltbl; /* Pointer to the cluster link map table (nulled on open, set by application) */
|
||||
#endif
|
||||
#if !_FS_TINY
|
||||
BYTE buf[_MAX_SS]; /* File private data read/write window */
|
||||
#endif
|
||||
} FIL;
|
||||
|
||||
|
||||
|
||||
/* Directory object structure (DIR) */
|
||||
|
||||
typedef struct {
|
||||
_FDID obj; /* Object identifier */
|
||||
DWORD dptr; /* Current read/write offset */
|
||||
DWORD clust; /* Current cluster */
|
||||
DWORD sect; /* Current sector */
|
||||
BYTE* dir; /* Pointer to the directory item in the win[] */
|
||||
BYTE fn[12]; /* SFN (in/out) {body[8],ext[3],status[1]} */
|
||||
#if _USE_LFN != 0
|
||||
DWORD blk_ofs; /* Offset of current entry block being processed (0xFFFFFFFF:Invalid) */
|
||||
#endif
|
||||
#if _USE_FIND
|
||||
const TCHAR* pat; /* Pointer to the name matching pattern */
|
||||
#endif
|
||||
} DIR;
|
||||
|
||||
|
||||
|
||||
/* File information structure (FILINFO) */
|
||||
|
||||
typedef struct {
|
||||
FSIZE_t fsize; /* File size */
|
||||
WORD fdate; /* Modified date */
|
||||
WORD ftime; /* Modified time */
|
||||
BYTE fattrib; /* File attribute */
|
||||
#if _USE_LFN != 0
|
||||
TCHAR altname[13]; /* Altenative file name */
|
||||
TCHAR fname[_MAX_LFN + 1]; /* Primary file name */
|
||||
#else
|
||||
TCHAR fname[13]; /* File name */
|
||||
#endif
|
||||
} FILINFO;
|
||||
|
||||
|
||||
|
||||
/* File function return code (FRESULT) */
|
||||
|
||||
typedef enum {
|
||||
FR_OK = 0, /* (0) Succeeded */
|
||||
FR_DISK_ERR, /* (1) A hard error occurred in the low level disk I/O layer */
|
||||
FR_INT_ERR, /* (2) Assertion failed */
|
||||
FR_NOT_READY, /* (3) The physical drive cannot work */
|
||||
FR_NO_FILE, /* (4) Could not find the file */
|
||||
FR_NO_PATH, /* (5) Could not find the path */
|
||||
FR_INVALID_NAME, /* (6) The path name format is invalid */
|
||||
FR_DENIED, /* (7) Access denied due to prohibited access or directory full */
|
||||
FR_EXIST, /* (8) Access denied due to prohibited access */
|
||||
FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */
|
||||
FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */
|
||||
FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */
|
||||
FR_NOT_ENABLED, /* (12) The volume has no work area */
|
||||
FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume */
|
||||
FR_MKFS_ABORTED, /* (14) The f_mkfs() aborted due to any problem */
|
||||
FR_TIMEOUT, /* (15) Could not get a grant to access the volume within defined period */
|
||||
FR_LOCKED, /* (16) The operation is rejected according to the file sharing policy */
|
||||
FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */
|
||||
FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > _FS_LOCK */
|
||||
FR_INVALID_PARAMETER /* (19) Given parameter is invalid */
|
||||
} FRESULT;
|
||||
|
||||
|
||||
|
||||
/*--------------------------------------------------------------*/
|
||||
/* FatFs module application interface */
|
||||
|
||||
FRESULT f_open (FIL* fp, const TCHAR* path, BYTE mode); /* Open or create a file */
|
||||
FRESULT f_close (FIL* fp); /* Close an open file object */
|
||||
FRESULT f_read (FIL* fp, void* buff, UINT btr, UINT* br); /* Read data from the file */
|
||||
FRESULT f_write (FIL* fp, const void* buff, UINT btw, UINT* bw); /* Write data to the file */
|
||||
FRESULT f_lseek (FIL* fp, FSIZE_t ofs); /* Move file pointer of the file object */
|
||||
FRESULT f_truncate (FIL* fp); /* Truncate the file */
|
||||
FRESULT f_sync (FIL* fp); /* Flush cached data of the writing file */
|
||||
FRESULT f_opendir (DIR* dp, const TCHAR* path); /* Open a directory */
|
||||
FRESULT f_closedir (DIR* dp); /* Close an open directory */
|
||||
FRESULT f_readdir (DIR* dp, FILINFO* fno); /* Read a directory item */
|
||||
FRESULT f_findfirst (DIR* dp, FILINFO* fno, const TCHAR* path, const TCHAR* pattern); /* Find first file */
|
||||
FRESULT f_findnext (DIR* dp, FILINFO* fno); /* Find next file */
|
||||
FRESULT f_mkdir (const TCHAR* path); /* Create a sub directory */
|
||||
FRESULT f_unlink (const TCHAR* path); /* Delete an existing file or directory */
|
||||
FRESULT f_rename (const TCHAR* path_old, const TCHAR* path_new); /* Rename/Move a file or directory */
|
||||
FRESULT f_stat (const TCHAR* path, FILINFO* fno); /* Get file status */
|
||||
FRESULT f_chmod (const TCHAR* path, BYTE attr, BYTE mask); /* Change attribute of a file/dir */
|
||||
FRESULT f_utime (const TCHAR* path, const FILINFO* fno); /* Change timestamp of a file/dir */
|
||||
FRESULT f_chdir (const TCHAR* path); /* Change current directory */
|
||||
FRESULT f_chdrive (const TCHAR* path); /* Change current drive */
|
||||
FRESULT f_getcwd (TCHAR* buff, UINT len); /* Get current directory */
|
||||
FRESULT f_getfree (const TCHAR* path, DWORD* nclst, FATFS** fatfs); /* Get number of free clusters on the drive */
|
||||
FRESULT f_getlabel (const TCHAR* path, TCHAR* label, DWORD* vsn); /* Get volume label */
|
||||
FRESULT f_setlabel (const TCHAR* label); /* Set volume label */
|
||||
FRESULT f_forward (FIL* fp, UINT(*func)(const BYTE*,UINT), UINT btf, UINT* bf); /* Forward data to the stream */
|
||||
FRESULT f_expand (FIL* fp, FSIZE_t szf, BYTE opt); /* Allocate a contiguous block to the file */
|
||||
FRESULT f_mount (FATFS* fs, const TCHAR* path, BYTE opt); /* Mount/Unmount a logical drive */
|
||||
FRESULT f_mkfs (const TCHAR* path, BYTE opt, DWORD au, void* work, UINT len); /* Create a FAT volume */
|
||||
FRESULT f_fdisk (BYTE pdrv, const DWORD* szt, void* work); /* Divide a physical drive into some partitions */
|
||||
int f_putc (TCHAR c, FIL* fp); /* Put a character to the file */
|
||||
int f_puts (const TCHAR* str, FIL* cp); /* Put a string to the file */
|
||||
int f_printf (FIL* fp, const TCHAR* str, ...); /* Put a formatted string to the file */
|
||||
TCHAR* f_gets (TCHAR* buff, int len, FIL* fp); /* Get a string from the file */
|
||||
|
||||
#define f_eof(fp) ((int)((fp)->fptr == (fp)->obj.objsize))
|
||||
#define f_error(fp) ((fp)->err)
|
||||
#define f_tell(fp) ((fp)->fptr)
|
||||
#define f_size(fp) ((fp)->obj.objsize)
|
||||
#define f_rewind(fp) f_lseek((fp), 0)
|
||||
#define f_rewinddir(dp) f_readdir((dp), 0)
|
||||
|
||||
#ifndef EOF
|
||||
#define EOF (-1)
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
/*--------------------------------------------------------------*/
|
||||
/* Additional user defined functions */
|
||||
|
||||
/* RTC function */
|
||||
#if !_FS_READONLY && !_FS_NORTC
|
||||
DWORD get_fattime (void);
|
||||
#endif
|
||||
|
||||
/* Unicode support functions */
|
||||
#if _USE_LFN != 0 /* Unicode - OEM code conversion */
|
||||
WCHAR ff_convert (WCHAR chr, UINT dir); /* OEM-Unicode bidirectional conversion */
|
||||
WCHAR ff_wtoupper (WCHAR chr); /* Unicode upper-case conversion */
|
||||
#if _USE_LFN == 3 /* Memory functions */
|
||||
void* ff_memalloc (UINT msize); /* Allocate memory block */
|
||||
void ff_memfree (void* mblock); /* Free memory block */
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* Sync functions */
|
||||
#if _FS_REENTRANT
|
||||
int ff_cre_syncobj (BYTE vol, _SYNC_t* sobj); /* Create a sync object */
|
||||
int ff_req_grant (_SYNC_t sobj); /* Lock sync object */
|
||||
void ff_rel_grant (_SYNC_t sobj); /* Unlock sync object */
|
||||
int ff_del_syncobj (_SYNC_t sobj); /* Delete a sync object */
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
/*--------------------------------------------------------------*/
|
||||
/* Flags and offset address */
|
||||
|
||||
|
||||
/* File access mode and open method flags (3rd argument of f_open) */
|
||||
#define FA_READ 0x01
|
||||
#define FA_WRITE 0x02
|
||||
#define FA_OPEN_EXISTING 0x00
|
||||
#define FA_CREATE_NEW 0x04
|
||||
#define FA_CREATE_ALWAYS 0x08
|
||||
#define FA_OPEN_ALWAYS 0x10
|
||||
#define FA_OPEN_APPEND 0x30
|
||||
|
||||
/* Fast seek controls (2nd argument of f_lseek) */
|
||||
#define CREATE_LINKMAP ((FSIZE_t)0 - 1)
|
||||
|
||||
/* Format options (2nd argument of f_mkfs) */
|
||||
#define FM_FAT 0x01
|
||||
#define FM_FAT32 0x02
|
||||
#define FM_EXFAT 0x04
|
||||
#define FM_ANY 0x07
|
||||
#define FM_SFD 0x08
|
||||
|
||||
/* Filesystem type (FATFS.fs_type) */
|
||||
#define FS_FAT12 1
|
||||
#define FS_FAT16 2
|
||||
#define FS_FAT32 3
|
||||
#define FS_EXFAT 4
|
||||
|
||||
/* File attribute bits for directory entry (FILINFO.fattrib) */
|
||||
#define AM_RDO 0x01 /* Read only */
|
||||
#define AM_HID 0x02 /* Hidden */
|
||||
#define AM_SYS 0x04 /* System */
|
||||
#define AM_DIR 0x10 /* Directory */
|
||||
#define AM_ARC 0x20 /* Archive */
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _FATFS */
|
|
@ -0,0 +1,267 @@
|
|||
/*---------------------------------------------------------------------------/
|
||||
/ FatFs - FAT file system module configuration file
|
||||
/---------------------------------------------------------------------------*/
|
||||
|
||||
#define _FFCONF 80186 /* Revision ID */
|
||||
|
||||
#include "user_config.h"
|
||||
|
||||
/*---------------------------------------------------------------------------/
|
||||
/ Function Configurations
|
||||
/---------------------------------------------------------------------------*/
|
||||
|
||||
#define _FS_READONLY 0
|
||||
/* This option switches read-only configuration. (0:Read/Write or 1:Read-only)
|
||||
/ Read-only configuration removes writing API functions, f_write(), f_sync(),
|
||||
/ f_unlink(), f_mkdir(), f_chmod(), f_rename(), f_truncate(), f_getfree()
|
||||
/ and optional writing functions as well. */
|
||||
|
||||
|
||||
#define _FS_MINIMIZE 0
|
||||
/* This option defines minimization level to remove some basic API functions.
|
||||
/
|
||||
/ 0: All basic functions are enabled.
|
||||
/ 1: f_stat(), f_getfree(), f_unlink(), f_mkdir(), f_truncate() and f_rename()
|
||||
/ are removed.
|
||||
/ 2: f_opendir(), f_readdir() and f_closedir() are removed in addition to 1.
|
||||
/ 3: f_lseek() function is removed in addition to 2. */
|
||||
|
||||
|
||||
#define _USE_STRFUNC 0
|
||||
/* This option switches string functions, f_gets(), f_putc(), f_puts() and
|
||||
/ f_printf().
|
||||
/
|
||||
/ 0: Disable string functions.
|
||||
/ 1: Enable without LF-CRLF conversion.
|
||||
/ 2: Enable with LF-CRLF conversion. */
|
||||
|
||||
|
||||
#define _USE_FIND 0
|
||||
/* This option switches filtered directory read functions, f_findfirst() and
|
||||
/ f_findnext(). (0:Disable, 1:Enable 2:Enable with matching altname[] too) */
|
||||
|
||||
|
||||
#define _USE_MKFS 0
|
||||
/* This option switches f_mkfs() function. (0:Disable or 1:Enable) */
|
||||
|
||||
|
||||
#define _USE_FASTSEEK 0
|
||||
/* This option switches fast seek function. (0:Disable or 1:Enable) */
|
||||
|
||||
|
||||
#define _USE_EXPAND 0
|
||||
/* This option switches f_expand function. (0:Disable or 1:Enable) */
|
||||
|
||||
|
||||
#define _USE_CHMOD 1
|
||||
/* This option switches attribute manipulation functions, f_chmod() and f_utime().
|
||||
/ (0:Disable or 1:Enable) Also _FS_READONLY needs to be 0 to enable this option. */
|
||||
|
||||
|
||||
#define _USE_LABEL 1
|
||||
/* This option switches volume label functions, f_getlabel() and f_setlabel().
|
||||
/ (0:Disable or 1:Enable) */
|
||||
|
||||
|
||||
#define _USE_FORWARD 0
|
||||
/* This option switches f_forward() function. (0:Disable or 1:Enable) */
|
||||
|
||||
|
||||
/*---------------------------------------------------------------------------/
|
||||
/ Locale and Namespace Configurations
|
||||
/---------------------------------------------------------------------------*/
|
||||
|
||||
#define _CODE_PAGE 932
|
||||
/* This option specifies the OEM code page to be used on the target system.
|
||||
/ Incorrect setting of the code page can cause a file open failure.
|
||||
/
|
||||
/ 1 - ASCII (No extended character. Non-LFN cfg. only)
|
||||
/ 437 - U.S.
|
||||
/ 720 - Arabic
|
||||
/ 737 - Greek
|
||||
/ 771 - KBL
|
||||
/ 775 - Baltic
|
||||
/ 850 - Latin 1
|
||||
/ 852 - Latin 2
|
||||
/ 855 - Cyrillic
|
||||
/ 857 - Turkish
|
||||
/ 860 - Portuguese
|
||||
/ 861 - Icelandic
|
||||
/ 862 - Hebrew
|
||||
/ 863 - Canadian French
|
||||
/ 864 - Arabic
|
||||
/ 865 - Nordic
|
||||
/ 866 - Russian
|
||||
/ 869 - Greek 2
|
||||
/ 932 - Japanese (DBCS)
|
||||
/ 936 - Simplified Chinese (DBCS)
|
||||
/ 949 - Korean (DBCS)
|
||||
/ 950 - Traditional Chinese (DBCS)
|
||||
*/
|
||||
|
||||
|
||||
#define _USE_LFN 3
|
||||
#define _MAX_LFN (FS_OBJ_NAME_LEN+1+1)
|
||||
/* The _USE_LFN switches the support of long file name (LFN).
|
||||
/
|
||||
/ 0: Disable support of LFN. _MAX_LFN has no effect.
|
||||
/ 1: Enable LFN with static working buffer on the BSS. Always NOT thread-safe.
|
||||
/ 2: Enable LFN with dynamic working buffer on the STACK.
|
||||
/ 3: Enable LFN with dynamic working buffer on the HEAP.
|
||||
/
|
||||
/ To enable the LFN, Unicode handling functions (option/unicode.c) must be added
|
||||
/ to the project. The working buffer occupies (_MAX_LFN + 1) * 2 bytes and
|
||||
/ additional 608 bytes at exFAT enabled. _MAX_LFN can be in range from 12 to 255.
|
||||
/ It should be set 255 to support full featured LFN operations.
|
||||
/ When use stack for the working buffer, take care on stack overflow. When use heap
|
||||
/ memory for the working buffer, memory management functions, ff_memalloc() and
|
||||
/ ff_memfree(), must be added to the project. */
|
||||
|
||||
|
||||
#define _LFN_UNICODE 0
|
||||
/* This option switches character encoding on the API. (0:ANSI/OEM or 1:UTF-16)
|
||||
/ To use Unicode string for the path name, enable LFN and set _LFN_UNICODE = 1.
|
||||
/ This option also affects behavior of string I/O functions. */
|
||||
|
||||
|
||||
#define _STRF_ENCODE 3
|
||||
/* When _LFN_UNICODE == 1, this option selects the character encoding ON THE FILE to
|
||||
/ be read/written via string I/O functions, f_gets(), f_putc(), f_puts and f_printf().
|
||||
/
|
||||
/ 0: ANSI/OEM
|
||||
/ 1: UTF-16LE
|
||||
/ 2: UTF-16BE
|
||||
/ 3: UTF-8
|
||||
/
|
||||
/ This option has no effect when _LFN_UNICODE == 0. */
|
||||
|
||||
|
||||
#define _FS_RPATH 2
|
||||
/* This option configures support of relative path.
|
||||
/
|
||||
/ 0: Disable relative path and remove related functions.
|
||||
/ 1: Enable relative path. f_chdir() and f_chdrive() are available.
|
||||
/ 2: f_getcwd() function is available in addition to 1.
|
||||
*/
|
||||
|
||||
|
||||
/*---------------------------------------------------------------------------/
|
||||
/ Drive/Volume Configurations
|
||||
/---------------------------------------------------------------------------*/
|
||||
|
||||
#define _VOLUMES 4
|
||||
/* Number of volumes (logical drives) to be used. */
|
||||
|
||||
|
||||
#define _STR_VOLUME_ID 1
|
||||
#define _VOLUME_STRS "SD0","SD1","SD2","SD3"
|
||||
/* _STR_VOLUME_ID switches string support of volume ID.
|
||||
/ When _STR_VOLUME_ID is set to 1, also pre-defined strings can be used as drive
|
||||
/ number in the path name. _VOLUME_STRS defines the drive ID strings for each
|
||||
/ logical drives. Number of items must be equal to _VOLUMES. Valid characters for
|
||||
/ the drive ID strings are: A-Z and 0-9. */
|
||||
|
||||
|
||||
#define _MULTI_PARTITION 1
|
||||
/* This option switches support of multi-partition on a physical drive.
|
||||
/ By default (0), each logical drive number is bound to the same physical drive
|
||||
/ number and only an FAT volume found on the physical drive will be mounted.
|
||||
/ When multi-partition is enabled (1), each logical drive number can be bound to
|
||||
/ arbitrary physical drive and partition listed in the VolToPart[]. Also f_fdisk()
|
||||
/ funciton will be available. */
|
||||
|
||||
|
||||
#define _MIN_SS 512
|
||||
#define _MAX_SS 512
|
||||
/* These options configure the range of sector size to be supported. (512, 1024,
|
||||
/ 2048 or 4096) Always set both 512 for most systems, all type of memory cards and
|
||||
/ harddisk. But a larger value may be required for on-board flash memory and some
|
||||
/ type of optical media. When _MAX_SS is larger than _MIN_SS, FatFs is configured
|
||||
/ to variable sector size and GET_SECTOR_SIZE command must be implemented to the
|
||||
/ disk_ioctl() function. */
|
||||
|
||||
|
||||
#define _USE_TRIM 0
|
||||
/* This option switches support of ATA-TRIM. (0:Disable or 1:Enable)
|
||||
/ To enable Trim function, also CTRL_TRIM command should be implemented to the
|
||||
/ disk_ioctl() function. */
|
||||
|
||||
|
||||
#define _FS_NOFSINFO 0
|
||||
/* If you need to know correct free space on the FAT32 volume, set bit 0 of this
|
||||
/ option, and f_getfree() function at first time after volume mount will force
|
||||
/ a full FAT scan. Bit 1 controls the use of last allocated cluster number.
|
||||
/
|
||||
/ bit0=0: Use free cluster count in the FSINFO if available.
|
||||
/ bit0=1: Do not trust free cluster count in the FSINFO.
|
||||
/ bit1=0: Use last allocated cluster number in the FSINFO if available.
|
||||
/ bit1=1: Do not trust last allocated cluster number in the FSINFO.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
/*---------------------------------------------------------------------------/
|
||||
/ System Configurations
|
||||
/---------------------------------------------------------------------------*/
|
||||
|
||||
#define _FS_TINY 0
|
||||
/* This option switches tiny buffer configuration. (0:Normal or 1:Tiny)
|
||||
/ At the tiny configuration, size of the file object (FIL) is reduced _MAX_SS bytes.
|
||||
/ Instead of private sector buffer eliminated from the file object, common sector
|
||||
/ buffer in the file system object (FATFS) is used for the file data transfer. */
|
||||
|
||||
|
||||
#define _FS_EXFAT 0
|
||||
/* This option switches support of exFAT file system in addition to the traditional
|
||||
/ FAT file system. (0:Disable or 1:Enable) To enable exFAT, also LFN must be enabled.
|
||||
/ Note that enabling exFAT discards C89 compatibility. */
|
||||
|
||||
|
||||
#define _FS_NORTC 0
|
||||
#define _NORTC_MON 6
|
||||
#define _NORTC_MDAY 21
|
||||
#define _NORTC_YEAR 2016
|
||||
/* The option _FS_NORTC switches timestamp functiton. If the system does not have
|
||||
/ any RTC function or valid timestamp is not needed, set _FS_NORTC = 1 to disable
|
||||
/ the timestamp function. All objects modified by FatFs will have a fixed timestamp
|
||||
/ defined by _NORTC_MON, _NORTC_MDAY and _NORTC_YEAR in local time.
|
||||
/ To enable timestamp function (_FS_NORTC = 0), get_fattime() function need to be
|
||||
/ added to the project to get current time form real-time clock. _NORTC_MON,
|
||||
/ _NORTC_MDAY and _NORTC_YEAR have no effect.
|
||||
/ These options have no effect at read-only configuration (_FS_READONLY = 1). */
|
||||
|
||||
|
||||
#define _FS_LOCK 0
|
||||
/* The option _FS_LOCK switches file lock function to control duplicated file open
|
||||
/ and illegal operation to open objects. This option must be 0 when _FS_READONLY
|
||||
/ is 1.
|
||||
/
|
||||
/ 0: Disable file lock function. To avoid volume corruption, application program
|
||||
/ should avoid illegal open, remove and rename to the open objects.
|
||||
/ >0: Enable file lock function. The value defines how many files/sub-directories
|
||||
/ can be opened simultaneously under file lock control. Note that the file
|
||||
/ lock control is independent of re-entrancy. */
|
||||
|
||||
|
||||
#define _FS_REENTRANT 0
|
||||
#define _FS_TIMEOUT 1000
|
||||
#define _SYNC_t HANDLE
|
||||
/* The option _FS_REENTRANT switches the re-entrancy (thread safe) of the FatFs
|
||||
/ module itself. Note that regardless of this option, file access to different
|
||||
/ volume is always re-entrant and volume control functions, f_mount(), f_mkfs()
|
||||
/ and f_fdisk() function, are always not re-entrant. Only file/directory access
|
||||
/ to the same volume is under control of this function.
|
||||
/
|
||||
/ 0: Disable re-entrancy. _FS_TIMEOUT and _SYNC_t have no effect.
|
||||
/ 1: Enable re-entrancy. Also user provided synchronization handlers,
|
||||
/ ff_req_grant(), ff_rel_grant(), ff_del_syncobj() and ff_cre_syncobj()
|
||||
/ function, must be added to the project. Samples are available in
|
||||
/ option/syscall.c.
|
||||
/
|
||||
/ The _FS_TIMEOUT defines timeout period in unit of time tick.
|
||||
/ The _SYNC_t defines O/S dependent sync object type. e.g. HANDLE, ID, OS_EVENT*,
|
||||
/ SemaphoreHandle_t and etc.. A header file for O/S definitions needs to be
|
||||
/ included somewhere in the scope of ff.c. */
|
||||
|
||||
|
||||
/*--- End of configuration options ---*/
|
|
@ -0,0 +1,38 @@
|
|||
/*-------------------------------------------*/
|
||||
/* Integer type definitions for FatFs module */
|
||||
/*-------------------------------------------*/
|
||||
|
||||
#ifndef _FF_INTEGER
|
||||
#define _FF_INTEGER
|
||||
|
||||
#ifdef _WIN32 /* FatFs development platform */
|
||||
|
||||
#include <windows.h>
|
||||
#include <tchar.h>
|
||||
typedef unsigned __int64 QWORD;
|
||||
|
||||
|
||||
#else /* Embedded platform */
|
||||
|
||||
/* These types MUST be 16-bit or 32-bit */
|
||||
typedef int INT;
|
||||
typedef unsigned int UINT;
|
||||
|
||||
/* This type MUST be 8-bit */
|
||||
typedef unsigned char BYTE;
|
||||
|
||||
/* These types MUST be 16-bit */
|
||||
typedef short SHORT;
|
||||
typedef unsigned short WORD;
|
||||
typedef unsigned short WCHAR;
|
||||
|
||||
/* These types MUST be 32-bit */
|
||||
typedef long LONG;
|
||||
typedef unsigned long DWORD;
|
||||
|
||||
/* This type MUST be 64-bit (Remove this for C89 compatibility) */
|
||||
typedef unsigned long long QWORD;
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -0,0 +1,632 @@
|
|||
|
||||
|
||||
#include <c_stdlib.h>
|
||||
#include <c_string.h>
|
||||
|
||||
#include "vfs_int.h"
|
||||
|
||||
#include "fatfs_prefix_lib.h"
|
||||
#include "ff.h"
|
||||
#include "fatfs_config.h"
|
||||
|
||||
|
||||
static FRESULT last_result = FR_OK;
|
||||
|
||||
static const char* const volstr[_VOLUMES] = {_VOLUME_STRS};
|
||||
|
||||
static int is_current_drive = FALSE;
|
||||
|
||||
|
||||
// forward declarations
|
||||
static sint32_t myfatfs_close( const struct vfs_file *fd );
|
||||
static sint32_t myfatfs_read( const struct vfs_file *fd, void *ptr, size_t len );
|
||||
static sint32_t myfatfs_write( const struct vfs_file *fd, const void *ptr, size_t len );
|
||||
static sint32_t myfatfs_lseek( const struct vfs_file *fd, sint32_t off, int whence );
|
||||
static sint32_t myfatfs_eof( const struct vfs_file *fd );
|
||||
static sint32_t myfatfs_tell( const struct vfs_file *fd );
|
||||
static sint32_t myfatfs_flush( const struct vfs_file *fd );
|
||||
static uint32_t myfatfs_fsize( const struct vfs_file *fd );
|
||||
static sint32_t myfatfs_ferrno( const struct vfs_file *fd );
|
||||
|
||||
static sint32_t myfatfs_closedir( const struct vfs_dir *dd );
|
||||
static vfs_item *myfatfs_readdir( const struct vfs_dir *dd );
|
||||
|
||||
static void myfatfs_iclose( const struct vfs_item *di );
|
||||
static uint32_t myfatfs_isize( const struct vfs_item *di );
|
||||
static sint32_t myfatfs_time( const struct vfs_item *di, struct vfs_time *tm );
|
||||
static const char *myfatfs_name( const struct vfs_item *di );
|
||||
static sint32_t myfatfs_is_dir( const struct vfs_item *di );
|
||||
static sint32_t myfatfs_is_rdonly( const struct vfs_item *di );
|
||||
static sint32_t myfatfs_is_hidden( const struct vfs_item *di );
|
||||
static sint32_t myfatfs_is_sys( const struct vfs_item *di );
|
||||
static sint32_t myfatfs_is_arch( const struct vfs_item *di );
|
||||
|
||||
static vfs_vol *myfatfs_mount( const char *name, int num );
|
||||
static vfs_file *myfatfs_open( const char *name, const char *mode );
|
||||
static vfs_dir *myfatfs_opendir( const char *name );
|
||||
static vfs_item *myfatfs_stat( const char *name );
|
||||
static sint32_t myfatfs_remove( const char *name );
|
||||
static sint32_t myfatfs_rename( const char *oldname, const char *newname );
|
||||
static sint32_t myfatfs_mkdir( const char *name );
|
||||
static sint32_t myfatfs_fsinfo( uint32_t *total, uint32_t *used );
|
||||
static sint32_t myfatfs_chdrive( const char *name );
|
||||
static sint32_t myfatfs_chdir( const char *name );
|
||||
static sint32_t myfatfs_errno( void );
|
||||
static void myfatfs_clearerr( void );
|
||||
|
||||
static sint32_t myfatfs_umount( const struct vfs_vol *vol );
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// function tables
|
||||
//
|
||||
static vfs_fs_fns myfatfs_fs_fns = {
|
||||
.mount = myfatfs_mount,
|
||||
.open = myfatfs_open,
|
||||
.opendir = myfatfs_opendir,
|
||||
.stat = myfatfs_stat,
|
||||
.remove = myfatfs_remove,
|
||||
.rename = myfatfs_rename,
|
||||
.mkdir = myfatfs_mkdir,
|
||||
.fsinfo = myfatfs_fsinfo,
|
||||
.fscfg = NULL,
|
||||
.format = NULL,
|
||||
.chdrive = myfatfs_chdrive,
|
||||
.chdir = myfatfs_chdir,
|
||||
.ferrno = myfatfs_errno,
|
||||
.clearerr = myfatfs_clearerr
|
||||
};
|
||||
|
||||
static vfs_file_fns myfatfs_file_fns = {
|
||||
.close = myfatfs_close,
|
||||
.read = myfatfs_read,
|
||||
.write = myfatfs_write,
|
||||
.lseek = myfatfs_lseek,
|
||||
.eof = myfatfs_eof,
|
||||
.tell = myfatfs_tell,
|
||||
.flush = myfatfs_flush,
|
||||
.size = myfatfs_fsize,
|
||||
.ferrno = myfatfs_ferrno
|
||||
};
|
||||
|
||||
static vfs_item_fns myfatfs_item_fns = {
|
||||
.close = myfatfs_iclose,
|
||||
.size = myfatfs_isize,
|
||||
.time = myfatfs_time,
|
||||
.name = myfatfs_name,
|
||||
.is_dir = myfatfs_is_dir,
|
||||
.is_rdonly = myfatfs_is_rdonly,
|
||||
.is_hidden = myfatfs_is_hidden,
|
||||
.is_sys = myfatfs_is_sys,
|
||||
.is_arch = myfatfs_is_arch
|
||||
};
|
||||
|
||||
static vfs_dir_fns myfatfs_dir_fns = {
|
||||
.close = myfatfs_closedir,
|
||||
.readdir = myfatfs_readdir
|
||||
};
|
||||
|
||||
static vfs_vol_fns myfatfs_vol_fns = {
|
||||
.umount = myfatfs_umount
|
||||
};
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// specific struct extensions
|
||||
//
|
||||
struct myvfs_vol {
|
||||
struct vfs_vol vfs_vol;
|
||||
char *ldrname;
|
||||
FATFS fs;
|
||||
};
|
||||
|
||||
struct myvfs_file {
|
||||
struct vfs_file vfs_file;
|
||||
FIL fp;
|
||||
};
|
||||
|
||||
struct myvfs_dir {
|
||||
struct vfs_dir vfs_dir;
|
||||
DIR dp;
|
||||
};
|
||||
|
||||
struct myvfs_item {
|
||||
struct vfs_item vfs_item;
|
||||
FILINFO fno;
|
||||
};
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// exported helper functions for FatFS
|
||||
//
|
||||
inline void *ff_memalloc( UINT size )
|
||||
{
|
||||
return c_malloc( size );
|
||||
}
|
||||
|
||||
inline void ff_memfree( void *mblock )
|
||||
{
|
||||
c_free( mblock );
|
||||
}
|
||||
|
||||
// TODO
|
||||
DWORD get_fattime( void )
|
||||
{
|
||||
DWORD stamp;
|
||||
vfs_time tm;
|
||||
|
||||
if (VFS_RES_OK == vfs_get_rtc( &tm )) {
|
||||
// sanity checks
|
||||
tm.year = (tm.year >= 1980) && (tm.year < 2108) ? tm.year : 1980;
|
||||
tm.mon = (tm.mon >= 1) && (tm.mon <= 12) ? tm.mon : 1;
|
||||
tm.day = (tm.day >= 1) && (tm.day <= 31) ? tm.day : 1;
|
||||
tm.hour = (tm.hour >= 0) && (tm.hour <= 23) ? tm.hour : 0;
|
||||
tm.min = (tm.min >= 0) && (tm.min <= 59) ? tm.min : 0;
|
||||
tm.sec = (tm.sec >= 0) && (tm.sec <= 59) ? tm.sec : 0;
|
||||
|
||||
stamp = (tm.year-1980) << 25 | tm.mon << 21 | tm.day << 16 |
|
||||
tm.hour << 11 | tm.min << 5 | tm.sec;
|
||||
} else {
|
||||
// default time stamp derived from ffconf.h
|
||||
stamp = ((DWORD)(_NORTC_YEAR - 1980) << 25 | (DWORD)_NORTC_MON << 21 | (DWORD)_NORTC_MDAY << 16);
|
||||
}
|
||||
|
||||
return stamp;
|
||||
}
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// volume functions
|
||||
//
|
||||
#define GET_FATFS_FS(descr) \
|
||||
const struct myvfs_vol *myvol = (const struct myvfs_vol *)descr; \
|
||||
FATFS *fs = (FATFS *)&(myvol->fs);
|
||||
|
||||
static sint32_t myfatfs_umount( const struct vfs_vol *vol )
|
||||
{
|
||||
GET_FATFS_FS(vol);
|
||||
|
||||
last_result = f_mount( NULL, myvol->ldrname, 0 );
|
||||
|
||||
c_free( myvol->ldrname );
|
||||
c_free( (void *)vol );
|
||||
|
||||
return last_result == FR_OK ? VFS_RES_OK : VFS_RES_ERR;
|
||||
}
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// file functions
|
||||
//
|
||||
#define GET_FIL_FP(descr) \
|
||||
const struct myvfs_file *myfd = (const struct myvfs_file *)descr; \
|
||||
FIL *fp = (FIL *)&(myfd->fp);
|
||||
|
||||
static sint32_t myfatfs_close( const struct vfs_file *fd )
|
||||
{
|
||||
GET_FIL_FP(fd)
|
||||
|
||||
last_result = f_close( fp );
|
||||
|
||||
// free descriptor memory
|
||||
c_free( (void *)fd );
|
||||
|
||||
return last_result == FR_OK ? VFS_RES_OK : VFS_RES_ERR;
|
||||
}
|
||||
|
||||
static sint32_t myfatfs_read( const struct vfs_file *fd, void *ptr, size_t len )
|
||||
{
|
||||
GET_FIL_FP(fd);
|
||||
UINT act_read;
|
||||
|
||||
last_result = f_read( fp, ptr, len, &act_read );
|
||||
|
||||
return last_result == FR_OK ? act_read : VFS_RES_ERR;
|
||||
}
|
||||
|
||||
static sint32_t myfatfs_write( const struct vfs_file *fd, const void *ptr, size_t len )
|
||||
{
|
||||
GET_FIL_FP(fd);
|
||||
UINT act_written;
|
||||
|
||||
last_result = f_write( fp, ptr, len, &act_written );
|
||||
|
||||
return last_result == FR_OK ? act_written : VFS_RES_ERR;
|
||||
}
|
||||
|
||||
static sint32_t myfatfs_lseek( const struct vfs_file *fd, sint32_t off, int whence )
|
||||
{
|
||||
GET_FIL_FP(fd);
|
||||
FSIZE_t new_pos;
|
||||
|
||||
switch (whence) {
|
||||
default:
|
||||
case VFS_SEEK_SET:
|
||||
new_pos = off > 0 ? off : 0;
|
||||
break;
|
||||
case VFS_SEEK_CUR:
|
||||
new_pos = f_tell( fp );
|
||||
new_pos += off;
|
||||
break;
|
||||
case VFS_SEEK_END:
|
||||
new_pos = f_size( fp );
|
||||
new_pos += off < 0 ? off : 0;
|
||||
break;
|
||||
};
|
||||
|
||||
last_result = f_lseek( fp, new_pos );
|
||||
new_pos = f_tell( fp );
|
||||
|
||||
return last_result == FR_OK ? new_pos : VFS_RES_ERR;
|
||||
}
|
||||
|
||||
static sint32_t myfatfs_eof( const struct vfs_file *fd )
|
||||
{
|
||||
GET_FIL_FP(fd);
|
||||
|
||||
last_result = FR_OK;
|
||||
|
||||
return f_eof( fp );
|
||||
}
|
||||
|
||||
static sint32_t myfatfs_tell( const struct vfs_file *fd )
|
||||
{
|
||||
GET_FIL_FP(fd);
|
||||
|
||||
last_result = FR_OK;
|
||||
|
||||
return f_tell( fp );
|
||||
}
|
||||
|
||||
static sint32_t myfatfs_flush( const struct vfs_file *fd )
|
||||
{
|
||||
GET_FIL_FP(fd);
|
||||
|
||||
last_result = f_sync( fp );
|
||||
|
||||
return last_result == FR_OK ? VFS_RES_OK : VFS_RES_ERR;
|
||||
}
|
||||
|
||||
static uint32_t myfatfs_fsize( const struct vfs_file *fd )
|
||||
{
|
||||
GET_FIL_FP(fd);
|
||||
|
||||
last_result = FR_OK;
|
||||
|
||||
return f_size( fp );
|
||||
}
|
||||
|
||||
static sint32_t myfatfs_ferrno( const struct vfs_file *fd )
|
||||
{
|
||||
return -last_result;
|
||||
}
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// dir functions
|
||||
//
|
||||
#define GET_DIR_DP(descr) \
|
||||
const struct myvfs_dir *mydd = (const struct myvfs_dir *)descr; \
|
||||
DIR *dp = (DIR *)&(mydd->dp);
|
||||
|
||||
static sint32_t myfatfs_closedir( const struct vfs_dir *dd )
|
||||
{
|
||||
GET_DIR_DP(dd);
|
||||
|
||||
last_result = f_closedir( dp );
|
||||
|
||||
// free descriptor memory
|
||||
c_free( (void *)dd );
|
||||
|
||||
return last_result == FR_OK ? VFS_RES_OK : VFS_RES_ERR;
|
||||
}
|
||||
|
||||
static vfs_item *myfatfs_readdir( const struct vfs_dir *dd )
|
||||
{
|
||||
GET_DIR_DP(dd);
|
||||
struct myvfs_item *di;
|
||||
|
||||
if (di = c_malloc( sizeof( struct myvfs_item ) )) {
|
||||
FILINFO *fno = &(di->fno);
|
||||
|
||||
if (FR_OK == (last_result = f_readdir( dp, fno ))) {
|
||||
// condition "no further item" is signalled with empty name
|
||||
if (fno->fname[0] != '\0') {
|
||||
di->vfs_item.fs_type = VFS_FS_FATFS;
|
||||
di->vfs_item.fns = &myfatfs_item_fns;
|
||||
return (vfs_item *)di;
|
||||
}
|
||||
}
|
||||
c_free( di );
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// dir info functions
|
||||
//
|
||||
#define GET_FILINFO_FNO(descr) \
|
||||
const struct myvfs_item *mydi = (const struct myvfs_item *)descr; \
|
||||
FILINFO *fno = (FILINFO *)&(mydi->fno);
|
||||
|
||||
static void myfatfs_iclose( const struct vfs_item *di )
|
||||
{
|
||||
GET_FILINFO_FNO(di);
|
||||
|
||||
// free descriptor memory
|
||||
c_free( (void *)di );
|
||||
}
|
||||
|
||||
static uint32_t myfatfs_isize( const struct vfs_item *di )
|
||||
{
|
||||
GET_FILINFO_FNO(di);
|
||||
|
||||
return fno->fsize;
|
||||
}
|
||||
|
||||
static sint32_t myfatfs_time( const struct vfs_item *di, struct vfs_time *tm )
|
||||
{
|
||||
GET_FILINFO_FNO(di);
|
||||
|
||||
tm->year = (fno->fdate >> 9) + 1980;
|
||||
tm->mon = (fno->fdate >> 5) & 0x0f;
|
||||
tm->day = fno->fdate & 0x1f;
|
||||
tm->hour = (fno->ftime >> 11);
|
||||
tm->min = (fno->ftime >> 5) & 0x3f;
|
||||
tm->sec = fno->ftime & 0x3f;
|
||||
|
||||
return VFS_RES_OK;
|
||||
}
|
||||
|
||||
static const char *myfatfs_name( const struct vfs_item *di )
|
||||
{
|
||||
GET_FILINFO_FNO(di);
|
||||
|
||||
return fno->fname;
|
||||
}
|
||||
|
||||
static sint32_t myfatfs_is_dir( const struct vfs_item *di )
|
||||
{
|
||||
GET_FILINFO_FNO(di);
|
||||
|
||||
return fno->fattrib & AM_DIR ? 1 : 0;
|
||||
}
|
||||
|
||||
static sint32_t myfatfs_is_rdonly( const struct vfs_item *di )
|
||||
{
|
||||
GET_FILINFO_FNO(di);
|
||||
|
||||
return fno->fattrib & AM_RDO ? 1 : 0;
|
||||
}
|
||||
|
||||
static sint32_t myfatfs_is_hidden( const struct vfs_item *di )
|
||||
{
|
||||
GET_FILINFO_FNO(di);
|
||||
|
||||
return fno->fattrib & AM_HID ? 1 : 0;
|
||||
}
|
||||
|
||||
static sint32_t myfatfs_is_sys( const struct vfs_item *di )
|
||||
{
|
||||
GET_FILINFO_FNO(di);
|
||||
|
||||
return fno->fattrib & AM_SYS ? 1 : 0;
|
||||
}
|
||||
|
||||
static sint32_t myfatfs_is_arch( const struct vfs_item *di )
|
||||
{
|
||||
GET_FILINFO_FNO(di);
|
||||
|
||||
return fno->fattrib & AM_ARC ? 1 : 0;
|
||||
}
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// filesystem functions
|
||||
//
|
||||
static vfs_vol *myfatfs_mount( const char *name, int num )
|
||||
{
|
||||
struct myvfs_vol *vol;
|
||||
const size_t len = c_strlen( name );
|
||||
|
||||
// num argument specifies the physical driver = SS/CS pin number for this sd card
|
||||
if (num >= 0) {
|
||||
for (int i = 0; i < NUM_LOGICAL_DRIVES; i++) {
|
||||
if (0 == c_strncmp( name, volstr[i], c_strlen( volstr[i] ) )) {
|
||||
VolToPart[i].pd = num;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (vol = c_malloc( sizeof( struct myvfs_vol ) )) {
|
||||
if (vol->ldrname = c_strdup( name )) {
|
||||
if (FR_OK == (last_result = f_mount( &(vol->fs), name, 1 ))) {
|
||||
vol->vfs_vol.fs_type = VFS_FS_FATFS;
|
||||
vol->vfs_vol.fns = &myfatfs_vol_fns;
|
||||
return (vfs_vol *)vol;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (vol) {
|
||||
if (vol->ldrname) c_free( vol->ldrname );
|
||||
c_free( vol );
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static BYTE myfatfs_mode2flag( const char *mode )
|
||||
{
|
||||
if (c_strlen( mode ) == 1) {
|
||||
if(c_strcmp( mode, "w" ) == 0)
|
||||
return FA_WRITE | FA_CREATE_ALWAYS;
|
||||
else if (c_strcmp( mode, "r" ) == 0)
|
||||
return FA_READ | FA_OPEN_EXISTING;
|
||||
else if (c_strcmp( mode, "a" ) == 0)
|
||||
return FA_WRITE | FA_OPEN_ALWAYS;
|
||||
else
|
||||
return FA_READ | FA_OPEN_EXISTING;
|
||||
} else if (c_strlen( mode ) == 2) {
|
||||
if (c_strcmp( mode, "r+" ) == 0)
|
||||
return FA_READ | FA_WRITE | FA_OPEN_EXISTING;
|
||||
else if (c_strcmp( mode, "w+" ) == 0)
|
||||
return FA_READ | FA_WRITE | FA_CREATE_ALWAYS;
|
||||
else if (c_strcmp( mode, "a+" ) ==0 )
|
||||
return FA_READ | FA_WRITE | FA_OPEN_ALWAYS;
|
||||
else
|
||||
return FA_READ | FA_OPEN_EXISTING;
|
||||
} else {
|
||||
return FA_READ | FA_OPEN_EXISTING;
|
||||
}
|
||||
}
|
||||
|
||||
static vfs_file *myfatfs_open( const char *name, const char *mode )
|
||||
{
|
||||
struct myvfs_file *fd;
|
||||
const BYTE flags = myfatfs_mode2flag( mode );
|
||||
|
||||
if (fd = c_malloc( sizeof( struct myvfs_file ) )) {
|
||||
if (FR_OK == (last_result = f_open( &(fd->fp), name, flags ))) {
|
||||
// skip to end of file for append mode
|
||||
if (flags & FA_OPEN_ALWAYS)
|
||||
f_lseek( &(fd->fp), f_size( &(fd->fp) ) );
|
||||
|
||||
fd->vfs_file.fs_type = VFS_FS_FATFS;
|
||||
fd->vfs_file.fns = &myfatfs_file_fns;
|
||||
return (vfs_file *)fd;
|
||||
} else {
|
||||
c_free( fd );
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static vfs_dir *myfatfs_opendir( const char *name )
|
||||
{
|
||||
struct myvfs_dir *dd;
|
||||
|
||||
if (dd = c_malloc( sizeof( struct myvfs_dir ) )) {
|
||||
if (FR_OK == (last_result = f_opendir( &(dd->dp), name ))) {
|
||||
dd->vfs_dir.fs_type = VFS_FS_FATFS;
|
||||
dd->vfs_dir.fns = &myfatfs_dir_fns;
|
||||
return (vfs_dir *)dd;
|
||||
} else {
|
||||
c_free( dd );
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static vfs_item *myfatfs_stat( const char *name )
|
||||
{
|
||||
struct myvfs_item *di;
|
||||
|
||||
if (di = c_malloc( sizeof( struct myvfs_item ) )) {
|
||||
if (FR_OK == (last_result = f_stat( name, &(di->fno) ))) {
|
||||
di->vfs_item.fs_type = VFS_FS_FATFS;
|
||||
di->vfs_item.fns = &myfatfs_item_fns;
|
||||
return (vfs_item *)di;
|
||||
} else {
|
||||
c_free( di );
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static sint32_t myfatfs_remove( const char *name )
|
||||
{
|
||||
last_result = f_unlink( name );
|
||||
|
||||
return last_result == FR_OK ? VFS_RES_OK : VFS_RES_ERR;
|
||||
}
|
||||
|
||||
static sint32_t myfatfs_rename( const char *oldname, const char *newname )
|
||||
{
|
||||
last_result = f_rename( oldname, newname );
|
||||
|
||||
return last_result == FR_OK ? VFS_RES_OK : VFS_RES_ERR;
|
||||
}
|
||||
|
||||
static sint32_t myfatfs_mkdir( const char *name )
|
||||
{
|
||||
last_result = f_mkdir( name );
|
||||
|
||||
return last_result == FR_OK ? VFS_RES_OK : VFS_RES_ERR;
|
||||
}
|
||||
|
||||
static sint32_t myfatfs_fsinfo( uint32_t *total, uint32_t *used )
|
||||
{
|
||||
DWORD free_clusters;
|
||||
FATFS *fatfs;
|
||||
|
||||
if ((last_result = f_getfree( "", &free_clusters, &fatfs )) == FR_OK) {
|
||||
// provide information in kByte since uint32_t would clip to 4 GByte
|
||||
*total = (fatfs->n_fatent * fatfs->csize) / (1024 / _MAX_SS);
|
||||
*used = *total - (free_clusters * fatfs->csize) / (1024 / _MAX_SS);
|
||||
}
|
||||
|
||||
return last_result == FR_OK ? VFS_RES_OK : VFS_RES_ERR;
|
||||
}
|
||||
|
||||
static sint32_t myfatfs_chdrive( const char *name )
|
||||
{
|
||||
last_result = f_chdrive( name );
|
||||
|
||||
return last_result == FR_OK ? VFS_RES_OK : VFS_RES_ERR;
|
||||
}
|
||||
|
||||
static sint32_t myfatfs_chdir( const char *name )
|
||||
{
|
||||
last_result = f_chdir( name );
|
||||
|
||||
return last_result == FR_OK ? VFS_RES_OK : VFS_RES_ERR;
|
||||
}
|
||||
|
||||
static sint32_t myfatfs_errno( void )
|
||||
{
|
||||
return -last_result;
|
||||
}
|
||||
|
||||
static void myfatfs_clearerr( void )
|
||||
{
|
||||
last_result = FR_OK;
|
||||
}
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// VFS interface functions
|
||||
//
|
||||
vfs_fs_fns *myfatfs_realm( const char *inname, char **outname, int set_current_drive )
|
||||
{
|
||||
if (inname[0] == '/') {
|
||||
char *oname;
|
||||
|
||||
// logical drive is specified, check if it's one of ours
|
||||
for (int i = 0; i < _VOLUMES; i++) {
|
||||
size_t volstr_len = c_strlen( volstr[i] );
|
||||
if (0 == c_strncmp( &(inname[1]), volstr[i], volstr_len )) {
|
||||
oname = c_strdup( inname );
|
||||
c_strcpy( oname, volstr[i] );
|
||||
oname[volstr_len] = ':';
|
||||
*outname = oname;
|
||||
|
||||
if (set_current_drive) is_current_drive = TRUE;
|
||||
return &myfatfs_fs_fns;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// no logical drive in patchspec, are we current drive?
|
||||
if (is_current_drive) {
|
||||
*outname = c_strdup( inname );
|
||||
return &myfatfs_fs_fns;
|
||||
}
|
||||
}
|
||||
|
||||
if (set_current_drive) is_current_drive = FALSE;
|
||||
return NULL;
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
|
||||
#############################################################
|
||||
# Required variables for each makefile
|
||||
# Discard this section from all parent makefiles
|
||||
# Expected variables (with automatic defaults):
|
||||
# CSRCS (all "C" files in the dir)
|
||||
# SUBDIRS (all subdirs with a Makefile)
|
||||
# GEN_LIBS - list of libs to be generated ()
|
||||
# GEN_IMAGES - list of images to be generated ()
|
||||
# COMPONENTS_xxx - a list of libs/objs in the form
|
||||
# subdir/lib to be extracted and rolled up into
|
||||
# a generated lib/image xxx.a ()
|
||||
#
|
||||
ifndef PDIR
|
||||
|
||||
GEN_LIBS = liboption.a
|
||||
CSRCS = unicode.c
|
||||
|
||||
endif
|
||||
|
||||
FATFS_INC_DIR = ../
|
||||
|
||||
#############################################################
|
||||
# Configuration i.e. compile options etc.
|
||||
# Target specific stuff (defines etc.) goes in here!
|
||||
# Generally values applying to a tree are captured in the
|
||||
# makefile at its root level - these are then overridden
|
||||
# for a subtree within the makefile rooted therein
|
||||
#
|
||||
#DEFINES +=
|
||||
|
||||
#############################################################
|
||||
# Recursion Magic - Don't touch this!!
|
||||
#
|
||||
# Each subtree potentially has an include directory
|
||||
# corresponding to the common APIs applicable to modules
|
||||
# rooted at that subtree. Accordingly, the INCLUDE PATH
|
||||
# of a module can only contain the include directories up
|
||||
# its parent path, and not its siblings
|
||||
#
|
||||
# Required for each makefile to inherit from the parent
|
||||
#
|
||||
|
||||
INCLUDES := $(INCLUDES) -I $(PDIR)include
|
||||
INCLUDES += -I ./
|
||||
INCLUDES += -I ../../platform
|
||||
PDIR := ../$(PDIR)
|
||||
sinclude $(PDIR)Makefile
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,388 @@
|
|||
/*------------------------------------------------------------------------*/
|
||||
/* Unicode - Local code bidirectional converter (C)ChaN, 2015 */
|
||||
/* (SBCS code pages) */
|
||||
/*------------------------------------------------------------------------*/
|
||||
/* 437 U.S.
|
||||
/ 720 Arabic
|
||||
/ 737 Greek
|
||||
/ 771 KBL
|
||||
/ 775 Baltic
|
||||
/ 850 Latin 1
|
||||
/ 852 Latin 2
|
||||
/ 855 Cyrillic
|
||||
/ 857 Turkish
|
||||
/ 860 Portuguese
|
||||
/ 861 Icelandic
|
||||
/ 862 Hebrew
|
||||
/ 863 Canadian French
|
||||
/ 864 Arabic
|
||||
/ 865 Nordic
|
||||
/ 866 Russian
|
||||
/ 869 Greek 2
|
||||
*/
|
||||
|
||||
#include "../ff.h"
|
||||
|
||||
|
||||
#if _CODE_PAGE == 437
|
||||
#define _TBLDEF 1
|
||||
static
|
||||
const WCHAR Tbl[] = { /* CP437(0x80-0xFF) to Unicode conversion table */
|
||||
0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7, 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x00EC, 0x00C4, 0x00C5,
|
||||
0x00C9, 0x00E6, 0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9, 0x00FF, 0x00D6, 0x00DC, 0x00A2, 0x00A3, 0x00A5, 0x20A7, 0x0192,
|
||||
0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x00AA, 0x00BA, 0x00BF, 0x2310, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB,
|
||||
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510,
|
||||
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
|
||||
0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
|
||||
0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4, 0x03A6, 0x0398, 0x03A9, 0x03B4, 0x221E, 0x03C6, 0x03B5, 0x2229,
|
||||
0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248, 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0
|
||||
};
|
||||
|
||||
#elif _CODE_PAGE == 720
|
||||
#define _TBLDEF 1
|
||||
static
|
||||
const WCHAR Tbl[] = { /* CP720(0x80-0xFF) to Unicode conversion table */
|
||||
0x0000, 0x0000, 0x00E9, 0x00E2, 0x0000, 0x00E0, 0x0000, 0x00E7, 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x0000, 0x0000, 0x0000,
|
||||
0x0000, 0x0651, 0x0652, 0x00F4, 0x00A4, 0x0640, 0x00FB, 0x00F9, 0x0621, 0x0622, 0x0623, 0x0624, 0x00A3, 0x0625, 0x0626, 0x0627,
|
||||
0x0628, 0x0629, 0x062A, 0x062B, 0x062C, 0x062D, 0x062E, 0x062F, 0x0630, 0x0631, 0x0632, 0x0633, 0x0634, 0x0635, 0x00AB, 0x00BB,
|
||||
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510,
|
||||
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
|
||||
0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
|
||||
0x0636, 0x0637, 0x0638, 0x0639, 0x063A, 0x0641, 0x00B5, 0x0642, 0x0643, 0x0644, 0x0645, 0x0646, 0x0647, 0x0648, 0x0649, 0x064A,
|
||||
0x2261, 0x064B, 0x064C, 0x064D, 0x064E, 0x064F, 0x0650, 0x2248, 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0
|
||||
};
|
||||
|
||||
#elif _CODE_PAGE == 737
|
||||
#define _TBLDEF 1
|
||||
static
|
||||
const WCHAR Tbl[] = { /* CP737(0x80-0xFF) to Unicode conversion table */
|
||||
0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397, 0x0398, 0x0399, 0x039A, 0x039B, 0x039C, 0x039D, 0x039E, 0x039F, 0x03A0,
|
||||
0x03A1, 0x03A3, 0x03A4, 0x03A5, 0x03A6, 0x03A7, 0x03A8, 0x03A9, 0x03B1, 0x03B2, 0x03B3, 0x03B4, 0x03B5, 0x03B6, 0x03B7, 0x03B8,
|
||||
0x03B9, 0x03BA, 0x03BB, 0x03BC, 0x03BD, 0x03BE, 0x03BF, 0x03C0, 0x03C1, 0x03C3, 0x03C2, 0x03C4, 0x03C5, 0x03C6, 0x03C7, 0x03C8,
|
||||
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510,
|
||||
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
|
||||
0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
|
||||
0x03C9, 0x03AC, 0x03AD, 0x03AE, 0x03CA, 0x03AF, 0x03CC, 0x03CD, 0x03CB, 0x03CE, 0x0386, 0x0388, 0x0389, 0x038A, 0x038C, 0x038E,
|
||||
0x038F, 0x00B1, 0x2265, 0x2264, 0x03AA, 0x03AB, 0x00F7, 0x2248, 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0
|
||||
};
|
||||
|
||||
#elif _CODE_PAGE == 771
|
||||
#define _TBLDEF 1
|
||||
static
|
||||
const WCHAR Tbl[] = { /* CP771(0x80-0xFF) to Unicode conversion table */
|
||||
0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F,
|
||||
0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F,
|
||||
0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437, 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F,
|
||||
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x2558, 0x2510,
|
||||
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
|
||||
0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518, 0x250C, 0x2588, 0x0104, 0x0105, 0x010C, 0x010D,
|
||||
0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447, 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F,
|
||||
0x0118, 0x0119, 0x0116, 0x0117, 0x012E, 0x012F, 0x0160, 0x0161, 0x0172, 0x0173, 0x016A, 0x016B, 0x017D, 0x017E, 0x25A0, 0x00A0
|
||||
};
|
||||
|
||||
#elif _CODE_PAGE == 775
|
||||
#define _TBLDEF 1
|
||||
static
|
||||
const WCHAR Tbl[] = { /* CP775(0x80-0xFF) to Unicode conversion table */
|
||||
0x0106, 0x00FC, 0x00E9, 0x0101, 0x00E4, 0x0123, 0x00E5, 0x0107, 0x0142, 0x0113, 0x0156, 0x0157, 0x012B, 0x0179, 0x00C4, 0x00C5,
|
||||
0x00C9, 0x00E6, 0x00C6, 0x014D, 0x00F6, 0x0122, 0x00A2, 0x015A, 0x015B, 0x00D6, 0x00DC, 0x00F8, 0x00A3, 0x00D8, 0x00D7, 0x00A4,
|
||||
0x0100, 0x012A, 0x00F3, 0x017B, 0x017C, 0x017A, 0x201D, 0x00A6, 0x00A9, 0x00AE, 0x00AC, 0x00BD, 0x00BC, 0x0141, 0x00AB, 0x00BB,
|
||||
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x0104, 0x010C, 0x0118, 0x0116, 0x2563, 0x2551, 0x2557, 0x255D, 0x012E, 0x0160, 0x2510,
|
||||
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x0172, 0x016A, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x017D,
|
||||
0x0105, 0x010D, 0x0119, 0x0117, 0x012F, 0x0161, 0x0173, 0x016B, 0x017E, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
|
||||
0x00D3, 0x00DF, 0x014C, 0x0143, 0x00F5, 0x00D5, 0x00B5, 0x0144, 0x0136, 0x0137, 0x013B, 0x013C, 0x0146, 0x0112, 0x0145, 0x2019,
|
||||
0x00AD, 0x00B1, 0x201C, 0x00BE, 0x00B6, 0x00A7, 0x00F7, 0x201E, 0x00B0, 0x2219, 0x00B7, 0x00B9, 0x00B3, 0x00B2, 0x25A0, 0x00A0
|
||||
};
|
||||
|
||||
#elif _CODE_PAGE == 850
|
||||
#define _TBLDEF 1
|
||||
static
|
||||
const WCHAR Tbl[] = { /* CP850(0x80-0xFF) to Unicode conversion table */
|
||||
0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7, 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x00EC, 0x00C4, 0x00C5,
|
||||
0x00C9, 0x00E6, 0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9, 0x00FF, 0x00D6, 0x00DC, 0x00F8, 0x00A3, 0x00D8, 0x00D7, 0x0192,
|
||||
0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x00AA, 0x00BA, 0x00BF, 0x00AE, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB,
|
||||
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x00C1, 0x00C2, 0x00C0, 0x00A9, 0x2563, 0x2551, 0x2557, 0x255D, 0x00A2, 0x00A5, 0x2510,
|
||||
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x00E3, 0x00C3, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x00A4,
|
||||
0x00F0, 0x00D0, 0x00CA, 0x00CB, 0x00C8, 0x0131, 0x00CD, 0x00CE, 0x00CF, 0x2518, 0x250C, 0x2588, 0x2584, 0x00A6, 0x00CC, 0x2580,
|
||||
0x00D3, 0x00DF, 0x00D4, 0x00D2, 0x00F5, 0x00D5, 0x00B5, 0x00FE, 0x00DE, 0x00DA, 0x00DB, 0x00D9, 0x00FD, 0x00DD, 0x00AF, 0x00B4,
|
||||
0x00AD, 0x00B1, 0x2017, 0x00BE, 0x00B6, 0x00A7, 0x00F7, 0x00B8, 0x00B0, 0x00A8, 0x00B7, 0x00B9, 0x00B3, 0x00B2, 0x25A0, 0x00A0
|
||||
};
|
||||
|
||||
#elif _CODE_PAGE == 852
|
||||
#define _TBLDEF 1
|
||||
static
|
||||
const WCHAR Tbl[] = { /* CP852(0x80-0xFF) to Unicode conversion table */
|
||||
0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x016F, 0x0107, 0x00E7, 0x0142, 0x00EB, 0x0150, 0x0151, 0x00EE, 0x0179, 0x00C4, 0x0106,
|
||||
0x00C9, 0x0139, 0x013A, 0x00F4, 0x00F6, 0x013D, 0x013E, 0x015A, 0x015B, 0x00D6, 0x00DC, 0x0164, 0x0165, 0x0141, 0x00D7, 0x010D,
|
||||
0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x0104, 0x0105, 0x017D, 0x017E, 0x0118, 0x0119, 0x00AC, 0x017A, 0x010C, 0x015F, 0x00AB, 0x00BB,
|
||||
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x00C1, 0x00C2, 0x011A, 0x015E, 0x2563, 0x2551, 0x2557, 0x255D, 0x017B, 0x017C, 0x2510,
|
||||
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x0102, 0x0103, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x00A4,
|
||||
0x0111, 0x0110, 0x010E, 0x00CB, 0x010F, 0x0147, 0x00CD, 0x00CE, 0x011B, 0x2518, 0x250C, 0x2588, 0x2584, 0x0162, 0x016E, 0x2580,
|
||||
0x00D3, 0x00DF, 0x00D4, 0x0143, 0x0144, 0x0148, 0x0160, 0x0161, 0x0154, 0x00DA, 0x0155, 0x0170, 0x00FD, 0x00DD, 0x0163, 0x00B4,
|
||||
0x00AD, 0x02DD, 0x02DB, 0x02C7, 0x02D8, 0x00A7, 0x00F7, 0x00B8, 0x00B0, 0x00A8, 0x02D9, 0x0171, 0x0158, 0x0159, 0x25A0, 0x00A0
|
||||
};
|
||||
|
||||
#elif _CODE_PAGE == 855
|
||||
#define _TBLDEF 1
|
||||
static
|
||||
const WCHAR Tbl[] = { /* CP855(0x80-0xFF) to Unicode conversion table */
|
||||
0x0452, 0x0402, 0x0453, 0x0403, 0x0451, 0x0401, 0x0454, 0x0404, 0x0455, 0x0405, 0x0456, 0x0406, 0x0457, 0x0407, 0x0458, 0x0408,
|
||||
0x0459, 0x0409, 0x045A, 0x040A, 0x045B, 0x040B, 0x045C, 0x040C, 0x045E, 0x040E, 0x045F, 0x040F, 0x044E, 0x042E, 0x044A, 0x042A,
|
||||
0x0430, 0x0410, 0x0431, 0x0411, 0x0446, 0x0426, 0x0434, 0x0414, 0x0435, 0x0415, 0x0444, 0x0424, 0x0433, 0x0413, 0x00AB, 0x00BB,
|
||||
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x0445, 0x0425, 0x0438, 0x0418, 0x2563, 0x2551, 0x2557, 0x255D, 0x0439, 0x0419, 0x2510,
|
||||
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x043A, 0x041A, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x00A4,
|
||||
0x043B, 0x041B, 0x043C, 0x041C, 0x043D, 0x041D, 0x043E, 0x041E, 0x043F, 0x2518, 0x250C, 0x2588, 0x2584, 0x041F, 0x044F, 0x2580,
|
||||
0x042F, 0x0440, 0x0420, 0x0441, 0x0421, 0x0442, 0x0422, 0x0443, 0x0423, 0x0436, 0x0416, 0x0432, 0x0412, 0x044C, 0x042C, 0x2116,
|
||||
0x00AD, 0x044B, 0x042B, 0x0437, 0x0417, 0x0448, 0x0428, 0x044D, 0x042D, 0x0449, 0x0429, 0x0447, 0x0427, 0x00A7, 0x25A0, 0x00A0
|
||||
};
|
||||
|
||||
#elif _CODE_PAGE == 857
|
||||
#define _TBLDEF 1
|
||||
static
|
||||
const WCHAR Tbl[] = { /* CP857(0x80-0xFF) to Unicode conversion table */
|
||||
0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7, 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x0131, 0x00C4, 0x00C5,
|
||||
0x00C9, 0x00E6, 0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9, 0x0130, 0x00D6, 0x00DC, 0x00F8, 0x00A3, 0x00D8, 0x015E, 0x015F,
|
||||
0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x011E, 0x011F, 0x00BF, 0x00AE, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB,
|
||||
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x00C1, 0x00C2, 0x00C0, 0x00A9, 0x2563, 0x2551, 0x2557, 0x255D, 0x00A2, 0x00A5, 0x2510,
|
||||
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x00E3, 0x00C3, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x00A4,
|
||||
0x00BA, 0x00AA, 0x00CA, 0x00CB, 0x00C8, 0x0000, 0x00CD, 0x00CE, 0x00CF, 0x2518, 0x250C, 0x2588, 0x2584, 0x00A6, 0x00CC, 0x2580,
|
||||
0x00D3, 0x00DF, 0x00D4, 0x00D2, 0x00F5, 0x00D5, 0x00B5, 0x0000, 0x00D7, 0x00DA, 0x00DB, 0x00D9, 0x00EC, 0x00FF, 0x00AF, 0x00B4,
|
||||
0x00AD, 0x00B1, 0x0000, 0x00BE, 0x00B6, 0x00A7, 0x00F7, 0x00B8, 0x00B0, 0x00A8, 0x00B7, 0x00B9, 0x00B3, 0x00B2, 0x25A0, 0x00A0
|
||||
};
|
||||
|
||||
#elif _CODE_PAGE == 860
|
||||
#define _TBLDEF 1
|
||||
static
|
||||
const WCHAR Tbl[] = { /* CP860(0x80-0xFF) to Unicode conversion table */
|
||||
0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E3, 0x00E0, 0x00C1, 0x00E7, 0x00EA, 0x00CA, 0x00E8, 0x00CD, 0x00D4, 0x00EC, 0x00C3, 0x00C2,
|
||||
0x00C9, 0x00C0, 0x00C8, 0x00F4, 0x00F5, 0x00F2, 0x00DA, 0x00F9, 0x00CC, 0x00D5, 0x00DC, 0x00A2, 0x00A3, 0x00D9, 0x20A7, 0x00D3,
|
||||
0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x00AA, 0x00BA, 0x00BF, 0x00D2, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB,
|
||||
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x2558, 0x2510,
|
||||
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
|
||||
0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
|
||||
0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4, 0x03A6, 0x0398, 0x03A9, 0x03B4, 0x221E, 0x03C6, 0x03B5, 0x2229,
|
||||
0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248, 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0
|
||||
};
|
||||
|
||||
#elif _CODE_PAGE == 861
|
||||
#define _TBLDEF 1
|
||||
static
|
||||
const WCHAR Tbl[] = { /* CP861(0x80-0xFF) to Unicode conversion table */
|
||||
0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E6, 0x00E7, 0x00EA, 0x00EB, 0x00E8, 0x00D0, 0x00F0, 0x00DE, 0x00C4, 0x00C5,
|
||||
0x00C9, 0x00E6, 0x00C6, 0x00F4, 0x00F6, 0x00FE, 0x00FB, 0x00DD, 0x00FD, 0x00D6, 0x00DC, 0x00F8, 0x00A3, 0x00D8, 0x20A7, 0x0192,
|
||||
0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00C1, 0x00CD, 0x00D3, 0x00DA, 0x00BF, 0x2310, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB,
|
||||
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510,
|
||||
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
|
||||
0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
|
||||
0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4, 0x03A6, 0x0398, 0x03A9, 0x03B4, 0x221E, 0x03C6, 0x03B5, 0x2229,
|
||||
0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248, 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0
|
||||
};
|
||||
|
||||
#elif _CODE_PAGE == 862
|
||||
#define _TBLDEF 1
|
||||
static
|
||||
const WCHAR Tbl[] = { /* CP862(0x80-0xFF) to Unicode conversion table */
|
||||
0x05D0, 0x05D1, 0x05D2, 0x05D3, 0x05D4, 0x05D5, 0x05D6, 0x05D7, 0x05D8, 0x05D9, 0x05DA, 0x05DB, 0x05DC, 0x05DD, 0x05DE, 0x05DF,
|
||||
0x05E0, 0x05E1, 0x05E2, 0x05E3, 0x05E4, 0x05E5, 0x05E6, 0x05E7, 0x05E8, 0x05E9, 0x05EA, 0x00A2, 0x00A3, 0x00A5, 0x20A7, 0x0192,
|
||||
0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x00AA, 0x00BA, 0x00BF, 0x2310, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB,
|
||||
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510,
|
||||
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
|
||||
0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
|
||||
0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4, 0x03A6, 0x0398, 0x03A9, 0x03B4, 0x221E, 0x03C6, 0x03B5, 0x2229,
|
||||
0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248, 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0
|
||||
};
|
||||
|
||||
#elif _CODE_PAGE == 863
|
||||
#define _TBLDEF 1
|
||||
static
|
||||
const WCHAR Tbl[] = { /* CP863(0x80-0xFF) to Unicode conversion table */
|
||||
0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00C2, 0x00E0, 0x00B6, 0x00E7, 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x00EC, 0x2017, 0x00C0,
|
||||
0x00C9, 0x00C8, 0x00CA, 0x00F4, 0x00CB, 0x00CF, 0x00FB, 0x00F9, 0x00A4, 0x00D4, 0x00DC, 0x00A2, 0x00A3, 0x00D9, 0x00DB, 0x0192,
|
||||
0x00A6, 0x00B4, 0x00F3, 0x00FA, 0x00A8, 0x00BB, 0x00B3, 0x00AF, 0x00CE, 0x3210, 0x00AC, 0x00BD, 0x00BC, 0x00BE, 0x00AB, 0x00BB,
|
||||
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510,
|
||||
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
|
||||
0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
|
||||
0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4, 0x03A6, 0x0398, 0x03A9, 0x03B4, 0x221E, 0x03C6, 0x03B5, 0x2219,
|
||||
0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248, 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0
|
||||
};
|
||||
|
||||
#elif _CODE_PAGE == 864
|
||||
#define _TBLDEF 1
|
||||
static
|
||||
const WCHAR Tbl[] = { /* CP864(0x80-0xFF) to Unicode conversion table */
|
||||
0x00B0, 0x00B7, 0x2219, 0x221A, 0x2592, 0x2500, 0x2502, 0x253C, 0x2524, 0x252C, 0x251C, 0x2534, 0x2510, 0x250C, 0x2514, 0x2518,
|
||||
0x03B2, 0x221E, 0x03C6, 0x00B1, 0x00BD, 0x00BC, 0x2248, 0x00AB, 0x00BB, 0xFEF7, 0xFEF8, 0x0000, 0x0000, 0xFEFB, 0xFEFC, 0x0000,
|
||||
0x00A0, 0x00AD, 0xFE82, 0x00A3, 0x00A4, 0xFE84, 0x0000, 0x20AC, 0xFE8E, 0xFE8F, 0xFE95, 0xFE99, 0x060C, 0xFE9D, 0xFEA1, 0xFEA5,
|
||||
0x0660, 0x0661, 0x0662, 0x0663, 0x0664, 0x0665, 0x0666, 0x0667, 0x0668, 0x0669, 0xFED1, 0x061B, 0xFEB1, 0xFEB5, 0xFEB9, 0x061F,
|
||||
0x00A2, 0xFE80, 0xFE81, 0xFE83, 0xFE85, 0xFECA, 0xFE8B, 0xFE8D, 0xFE91, 0xFE93, 0xFE97, 0xFE9B, 0xFE9F, 0xFEA3, 0xFEA7, 0xFEA9,
|
||||
0xFEAB, 0xFEAD, 0xFEAF, 0xFEB3, 0xFEB7, 0xFEBB, 0xFEBF, 0xFEC1, 0xFEC5, 0xFECB, 0xFECF, 0x00A6, 0x00AC, 0x00F7, 0x00D7, 0xFEC9,
|
||||
0x0640, 0xFED3, 0xFED7, 0xFEDB, 0xFEDF, 0xFEE3, 0xFEE7, 0xFEEB, 0xFEED, 0xFEEF, 0xFEF3, 0xFEBD, 0xFECC, 0xFECE, 0xFECD, 0xFEE1,
|
||||
0xFE7D, 0x0651, 0xFEE5, 0xFEE9, 0xFEEC, 0xFEF0, 0xFEF2, 0xFED0, 0xFED5, 0xFEF5, 0xFEF6, 0xFEDD, 0xFED9, 0xFEF1, 0x25A0, 0x0000
|
||||
};
|
||||
|
||||
#elif _CODE_PAGE == 865
|
||||
#define _TBLDEF 1
|
||||
static
|
||||
const WCHAR Tbl[] = { /* CP865(0x80-0xFF) to Unicode conversion table */
|
||||
0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7, 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x00EC, 0x00C4, 0x00C5,
|
||||
0x00C5, 0x00E6, 0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9, 0x00FF, 0x00D6, 0x00DC, 0x00F8, 0x00A3, 0x00D8, 0x20A7, 0x0192,
|
||||
0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x00AA, 0x00BA, 0x00BF, 0x2310, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00A4,
|
||||
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x2558, 0x2510,
|
||||
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
|
||||
0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
|
||||
0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4, 0x03A6, 0x0398, 0x03A9, 0x03B4, 0x221E, 0x03C6, 0x03B5, 0x2229,
|
||||
0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248, 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0
|
||||
};
|
||||
|
||||
#elif _CODE_PAGE == 866
|
||||
#define _TBLDEF 1
|
||||
static
|
||||
const WCHAR Tbl[] = { /* CP866(0x80-0xFF) to Unicode conversion table */
|
||||
0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F,
|
||||
0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F,
|
||||
0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437, 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F,
|
||||
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510,
|
||||
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
|
||||
0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
|
||||
0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447, 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F,
|
||||
0x0401, 0x0451, 0x0404, 0x0454, 0x0407, 0x0457, 0x040E, 0x045E, 0x00B0, 0x2219, 0x00B7, 0x221A, 0x2116, 0x00A4, 0x25A0, 0x00A0
|
||||
};
|
||||
|
||||
#elif _CODE_PAGE == 869
|
||||
#define _TBLDEF 1
|
||||
static
|
||||
const WCHAR Tbl[] = { /* CP869(0x80-0xFF) to Unicode conversion table */
|
||||
0x00B7, 0x00B7, 0x00B7, 0x00B7, 0x00B7, 0x00B7, 0x0386, 0x00B7, 0x00B7, 0x00AC, 0x00A6, 0x2018, 0x2019, 0x0388, 0x2015, 0x0389,
|
||||
0x038A, 0x03AA, 0x038C, 0x00B7, 0x00B7, 0x038E, 0x03AB, 0x00A9, 0x038F, 0x00B2, 0x00B3, 0x03AC, 0x00A3, 0x03AD, 0x03AE, 0x03AF,
|
||||
0x03CA, 0x0390, 0x03CC, 0x03CD, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397, 0x00BD, 0x0398, 0x0399, 0x00AB, 0x00BB,
|
||||
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x039A, 0x039B, 0x039C, 0x039D, 0x2563, 0x2551, 0x2557, 0x255D, 0x039E, 0x039F, 0x2510,
|
||||
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x0A30, 0x03A1, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x03A3,
|
||||
0x03A4, 0x03A5, 0x03A6, 0x03A7, 0x03A8, 0x03A9, 0x03B1, 0x03B2, 0x03B3, 0x2518, 0x250C, 0x2588, 0x2584, 0x03B4, 0x03B5, 0x2580,
|
||||
0x03B6, 0x03B7, 0x03B8, 0x03B9, 0x03BA, 0x03BB, 0x03BC, 0x03BD, 0x03BE, 0x03BF, 0x03C0, 0x03C1, 0x03C3, 0x03C2, 0x03C4, 0x0384,
|
||||
0x00AD, 0x00B1, 0x03C5, 0x03C6, 0x03C7, 0x00A7, 0x03C8, 0x0385, 0x00B0, 0x00A8, 0x03C9, 0x03CB, 0x03B0, 0x03CE, 0x25A0, 0x00A0
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#if !_TBLDEF || !_USE_LFN
|
||||
#error This file is not needed at current configuration. Remove from the project.
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
WCHAR ff_convert ( /* Converted character, Returns zero on error */
|
||||
WCHAR chr, /* Character code to be converted */
|
||||
UINT dir /* 0: Unicode to OEM code, 1: OEM code to Unicode */
|
||||
)
|
||||
{
|
||||
WCHAR c;
|
||||
|
||||
|
||||
if (chr < 0x80) { /* ASCII */
|
||||
c = chr;
|
||||
|
||||
} else {
|
||||
if (dir) { /* OEM code to Unicode */
|
||||
c = (chr >= 0x100) ? 0 : Tbl[chr - 0x80];
|
||||
|
||||
} else { /* Unicode to OEM code */
|
||||
for (c = 0; c < 0x80; c++) {
|
||||
if (chr == Tbl[c]) break;
|
||||
}
|
||||
c = (c + 0x80) & 0xFF;
|
||||
}
|
||||
}
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
|
||||
WCHAR ff_wtoupper ( /* Returns upper converted character */
|
||||
WCHAR chr /* Unicode character to be upper converted (BMP only) */
|
||||
)
|
||||
{
|
||||
/* Compressed upper conversion table */
|
||||
static const WCHAR cvt1[] = { /* U+0000 - U+0FFF */
|
||||
/* Basic Latin */
|
||||
0x0061,0x031A,
|
||||
/* Latin-1 Supplement */
|
||||
0x00E0,0x0317, 0x00F8,0x0307, 0x00FF,0x0001,0x0178,
|
||||
/* Latin Extended-A */
|
||||
0x0100,0x0130, 0x0132,0x0106, 0x0139,0x0110, 0x014A,0x012E, 0x0179,0x0106,
|
||||
/* Latin Extended-B */
|
||||
0x0180,0x004D,0x0243,0x0181,0x0182,0x0182,0x0184,0x0184,0x0186,0x0187,0x0187,0x0189,0x018A,0x018B,0x018B,0x018D,0x018E,0x018F,0x0190,0x0191,0x0191,0x0193,0x0194,0x01F6,0x0196,0x0197,0x0198,0x0198,0x023D,0x019B,0x019C,0x019D,0x0220,0x019F,0x01A0,0x01A0,0x01A2,0x01A2,0x01A4,0x01A4,0x01A6,0x01A7,0x01A7,0x01A9,0x01AA,0x01AB,0x01AC,0x01AC,0x01AE,0x01AF,0x01AF,0x01B1,0x01B2,0x01B3,0x01B3,0x01B5,0x01B5,0x01B7,0x01B8,0x01B8,0x01BA,0x01BB,0x01BC,0x01BC,0x01BE,0x01F7,0x01C0,0x01C1,0x01C2,0x01C3,0x01C4,0x01C5,0x01C4,0x01C7,0x01C8,0x01C7,0x01CA,0x01CB,0x01CA,
|
||||
0x01CD,0x0110, 0x01DD,0x0001,0x018E, 0x01DE,0x0112, 0x01F3,0x0003,0x01F1,0x01F4,0x01F4, 0x01F8,0x0128,
|
||||
0x0222,0x0112, 0x023A,0x0009,0x2C65,0x023B,0x023B,0x023D,0x2C66,0x023F,0x0240,0x0241,0x0241, 0x0246,0x010A,
|
||||
/* IPA Extensions */
|
||||
0x0253,0x0040,0x0181,0x0186,0x0255,0x0189,0x018A,0x0258,0x018F,0x025A,0x0190,0x025C,0x025D,0x025E,0x025F,0x0193,0x0261,0x0262,0x0194,0x0264,0x0265,0x0266,0x0267,0x0197,0x0196,0x026A,0x2C62,0x026C,0x026D,0x026E,0x019C,0x0270,0x0271,0x019D,0x0273,0x0274,0x019F,0x0276,0x0277,0x0278,0x0279,0x027A,0x027B,0x027C,0x2C64,0x027E,0x027F,0x01A6,0x0281,0x0282,0x01A9,0x0284,0x0285,0x0286,0x0287,0x01AE,0x0244,0x01B1,0x01B2,0x0245,0x028D,0x028E,0x028F,0x0290,0x0291,0x01B7,
|
||||
/* Greek, Coptic */
|
||||
0x037B,0x0003,0x03FD,0x03FE,0x03FF, 0x03AC,0x0004,0x0386,0x0388,0x0389,0x038A, 0x03B1,0x0311,
|
||||
0x03C2,0x0002,0x03A3,0x03A3, 0x03C4,0x0308, 0x03CC,0x0003,0x038C,0x038E,0x038F, 0x03D8,0x0118,
|
||||
0x03F2,0x000A,0x03F9,0x03F3,0x03F4,0x03F5,0x03F6,0x03F7,0x03F7,0x03F9,0x03FA,0x03FA,
|
||||
/* Cyrillic */
|
||||
0x0430,0x0320, 0x0450,0x0710, 0x0460,0x0122, 0x048A,0x0136, 0x04C1,0x010E, 0x04CF,0x0001,0x04C0, 0x04D0,0x0144,
|
||||
/* Armenian */
|
||||
0x0561,0x0426,
|
||||
|
||||
0x0000
|
||||
};
|
||||
static const WCHAR cvt2[] = { /* U+1000 - U+FFFF */
|
||||
/* Phonetic Extensions */
|
||||
0x1D7D,0x0001,0x2C63,
|
||||
/* Latin Extended Additional */
|
||||
0x1E00,0x0196, 0x1EA0,0x015A,
|
||||
/* Greek Extended */
|
||||
0x1F00,0x0608, 0x1F10,0x0606, 0x1F20,0x0608, 0x1F30,0x0608, 0x1F40,0x0606,
|
||||
0x1F51,0x0007,0x1F59,0x1F52,0x1F5B,0x1F54,0x1F5D,0x1F56,0x1F5F, 0x1F60,0x0608,
|
||||
0x1F70,0x000E,0x1FBA,0x1FBB,0x1FC8,0x1FC9,0x1FCA,0x1FCB,0x1FDA,0x1FDB,0x1FF8,0x1FF9,0x1FEA,0x1FEB,0x1FFA,0x1FFB,
|
||||
0x1F80,0x0608, 0x1F90,0x0608, 0x1FA0,0x0608, 0x1FB0,0x0004,0x1FB8,0x1FB9,0x1FB2,0x1FBC,
|
||||
0x1FCC,0x0001,0x1FC3, 0x1FD0,0x0602, 0x1FE0,0x0602, 0x1FE5,0x0001,0x1FEC, 0x1FF2,0x0001,0x1FFC,
|
||||
/* Letterlike Symbols */
|
||||
0x214E,0x0001,0x2132,
|
||||
/* Number forms */
|
||||
0x2170,0x0210, 0x2184,0x0001,0x2183,
|
||||
/* Enclosed Alphanumerics */
|
||||
0x24D0,0x051A, 0x2C30,0x042F,
|
||||
/* Latin Extended-C */
|
||||
0x2C60,0x0102, 0x2C67,0x0106, 0x2C75,0x0102,
|
||||
/* Coptic */
|
||||
0x2C80,0x0164,
|
||||
/* Georgian Supplement */
|
||||
0x2D00,0x0826,
|
||||
/* Full-width */
|
||||
0xFF41,0x031A,
|
||||
|
||||
0x0000
|
||||
};
|
||||
const WCHAR *p;
|
||||
WCHAR bc, nc, cmd;
|
||||
|
||||
|
||||
p = chr < 0x1000 ? cvt1 : cvt2;
|
||||
for (;;) {
|
||||
bc = *p++; /* Get block base */
|
||||
if (!bc || chr < bc) break;
|
||||
nc = *p++; cmd = nc >> 8; nc &= 0xFF; /* Get processing command and block size */
|
||||
if (chr < bc + nc) { /* In the block? */
|
||||
switch (cmd) {
|
||||
case 0: chr = p[chr - bc]; break; /* Table conversion */
|
||||
case 1: chr -= (chr - bc) & 1; break; /* Case pairs */
|
||||
case 2: chr -= 16; break; /* Shift -16 */
|
||||
case 3: chr -= 32; break; /* Shift -32 */
|
||||
case 4: chr -= 48; break; /* Shift -48 */
|
||||
case 5: chr -= 26; break; /* Shift -26 */
|
||||
case 6: chr += 8; break; /* Shift +8 */
|
||||
case 7: chr -= 80; break; /* Shift -80 */
|
||||
case 8: chr -= 0x1C60; break; /* Shift -0x1C60 */
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (!cmd) p += nc;
|
||||
}
|
||||
|
||||
return chr;
|
||||
}
|
||||
|
|
@ -0,0 +1,151 @@
|
|||
/*------------------------------------------------------------------------*/
|
||||
/* Sample code of OS dependent controls for FatFs */
|
||||
/* (C)ChaN, 2014 */
|
||||
/*------------------------------------------------------------------------*/
|
||||
|
||||
|
||||
#include "../ff.h"
|
||||
|
||||
|
||||
#if _FS_REENTRANT
|
||||
/*------------------------------------------------------------------------*/
|
||||
/* Create a Synchronization Object
|
||||
/*------------------------------------------------------------------------*/
|
||||
/* This function is called in f_mount() function to create a new
|
||||
/ synchronization object, such as semaphore and mutex. When a 0 is returned,
|
||||
/ the f_mount() function fails with FR_INT_ERR.
|
||||
*/
|
||||
|
||||
int ff_cre_syncobj ( /* 1:Function succeeded, 0:Could not create the sync object */
|
||||
BYTE vol, /* Corresponding volume (logical drive number) */
|
||||
_SYNC_t *sobj /* Pointer to return the created sync object */
|
||||
)
|
||||
{
|
||||
int ret;
|
||||
|
||||
|
||||
*sobj = CreateMutex(NULL, FALSE, NULL); /* Win32 */
|
||||
ret = (int)(*sobj != INVALID_HANDLE_VALUE);
|
||||
|
||||
// *sobj = SyncObjects[vol]; /* uITRON (give a static sync object) */
|
||||
// ret = 1; /* The initial value of the semaphore must be 1. */
|
||||
|
||||
// *sobj = OSMutexCreate(0, &err); /* uC/OS-II */
|
||||
// ret = (int)(err == OS_NO_ERR);
|
||||
|
||||
// *sobj = xSemaphoreCreateMutex(); /* FreeRTOS */
|
||||
// ret = (int)(*sobj != NULL);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------*/
|
||||
/* Delete a Synchronization Object */
|
||||
/*------------------------------------------------------------------------*/
|
||||
/* This function is called in f_mount() function to delete a synchronization
|
||||
/ object that created with ff_cre_syncobj() function. When a 0 is returned,
|
||||
/ the f_mount() function fails with FR_INT_ERR.
|
||||
*/
|
||||
|
||||
int ff_del_syncobj ( /* 1:Function succeeded, 0:Could not delete due to any error */
|
||||
_SYNC_t sobj /* Sync object tied to the logical drive to be deleted */
|
||||
)
|
||||
{
|
||||
int ret;
|
||||
|
||||
|
||||
ret = CloseHandle(sobj); /* Win32 */
|
||||
|
||||
// ret = 1; /* uITRON (nothing to do) */
|
||||
|
||||
// OSMutexDel(sobj, OS_DEL_ALWAYS, &err); /* uC/OS-II */
|
||||
// ret = (int)(err == OS_NO_ERR);
|
||||
|
||||
// vSemaphoreDelete(sobj); /* FreeRTOS */
|
||||
// ret = 1;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------*/
|
||||
/* Request Grant to Access the Volume */
|
||||
/*------------------------------------------------------------------------*/
|
||||
/* This function is called on entering file functions to lock the volume.
|
||||
/ When a 0 is returned, the file function fails with FR_TIMEOUT.
|
||||
*/
|
||||
|
||||
int ff_req_grant ( /* 1:Got a grant to access the volume, 0:Could not get a grant */
|
||||
_SYNC_t sobj /* Sync object to wait */
|
||||
)
|
||||
{
|
||||
int ret;
|
||||
|
||||
ret = (int)(WaitForSingleObject(sobj, _FS_TIMEOUT) == WAIT_OBJECT_0); /* Win32 */
|
||||
|
||||
// ret = (int)(wai_sem(sobj) == E_OK); /* uITRON */
|
||||
|
||||
// OSMutexPend(sobj, _FS_TIMEOUT, &err)); /* uC/OS-II */
|
||||
// ret = (int)(err == OS_NO_ERR);
|
||||
|
||||
// ret = (int)(xSemaphoreTake(sobj, _FS_TIMEOUT) == pdTRUE); /* FreeRTOS */
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------*/
|
||||
/* Release Grant to Access the Volume */
|
||||
/*------------------------------------------------------------------------*/
|
||||
/* This function is called on leaving file functions to unlock the volume.
|
||||
*/
|
||||
|
||||
void ff_rel_grant (
|
||||
_SYNC_t sobj /* Sync object to be signaled */
|
||||
)
|
||||
{
|
||||
ReleaseMutex(sobj); /* Win32 */
|
||||
|
||||
// sig_sem(sobj); /* uITRON */
|
||||
|
||||
// OSMutexPost(sobj); /* uC/OS-II */
|
||||
|
||||
// xSemaphoreGive(sobj); /* FreeRTOS */
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
#if _USE_LFN == 3 /* LFN with a working buffer on the heap */
|
||||
/*------------------------------------------------------------------------*/
|
||||
/* Allocate a memory block */
|
||||
/*------------------------------------------------------------------------*/
|
||||
/* If a NULL is returned, the file function fails with FR_NOT_ENOUGH_CORE.
|
||||
*/
|
||||
|
||||
void* ff_memalloc ( /* Returns pointer to the allocated memory block */
|
||||
UINT msize /* Number of bytes to allocate */
|
||||
)
|
||||
{
|
||||
return malloc(msize); /* Allocate a new memory block with POSIX API */
|
||||
}
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------*/
|
||||
/* Free a memory block */
|
||||
/*------------------------------------------------------------------------*/
|
||||
|
||||
void ff_memfree (
|
||||
void* mblock /* Pointer to the memory block to free */
|
||||
)
|
||||
{
|
||||
free(mblock); /* Discard the memory block with POSIX API */
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,17 @@
|
|||
#include "../ff.h"
|
||||
|
||||
#if _USE_LFN != 0
|
||||
|
||||
#if _CODE_PAGE == 932 /* Japanese Shift_JIS */
|
||||
#include "cc932.c"
|
||||
#elif _CODE_PAGE == 936 /* Simplified Chinese GBK */
|
||||
#include "cc936.c"
|
||||
#elif _CODE_PAGE == 949 /* Korean */
|
||||
#include "cc949.c"
|
||||
#elif _CODE_PAGE == 950 /* Traditional Chinese Big5 */
|
||||
#include "cc950.c"
|
||||
#else /* Single Byte Character-Set */
|
||||
#include "ccsbcs.c"
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -11,6 +11,9 @@
|
|||
#define SPI 0
|
||||
#define HSPI 1
|
||||
|
||||
#define SPI_ORDER_LSB 0
|
||||
#define SPI_ORDER_MSB 1
|
||||
|
||||
|
||||
|
||||
//lcd drive function
|
||||
|
@ -18,7 +21,13 @@ void spi_lcd_mode_init(uint8 spi_no);
|
|||
void spi_lcd_9bit_write(uint8 spi_no,uint8 high_bit,uint8 low_8bit);
|
||||
|
||||
//spi master init funtion
|
||||
uint32_t spi_set_clkdiv(uint8 spi_no, uint32_t clock_div);
|
||||
void spi_master_init(uint8 spi_no, unsigned cpol, unsigned cpha, uint32_t clock_div);
|
||||
void spi_mast_byte_order(uint8 spi_no, uint8 order);
|
||||
// blocked buffer write
|
||||
void spi_mast_blkset(uint8 spi_no, size_t bitlen, const uint8 *data);
|
||||
// blocked buffer read
|
||||
void spi_mast_blkget(uint8 spi_no, size_t bitlen, uint8 *data);
|
||||
// fill MOSI buffer
|
||||
void spi_mast_set_mosi(uint8 spi_no, uint16 offset, uint8 bitlen, uint32 data);
|
||||
// retrieve data from MISO buffer
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
#ifndef __FATFS_CONFIG_H__
|
||||
#define __FATFS_CONFIG_H__
|
||||
|
||||
|
||||
// don't redefine the PARTITION type
|
||||
#ifndef _FATFS
|
||||
typedef struct {
|
||||
BYTE pd; /* Physical drive number */
|
||||
BYTE pt; /* Partition: 0:Auto detect, 1-4:Forced partition) */
|
||||
} PARTITION;
|
||||
#endif
|
||||
|
||||
// Table to map physical drive & partition to a logical volume.
|
||||
// The first value is the physical drive and contains the GPIO pin for SS/CS of the SD card (default pin 8)
|
||||
// The second value is the partition number.
|
||||
#define NUM_LOGICAL_DRIVES 4
|
||||
PARTITION VolToPart[NUM_LOGICAL_DRIVES] = {
|
||||
{8, 1}, /* Logical drive "0:" ==> SS pin 8, 1st partition */
|
||||
{8, 2}, /* Logical drive "1:" ==> SS pin 8, 2st partition */
|
||||
{8, 3}, /* Logical drive "2:" ==> SS pin 8, 3st partition */
|
||||
{8, 4} /* Logical drive "3:" ==> SS pin 8, 4st partition */
|
||||
};
|
||||
|
||||
#endif /* __FATFS_CONFIG_H__ */
|
|
@ -68,9 +68,14 @@ extern void luaL_assertfail(const char *file, int line, const char *message);
|
|||
//#define MD2_ENABLE
|
||||
#define SHA2_ENABLE
|
||||
|
||||
#define BUILD_SPIFFS 1
|
||||
#define BUILD_SPIFFS
|
||||
#define SPIFFS_CACHE 1
|
||||
|
||||
//#define BUILD_FATFS
|
||||
|
||||
// maximum length of a filename
|
||||
#define FS_OBJ_NAME_LEN 31
|
||||
|
||||
// Uncomment this next line for fastest startup
|
||||
// It reduces the format time dramatically
|
||||
// #define SPIFFS_MAX_FILESYSTEM_SIZE 32768
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
#include C_HEADER_STDLIB
|
||||
#include C_HEADER_STRING
|
||||
#ifndef LUA_CROSS_COMPILER
|
||||
#include "flash_fs.h"
|
||||
#include "vfs.h"
|
||||
#else
|
||||
#endif
|
||||
|
||||
|
@ -671,8 +671,8 @@ static const char *getFSF (lua_State *L, void *ud, size_t *size) {
|
|||
return "\n";
|
||||
}
|
||||
|
||||
if (fs_eof(lf->f)) return NULL;
|
||||
*size = fs_read(lf->f, lf->buff, sizeof(lf->buff));
|
||||
if (vfs_eof(lf->f)) return NULL;
|
||||
*size = vfs_read(lf->f, lf->buff, sizeof(lf->buff));
|
||||
|
||||
return (*size > 0) ? lf->buff : NULL;
|
||||
}
|
||||
|
@ -697,29 +697,29 @@ LUALIB_API int luaL_loadfsfile (lua_State *L, const char *filename) {
|
|||
}
|
||||
else {
|
||||
lua_pushfstring(L, "@%s", filename);
|
||||
lf.f = fs_open(filename, FS_RDONLY);
|
||||
if (lf.f < FS_OPEN_OK) return errfsfile(L, "open", fnameindex);
|
||||
lf.f = vfs_open(filename, "r");
|
||||
if (!lf.f) return errfsfile(L, "open", fnameindex);
|
||||
}
|
||||
// if(fs_size(lf.f)>LUAL_BUFFERSIZE)
|
||||
// return luaL_error(L, "file is too big");
|
||||
c = fs_getc(lf.f);
|
||||
c = vfs_getc(lf.f);
|
||||
if (c == '#') { /* Unix exec. file? */
|
||||
lf.extraline = 1;
|
||||
while ((c = fs_getc(lf.f)) != EOF && c != '\n') ; /* skip first line */
|
||||
if (c == '\n') c = fs_getc(lf.f);
|
||||
while ((c = vfs_getc(lf.f)) != VFS_EOF && c != '\n') ; /* skip first line */
|
||||
if (c == '\n') c = vfs_getc(lf.f);
|
||||
}
|
||||
if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */
|
||||
fs_close(lf.f);
|
||||
lf.f = fs_open(filename, FS_RDONLY); /* reopen in binary mode */
|
||||
if (lf.f < FS_OPEN_OK) return errfsfile(L, "reopen", fnameindex);
|
||||
vfs_close(lf.f);
|
||||
lf.f = vfs_open(filename, "r"); /* reopen in binary mode */
|
||||
if (!lf.f) return errfsfile(L, "reopen", fnameindex);
|
||||
/* skip eventual `#!...' */
|
||||
while ((c = fs_getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ;
|
||||
while ((c = vfs_getc(lf.f)) != VFS_EOF && c != LUA_SIGNATURE[0]) ;
|
||||
lf.extraline = 0;
|
||||
}
|
||||
fs_ungetc(c, lf.f);
|
||||
vfs_ungetc(c, lf.f);
|
||||
status = lua_load(L, getFSF, &lf, lua_tostring(L, -1));
|
||||
|
||||
if (filename) fs_close(lf.f); /* close file (even in case of errors) */
|
||||
if (filename) vfs_close(lf.f); /* close file (even in case of errors) */
|
||||
lua_remove(L, fnameindex);
|
||||
return status;
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
#include "c_stdio.h"
|
||||
#include "c_stdlib.h"
|
||||
#include "c_string.h"
|
||||
#include "flash_fs.h"
|
||||
#include "vfs.h"
|
||||
|
||||
#define liolib_c
|
||||
#define LUA_LIB
|
||||
|
@ -39,7 +39,7 @@ static const int liolib_keys[] = {(int)&luaL_callmeta, (int)&luaL_typerror, (int
|
|||
static const char *const fnames[] = {"input", "output"};
|
||||
|
||||
static int pushresult (lua_State *L, int i, const char *filename) {
|
||||
int en = fs_error(0); /* calls to Lua API may change this value */
|
||||
int en = vfs_ferrno(0); /* calls to Lua API may change this value */
|
||||
if (i) {
|
||||
lua_pushboolean(L, 1);
|
||||
return 1;
|
||||
|
@ -57,7 +57,7 @@ static int pushresult (lua_State *L, int i, const char *filename) {
|
|||
|
||||
|
||||
static void fileerror (lua_State *L, int arg, const char *filename) {
|
||||
lua_pushfstring(L, "%s: err(%d)", filename, fs_error(0));
|
||||
lua_pushfstring(L, "%s: err(%d)", filename, vfs_ferrno(0));
|
||||
luaL_argerror(L, arg, lua_tostring(L, -1));
|
||||
}
|
||||
|
||||
|
@ -130,7 +130,7 @@ static int io_pclose (lua_State *L) {
|
|||
*/
|
||||
static int io_fclose (lua_State *L) {
|
||||
int *p = tofilep(L);
|
||||
int ok = (fs_close(*p) == 0);
|
||||
int ok = (vfs_close(*p) == 0);
|
||||
*p = FS_OPEN_OK - 1;
|
||||
return pushresult(L, ok, NULL);
|
||||
}
|
||||
|
@ -149,7 +149,7 @@ static int aux_close (lua_State *L) {
|
|||
lua_pushliteral(L, "cannot close standard file");
|
||||
return 2;
|
||||
}
|
||||
int ok = (fs_close(*p) == 0);
|
||||
int ok = (vfs_close(*p) == 0);
|
||||
*p = FS_OPEN_OK - 1;
|
||||
return pushresult(L, ok, NULL);
|
||||
#endif
|
||||
|
@ -187,7 +187,7 @@ static int io_open (lua_State *L) {
|
|||
const char *filename = luaL_checkstring(L, 1);
|
||||
const char *mode = luaL_optstring(L, 2, "r");
|
||||
int *pf = newfile(L);
|
||||
*pf = fs_open(filename, fs_mode2flag(mode));
|
||||
*pf = vfs_open(filename, mode);
|
||||
return (*pf == FS_OPEN_OK - 1) ? pushresult(L, 0, filename) : 1;
|
||||
}
|
||||
|
||||
|
@ -201,7 +201,7 @@ static int io_popen (lua_State *L) {
|
|||
const char *filename = luaL_checkstring(L, 1);
|
||||
const char *mode = luaL_optstring(L, 2, "r");
|
||||
int *pf = newfile(L);
|
||||
*pf = lua_popen(L, filename, fs_mode2flag(mode));
|
||||
*pf = lua_popen(L, filename, fs_mode2flags(mode));
|
||||
return (*pf == FS_OPEN_OK - 1) ? pushresult(L, 0, filename) : 1;
|
||||
}
|
||||
|
||||
|
@ -230,7 +230,7 @@ static int g_iofile (lua_State *L, int f, const char *mode) {
|
|||
const char *filename = lua_tostring(L, 1);
|
||||
if (filename) {
|
||||
int *pf = newfile(L);
|
||||
*pf = fs_open(filename, fs_mode2flag(mode));
|
||||
*pf = vfs_open(filename, mode);
|
||||
if (*pf == FS_OPEN_OK - 1)
|
||||
fileerror(L, 1, filename);
|
||||
}
|
||||
|
@ -282,7 +282,7 @@ static int io_lines (lua_State *L) {
|
|||
else {
|
||||
const char *filename = luaL_checkstring(L, 1);
|
||||
int *pf = newfile(L);
|
||||
*pf = fs_open(filename, FS_RDONLY);
|
||||
*pf = vfs_open(filename, "r");
|
||||
if (*pf == FS_OPEN_OK - 1)
|
||||
fileerror(L, 1, filename);
|
||||
aux_lines(L, lua_gettop(L), 1);
|
||||
|
@ -312,8 +312,8 @@ static int read_number (lua_State *L, int f) {
|
|||
#endif
|
||||
|
||||
static int test_eof (lua_State *L, int f) {
|
||||
int c = fs_getc(f);
|
||||
fs_ungetc(c, f);
|
||||
int c = vfs_getc(f);
|
||||
vfs_ungetc(c, f);
|
||||
lua_pushlstring(L, NULL, 0);
|
||||
return (c != EOF);
|
||||
}
|
||||
|
@ -347,7 +347,7 @@ static int read_line (lua_State *L, int f) {
|
|||
signed char c = EOF;
|
||||
int i = 0;
|
||||
do{
|
||||
c = (signed char)fs_getc(f);
|
||||
c = (signed char)vfs_getc(f);
|
||||
if(c==EOF){
|
||||
break;
|
||||
}
|
||||
|
@ -377,7 +377,7 @@ static int read_chars (lua_State *L, int f, size_t n) {
|
|||
do {
|
||||
char *p = luaL_prepbuffer(&b);
|
||||
if (rlen > n) rlen = n; /* cannot read more than asked */
|
||||
nr = fs_read(f, p, rlen);
|
||||
nr = vfs_read(f, p, rlen);
|
||||
luaL_addsize(&b, nr);
|
||||
n -= nr; /* still have to read `n' chars */
|
||||
} while (n > 0 && nr == rlen); /* until end of count or eof */
|
||||
|
@ -390,7 +390,7 @@ static int g_read (lua_State *L, int f, int first) {
|
|||
int nargs = lua_gettop(L) - 1;
|
||||
int success;
|
||||
int n;
|
||||
fs_clearerr(f);
|
||||
//vfs_clearerr(f);
|
||||
if (nargs == 0) { /* no arguments? */
|
||||
success = read_line(L, f);
|
||||
n = first+1; /* to return 1 result */
|
||||
|
@ -425,7 +425,7 @@ static int g_read (lua_State *L, int f, int first) {
|
|||
}
|
||||
}
|
||||
}
|
||||
if (fs_error(f))
|
||||
if (vfs_ferrno(f))
|
||||
return pushresult(L, 0, NULL);
|
||||
if (!success) {
|
||||
lua_pop(L, 1); /* remove last result */
|
||||
|
@ -453,8 +453,8 @@ static int io_readline (lua_State *L) {
|
|||
return 0;
|
||||
}
|
||||
sucess = read_line(L, *pf);
|
||||
if (fs_error(*pf))
|
||||
return luaL_error(L, "err(%d)", fs_error(*pf));
|
||||
if (vfs_ferrno(*pf))
|
||||
return luaL_error(L, "err(%d)", vfs_ferrno(*pf));
|
||||
if (sucess) return 1;
|
||||
else { /* EOF */
|
||||
if (lua_toboolean(L, lua_upvalueindex(2))) { /* generator created file? */
|
||||
|
@ -484,7 +484,7 @@ static int g_write (lua_State *L, int f, int arg) {
|
|||
{
|
||||
size_t l;
|
||||
const char *s = luaL_checklstring(L, arg, &l);
|
||||
status = status && (fs_write(f, s, l) == l);
|
||||
status = status && (vfs_write(f, s, l) == l);
|
||||
}
|
||||
}
|
||||
return pushresult(L, status, NULL);
|
||||
|
@ -502,16 +502,16 @@ static int f_write (lua_State *L) {
|
|||
|
||||
|
||||
static int f_seek (lua_State *L) {
|
||||
static const int mode[] = {FS_SEEK_SET, FS_SEEK_CUR, FS_SEEK_END};
|
||||
static const int mode[] = {VFS_SEEK_SET, VFS_SEEK_CUR, VFS_SEEK_END};
|
||||
static const char *const modenames[] = {"set", "cur", "end", NULL};
|
||||
int f = tofile(L);
|
||||
int op = luaL_checkoption(L, 2, "cur", modenames);
|
||||
long offset = luaL_optlong(L, 3, 0);
|
||||
op = fs_seek(f, offset, mode[op]);
|
||||
op = vfs_lseek(f, offset, mode[op]);
|
||||
if (op)
|
||||
return pushresult(L, 0, NULL); /* error */
|
||||
else {
|
||||
lua_pushinteger(L, fs_tell(f));
|
||||
lua_pushinteger(L, vfs_tell(f));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
@ -530,12 +530,12 @@ static int f_setvbuf (lua_State *L) {
|
|||
|
||||
|
||||
static int io_flush (lua_State *L) {
|
||||
return pushresult(L, fs_flush(getiofile(L, IO_OUTPUT)) == 0, NULL);
|
||||
return pushresult(L, vfs_flush(getiofile(L, IO_OUTPUT)) == 0, NULL);
|
||||
}
|
||||
|
||||
|
||||
static int f_flush (lua_State *L) {
|
||||
return pushresult(L, fs_flush(tofile(L)) == 0, NULL);
|
||||
return pushresult(L, vfs_flush(tofile(L)) == 0, NULL);
|
||||
}
|
||||
|
||||
#undef MIN_OPT_LEVEL
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
#include C_HEADER_FCNTL
|
||||
|
||||
#ifndef LUA_CROSS_COMPILER
|
||||
#include "flash_fs.h"
|
||||
#include "vfs.h"
|
||||
#endif
|
||||
|
||||
#include "lauxlib.h"
|
||||
|
@ -340,9 +340,9 @@ static int readable (const char *filename) {
|
|||
}
|
||||
#else
|
||||
static int readable (const char *filename) {
|
||||
int f = fs_open(filename, FS_RDONLY); /* try to open file */
|
||||
if (f < FS_OPEN_OK) return 0; /* open failed */
|
||||
fs_close(f);
|
||||
int f = vfs_open(filename, "r"); /* try to open file */
|
||||
if (!f) return 0; /* open failed */
|
||||
vfs_close(f);
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
#include "c_stdio.h"
|
||||
#include "c_stdlib.h"
|
||||
#include "c_string.h"
|
||||
#include "flash_fs.h"
|
||||
#include "user_interface.h"
|
||||
#include "user_version.h"
|
||||
#include "driver/readline.h"
|
||||
#include "driver/uart.h"
|
||||
|
|
|
@ -52,6 +52,7 @@ INCLUDES += -I ../spiffs
|
|||
INCLUDES += -I ../smart
|
||||
INCLUDES += -I ../cjson
|
||||
INCLUDES += -I ../dhtlib
|
||||
INCLUDES += -I ../fatfs
|
||||
INCLUDES += -I ../http
|
||||
INCLUDES += -I ../websocket
|
||||
PDIR := ../$(PDIR)
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
#include "platform.h"
|
||||
#include "c_types.h"
|
||||
#include "c_stdlib.h"
|
||||
#include "flash_fs.h"
|
||||
#include "vfs.h"
|
||||
#include "../crypto/digests.h"
|
||||
#include "../crypto/mech.h"
|
||||
|
||||
|
@ -243,6 +243,11 @@ static int crypto_hash_gcdelete (lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static sint32_t vfs_read_wrap (int fd, void *ptr, size_t len)
|
||||
{
|
||||
return vfs_read (fd, ptr, len);
|
||||
}
|
||||
|
||||
/* rawdigest = crypto.hash("MD5", filename)
|
||||
* strdigest = crypto.toHex(rawdigest)
|
||||
*/
|
||||
|
@ -254,17 +259,17 @@ static int crypto_flhash (lua_State *L)
|
|||
const char *filename = luaL_checkstring (L, 2);
|
||||
|
||||
// Open the file
|
||||
int file_fd = fs_open (filename, FS_RDONLY);
|
||||
if(file_fd < FS_OPEN_OK) {
|
||||
int file_fd = vfs_open (filename, "r");
|
||||
if(!file_fd) {
|
||||
return bad_file(L);
|
||||
}
|
||||
|
||||
// Compute hash
|
||||
uint8_t digest[mi->digest_size];
|
||||
int returncode = crypto_fhash (mi, &fs_read, file_fd, digest);
|
||||
int returncode = crypto_fhash (mi, &vfs_read_wrap, file_fd, digest);
|
||||
|
||||
// Finish up
|
||||
fs_close(file_fd);
|
||||
vfs_close(file_fd);
|
||||
|
||||
if (returncode == ENOMEM)
|
||||
return bad_mem (L);
|
||||
|
|
|
@ -45,7 +45,7 @@
|
|||
#include "espconn.h"
|
||||
#include "lwip/tcp.h"
|
||||
#include "lwip/pbuf.h"
|
||||
#include "flash_fs.h"
|
||||
#include "vfs.h"
|
||||
#include "task/task.h"
|
||||
|
||||
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
|
||||
|
@ -485,20 +485,20 @@ static int enduser_setup_http_load_payload(void)
|
|||
{
|
||||
ENDUSER_SETUP_DEBUG("enduser_setup_http_load_payload");
|
||||
|
||||
int err = (FS_OPEN_OK -1);
|
||||
int err2 = (FS_OPEN_OK -1);
|
||||
int err = VFS_RES_ERR;
|
||||
int err2 = VFS_RES_ERR;
|
||||
int file_len = 0;
|
||||
int f = fs_open(http_html_filename, fs_mode2flag("r"));
|
||||
if (f >= FS_OPEN_OK) {
|
||||
err = fs_seek(f, 0, FS_SEEK_END);
|
||||
file_len = (int) fs_tell(f);
|
||||
err2 = fs_seek(f, 0, FS_SEEK_SET);
|
||||
int f = vfs_open(http_html_filename, "r");
|
||||
if (f) {
|
||||
err = vfs_lseek(f, 0, VFS_SEEK_END);
|
||||
file_len = (int) vfs_tell(f);
|
||||
err2 = vfs_lseek(f, 0, VFS_SEEK_SET);
|
||||
}
|
||||
|
||||
const char cl_hdr[] = "Content-length:%5d\r\n\r\n";
|
||||
const size_t cl_len = LITLEN(cl_hdr) + 3; /* room to expand %4d */
|
||||
|
||||
if (f < FS_OPEN_OK || err < FS_OPEN_OK || err2 < FS_OPEN_OK)
|
||||
if (!f || err != VFS_RES_OK || err2 != VFS_RES_OK)
|
||||
{
|
||||
ENDUSER_SETUP_DEBUG("enduser_setup_http_load_payload unable to load file enduser_setup.html, loading backup HTML.");
|
||||
|
||||
|
@ -531,8 +531,8 @@ static int enduser_setup_http_load_payload(void)
|
|||
c_memcpy(&(state->http_payload_data[offset]), &(http_header_200), LITLEN(http_header_200));
|
||||
offset += LITLEN(http_header_200);
|
||||
offset += c_sprintf(state->http_payload_data + offset, cl_hdr, file_len);
|
||||
fs_read(f, &(state->http_payload_data[offset]), file_len);
|
||||
fs_close(f);
|
||||
vfs_read(f, &(state->http_payload_data[offset]), file_len);
|
||||
vfs_close(f);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -5,42 +5,95 @@
|
|||
#include "platform.h"
|
||||
|
||||
#include "c_types.h"
|
||||
#include "flash_fs.h"
|
||||
#include "vfs.h"
|
||||
#include "c_string.h"
|
||||
|
||||
static volatile int file_fd = FS_OPEN_OK - 1;
|
||||
static int file_fd = 0;
|
||||
static int rtc_cb_ref = LUA_NOREF;
|
||||
|
||||
// Lua: open(filename, mode)
|
||||
static int file_open( lua_State* L )
|
||||
|
||||
static void table2tm( lua_State *L, vfs_time *tm )
|
||||
{
|
||||
size_t len;
|
||||
if((FS_OPEN_OK - 1)!=file_fd){
|
||||
fs_close(file_fd);
|
||||
file_fd = FS_OPEN_OK - 1;
|
||||
int idx = lua_gettop( L );
|
||||
|
||||
// extract items from table
|
||||
lua_getfield( L, idx, "year" );
|
||||
lua_getfield( L, idx, "mon" );
|
||||
lua_getfield( L, idx, "day" );
|
||||
lua_getfield( L, idx, "hour" );
|
||||
lua_getfield( L, idx, "min" );
|
||||
lua_getfield( L, idx, "sec" );
|
||||
|
||||
tm->year = luaL_optint( L, ++idx, 2016 );
|
||||
tm->mon = luaL_optint( L, ++idx, 6 );
|
||||
tm->day = luaL_optint( L, ++idx, 21 );
|
||||
tm->hour = luaL_optint( L, ++idx, 0 );
|
||||
tm->min = luaL_optint( L, ++idx, 0 );
|
||||
tm->sec = luaL_optint( L, ++idx, 0 );
|
||||
|
||||
// remove items from stack
|
||||
lua_pop( L, 6 );
|
||||
}
|
||||
|
||||
static sint32_t file_rtc_cb( vfs_time *tm )
|
||||
{
|
||||
sint32_t res = VFS_RES_ERR;
|
||||
|
||||
if (rtc_cb_ref != LUA_NOREF) {
|
||||
lua_State *L = lua_getstate();
|
||||
|
||||
lua_rawgeti( L, LUA_REGISTRYINDEX, rtc_cb_ref );
|
||||
lua_call( L, 0, 1 );
|
||||
|
||||
if (lua_type( L, lua_gettop( L ) ) == LUA_TTABLE) {
|
||||
table2tm( L, tm );
|
||||
res = VFS_RES_OK;
|
||||
}
|
||||
|
||||
// pop item returned by callback
|
||||
lua_pop( L, 1 );
|
||||
}
|
||||
|
||||
const char *fname = luaL_checklstring( L, 1, &len );
|
||||
luaL_argcheck(L, len < FS_NAME_MAX_LENGTH && c_strlen(fname) == len, 1, "filename invalid");
|
||||
return res;
|
||||
}
|
||||
|
||||
const char *mode = luaL_optstring(L, 2, "r");
|
||||
// Lua: on()
|
||||
static int file_on(lua_State *L)
|
||||
{
|
||||
enum events{
|
||||
ON_RTC = 0
|
||||
};
|
||||
const char *const eventnames[] = {"rtc", NULL};
|
||||
|
||||
file_fd = fs_open(fname, fs_mode2flag(mode));
|
||||
int event = luaL_checkoption(L, 1, "rtc", eventnames);
|
||||
|
||||
if(file_fd < FS_OPEN_OK){
|
||||
file_fd = FS_OPEN_OK - 1;
|
||||
lua_pushnil(L);
|
||||
} else {
|
||||
lua_pushboolean(L, 1);
|
||||
switch (event) {
|
||||
case ON_RTC:
|
||||
luaL_unref(L, LUA_REGISTRYINDEX, rtc_cb_ref);
|
||||
|
||||
if ((lua_type(L, 2) == LUA_TFUNCTION) ||
|
||||
(lua_type(L, 2) == LUA_TLIGHTFUNCTION)) {
|
||||
lua_pushvalue(L, 2); // copy argument (func) to the top of stack
|
||||
rtc_cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
||||
vfs_register_rtc_cb(file_rtc_cb);
|
||||
} else {
|
||||
rtc_cb_ref = LUA_NOREF;
|
||||
vfs_register_rtc_cb(NULL);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Lua: close()
|
||||
static int file_close( lua_State* L )
|
||||
{
|
||||
if((FS_OPEN_OK - 1)!=file_fd){
|
||||
fs_close(file_fd);
|
||||
file_fd = FS_OPEN_OK - 1;
|
||||
if(file_fd){
|
||||
vfs_close(file_fd);
|
||||
file_fd = 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
@ -50,7 +103,7 @@ static int file_format( lua_State* L )
|
|||
{
|
||||
size_t len;
|
||||
file_close(L);
|
||||
if( !fs_format() )
|
||||
if( !vfs_format() )
|
||||
{
|
||||
NODE_ERR( "\n*** ERROR ***: unable to format. FS might be compromised.\n" );
|
||||
NODE_ERR( "It is advised to re-flash the NodeMCU image.\n" );
|
||||
|
@ -59,44 +112,77 @@ static int file_format( lua_State* L )
|
|||
else{
|
||||
NODE_ERR( "format done.\n" );
|
||||
}
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if defined(BUILD_SPIFFS)
|
||||
static int file_fscfg (lua_State *L)
|
||||
{
|
||||
uint32_t phys_addr, phys_size;
|
||||
|
||||
extern spiffs fs;
|
||||
vfs_fscfg("/FLASH", &phys_addr, &phys_size);
|
||||
|
||||
lua_pushinteger (L, phys_addr);
|
||||
lua_pushinteger (L, phys_size);
|
||||
return 2;
|
||||
}
|
||||
|
||||
// Lua: open(filename, mode)
|
||||
static int file_open( lua_State* L )
|
||||
{
|
||||
size_t len;
|
||||
if(file_fd){
|
||||
vfs_close(file_fd);
|
||||
file_fd = 0;
|
||||
}
|
||||
|
||||
const char *fname = luaL_checklstring( L, 1, &len );
|
||||
const char *basename = vfs_basename( fname );
|
||||
luaL_argcheck(L, c_strlen(basename) <= FS_OBJ_NAME_LEN && c_strlen(fname) == len, 1, "filename invalid");
|
||||
|
||||
const char *mode = luaL_optstring(L, 2, "r");
|
||||
|
||||
file_fd = vfs_open(fname, mode);
|
||||
|
||||
if(!file_fd){
|
||||
lua_pushnil(L);
|
||||
} else {
|
||||
lua_pushboolean(L, 1);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Lua: list()
|
||||
static int file_list( lua_State* L )
|
||||
{
|
||||
spiffs_DIR d;
|
||||
struct spiffs_dirent e;
|
||||
struct spiffs_dirent *pe = &e;
|
||||
vfs_dir *dir;
|
||||
vfs_item *item;
|
||||
|
||||
lua_newtable( L );
|
||||
SPIFFS_opendir(&fs, "/", &d);
|
||||
while ((pe = SPIFFS_readdir(&d, pe))) {
|
||||
// NODE_ERR(" %s size:%i\n", pe->name, pe->size);
|
||||
lua_pushinteger(L, pe->size);
|
||||
lua_setfield( L, -2, pe->name );
|
||||
if (dir = vfs_opendir("")) {
|
||||
lua_newtable( L );
|
||||
while (item = vfs_readdir(dir)) {
|
||||
lua_pushinteger(L, vfs_item_size(item));
|
||||
lua_setfield(L, -2, vfs_item_name(item));
|
||||
vfs_closeitem(item);
|
||||
}
|
||||
vfs_closedir(dir);
|
||||
return 1;
|
||||
}
|
||||
SPIFFS_closedir(&d);
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int file_seek (lua_State *L)
|
||||
{
|
||||
static const int mode[] = {FS_SEEK_SET, FS_SEEK_CUR, FS_SEEK_END};
|
||||
static const int mode[] = {VFS_SEEK_SET, VFS_SEEK_CUR, VFS_SEEK_END};
|
||||
static const char *const modenames[] = {"set", "cur", "end", NULL};
|
||||
if((FS_OPEN_OK - 1)==file_fd)
|
||||
if(!file_fd)
|
||||
return luaL_error(L, "open a file first");
|
||||
int op = luaL_checkoption(L, 1, "cur", modenames);
|
||||
long offset = luaL_optlong(L, 2, 0);
|
||||
op = fs_seek(file_fd, offset, mode[op]);
|
||||
op = vfs_lseek(file_fd, offset, mode[op]);
|
||||
if (op < 0)
|
||||
lua_pushnil(L); /* error */
|
||||
else
|
||||
lua_pushinteger(L, fs_tell(file_fd));
|
||||
lua_pushinteger(L, vfs_tell(file_fd));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -105,12 +191,14 @@ static int file_exists( lua_State* L )
|
|||
{
|
||||
size_t len;
|
||||
const char *fname = luaL_checklstring( L, 1, &len );
|
||||
luaL_argcheck(L, len < FS_NAME_MAX_LENGTH && c_strlen(fname) == len, 1, "filename invalid");
|
||||
const char *basename = vfs_basename( fname );
|
||||
luaL_argcheck(L, c_strlen(basename) <= FS_OBJ_NAME_LEN && c_strlen(fname) == len, 1, "filename invalid");
|
||||
|
||||
spiffs_stat stat;
|
||||
int rc = SPIFFS_stat(&fs, (char *)fname, &stat);
|
||||
vfs_item *stat = vfs_stat((char *)fname);
|
||||
|
||||
lua_pushboolean(L, (rc == SPIFFS_OK ? 1 : 0));
|
||||
lua_pushboolean(L, stat ? 1 : 0);
|
||||
|
||||
if (stat) vfs_closeitem(stat);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
@ -120,49 +208,43 @@ static int file_remove( lua_State* L )
|
|||
{
|
||||
size_t len;
|
||||
const char *fname = luaL_checklstring( L, 1, &len );
|
||||
luaL_argcheck(L, len < FS_NAME_MAX_LENGTH && c_strlen(fname) == len, 1, "filename invalid");
|
||||
const char *basename = vfs_basename( fname );
|
||||
luaL_argcheck(L, c_strlen(basename) <= FS_OBJ_NAME_LEN && c_strlen(fname) == len, 1, "filename invalid");
|
||||
file_close(L);
|
||||
SPIFFS_remove(&fs, (char *)fname);
|
||||
return 0;
|
||||
vfs_remove((char *)fname);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Lua: flush()
|
||||
static int file_flush( lua_State* L )
|
||||
{
|
||||
if((FS_OPEN_OK - 1)==file_fd)
|
||||
if(!file_fd)
|
||||
return luaL_error(L, "open a file first");
|
||||
if(fs_flush(file_fd) == 0)
|
||||
if(vfs_flush(file_fd) == 0)
|
||||
lua_pushboolean(L, 1);
|
||||
else
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
}
|
||||
#if 0
|
||||
// Lua: check()
|
||||
static int file_check( lua_State* L )
|
||||
{
|
||||
file_close(L);
|
||||
lua_pushinteger(L, fs_check());
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Lua: rename("oldname", "newname")
|
||||
static int file_rename( lua_State* L )
|
||||
{
|
||||
size_t len;
|
||||
if((FS_OPEN_OK - 1)!=file_fd){
|
||||
fs_close(file_fd);
|
||||
file_fd = FS_OPEN_OK - 1;
|
||||
if(file_fd){
|
||||
vfs_close(file_fd);
|
||||
file_fd = 0;
|
||||
}
|
||||
|
||||
const char *oldname = luaL_checklstring( L, 1, &len );
|
||||
luaL_argcheck(L, len < FS_NAME_MAX_LENGTH && c_strlen(oldname) == len, 1, "filename invalid");
|
||||
const char *basename = vfs_basename( oldname );
|
||||
luaL_argcheck(L, c_strlen(basename) <= FS_OBJ_NAME_LEN && c_strlen(oldname) == len, 1, "filename invalid");
|
||||
|
||||
const char *newname = luaL_checklstring( L, 2, &len );
|
||||
luaL_argcheck(L, len < FS_NAME_MAX_LENGTH && c_strlen(newname) == len, 2, "filename invalid");
|
||||
basename = vfs_basename( newname );
|
||||
luaL_argcheck(L, c_strlen(basename) <= FS_OBJ_NAME_LEN && c_strlen(newname) == len, 2, "filename invalid");
|
||||
|
||||
if(SPIFFS_OK==myspiffs_rename( oldname, newname )){
|
||||
if(0 <= vfs_rename( oldname, newname )){
|
||||
lua_pushboolean(L, 1);
|
||||
} else {
|
||||
lua_pushboolean(L, 0);
|
||||
|
@ -170,26 +252,6 @@ static int file_rename( lua_State* L )
|
|||
return 1;
|
||||
}
|
||||
|
||||
// Lua: fsinfo()
|
||||
static int file_fsinfo( lua_State* L )
|
||||
{
|
||||
u32_t total, used;
|
||||
if (SPIFFS_info(&fs, &total, &used)) {
|
||||
return luaL_error(L, "file system failed");
|
||||
}
|
||||
NODE_DBG("total: %d, used:%d\n", total, used);
|
||||
if(total>0x7FFFFFFF || used>0x7FFFFFFF || used > total)
|
||||
{
|
||||
return luaL_error(L, "file system error");
|
||||
}
|
||||
lua_pushinteger(L, total-used);
|
||||
lua_pushinteger(L, used);
|
||||
lua_pushinteger(L, total);
|
||||
return 3;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
// g_read()
|
||||
static int file_g_read( lua_State* L, int n, int16_t end_char )
|
||||
{
|
||||
|
@ -199,14 +261,14 @@ static int file_g_read( lua_State* L, int n, int16_t end_char )
|
|||
end_char = EOF;
|
||||
|
||||
luaL_Buffer b;
|
||||
if((FS_OPEN_OK - 1)==file_fd)
|
||||
if(!file_fd)
|
||||
return luaL_error(L, "open a file first");
|
||||
|
||||
luaL_buffinit(L, &b);
|
||||
char *p = luaL_prepbuffer(&b);
|
||||
int i;
|
||||
|
||||
n = fs_read(file_fd, p, n);
|
||||
n = vfs_read(file_fd, p, n);
|
||||
for (i = 0; i < n; ++i)
|
||||
if (p[i] == end_char)
|
||||
{
|
||||
|
@ -219,7 +281,7 @@ static int file_g_read( lua_State* L, int n, int16_t end_char )
|
|||
return (lua_objlen(L, -1) > 0); /* check whether read something */
|
||||
}
|
||||
|
||||
fs_seek(file_fd, -(n - i), SEEK_CUR);
|
||||
vfs_lseek(file_fd, -(n - i), VFS_SEEK_CUR);
|
||||
luaL_addsize(&b, i);
|
||||
luaL_pushresult(&b); /* close buffer */
|
||||
return 1; /* read at least an `eol' */
|
||||
|
@ -262,11 +324,11 @@ static int file_readline( lua_State* L )
|
|||
// Lua: write("string")
|
||||
static int file_write( lua_State* L )
|
||||
{
|
||||
if((FS_OPEN_OK - 1)==file_fd)
|
||||
if(!file_fd)
|
||||
return luaL_error(L, "open a file first");
|
||||
size_t l, rl;
|
||||
const char *s = luaL_checklstring(L, 1, &l);
|
||||
rl = fs_write(file_fd, s, l);
|
||||
rl = vfs_write(file_fd, s, l);
|
||||
if(rl==l)
|
||||
lua_pushboolean(L, 1);
|
||||
else
|
||||
|
@ -277,13 +339,13 @@ static int file_write( lua_State* L )
|
|||
// Lua: writeline("string")
|
||||
static int file_writeline( lua_State* L )
|
||||
{
|
||||
if((FS_OPEN_OK - 1)==file_fd)
|
||||
if(!file_fd)
|
||||
return luaL_error(L, "open a file first");
|
||||
size_t l, rl;
|
||||
const char *s = luaL_checklstring(L, 1, &l);
|
||||
rl = fs_write(file_fd, s, l);
|
||||
rl = vfs_write(file_fd, s, l);
|
||||
if(rl==l){
|
||||
rl = fs_write(file_fd, "\n", 1);
|
||||
rl = vfs_write(file_fd, "\n", 1);
|
||||
if(rl==1)
|
||||
lua_pushboolean(L, 1);
|
||||
else
|
||||
|
@ -295,13 +357,79 @@ static int file_writeline( lua_State* L )
|
|||
return 1;
|
||||
}
|
||||
|
||||
static int file_fscfg (lua_State *L)
|
||||
// Lua: fsinfo()
|
||||
static int file_fsinfo( lua_State* L )
|
||||
{
|
||||
lua_pushinteger (L, fs.cfg.phys_addr);
|
||||
lua_pushinteger (L, fs.cfg.phys_size);
|
||||
return 2;
|
||||
u32_t total, used;
|
||||
if (vfs_fsinfo("", &total, &used)) {
|
||||
return luaL_error(L, "file system failed");
|
||||
}
|
||||
NODE_DBG("total: %d, used:%d\n", total, used);
|
||||
if(total>0x7FFFFFFF || used>0x7FFFFFFF || used > total)
|
||||
{
|
||||
return luaL_error(L, "file system error");
|
||||
}
|
||||
lua_pushinteger(L, total-used);
|
||||
lua_pushinteger(L, used);
|
||||
lua_pushinteger(L, total);
|
||||
return 3;
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
vfs_vol *vol;
|
||||
} volume_type;
|
||||
|
||||
// Lua: vol = file.mount("/SD0")
|
||||
static int file_mount( lua_State *L )
|
||||
{
|
||||
const char *ldrv = luaL_checkstring( L, 1 );
|
||||
int num = luaL_optint( L, 2, -1 );
|
||||
volume_type *vol = (volume_type *)lua_newuserdata( L, sizeof( volume_type ) );
|
||||
|
||||
if (vol->vol = vfs_mount( ldrv, num )) {
|
||||
/* set its metatable */
|
||||
luaL_getmetatable(L, "vfs.vol");
|
||||
lua_setmetatable(L, -2);
|
||||
return 1;
|
||||
} else {
|
||||
// remove created userdata
|
||||
lua_pop( L, 1 );
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Lua: success = file.chdir("/SD0/")
|
||||
static int file_chdir( lua_State *L )
|
||||
{
|
||||
const char *path = luaL_checkstring( L, 1 );
|
||||
|
||||
lua_pushboolean( L, 0 <= vfs_chdir( path ) );
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int file_vol_umount( lua_State *L )
|
||||
{
|
||||
volume_type *vol = luaL_checkudata( L, 1, "file.vol" );
|
||||
luaL_argcheck( L, vol, 1, "volume expected" );
|
||||
|
||||
lua_pushboolean( L, 0 <= vfs_umount( vol->vol ) );
|
||||
|
||||
// invalidate vfs descriptor, it has been free'd anyway
|
||||
vol->vol = NULL;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static const LUA_REG_TYPE file_vol_map[] =
|
||||
{
|
||||
{ LSTRKEY( "umount" ), LFUNCVAL( file_vol_umount )},
|
||||
//{ LSTRKEY( "getfree" ), LFUNCVAL( file_vol_getfree )},
|
||||
//{ LSTRKEY( "getlabel" ), LFUNCVAL( file_vol_getlabel )},
|
||||
//{ LSTRKEY( "__gc" ), LFUNCVAL( file_vol_free ) },
|
||||
{ LSTRKEY( "__index" ), LROVAL( file_vol_map ) },
|
||||
{ LNILKEY, LNILVAL }
|
||||
};
|
||||
|
||||
// Module function map
|
||||
static const LUA_REG_TYPE file_map[] = {
|
||||
{ LSTRKEY( "list" ), LFUNCVAL( file_list ) },
|
||||
|
@ -311,17 +439,27 @@ static const LUA_REG_TYPE file_map[] = {
|
|||
{ LSTRKEY( "writeline" ), LFUNCVAL( file_writeline ) },
|
||||
{ LSTRKEY( "read" ), LFUNCVAL( file_read ) },
|
||||
{ LSTRKEY( "readline" ), LFUNCVAL( file_readline ) },
|
||||
#ifdef BUILD_SPIFFS
|
||||
{ LSTRKEY( "format" ), LFUNCVAL( file_format ) },
|
||||
#if defined(BUILD_SPIFFS)
|
||||
{ LSTRKEY( "fscfg" ), LFUNCVAL( file_fscfg ) },
|
||||
#endif
|
||||
{ LSTRKEY( "remove" ), LFUNCVAL( file_remove ) },
|
||||
{ LSTRKEY( "seek" ), LFUNCVAL( file_seek ) },
|
||||
{ LSTRKEY( "flush" ), LFUNCVAL( file_flush ) },
|
||||
{ LSTRKEY( "rename" ), LFUNCVAL( file_rename ) },
|
||||
{ LSTRKEY( "fsinfo" ), LFUNCVAL( file_fsinfo ) },
|
||||
{ LSTRKEY( "fscfg" ), LFUNCVAL( file_fscfg ) },
|
||||
{ LSTRKEY( "exists" ), LFUNCVAL( file_exists ) },
|
||||
{ LSTRKEY( "fsinfo" ), LFUNCVAL( file_fsinfo ) },
|
||||
{ LSTRKEY( "on" ), LFUNCVAL( file_on ) },
|
||||
#ifdef BUILD_FATFS
|
||||
{ LSTRKEY( "mount" ), LFUNCVAL( file_mount ) },
|
||||
{ LSTRKEY( "chdir" ), LFUNCVAL( file_chdir ) },
|
||||
#endif
|
||||
{ LNILKEY, LNILVAL }
|
||||
};
|
||||
|
||||
NODEMCU_MODULE(FILE, "file", file_map, NULL);
|
||||
int luaopen_file( lua_State *L ) {
|
||||
luaL_rometatable( L, "file.vol", (void *)file_vol_map );
|
||||
return 0;
|
||||
}
|
||||
|
||||
NODEMCU_MODULE(FILE, "file", file_map, luaopen_file);
|
||||
|
|
|
@ -23,7 +23,7 @@
|
|||
#include "driver/uart.h"
|
||||
#include "user_interface.h"
|
||||
#include "flash_api.h"
|
||||
#include "flash_fs.h"
|
||||
#include "vfs.h"
|
||||
#include "user_version.h"
|
||||
#include "rom.h"
|
||||
#include "task/task.h"
|
||||
|
@ -226,11 +226,11 @@ static int writer(lua_State* L, const void* p, size_t size, void* u)
|
|||
{
|
||||
UNUSED(L);
|
||||
int file_fd = *( (int *)u );
|
||||
if ((FS_OPEN_OK - 1) == file_fd)
|
||||
if (!file_fd)
|
||||
return 1;
|
||||
NODE_DBG("get fd:%d,size:%d\n", file_fd, size);
|
||||
|
||||
if (size != 0 && (size != fs_write(file_fd, (const char *)p, size)) )
|
||||
if (size != 0 && (size != vfs_write(file_fd, (const char *)p, size)) )
|
||||
return 1;
|
||||
NODE_DBG("write fd:%d,size:%d\n", file_fd, size);
|
||||
return 0;
|
||||
|
@ -241,23 +241,26 @@ static int writer(lua_State* L, const void* p, size_t size, void* u)
|
|||
static int node_compile( lua_State* L )
|
||||
{
|
||||
Proto* f;
|
||||
int file_fd = FS_OPEN_OK - 1;
|
||||
int file_fd = 0;
|
||||
size_t len;
|
||||
const char *fname = luaL_checklstring( L, 1, &len );
|
||||
if ( len >= FS_NAME_MAX_LENGTH )
|
||||
return luaL_error(L, "filename too long");
|
||||
const char *basename = vfs_basename( fname );
|
||||
luaL_argcheck(L, c_strlen(basename) <= FS_OBJ_NAME_LEN && c_strlen(fname) == len, 1, "filename invalid");
|
||||
|
||||
char output[FS_NAME_MAX_LENGTH];
|
||||
char *output = luaM_malloc( L, len+1 );
|
||||
c_strcpy(output, fname);
|
||||
// check here that filename end with ".lua".
|
||||
if (len < 4 || (c_strcmp( output + len - 4, ".lua") != 0) )
|
||||
if (len < 4 || (c_strcmp( output + len - 4, ".lua") != 0) ) {
|
||||
luaM_free( L, output );
|
||||
return luaL_error(L, "not a .lua file");
|
||||
}
|
||||
|
||||
output[c_strlen(output) - 2] = 'c';
|
||||
output[c_strlen(output) - 1] = '\0';
|
||||
NODE_DBG(output);
|
||||
NODE_DBG("\n");
|
||||
if (luaL_loadfsfile(L, fname) != 0) {
|
||||
luaM_free( L, output );
|
||||
return luaL_error(L, lua_tostring(L, -1));
|
||||
}
|
||||
|
||||
|
@ -265,9 +268,10 @@ static int node_compile( lua_State* L )
|
|||
|
||||
int stripping = 1; /* strip debug information? */
|
||||
|
||||
file_fd = fs_open(output, fs_mode2flag("w+"));
|
||||
if (file_fd < FS_OPEN_OK)
|
||||
file_fd = vfs_open(output, "w+");
|
||||
if (!file_fd)
|
||||
{
|
||||
luaM_free( L, output );
|
||||
return luaL_error(L, "cannot open/write to file");
|
||||
}
|
||||
|
||||
|
@ -275,12 +279,13 @@ static int node_compile( lua_State* L )
|
|||
int result = luaU_dump(L, f, writer, &file_fd, stripping);
|
||||
lua_unlock(L);
|
||||
|
||||
if (fs_flush(file_fd) < 0) { // result codes aren't propagated by flash_fs.h
|
||||
if (vfs_flush(file_fd) != VFS_RES_OK) {
|
||||
// overwrite Lua error, like writer() does in case of a file io error
|
||||
result = 1;
|
||||
}
|
||||
fs_close(file_fd);
|
||||
file_fd = FS_OPEN_OK - 1;
|
||||
vfs_close(file_fd);
|
||||
file_fd = 0;
|
||||
luaM_free( L, output );
|
||||
|
||||
if (result == LUA_ERR_CC_INTOVERFLOW) {
|
||||
return luaL_error(L, "value too big or small for target integer type");
|
||||
|
|
|
@ -1,28 +0,0 @@
|
|||
#include "flash_fs.h"
|
||||
#include "c_string.h"
|
||||
|
||||
#include "spiffs.h"
|
||||
|
||||
int fs_mode2flag(const char *mode){
|
||||
if(c_strlen(mode)==1){
|
||||
if(c_strcmp(mode,"w")==0)
|
||||
return FS_WRONLY|FS_CREAT|FS_TRUNC;
|
||||
else if(c_strcmp(mode, "r")==0)
|
||||
return FS_RDONLY;
|
||||
else if(c_strcmp(mode, "a")==0)
|
||||
return FS_WRONLY|FS_CREAT|FS_APPEND;
|
||||
else
|
||||
return FS_RDONLY;
|
||||
} else if (c_strlen(mode)==2){
|
||||
if(c_strcmp(mode,"r+")==0)
|
||||
return FS_RDWR;
|
||||
else if(c_strcmp(mode, "w+")==0)
|
||||
return FS_RDWR|FS_CREAT|FS_TRUNC;
|
||||
else if(c_strcmp(mode, "a+")==0)
|
||||
return FS_RDWR|FS_CREAT|FS_APPEND;
|
||||
else
|
||||
return FS_RDONLY;
|
||||
} else {
|
||||
return FS_RDONLY;
|
||||
}
|
||||
}
|
|
@ -1,52 +0,0 @@
|
|||
|
||||
#ifndef __FLASH_FS_H__
|
||||
#define __FLASH_FS_H__
|
||||
|
||||
#include "user_config.h"
|
||||
|
||||
#if defined( BUILD_SPIFFS )
|
||||
|
||||
#include "myspiffs.h"
|
||||
|
||||
#define FS_OPEN_OK 1
|
||||
|
||||
#define FS_RDONLY SPIFFS_RDONLY
|
||||
#define FS_WRONLY SPIFFS_WRONLY
|
||||
#define FS_RDWR SPIFFS_RDWR
|
||||
#define FS_APPEND SPIFFS_APPEND
|
||||
#define FS_TRUNC SPIFFS_TRUNC
|
||||
#define FS_CREAT SPIFFS_CREAT
|
||||
#define FS_EXCL SPIFFS_EXCL
|
||||
|
||||
#define FS_SEEK_SET SPIFFS_SEEK_SET
|
||||
#define FS_SEEK_CUR SPIFFS_SEEK_CUR
|
||||
#define FS_SEEK_END SPIFFS_SEEK_END
|
||||
|
||||
#define fs_open myspiffs_open
|
||||
#define fs_close myspiffs_close
|
||||
#define fs_write myspiffs_write
|
||||
#define fs_read myspiffs_read
|
||||
#define fs_seek myspiffs_lseek
|
||||
#define fs_eof myspiffs_eof
|
||||
#define fs_getc myspiffs_getc
|
||||
#define fs_ungetc myspiffs_ungetc
|
||||
#define fs_flush myspiffs_flush
|
||||
#define fs_error myspiffs_error
|
||||
#define fs_clearerr myspiffs_clearerr
|
||||
#define fs_tell myspiffs_tell
|
||||
|
||||
#define fs_format myspiffs_format
|
||||
#define fs_check myspiffs_check
|
||||
#define fs_rename myspiffs_rename
|
||||
#define fs_size myspiffs_size
|
||||
|
||||
#define fs_mount myspiffs_mount
|
||||
#define fs_unmount myspiffs_unmount
|
||||
|
||||
#define FS_NAME_MAX_LENGTH SPIFFS_OBJ_NAME_LEN
|
||||
|
||||
#endif
|
||||
|
||||
int fs_mode2flag(const char *mode);
|
||||
|
||||
#endif // #ifndef __FLASH_FS_H__
|
|
@ -671,7 +671,7 @@ int platform_i2c_recv_byte( unsigned id, int ack ){
|
|||
|
||||
// *****************************************************************************
|
||||
// SPI platform interface
|
||||
uint32_t platform_spi_setup( uint8_t id, int mode, unsigned cpol, unsigned cpha, uint32_t clock_div)
|
||||
uint32_t platform_spi_setup( uint8_t id, int mode, unsigned cpol, unsigned cpha, uint32_t clock_div )
|
||||
{
|
||||
spi_master_init( id, cpol, cpha, clock_div );
|
||||
return 1;
|
||||
|
@ -696,6 +696,49 @@ spi_data_type platform_spi_send_recv( uint8_t id, uint8_t bitlen, spi_data_type
|
|||
return spi_mast_get_miso( id, 0, bitlen );
|
||||
}
|
||||
|
||||
int platform_spi_blkwrite( uint8_t id, size_t len, const uint8_t *data )
|
||||
{
|
||||
spi_mast_byte_order( id, SPI_ORDER_LSB );
|
||||
|
||||
while (len > 0) {
|
||||
size_t chunk_len = len > 64 ? 64 : len;
|
||||
|
||||
spi_mast_blkset( id, chunk_len * 8, data );
|
||||
spi_mast_transaction( id, 0, 0, 0, 0, chunk_len * 8, 0, 0 );
|
||||
|
||||
data = &(data[chunk_len]);
|
||||
len -= chunk_len;
|
||||
}
|
||||
|
||||
spi_mast_byte_order( id, SPI_ORDER_MSB );
|
||||
|
||||
return PLATFORM_OK;
|
||||
}
|
||||
|
||||
int platform_spi_blkread( uint8_t id, size_t len, uint8_t *data )
|
||||
{
|
||||
uint8_t mosi_idle[64];
|
||||
|
||||
os_memset( (void *)mosi_idle, 0xff, len > 64 ? 64 : len );
|
||||
|
||||
spi_mast_byte_order( id, SPI_ORDER_LSB );
|
||||
|
||||
while (len > 0 ) {
|
||||
size_t chunk_len = len > 64 ? 64 : len;
|
||||
|
||||
spi_mast_blkset( id, chunk_len * 8, mosi_idle );
|
||||
spi_mast_transaction( id, 0, 0, 0, 0, chunk_len * 8, 0, -1 );
|
||||
spi_mast_blkget( id, chunk_len * 8, data );
|
||||
|
||||
data = &(data[chunk_len]);
|
||||
len -= chunk_len;
|
||||
}
|
||||
|
||||
spi_mast_byte_order( id, SPI_ORDER_MSB );
|
||||
|
||||
return PLATFORM_OK;
|
||||
}
|
||||
|
||||
int platform_spi_set_mosi( uint8_t id, uint16_t offset, uint8_t bitlen, spi_data_type data )
|
||||
{
|
||||
if (offset + bitlen > 512)
|
||||
|
|
|
@ -110,6 +110,8 @@ int platform_spi_send( uint8_t id, uint8_t bitlen, spi_data_type data );
|
|||
spi_data_type platform_spi_send_recv( uint8_t id, uint8_t bitlen, spi_data_type data );
|
||||
void platform_spi_select( unsigned id, int is_select );
|
||||
|
||||
int platform_spi_blkwrite( uint8_t id, size_t len, const uint8_t *data );
|
||||
int platform_spi_blkread( uint8_t id, size_t len, uint8_t *data );
|
||||
int platform_spi_set_mosi( uint8_t id, uint16_t offset, uint8_t bitlen, spi_data_type data );
|
||||
spi_data_type platform_spi_get_miso( uint8_t id, uint16_t offset, uint8_t bitlen );
|
||||
int platform_spi_transaction( uint8_t id, uint8_t cmd_bitlen, spi_data_type cmd_data,
|
||||
|
|
|
@ -0,0 +1,563 @@
|
|||
|
||||
#include "platform.h"
|
||||
#include "driver/spi.h"
|
||||
#include "c_types.h"
|
||||
|
||||
#include "sdcard.h"
|
||||
|
||||
|
||||
#define CHECK_SSPIN(pin) \
|
||||
if (pin < 1 || pin > NUM_GPIO) return FALSE; \
|
||||
m_ss_pin = pin;
|
||||
|
||||
|
||||
//==============================================================================
|
||||
// SD card commands
|
||||
/** GO_IDLE_STATE - init card in spi mode if CS low */
|
||||
uint8_t const CMD0 = 0X00;
|
||||
/** SEND_IF_COND - verify SD Memory Card interface operating condition.*/
|
||||
uint8_t const CMD8 = 0X08;
|
||||
/** SEND_CSD - read the Card Specific Data (CSD register) */
|
||||
uint8_t const CMD9 = 0X09;
|
||||
/** SEND_CID - read the card identification information (CID register) */
|
||||
uint8_t const CMD10 = 0X0A;
|
||||
/** STOP_TRANSMISSION - end multiple block read sequence */
|
||||
uint8_t const CMD12 = 0X0C;
|
||||
/** SEND_STATUS - read the card status register */
|
||||
uint8_t const CMD13 = 0X0D;
|
||||
/** READ_SINGLE_BLOCK - read a single data block from the card */
|
||||
uint8_t const CMD17 = 0X11;
|
||||
/** READ_MULTIPLE_BLOCK - read a multiple data blocks from the card */
|
||||
uint8_t const CMD18 = 0X12;
|
||||
/** WRITE_BLOCK - write a single data block to the card */
|
||||
uint8_t const CMD24 = 0X18;
|
||||
/** WRITE_MULTIPLE_BLOCK - write blocks of data until a STOP_TRANSMISSION */
|
||||
uint8_t const CMD25 = 0X19;
|
||||
/** ERASE_WR_BLK_START - sets the address of the first block to be erased */
|
||||
uint8_t const CMD32 = 0X20;
|
||||
/** ERASE_WR_BLK_END - sets the address of the last block of the continuous
|
||||
range to be erased*/
|
||||
uint8_t const CMD33 = 0X21;
|
||||
/** ERASE - erase all previously selected blocks */
|
||||
uint8_t const CMD38 = 0X26;
|
||||
/** APP_CMD - escape for application specific command */
|
||||
uint8_t const CMD55 = 0X37;
|
||||
/** READ_OCR - read the OCR register of a card */
|
||||
uint8_t const CMD58 = 0X3A;
|
||||
/** CRC_ON_OFF - enable or disable CRC checking */
|
||||
uint8_t const CMD59 = 0X3B;
|
||||
/** SET_WR_BLK_ERASE_COUNT - Set the number of write blocks to be
|
||||
pre-erased before writing */
|
||||
uint8_t const ACMD23 = 0X17;
|
||||
/** SD_SEND_OP_COMD - Sends host capacity support information and
|
||||
activates the card's initialization process */
|
||||
uint8_t const ACMD41 = 0X29;
|
||||
//==============================================================================
|
||||
/** status for card in the ready state */
|
||||
uint8_t const R1_READY_STATE = 0X00;
|
||||
/** status for card in the idle state */
|
||||
uint8_t const R1_IDLE_STATE = 0X01;
|
||||
/** status bit for illegal command */
|
||||
uint8_t const R1_ILLEGAL_COMMAND = 0X04;
|
||||
/** start data token for read or write single block*/
|
||||
uint8_t const DATA_START_BLOCK = 0XFE;
|
||||
/** stop token for write multiple blocks*/
|
||||
uint8_t const STOP_TRAN_TOKEN = 0XFD;
|
||||
/** start data token for write multiple blocks*/
|
||||
uint8_t const WRITE_MULTIPLE_TOKEN = 0XFC;
|
||||
/** mask for data response tokens after a write block operation */
|
||||
uint8_t const DATA_RES_MASK = 0X1F;
|
||||
/** write data accepted token */
|
||||
uint8_t const DATA_RES_ACCEPTED = 0X05;
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// SD card errors
|
||||
/** timeout error for command CMD0 (initialize card in SPI mode) */
|
||||
uint8_t const SD_CARD_ERROR_CMD0 = 0X1;
|
||||
/** CMD8 was not accepted - not a valid SD card*/
|
||||
uint8_t const SD_CARD_ERROR_CMD8 = 0X2;
|
||||
/** card returned an error response for CMD12 (stop multiblock read) */
|
||||
uint8_t const SD_CARD_ERROR_CMD12 = 0X3;
|
||||
/** card returned an error response for CMD17 (read block) */
|
||||
uint8_t const SD_CARD_ERROR_CMD17 = 0X4;
|
||||
/** card returned an error response for CMD18 (read multiple block) */
|
||||
uint8_t const SD_CARD_ERROR_CMD18 = 0X5;
|
||||
/** card returned an error response for CMD24 (write block) */
|
||||
uint8_t const SD_CARD_ERROR_CMD24 = 0X6;
|
||||
/** WRITE_MULTIPLE_BLOCKS command failed */
|
||||
uint8_t const SD_CARD_ERROR_CMD25 = 0X7;
|
||||
/** card returned an error response for CMD58 (read OCR) */
|
||||
uint8_t const SD_CARD_ERROR_CMD58 = 0X8;
|
||||
/** SET_WR_BLK_ERASE_COUNT failed */
|
||||
uint8_t const SD_CARD_ERROR_ACMD23 = 0X9;
|
||||
/** ACMD41 initialization process timeout */
|
||||
uint8_t const SD_CARD_ERROR_ACMD41 = 0XA;
|
||||
/** card returned a bad CSR version field */
|
||||
uint8_t const SD_CARD_ERROR_BAD_CSD = 0XB;
|
||||
/** erase block group command failed */
|
||||
uint8_t const SD_CARD_ERROR_ERASE = 0XC;
|
||||
/** card not capable of single block erase */
|
||||
uint8_t const SD_CARD_ERROR_ERASE_SINGLE_BLOCK = 0XD;
|
||||
/** Erase sequence timed out */
|
||||
uint8_t const SD_CARD_ERROR_ERASE_TIMEOUT = 0XE;
|
||||
/** card returned an error token instead of read data */
|
||||
uint8_t const SD_CARD_ERROR_READ = 0XF;
|
||||
/** read CID or CSD failed */
|
||||
uint8_t const SD_CARD_ERROR_READ_REG = 0X10;
|
||||
/** timeout while waiting for start of read data */
|
||||
uint8_t const SD_CARD_ERROR_READ_TIMEOUT = 0X11;
|
||||
/** card did not accept STOP_TRAN_TOKEN */
|
||||
uint8_t const SD_CARD_ERROR_STOP_TRAN = 0X12;
|
||||
/** card returned an error token as a response to a write operation */
|
||||
uint8_t const SD_CARD_ERROR_WRITE = 0X13;
|
||||
/** attempt to write protected block zero */
|
||||
uint8_t const SD_CARD_ERROR_WRITE_BLOCK_ZERO = 0X14; // REMOVE - not used
|
||||
/** card did not go ready for a multiple block write */
|
||||
uint8_t const SD_CARD_ERROR_WRITE_MULTIPLE = 0X15;
|
||||
/** card returned an error to a CMD13 status check after a write */
|
||||
uint8_t const SD_CARD_ERROR_WRITE_PROGRAMMING = 0X16;
|
||||
/** timeout occurred during write programming */
|
||||
uint8_t const SD_CARD_ERROR_WRITE_TIMEOUT = 0X17;
|
||||
/** incorrect rate selected */
|
||||
uint8_t const SD_CARD_ERROR_SCK_RATE = 0X18;
|
||||
/** init() not called */
|
||||
uint8_t const SD_CARD_ERROR_INIT_NOT_CALLED = 0X19;
|
||||
/** card returned an error for CMD59 (CRC_ON_OFF) */
|
||||
uint8_t const SD_CARD_ERROR_CMD59 = 0X1A;
|
||||
/** invalid read CRC */
|
||||
uint8_t const SD_CARD_ERROR_READ_CRC = 0X1B;
|
||||
/** SPI DMA error */
|
||||
uint8_t const SD_CARD_ERROR_SPI_DMA = 0X1C;
|
||||
//------------------------------------------------------------------------------
|
||||
// card types
|
||||
uint8_t const SD_CARD_TYPE_INVALID = 0;
|
||||
/** Standard capacity V1 SD card */
|
||||
uint8_t const SD_CARD_TYPE_SD1 = 1;
|
||||
/** Standard capacity V2 SD card */
|
||||
uint8_t const SD_CARD_TYPE_SD2 = 2;
|
||||
/** High Capacity SD card */
|
||||
uint8_t const SD_CARD_TYPE_SDHC = 3;
|
||||
|
||||
|
||||
typedef struct {
|
||||
uint32_t start, target;
|
||||
} to_t;
|
||||
|
||||
static uint8_t m_spi_no, m_ss_pin, m_status, m_type, m_error;
|
||||
|
||||
static void sdcard_chipselect_low( void ) {
|
||||
platform_gpio_write( m_ss_pin, PLATFORM_GPIO_LOW );
|
||||
}
|
||||
|
||||
static void sdcard_chipselect_high( void ) {
|
||||
platform_gpio_write( m_ss_pin, PLATFORM_GPIO_HIGH );
|
||||
// send some cc to ensure that MISO returns to high
|
||||
platform_spi_send_recv( m_spi_no, 8, 0xff );
|
||||
}
|
||||
|
||||
static void set_timeout( to_t *to, uint32_t us )
|
||||
{
|
||||
uint32_t offset;
|
||||
|
||||
to->start = system_get_time();
|
||||
|
||||
offset = 0xffffffff - to->start;
|
||||
if (offset > us) {
|
||||
to->target = us - offset;
|
||||
} else {
|
||||
to->target = to->start + us;
|
||||
}
|
||||
}
|
||||
|
||||
static uint8_t timed_out( to_t *to )
|
||||
{
|
||||
uint32_t now = system_get_time();
|
||||
|
||||
if (to->start < to->target) {
|
||||
if ((now >= to->start) && (now <= to->target)) {
|
||||
return FALSE;
|
||||
} else {
|
||||
return TRUE;
|
||||
}
|
||||
} else {
|
||||
if ((now >= to->start) || (now <= to->target)) {
|
||||
return FALSE;
|
||||
} else {
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int sdcard_wait_not_busy( uint32_t us )
|
||||
{
|
||||
to_t to;
|
||||
|
||||
set_timeout( &to, us );
|
||||
while (platform_spi_send_recv( m_spi_no, 8, 0xff ) != 0xff) {
|
||||
if (timed_out( &to )) {
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
return TRUE;
|
||||
|
||||
fail:
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static uint8_t sdcard_command( uint8_t cmd, uint32_t arg )
|
||||
{
|
||||
sdcard_chipselect_low();
|
||||
|
||||
// wait until card is busy
|
||||
sdcard_wait_not_busy( 100 * 1000 );
|
||||
|
||||
// send command
|
||||
// with precalculated CRC - correct for CMD0 with arg zero or CMD8 with arg 0x1AA
|
||||
const uint8_t crc = cmd == CMD0 ? 0x95 : 0x87;
|
||||
platform_spi_transaction( m_spi_no, 16, (cmd | 0x40) << 8 | arg >> 24, 32, arg << 8 | crc, 0, 0, 0 );
|
||||
|
||||
// skip dangling byte of data transfer
|
||||
if (cmd == CMD12) {
|
||||
platform_spi_transaction( m_spi_no, 8, 0xff, 0, 0, 0, 0, 0 );
|
||||
}
|
||||
|
||||
// wait for response
|
||||
for (uint8_t i = 0; ((m_status = platform_spi_send_recv( m_spi_no, 8, 0xff )) & 0x80) && i != 0xFF; i++) ;
|
||||
|
||||
return m_status;
|
||||
}
|
||||
|
||||
static uint8_t sdcard_acmd( uint8_t cmd, uint32_t arg ) {
|
||||
sdcard_command( CMD55, 0 );
|
||||
return sdcard_command( cmd, arg );
|
||||
}
|
||||
|
||||
static int sdcard_write_data( uint8_t token, const uint8_t *src)
|
||||
{
|
||||
uint16_t crc = 0xffff;
|
||||
|
||||
platform_spi_transaction( m_spi_no, 8, token, 0, 0, 0, 0, 0 );
|
||||
platform_spi_blkwrite( m_spi_no, 512, src );
|
||||
platform_spi_transaction( m_spi_no, 16, crc, 0, 0, 0, 0, 0 );
|
||||
|
||||
m_status = platform_spi_send_recv( m_spi_no, 8, 0xff );
|
||||
if ((m_status & DATA_RES_MASK) != DATA_RES_ACCEPTED) {
|
||||
m_error = SD_CARD_ERROR_WRITE;
|
||||
goto fail;
|
||||
}
|
||||
return TRUE;
|
||||
|
||||
fail:
|
||||
sdcard_chipselect_high();
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static int sdcard_read_data( uint8_t *dst, size_t count )
|
||||
{
|
||||
to_t to;
|
||||
|
||||
// wait for start block token
|
||||
set_timeout( &to, 100 * 1000 );
|
||||
while ((m_status = platform_spi_send_recv( m_spi_no, 8, 0xff)) == 0xff) {
|
||||
if (timed_out( &to )) {
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
|
||||
if (m_status != DATA_START_BLOCK) {
|
||||
m_error = SD_CARD_ERROR_READ;
|
||||
goto fail;
|
||||
}
|
||||
// transfer data
|
||||
platform_spi_blkread( m_spi_no, count, (void *)dst );
|
||||
|
||||
// discard crc
|
||||
platform_spi_transaction( m_spi_no, 16, 0xffff, 0, 0, 0, 0, 0 );
|
||||
|
||||
sdcard_chipselect_high();
|
||||
return TRUE;
|
||||
|
||||
fail:
|
||||
sdcard_chipselect_high();
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static int sdcard_read_register( uint8_t cmd, uint8_t *buf )
|
||||
{
|
||||
if (sdcard_command( cmd, 0 )) {
|
||||
m_error = SD_CARD_ERROR_READ_REG;
|
||||
goto fail;
|
||||
}
|
||||
return sdcard_read_data( buf, 16 );
|
||||
|
||||
fail:
|
||||
sdcard_chipselect_high();
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
int platform_sdcard_init( uint8_t spi_no, uint8_t ss_pin )
|
||||
{
|
||||
uint32_t arg, user_spi_clkdiv;
|
||||
to_t to;
|
||||
|
||||
m_type = SD_CARD_TYPE_INVALID;
|
||||
m_error = 0;
|
||||
|
||||
if (spi_no > 1) {
|
||||
return FALSE;
|
||||
}
|
||||
m_spi_no = spi_no;
|
||||
CHECK_SSPIN(ss_pin);
|
||||
|
||||
platform_gpio_write( m_ss_pin, PLATFORM_GPIO_HIGH );
|
||||
platform_gpio_mode( m_ss_pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT );
|
||||
|
||||
// set SPI clock to 400 kHz for init phase
|
||||
user_spi_clkdiv = spi_set_clkdiv( m_spi_no, 200 );
|
||||
|
||||
// apply initialization sequence:
|
||||
// keep ss and io high, apply clock for max(1ms; 74cc)
|
||||
// 1ms requires 400cc @ 400kHz
|
||||
for (int i = 0; i < 2; i++) {
|
||||
platform_spi_transaction( m_spi_no, 0, 0, 0, 0, 0, 200, 0 );
|
||||
}
|
||||
|
||||
// command to go idle in SPI mode
|
||||
set_timeout( &to, 500 * 1000 );
|
||||
while (sdcard_command( CMD0, 0 ) != R1_IDLE_STATE) {
|
||||
if (timed_out( &to )) {
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
|
||||
set_timeout( &to, 500 * 1000 );
|
||||
while (1) {
|
||||
if (sdcard_command( CMD8, 0x1aa) == (R1_ILLEGAL_COMMAND | R1_IDLE_STATE)) {
|
||||
m_type = SD_CARD_TYPE_SD1;
|
||||
break;
|
||||
}
|
||||
for (uint8_t i = 0; i < 4; i++) {
|
||||
m_status = platform_spi_send_recv( m_spi_no, 8, 0xff );
|
||||
}
|
||||
if (m_status == 0xaa) {
|
||||
m_type = SD_CARD_TYPE_SD2;
|
||||
break;
|
||||
}
|
||||
if (timed_out( &to )) {
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
// initialize card and send host supports SDHC if SD2
|
||||
arg = m_type == SD_CARD_TYPE_SD2 ? 0x40000000 : 0;
|
||||
|
||||
set_timeout( &to, 500 * 1000 );
|
||||
while (sdcard_acmd( ACMD41, arg ) != R1_READY_STATE) {
|
||||
if (timed_out( &to )) {
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
// if SD2 read OCR register to check for SDHC card
|
||||
if (m_type == SD_CARD_TYPE_SD2) {
|
||||
if (sdcard_command( CMD58, 0 )) {
|
||||
m_error = SD_CARD_ERROR_CMD58;
|
||||
goto fail;
|
||||
}
|
||||
if ((platform_spi_send_recv( m_spi_no, 8, 0xff ) & 0xC0) == 0xC0) {
|
||||
m_type = SD_CARD_TYPE_SDHC;
|
||||
}
|
||||
// Discard rest of ocr - contains allowed voltage range.
|
||||
for (uint8_t i = 0; i < 3; i++) {
|
||||
platform_spi_send_recv( m_spi_no, 8, 0xff);
|
||||
}
|
||||
}
|
||||
sdcard_chipselect_high();
|
||||
|
||||
// re-apply user's spi clock divider
|
||||
spi_set_clkdiv( m_spi_no, user_spi_clkdiv );
|
||||
|
||||
return TRUE;
|
||||
|
||||
fail:
|
||||
sdcard_chipselect_high();
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
int platform_sdcard_status( void )
|
||||
{
|
||||
return m_status;
|
||||
}
|
||||
|
||||
int platform_sdcard_error( void )
|
||||
{
|
||||
return m_error;
|
||||
}
|
||||
|
||||
int platform_sdcard_type( void )
|
||||
{
|
||||
return m_type;
|
||||
}
|
||||
|
||||
int platform_sdcard_read_block( uint8_t ss_pin, uint32_t block, uint8_t *dst )
|
||||
{
|
||||
CHECK_SSPIN(ss_pin);
|
||||
|
||||
// generate byte address for pre-SDHC types
|
||||
if (m_type != SD_CARD_TYPE_SDHC) {
|
||||
block <<= 9;
|
||||
}
|
||||
if (sdcard_command( CMD17, block )) {
|
||||
m_error = SD_CARD_ERROR_CMD17;
|
||||
goto fail;
|
||||
}
|
||||
return sdcard_read_data( dst, 512 );
|
||||
|
||||
fail:
|
||||
sdcard_chipselect_high();
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
int platform_sdcard_read_blocks( uint8_t ss_pin, uint32_t block, size_t num, uint8_t *dst )
|
||||
{
|
||||
CHECK_SSPIN(ss_pin);
|
||||
|
||||
if (num == 0) {
|
||||
return TRUE;
|
||||
}
|
||||
if (num == 1) {
|
||||
return platform_sdcard_read_block( ss_pin, block, dst );
|
||||
}
|
||||
|
||||
// generate byte address for pre-SDHC types
|
||||
if (m_type != SD_CARD_TYPE_SDHC) {
|
||||
block <<= 9;
|
||||
}
|
||||
|
||||
// command READ_MULTIPLE_BLOCKS
|
||||
if (sdcard_command( CMD18, block )) {
|
||||
m_error = SD_CARD_ERROR_CMD18;
|
||||
goto fail;
|
||||
}
|
||||
|
||||
// read required blocks
|
||||
while (num > 0) {
|
||||
sdcard_chipselect_low();
|
||||
if (sdcard_read_data( dst, 512 )) {
|
||||
num--;
|
||||
dst = &(dst[512]);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// issue command STOP_TRANSMISSION
|
||||
if (sdcard_command( CMD12, 0 )) {
|
||||
m_error = SD_CARD_ERROR_CMD12;
|
||||
goto fail;
|
||||
}
|
||||
sdcard_chipselect_high();
|
||||
return TRUE;
|
||||
|
||||
fail:
|
||||
sdcard_chipselect_high();
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
int platform_sdcard_read_csd( uint8_t ss_pin, uint8_t *csd )
|
||||
{
|
||||
CHECK_SSPIN(ss_pin);
|
||||
|
||||
return sdcard_read_register( CMD9, csd );
|
||||
}
|
||||
|
||||
int platform_sdcard_read_cid( uint8_t ss_pin, uint8_t *cid )
|
||||
{
|
||||
CHECK_SSPIN(ss_pin);
|
||||
|
||||
return sdcard_read_register( CMD10, cid );
|
||||
}
|
||||
|
||||
int platform_sdcard_write_block( uint8_t ss_pin, uint32_t block, const uint8_t *src )
|
||||
{
|
||||
CHECK_SSPIN(ss_pin);
|
||||
|
||||
// generate byte address for pre-SDHC types
|
||||
if (m_type != SD_CARD_TYPE_SDHC) {
|
||||
block <<= 9;
|
||||
}
|
||||
if (sdcard_command( CMD24, block )) {
|
||||
m_error = SD_CARD_ERROR_CMD24;
|
||||
goto fail;
|
||||
}
|
||||
if (! sdcard_write_data( DATA_START_BLOCK, src )) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
sdcard_chipselect_high();
|
||||
return TRUE;
|
||||
|
||||
fail:
|
||||
sdcard_chipselect_high();
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static int sdcard_write_stop( void )
|
||||
{
|
||||
sdcard_chipselect_low();
|
||||
|
||||
if (! sdcard_wait_not_busy( 100 * 1000 )) {
|
||||
goto fail;
|
||||
}
|
||||
platform_spi_transaction( m_spi_no, 8, STOP_TRAN_TOKEN, 0, 0, 0, 0, 0 );
|
||||
if (! sdcard_wait_not_busy( 100 * 1000 )) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
sdcard_chipselect_high();
|
||||
return TRUE;
|
||||
|
||||
fail:
|
||||
m_error = SD_CARD_ERROR_STOP_TRAN;
|
||||
sdcard_chipselect_high();
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
int platform_sdcard_write_blocks( uint8_t ss_pin, uint32_t block, size_t num, const uint8_t *src )
|
||||
{
|
||||
CHECK_SSPIN(ss_pin);
|
||||
|
||||
if (sdcard_acmd( ACMD23, num )) {
|
||||
m_error = SD_CARD_ERROR_ACMD23;
|
||||
goto fail;
|
||||
}
|
||||
// generate byte address for pre-SDHC types
|
||||
if (m_type != SD_CARD_TYPE_SDHC) {
|
||||
block <<= 9;
|
||||
}
|
||||
if (sdcard_command( CMD25, block )) {
|
||||
m_error = SD_CARD_ERROR_CMD25;
|
||||
goto fail;
|
||||
}
|
||||
sdcard_chipselect_high();
|
||||
|
||||
for (size_t b = 0; b < num; b++, src += 512) {
|
||||
sdcard_chipselect_low();
|
||||
|
||||
// wait for previous write to finish
|
||||
if (! sdcard_wait_not_busy( 100 * 1000 )) {
|
||||
goto fail_write;
|
||||
}
|
||||
if (! sdcard_write_data( WRITE_MULTIPLE_TOKEN, src )) {
|
||||
goto fail_write;
|
||||
}
|
||||
|
||||
sdcard_chipselect_high();
|
||||
}
|
||||
|
||||
return sdcard_write_stop();
|
||||
|
||||
fail_write:
|
||||
m_error = SD_CARD_ERROR_WRITE_MULTIPLE;
|
||||
fail:
|
||||
sdcard_chipselect_high();
|
||||
return FALSE;
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
#ifndef _SDCARD_H
|
||||
#define _SDCARD_H
|
||||
|
||||
#include "c_types.h"
|
||||
|
||||
int platform_sdcard_init( uint8_t spi_no, uint8_t ss_pin );
|
||||
int platform_sdcard_status( void );
|
||||
int platform_sdcard_error( void );
|
||||
int platform_sdcard_type( void );
|
||||
int platform_sdcard_read_block( uint8_t ss_pin, uint32_t block, uint8_t *dst );
|
||||
int platform_sdcard_read_blocks( uint8_t ss_pin, uint32_t block, size_t num, uint8_t *dst );
|
||||
int platform_sdcard_read_csd( uint8_t ss_pin, uint8_t *csd );
|
||||
int platform_sdcard_read_cid( uint8_t ss_pin, uint8_t *cid );
|
||||
int platform_sdcard_write_block( uint8_t ss_pin, uint32_t block, const uint8_t *src );
|
||||
int platform_sdcard_write_blocks( uint8_t ss_pin, uint32_t block, size_t num, const uint8_t *src );
|
||||
|
||||
#endif
|
|
@ -0,0 +1,469 @@
|
|||
|
||||
|
||||
#include "c_stdlib.h"
|
||||
#include "vfs.h"
|
||||
|
||||
|
||||
#define LDRV_TRAVERSAL 0
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// RTC system interface
|
||||
//
|
||||
static sint32_t (*rtc_cb)( vfs_time *tm ) = NULL;
|
||||
|
||||
// called by operating system
|
||||
void vfs_register_rtc_cb( sint32_t (*cb)( vfs_time *tm ) )
|
||||
{
|
||||
// allow NULL pointer to unregister callback function
|
||||
rtc_cb = cb;
|
||||
}
|
||||
|
||||
// called by file system drivers
|
||||
sint32_t vfs_get_rtc( vfs_time *tm )
|
||||
{
|
||||
if (rtc_cb) {
|
||||
return rtc_cb( tm );
|
||||
}
|
||||
|
||||
return VFS_RES_ERR;
|
||||
}
|
||||
|
||||
|
||||
static int dir_level = 1;
|
||||
|
||||
static const char *normalize_path( const char *path )
|
||||
{
|
||||
#if ! LDRV_TRAVERSAL
|
||||
return path;
|
||||
#else
|
||||
const char *temp = path;
|
||||
size_t len;
|
||||
|
||||
while ((len = c_strlen( temp )) >= 2) {
|
||||
if (temp[0] == '.' && temp[1] == '.') {
|
||||
--dir_level;
|
||||
if (len >= 4 && dir_level > 0) {
|
||||
// prepare next step
|
||||
temp = &(temp[4]);
|
||||
} else {
|
||||
// we're at top, the remainder is expected be an absolute path
|
||||
temp = &(temp[3]);
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (dir_level > 0) {
|
||||
// no traversal on root level
|
||||
return path;
|
||||
} else {
|
||||
// path traverses via root
|
||||
return temp;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// file system functions
|
||||
//
|
||||
vfs_vol *vfs_mount( const char *name, int num )
|
||||
{
|
||||
vfs_fs_fns *fs_fns;
|
||||
const char *normname = normalize_path( name );
|
||||
char *outname;
|
||||
|
||||
#ifdef BUILD_SPIFFS
|
||||
if (fs_fns = myspiffs_realm( normname, &outname, FALSE )) {
|
||||
return fs_fns->mount( outname, num );
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef BUILD_FATFS
|
||||
if (fs_fns = myfatfs_realm( normname, &outname, FALSE )) {
|
||||
vfs_vol *r = fs_fns->mount( outname, num );
|
||||
c_free( outname );
|
||||
return r;
|
||||
}
|
||||
#endif
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int vfs_open( const char *name, const char *mode )
|
||||
{
|
||||
vfs_fs_fns *fs_fns;
|
||||
const char *normname = normalize_path( name );
|
||||
char *outname;
|
||||
|
||||
#ifdef BUILD_SPIFFS
|
||||
if (fs_fns = myspiffs_realm( normname, &outname, FALSE )) {
|
||||
return (int)fs_fns->open( outname, mode );
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef BUILD_FATFS
|
||||
if (fs_fns = myfatfs_realm( normname, &outname, FALSE )) {
|
||||
int r = (int)fs_fns->open( outname, mode );
|
||||
c_free( outname );
|
||||
return r;
|
||||
}
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
vfs_dir *vfs_opendir( const char *name )
|
||||
{
|
||||
vfs_fs_fns *fs_fns;
|
||||
const char *normname = normalize_path( name );
|
||||
char *outname;
|
||||
|
||||
#ifdef BUILD_SPIFFS
|
||||
if (fs_fns = myspiffs_realm( normname, &outname, FALSE )) {
|
||||
return fs_fns->opendir( outname );
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef BUILD_FATFS
|
||||
if (fs_fns = myfatfs_realm( normname, &outname, FALSE )) {
|
||||
vfs_dir *r = fs_fns->opendir( outname );
|
||||
c_free( outname );
|
||||
return r;
|
||||
}
|
||||
#endif
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
vfs_item *vfs_stat( const char *name )
|
||||
{
|
||||
vfs_fs_fns *fs_fns;
|
||||
const char *normname = normalize_path( name );
|
||||
char *outname;
|
||||
|
||||
#ifdef BUILD_SPIFFS
|
||||
if (fs_fns = myspiffs_realm( normname, &outname, FALSE )) {
|
||||
return fs_fns->stat( outname );
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef BUILD_FATFS
|
||||
if (fs_fns = myfatfs_realm( normname, &outname, FALSE )) {
|
||||
vfs_item *r = fs_fns->stat( outname );
|
||||
c_free( outname );
|
||||
return r;
|
||||
}
|
||||
#endif
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
sint32_t vfs_remove( const char *name )
|
||||
{
|
||||
vfs_fs_fns *fs_fns;
|
||||
const char *normname = normalize_path( name );
|
||||
char *outname;
|
||||
|
||||
#ifdef BUILD_SPIFFS
|
||||
if (fs_fns = myspiffs_realm( normname, &outname, FALSE )) {
|
||||
return fs_fns->remove( outname );
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef BUILD_FATFS
|
||||
if (fs_fns = myfatfs_realm( normname, &outname, FALSE )) {
|
||||
sint32_t r = fs_fns->remove( outname );
|
||||
c_free( outname );
|
||||
return r;
|
||||
}
|
||||
#endif
|
||||
|
||||
return VFS_RES_ERR;
|
||||
}
|
||||
|
||||
sint32_t vfs_rename( const char *oldname, const char *newname )
|
||||
{
|
||||
vfs_fs_fns *fs_fns;
|
||||
const char *normoldname = normalize_path( oldname );
|
||||
const char *normnewname = normalize_path( newname );
|
||||
char *oldoutname, *newoutname;
|
||||
|
||||
#ifdef BUILD_SPIFFS
|
||||
if (myspiffs_realm( normoldname, &oldoutname, FALSE )) {
|
||||
if (fs_fns = myspiffs_realm( normnewname, &newoutname, FALSE )) {
|
||||
return fs_fns->rename( oldoutname, newoutname );
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef BUILD_FATFS
|
||||
if (myfatfs_realm( normoldname, &oldoutname, FALSE )) {
|
||||
if (fs_fns = myfatfs_realm( normnewname, &newoutname, FALSE )) {
|
||||
sint32_t r = fs_fns->rename( oldoutname, newoutname );
|
||||
c_free( oldoutname );
|
||||
c_free( newoutname );
|
||||
return r;
|
||||
}
|
||||
c_free( oldoutname );
|
||||
}
|
||||
#endif
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
sint32_t vfs_mkdir( const char *name )
|
||||
{
|
||||
vfs_fs_fns *fs_fns;
|
||||
const char *normname = normalize_path( name );
|
||||
char *outname;
|
||||
|
||||
#ifdef BUILD_SPIFFS
|
||||
// not supported
|
||||
#endif
|
||||
|
||||
#ifdef BUILD_FATFS
|
||||
if (fs_fns = myfatfs_realm( normname, &outname, FALSE )) {
|
||||
sint32_t r = fs_fns->mkdir( outname );
|
||||
c_free( outname );
|
||||
return r;
|
||||
}
|
||||
#endif
|
||||
|
||||
return VFS_RES_ERR;
|
||||
}
|
||||
|
||||
sint32_t vfs_fsinfo( const char *name, uint32_t *total, uint32_t *used )
|
||||
{
|
||||
vfs_fs_fns *fs_fns;
|
||||
char *outname;
|
||||
|
||||
if (!name) name = ""; // current drive
|
||||
|
||||
const char *normname = normalize_path( name );
|
||||
|
||||
#ifdef BUILD_SPIFFS
|
||||
if (fs_fns = myspiffs_realm( normname, &outname, FALSE )) {
|
||||
return fs_fns->fsinfo( total, used );
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef BUILD_FATFS
|
||||
if (fs_fns = myfatfs_realm( normname, &outname, FALSE )) {
|
||||
c_free( outname );
|
||||
return fs_fns->fsinfo( total, used );
|
||||
}
|
||||
#endif
|
||||
|
||||
return VFS_RES_ERR;
|
||||
}
|
||||
|
||||
sint32_t vfs_fscfg( const char *name, uint32_t *phys_addr, uint32_t *phys_size)
|
||||
{
|
||||
vfs_fs_fns *fs_fns;
|
||||
char *outname;
|
||||
|
||||
#ifdef BUILD_SPIFFS
|
||||
if (fs_fns = myspiffs_realm( "/FLASH", &outname, FALSE )) {
|
||||
return fs_fns->fscfg( phys_addr, phys_size );
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef BUILD_FATFS
|
||||
// not supported
|
||||
#endif
|
||||
|
||||
// Error
|
||||
return VFS_RES_ERR;
|
||||
}
|
||||
|
||||
sint32_t vfs_format( void )
|
||||
{
|
||||
vfs_fs_fns *fs_fns;
|
||||
char *outname;
|
||||
|
||||
#ifdef BUILD_SPIFFS
|
||||
if (fs_fns = myspiffs_realm( "/FLASH", &outname, FALSE )) {
|
||||
return fs_fns->format();
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef BUILD_FATFS
|
||||
// not supported
|
||||
#endif
|
||||
|
||||
// Error
|
||||
return 0;
|
||||
}
|
||||
|
||||
sint32_t vfs_chdir( const char *path )
|
||||
{
|
||||
vfs_fs_fns *fs_fns;
|
||||
const char *normpath = normalize_path( path );
|
||||
const char *level;
|
||||
char *outname;
|
||||
int ok = VFS_RES_ERR;
|
||||
|
||||
#if LDRV_TRAVERSAL
|
||||
// track dir level
|
||||
if (normpath[0] == '/') {
|
||||
dir_level = 0;
|
||||
level = &(normpath[1]);
|
||||
} else {
|
||||
level = normpath;
|
||||
}
|
||||
while (c_strlen( level ) > 0) {
|
||||
dir_level++;
|
||||
if (level = c_strchr( level, '/' )) {
|
||||
level++;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef BUILD_SPIFFS
|
||||
if (fs_fns = myspiffs_realm( normpath, &outname, TRUE )) {
|
||||
// our SPIFFS integration doesn't support directories
|
||||
if (c_strlen( outname ) == 0) {
|
||||
ok = VFS_RES_OK;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef BUILD_FATFS
|
||||
if (fs_fns = myfatfs_realm( normpath, &outname, TRUE )) {
|
||||
if (c_strchr( outname, ':' )) {
|
||||
// need to set FatFS' default drive
|
||||
fs_fns->chdrive( outname );
|
||||
// and force chdir to root in case path points only to root
|
||||
fs_fns->chdir( "/" );
|
||||
}
|
||||
if (fs_fns->chdir( outname ) == VFS_RES_OK) {
|
||||
ok = VFS_RES_OK;
|
||||
}
|
||||
c_free( outname );
|
||||
}
|
||||
#endif
|
||||
|
||||
return ok == VFS_RES_OK ? VFS_RES_OK : VFS_RES_ERR;
|
||||
}
|
||||
|
||||
sint32_t vfs_errno( const char *name )
|
||||
{
|
||||
vfs_fs_fns *fs_fns;
|
||||
const char *normname = normalize_path( name );
|
||||
char *outname;
|
||||
|
||||
if (!name) name = ""; // current drive
|
||||
|
||||
#ifdef BUILD_SPIFFS
|
||||
if (fs_fns = myspiffs_realm( normname, &outname, FALSE )) {
|
||||
return fs_fns->ferrno( );
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef BUILD_FATFS
|
||||
if (fs_fns = myfatfs_realm( normname, &outname, FALSE )) {
|
||||
sint32_t r = fs_fns->ferrno( );
|
||||
c_free( outname );
|
||||
return r;
|
||||
}
|
||||
#endif
|
||||
|
||||
return VFS_RES_ERR;
|
||||
}
|
||||
|
||||
sint32_t vfs_ferrno( int fd )
|
||||
{
|
||||
vfs_file *f = (vfs_file *)fd;
|
||||
|
||||
if (f) {
|
||||
return f->fns->ferrno ? f->fns->ferrno( f ) : 0;
|
||||
} else {
|
||||
vfs_fs_fns *fs_fns;
|
||||
const char *name = ""; // current drive
|
||||
char *outname;
|
||||
|
||||
#ifdef BUILD_SPIFFS
|
||||
if (fs_fns = myspiffs_realm( name, &outname, FALSE )) {
|
||||
return fs_fns->ferrno( );
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef BUILD_FATFS
|
||||
if (fs_fns = myfatfs_realm( name, &outname, FALSE )) {
|
||||
sint32_t r = fs_fns->ferrno( );
|
||||
c_free( outname );
|
||||
return r;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void vfs_clearerr( const char *name )
|
||||
{
|
||||
vfs_fs_fns *fs_fns;
|
||||
char *outname;
|
||||
|
||||
if (!name) name = ""; // current drive
|
||||
|
||||
const char *normname = normalize_path( name );
|
||||
|
||||
#ifdef BUILD_SPIFFS
|
||||
if (fs_fns = myspiffs_realm( normname, &outname, FALSE )) {
|
||||
fs_fns->clearerr( );
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef BUILD_FATFS
|
||||
if (fs_fns = myfatfs_realm( normname, &outname, FALSE )) {
|
||||
fs_fns->clearerr( );
|
||||
c_free( outname );
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
const char *vfs_basename( const char *path )
|
||||
{
|
||||
const char *basename;
|
||||
|
||||
// deduce basename (incl. extension) for length check
|
||||
if (basename = c_strrchr( path, '/' )) {
|
||||
basename++;
|
||||
} else if (basename = c_strrchr( path, ':' )) {
|
||||
basename++;
|
||||
} else {
|
||||
basename = path;
|
||||
}
|
||||
|
||||
return basename;
|
||||
}
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// supplementary functions
|
||||
//
|
||||
int vfs_getc( int fd )
|
||||
{
|
||||
unsigned char c = 0xFF;
|
||||
sint32_t res;
|
||||
|
||||
if(!vfs_eof( fd )) {
|
||||
if (1 != vfs_read( fd, &c, 1 )) {
|
||||
NODE_DBG("getc errno %i\n", vfs_ferrno( fd ));
|
||||
return VFS_EOF;
|
||||
} else {
|
||||
return (int)c;
|
||||
}
|
||||
}
|
||||
return VFS_EOF;
|
||||
}
|
||||
|
||||
int vfs_ungetc( int c, int fd )
|
||||
{
|
||||
return vfs_lseek( fd, -1, VFS_SEEK_CUR );
|
||||
}
|
|
@ -0,0 +1,248 @@
|
|||
|
||||
#ifndef __VFS_H__
|
||||
#define __VFS_H__
|
||||
|
||||
#include "vfs_int.h"
|
||||
|
||||
// DEPRECATED, DON'T USE
|
||||
// Check for fd != 0 instead
|
||||
#define FS_OPEN_OK 1
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// file functions
|
||||
//
|
||||
|
||||
// vfs_close - close file descriptor and free memory
|
||||
// fd: file descriptor
|
||||
// Returns: VFS_RES_OK or negative value in case of error
|
||||
inline sint32_t vfs_close( int fd ) {
|
||||
vfs_file *f = (vfs_file *)fd;
|
||||
return f ? f->fns->close( f ) : VFS_RES_ERR;
|
||||
}
|
||||
|
||||
// vfs_read - read data from file
|
||||
// fd: file descriptor
|
||||
// ptr: destination data buffer
|
||||
// len: requested length
|
||||
// Returns: Number of bytes read, or VFS_RES_ERR in case of error
|
||||
inline sint32_t vfs_read( int fd, void *ptr, size_t len ) {
|
||||
vfs_file *f = (vfs_file *)fd;
|
||||
return f ? f->fns->read( f, ptr, len ) : VFS_RES_ERR;
|
||||
}
|
||||
|
||||
// vfs_write - write data to file
|
||||
// fd: file descriptor
|
||||
// ptr: source data buffer
|
||||
// len: requested length
|
||||
// Returns: Number of bytes written, or VFS_RES_ERR in case of error
|
||||
inline sint32_t vfs_write( int fd, const void *ptr, size_t len ) {
|
||||
vfs_file *f = (vfs_file *)fd;
|
||||
return f ? f->fns->write( f, ptr, len ) : VFS_RES_ERR;
|
||||
}
|
||||
|
||||
int vfs_getc( int fd );
|
||||
|
||||
int vfs_ungetc( int c, int fd );
|
||||
|
||||
// vfs_lseek - move read/write pointer
|
||||
// fd: file descriptor
|
||||
// off: offset
|
||||
// whence: VFS_SEEK_SET - set pointer to off
|
||||
// VFS_SEEK_CUR - set pointer to current position + off
|
||||
// VFS_SEEK_END - set pointer to end of file + off
|
||||
// Returns: New position, or VFS_RES_ERR in case of error
|
||||
inline sint32_t vfs_lseek( int fd, sint32_t off, int whence ) {
|
||||
vfs_file *f = (vfs_file *)fd;
|
||||
return f ? f->fns->lseek( f, off, whence ) : VFS_RES_ERR;
|
||||
}
|
||||
|
||||
// vfs_eof - test for end-of-file
|
||||
// fd: file descriptor
|
||||
// Returns: 0 if not at end, != 0 if end of file
|
||||
inline sint32_t vfs_eof( int fd ) {
|
||||
vfs_file *f = (vfs_file *)fd;
|
||||
return f ? f->fns->eof( f ) : VFS_RES_ERR;
|
||||
}
|
||||
|
||||
// vfs_tell - get read/write position
|
||||
// fd: file descriptor
|
||||
// Returns: Current position
|
||||
inline sint32_t vfs_tell( int fd ) {
|
||||
vfs_file *f = (vfs_file *)fd;
|
||||
return f ? f->fns->tell( f ) : VFS_RES_ERR;
|
||||
}
|
||||
|
||||
// vfs_flush - flush write cache to file
|
||||
// fd: file descriptor
|
||||
// Returns: VFS_RES_OK, or VFS_RES_ERR in case of error
|
||||
inline sint32_t vfs_flush( int fd ) {
|
||||
vfs_file *f = (vfs_file *)fd;
|
||||
return f ? f->fns->flush( f ) : VFS_RES_ERR;
|
||||
}
|
||||
|
||||
// vfs_size - get current file size
|
||||
// fd: file descriptor
|
||||
// Returns: File size
|
||||
inline uint32_t vfs_size( int fd ) {
|
||||
vfs_file *f = (vfs_file *)fd;
|
||||
return f && f->fns->size ? f->fns->size( f ) : 0;
|
||||
}
|
||||
|
||||
// vfs_ferrno - get file system specific errno
|
||||
// fd: file descriptor
|
||||
// Returns: errno
|
||||
sint32_t vfs_ferrno( int fd );
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// dir functions
|
||||
//
|
||||
|
||||
// vfs_closedir - close directory descriptor and free memory
|
||||
// dd: dir descriptor
|
||||
// Returns: VFS_RES_OK, or VFS_RES_ERR in case of error
|
||||
inline sint32_t vfs_closedir( vfs_dir *dd ) { return dd->fns->close( dd ); }
|
||||
|
||||
// vfs_readdir - read next directory item
|
||||
// dd: dir descriptor
|
||||
// Returns: item object, or NULL in case of error
|
||||
inline vfs_item *vfs_readdir( vfs_dir *dd ) { return dd->fns->readdir( dd ); }
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// dir item functions
|
||||
//
|
||||
|
||||
// vfs_closeitem - close directory item and free memory
|
||||
// di: item descriptor
|
||||
// Returns: nothing
|
||||
inline void vfs_closeitem( vfs_item *di ) { return di->fns->close( di ); }
|
||||
|
||||
// vfs_item_size - get item's size
|
||||
// di: item descriptor
|
||||
// Returns: Item size
|
||||
inline uint32_t vfs_item_size( vfs_item *di ) { return di->fns->size( di ); }
|
||||
|
||||
// vfs_item_time - get item's modification time
|
||||
// di: item descriptor
|
||||
// Returns: Item modification time
|
||||
inline sint32_t vfs_item_time( vfs_item *di, struct vfs_time *tm ) { return di->fns->time ? di->fns->time( di, tm ) : VFS_RES_ERR; }
|
||||
|
||||
// vfs_item_name - get item's name
|
||||
// di: item descriptor
|
||||
// Returns: Item name
|
||||
inline const char *vfs_item_name( vfs_item *di ) { return di->fns->name( di ); }
|
||||
|
||||
// vfs_item_is_dir - check for directory
|
||||
// di: item descriptor
|
||||
// Returns: >0 if item is a directory, 0 if not
|
||||
inline sint32_t vfs_item_is_dir( vfs_item *di ) { return di->fns->is_dir ? di->fns->is_dir( di ) : 0; }
|
||||
|
||||
// vfs_item_is_rdonly - check for read-only
|
||||
// di: item descriptor
|
||||
// Returns: >0 if item is read only, 0 if not
|
||||
inline sint32_t vfs_item_is_rdonly( vfs_item *di ) { return di->fns->is_rdonly ? di->fns->is_rdonly( di ) : 0; }
|
||||
|
||||
// vfs_item_is_hidden - check for hidden attribute
|
||||
// di: item descriptor
|
||||
// Returns: >0 if item is hidden, 0 if not
|
||||
inline sint32_t vfs_item_is_hidden( vfs_item *di ) { return di->fns->is_hidden ? di->fns->is_hidden( di ) : 0; }
|
||||
|
||||
// vfs_item_is_sys - check for sys attribute
|
||||
// di: item descriptor
|
||||
// Returns: >0 if item is sys, 0 if not
|
||||
inline sint32_t vfs_item_is_sys( vfs_item *di ) { return di->fns->is_sys ? di->fns->is_sys( di ) : 0; }
|
||||
|
||||
// vfs_item_is_arch - check for archive attribute
|
||||
// di: item descriptor
|
||||
// Returns: >0 if item is archive, 0 if not
|
||||
inline sint32_t vfs_item_is_arch( vfs_item *di ) { return di->fns->is_arch ? di->fns->is_arch( di ) : 0; }
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// volume functions
|
||||
//
|
||||
|
||||
// vfs_umount - unmount logical drive and free memory
|
||||
// vol: volume object
|
||||
// Returns: VFS_RES_OK, or VFS_RES_ERR in case of error
|
||||
inline sint32_t vfs_umount( vfs_vol *vol ) { return vol->fns->umount( vol ); }
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// file system functions
|
||||
//
|
||||
|
||||
// vfs_mount - unmount logical drive
|
||||
// name: name of logical drive
|
||||
// num: drive's physical number (eg. SS/CS pin), negative values are ignored
|
||||
// Returns: Volume object, or NULL in case of error
|
||||
vfs_vol *vfs_mount( const char *name, int num );
|
||||
|
||||
// vfs_open - open file
|
||||
// name: file name
|
||||
// mode: open mode
|
||||
// Returns: File descriptor, or NULL in case of error
|
||||
int vfs_open( const char *name, const char *mode );
|
||||
|
||||
// vfs_opendir - open directory
|
||||
// name: dir name
|
||||
// Returns: Directory descriptor, or NULL in case of error
|
||||
vfs_dir *vfs_opendir( const char *name );
|
||||
|
||||
// vfs_stat - stat file or directory
|
||||
// name: file or directory name
|
||||
// Returns: Item object, or NULL in case of error
|
||||
vfs_item *vfs_stat( const char *name );
|
||||
|
||||
// vfs_remove - remove file or directory
|
||||
// name: file or directory name
|
||||
// Returns: VFS_RES_OK, or VFS_RES_ERR in case of error
|
||||
sint32_t vfs_remove( const char *name );
|
||||
|
||||
// vfs_rename - rename file or directory
|
||||
// name: file or directory name
|
||||
// Returns: VFS_RES_OK, or VFS_RES_ERR in case of error
|
||||
sint32_t vfs_rename( const char *oldname, const char *newname );
|
||||
|
||||
// vfs_mkdir - create directory
|
||||
// name: directory name
|
||||
// Returns: VFS_RES_OK, or VFS_RES_ERR in case of error
|
||||
sint32_t vfs_mkdir( const char *name );
|
||||
|
||||
// vfs_fsinfo - get file system info
|
||||
// name: logical drive identifier
|
||||
// total: receives total amount
|
||||
// used: received used amount
|
||||
// Returns: VFS_RES_OK, or VFS_RES_ERR in case of error
|
||||
sint32_t vfs_fsinfo( const char *name, uint32_t *total, uint32_t *used );
|
||||
|
||||
// vfs_format - format file system
|
||||
// Returns: 1, or 0 in case of error
|
||||
sint32_t vfs_format( void );
|
||||
|
||||
// vfs_chdir - change default directory
|
||||
// path: new default directory
|
||||
// Returns: VFS_RES_OK, or VFS_RES_ERR in case of error
|
||||
sint32_t vfs_chdir( const char *path );
|
||||
|
||||
// vfs_fscfg - query configuration settings of file system
|
||||
// phys_addr: pointer to store physical address information
|
||||
// phys_size: pointer to store physical size information
|
||||
// Returns: VFS_RES_OK, or VFS_RES_ERR in case of error
|
||||
sint32_t vfs_fscfg( const char *name, uint32_t *phys_addr, uint32_t *phys_size);
|
||||
|
||||
// vfs_errno - get file system specific errno
|
||||
// name: logical drive identifier
|
||||
// Returns: errno
|
||||
sint32_t vfs_errno( const char *name );
|
||||
|
||||
// vfs_clearerr - cleaer file system specific errno
|
||||
void vfs_clearerr( const char *name );
|
||||
|
||||
// vfs_register_rtc_cb - register callback function for RTC query
|
||||
// cb: pointer to callback function
|
||||
void vfs_register_rtc_cb( sint32_t (*cb)( vfs_time *tm ) );
|
||||
|
||||
// vfs_basename - identify basename (incl. extension)
|
||||
// path: full file system path
|
||||
// Returns: pointer to basename within path string
|
||||
const char *vfs_basename( const char *path );
|
||||
|
||||
#endif
|
|
@ -0,0 +1,136 @@
|
|||
// internal definitions for vfs
|
||||
|
||||
#ifndef __VFS_INT_H__
|
||||
#define __VFS_INT_H__
|
||||
|
||||
#include <c_string.h>
|
||||
#include <c_stdint.h>
|
||||
|
||||
#if 0
|
||||
#include "spiffs.h"
|
||||
|
||||
#include "fatfs_prefix_lib.h"
|
||||
#include "ff.h"
|
||||
#endif
|
||||
|
||||
|
||||
#define VFS_EOF -1
|
||||
|
||||
enum vfs_filesystems {
|
||||
VFS_FS_NONE = 0,
|
||||
VFS_FS_SPIFFS,
|
||||
VFS_FS_FATFS
|
||||
};
|
||||
|
||||
enum vfs_seek {
|
||||
VFS_SEEK_SET = 0,
|
||||
VFS_SEEK_CUR,
|
||||
VFS_SEEK_END
|
||||
};
|
||||
|
||||
enum vfs_result {
|
||||
VFS_RES_OK = 0,
|
||||
VFS_RES_ERR = -1
|
||||
};
|
||||
|
||||
|
||||
struct vfs_time {
|
||||
int year, mon, day;
|
||||
int hour, min, sec;
|
||||
};
|
||||
typedef struct vfs_time vfs_time;
|
||||
|
||||
// generic file descriptor
|
||||
struct vfs_file {
|
||||
int fs_type;
|
||||
const struct vfs_file_fns *fns;
|
||||
};
|
||||
typedef const struct vfs_file vfs_file;
|
||||
|
||||
// file descriptor functions
|
||||
struct vfs_file_fns {
|
||||
sint32_t (*close)( const struct vfs_file *fd );
|
||||
sint32_t (*read)( const struct vfs_file *fd, void *ptr, size_t len );
|
||||
sint32_t (*write)( const struct vfs_file *fd, const void *ptr, size_t len );
|
||||
sint32_t (*lseek)( const struct vfs_file *fd, sint32_t off, int whence );
|
||||
sint32_t (*eof)( const struct vfs_file *fd );
|
||||
sint32_t (*tell)( const struct vfs_file *fd );
|
||||
sint32_t (*flush)( const struct vfs_file *fd );
|
||||
uint32_t (*size)( const struct vfs_file *fd );
|
||||
sint32_t (*ferrno)( const struct vfs_file *fd );
|
||||
};
|
||||
typedef const struct vfs_file_fns vfs_file_fns;
|
||||
|
||||
// generic dir item descriptor
|
||||
struct vfs_item {
|
||||
int fs_type;
|
||||
const struct vfs_item_fns *fns;
|
||||
};
|
||||
typedef const struct vfs_item vfs_item;
|
||||
|
||||
// directory item functions
|
||||
struct vfs_item_fns {
|
||||
void (*close)( const struct vfs_item *di );
|
||||
uint32_t (*size)( const struct vfs_item *di );
|
||||
sint32_t (*time)( const struct vfs_item *di, struct vfs_time *tm );
|
||||
const char *(*name)( const struct vfs_item *di );
|
||||
sint32_t (*is_dir)( const struct vfs_item *di );
|
||||
sint32_t (*is_rdonly)( const struct vfs_item *di );
|
||||
sint32_t (*is_hidden)( const struct vfs_item *di );
|
||||
sint32_t (*is_sys)( const struct vfs_item *di );
|
||||
sint32_t (*is_arch)( const struct vfs_item *di );
|
||||
};
|
||||
typedef const struct vfs_item_fns vfs_item_fns;
|
||||
|
||||
// generic dir descriptor
|
||||
struct vfs_dir {
|
||||
int fs_type;
|
||||
const struct vfs_dir_fns *fns;
|
||||
};
|
||||
typedef const struct vfs_dir vfs_dir;
|
||||
|
||||
// dir descriptor functions
|
||||
struct vfs_dir_fns {
|
||||
sint32_t (*close)( const struct vfs_dir *dd );
|
||||
vfs_item *(*readdir)( const struct vfs_dir *dd );
|
||||
};
|
||||
typedef const struct vfs_dir_fns vfs_dir_fns;
|
||||
|
||||
// generic volume descriptor
|
||||
struct vfs_vol {
|
||||
int fs_type;
|
||||
const struct vfs_vol_fns *fns;
|
||||
};
|
||||
typedef const struct vfs_vol vfs_vol;
|
||||
|
||||
// volume functions
|
||||
struct vfs_vol_fns {
|
||||
sint32_t (*umount)( const struct vfs_vol *vol );
|
||||
};
|
||||
typedef const struct vfs_vol_fns vfs_vol_fns;
|
||||
|
||||
struct vfs_fs_fns {
|
||||
vfs_vol *(*mount)( const char *name, int num );
|
||||
vfs_file *(*open)( const char *name, const char *mode );
|
||||
vfs_dir *(*opendir)( const char *name );
|
||||
vfs_item *(*stat)( const char *name );
|
||||
sint32_t (*remove)( const char *name );
|
||||
sint32_t (*rename)( const char *oldname, const char *newname );
|
||||
sint32_t (*mkdir)( const char *name );
|
||||
sint32_t (*fsinfo)( uint32_t *total, uint32_t *used );
|
||||
sint32_t (*fscfg)( uint32_t *phys_addr, uint32_t *phys_size );
|
||||
sint32_t (*format)( void );
|
||||
sint32_t (*chdrive)( const char * );
|
||||
sint32_t (*chdir)( const char * );
|
||||
sint32_t (*ferrno)( void );
|
||||
void (*clearerr)( void );
|
||||
};
|
||||
typedef const struct vfs_fs_fns vfs_fs_fns;
|
||||
|
||||
|
||||
vfs_fs_fns *myspiffs_realm( const char *inname, char **outname, int set_current_drive );
|
||||
vfs_fs_fns *myfatfs_realm( const char *inname, char **outname, int set_current_drive );
|
||||
|
||||
sint32_t vfs_get_rtc( vfs_time *tm );
|
||||
|
||||
#endif
|
|
@ -204,87 +204,6 @@ int myspiffs_format( void )
|
|||
return myspiffs_mount();
|
||||
}
|
||||
|
||||
int myspiffs_check( void )
|
||||
{
|
||||
// ets_wdt_disable();
|
||||
// int res = (int)SPIFFS_check(&fs);
|
||||
// ets_wdt_enable();
|
||||
// return res;
|
||||
}
|
||||
|
||||
int myspiffs_open(const char *name, int flags){
|
||||
return (int)SPIFFS_open(&fs, (char *)name, (spiffs_flags)flags, 0);
|
||||
}
|
||||
|
||||
int myspiffs_close( int fd ){
|
||||
return SPIFFS_close(&fs, (spiffs_file)fd);
|
||||
}
|
||||
size_t myspiffs_write( int fd, const void* ptr, size_t len ){
|
||||
#if 0
|
||||
if(fd==c_stdout || fd==c_stderr){
|
||||
uart0_tx_buffer((u8_t*)ptr, len);
|
||||
return len;
|
||||
}
|
||||
#endif
|
||||
int res = SPIFFS_write(&fs, (spiffs_file)fd, (void *)ptr, len);
|
||||
if (res < 0) {
|
||||
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) {
|
||||
NODE_DBG("read errno %i\n", SPIFFS_errno(&fs));
|
||||
return 0;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
int myspiffs_lseek( int fd, int off, int whence ){
|
||||
return SPIFFS_lseek(&fs, (spiffs_file)fd, off, whence);
|
||||
}
|
||||
int myspiffs_eof( int fd ){
|
||||
return SPIFFS_eof(&fs, (spiffs_file)fd);
|
||||
}
|
||||
int myspiffs_tell( int fd ){
|
||||
return SPIFFS_tell(&fs, (spiffs_file)fd);
|
||||
}
|
||||
int myspiffs_getc( int fd ){
|
||||
unsigned char c = 0xFF;
|
||||
int res;
|
||||
if(!myspiffs_eof(fd)){
|
||||
res = SPIFFS_read(&fs, (spiffs_file)fd, &c, 1);
|
||||
if (res != 1) {
|
||||
NODE_DBG("getc errno %i\n", SPIFFS_errno(&fs));
|
||||
return (int)EOF;
|
||||
} else {
|
||||
return (int)c;
|
||||
}
|
||||
}
|
||||
return (int)EOF;
|
||||
}
|
||||
int myspiffs_ungetc( int c, int fd ){
|
||||
return SPIFFS_lseek(&fs, (spiffs_file)fd, -1, SEEK_CUR);
|
||||
}
|
||||
int myspiffs_flush( int fd ){
|
||||
return SPIFFS_fflush(&fs, (spiffs_file)fd);
|
||||
}
|
||||
int myspiffs_error( int fd ){
|
||||
return SPIFFS_errno(&fs);
|
||||
}
|
||||
void myspiffs_clearerr( int fd ){
|
||||
SPIFFS_clearerr(&fs);
|
||||
}
|
||||
int myspiffs_rename( const char *old, const char *newname ){
|
||||
return SPIFFS_rename(&fs, (char *)old, (char *)newname);
|
||||
}
|
||||
size_t myspiffs_size( int fd ){
|
||||
int32_t curpos = SPIFFS_tell(&fs, (spiffs_file) fd);
|
||||
int32_t size = SPIFFS_lseek(&fs, (spiffs_file) fd, SPIFFS_SEEK_END, 0);
|
||||
(void) SPIFFS_lseek(&fs, (spiffs_file) fd, SPIFFS_SEEK_SET, curpos);
|
||||
return size;
|
||||
}
|
||||
#if 0
|
||||
void test_spiffs() {
|
||||
char buf[12];
|
||||
|
@ -302,3 +221,404 @@ void test_spiffs() {
|
|||
NODE_DBG("--> %s <--\n", buf);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
// ***************************************************************************
|
||||
// vfs API
|
||||
// ***************************************************************************
|
||||
|
||||
#include <c_stdlib.h>
|
||||
#include "vfs_int.h"
|
||||
|
||||
#define MY_LDRV_ID "FLASH"
|
||||
|
||||
// default current drive
|
||||
static int is_current_drive = TRUE;
|
||||
|
||||
// forward declarations
|
||||
static sint32_t myspiffs_vfs_close( const struct vfs_file *fd );
|
||||
static sint32_t myspiffs_vfs_read( const struct vfs_file *fd, void *ptr, size_t len );
|
||||
static sint32_t myspiffs_vfs_write( const struct vfs_file *fd, const void *ptr, size_t len );
|
||||
static sint32_t myspiffs_vfs_lseek( const struct vfs_file *fd, sint32_t off, int whence );
|
||||
static sint32_t myspiffs_vfs_eof( const struct vfs_file *fd );
|
||||
static sint32_t myspiffs_vfs_tell( const struct vfs_file *fd );
|
||||
static sint32_t myspiffs_vfs_flush( const struct vfs_file *fd );
|
||||
static sint32_t myspiffs_vfs_ferrno( const struct vfs_file *fd );
|
||||
|
||||
static sint32_t myspiffs_vfs_closedir( const struct vfs_dir *dd );
|
||||
static vfs_item *myspiffs_vfs_readdir( const struct vfs_dir *dd );
|
||||
|
||||
static void myspiffs_vfs_iclose( const struct vfs_item *di );
|
||||
static uint32_t myspiffs_vfs_isize( const struct vfs_item *di );
|
||||
//static const struct tm *myspiffs_vfs_time( const struct vfs_item *di );
|
||||
static const char *myspiffs_vfs_name( const struct vfs_item *di );
|
||||
|
||||
static vfs_vol *myspiffs_vfs_mount( const char *name, int num );
|
||||
static vfs_file *myspiffs_vfs_open( const char *name, const char *mode );
|
||||
static vfs_dir *myspiffs_vfs_opendir( const char *name );
|
||||
static vfs_item *myspiffs_vfs_stat( const char *name );
|
||||
static sint32_t myspiffs_vfs_remove( const char *name );
|
||||
static sint32_t myspiffs_vfs_rename( const char *oldname, const char *newname );
|
||||
static sint32_t myspiffs_vfs_fsinfo( uint32_t *total, uint32_t *used );
|
||||
static sint32_t myspiffs_vfs_fscfg( uint32_t *phys_addr, uint32_t *phys_size );
|
||||
static sint32_t myspiffs_vfs_format( void );
|
||||
static sint32_t myspiffs_vfs_errno( void );
|
||||
static void myspiffs_vfs_clearerr( void );
|
||||
|
||||
static sint32_t myspiffs_vfs_umount( const struct vfs_vol *vol );
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// function tables
|
||||
//
|
||||
static vfs_fs_fns myspiffs_fs_fns = {
|
||||
.mount = myspiffs_vfs_mount,
|
||||
.open = myspiffs_vfs_open,
|
||||
.opendir = myspiffs_vfs_opendir,
|
||||
.stat = myspiffs_vfs_stat,
|
||||
.remove = myspiffs_vfs_remove,
|
||||
.rename = myspiffs_vfs_rename,
|
||||
.mkdir = NULL,
|
||||
.fsinfo = myspiffs_vfs_fsinfo,
|
||||
.fscfg = myspiffs_vfs_fscfg,
|
||||
.format = myspiffs_vfs_format,
|
||||
.chdrive = NULL,
|
||||
.chdir = NULL,
|
||||
.ferrno = myspiffs_vfs_errno,
|
||||
.clearerr = myspiffs_vfs_clearerr
|
||||
};
|
||||
|
||||
static vfs_file_fns myspiffs_file_fns = {
|
||||
.close = myspiffs_vfs_close,
|
||||
.read = myspiffs_vfs_read,
|
||||
.write = myspiffs_vfs_write,
|
||||
.lseek = myspiffs_vfs_lseek,
|
||||
.eof = myspiffs_vfs_eof,
|
||||
.tell = myspiffs_vfs_tell,
|
||||
.flush = myspiffs_vfs_flush,
|
||||
.size = NULL,
|
||||
.ferrno = myspiffs_vfs_ferrno
|
||||
};
|
||||
|
||||
static vfs_item_fns myspiffs_item_fns = {
|
||||
.close = myspiffs_vfs_iclose,
|
||||
.size = myspiffs_vfs_isize,
|
||||
.time = NULL,
|
||||
.name = myspiffs_vfs_name,
|
||||
.is_dir = NULL,
|
||||
.is_rdonly = NULL,
|
||||
.is_hidden = NULL,
|
||||
.is_sys = NULL,
|
||||
.is_arch = NULL
|
||||
};
|
||||
|
||||
static vfs_dir_fns myspiffs_dd_fns = {
|
||||
.close = myspiffs_vfs_closedir,
|
||||
.readdir = myspiffs_vfs_readdir
|
||||
};
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// specific struct extensions
|
||||
//
|
||||
struct myvfs_file {
|
||||
struct vfs_file vfs_file;
|
||||
spiffs_file fh;
|
||||
};
|
||||
|
||||
struct myvfs_dir {
|
||||
struct vfs_dir vfs_dir;
|
||||
spiffs_DIR d;
|
||||
};
|
||||
|
||||
struct myvfs_stat {
|
||||
struct vfs_item vfs_item;
|
||||
spiffs_stat s;
|
||||
};
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// stat functions
|
||||
//
|
||||
#define GET_STAT_S(descr) \
|
||||
const struct myvfs_stat *mystat = (const struct myvfs_stat *)descr; \
|
||||
spiffs_stat *s = (spiffs_stat *)&(mystat->s);
|
||||
|
||||
static void myspiffs_vfs_iclose( const struct vfs_item *di ) {
|
||||
// free descriptor memory
|
||||
c_free( (void *)di );
|
||||
}
|
||||
|
||||
static uint32_t myspiffs_vfs_isize( const struct vfs_item *di ) {
|
||||
GET_STAT_S(di);
|
||||
|
||||
return s->size;
|
||||
}
|
||||
|
||||
static const char *myspiffs_vfs_name( const struct vfs_item *di ) {
|
||||
GET_STAT_S(di);
|
||||
|
||||
return s->name;
|
||||
}
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// volume functions
|
||||
//
|
||||
static sint32_t myspiffs_vfs_umount( const struct vfs_vol *vol ) {
|
||||
// not implemented
|
||||
|
||||
return VFS_RES_ERR;
|
||||
}
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// dir functions
|
||||
//
|
||||
#define GET_DIR_D(descr) \
|
||||
const struct myvfs_dir *mydd = (const struct myvfs_dir *)descr; \
|
||||
spiffs_DIR *d = (spiffs_DIR *)&(mydd->d);
|
||||
|
||||
static sint32_t myspiffs_vfs_closedir( const struct vfs_dir *dd ) {
|
||||
GET_DIR_D(dd);
|
||||
|
||||
sint32_t res = SPIFFS_closedir( d );
|
||||
|
||||
// free descriptor memory
|
||||
c_free( (void *)dd );
|
||||
}
|
||||
|
||||
static vfs_item *myspiffs_vfs_readdir( const struct vfs_dir *dd ) {
|
||||
GET_DIR_D(dd);
|
||||
struct myvfs_stat *stat;
|
||||
struct spiffs_dirent dirent;
|
||||
|
||||
if (stat = c_malloc( sizeof( struct myvfs_stat ) )) {
|
||||
if (SPIFFS_readdir( d, &dirent )) {
|
||||
stat->vfs_item.fs_type = VFS_FS_FATFS;
|
||||
stat->vfs_item.fns = &myspiffs_item_fns;
|
||||
// copy entries to vfs' directory item
|
||||
stat->s.size = dirent.size;
|
||||
c_strncpy( stat->s.name, dirent.name, SPIFFS_OBJ_NAME_LEN );
|
||||
return (vfs_item *)stat;
|
||||
} else {
|
||||
c_free( stat );
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// file functions
|
||||
//
|
||||
#define GET_FILE_FH(descr) \
|
||||
const struct myvfs_file *myfd = (const struct myvfs_file *)descr; \
|
||||
spiffs_file fh = myfd->fh;
|
||||
|
||||
static sint32_t myspiffs_vfs_close( const struct vfs_file *fd ) {
|
||||
GET_FILE_FH(fd);
|
||||
|
||||
sint32_t res = SPIFFS_close( &fs, fh );
|
||||
|
||||
// free descriptor memory
|
||||
c_free( (void *)fd );
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static sint32_t myspiffs_vfs_read( const struct vfs_file *fd, void *ptr, size_t len ) {
|
||||
GET_FILE_FH(fd);
|
||||
|
||||
return SPIFFS_read( &fs, fh, ptr, len );
|
||||
}
|
||||
|
||||
static sint32_t myspiffs_vfs_write( const struct vfs_file *fd, const void *ptr, size_t len ) {
|
||||
GET_FILE_FH(fd);
|
||||
|
||||
return SPIFFS_write( &fs, fh, (void *)ptr, len );
|
||||
}
|
||||
|
||||
static sint32_t myspiffs_vfs_lseek( const struct vfs_file *fd, sint32_t off, int whence ) {
|
||||
GET_FILE_FH(fd);
|
||||
int spiffs_whence;
|
||||
|
||||
switch (whence) {
|
||||
default:
|
||||
case VFS_SEEK_SET:
|
||||
spiffs_whence = SPIFFS_SEEK_SET;
|
||||
break;
|
||||
case VFS_SEEK_CUR:
|
||||
spiffs_whence = SPIFFS_SEEK_CUR;
|
||||
break;
|
||||
case VFS_SEEK_END:
|
||||
spiffs_whence = SPIFFS_SEEK_END;
|
||||
break;
|
||||
}
|
||||
|
||||
return SPIFFS_lseek( &fs, fh, off, spiffs_whence );
|
||||
}
|
||||
|
||||
static sint32_t myspiffs_vfs_eof( const struct vfs_file *fd ) {
|
||||
GET_FILE_FH(fd);
|
||||
|
||||
return SPIFFS_eof( &fs, fh );
|
||||
}
|
||||
|
||||
static sint32_t myspiffs_vfs_tell( const struct vfs_file *fd ) {
|
||||
GET_FILE_FH(fd);
|
||||
|
||||
return SPIFFS_tell( &fs, fh );
|
||||
}
|
||||
|
||||
static sint32_t myspiffs_vfs_flush( const struct vfs_file *fd ) {
|
||||
GET_FILE_FH(fd);
|
||||
|
||||
return SPIFFS_fflush( &fs, fh ) >= 0 ? VFS_RES_OK : VFS_RES_ERR;
|
||||
}
|
||||
|
||||
static sint32_t myspiffs_vfs_ferrno( const struct vfs_file *fd ) {
|
||||
return SPIFFS_errno( &fs );
|
||||
}
|
||||
|
||||
|
||||
static int fs_mode2flag(const char *mode){
|
||||
if(c_strlen(mode)==1){
|
||||
if(c_strcmp(mode,"w")==0)
|
||||
return SPIFFS_WRONLY|SPIFFS_CREAT|SPIFFS_TRUNC;
|
||||
else if(c_strcmp(mode, "r")==0)
|
||||
return SPIFFS_RDONLY;
|
||||
else if(c_strcmp(mode, "a")==0)
|
||||
return SPIFFS_WRONLY|SPIFFS_CREAT|SPIFFS_APPEND;
|
||||
else
|
||||
return SPIFFS_RDONLY;
|
||||
} else if (c_strlen(mode)==2){
|
||||
if(c_strcmp(mode,"r+")==0)
|
||||
return SPIFFS_RDWR;
|
||||
else if(c_strcmp(mode, "w+")==0)
|
||||
return SPIFFS_RDWR|SPIFFS_CREAT|SPIFFS_TRUNC;
|
||||
else if(c_strcmp(mode, "a+")==0)
|
||||
return SPIFFS_RDWR|SPIFFS_CREAT|SPIFFS_APPEND;
|
||||
else
|
||||
return SPIFFS_RDONLY;
|
||||
} else {
|
||||
return SPIFFS_RDONLY;
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// filesystem functions
|
||||
//
|
||||
static vfs_file *myspiffs_vfs_open( const char *name, const char *mode ) {
|
||||
struct myvfs_file *fd;
|
||||
int flags = fs_mode2flag( mode );
|
||||
|
||||
if (fd = (struct myvfs_file *)c_malloc( sizeof( struct myvfs_file ) )) {
|
||||
if (0 < (fd->fh = SPIFFS_open( &fs, name, flags, 0 ))) {
|
||||
fd->vfs_file.fs_type = VFS_FS_SPIFFS;
|
||||
fd->vfs_file.fns = &myspiffs_file_fns;
|
||||
return (vfs_file *)fd;
|
||||
} else {
|
||||
c_free( fd );
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static vfs_dir *myspiffs_vfs_opendir( const char *name ){
|
||||
struct myvfs_dir *dd;
|
||||
|
||||
if (dd = (struct myvfs_dir *)c_malloc( sizeof( struct myvfs_dir ) )) {
|
||||
if (SPIFFS_opendir( &fs, name, &(dd->d) )) {
|
||||
dd->vfs_dir.fs_type = VFS_FS_SPIFFS;
|
||||
dd->vfs_dir.fns = &myspiffs_dd_fns;
|
||||
return (vfs_dir *)dd;
|
||||
} else {
|
||||
c_free( dd );
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static vfs_item *myspiffs_vfs_stat( const char *name ) {
|
||||
struct myvfs_stat *s;
|
||||
|
||||
if (s = (struct myvfs_stat *)c_malloc( sizeof( struct myvfs_stat ) )) {
|
||||
if (0 <= SPIFFS_stat( &fs, name, &(s->s) )) {
|
||||
s->vfs_item.fs_type = VFS_FS_SPIFFS;
|
||||
s->vfs_item.fns = &myspiffs_item_fns;
|
||||
return (vfs_item *)s;
|
||||
} else {
|
||||
c_free( s );
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static sint32_t myspiffs_vfs_remove( const char *name ) {
|
||||
return SPIFFS_remove( &fs, name );
|
||||
}
|
||||
|
||||
static sint32_t myspiffs_vfs_rename( const char *oldname, const char *newname ) {
|
||||
return SPIFFS_rename( &fs, oldname, newname );
|
||||
}
|
||||
|
||||
static sint32_t myspiffs_vfs_fsinfo( uint32_t *total, uint32_t *used ) {
|
||||
return SPIFFS_info( &fs, total, used );
|
||||
}
|
||||
|
||||
static sint32_t myspiffs_vfs_fscfg( uint32_t *phys_addr, uint32_t *phys_size ) {
|
||||
*phys_addr = fs.cfg.phys_addr;
|
||||
*phys_size = fs.cfg.phys_size;
|
||||
return VFS_RES_OK;
|
||||
}
|
||||
|
||||
static vfs_vol *myspiffs_vfs_mount( const char *name, int num ) {
|
||||
// volume descriptor not supported, just return TRUE / FALSE
|
||||
return myspiffs_mount() ? (vfs_vol *)1 : NULL;
|
||||
}
|
||||
|
||||
static sint32_t myspiffs_vfs_format( void ) {
|
||||
return myspiffs_format();
|
||||
}
|
||||
|
||||
static sint32_t myspiffs_vfs_errno( void ) {
|
||||
return SPIFFS_errno( &fs );
|
||||
}
|
||||
|
||||
static void myspiffs_vfs_clearerr( void ) {
|
||||
SPIFFS_clearerr( &fs );
|
||||
}
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// VFS interface functions
|
||||
//
|
||||
vfs_fs_fns *myspiffs_realm( const char *inname, char **outname, int set_current_drive ) {
|
||||
if (inname[0] == '/') {
|
||||
size_t idstr_len = c_strlen( MY_LDRV_ID );
|
||||
// logical drive is specified, check if it's our id
|
||||
if (0 == c_strncmp( &(inname[1]), MY_LDRV_ID, idstr_len )) {
|
||||
*outname = (char *)&(inname[1 + idstr_len]);
|
||||
if (*outname[0] == '/') {
|
||||
// skip leading /
|
||||
(*outname)++;
|
||||
}
|
||||
|
||||
if (set_current_drive) is_current_drive = TRUE;
|
||||
return &myspiffs_fs_fns;
|
||||
}
|
||||
} else {
|
||||
// no logical drive in patchspec, are we current drive?
|
||||
if (is_current_drive) {
|
||||
*outname = (char *)inname;
|
||||
return &myspiffs_fs_fns;
|
||||
}
|
||||
}
|
||||
|
||||
if (set_current_drive) is_current_drive = FALSE;
|
||||
return NULL;
|
||||
}
|
||||
|
|
|
@ -14,6 +14,8 @@
|
|||
#include "nodemcu_spiffs.h"
|
||||
// ----------- >8 ------------
|
||||
|
||||
#include "user_config.h"
|
||||
|
||||
// compile time switches
|
||||
|
||||
// Set generic spiffs debug output call.
|
||||
|
@ -99,7 +101,7 @@
|
|||
// zero-termination character, meaning maximum string of characters
|
||||
// can at most be SPIFFS_OBJ_NAME_LEN - 1.
|
||||
#ifndef SPIFFS_OBJ_NAME_LEN
|
||||
#define SPIFFS_OBJ_NAME_LEN (32)
|
||||
#define SPIFFS_OBJ_NAME_LEN (FS_OBJ_NAME_LEN+1)
|
||||
#endif
|
||||
|
||||
// Size of buffer allocated on stack used when copying data.
|
||||
|
|
|
@ -12,7 +12,8 @@
|
|||
#include "platform.h"
|
||||
#include "c_string.h"
|
||||
#include "c_stdlib.h"
|
||||
#include "flash_fs.h"
|
||||
#include "c_stdio.h"
|
||||
#include "vfs.h"
|
||||
#include "flash_api.h"
|
||||
#include "user_interface.h"
|
||||
#include "user_exceptions.h"
|
||||
|
@ -103,11 +104,11 @@ void nodemcu_init(void)
|
|||
espconn_secure_set_size(ESPCONN_CLIENT, SSL_BUFFER_SIZE);
|
||||
#endif
|
||||
|
||||
#if defined ( BUILD_SPIFFS )
|
||||
if (!fs_mount()) {
|
||||
#ifdef BUILD_SPIFFS
|
||||
if (!vfs_mount("/FLASH", 0)) {
|
||||
// Failed to mount -- try reformat
|
||||
c_printf("Formatting file system. Please wait...\n");
|
||||
if (!fs_format()) {
|
||||
if (!vfs_format()) {
|
||||
NODE_ERR( "\n*** ERROR ***: unable to format. FS might be compromised.\n" );
|
||||
NODE_ERR( "It is advised to re-flash the NodeMCU image.\n" );
|
||||
}
|
||||
|
|
|
@ -5,10 +5,44 @@
|
|||
|
||||
The file module provides access to the file system and its individual files.
|
||||
|
||||
The file system is a flat file system, with no notion of directories/folders.
|
||||
The file system is a flat file system, with no notion of subdirectories/folders.
|
||||
|
||||
Only one file can be open at any given time.
|
||||
|
||||
Besides the SPIFFS file system on internal flash, this module can also access FAT partitions on an external SD card is [FatFS is enabled](../sdcard.md).
|
||||
|
||||
```lua
|
||||
-- open file in flash:
|
||||
if file.open("init.lua") then
|
||||
print(file.read())
|
||||
file.close()
|
||||
end
|
||||
|
||||
-- or with full pathspec
|
||||
file.open("/FLASH/init.lua")
|
||||
|
||||
-- open file on SD card
|
||||
if file.open("/SD0/somefile.txt") then
|
||||
print(file.read())
|
||||
file.close()
|
||||
end
|
||||
```
|
||||
|
||||
## file.chdir()
|
||||
|
||||
Change current directory (and drive). This will be used when no drive/directory is prepended to filenames.
|
||||
|
||||
Current directory defaults to the root of internal SPIFFS (`/FLASH`) after system start.
|
||||
|
||||
#### Syntax
|
||||
`file.chdir(dir)`
|
||||
|
||||
#### Parameters
|
||||
`dir` directory name - `/FLASH`, `/SD0`, `/SD1`, etc.
|
||||
|
||||
#### Returns
|
||||
`true` on success, `false` otherwise
|
||||
|
||||
## file.close()
|
||||
|
||||
Closes the open file, if any.
|
||||
|
@ -25,9 +59,10 @@ none
|
|||
#### Example
|
||||
```lua
|
||||
-- open 'init.lua', print the first line.
|
||||
file.open("init.lua", "r")
|
||||
print(file.readline())
|
||||
file.close()
|
||||
if file.open("init.lua", "r") then
|
||||
print(file.readline())
|
||||
file.close()
|
||||
end
|
||||
```
|
||||
#### See also
|
||||
[`file.open()`](#fileopen)
|
||||
|
@ -76,13 +111,14 @@ none
|
|||
#### Example
|
||||
```lua
|
||||
-- open 'init.lua' in 'a+' mode
|
||||
file.open("init.lua", "a+")
|
||||
-- write 'foo bar' to the end of the file
|
||||
file.write('foo bar')
|
||||
file.flush()
|
||||
-- write 'baz' too
|
||||
file.write('baz')
|
||||
file.close()
|
||||
if file.open("init.lua", "a+") then
|
||||
-- write 'foo bar' to the end of the file
|
||||
file.write('foo bar')
|
||||
file.flush()
|
||||
-- write 'baz' too
|
||||
file.write('baz')
|
||||
file.close()
|
||||
end
|
||||
```
|
||||
#### See also
|
||||
[`file.close()`](#fileclose)
|
||||
|
@ -91,6 +127,8 @@ file.close()
|
|||
|
||||
Format the file system. Completely erases any existing file system and writes a new one. Depending on the size of the flash chip in the ESP, this may take several seconds.
|
||||
|
||||
Not supported for SD cards.
|
||||
|
||||
#### Syntax
|
||||
`file.format()`
|
||||
|
||||
|
@ -107,6 +145,8 @@ none
|
|||
|
||||
Returns the flash address and physical size of the file system area, in bytes.
|
||||
|
||||
Not supported for SD cards.
|
||||
|
||||
#### Syntax
|
||||
`file.fscfg()`
|
||||
|
||||
|
@ -124,7 +164,7 @@ print(string.format("0x%x", file.fscfg()))
|
|||
|
||||
## file.fsinfo()
|
||||
|
||||
Return size information for the file system, in bytes.
|
||||
Return size information for the file system. The unit is Byte for SPIFFS and kByte for FatFS.
|
||||
|
||||
#### Syntax
|
||||
`file.fsinfo()`
|
||||
|
@ -142,7 +182,7 @@ none
|
|||
```lua
|
||||
-- get file system info
|
||||
remaining, used, total=file.fsinfo()
|
||||
print("\nFile system info:\nTotal : "..total.." Bytes\nUsed : "..used.." Bytes\nRemain: "..remaining.." Bytes\n")
|
||||
print("\nFile system info:\nTotal : "..total.." (k)Bytes\nUsed : "..used.." (k)Bytes\nRemain: "..remaining.." (k)Bytes\n")
|
||||
```
|
||||
|
||||
## file.list()
|
||||
|
@ -166,6 +206,60 @@ for k,v in pairs(l) do
|
|||
end
|
||||
```
|
||||
|
||||
## file.mount()
|
||||
|
||||
Mounts a FatFs volume on SD card.
|
||||
|
||||
Not supported for internal flash.
|
||||
|
||||
#### Syntax
|
||||
`file.mount(ldrv[, pin])`
|
||||
|
||||
#### Parameters
|
||||
- `ldrv` name of the logical drive, `SD0:`, `SD1:`, etc.
|
||||
- `pin` 1~12, IO index for SS/CS, defaults to 8 if omitted.
|
||||
|
||||
#### Returns
|
||||
Volume object
|
||||
|
||||
#### Example
|
||||
```lua
|
||||
vol = file.mount("SD0:")
|
||||
vol:umount()
|
||||
```
|
||||
|
||||
## file.on()
|
||||
|
||||
Registers callback functions.
|
||||
|
||||
Trigger events are:
|
||||
- `rtc` deliver current date & time to the file system. Function is expected to return a table containing the fields `year`, `mon`, `day`, `hour`, `min`, `sec` of current date and time. Not supported for internal flash.
|
||||
|
||||
#### Syntax
|
||||
`file.on(event[, function()])`
|
||||
|
||||
#### Parameters
|
||||
- `event` string
|
||||
- `function()` callback function. Unregisters the callback if `function()` is omitted.
|
||||
|
||||
#### Returns
|
||||
`nil`
|
||||
|
||||
#### Example
|
||||
```lua
|
||||
sntp.sync(server_ip,
|
||||
function()
|
||||
print("sntp time sync ok")
|
||||
file.on("rtc",
|
||||
function()
|
||||
return rtctime.epoch2cal(rtctime.get())
|
||||
end)
|
||||
end)
|
||||
```
|
||||
|
||||
#### See also
|
||||
[`rtctime.epoch2cal()`](rtctime.md#rtctimepoch2cal)
|
||||
|
||||
## file.open()
|
||||
|
||||
Opens a file for access, potentially creating it (for write modes).
|
||||
|
@ -191,9 +285,10 @@ When done with the file, it must be closed using `file.close()`.
|
|||
#### Example
|
||||
```lua
|
||||
-- open 'init.lua', print the first line.
|
||||
file.open("init.lua", "r")
|
||||
print(file.readline())
|
||||
file.close()
|
||||
if file.open("init.lua", "r") then
|
||||
print(file.readline())
|
||||
file.close()
|
||||
end
|
||||
```
|
||||
#### See also
|
||||
- [`file.close()`](#fileclose)
|
||||
|
@ -218,14 +313,16 @@ File content as a string, or nil when EOF
|
|||
#### Example
|
||||
```lua
|
||||
-- print the first line of 'init.lua'
|
||||
file.open("init.lua", "r")
|
||||
print(file.read('\n'))
|
||||
file.close()
|
||||
if file.open("init.lua", "r") then
|
||||
print(file.read('\n'))
|
||||
file.close()
|
||||
end
|
||||
|
||||
-- print the first 5 bytes of 'init.lua'
|
||||
file.open("init.lua", "r")
|
||||
print(file.read(5))
|
||||
file.close()
|
||||
if file.open("init.lua", "r") then
|
||||
print(file.read(5))
|
||||
file.close()
|
||||
end
|
||||
```
|
||||
|
||||
#### See also
|
||||
|
@ -248,9 +345,10 @@ File content in string, line by line, including EOL('\n'). Return `nil` when EOF
|
|||
#### Example
|
||||
```lua
|
||||
-- print the first line of 'init.lua'
|
||||
file.open("init.lua", "r")
|
||||
print(file.readline())
|
||||
file.close()
|
||||
if file.open("init.lua", "r") then
|
||||
print(file.readline())
|
||||
file.close()
|
||||
end
|
||||
```
|
||||
#### See also
|
||||
- [`file.open()`](#fileopen)
|
||||
|
@ -320,11 +418,12 @@ the resulting file position, or `nil` on error
|
|||
|
||||
#### Example
|
||||
```lua
|
||||
file.open("init.lua", "r")
|
||||
-- skip the first 5 bytes of the file
|
||||
file.seek("set", 5)
|
||||
print(file.readline())
|
||||
file.close()
|
||||
if file.open("init.lua", "r") then
|
||||
-- skip the first 5 bytes of the file
|
||||
file.seek("set", 5)
|
||||
print(file.readline())
|
||||
file.close()
|
||||
end
|
||||
```
|
||||
#### See also
|
||||
[`file.open()`](#fileopen)
|
||||
|
@ -345,10 +444,11 @@ Write a string to the open file.
|
|||
#### Example
|
||||
```lua
|
||||
-- open 'init.lua' in 'a+' mode
|
||||
file.open("init.lua", "a+")
|
||||
-- write 'foo bar' to the end of the file
|
||||
file.write('foo bar')
|
||||
file.close()
|
||||
if file.open("init.lua", "a+") then
|
||||
-- write 'foo bar' to the end of the file
|
||||
file.write('foo bar')
|
||||
file.close()
|
||||
end
|
||||
```
|
||||
|
||||
#### See also
|
||||
|
@ -371,10 +471,11 @@ Write a string to the open file and append '\n' at the end.
|
|||
#### Example
|
||||
```lua
|
||||
-- open 'init.lua' in 'a+' mode
|
||||
file.open("init.lua", "a+")
|
||||
-- write 'foo bar' to the end of the file
|
||||
file.writeline('foo bar')
|
||||
file.close()
|
||||
if file.open("init.lua", "a+") then
|
||||
-- write 'foo bar' to the end of the file
|
||||
file.writeline('foo bar')
|
||||
file.close()
|
||||
end
|
||||
```
|
||||
|
||||
#### See also
|
||||
|
|
|
@ -0,0 +1,71 @@
|
|||
# FAT File System on SD Card
|
||||
|
||||
Accessing files on external SD cards is currently only supported from the `file` module. This imposes the same overall restrictions of internal SPIFFS to SD cards:
|
||||
- only one file can be opened at a time
|
||||
- no support for sub-folders
|
||||
- no timestamps
|
||||
- no file attributes (read-only, system, etc.)
|
||||
|
||||
Work is in progress to extend the `file` API with support for the missing features.
|
||||
|
||||
## Enabling FatFs
|
||||
|
||||
The FAT file system is implemented by [Chan's FatFs](http://elm-chan.org/fsw/ff/00index_e.html) version [R0.12a](http://elm-chan.org/fsw/ff/ff12a.zip). It's disabled by default to save memory space and has to be enabled before compiling the firmware:
|
||||
|
||||
Uncomment `#define BUILD_FATFS` in [`user_config.h`](../../app/include/user_config.h).
|
||||
|
||||
## SD Card connection
|
||||
|
||||
The SD card is operated in SPI mode, thus the card has to be wired to the respective ESP pins of the HSPI interface. There are several naming schemes used on different adapters - the following list shows alternative terms:
|
||||
- `CK, CLK, SCLK` to pin5 / GPIO14
|
||||
- `DO, DAT0, MISO` to pin 6 / GPIO12
|
||||
- `DI, CMD, MOSI` to pin 7 / GPIO13
|
||||
- `CS, DAT3, SS` to pin 8 / GPIO15 recommended
|
||||
- `VCC, VDD` to 3V3 supply
|
||||
- `VSS, GND` to common ground
|
||||
|
||||
Connection of `SS/CS` can be done to any of the GPIOs on pins 1 to 12. This allows coexistence of the SD card with other SPI slaves on the same bus. There's no support for detection of card presence or the write protection switch. These would need to be handled as additional GPIOs in the user application.
|
||||
|
||||
!!! caution
|
||||
|
||||
The adapter does not require level shifters since SD and ESP are supposed to be powered with the same voltage. If your specific model contains level shifters then make sure that both sides can be operated at 3V3.
|
||||
|
||||
<img src="../img/micro_sd.jpg" alt="1:1 micro-sd adapter" width="200"/>
|
||||
<img src="../img/micro_sd_shield.jpg" alt="micro-sd shield" width="200"/>
|
||||
|
||||
## Lua bindings
|
||||
|
||||
Before mounting the volume(s) on the SD card, you need to initialize the SPI interface from Lua.
|
||||
|
||||
```lua
|
||||
spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 8)
|
||||
|
||||
-- initialize other spi slaves
|
||||
|
||||
-- then mount the sd
|
||||
-- note: the card initialization process during `file.mount()` will set spi divider temporarily to 200 (400 kHz)
|
||||
-- it's reverted back to the current user setting before `file.mount()` finishes
|
||||
vol = file.mount("/SD0", 8) -- 2nd parameter is optional for non-standard SS/CS pin
|
||||
if not vol then
|
||||
print("retry mounting")
|
||||
vol = file.mount("/SD0", 8)
|
||||
if not vol then
|
||||
error("mount failed")
|
||||
end
|
||||
end
|
||||
file.open("/SD0/path/to/somefile")
|
||||
print(file.read())
|
||||
file.close()
|
||||
```
|
||||
|
||||
!!! note
|
||||
|
||||
If the card doesn't work when calling `file.mount()` for the first time then re-try the command. It's possible that certain cards time out during the first initialization after power-up.
|
||||
|
||||
The logical drives are mounted at the root of a unified directory tree where the mount points distinguish between internal flash (`/FLASH`) and the card's paritions (`/SD0` to `/SD3`). Files are accessed via either the absolute hierarchical path or relative to the current working directory. It defaults to `/FLASH` and can be changed with `file.chdir(path)`.
|
||||
|
||||
Subdirectories are supported on FAT volumes only.
|
||||
|
||||
## Multiple partitions / multiple cards
|
||||
|
||||
The mapping from logical volumes (eg. `/SD0`) to partitions on an SD card is defined in [`fatfs_config.h`](../../app/include/fatfs_config.h). More volumes can be added to the `VolToPart` array with any combination of physical drive number (aka SS/CS pin) and partition number. Their names have to be added to `_VOLUME_STRS` in [`ffconf.h`](../../app/fatfs/ffconf.h) as well.
|
Binary file not shown.
After Width: | Height: | Size: 36 KiB |
Binary file not shown.
After Width: | Height: | Size: 125 KiB |
|
@ -26,7 +26,8 @@ pages:
|
|||
- Home: 'en/index.md'
|
||||
- Building the firmware: 'en/build.md'
|
||||
- Flashing the firmware: 'en/flash.md'
|
||||
- Filesystem notes: 'en/spiffs.md'
|
||||
- Internal filesystem notes: 'en/spiffs.md'
|
||||
- Filesystem on SD card: 'en/sdcard.md'
|
||||
- Uploading code: 'en/upload.md'
|
||||
- FAQs:
|
||||
- Lua Developer FAQ: 'en/lua-developer-faq.md'
|
||||
|
|
|
@ -11,6 +11,9 @@ cat user_modules.h
|
|||
|
||||
# enable SSL
|
||||
sed -i.bak 's@//#define CLIENT_SSL_ENABLE@#define CLIENT_SSL_ENABLE@' user_config.h
|
||||
|
||||
# enable FATFS
|
||||
sed -i 's@//#define BUILD_FATFS@#define BUILD_FATFS@' user_config.h
|
||||
cat user_config.h
|
||||
|
||||
cd "$TRAVIS_BUILD_DIR"/ld || exit
|
||||
|
|
Loading…
Reference in New Issue