Просмотр исходного кода

update: 升级并整理fatfs,在win32下ramdisk正常,其他bsp待测试tf挂载

Wendal Chen 4 лет назад
Родитель
Сommit
41c6adaeaa

+ 25 - 0
bsp/win32/module_test/006fatfs.lua

@@ -0,0 +1,25 @@
+
+
+local sys = require "sys"
+
+log.info("sys", "from win32")
+
+sys.taskInit(function ()
+    sys.wait(1000)
+
+    if fatfs ~= nil then
+        fatfs.debug(1)
+        fatfs.mount("ram", 64*1024)
+        fatfs.mkfs("ram")
+        sys.wait(100)
+        local data, err = fatfs.getfree("ram")
+        if data then
+            log.info("fatfs", "ramdisk", json.encode(data))
+        else
+            log.info("fatfs", "ramdisk", "err", err)
+        end
+    end
+    os.exit(0)
+end)
+
+sys.run()

+ 10 - 0
bsp/win32/port/luat_base_win32.c

@@ -1,11 +1,13 @@
 #include "luat_base.h"
 #include "luat_msgbus.h"
 #include "luat_fs.h"
+#include "luat_timer.h"
 #include <stdlib.h>
 
 LUAMOD_API int luaopen_win32( lua_State *L );
 int luaopen_lfs(lua_State * L);
 int luaopen_rs232_core(lua_State * L);
+int luaopen_fatfs(lua_State * L);
 
 static const luaL_Reg loadedlibs[] = {
   {"_G", luaopen_base}, // _G
@@ -35,6 +37,7 @@ static const luaL_Reg loadedlibs[] = {
 //   {"rs232.core", luaopen_rs232_core},
 #endif
   {"crypto", luaopen_crypto},
+  {"fatfs", luaopen_fatfs},
   {NULL, NULL}
 };
 
@@ -110,6 +113,13 @@ void luat_os_standy(int timeout) {
     return; // nop
 }
 
+void luat_ota_reboot(int timeout) {
+  if (timeout > 0)
+    luat_timer_mdelay(timeout * 1000);
+  exit(0);
+}
+
+
 //--------------------------------------------------------------------------------
 // for freertos
 #include "FreeRTOS.h"

+ 19 - 3
luat/packages/fatfs/diskio.h

@@ -1,5 +1,5 @@
 /*-----------------------------------------------------------------------/
-/  Low level disk interface modlue include file   (C)ChaN, 2014          /
+/  Low level disk interface modlue include file   (C)ChaN, 2019          /
 /-----------------------------------------------------------------------*/
 
 #ifndef _DISKIO_DEFINED
@@ -28,10 +28,25 @@ typedef enum {
 
 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_read (BYTE pdrv, BYTE* buff, LBA_t sector, UINT count);
+DRESULT disk_write (BYTE pdrv, const BYTE* buff, LBA_t sector, UINT count);
 DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff);
 
+typedef struct block_disk_opts {
+    DSTATUS (*initialize) (void* userdata);
+	DSTATUS (*status) (void* userdata);
+	DRESULT (*read) (void* userdata, BYTE* buff, LBA_t sector, UINT count);
+	DRESULT (*write) (void* userdata, const BYTE* buff, LBA_t sector, UINT count);
+	DRESULT (*ioctl) (void* userdata, BYTE cmd, void* buff);
+}block_disk_opts_t;
+
+typedef struct block_disk {
+	void* userdata;
+	const block_disk_opts_t* opts;
+}block_disk_t;
+
+DRESULT diskio_open(BYTE pdrv, block_disk_t * disk);
+DRESULT diskio_close(BYTE pdrv);
 
 /* Disk Status Bits (DSTATUS) */
 
@@ -77,6 +92,7 @@ DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff);
 #define CT_SDC		(CT_SD1|CT_SD2)	/* SD */
 #define CT_BLOCK	0x08		/* Block addressing */
 
+
 #ifdef __cplusplus
 }
 #endif

+ 105 - 0
luat/packages/fatfs/diskio_impl.c

