Page 1 of 2

STM32F4Discovery USB Mass Storage via OTG

Posted: Fri Apr 05, 2019 11:37 pm
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

Re: STM32F4Discovery USB Mass Storage via OTG

Posted: Sat Apr 06, 2019 6:09 am
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

Re: STM32F4Discovery USB Mass Storage via OTG

Posted: Sat Apr 06, 2019 2:36 pm
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

Re: STM32F4Discovery USB Mass Storage via OTG

Posted: Sat Apr 06, 2019 5:11 pm
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

Re: STM32F4Discovery USB Mass Storage via OTG

Posted: Mon Apr 22, 2019 12:32 am
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

Re: STM32F4Discovery USB Mass Storage via OTG

Posted: Mon Apr 22, 2019 5:03 am
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

Re: STM32F4Discovery USB Mass Storage via OTG

Posted: Mon Apr 22, 2019 5:58 pm
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 ? :|

Re: STM32F4Discovery USB Mass Storage via OTG

Posted: Mon Apr 22, 2019 6:03 pm
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

Re: STM32F4Discovery USB Mass Storage via OTG

Posted: Sat Apr 27, 2019 11:35 am
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

Re: STM32F4Discovery USB Mass Storage via OTG

Posted: Sat Apr 27, 2019 3:20 pm
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