Skip to content

Commit 1e548ab

Browse files
committed
libraries/sd: Allow changing the default clock settings for the initialization and transfer states.
1 parent b87dac9 commit 1e548ab

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

libraries/fs/sd/SDFileSystem.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@
122122
SDFileSystem::SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name) :
123123
FATFileSystem(name), _spi(mosi, miso, sclk), _cs(cs) {
124124
_cs = 1;
125+
126+
// Set default to 100kHz for initialisation and 1MHz for data transfer
127+
_init_sck = 100000;
128+
_transfer_sck = 1000000;
125129
}
126130

127131
#define R1_IDLE_STATE (1 << 0)
@@ -143,8 +147,8 @@ SDFileSystem::SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs,
143147
#define SDCARD_V2HC 3
144148

145149
int SDFileSystem::initialise_card() {
146-
// Set to 100kHz for initialisation, and clock card with cs = 1
147-
_spi.frequency(100000);
150+
// Set to SCK for initialisation, and clock card with cs = 1
151+
_spi.frequency(_init_sck);
148152
_cs = 1;
149153
for (int i = 0; i < 16; i++) {
150154
_spi.write(0xFF);
@@ -209,8 +213,9 @@ int SDFileSystem::disk_initialize() {
209213
debug("Set 512-byte block timed out\n");
210214
return 1;
211215
}
212-
213-
_spi.frequency(1000000); // Set to 1MHz for data transfer
216+
217+
// Set SCK for data transfer
218+
_spi.frequency(_transfer_sck);
214219
return 0;
215220
}
216221

libraries/fs/sd/SDFileSystem.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,13 @@ class SDFileSystem : public FATFileSystem {
7373
int _write(const uint8_t *buffer, uint32_t length);
7474
uint64_t _sd_sectors();
7575
uint64_t _sectors;
76-
76+
77+
void set_init_sck(uint32_t sck) { _init_sck = sck; }
78+
// Note: The highest SPI clock rate is 20 MHz for MMC and 25 MHz for SD
79+
void set_transfer_sck(uint32_t sck) { _transfer_sck = sck; }
80+
uint32_t _init_sck;
81+
uint32_t _transfer_sck;
82+
7783
SPI _spi;
7884
DigitalOut _cs;
7985
int cdv;

0 commit comments

Comments
 (0)