@@ -0,0 +1,105 @@
+/*-----------------------------------------------------------------------*/
+/* 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 "luat_base.h"
+#include "luat_spi.h"
+#include "luat_timer.h"
+#include "luat_gpio.h"
+#include "lauxlib.h"
+
+#include "ff.h"			/* Obtains integer types */
+#include "diskio.h"		/* Declarations of disk functions */
+
+#define LUAT_LOG_TAG "luat.fatfs"
+#include "luat_log.h"
+
+BYTE FATFS_DEBUG = 0; // debug log, 0 -- disable , 1 -- enable
+BYTE FATFS_SPI_ID = 0; // 0 -- SPI_1, 1 -- SPI_2
+BYTE FATFS_SPI_CS = 3; // GPIO 3
+
+static block_disk_t disks[FF_VOLUMES+1] = {0};
+
+DSTATUS disk_initialize (BYTE pdrv) {
+	if (FATFS_DEBUG)
+		LLOGD("disk_initialize >> %d", pdrv);
+	if (pdrv >= FF_VOLUMES || disks[pdrv].userdata == NULL) {
+		return RES_NOTRDY;
+	}
+	return RES_OK;
+}
+
+DSTATUS disk_status (BYTE pdrv) {
+	if (FATFS_DEBUG)
+		LLOGD("disk_status >> %d", pdrv);
+	if (pdrv >= FF_VOLUMES || disks[pdrv].userdata == NULL) {
+		return RES_NOTRDY;
+	}
+	return disks[pdrv].opts->status(disks[pdrv].userdata);
+}
+
+DRESULT disk_read (BYTE pdrv, BYTE* buff, LBA_t sector, UINT count) {
+	if (FATFS_DEBUG)
+		LLOGD("disk_read >> %d", pdrv);
+	if (pdrv >= FF_VOLUMES || disks[pdrv].userdata == NULL) {
+		return RES_NOTRDY;
+	}
+	return disks[pdrv].opts->read(disks[pdrv].userdata, buff, sector, count);
+}
+
+DRESULT disk_write (BYTE pdrv, const BYTE* buff, LBA_t sector, UINT count) {
+	//if (FATFS_DEBUG)
+	//	LLOGD("disk_write >> %d", pdrv);
+	if (pdrv >= FF_VOLUMES || disks[pdrv].userdata == NULL) {
+		return RES_NOTRDY;
+	}
+	return disks[pdrv].opts->write(disks[pdrv].userdata, buff, sector, count);
+}
+
+DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff) {
+	if (FATFS_DEBUG)
+		LLOGD("disk_ioctl >> %d %d", pdrv, cmd);
+	if (pdrv >= FF_VOLUMES || disks[pdrv].userdata == NULL) {
+		return RES_NOTRDY;
+	}
+	return disks[pdrv].opts->ioctl(disks[pdrv].userdata, cmd, buff);
+}
+
+DRESULT diskio_open(BYTE pdrv, block_disk_t * disk) {
+	if (FATFS_DEBUG)
+		LLOGD("disk_open >> %d", pdrv);
+	if (pdrv >= FF_VOLUMES || disks[pdrv].userdata != NULL) {
+		return RES_NOTRDY;
+	}
+	disks[pdrv].opts = disk->opts;
+	disks[pdrv].userdata = disk->userdata;
+	return RES_OK;
+}
+
+#include "luat_malloc.h"
+DRESULT diskio_close(BYTE pdrv) {
+	if (pdrv >= FF_VOLUMES || disks[pdrv].userdata == NULL) {
+		return RES_NOTRDY;
+	}
+	disks[pdrv].opts = NULL;
+	if (disks[pdrv].userdata)
+		luat_heap_free(disks[pdrv].userdata);
+	disks[pdrv].userdata = NULL;
+	return RES_OK;
+}
+
+DWORD get_fattime (void) {
+	
+	return ((2020UL-1980) << 25) /* Year = 2010 */
+			| (1UL << 21) /* Month = 11 */
+			| (1UL << 16) /* Day = 2 */
+			| (1U << 11) /* Hour = 15 */
+			| (0U << 5) /* Min = 0 */
+			| (0U >> 1) /* Sec = 0 */
+	;
+}

+ 136 - 0
luat/packages/fatfs/diskio_ramdisk.c

@@ -0,0 +1,136 @@
+
+#include "luat_base.h"
+#include "luat_spi.h"
+#include "luat_malloc.h"
+
+#define LUAT_LOG_TAG "ramdisk"
+#include "luat_log.h"
+
+#include "ff.h"
+#include "diskio.h"
+
+extern BYTE FATFS_DEBUG; // debug log, 0 -- disable , 1 -- enable
+
+typedef struct luat_ramdisk {
+    BYTE debug;
+    size_t len;
+    void* ptr;
+}luat_ramdisk_t;
+
+DSTATUS ramdisk_initialize (void* userdata);
+DSTATUS ramdisk_status (void* userdata);
+DRESULT ramdisk_read (void* userdata, BYTE* buff, LBA_t sector, UINT count);
+DRESULT ramdisk_write (void* userdata, const BYTE* buff, LBA_t sector, UINT count);
+DRESULT ramdisk_ioctl (void* userdata, BYTE cmd, void* buff);
+
+const block_disk_opts_t ramdisk_disk_opts = {
+    .initialize = ramdisk_initialize,
+    .status = ramdisk_status,
+    .read = ramdisk_read,
+    .write = ramdisk_write,
+    .ioctl = ramdisk_ioctl,
+};
+
+// TODO 补齐实现
+
+DSTATUS ramdisk_initialize (void* userdata) {
+    luat_ramdisk_t* disk = (luat_ramdisk_t*)userdata;
+    if (disk->ptr == NULL || disk->len < FF_MIN_SS) {
+        if (FATFS_DEBUG)
+            LLOGD("ramdisk initialize check error");
+        return RES_ERROR;
+    }
+    if (FATFS_DEBUG)
+        LLOGD("ramdisk initialize ok");
+    return RES_OK;
+};
+
+DSTATUS ramdisk_status (void* userdata) {
+    luat_ramdisk_t* disk = (luat_ramdisk_t*)userdata;
+    if (disk->ptr == NULL || disk->len < FF_MIN_SS){
+        if (FATFS_DEBUG)
+            LLOGD("ramdisk status check error");
+        return RES_ERROR;
+    }
+    return RES_OK;
+};
+
+DRESULT ramdisk_read (void* userdata, BYTE* buff, LBA_t sector, UINT count) {
+    luat_ramdisk_t* disk = (luat_ramdisk_t*)userdata;
+    if (disk->ptr == NULL || disk->len < FF_MIN_SS){
+        if (FATFS_DEBUG)
+            LLOGD("ramdisk read check error");
+        return RES_ERROR;
+    }
+    memcpy(buff, disk->ptr + (sector) * FF_MIN_SS, FF_MIN_SS*count);
+    return RES_OK;
+};
+
+DRESULT ramdisk_write (void* userdata, const BYTE* buff, LBA_t sector, UINT count) {
+    luat_ramdisk_t* disk = (luat_ramdisk_t*)userdata;
+    if (disk->ptr == NULL || disk->len < FF_MIN_SS){
+        if (FATFS_DEBUG)
+            LLOGD("ramdisk write check error");
+        return RES_ERROR;
+    }
+    //LLOGD("write(disk->ptr == %p) at %p , len = 0x%08X", disk->ptr, disk->ptr + (sector) * FF_MIN_SS, FF_MIN_SS*count);
+    memcpy(disk->ptr + (sector) * FF_MIN_SS, buff, FF_MIN_SS*count);
+    return RES_OK;
+};
+
+DRESULT ramdisk_ioctl (void* userdata, BYTE cmd, void* buff) {
+    luat_ramdisk_t* disk = (luat_ramdisk_t*)userdata;
+    if (disk->ptr == NULL || disk->len < FF_MIN_SS){
+        if (FATFS_DEBUG)
+            LLOGD("ramdisk ioctl check error");
+        return RES_ERROR;
+    }
+    if (FATFS_DEBUG)
+            LLOGD("ramdisk ioctl cmd %d", cmd);
+    switch (cmd) {
+        case CTRL_SYNC :
+            //*(DWORD*)buff = 0;
+            break;
+        case GET_SECTOR_COUNT:
+            *(DWORD*)buff = disk->len / FF_MIN_SS;
+            LLOGD("ramdisk GET_SECTOR_COUNT %d", disk->len / FF_MIN_SS);
+            break;
+        case GET_SECTOR_SIZE:
+            *(DWORD*)buff = FF_MIN_SS;
+            LLOGD("ramdisk GET_SECTOR_SIZE %d", FF_MIN_SS);
+            break;
+        case GET_BLOCK_SIZE:
+            *(DWORD*)buff = 1;
+            break;
+        default :
+            if (FATFS_DEBUG)
+                LLOGD("ramdisk unkown cmd %d", cmd);
+            return RES_ERROR;
+    }
+    return RES_OK;
+};
+
+DRESULT diskio_open_ramdisk(BYTE pdrv, size_t len) {
+    void* ptr = luat_heap_malloc(len + sizeof(luat_ramdisk_t));
+    if (ptr == NULL) {
+        LLOGW("luat_heap_malloc return NULL");
+        return RES_ERROR;
+    }
+    memset(ptr, 0, len + sizeof(luat_ramdisk_t));
+    luat_ramdisk_t* ramdisk = ptr;
+    ramdisk->ptr = ramdisk + sizeof(luat_ramdisk_t);
+    ramdisk->len = len;
+    ramdisk->debug = 0;
+
+    block_disk_t disk = {
+        .opts = &ramdisk_disk_opts,
+        .userdata = ramdisk,
+    };
+
+    DRESULT ret = diskio_open(pdrv, &disk);
+    if (ret != RES_OK) {
+        luat_heap_free(ptr);
+    }
+    return ret;
+}
+

