STM32F4Discovery USB Mass Storage via OTG

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Beginner
Posts: 6
Joined: Fri Apr 05, 2019 11:07 pm

STM32F4Discovery USB Mass Storage via OTG

Post by Beginner »

Hello,

I have been trying to learn it by myself but it seems too complicated to me. I would like to store data in a .txt file, for example, on a USB key connected via OTG on a STM32F4Discovery using FatFs. Is it possible to implement it using ChibiOS ?

Regards,
Beginner
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: STM32F4Discovery USB Mass Storage via OTG

Post by Giovanni »

That would require the USB host driver in the community repository, others have already done it but I would rate it not easy.

Using an SD card would be much easier, the FatFS demos already have all that is required.

Giovanni
Beginner
Posts: 6
Joined: Fri Apr 05, 2019 11:07 pm

Re: STM32F4Discovery USB Mass Storage via OTG

Post by Beginner »

Thank you Giovanni for the reply.

Is this the required module to do it with SD card ?

I wanted to do it on a USB key because I already have the hardware (USB key + OTG cable) and this kind of SD card reader seems to be limited to 2Go.

If anyone is able to provide me a link to someone's code who is able to easily write files either on USB or SD... do not hesitate to share ! (After several hours of searching, I was not able to find anything suitable for my application) :)

Regards,
Beginner
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: STM32F4Discovery USB Mass Storage via OTG

Post by Giovanni »

Hi,

Look at the STM32/RT-STM32-LWIP-FATFS-USB demo, it already integrates FatFS, you need to work on board files and remove lwIP and related code.

Giovanni
Beginner
Posts: 6
Joined: Fri Apr 05, 2019 11:07 pm

Re: STM32F4Discovery USB Mass Storage via OTG

Post by Beginner »

Hello again,

I bought this SD card module. I would like to wire it to use it as in this example to use it with FatFS on my STM32F4Discovery.

Where should I look for which pins to use ? And how to define mmccfg (ls_spicfg and hs_spicfg) ?

Would :
    nss - pa4
    mosi- pa7
    miso - pa6
    sck - pa5
    gnd - gnd
    vcc - 3v
be correct to use with spi1 (or are there multiple possibilities, if yes, how do you know which to use)? Do I need to use the vcc/gnd pins twice as there are two of each on my SD card module ?

Thanks in advance :D
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: STM32F4Discovery USB Mass Storage via OTG

Post by Giovanni »

Hi,

You can use any SPI, just make sure that:

1) SPI pins are not already used on the board itself or you would have conflicts.
2) Verify that the SPI you choose does not use DMAs channels already used by other drivers.

Giovanni
Beginner
Posts: 6
Joined: Fri Apr 05, 2019 11:07 pm

Re: STM32F4Discovery USB Mass Storage via OTG

Post by Beginner »

Hi,

I activated MMC_SPI, SPI and SPI1, then I configured FatFS but now, nothing happens, I expected to create a simple test.txt at the root of my SD card. I tried the following :

Code: Select all

FATFS fs;
FIL fil;

static SPIConfig hs_spicfg = {false, NULL, GPIOB, 5, 0, 0};
static SPIConfig ls_spicfg = {false, NULL, GPIOB, 5, SPI_CR1_BR_2 | SPI_CR1_BR_1, 0};

static MMCConfig mmccfg = {&SPID1, &ls_spicfg, &hs_spicfg};
MMCDriver MMCD1;


and

Code: Select all

int main(void) {
  halInit();
  chSysInit();

  palSetPadMode(GPIOA, 5, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_HIGHEST);    /* New SCK */
  palSetPadMode(GPIOA, 6, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_HIGHEST);    /* New MISO*/
  palSetPadMode(GPIOA, 7, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_HIGHEST);    /* New MOSI*/
  palSetPadMode(GPIOB, 5, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_HIGHEST);
  palClearPad(GPIOB, 5);

  mmcObjectInit(&MMCD1);
  mmcStart(&MMCD1, &mmccfg);

  f_mount(&fs, "", 0);
  f_open(&fil, "test.txt", FA_WRITE);
  f_close(&fil);

  mmcStop(&MMCD1);
  ...


    CS - PB5
    MOSI - PA7
    SCK - PA5
    MISO - PA6

Where am I wrong ? :|
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: STM32F4Discovery USB Mass Storage via OTG

Post by Giovanni »

Please look at demos, you are missing entirely the "connect" part: mmcConnect().

Also, enable assertions in chconf.h, those can catch this kind of errors.

Giovanni
Beginner
Posts: 6
Joined: Fri Apr 05, 2019 11:07 pm

Re: STM32F4Discovery USB Mass Storage via OTG

Post by Beginner »

Hi Giovanni !

Thanks for the hints, I adapted the code from /demos/STM32/RT-STM32F103-OLIMEX_STM32_P103-FATFS and it works.

Are InsertHandler/RemoveHandler supposed to periodically check the presence of SD card in the reader and connect/disconnect ? It seems that I need the remove and put the SD card before running it on the STM32F4 otherwise it does not work. And if I remove the SD card while running nothing happens (i.e. fs_ready stays TRUE). Where should I look for to make it connect/disconnect properly ?

Best regards
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: STM32F4Discovery USB Mass Storage via OTG

Post by Giovanni »

Hi,

Hi, you need to provide card detection code for your board, there is a function to be filled in board.c. Does your connector have a switch for this? you need to connect it to a GPIO.

Giovanni
Post Reply