Unverified Commit 074b9523 authored by Earle F. Philhower, III's avatar Earle F. Philhower, III Committed by GitHub

Add FatFS and FatFSUSB - Wear-Leveled FTL based FAT filesystem for onboard flash (#2028)

* Add FatFS for onboard flash use/sharing of FAT

* Move all to "fatfs" namespace

The FatFS library defines commonly used names like WORD which could conflict
with user code otherwise.

* Restyle

* FTL-based, wear-leveling FatFS with USB export

Allow using FAT filesystem with onboard flash in a safer, wear-leveled
way and to export the onboard flash to the host as a USB drive for easy
data transfer.

* Update documentation

* Fix submodule reference

* Don't spellehcek ChaN FatFS files

* Disable FTL debugging

* More codespell skips

* Move to latest SPIFTL library

* Allow using raw flash instead of FTL

* Remove unneeded static FIL 4k allocation

* Expose FAT FS format configuration options

* Update documentation

* Remove USB partial flash rewrites

* Remove unneeded dups of FatFS sources

Leave the LICENSE.md and README.md to point to upstream.

* Clean up comments
parent 11dfb2c9
......@@ -24,7 +24,7 @@ jobs:
- name: Run codespell
uses: codespell-project/actions-codespell@master
with:
skip: ./ArduinoCore-API,./libraries/ESP8266SdFat,./libraries/Adafruit_TinyUSB_Arduino,./libraries/LittleFS/lib,./tools/pyserial,./pico-sdk,./.github,./docs/i2s.rst,./cores/rp2040/api,./libraries/FreeRTOS,./tools/libbearssl/bearssl,./include,./libraries/WiFi/examples/BearSSL_Server,./ota/uzlib,./libraries/http-parser/lib,./libraries/WebServer/examples/HelloServerBearSSL/HelloServerBearSSL.ino,./libraries/HTTPUpdateServer/examples/SecureBearSSLUpdater/SecureBearSSLUpdater.ino,./.git
skip: ./ArduinoCore-API,./libraries/ESP8266SdFat,./libraries/Adafruit_TinyUSB_Arduino,./libraries/LittleFS/lib,./tools/pyserial,./pico-sdk,./.github,./docs/i2s.rst,./cores/rp2040/api,./libraries/FreeRTOS,./tools/libbearssl/bearssl,./include,./libraries/WiFi/examples/BearSSL_Server,./ota/uzlib,./libraries/http-parser/lib,./libraries/WebServer/examples/HelloServerBearSSL/HelloServerBearSSL.ino,./libraries/HTTPUpdateServer/examples/SecureBearSSLUpdater/SecureBearSSLUpdater.ino,./.git,./libraries/FatFS/lib/fatfs,./libraries/FatFS/src/diskio.h,./libraries/FatFS/src/ff.cpp,./libraries/FatFS/src/ffconf.h,./libraries/FatFS/src/ffsystem.cpp,./libraries/FatFS/src/ff.h
ignore_words_list: ser,dout
# Consistent style
......
......@@ -37,3 +37,6 @@
[submodule "libraries/http_parser/lib/http-parser"]
path = libraries/http-parser/lib/http-parser
url = https://github.com/nodejs/http-parser.git
[submodule "libraries/FatFS/lib/SPIFTL"]
path = libraries/FatFS/lib/SPIFTL
url = https://github.com/earlephilhower/SPIFTL.git
......@@ -253,6 +253,8 @@ The installed tools include a version of OpenOCD (in the pqt-openocd directory)
* [http-parser](https://github.com/nodejs/http-parser) is copyright Joyent, Inc. and other Node contributors.
* WebServer code modified from the [ESP32 WebServer](https://github.com/espressif/arduino-esp32/tree/master/libraries/WebServer) and is copyright (c) 2015 Ivan Grokhotkov and others.
* [Xoshiro-cpp](https://github.com/Reputeless/Xoshiro-cpp) is copyright (c) 2020 Ryo Suzuki and distributed under the MIT license.
* [FatFS low-level filesystem](http://elm-chan.org/fsw/ff/) code is Copyright (C) 2024, ChaN, all rights reserved.
-Earle F. Philhower, III
earlephilhower@yahoo.com
FatFSUSB
========
When the onboard flash memory is used as a ``FatFS`` filesystem, the
``FatFSUSB`` can be used to allow exporting it to a PC as a standard
memory stick. The PC can then access, add, and remove files as if the
Pico was a USB memory stick, and upon ejection the Pico can access
any new files just as if it made them itself.
(Note, if you are using LittleFS then you need to use ``SingleFileDrive``
to export a single file, not this class, because the PC does not
understand the LittleFS disk format.)
Callbacks, Interrupt Safety, and File Operations
------------------------------------------------
The ``FatFSUSB`` library allows your application to get a callback
when a PC attempts to mount or unmount the Pico as a FAT drive.
When the drive is being used by the Pico (i.e. any ``File`` is open for
read or write, the ``FatFS`` is not ``end()`` -ed and still mounted,
etc.) the host may not access it. Conversely, while the host PC is
connected to the drive no ``FatFS`` access by the Pico is allowed.
Your ``driveReady`` callback will be called when the PC attempts to mount
the drive. If you have any files open, then this callback can report back
that the drive is not yet ready. When you complete file processing, the PC
can re-attempt to mount the drive and your callback can return ``true`` .
The ``onPlug`` callback will generally ``FatFS.end()`` and set a
global flag letting your application know not to touch the filesystem until
the flag is cleared by the ``onUnplug`` callback (which will also do a
``FatFS.begin()`` call).
Failing to close all files **and** ``FatFS.end()`` before granting the
PC access to flash memory will result in corruption. FAT does not allow multiple
writers to access the same drive. Even mounting and only reading files from
the PC may cause hidden writes (things like access time, etc.) which would
also cause corruption.
See the included ``Listfiles-USB`` sketch for an example of working with
these limitations.
......@@ -33,6 +33,7 @@ following include to the sketch:
#include "LittleFS.h" // LittleFS is declared
// #include <SDFS.h>
// #include <SD.h>
// #include <FatFS.h>
Compatible Filesystem APIs
......@@ -48,10 +49,45 @@ SD card reader.
SD is the Arduino-supported, somewhat old and limited SD card filesystem.
It is recommended to use SDFS for new applications instead of SD.
All three of these filesystems can open and manipulate ``File`` and ``Dir``
FatFS implements a wear-leveled, FTL-backed FAT filesystem in the onboard
flash which can be easily accessed over USB as a standard memory stick
via FatFSUSB.
All of these filesystems can open and manipulate ``File`` and ``Dir``
objects with the same code because the implement a common end-user
filesystem API.
FatFS File System Caveats and Warnings
--------------------------------------
The FAT filesystem is ubiquitous, but it is also around 50 years old and ill
suited to SPI flash memory due to having "hot spots" like the FAT copies that
are rewritten many times over. SPI flash allows a high, but limited, number
of writes before losing the ability to write safely. Applications like
data loggers where many writes occur could end up wearing out the SPI flash
sector that holds the FAT **years** before coming close to the write limits of
the data sectors.
To circumvent this issue, the FatFS implementation here uses a flash translation
layer (FTL) developed for SPI flash on embedded systems. This allows for the
same LBA to be written over and over by the FAT filesystem, but use different
flash locations. For more information see
[SPIFTL](https://github.com/earlephilhower/SPIFTL). In this mode the Pico
flash appears as a normal, 512-byte sector drive to the FAT.
What this means, practically, is that about 5KB of RAM per megabyte of flash
is required for housekeeping. Writes can also become very slow if most of the
flash LBA range is used (i.e. if the FAT drive is 99% full) due to the need
for garbage collection processes to move data around and preserve the flash
lifetime.
Alternatively, if an FTL is not desired or memory is tight, FatFS can use the
raw flash directly. In this mode sectors are 4K in size and flash is mapped
1:1 to sectors, so things like the FAT table updates will all use the same
physical flash bits. For low-utilization operations this may be fine, but if
significant writes are done (from the Pico or the PC host) this may wear out
portions of flash very quickly , rendering it unusable.
LittleFS File System Limitations
--------------------------------
......@@ -115,8 +151,8 @@ second SPI port, ``SPI1``. Just use the following call in place of
SD.begin(cspin, SPI1);
File system object (LittleFS/SD/SDFS)
-------------------------------------
File system object (LittleFS/SD/SDFS/FatFS)
-------------------------------------------
setConfig
~~~~~~~~~
......@@ -131,14 +167,22 @@ setConfig
c2.setCSPin(12);
SDFS.setConfig(c2);
FatFSConfig c3;
c3.setUseFTL(false); // Directly access flash memory
c3.setDirEntries(256); // We need 256 root directory entries on a format()
c3.setFATCopies(1); // Only 1 FAT to save 4K of space and extra writes
FatFS.setConfig(c3);
FatFS.format(); // Format using these settings, erasing everything
This method allows you to configure the parameters of a filesystem
before mounting. All filesystems have their own ``*Config`` (i.e.
``SDFSConfig`` or ``LittleFSConfig`` with their custom set of options.
All filesystems allow explicitly enabling/disabling formatting when
mounts fail. If you do not call this ``setConfig`` method before
perforing ``begin()``, you will get the filesystem's default
behavior and configuration. By default, LittleFS will autoformat the
filesystem if it cannot mount it, while SDFS will not.
behavior and configuration. By default, LittleFS and FatFS will autoformat the
filesystem if it cannot mount it, while SDFS will not. FatFS will also use
the built-in FTL to support 512 byte sectors and higher write lifetime.
begin
~~~~~
......
......@@ -8,6 +8,8 @@ Arduino KEYWORD3 RESERVED_WORD
# Datatypes (KEYWORD1)
#######################################
Dir KEYWORD1
File KEYWORD1
timeval KEYWORD1
time_t KEYWORD1
......@@ -15,6 +17,10 @@ time_t KEYWORD1
# Methods and Functions (KEYWORD2)
#######################################
openDir KEYWORD2
setConfig KEYWORD2
fileCreationTime KEYWORD2
setup1 KEYWORD2
loop1 KEYWORD2
......@@ -64,7 +70,6 @@ digitalReadFast KEYWORD2
enableDoubleResetBootloader KEYWORD2
Dir KEYWORD2
openDir KEYWORD2
next KEYWORD2
getLastWrite KEYWORD2
......
#######################################
# Syntax Coloring Map
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
FatFS KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
format KEYWORD2
FatFSConfig KEYWORD2
setUseFTL KEYWORD2
setDirEntries KEYWORD2
setFATCopies KEYWORD2
#######################################
# Constants (LITERAL1)
#######################################
Subproject commit 3016627c3f727fa31e7b8673bedecf46642fcedd
FatFs License
FatFs has being developped as a personal project of the author, ChaN. It is free from the code anyone else wrote at current release. Following code block shows a copy of the FatFs license document that heading the source files.
/*----------------------------------------------------------------------------/
/ FatFs - Generic FAT Filesystem Module Rx.xx /
/-----------------------------------------------------------------------------/
/
/ Copyright (C) 20xx, 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.
/----------------------------------------------------------------------------*/
Therefore FatFs license is one of the BSD-style licenses, but there is a significant feature. FatFs is mainly intended for embedded systems. In order to extend the usability for commercial products, the redistributions of FatFs in binary form, such as embedded code, binary library and any forms without source code, do not need to include about FatFs in the documentations. This is equivalent to the 1-clause BSD license. Of course FatFs is compatible with the most of open source software licenses include GNU GPL. When you redistribute the FatFs source code with changes or create a fork, the license can also be changed to GNU GPL, BSD-style license or any open source software license that not conflict with FatFs license.
FatFS home at http://elm-chan.org/fsw/ff/
name=FatFS
version=0.15.0
author=Earle F. Philhower, III <earlephilhower@yahoo.com>
maintainer=Earle F. Philhower, III <earlephilhower@yahoo.com>
sentence=FS filesystem for use on flash using ChaN's FatFS code
paragraph=FS filesystem for use on flash using ChaN's FatFS code
category=Data Storage
url=https://github.com/earlephilhower/arduino-pico
architectures=rp2040
dot_a_linkage=true
/*
FatFS.cpp - file system wrapper for FatFS
Copyright (c) 2024 Earle F. Philhower, III. All rights reserved.
Based on spiffs_api.cpp which is:
| Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
This code was influenced by NodeMCU and Sming libraries, and first version of
Arduino wrapper written by Hristo Gochkov.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "FatFS.h"
#include <FS.h>
//#define FTL_DEBUG 1
#include "../lib/SPIFTL/FlashInterfaceRP2040.h"
#include "../lib/SPIFTL/SPIFTL.h"
using namespace fs;
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_FATFS)
extern uint8_t _FS_start;
extern uint8_t _FS_end;
FS FatFS = FS(FSImplPtr(new fatfs::FatFSImpl()));
static FlashInterfaceRP2040 *_fi = new FlashInterfaceRP2040(&_FS_start, &_FS_end);
static SPIFTL *_ftl = nullptr;
uint16_t _sectorSize = 512;
#endif
namespace fatfs {
bool FatFSImpl::begin() {
if (_mounted) {
return true;
}
if (_cfg._useFTL) {
if (!_ftl) {
_ftl = new SPIFTL(_fi);
}
_sectorSize = 512;
} else {
_sectorSize = 4096;
if (_ftl) {
delete _ftl;
_ftl = nullptr;
}
}
_mounted = (FR_OK == f_mount(&_fatfs, "", 1));
if (!_mounted && _cfg._autoFormat) {
format();
_mounted = (FR_OK == f_mount(&_fatfs, "", 1));
}
//FsDateTime::setCallback(dateTimeCB); TODO = callback
return _mounted;
}
void FatFSImpl::end() {
if (_mounted) {
f_unmount("");
}
sync();
_mounted = false;
}
// Required to be global because SDFAT doesn't allow a this pointer in it's own time call
time_t (*__fatfs_timeCallback)(void) = nullptr;
FileImplPtr FatFSImpl::open(const char* path, OpenMode openMode, AccessMode accessMode) {
if (!_mounted) {
DEBUGV("FatFSImpl::open() called on unmounted FS\n");
return FileImplPtr();
}
if (!path || !path[0]) {
DEBUGV("FatFSImpl::open() called with invalid filename\n");
return FileImplPtr();
}
BYTE flags = _getFlags(openMode, accessMode);
if ((openMode && OM_CREATE) && strchr(path, '/')) {
// For file creation, silently make subdirs as needed. If any fail,
// it will be caught by the real file open later on
char *pathStr = strdup(path);
if (pathStr) {
// Make dirs up to the final fnamepart
char *ptr = strrchr(pathStr, '/');
if (ptr && ptr != pathStr) { // Don't try to make root dir!
*ptr = 0;
f_mkdir(pathStr);
}
}
free(pathStr);
}
auto sharedFd = std::make_shared<FIL>();
if (FR_OK == f_open(sharedFd.get(), path, flags)) {
return std::make_shared<FatFSFileImpl>(this, sharedFd, path, FA_WRITE & flags ? true : false);
}
sharedFd = nullptr;
DEBUGV("FatFSImpl::openFile: path=`%s` openMode=%d accessMode=%d FAILED", path, openMode, accessMode);
return FileImplPtr();
}
DirImplPtr FatFSImpl::openDir(const char* path) {
if (!_mounted) {
return DirImplPtr();
}
char *pathStr = strdup(path); // Allow edits on our scratch copy
if (!pathStr) {
// OOM
return DirImplPtr();
}
// Get rid of any trailing slashes
while (strlen(pathStr) && (pathStr[strlen(pathStr) - 1] == '/')) {
pathStr[strlen(pathStr) - 1] = 0;
}
// At this point we have a name of "/blah/blah/blah" or "blah" or ""
// If that references a directory, just open it and we're done.
DIR dirFile;
FILINFO fno;
const char *filter = "";
if (!pathStr[0]) {
// openDir("") === openDir("/")
f_opendir(&dirFile, "/");
filter = "";
} else if (FR_OK == f_stat(pathStr, &fno)) {
if (fno.fattrib & AM_DIR) {
// Easy peasy, path specifies an existing dir!
f_opendir(&dirFile, pathStr);
filter = "";
} else {
// This is a file, so open the containing dir
char *ptr = strrchr(pathStr, '/');
if (!ptr) {
// No slashes, open the root dir
f_opendir(&dirFile, "/");
filter = pathStr;
} else {
// We've got slashes, open the dir one up
*ptr = 0; // Remove slash, truncare string
f_opendir(&dirFile, pathStr);
filter = ptr + 1;
}
}
} else {
// Name doesn't exist, so use the parent dir of whatever was sent in
// This is a file, so open the containing dir
char *ptr = strrchr(pathStr, '/');
if (!ptr) {
// No slashes, open the root dir
f_opendir(&dirFile, "/");
filter = pathStr;
} else {
// We've got slashes, open the dir one up
*ptr = 0; // Remove slash, truncare string
f_opendir(&dirFile, pathStr);
filter = ptr + 1;
}
}
// TODO -can this ever happen?
// if (!dirFile) {
// DEBUGV("FatFSImpl::openDir: path=`%s`\n", path);
// return DirImplPtr();
// }
auto sharedDir = std::make_shared<DIR>(dirFile);
auto ret = std::make_shared<FatFSDirImpl>(filter, this, sharedDir, pathStr);
free(pathStr);
return ret;
}
bool FatFSImpl::format() {
if (_mounted) {
return false;
}
BYTE *work = new BYTE[4096]; /* Work area (larger is better for processing time) */
MKFS_PARM opt = { FM_FAT | FM_SFD, _cfg._fatCopies, 1, _sectorSize, _cfg._dirEntries};
auto ret = f_mkfs("", &opt, work, 4096);
delete[] work;
return ret == FR_OK;
}
DSTATUS disk_status(BYTE p) {
(void) p;
return 0;
}
static bool started = false;
void disk_format() {
if (!started && _ftl) {
_ftl->format();
}
}
DSTATUS disk_initialize(BYTE p) {
(void) p;
if (!started) {
if (_ftl) {
_ftl->start();
}
started = true;
}
return 0;
}
DRESULT disk_read(BYTE p, BYTE *buff, LBA_t sect, UINT count) {
(void) p;
for (unsigned int i = 0; i < count; i++) {
if (_ftl) {
_ftl->read(sect + i, buff + i * _sectorSize);
} else {
_fi->read(sect + i, 0, buff, _sectorSize);
}
}
return RES_OK;
}
DRESULT disk_write(BYTE pdrv, const BYTE* buff, LBA_t sector, UINT count) {
(void) pdrv;
for (unsigned int i = 0; i < count; i++) {
if (_ftl) {
_ftl->write(sector + i, buff + i * _sectorSize);
} else {
_fi->eraseBlock(sector + i);
_fi->program(sector + i, 0, buff + i * _sectorSize, _sectorSize);
}
}
return RES_OK;
}
DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void* buff) {
(void) pdrv;
switch (cmd) {
case CTRL_SYNC:
if (_ftl) {
_ftl->persist();
}
return RES_OK;
case GET_SECTOR_COUNT: {
LBA_t *p = (LBA_t *)buff;
if (_ftl) {
*p = _ftl->lbaCount();
} else {
*p = _fi->size() / 4096;
}
return RES_OK;
}
case GET_SECTOR_SIZE: {
WORD *w = (WORD *)buff;
*w = _sectorSize;
return RES_OK;
}
case GET_BLOCK_SIZE: {
DWORD *dw = (DWORD *)buff;
*dw = _sectorSize;
return RES_OK;
}
case CTRL_TRIM: {
LBA_t *lba = (LBA_t *)buff;
for (unsigned int i = lba[0]; i < lba[1]; i++) {
if (_ftl) {
_ftl->trim(i);
} else {
_fi->eraseBlock(i);
}
}
return RES_OK;
}
default:
return RES_PARERR;
}
}
DWORD get_fattime() {
time_t now;
if (fatfs::__fatfs_timeCallback) {
now = fatfs::__fatfs_timeCallback();
} else {
now = time(nullptr);
}
struct tm *stm = localtime(&now);
if (stm->tm_year < 80) {
// FAT can't report years before 1980
stm->tm_year = 80;
}
return (DWORD)(stm->tm_year - 80) << 25 |
(DWORD)(stm->tm_mon + 1) << 21 |
(DWORD)stm->tm_mday << 16 |
(DWORD)stm->tm_hour << 11 |
(DWORD)stm->tm_min << 5 |
(DWORD)stm->tm_sec >> 1;
}
}
This diff is collapsed.
/* -----------------------------------------------------------------------/
/ Low level disk interface modlue include file (C)ChaN, 2019 /
/-----------------------------------------------------------------------*/
#ifndef _DISKIO_DEFINED
#define _DISKIO_DEFINED
//#ifdef __cplusplus
//extern "C" {
//#endif
namespace fatfs {
/* 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, 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);
/* 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 FF_FS_READONLY == 0) */
#define GET_SECTOR_COUNT 1 /* Get media size (needed at FF_USE_MKFS == 1) */
#define GET_SECTOR_SIZE 2 /* Get sector size (needed at FF_MAX_SS != FF_MIN_SS) */
#define GET_BLOCK_SIZE 3 /* Get erase block size (needed at FF_USE_MKFS == 1) */
#define CTRL_TRIM 4 /* Inform device that the data on the block of sectors is no longer used (needed at FF_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
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
/*------------------------------------------------------------------------*/
/* A Sample Code of User Provided OS Dependent Functions for FatFs */
/*------------------------------------------------------------------------*/
#include "ff.h"
#if FF_USE_LFN == 3 /* Use dynamic memory allocation */
/*------------------------------------------------------------------------*/
/* Allocate/Free a Memory Block */
/*------------------------------------------------------------------------*/
#include <stdlib.h> /* with POSIX API */
void* ff_memalloc( /* Returns pointer to the allocated memory block (null if not enough core) */
UINT msize /* Number of bytes to allocate */
) {
return malloc((size_t)msize); /* Allocate a new memory block */
}
void ff_memfree(
void* mblock /* Pointer to the memory block to free (no effect if null) */
) {
free(mblock); /* Free the memory block */
}
#endif
#if FF_FS_REENTRANT /* Mutal exclusion */
/*------------------------------------------------------------------------*/
/* Definitions of Mutex */
/*------------------------------------------------------------------------*/
#define OS_TYPE 0 /* 0:Win32, 1:uITRON4.0, 2:uC/OS-II, 3:FreeRTOS, 4:CMSIS-RTOS */
#if OS_TYPE == 0 /* Win32 */
#include <windows.h>
static HANDLE Mutex[FF_VOLUMES + 1]; /* Table of mutex handle */
#elif OS_TYPE == 1 /* uITRON */
#include "itron.h"
#include "kernel.h"
static mtxid Mutex[FF_VOLUMES + 1]; /* Table of mutex ID */
#elif OS_TYPE == 2 /* uc/OS-II */
#include "includes.h"
static OS_EVENT *Mutex[FF_VOLUMES + 1]; /* Table of mutex pinter */
#elif OS_TYPE == 3 /* FreeRTOS */
#include "FreeRTOS.h"
#include "semphr.h"
static SemaphoreHandle_t Mutex[FF_VOLUMES + 1]; /* Table of mutex handle */
#elif OS_TYPE == 4 /* CMSIS-RTOS */
#include "cmsis_os.h"
static osMutexId Mutex[FF_VOLUMES + 1]; /* Table of mutex ID */
#endif
/*------------------------------------------------------------------------*/
/* Create a Mutex */
/*------------------------------------------------------------------------*/
/* This function is called in f_mount function to create a new mutex
/ or semaphore for the volume. When a 0 is returned, the f_mount function
/ fails with FR_INT_ERR.
*/
int ff_mutex_create( /* Returns 1:Function succeeded or 0:Could not create the mutex */
int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */
) {
#if OS_TYPE == 0 /* Win32 */
Mutex[vol] = CreateMutex(NULL, FALSE, NULL);
return (int)(Mutex[vol] != INVALID_HANDLE_VALUE);
#elif OS_TYPE == 1 /* uITRON */
T_CMTX cmtx = {TA_TPRI, 1};
Mutex[vol] = acre_mtx(&cmtx);
return (int)(Mutex[vol] > 0);
#elif OS_TYPE == 2 /* uC/OS-II */
OS_ERR err;
Mutex[vol] = OSMutexCreate(0, &err);
return (int)(err == OS_NO_ERR);
#elif OS_TYPE == 3 /* FreeRTOS */
Mutex[vol] = xSemaphoreCreateMutex();
return (int)(Mutex[vol] != NULL);
#elif OS_TYPE == 4 /* CMSIS-RTOS */
osMutexDef(cmsis_os_mutex);
Mutex[vol] = osMutexCreate(osMutex(cmsis_os_mutex));
return (int)(Mutex[vol] != NULL);
#endif
}
/*------------------------------------------------------------------------*/
/* Delete a Mutex */
/*------------------------------------------------------------------------*/
/* This function is called in f_mount function to delete a mutex or
/ semaphore of the volume created with ff_mutex_create function.
*/
void ff_mutex_delete( /* Returns 1:Function succeeded or 0:Could not delete due to an error */
int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */
) {
#if OS_TYPE == 0 /* Win32 */
CloseHandle(Mutex[vol]);
#elif OS_TYPE == 1 /* uITRON */
del_mtx(Mutex[vol]);
#elif OS_TYPE == 2 /* uC/OS-II */
OS_ERR err;
OSMutexDel(Mutex[vol], OS_DEL_ALWAYS, &err);
#elif OS_TYPE == 3 /* FreeRTOS */
vSemaphoreDelete(Mutex[vol]);
#elif OS_TYPE == 4 /* CMSIS-RTOS */
osMutexDelete(Mutex[vol]);
#endif
}
/*------------------------------------------------------------------------*/
/* Request a Grant to Access the Volume */
/*------------------------------------------------------------------------*/
/* This function is called on enter file functions to lock the volume.
/ When a 0 is returned, the file function fails with FR_TIMEOUT.
*/
int ff_mutex_take( /* Returns 1:Succeeded or 0:Timeout */
int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */
) {
#if OS_TYPE == 0 /* Win32 */
return (int)(WaitForSingleObject(Mutex[vol], FF_FS_TIMEOUT) == WAIT_OBJECT_0);
#elif OS_TYPE == 1 /* uITRON */
return (int)(tloc_mtx(Mutex[vol], FF_FS_TIMEOUT) == E_OK);
#elif OS_TYPE == 2 /* uC/OS-II */
OS_ERR err;
OSMutexPend(Mutex[vol], FF_FS_TIMEOUT, &err));
return (int)(err == OS_NO_ERR);
#elif OS_TYPE == 3 /* FreeRTOS */
return (int)(xSemaphoreTake(Mutex[vol], FF_FS_TIMEOUT) == pdTRUE);
#elif OS_TYPE == 4 /* CMSIS-RTOS */
return (int)(osMutexWait(Mutex[vol], FF_FS_TIMEOUT) == osOK);
#endif
}
/*------------------------------------------------------------------------*/
/* Release a Grant to Access the Volume */
/*------------------------------------------------------------------------*/
/* This function is called on leave file functions to unlock the volume.
*/
void ff_mutex_give(
int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */
) {
#if OS_TYPE == 0 /* Win32 */
ReleaseMutex(Mutex[vol]);
#elif OS_TYPE == 1 /* uITRON */
unl_mtx(Mutex[vol]);
#elif OS_TYPE == 2 /* uC/OS-II */
OSMutexPost(Mutex[vol]);
#elif OS_TYPE == 3 /* FreeRTOS */
xSemaphoreGive(Mutex[vol]);
#elif OS_TYPE == 4 /* CMSIS-RTOS */
osMutexRelease(Mutex[vol]);
#endif
}
#endif /* FF_FS_REENTRANT */
This diff is collapsed.
// FatFS + FatFSUSB listFiles example
#include <FatFS.h>
#include <FatFSUSB.h>
volatile bool updated = false;
volatile bool driveConnected = false;
volatile bool inPrinting = false;
// Called by FatFSUSB when the drive is released. We note this, restart FatFS, and tell the main loop to rescan.
void unplug(uint32_t i) {
(void) i;
driveConnected = false;
updated = true;
FatFS.begin();
}
// Called by FatFSUSB when the drive is mounted by the PC. Have to stop FatFS, since the drive data can change, note it, and continue.
void plug(uint32_t i) {
(void) i;
driveConnected = true;
FatFS.end();
}
// Called by FatFSUSB to determine if it is safe to let the PC mount the USB drive. If we're accessing the FS in any way, have any Files open, etc. then it's not safe to let the PC mount the drive.
bool mountable(uint32_t i) {
(void) i;
return !inPrinting;
}
void setup() {
Serial.begin(115200);
while (!Serial) {
delay(1);
}
delay(5000);
if (!FatFS.begin()) {
Serial.println("FatFS initialization failed!");
while (1) {
delay(1);
}
}
Serial.println("FatFS initialization done.");
inPrinting = true;
printDirectory("/", 0);
inPrinting = false;
// Set up callbacks
FatFSUSB.onUnplug(unplug);
FatFSUSB.onPlug(plug);
FatFSUSB.driveReady(mountable);
// Start FatFS USB drive mode
FatFSUSB.begin();
Serial.println("FatFSUSB started.");
Serial.println("Connect drive via USB to upload/erase files and re-display");
}
void loop() {
if (updated && !driveConnected) {
inPrinting = true;
Serial.println("\n\nDisconnected, new file listing:");
printDirectory("/", 0);
updated = false;
inPrinting = false;
}
}
void printDirectory(String dirName, int numTabs) {
Dir dir = FatFS.openDir(dirName);
while (true) {
if (!dir.next()) {
// no more files
break;
}
for (uint8_t i = 0; i < numTabs; i++) {
Serial.print('\t');
}
Serial.print(dir.fileName());
if (dir.isDirectory()) {
Serial.println("/");
printDirectory(dirName + "/" + dir.fileName(), numTabs + 1);
} else {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.print(dir.fileSize(), DEC);
time_t cr = dir.fileCreationTime();
struct tm* tmstruct = localtime(&cr);
Serial.printf("\t%d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
}
}
}
#######################################
# Syntax Coloring Map
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
FatFSUSB KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
driveReady KEYWORD1
onPlug KEYWORD1
onUnplug KEYWORD1
#######################################
# Constants (LITERAL1)
#######################################
name=FatFSUSB
version=1.0.0
author=Earle F. Philhower, III <earlephilhower@yahoo.com>
maintainer=Earle F. Philhower, III <earlephilhower@yahoo.com>
sentence=Allows using USB MSC (USB stick) to transfer an onboard flash file to a PC
paragraph=Emulates a USB stick and presents a single file for users to copy over/erase
category=Device Control
url=https://github.com/earlephilhower/arduino-pico
architectures=rp2040
dot_a_linkage=true
depends=FatFS
/*
FatFSUSB - Export onboard FatFS/FTL to host for data movement
Copyright (c) 2024 Earle F. Philhower, III. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "FatFSUSB.h"
#include <FatFS.h>
#include <class/msc/msc.h>
FatFSUSBClass FatFSUSB;
// Ensure we are logged in to the USB framework
void __USBInstallMassStorage() {
/* dummy */
}
FatFSUSBClass::FatFSUSBClass() {
}
FatFSUSBClass::~FatFSUSBClass() {
end();
}
void FatFSUSBClass::onPlug(void (*cb)(uint32_t), uint32_t cbData) {
_cbPlug = cb;
_cbPlugData = cbData;
}
void FatFSUSBClass::onUnplug(void (*cb)(uint32_t), uint32_t cbData) {
_cbUnplug = cb;
_cbUnplugData = cbData;
}
void FatFSUSBClass::driveReady(bool (*cb)(uint32_t), uint32_t cbData) {
_driveReady = cb;
_driveReadyData = cbData;
}
bool FatFSUSBClass::begin() {
if (_started) {
return false;
}
_started = true;
fatfs::disk_initialize(0);
fatfs::WORD ss;
fatfs::disk_ioctl(0, GET_SECTOR_SIZE, &ss);
_sectSize = ss;
_sectBuff = new uint8_t[_sectSize];
_sectNum = -1;
return true;
}
void FatFSUSBClass::end() {
if (_started) {
_started = false;
delete[] _sectBuff;
}
}
// Invoked to determine max LUN
extern "C" uint8_t tud_msc_get_maxlun_cb(void) {
return 1;
}
// Invoked when received SCSI_CMD_INQUIRY
// Application fill vendor id, product id and revision with string up to 8, 16, 4 characters respectively
extern "C" void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4]) {
(void) lun;
const char vid[] = "PicoDisk";
const char pid[] = "Mass Storage";
const char rev[] = "1.0";
memcpy(vendor_id, vid, strlen(vid));
memcpy(product_id, pid, strlen(pid));
memcpy(product_rev, rev, strlen(rev));
}
bool FatFSUSBClass::testUnitReady() {
bool ret = _started;
if (_driveReady) {
ret &= _driveReady(_driveReadyData);
}
return ret;
}
// Invoked when received Test Unit Ready command.
// return true allowing host to read/write this LUN e.g SD card inserted
extern "C" bool tud_msc_test_unit_ready_cb(uint8_t lun) {
(void) lun;
return FatFSUSB.testUnitReady();
}
// Invoked when received SCSI_CMD_READ_CAPACITY_10 and SCSI_CMD_READ_FORMAT_CAPACITY to determine the disk size
// Application update block count and block size
extern "C" void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_size) {
(void) lun;
fatfs::LBA_t p;
fatfs::disk_ioctl(0, GET_SECTOR_COUNT, &p);
*block_count = p;
fatfs::WORD ss;
fatfs::disk_ioctl(0, GET_SECTOR_SIZE, &ss);
*block_size = ss;
}
// Callback invoked when received READ10 command.
// Copy disk's data to buffer (up to bufsize) and return number of copied bytes.
extern "C" int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize) {
(void) lun;
return FatFSUSB.read10(lba, offset, buffer, bufsize);
}
int32_t FatFSUSBClass::read10(uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize) {
fatfs::LBA_t _hddsects;
fatfs::disk_ioctl(0, GET_SECTOR_COUNT, &_hddsects);
if (!_started || (lba >= _hddsects)) {
return -1;
}
assert(offset + bufsize <= _sectSize);
if (_sectNum >= 0) {
// Flush the temp data out, we need to use the space
fatfs::disk_write(0, _sectBuff, _sectNum, 1);
_sectNum = -1;
}
fatfs::disk_read(0, _sectBuff, lba, 1);
memcpy(buffer, _sectBuff + offset, bufsize);
return bufsize;
}
extern "C" bool tud_msc_is_writable_cb(uint8_t lun) {
(void) lun;
return true;
}
// Callback invoked when received WRITE10 command.
// Process data in buffer to disk's storage and return number of written bytes
extern "C" int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize) {
(void) lun;
return FatFSUSB.write10(lba, offset, buffer, bufsize);
}
int32_t FatFSUSBClass::write10(uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize) {
fatfs::LBA_t _hddsects;
fatfs::disk_ioctl(0, GET_SECTOR_COUNT, &_hddsects);
if (!_started || (lba >= _hddsects)) {
return -1;
}
assert(offset + bufsize <= _sectSize);
if ((offset == 0) && (bufsize == _sectSize)) {
fatfs::disk_write(0, buffer, lba, 1);
return _sectSize;
}
if ((int)_sectNum == (int)lba) {
memcpy(_sectBuff + offset, buffer, bufsize);
} else {
if (_sectNum >= 0) {
// Need to flush old sector out
fatfs::disk_write(0, _sectBuff, _sectNum, 1);
}
fatfs::disk_read(0, _sectBuff, lba, 1);
memcpy(_sectBuff + offset, buffer, bufsize);
_sectNum = lba;
}
if (offset + bufsize >= _sectSize) {
// We've filled up a sector, write it out!
fatfs::disk_write(0, _sectBuff, lba, 1);
_sectNum = -1;
}
return bufsize;
}
extern "C" bool tud_msc_set_sense(uint8_t lun, uint8_t sense_key, uint8_t add_sense_code, uint8_t add_sense_qualifier);
// Callback invoked when received an SCSI command not in built-in list below
// - READ_CAPACITY10, READ_FORMAT_CAPACITY, INQUIRY, MODE_SENSE6, REQUEST_SENSE
// - READ10 and WRITE10 has their own callbacks
extern "C" int32_t tud_msc_scsi_cb(uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize) {
const int SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL = 0x1E;
const int SCSI_CMD_START_STOP_UNIT = 0x1B;
const int SCSI_SENSE_ILLEGAL_REQUEST = 0x05;
void const* response = NULL;
int32_t resplen = 0;
// most scsi handled is input
bool in_xfer = true;
scsi_start_stop_unit_t const * start_stop = (scsi_start_stop_unit_t const *) scsi_cmd;
switch (scsi_cmd[0]) {
case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL:
// Host is about to read/write etc ... better not to disconnect disk
if (scsi_cmd[4] & 1) {
FatFSUSB.plug();
}
resplen = 0;
break;
case SCSI_CMD_START_STOP_UNIT:
// Host try to eject/safe remove/poweroff us. We could safely disconnect with disk storage, or go into lower power
if (!start_stop->start && start_stop->load_eject) {
FatFSUSB.unplug();
} else if (start_stop->start && start_stop->load_eject) {
FatFSUSB.plug();
}
resplen = 0;
break;
default:
// Set Sense = Invalid Command Operation
tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
// negative means error -> tinyusb could stall and/or response with failed status
resplen = -1;
break;
}
// return resplen must not larger than bufsize
if (resplen > bufsize) {
resplen = bufsize;
}
if (response && (resplen > 0)) {
if (in_xfer) {
memcpy(buffer, response, resplen);
} else {
// SCSI output
}
}
return resplen;
}
void FatFSUSBClass::plug() {
if (_started && _cbPlug) {
_cbPlug(_cbPlugData);
}
}
void FatFSUSBClass::unplug() {
if (_started) {
if (_sectNum >= 0) {
// Flush the temp data out
fatfs::disk_write(0, _sectBuff, _sectNum, 1);
_sectNum = -1;
}
fatfs::disk_ioctl(0, CTRL_SYNC, nullptr);
if (_cbUnplug) {
_cbUnplug(_cbUnplugData);
}
}
}
// Callback invoked on start/stop
extern "C" bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool load_eject) {
(void) lun;
(void) power_condition;
if (start && load_eject) {
FatFSUSB.plug();
} else if (!start && load_eject) {
FatFSUSB.unplug();
}
return true;
}
/*
FatFSUSB - Export onboard FatFS/FTL to host for data movement
Copyright (c) 2024 Earle F. Philhower, III. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#pragma once
#include <Arduino.h>
class FatFSUSBClass {
public:
FatFSUSBClass();
~FatFSUSBClass();
bool begin();
void end();
void driveReady(bool (*cb)(uint32_t), uint32_t cbData = 0);
void onPlug(void (*cb)(uint32_t), uint32_t cbData = 0);
void onUnplug(void (*cb)(uint32_t), uint32_t cbData = 0);
// Only for internal TinyUSB callback use
bool testUnitReady();
int32_t read10(uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize);
int32_t write10(uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize);
void plug();;
void unplug();
private:
bool _started = false;
int32_t _sectNum = -1;
uint8_t *_sectBuff = nullptr;
uint16_t _sectSize = 0;
void (*_cbPlug)(uint32_t) = nullptr;
uint32_t _cbPlugData = 0;
void (*_cbUnplug)(uint32_t) = nullptr;
uint32_t _cbUnplugData = 0;
bool (*_driveReady)(uint32_t) = nullptr;
uint32_t _driveReadyData = 0;
};
extern FatFSUSBClass FatFSUSB;
......@@ -14,7 +14,8 @@ for dir in ./cores/rp2040 ./libraries/EEPROM ./libraries/I2S ./libraries/SingleF
./libraries/MouseBT ./libraries/SerialBT ./libraries/HID_Bluetooth \
./libraries/JoystickBLE ./libraries/KeyboardBLE ./libraries/MouseBLE \
./libraries/lwIP_w5500 ./libraries/lwIP_w5100 ./libraries/lwIP_enc28j60 \
./libraries/SPISlave ./libraries/lwIP_ESPHost; do
./libraries/SPISlave ./libraries/lwIP_ESPHost ./libraries/FatFS\
./libraries/FatFSUSB; do
find $dir -type f \( -name "*.c" -o -name "*.h" -o -name "*.cpp" \) -a \! -path '*api*' -exec astyle --suffix=none --options=./tests/astyle_core.conf \{\} \;
find $dir -type f -name "*.ino" -exec astyle --suffix=none --options=./tests/astyle_examples.conf \{\} \;
done
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment