Skip to content

Commit f7a565d

Browse files
Use SPI transactions and SPISettings in SD library
1 parent a0f5a2e commit f7a565d

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

libraries/SD/src/utility/Sd2Card.cpp

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#ifndef SOFTWARE_SPI
2525
#ifdef USE_SPI_LIB
2626
#include <SPI.h>
27+
static SPISettings settings;
2728
#endif
2829
// functions for hardware SPI
2930
/** Send a byte to the card */
@@ -158,9 +159,15 @@ uint32_t Sd2Card::cardSize(void) {
158159
//------------------------------------------------------------------------------
159160
void Sd2Card::chipSelectHigh(void) {
160161
digitalWrite(chipSelectPin_, HIGH);
162+
#ifdef USE_SPI_LIB
163+
SPI.endTransaction();
164+
#endif
161165
}
162166
//------------------------------------------------------------------------------
163167
void Sd2Card::chipSelectLow(void) {
168+
#ifdef USE_SPI_LIB
169+
SPI.beginTransaction(settings);
170+
#endif
164171
digitalWrite(chipSelectPin_, LOW);
165172
}
166173
//------------------------------------------------------------------------------
@@ -233,7 +240,7 @@ uint8_t Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) {
233240

234241
// set pin modes
235242
pinMode(chipSelectPin_, OUTPUT);
236-
chipSelectHigh();
243+
digitalWrite(chipSelectPin_, HIGH);
237244
#ifndef USE_SPI_LIB
238245
pinMode(SPI_MISO_PIN, INPUT);
239246
pinMode(SPI_MOSI_PIN, OUTPUT);
@@ -251,16 +258,18 @@ uint8_t Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) {
251258
SPSR &= ~(1 << SPI2X);
252259
#else // USE_SPI_LIB
253260
SPI.begin();
254-
#ifdef SPI_CLOCK_DIV128
255-
SPI.setClockDivider(SPI_CLOCK_DIV128);
256-
#else
257-
SPI.setClockDivider(255);
258-
#endif
261+
settings = SPISettings(250000, MSBFIRST, SPI_MODE0);
259262
#endif // USE_SPI_LIB
260263
#endif // SOFTWARE_SPI
261264

262265
// must supply min of 74 clock cycles with CS high.
266+
#ifdef USE_SPI_LIB
267+
SPI.beginTransaction(settings);
268+
#endif
263269
for (uint8_t i = 0; i < 10; i++) spiSend(0XFF);
270+
#ifdef USE_SPI_LIB
271+
SPI.endTransaction();
272+
#endif
264273

265274
chipSelectLow();
266275

@@ -497,21 +506,15 @@ uint8_t Sd2Card::setSckRate(uint8_t sckRateID) {
497506
SPCR |= (sckRateID & 4 ? (1 << SPR1) : 0)
498507
| (sckRateID & 2 ? (1 << SPR0) : 0);
499508
#else // USE_SPI_LIB
500-
int v;
501-
#ifdef SPI_CLOCK_DIV128
502509
switch (sckRateID) {
503-
case 0: v=SPI_CLOCK_DIV2; break;
504-
case 1: v=SPI_CLOCK_DIV4; break;
505-
case 2: v=SPI_CLOCK_DIV8; break;
506-
case 3: v=SPI_CLOCK_DIV16; break;
507-
case 4: v=SPI_CLOCK_DIV32; break;
508-
case 5: v=SPI_CLOCK_DIV64; break;
509-
case 6: v=SPI_CLOCK_DIV128; break;
510-
}
511-
#else // SPI_CLOCK_DIV128
512-
v = 2 << sckRateID;
513-
#endif // SPI_CLOCK_DIV128
514-
SPI.setClockDivider(v);
510+
case 0: settings = SPISettings(25000000, MSBFIRST, SPI_MODE0); break;
511+
case 1: settings = SPISettings(4000000, MSBFIRST, SPI_MODE0); break;
512+
case 2: settings = SPISettings(2000000, MSBFIRST, SPI_MODE0); break;
513+
case 3: settings = SPISettings(1000000, MSBFIRST, SPI_MODE0); break;
514+
case 4: settings = SPISettings(500000, MSBFIRST, SPI_MODE0); break;
515+
case 5: settings = SPISettings(250000, MSBFIRST, SPI_MODE0); break;
516+
default: settings = SPISettings(125000, MSBFIRST, SPI_MODE0);
517+
}
515518
#endif // USE_SPI_LIB
516519
return true;
517520
}

0 commit comments

Comments
 (0)