Skip to content

Commit 5787b4c

Browse files
Allow selecting SPI port for SD/SDFS filesystem (#759)
Fixes #758
1 parent e947895 commit 5787b4c

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

docs/fs.rst

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,19 @@ The included ``SD`` library is the Arduino standard one. Please refer to
9898
the [Arduino SD reference](https://www.arduino.cc/en/reference/SD) for
9999
more information.
100100

101+
Using Second SPI port for SD
102+
----------------------------
103+
The ``SD`` library ``begin()`` has been modified to allow you to use the
104+
second SPI port, ``SPI1``. Just use the following call in place of
105+
``SD.begin(cspin)``
106+
107+
.. code:: cpp
108+
109+
SD.begin(cspin, SPI1);
110+
101111
102112
File system object (LittleFS/SD/SDFS)
103-
--------------------------------------------
113+
-------------------------------------
104114

105115
setConfig
106116
~~~~~~~~~

docs/ota.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ A firmware file is uploaded via any method (Ethernet, WiFi, serial ZModem, etc.)
233233

234234
The ROM layout consists of:
235235

236-
... code:
236+
.. code:: cpp
237237
238238
[boot2.S] [OTA Bootloader] [0-pad] [OTA partition table] [Main sketch] [LittleFS filesystem] [EEPROM]
239239

libraries/SD/src/SD.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@
3232

3333
class SDClass {
3434
public:
35-
boolean begin(uint8_t csPin, uint32_t cfg = SPI_HALF_SPEED) {
36-
SDFS.setConfig(SDFSConfig(csPin, cfg));
35+
boolean begin(uint8_t csPin, HardwareSPI &spi) {
36+
SDFS.setConfig(SDFSConfig(csPin, SPI_HALF_SPEED, spi));
37+
return (boolean)SDFS.begin();
38+
}
39+
boolean begin(uint8_t csPin, uint32_t cfg = SPI_HALF_SPEED, HardwareSPI &spi = SPI) {
40+
SDFS.setConfig(SDFSConfig(csPin, cfg, spi));
3741
return (boolean)SDFS.begin();
3842
}
3943

libraries/SDFS/src/SDFS.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class SDFSConfig : public FSConfig {
4545
public:
4646
static constexpr uint32_t FSId = 0x53444653;
4747

48-
SDFSConfig(uint8_t csPin = 4, uint32_t spi = SD_SCK_MHZ(10)) : FSConfig(FSId, false), _csPin(csPin), _part(0), _spiSettings(spi) { }
48+
SDFSConfig(uint8_t csPin = 4, uint32_t spi = SD_SCK_MHZ(10), HardwareSPI &port = SPI) : FSConfig(FSId, false), _csPin(csPin), _part(0), _spiSettings(spi), _spi(&port) { }
4949

5050
SDFSConfig setAutoFormat(bool val = true) {
5151
_autoFormat = val;
@@ -55,10 +55,14 @@ class SDFSConfig : public FSConfig {
5555
_csPin = pin;
5656
return *this;
5757
}
58-
SDFSConfig setSPI(uint32_t spi) {
58+
SDFSConfig setSPISpeed(uint32_t spi) {
5959
_spiSettings = spi;
6060
return *this;
6161
}
62+
SDFSConfig setSPI(HardwareSPI &spi) {
63+
_spi = &spi;
64+
return true;
65+
}
6266
SDFSConfig setPart(uint8_t part) {
6367
_part = part;
6468
return *this;
@@ -68,6 +72,7 @@ class SDFSConfig : public FSConfig {
6872
uint8_t _csPin;
6973
uint8_t _part;
7074
uint32_t _spiSettings;
75+
HardwareSPI *_spi;
7176
};
7277

7378
class SDFSImpl : public FSImpl {
@@ -146,10 +151,11 @@ class SDFSImpl : public FSImpl {
146151
if (_mounted) {
147152
return true;
148153
}
149-
_mounted = _fs.begin(_cfg._csPin, _cfg._spiSettings);
154+
SdSpiConfig ssc(_cfg._csPin, SHARED_SPI, _cfg._spiSettings, _cfg._spi);
155+
_mounted = _fs.begin(ssc);
150156
if (!_mounted && _cfg._autoFormat) {
151157
format();
152-
_mounted = _fs.begin(_cfg._csPin, _cfg._spiSettings);
158+
_mounted = _fs.begin(ssc);
153159
}
154160
FsDateTime::setCallback(dateTimeCB);
155161
return _mounted;

0 commit comments

Comments
 (0)