Skip to content

Commit 70d7a83

Browse files
committed
stm: If SD card inserted on hard reset, it is the medium for USB MSD.
1 parent a8d404e commit 70d7a83

5 files changed

Lines changed: 226 additions & 185 deletions

File tree

stm/main.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <stm32f4xx_rtc.h>
1111
#include <stm32f4xx_usart.h>
1212
#include <stm32f4xx_rng.h>
13+
#include <usbd_storage_msd.h>
1314
#include <stm_misc.h>
1415
#include "std.h"
1516

@@ -815,14 +816,6 @@ int main(void) {
815816
flash_error(4);
816817
}
817818

818-
#ifdef USE_HOST_MODE
819-
// USB host
820-
pyb_usb_host_init();
821-
#elif defined(USE_DEVICE_MODE)
822-
// USB device
823-
pyb_usb_dev_init();
824-
#endif
825-
826819
if (first_soft_reset) {
827820
#if MICROPY_HW_HAS_MMA7660
828821
// MMA: init and reset address to zero
@@ -839,10 +832,23 @@ int main(void) {
839832
FRESULT res = f_mount(&fatfs1, "1:", 1);
840833
if (res != FR_OK) {
841834
printf("[SD] could not mount SD card\n");
835+
} else {
836+
if (first_soft_reset) {
837+
// use SD card as medium for the USB MSD
838+
usbd_storage_select_medium(USBD_STORAGE_MEDIUM_SDCARD);
839+
}
842840
}
843841
}
844842
#endif
845843

844+
#ifdef USE_HOST_MODE
845+
// USB host
846+
pyb_usb_host_init();
847+
#elif defined(USE_DEVICE_MODE)
848+
// USB device
849+
pyb_usb_dev_init();
850+
#endif
851+
846852
// run main script
847853
{
848854
vstr_t *vstr = vstr_new();

stm/sdcard.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ void sdcard_power_off(void) {
9595
SD_DeInit();
9696
}
9797

98+
uint64_t sdcard_get_capacity_in_bytes(void) {
99+
SD_CardInfo SDCardInfo;
100+
SD_GetCardInfo(&SDCardInfo);
101+
return SDCardInfo.CardCapacity;
102+
}
103+
98104
bool sdcard_read_block(uint8_t *dest, uint32_t block_num) {
99105
// TODO return error if not powered on
100106

stm/sdcard.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ void sdcard_init(void);
55
bool sdcard_is_present(void);
66
bool sdcard_power_on(void);
77
void sdcard_power_off(void);
8+
uint64_t sdcard_get_capacity_in_bytes(void);
89
bool sdcard_read_block(uint8_t *dest, uint32_t block_num);
910
bool sdcard_write_block(const uint8_t *src, uint32_t block_num);
1011

0 commit comments

Comments
 (0)