+ 51 - 27
luat/packages/fatfs/diskio.c → luat/packages/fatfs/diskio_spitf.c

@@ -147,14 +147,14 @@ void rcvr_mmc (
 	#endif
 	//u8* buf2 = 0x00;
 	//u8** buf = &buf2;
-	u8 tmp[bc];
+	BYTE tmp[bc];
 	
 	for(size_t i = 0; i < bc; i++)
 	{
 		tmp[i] = 0xFF;
 	}
 	
-	s32 t = luat_spi_transfer(FATFS_SPI_ID, tmp, buff, bc);
+	DWORD t = luat_spi_transfer(FATFS_SPI_ID, tmp, buff, bc);
 	//s32 t = platform_spi_recv(0, buf, bc);
 	
 	//memcpy(buff, buf2, bc);
@@ -192,7 +192,7 @@ int wait_ready (void)	/* 1:OK, 0:Timeout */
 /*-----------------------------------------------------------------------*/
 
 static
-void deselect (void)
+void spi_cs_deselect (void)
 {
 	BYTE d;
 
@@ -209,7 +209,7 @@ void deselect (void)
 /*-----------------------------------------------------------------------*/
 
 static
-int select (void)	/* 1:OK, 0:Timeout */
+int spi_cs_select (void)	/* 1:OK, 0:Timeout */
 {
 	BYTE d;
 
@@ -219,7 +219,7 @@ int select (void)	/* 1:OK, 0:Timeout */
 	rcvr_mmc(&d, 1);	/* Dummy clock (force DO enabled) */
 	if (wait_ready()) return 1;	/* Wait for card ready */
 
-	deselect();
+	spi_cs_deselect();
 	return 0;			/* Failed */
 }
 
@@ -305,8 +305,8 @@ BYTE send_cmd (		/* Returns command response (bit7==1:Send failed)*/
 
 	/* Select the card and wait for ready except to stop multiple block read */
 	if (cmd != CMD12) {
-		deselect();
-		if (!select()) return 0xFF;
+		spi_cs_deselect();
+		if (!spi_cs_select()) return 0xFF;
 	}
 
 	/* Send a command packet */
@@ -344,11 +344,11 @@ BYTE send_cmd (		/* Returns command response (bit7==1:Send failed)*/
 /* Get Disk Status                                                       */
 /*-----------------------------------------------------------------------*/
 
-DSTATUS disk_status (
-	BYTE drv			/* Drive number (always 0) */
+DSTATUS spitf_status (
+	void* userdata
 )
 {
-	if (drv) return STA_NOINIT;
+	//if (drv) return STA_NOINIT;
 
 	return Stat;
 }
@@ -359,8 +359,8 @@ DSTATUS disk_status (
 /* Initialize Disk Drive                                                 */
 /*-----------------------------------------------------------------------*/
 
-DSTATUS disk_initialize (
-	BYTE drv		/* Physical drive nmuber (0) */
+DSTATUS spitf_initialize (
+	void* userdata
 )
 {
 	BYTE n, ty, cmd, buf[4];
@@ -368,7 +368,7 @@ DSTATUS disk_initialize (
 	DSTATUS s;
 
 
-	if (drv) return RES_NOTRDY;
+	//if (drv) return RES_NOTRDY;
 
 	//dly_us(10000);			/* 10ms */
 	//CS_INIT(); CS_H();		/* Initialize port pin tied to CS */
@@ -413,7 +413,7 @@ DSTATUS disk_initialize (
 	s = ty ? 0 : STA_NOINIT;
 	Stat = s;
 
-	deselect();
+	spi_cs_deselect();
 
 	//luat_spi_close(FATFS_SPI_ID);
 	//spi.setup(id,0,0,8,400*1000,1)
@@ -428,8 +428,8 @@ DSTATUS disk_initialize (
 /* Read Sector(s)                                                        */
 /*-----------------------------------------------------------------------*/
 
-DRESULT disk_read (
-	BYTE drv,			/* Physical drive nmuber (0) */
+DRESULT spitf_read (
+	void* userdata,
 	BYTE *buff,			/* Pointer to the data buffer to store read data */
 	DWORD sector,		/* Start sector number (LBA) */
 	UINT count			/* Sector count (1..128) */
@@ -438,7 +438,7 @@ DRESULT disk_read (
 	BYTE cmd;
 
 
-	if (disk_status(drv) & STA_NOINIT) return RES_NOTRDY;
+	if (spitf_status(userdata) & STA_NOINIT) return RES_NOTRDY;
 	if (!(CardType & CT_BLOCK)) sector *= 512;	/* Convert LBA to byte address if needed */
 
 	cmd = count > 1 ? CMD18 : CMD17;			/*  READ_MULTIPLE_BLOCK : READ_SINGLE_BLOCK */
@@ -449,7 +449,7 @@ DRESULT disk_read (
 		} while (--count);
 		if (cmd == CMD18) send_cmd(CMD12, 0);	/* STOP_TRANSMISSION */
 	}
-	deselect();
+	spi_cs_deselect();
 
 	return count ? RES_ERROR : RES_OK;
 }
@@ -460,14 +460,14 @@ DRESULT disk_read (
 /* Write Sector(s)                                                       */
 /*-----------------------------------------------------------------------*/
 
-DRESULT disk_write (
-	BYTE drv,			/* Physical drive nmuber (0) */
+DRESULT spitf_write (
+	void* userdata,
 	const BYTE *buff,	/* Pointer to the data to be written */
 	DWORD sector,		/* Start sector number (LBA) */
 	UINT count			/* Sector count (1..128) */
 )
 {
-	if (disk_status(drv) & STA_NOINIT) return RES_NOTRDY;
+	if (spitf_status(userdata) & STA_NOINIT) return RES_NOTRDY;
 	if (!(CardType & CT_BLOCK)) sector *= 512;	/* Convert LBA to byte address if needed */
 
 	if (count == 1) {	/* Single block write */
@@ -486,7 +486,7 @@ DRESULT disk_write (
 				count = 1;
 		}
 	}
-	deselect();
+	spi_cs_deselect();
 
 	return count ? RES_ERROR : RES_OK;
 }
@@ -496,8 +496,8 @@ DRESULT disk_write (
 /* Miscellaneous Functions                                               */
 /*-----------------------------------------------------------------------*/
 
-DRESULT disk_ioctl (
-	BYTE drv,		/* Physical drive nmuber (0) */
+DRESULT spitf_ioctl (
+	void* userdata,
 	BYTE ctrl,		/* Control code */
 	void *buff		/* Buffer to send/receive control data */
 )
@@ -507,12 +507,12 @@ DRESULT disk_ioctl (
 	DWORD cs;
 
 
-	if (disk_status(drv) & STA_NOINIT) return RES_NOTRDY;	/* Check if card is in the socket */
+	if (spitf_status(userdata) & STA_NOINIT) return RES_NOTRDY;	/* Check if card is in the socket */
 
 	res = RES_ERROR;
 	switch (ctrl) {
 		case CTRL_SYNC :		/* Make sure that no pending write process */
-			if (select()) res = RES_OK;
+			if (spi_cs_select()) res = RES_OK;
 			break;
 
 		case GET_SECTOR_COUNT :	/* Get number of sectors on the disk (DWORD) */
@@ -538,11 +538,35 @@ DRESULT disk_ioctl (
 			res = RES_PARERR;
 	}
 
-	deselect();
+	spi_cs_deselect();
 
 	return res;
 }
 
+// DSTATUS spitf_initialize (void* userdata);
+// DSTATUS ramdisk_status (void* userdata);
+// DRESULT ramdisk_read (void* userdata, BYTE* buff, LBA_t sector, UINT count);
+// DRESULT ramdisk_write (void* userdata, const BYTE* buff, LBA_t sector, UINT count);
+// DRESULT ramdisk_ioctl (void* userdata, BYTE cmd, void* buff);
+
+const block_disk_opts_t spitf_disk_opts = {
+    .initialize = spitf_initialize,
+    .status = spitf_status,
+    .read = spitf_read,
+    .write = spitf_write,
+    .ioctl = spitf_ioctl,
+};
+
+DRESULT diskio_open_spitf(BYTE pdrv, BYTE id, BYTE cs) {
+	block_disk_t disk = {
+        .opts = &spitf_disk_opts,
+        .userdata = "spitf",
+    };
+	FATFS_SPI_ID = id;
+	FATFS_SPI_CS = cs;
+	return diskio_open(pdrv, &disk);
+}
+
 //static DWORD get_fattime() {
 //	how to get?
 //}

Разница между файлами не показана из-за своего большого размера
+ 205 - 236
luat/packages/fatfs/ff.c


+ 64 - 50
luat/packages/fatfs/ff.h

@@ -1,8 +1,8 @@
 /*----------------------------------------------------------------------------/
-/  FatFs - Generic FAT Filesystem module  R0.13c                              /
+/  FatFs - Generic FAT Filesystem module  R0.14b                              /
 /-----------------------------------------------------------------------------/
 /
-/ Copyright (C) 2018, ChaN, all right reserved.
+/ Copyright (C) 2021, 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
@@ -20,12 +20,12 @@
 
 
 #ifndef FF_DEFINED
-#define FF_DEFINED	86604	/* Revision ID */
+#define FF_DEFINED	86631	/* Revision ID */
 
 #ifdef __cplusplus
 extern "C" {
 #endif
-//#include "platform.h"
+
 #include "ffconf.h"		/* FatFs configuration options */
 
 #if FF_DEFINED != FFCONF_DEF
@@ -35,54 +35,57 @@ extern "C" {
 
 /* Integer types used for FatFs API */
 
-#if defined(_WIN32)	/* Main development platform */
+#if defined(_WIN32)		/* Windows VC++ (for development only) */
 #define FF_INTDEF 2
 #include <windows.h>
 typedef unsigned __int64 QWORD;
+#include <float.h>
+#define isnan(v) _isnan(v)
+#define isinf(v) (!_finite(v))
+
 #elif (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__cplusplus)	/* C99 or later */
 #define FF_INTDEF 2
 #include <stdint.h>
 typedef unsigned int	UINT;	/* int must be 16-bit or 32-bit */
 typedef unsigned char	BYTE;	/* char must be 8-bit */
 typedef uint16_t		WORD;	/* 16-bit unsigned integer */
-typedef uint16_t		WCHAR;	/* 16-bit unsigned integer */
 typedef uint32_t		DWORD;	/* 32-bit unsigned integer */
 typedef uint64_t		QWORD;	/* 64-bit unsigned integer */
-typedef uint8_t 		u8;
-typedef int32_t			s32;
-typedef uint32_t		u32;
+typedef WORD			WCHAR;	/* UTF-16 character type */
+
 #else  	/* Earlier than C99 */
 #define FF_INTDEF 1
 typedef unsigned int	UINT;	/* int must be 16-bit or 32-bit */
 typedef unsigned char	BYTE;	/* char must be 8-bit */
 typedef unsigned short	WORD;	/* 16-bit unsigned integer */
-typedef unsigned short	WCHAR;	/* 16-bit unsigned integer */
 typedef unsigned long	DWORD;	/* 32-bit unsigned integer */
+typedef WORD			WCHAR;	/* UTF-16 character type */
 #endif
 
 
-/* Definitions of volume management */
+/* Type of file size and LBA variables */
 
-#if FF_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 */
+#if FF_FS_EXFAT
+#if FF_INTDEF != 2
+#error exFAT feature wants C99 or later
 #endif
-
-#if FF_STR_VOLUME_ID
-#ifndef FF_VOLUME_STRS
-extern const char* VolumeStr[FF_VOLUMES];	/* User defied volume ID */
+typedef QWORD FSIZE_t;
+#if FF_LBA64
+typedef QWORD LBA_t;
+#else
+typedef DWORD LBA_t;
 #endif
+#else
+#if FF_LBA64
+#error exFAT needs to be enabled when enable 64-bit LBA
+#endif
+typedef DWORD FSIZE_t;
+typedef DWORD LBA_t;
 #endif
 
 
 
-/* Type of path name strings on FatFs API */
-
-#ifndef _INC_TCHAR
-#define _INC_TCHAR
+/* Type of path name strings on FatFs API (TCHAR) */
 
 #if FF_USE_LFN && FF_LFN_UNICODE == 1 	/* Unicode in UTF-16 encoding */
 typedef WCHAR TCHAR;
@@ -104,19 +107,22 @@ typedef char TCHAR;
 #define _TEXT(x) x
 #endif
 
-#endif
 
 
+/* Definitions of volume management */
 
-/* Type of file size variables */
+#if FF_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 mapping table */
+#endif
 
-#if FF_FS_EXFAT
-#if FF_INTDEF != 2
-#error exFAT feature wants C99 or later
+#if FF_STR_VOLUME_ID
+#ifndef FF_VOLUME_STRS
+extern const char* VolumeStr[FF_VOLUMES];	/* User defied volume ID */
 #endif
-typedef QWORD FSIZE_t;
-#else
-typedef DWORD FSIZE_t;
 #endif
 
 
@@ -158,14 +164,14 @@ typedef struct {
 #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 */
+	LBA_t	volbase;		/* Volume base sector */
+	LBA_t	fatbase;		/* FAT base sector */
+	LBA_t	dirbase;		/* Root directory base sector/cluster */
+	LBA_t	database;		/* Data base sector */
 #if FF_FS_EXFAT
-	DWORD	bitbase;		/* Allocation bitmap base sector */
+	LBA_t	bitbase;		/* Allocation bitmap base sector */
 #endif
-	DWORD	winsect;		/* Current sector appearing in the win[] */
+	LBA_t	winsect;		/* Current sector appearing in the win[] */
 	BYTE	win[FF_MAX_SS];	/* Disk access window for Directory, FAT (and file data at tiny cfg) */
 } FATFS;
 
@@ -202,9 +208,9 @@ typedef struct {
 	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 fptr is 0) */
-	DWORD	sect;			/* Sector number appearing in buf[] (0:invalid) */
+	LBA_t	sect;			/* Sector number appearing in buf[] (0:invalid) */
 #if !FF_FS_READONLY
-	DWORD	dir_sect;		/* Sector number containing the directory entry (not used at exFAT) */
+	LBA_t	dir_sect;		/* Sector number containing the directory entry (not used at exFAT) */
 	BYTE*	dir_ptr;		/* Pointer to the directory entry in the win[] (not used at exFAT) */
 #endif
 #if FF_USE_FASTSEEK
@@ -223,7 +229,7 @@ typedef struct {
 	FFOBJID	obj;			/* Object identifier */
 	DWORD	dptr;			/* Current read/write offset */
 	DWORD	clust;			/* Current cluster */
-	DWORD	sect;			/* Current sector (0:Read operation has terminated) */
+	LBA_t	sect;			/* Current sector (0:Read operation has terminated) */
 	BYTE*	dir;			/* Pointer to the directory item in the win[] */
 	BYTE	fn[12];			/* SFN (in/out) {body[8],ext[3],status[1]} */
 #if FF_USE_LFN
@@ -253,6 +259,18 @@ typedef struct {
 
 
 
+/* Format parameter structure (MKFS_PARM) */
+
+typedef struct {
+	BYTE fmt;			/* Format option (FM_FAT, FM_FAT32, FM_EXFAT and FM_SFD) */
+	BYTE n_fat;			/* Number of FATs */
+	UINT align;			/* Data area alignment (sector) */
+	UINT n_root;		/* Number of root directory entries */
+	DWORD au_size;		/* Cluster size (byte) */
+} MKFS_PARM;
+
+
+
 /* File function return code (FRESULT) */
 
 typedef enum {
@@ -304,14 +322,14 @@ FRESULT f_utime (const TCHAR* path, const FILINFO* fno);			/* Change timestamp o
 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_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_expand (FIL* fp, FSIZE_t fsz, 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 */
+FRESULT f_mkfs (const TCHAR* path, const MKFS_PARM* opt, void* work, UINT len);	/* Create a FAT volume */
+FRESULT f_fdisk (BYTE pdrv, const LBA_t ptbl[], void* work);		/* Divide a physical drive into some partitions */
 FRESULT f_setcp (WORD cp);											/* Set current code page */
 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 */
@@ -327,10 +345,6 @@ TCHAR* f_gets (TCHAR* buff, int len, FIL* fp);						/* Get a string from the fil
 #define f_rmdir(path) f_unlink(path)
 #define f_unmount(path) f_mount(0, path, 0)
 
-#ifndef EOF
-#define EOF (-1)
-#endif
-
 
 
 

+ 56 - 43
luat/packages/fatfs/ffconf.h

@@ -2,7 +2,7 @@
 /  FatFs Functional Configurations
 /---------------------------------------------------------------------------*/
 
-#define FFCONF_DEF	86604	/* Revision ID */
+#define FFCONF_DEF	86631	/* Revision ID */
 
 /*---------------------------------------------------------------------------/
 / Function Configurations
@@ -25,14 +25,6 @@
 /   3: f_lseek() function is removed in addition to 2. */
 
 
-#define FF_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 FF_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) */
@@ -64,6 +56,30 @@
 /* This option switches f_forward() function. (0:Disable or 1:Enable) */
 
 
+#define FF_USE_STRFUNC	0
+#define FF_PRINT_LLI	0
+#define FF_PRINT_FLOAT	0
+#define FF_STRF_ENCODE	0
+/* FF_USE_STRFUNC switches string functions, f_gets(), f_putc(), f_puts() and
+/  f_printf().
+/
+/   0: Disable. FF_PRINT_LLI, FF_PRINT_FLOAT and FF_STRF_ENCODE have no effect.
+/   1: Enable without LF-CRLF conversion.
+/   2: Enable with LF-CRLF conversion.
+/
+/  FF_PRINT_LLI = 1 makes f_printf() support long long argument and FF_PRINT_FLOAT = 1/2
+   makes f_printf() support floating point argument. These features want C99 or later.
+/  When FF_LFN_UNICODE >= 1 with LFN enabled, string functions convert the character
+/  encoding in it. FF_STRF_ENCODE selects assumption of character encoding ON THE FILE
+/  to be read/written via those functions.
+/
+/   0: ANSI/OEM in current CP
+/   1: Unicode in UTF-16LE
+/   2: Unicode in UTF-16BE
+/   3: Unicode in UTF-8
+*/
+
+
 /*---------------------------------------------------------------------------/
 / Locale and Namespace Configurations
 /---------------------------------------------------------------------------*/
@@ -102,7 +118,7 @@
 /* The FF_USE_LFN switches the support for LFN (long file name).
 /
 /   0: Disable LFN. FF_MAX_LFN has no effect.
-/   1: Enable LFN with static working buffer on the BSS. Always NOT thread-safe.
+/   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.
 /
@@ -110,11 +126,11 @@
 /  requiers certain internal working buffer occupies (FF_MAX_LFN + 1) * 2 bytes and
 /  additional (FF_MAX_LFN + 44) / 15 * 32 bytes when exFAT is enabled.
 /  The FF_MAX_LFN defines size of the working buffer in UTF-16 code unit and it can
-/  be in range of 12 to 255. It is recommended to be set 255 to fully support LFN
+/  be in range of 12 to 255. It is recommended to be set it 255 to fully support LFN
 /  specification.
 /  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() in ffsystem.c, need to be added to the project. */
+/  ff_memfree() exemplified in ffsystem.c, need to be added to the project. */
 
 
 #define FF_LFN_UNICODE	0
@@ -137,19 +153,6 @@
 /  on character encoding. When LFN is not enabled, these options have no effect. */
 
 
-#define FF_STRF_ENCODE	3
-/* When FF_LFN_UNICODE >= 1 with LFN enabled, string I/O functions, f_gets(),
-/  f_putc(), f_puts and f_printf() convert the character encoding in it.
-/  This option selects assumption of character encoding ON THE FILE to be
-/  read/written via those functions.
-/
-/   0: ANSI/OEM in current CP
-/   1: Unicode in UTF-16LE
-/   2: Unicode in UTF-16BE
-/   3: Unicode in UTF-8
-*/
-
-
 #define FF_FS_RPATH		0
 /* This option configures support for relative path.
 /
@@ -194,30 +197,28 @@
 #define FF_MAX_SS		512
 /* This set of options configures the range of sector size to be supported. (512,
 /  1024, 2048 or 4096) Always set both 512 for most systems, generic memory card and
-/  harddisk. But a larger value may be required for on-board flash memory and some
+/  harddisk, but a larger value may be required for on-board flash memory and some
 /  type of optical media. When FF_MAX_SS is larger than FF_MIN_SS, FatFs is configured
 /  for variable sector size mode and disk_ioctl() function needs to implement
 /  GET_SECTOR_SIZE command. */
 
 
+#define FF_LBA64		0
+/* This option switches support for 64-bit LBA. (0:Disable or 1:Enable)
+/  To enable the 64-bit LBA, also exFAT needs to be enabled. (FF_FS_EXFAT == 1) */
+
+
+#define FF_MIN_GPT		0x10000000
+/* Minimum number of sectors to switch GPT as partitioning format in f_mkfs and
+/  f_fdisk function. 0x100000000 max. This option has no effect when FF_LBA64 == 0. */
+
+
 #define FF_USE_TRIM		0
 /* This option switches support for ATA-TRIM. (0:Disable or 1:Enable)
 /  To enable Trim function, also CTRL_TRIM command should be implemented to the
 /  disk_ioctl() function. */
 
 
-#define FF_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
@@ -236,10 +237,10 @@
 /  Note that enabling exFAT discards ANSI C (C89) compatibility. */
 
 
-#define FF_FS_NORTC		1
-#define FF_NORTC_MON	12
-#define FF_NORTC_MDAY	10
-#define FF_NORTC_YEAR	2018
+#define FF_FS_NORTC		0
+#define FF_NORTC_MON	1
+#define FF_NORTC_MDAY	1
+#define FF_NORTC_YEAR	2020
 /* The option FF_FS_NORTC switches timestamp functiton. If the system does not have
 /  any RTC function or valid timestamp is not needed, set FF_FS_NORTC = 1 to disable
 /  the timestamp function. Every object modified by FatFs will have a fixed timestamp
@@ -247,7 +248,19 @@
 /  To enable timestamp function (FF_FS_NORTC = 0), get_fattime() function need to be
 /  added to the project to read current time form real-time clock. FF_NORTC_MON,
 /  FF_NORTC_MDAY and FF_NORTC_YEAR have no effect.
-/  These options have no effect at read-only configuration (FF_FS_READONLY = 1). */
+/  These options have no effect in read-only configuration (FF_FS_READONLY = 1). */
+
+
+#define FF_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.
+*/
 
 
 #define FF_FS_LOCK		0

+ 6 - 10
luat/packages/fatfs/ffunicode.c

@@ -1,5 +1,5 @@
 /*------------------------------------------------------------------------*/
-/* Unicode handling functions for FatFs R0.13c                            */
+/* Unicode handling functions for FatFs R0.13+                            */
 /*------------------------------------------------------------------------*/
 /* This module will occupy a huge memory in the .const section when the    /
 /  FatFs is configured for LFN with DBCS. If the system has any Unicode    /
@@ -7,7 +7,7 @@
 /  that function to avoid silly memory consumption.                        /
 /-------------------------------------------------------------------------*/
 /*
-/ Copyright (C) 2018, ChaN, all right reserved.
+/ Copyright (C) 2014, 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
@@ -25,11 +25,7 @@
 
 #include "ff.h"
 
-#if FF_USE_LFN	/* This module will be blanked at non-LFN configuration */
-
-#if FF_DEFINED != 86604	/* Revision ID */
-#error Wrong include file (ff.h).
-#endif
+#if FF_USE_LFN	/* This module will be blanked if non-LFN configuration */
 
 #define MERGE2(a, b) a ## b
 #define CVTBL(tbl, cp) MERGE2(tbl, cp)
@@ -15245,7 +15241,7 @@ WCHAR ff_uni2oem (	/* Returns OEM code character, zero on error */
 	return c;
 }
 
-WCHAR ff_oem2uni (	/* Returns Unicode character, zero on error */
+WCHAR ff_oem2uni (	/* Returns Unicode character in UTF-16, zero on error */
 	WCHAR	oem,	/* OEM code to be converted */
 	WORD	cp		/* Code page for the conversion */
 )
@@ -15312,7 +15308,7 @@ WCHAR ff_uni2oem (	/* Returns OEM code character, zero on error */
 }
 
 
-WCHAR ff_oem2uni (	/* Returns Unicode character, zero on error */
+WCHAR ff_oem2uni (	/* Returns Unicode character in UTF-16, zero on error */
 	WCHAR	oem,	/* OEM code to be converted */
 	WORD	cp		/* Code page for the conversion */
 )
@@ -15411,7 +15407,7 @@ WCHAR ff_uni2oem (	/* Returns OEM code character, zero on error */
 }
 
 
-WCHAR ff_oem2uni (	/* Returns Unicode character, zero on error */
+WCHAR ff_oem2uni (	/* Returns Unicode character in UTF-16, zero on error */
 	WCHAR	oem,	/* OEM code to be converted (DBC if >=0x100) */
 	WORD	cp		/* Code page for the conversion */
 )

+ 58 - 27
luat/packages/fatfs/luat_lib_fatfs.c

@@ -3,7 +3,7 @@
 #include "luat_spi.h"
 #include "luat_timer.h"
 #include "luat_gpio.h"
-#include "lauxlib.h"
+#include "luat_malloc.h"
 
 #include "ff.h"			/* Obtains integer types */
 #include "diskio.h"		/* Declarations of disk functions */
@@ -11,38 +11,55 @@
 #define LUAT_LOG_TAG "luat.fatfs"
 #include "luat_log.h"
 
-static FATFS fs;		/* FatFs work area needed for each volume */
+static FATFS *fs = NULL;		/* FatFs work area needed for each volume */
 extern BYTE FATFS_DEBUG; // debug log, 0 -- disable , 1 -- enable
 extern BYTE FATFS_SPI_ID; // 0 -- SPI_1, 1 -- SPI_2
 extern BYTE FATFS_SPI_CS; // GPIO 3
 
+DRESULT diskio_open_ramdisk(BYTE pdrv, size_t len);
+DRESULT diskio_open_spitf(BYTE pdrv, BYTE id, BYTE cs);
+
 static int fatfs_mount(lua_State *L)
 {
-    //int spiId = luaL_checkinteger(L, 1);    
-    //int result = platform_spi_close(spiId);
-	//if (FATFS_DEBUG)
-	//	LLOGD("fatfs_init>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\r\n");
+	if (FATFS_DEBUG)
+		LLOGD("fatfs_init>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
+
+	if (fs == NULL) {
+		fs = luat_heap_malloc(sizeof(FATFS));
+	}
 
-	//DWORD fre_clust, fre_sect, tot_sect;
 	// 挂载点
 	const char *mount_point = luaL_optstring(L, 1, "");
 	FATFS_SPI_ID = luaL_optinteger(L, 2, 0); // SPI_1
 	FATFS_SPI_CS = luaL_optinteger(L, 3, 3); // GPIO_3
 
-	DRESULT re = f_mount(&fs, mount_point, 0);
+	if (!strcmp("ramdisk", mount_point) || !strcmp("ram", mount_point)) {
+		LLOGD("init ramdisk at FatFS");
+		diskio_open_ramdisk(0, luaL_optinteger(L, 2, 64*1024));
+	} else {
+		#ifdef LUA_USE_WINDOWS
+		LLOGE("win32/posix only support ramdisk");
+		return 0;
+		#else
+		LLOGD("init sdcard at spi=%d cs=%d", FATFS_SPI_ID, FATFS_SPI_CS);
+		diskio_open_spitf(0, FATFS_SPI_ID, FATFS_SPI_CS);
+		#endif
+	}
+
+	DRESULT re = f_mount(fs, mount_point, 0);
 	
 	lua_pushinteger(L, re);
 	if (re == FR_OK) {
 		if (FATFS_DEBUG)
-			LLOGD("[FatFS]fatfs_init success\r\n");
+			LLOGD("[FatFS]fatfs_init success");
 	}
 	else {
 		if (FATFS_DEBUG)
-			LLOGD("[FatFS]fatfs_init FAIL!! re=%d\r\n", re);
+			LLOGD("[FatFS]fatfs_init FAIL!! re=%d", re);
 	}
 
-	//if (FATFS_DEBUG)
-	//	LLOGD("fatfs_init<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\r\n");
+	if (FATFS_DEBUG)
+		LLOGD("fatfs_init<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
     return 1;
 }
 
@@ -55,11 +72,25 @@ static int fatfs_unmount(lua_State *L) {
 
 static int fatfs_mkfs(lua_State *L) {
 	const char *mount_point = luaL_optstring(L, 1, "");
-	BYTE sfd = luaL_optinteger(L, 2, 0);
-	DWORD au = luaL_optinteger(L, 3, 0);
-	BYTE work[FF_MAX_SS];
-	FRESULT re = f_mkfs(mount_point, sfd, au, work, sizeof work);
+	// BYTE sfd = luaL_optinteger(L, 2, 0);
+	// DWORD au = luaL_optinteger(L, 3, 0);
+	BYTE work[FF_MAX_SS] = {0};
+	if (FATFS_DEBUG)
+		LLOGI("mkfs GO %d");
+	MKFS_PARM parm = {
+		.fmt = FM_ANY, // 暂时应付一下ramdisk
+		.au_size = 0,
+		.align = 0,
+		.n_fat = 0,
+		.n_root = 0,
+	};
+	if (!strcmp("ramdisk", mount_point) || !strcmp("ram", mount_point)) {
+		parm.fmt = FM_ANY | FM_SFD;
+	}
+	FRESULT re = f_mkfs(mount_point, &parm, work, FF_MAX_SS);
 	lua_pushinteger(L, re);
+	if (FATFS_DEBUG)
+		LLOGI("mkfs ret %d", re);
 	return 1;
 }
 
@@ -96,7 +127,7 @@ static int fatfs_getfree(lua_State *L)
 	lua_pushinteger(L, fre_sect / 2);
 	lua_settable(L, -3);
 	
-	return lua_gettop(L);
+	return 1;
 }
 
 // ------------------------------------------------
@@ -168,7 +199,7 @@ static int fatfs_lsdir(lua_State *L)
 		lua_settable(L, -3);
 	}
 	f_closedir(&dir);
-	LLOGD("[FatFS] lua_gettop=%d\r\n", lua_gettop(L));
+	LLOGD("[FatFS] lua_gettop=%d", lua_gettop(L));
     return 2;
 }
 
@@ -234,7 +265,7 @@ static int fatfs_open(lua_State *L) {
 	flag |= luaL_optinteger(L, 4, 0); // 第四个参数
 
 	if (FATFS_DEBUG)
-		LLOGD("[FatFS]open %s %0X\r\n", path, flag);
+		LLOGD("[FatFS]open %s %0X", path, flag);
 	DRESULT re = f_open(fil, path, (BYTE)flag);
 	if (re != FR_OK) {
 		lua_remove(L, -1);
@@ -292,10 +323,10 @@ static int fatfs_read(lua_State *L) {
 		return 2;
 	}
 	UINT limit = luaL_optinteger(L, 2, 512);
-	u8 buf[limit];
+	BYTE buf[limit];
 	UINT len;
 	if (FATFS_DEBUG)
-		LLOGD("[FatFS]readfile limit=%d\r\n", limit);
+		LLOGD("[FatFS]readfile limit=%d", limit);
 	FRESULT re = f_read((FIL*)lua_touserdata(L, 1), buf, limit, &len);
 	lua_pushinteger(L, re);
 	if (re != FR_OK) {
@@ -332,7 +363,7 @@ static int fatfs_write(lua_State *L) {
          re = f_write(fil, buf, len, &len);
     }
     if (FATFS_DEBUG)
-		LLOGD("[FatFS]write re=%d len=%d\r\n", re, len);
+		LLOGD("[FatFS]write re=%d len=%d", re, len);
     lua_pushinteger(L, re);
     lua_pushinteger(L, len);
     return 2;
@@ -389,16 +420,16 @@ static int fatfs_readfile(lua_State *L) {
 		return 1;
 	}
 
-	u32 limit = luaL_optinteger(L, 2, 512);
-	u32 seek = luaL_optinteger(L, 3, 0);
+	DWORD limit = luaL_optinteger(L, 2, 512);
+	DWORD seek = luaL_optinteger(L, 3, 0);
 	if (seek > 0) {
 		f_lseek(&fil, seek);
 	}
 
-	u8 buf[limit];
+	BYTE buf[limit];
 	UINT len;
 	if (FATFS_DEBUG)
-		LLOGD("[FatFS]readfile seek=%d limit=%d\r\n", seek, limit);
+		LLOGD("[FatFS]readfile seek=%d limit=%d", seek, limit);
 	FRESULT fr = f_read(&fil, buf, limit, &len);
 	if (fr != FR_OK) {
 		lua_pushinteger(L, -3);
@@ -409,7 +440,7 @@ static int fatfs_readfile(lua_State *L) {
 	lua_pushinteger(L, 0);
 	lua_pushlstring(L, buf, len);
 	if (FATFS_DEBUG)
-		LLOGD("[FatFS]readfile seek=%d limit=%d len=%d\r\n", seek, limit, len);
+		LLOGD("[FatFS]readfile seek=%d limit=%d len=%d", seek, limit, len);
 	return 2;
 }
 

Некоторые файлы не были показаны из-за большого количества измененных файлов