Skip to content

Commit d74df4e

Browse files
committed
SPI port selectable from variant
1 parent 7ef3f44 commit d74df4e

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

src/utility/Sd2Card.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
//------------------------------------------------------------------------------
2424
#ifndef SOFTWARE_SPI
2525
#ifdef USE_SPI_LIB
26+
27+
#ifndef SDCARD_SPI
28+
#define SDCARD_SPI SPI
29+
#endif
30+
2631
#include <SPI.h>
2732
static SPISettings settings;
2833
#endif
@@ -34,7 +39,7 @@ static void spiSend(uint8_t b) {
3439
while (!(SPSR & (1 << SPIF)))
3540
;
3641
#else
37-
SPI.transfer(b);
42+
SDCARD_SPI.transfer(b);
3843
#endif
3944
}
4045
/** Receive a byte from the card */
@@ -43,7 +48,7 @@ static uint8_t spiRec(void) {
4348
spiSend(0XFF);
4449
return SPDR;
4550
#else
46-
return SPI.transfer(0xFF);
51+
return SDCARD_SPI.transfer(0xFF);
4752
#endif
4853
}
4954
#else // SOFTWARE_SPI
@@ -164,7 +169,7 @@ void Sd2Card::chipSelectHigh(void) {
164169
#ifdef USE_SPI_LIB
165170
if (chip_select_asserted) {
166171
chip_select_asserted = 0;
167-
SPI.endTransaction();
172+
SDCARD_SPI.endTransaction();
168173
}
169174
#endif
170175
}
@@ -173,7 +178,7 @@ void Sd2Card::chipSelectLow(void) {
173178
#ifdef USE_SPI_LIB
174179
if (!chip_select_asserted) {
175180
chip_select_asserted = 1;
176-
SPI.beginTransaction(settings);
181+
SDCARD_SPI.beginTransaction(settings);
177182
}
178183
#endif
179184
digitalWrite(chipSelectPin_, LOW);
@@ -265,18 +270,18 @@ uint8_t Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) {
265270
// clear double speed
266271
SPSR &= ~(1 << SPI2X);
267272
#else // USE_SPI_LIB
268-
SPI.begin();
273+
SDCARD_SPI.begin();
269274
settings = SPISettings(250000, MSBFIRST, SPI_MODE0);
270275
#endif // USE_SPI_LIB
271276
#endif // SOFTWARE_SPI
272277

273278
// must supply min of 74 clock cycles with CS high.
274279
#ifdef USE_SPI_LIB
275-
SPI.beginTransaction(settings);
280+
SDCARD_SPI.beginTransaction(settings);
276281
#endif
277282
for (uint8_t i = 0; i < 10; i++) spiSend(0XFF);
278283
#ifdef USE_SPI_LIB
279-
SPI.endTransaction();
284+
SDCARD_SPI.endTransaction();
280285
#endif
281286

282287
chipSelectLow();

src/utility/Sd2Card.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,27 @@ uint8_t const SPI_QUARTER_SPEED = 2;
6565
* as an output by init(). An avr processor will not function as an SPI
6666
* master unless SS is set to output mode.
6767
*/
68+
#ifndef SDCARD_SS_PIN
6869
/** The default chip select pin for the SD card is SS. */
6970
uint8_t const SD_CHIP_SELECT_PIN = SS;
71+
#else
72+
uint8_t const SD_CHIP_SELECT_PIN = SDCARD_SS_PIN;
73+
#endif
7074

7175
// The following three pins must not be redefined for hardware SPI,
7276
// so ensure that they are taken from pins_arduino.h or variant.h, depending on architecture.
77+
#ifndef SDCARD_MOSI_PIN
7378
/** SPI Master Out Slave In pin */
7479
uint8_t const SPI_MOSI_PIN = MOSI;
7580
/** SPI Master In Slave Out pin */
7681
uint8_t const SPI_MISO_PIN = MISO;
7782
/** SPI Clock pin */
7883
uint8_t const SPI_SCK_PIN = SCK;
84+
#else
85+
uint8_t const SPI_MOSI_PIN = SDCARD_MOSI_PIN;
86+
uint8_t const SPI_MISO_PIN = SDCARD_MISO_PIN;
87+
uint8_t const SPI_SCK_PIN = SDCARD_SCK_PIN;
88+
#endif
7989

8090
/** optimize loops for hardware SPI */
8191
#ifndef USE_SPI_LIB

0 commit comments

Comments
 (0)