Skip to content

Commit 08c6ec9

Browse files
authored
Merge pull request #209 from adafruit/sdfat_v2
Update msc sdfat example to work with SdFat v2
2 parents cdf49a2 + 62a75b7 commit 08c6ec9

File tree

7 files changed

+234
-96
lines changed

7 files changed

+234
-96
lines changed

examples/MassStorage/msc_esp32_file_browser/msc_esp32_file_browser.ino

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Adafruit_FlashTransport_ESP32 flashTransport;
6060
Adafruit_SPIFlash flash(&flashTransport);
6161

6262
// file system object from SdFat
63-
FatFileSystem fatfs;
63+
FatVolume fatfs;
6464

6565
// USB Mass Storage object
6666
Adafruit_USBD_MSC usb_msc;
@@ -71,7 +71,7 @@ bool fs_changed; // Set to true when browser write to flash
7171
const char* host = "esp32fs";
7272
WebServer server(80);
7373
//holds the current upload
74-
File fsUploadFile;
74+
File32 fsUploadFile;
7575

7676
//--------------------------------------------------------------------+
7777
// Setup
@@ -246,7 +246,7 @@ String getContentType(String filename) {
246246

247247
bool exists(String path){
248248
bool yes = false;
249-
File file = fatfs.open(path, O_READ);
249+
File32 file = fatfs.open(path, O_READ);
250250
if(file && !file.isDirectory()){
251251
yes = true;
252252
}
@@ -265,7 +265,7 @@ bool handleFileRead(String path) {
265265
// if (exists(pathWithGz)) {
266266
// path += ".gz";
267267
// }
268-
File file = fatfs.open(path, O_READ);
268+
File32 file = fatfs.open(path, O_READ);
269269
server.streamFile(file, contentType);
270270
file.close();
271271
return true;
@@ -333,7 +333,7 @@ void handleFileCreate() {
333333
if (exists(path)) {
334334
return server.send(500, "text/plain", "FILE EXISTS");
335335
}
336-
File file = fatfs.open(path, O_WRITE | O_CREAT);
336+
File32 file = fatfs.open(path, O_WRITE | O_CREAT);
337337
if (file) {
338338
file.close();
339339
} else {
@@ -352,12 +352,12 @@ void handleFileList() {
352352
String path = server.arg("dir");
353353
DBG_SERIAL.println("handleFileList: " + path);
354354

355-
File root = fatfs.open(path);
355+
File32 root = fatfs.open(path);
356356
path = String();
357357

358358
String output = "[";
359359
if(root.isDirectory()){
360-
File file = root.openNextFile();
360+
File32 file = root.openNextFile();
361361
char fname[256];
362362
while(file){
363363
if (output != "[") {
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2022 Ha Thach (tinyusb.org) for Adafruit Industries
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
#ifndef FLASH_CONFIG_H_
26+
#define FLASH_CONFIG_H_
27+
28+
// Un-comment to run example with custom SPI and SS e.g with FRAM breakout
29+
// #define CUSTOM_CS A5
30+
// #define CUSTOM_SPI SPI
31+
32+
#if defined(CUSTOM_CS) && defined(CUSTOM_SPI)
33+
Adafruit_FlashTransport_SPI flashTransport(CUSTOM_CS, CUSTOM_SPI);
34+
35+
#elif defined(ARDUINO_ARCH_ESP32)
36+
37+
// ESP32 use same flash device that store code for file system.
38+
// SPIFlash will parse partition.cvs to detect FATFS partition to use
39+
Adafruit_FlashTransport_ESP32 flashTransport;
40+
41+
#elif defined(ARDUINO_ARCH_RP2040)
42+
43+
// RP2040 use same flash device that store code for file system. Therefore we
44+
// only need to specify start address and size (no need SPI or SS)
45+
// By default (start=0, size=0), values that match file system setting in
46+
// 'Tools->Flash Size' menu selection will be used.
47+
Adafruit_FlashTransport_RP2040 flashTransport;
48+
49+
// To be compatible with CircuitPython partition scheme (start_address = 1 MB,
50+
// size = total flash - 1 MB) use const value (CPY_START_ADDR, CPY_SIZE) or
51+
// subclass Adafruit_FlashTransport_RP2040_CPY. Un-comment either of the
52+
// following line:
53+
// Adafruit_FlashTransport_RP2040
54+
// flashTransport(Adafruit_FlashTransport_RP2040::CPY_START_ADDR,
55+
// Adafruit_FlashTransport_RP2040::CPY_SIZE);
56+
// Adafruit_FlashTransport_RP2040_CPY flashTransport;
57+
58+
#else
59+
60+
// On-board external flash (QSPI or SPI) macros should already
61+
// defined in your board variant if supported
62+
// - EXTERNAL_FLASH_USE_QSPI
63+
// - EXTERNAL_FLASH_USE_CS/EXTERNAL_FLASH_USE_SPI
64+
65+
#if defined(EXTERNAL_FLASH_USE_QSPI)
66+
67+
Adafruit_FlashTransport_QSPI flashTransport;
68+
69+
#elif defined(EXTERNAL_FLASH_USE_SPI)
70+
71+
Adafruit_FlashTransport_SPI flashTransport(EXTERNAL_FLASH_USE_CS,
72+
EXTERNAL_FLASH_USE_SPI);
73+
74+
#else
75+
#error No (Q)SPI flash are defined for your board !
76+
#endif
77+
#endif
78+
79+
#endif

examples/MassStorage/msc_external_flash/msc_external_flash.ino

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -27,49 +27,13 @@
2727
#include "Adafruit_SPIFlash.h"
2828
#include "Adafruit_TinyUSB.h"
2929

30-
// Un-comment to run example with custom SPI SPI and SS e.g with FRAM breakout
31-
// #define CUSTOM_CS A5
32-
// #define CUSTOM_SPI SPI
33-
34-
#if defined(CUSTOM_CS) && defined(CUSTOM_SPI)
35-
Adafruit_FlashTransport_SPI flashTransport(CUSTOM_CS, CUSTOM_SPI);
36-
37-
#elif defined(ARDUINO_ARCH_ESP32)
38-
// ESP32 use same flash device that store code.
39-
// Therefore there is no need to specify the SPI and SS
40-
Adafruit_FlashTransport_ESP32 flashTransport;
41-
42-
#elif defined(ARDUINO_ARCH_RP2040)
43-
// RP2040 use same flash device that store code.
44-
// Therefore there is no need to specify the SPI and SS
45-
// Use default (no-args) constructor to be compatible with CircuitPython partition scheme
46-
Adafruit_FlashTransport_RP2040 flashTransport;
47-
48-
// For generic usage:
49-
// Adafruit_FlashTransport_RP2040 flashTransport(start_address, size)
50-
// If start_address and size are both 0, value that match filesystem setting in
51-
// 'Tools->Flash Size' menu selection will be used
52-
53-
#else
54-
// On-board external flash (QSPI or SPI) macros should already
55-
// defined in your board variant if supported
56-
// - EXTERNAL_FLASH_USE_QSPI
57-
// - EXTERNAL_FLASH_USE_CS/EXTERNAL_FLASH_USE_SPI
58-
#if defined(EXTERNAL_FLASH_USE_QSPI)
59-
Adafruit_FlashTransport_QSPI flashTransport;
60-
61-
#elif defined(EXTERNAL_FLASH_USE_SPI)
62-
Adafruit_FlashTransport_SPI flashTransport(EXTERNAL_FLASH_USE_CS, EXTERNAL_FLASH_USE_SPI);
63-
64-
#else
65-
#error No QSPI/SPI flash are defined on your board variant.h !
66-
#endif
67-
#endif
30+
// for flashTransport definition
31+
#include "flash_config.h"
6832

6933
Adafruit_SPIFlash flash(&flashTransport);
7034

7135
// file system object from SdFat
72-
FatFileSystem fatfs;
36+
FatVolume fatfs;
7337

7438
FatFile root;
7539
FatFile file;
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2022 Ha Thach (tinyusb.org) for Adafruit Industries
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
#ifndef FLASH_CONFIG_H_
26+
#define FLASH_CONFIG_H_
27+
28+
// Un-comment to run example with custom SPI and SS e.g with FRAM breakout
29+
// #define CUSTOM_CS A5
30+
// #define CUSTOM_SPI SPI
31+
32+
#if defined(CUSTOM_CS) && defined(CUSTOM_SPI)
33+
Adafruit_FlashTransport_SPI flashTransport(CUSTOM_CS, CUSTOM_SPI);
34+
35+
#elif defined(ARDUINO_ARCH_ESP32)
36+
37+
// ESP32 use same flash device that store code for file system.
38+
// SPIFlash will parse partition.cvs to detect FATFS partition to use
39+
Adafruit_FlashTransport_ESP32 flashTransport;
40+
41+
#elif defined(ARDUINO_ARCH_RP2040)
42+
43+
// RP2040 use same flash device that store code for file system. Therefore we
44+
// only need to specify start address and size (no need SPI or SS)
45+
// By default (start=0, size=0), values that match file system setting in
46+
// 'Tools->Flash Size' menu selection will be used.
47+
Adafruit_FlashTransport_RP2040 flashTransport;
48+
49+
// To be compatible with CircuitPython partition scheme (start_address = 1 MB,
50+
// size = total flash - 1 MB) use const value (CPY_START_ADDR, CPY_SIZE) or
51+
// subclass Adafruit_FlashTransport_RP2040_CPY. Un-comment either of the
52+
// following line:
53+
// Adafruit_FlashTransport_RP2040
54+
// flashTransport(Adafruit_FlashTransport_RP2040::CPY_START_ADDR,
55+
// Adafruit_FlashTransport_RP2040::CPY_SIZE);
56+
// Adafruit_FlashTransport_RP2040_CPY flashTransport;
57+
58+
#else
59+
60+
// On-board external flash (QSPI or SPI) macros should already
61+
// defined in your board variant if supported
62+
// - EXTERNAL_FLASH_USE_QSPI
63+
// - EXTERNAL_FLASH_USE_CS/EXTERNAL_FLASH_USE_SPI
64+
65+
#if defined(EXTERNAL_FLASH_USE_QSPI)
66+
67+
Adafruit_FlashTransport_QSPI flashTransport;
68+
69+
#elif defined(EXTERNAL_FLASH_USE_SPI)
70+
71+
Adafruit_FlashTransport_SPI flashTransport(EXTERNAL_FLASH_USE_CS,
72+
EXTERNAL_FLASH_USE_SPI);
73+
74+
#else
75+
#error No (Q)SPI flash are defined for your board !
76+
#endif
77+
#endif
78+
79+
#endif

0 commit comments

Comments
 (0)