Skip to content

Commit ec220d9

Browse files
Merge remote-tracking branch 'upstream/master'
2 parents befaefc + 0007856 commit ec220d9

File tree

25 files changed

+798
-195
lines changed

25 files changed

+798
-195
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ Freescale:
4242
* [KL46Z](https://mbed.org/platforms/FRDM-KL46Z/) (Cortex-M0+)
4343

4444
STMicroelectronics:
45+
* [Nucleo-F103RB](https://mbed.org/platforms/ST-Nucleo-F103RB/) (Cortex-M3)
46+
* [Nucleo-L152RE](https://mbed.org/platforms/ST-Nucleo-L152RE/) (Cortex-M3)
47+
* [Nucleo-F030R8](https://mbed.org/platforms/ST-Nucleo-F030R8/) (Cortex-M0)
48+
* [Nucleo-F401RE](https://mbed.org/platforms/ST-Nucleo-F401RE/) (Cortex-M4)
4549
* STM32F407 (Cortex-M4)
4650

4751
Supported Toolchains and IDEs

libraries/USBDevice/USBDevice/USBDevice.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -703,12 +703,15 @@ bool USBDevice::configured(void)
703703
return (device.state == CONFIGURED);
704704
}
705705

706-
void USBDevice::connect(void)
706+
void USBDevice::connect(bool blocking)
707707
{
708708
/* Connect device */
709709
USBHAL::connect();
710-
/* Block if not configured */
711-
while (!configured());
710+
711+
if (blocking) {
712+
/* Block if not configured */
713+
while (!configured());
714+
}
712715
}
713716

714717
void USBDevice::disconnect(void)

libraries/USBDevice/USBDevice/USBDevice.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ class USBDevice: public USBHAL
3737

3838
/*
3939
* Connect a device
40+
*
41+
* @param blocking: block if not configured
4042
*/
41-
void connect(void);
43+
void connect(bool blocking = true);
4244

4345
/*
4446
* Disconnect a device

libraries/USBDevice/USBMSD/USBMSD.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ bool USBMSD::USBCallback_request(void) {
103103
}
104104

105105

106-
bool USBMSD::connect() {
107-
106+
bool USBMSD::connect(bool blocking) {
108107
//disk initialization
109108
if (disk_status() & NO_INIT) {
110109
if (disk_initialize()) {
@@ -131,7 +130,7 @@ bool USBMSD::connect() {
131130
}
132131

133132
//connect the device
134-
USBDevice::connect();
133+
USBDevice::connect(blocking);
135134
return true;
136135
}
137136

libraries/USBDevice/USBMSD/USBMSD.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ class USBMSD: public USBDevice {
7070
/**
7171
* Connect the USB MSD device. Establish disk initialization before really connect the device.
7272
*
73+
* @param blocking if not configured
7374
* @returns true if successful
7475
*/
75-
bool connect();
76+
bool connect(bool blocking = true);
7677

7778
/**
7879
* Disconnect the USB MSD device.

libraries/fs/sd/SDFileSystem.cpp

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,12 @@
120120
#define SD_DBG 0
121121

122122
SDFileSystem::SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name) :
123-
FATFileSystem(name), _spi(mosi, miso, sclk), _cs(cs) {
123+
FATFileSystem(name), _spi(mosi, miso, sclk), _cs(cs), _is_initialized(0) {
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);
@@ -200,21 +204,30 @@ int SDFileSystem::initialise_card_v2() {
200204
}
201205

202206
int SDFileSystem::disk_initialize() {
203-
int i = initialise_card();
204-
debug_if(SD_DBG, "init card = %d\n", i);
207+
_is_initialized = initialise_card();
208+
if (_is_initialized == 0) {
209+
debug("Fail to initialize card\n");
210+
return 1;
211+
}
212+
debug_if(SD_DBG, "init card = %d\n", _is_initialized);
205213
_sectors = _sd_sectors();
206214

207215
// Set block length to 512 (CMD16)
208216
if (_cmd(16, 512) != 0) {
209217
debug("Set 512-byte block timed out\n");
210218
return 1;
211219
}
212-
213-
_spi.frequency(1000000); // Set to 1MHz for data transfer
220+
221+
// Set SCK for data transfer
222+
_spi.frequency(_transfer_sck);
214223
return 0;
215224
}
216225

217226
int SDFileSystem::disk_write(const uint8_t *buffer, uint64_t block_number) {
227+
if (!_is_initialized) {
228+
return -1;
229+
}
230+
218231
// set write address for single block (CMD24)
219232
if (_cmd(24, block_number * cdv) != 0) {
220233
return 1;
@@ -226,6 +239,10 @@ int SDFileSystem::disk_write(const uint8_t *buffer, uint64_t block_number) {
226239
}
227240

228241
int SDFileSystem::disk_read(uint8_t *buffer, uint64_t block_number) {
242+
if (!_is_initialized) {
243+
return -1;
244+
}
245+
229246
// set read address for single block (CMD17)
230247
if (_cmd(17, block_number * cdv) != 0) {
231248
return 1;
@@ -236,7 +253,15 @@ int SDFileSystem::disk_read(uint8_t *buffer, uint64_t block_number) {
236253
return 0;
237254
}
238255

239-
int SDFileSystem::disk_status() { return 0; }
256+
int SDFileSystem::disk_status() {
257+
// FATFileSystem::disk_status() returns 0 when initialized
258+
if (_is_initialized) {
259+
return 0;
260+
} else {
261+
return 1;
262+
}
263+
}
264+
240265
int SDFileSystem::disk_sync() { return 0; }
241266
uint64_t SDFileSystem::disk_sectors() { return _sectors; }
242267

libraries/fs/sd/SDFileSystem.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,17 @@ 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;
86+
int _is_initialized;
8087
};
8188

8289
#endif
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
LR_IROM1 0x00000000 0x8000 { ; load region size_region (32k)
2+
ER_IROM1 0x00000000 0x8000 { ; load address = execution address
3+
*.o (RESET, +First)
4+
*(InRoot$$Sections)
5+
.ANY (+RO)
6+
}
7+
; 8_byte_aligned(48 vect * 4 bytes) = 8_byte_aligned(0xC0) = 0xC0
8+
; 0x1000 - 0xC0 = 0xF40
9+
RW_IRAM1 0x1FFFFCC0 0xF40 {
10+
.ANY (+RW +ZI)
11+
}
12+
}

0 commit comments

Comments
 (0)