cancel
Showing results for 
Search instead for 
Did you mean: 

How do I make stm32 and w25q128 work as a usb msc?

carlogulliani
Associate III

I am trying to use stm32f407 and w25q128 to get a usb drive, for this I wrote a driver to control the flash drive and initialized the USB MSC using CubeMX. When I connect the stm32 to the PC (Ubuntu) I see the new storage device in the Disks app but it doesn't mount, I tried to erase it but the process freezes until I reboot the board

w251128 has:

PageSize = 256

SectorSize = 4096

SectorCount = 4096

PageCount = 65536

BlockSize = 65536

CapacityInKiloByte = 16Mb

Based on this information, I set the defines in `usbd_storage_if.c`:

```

#define STORAGE_LUN_NBR 1

#define STORAGE_BLK_NBR 0x1000

#define STORAGE_BLK_SIZ 0x1000

int8_t STORAGE_Read_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)

{

w25q_readSector(buf, blk_addr, 0, blk_len * 4096); // 0 is offset

return USBD_OK;

}

int8_t STORAGE_Write_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)

{

w25q_eraseBlock(blk_addr);

w25q_writeBlock(buf, blk_addr, 0, blk_len * 4096);

return USBD_OK;

}

```

I added some printfs inside these methods to notify the start and end of the process, if I erase blocks/sectors/pages from the main function in `main.c` everything goes fine, but when I try to erase the flash drive using PC OS and `usbd_storage_if.c` I see that the process just started but never finished, also adding the above code to the read function gives the following popup window "Error wiping device: Failed to probe the device 'dev/-sdb' (udisks-error-quark, 0)".

I also tried to comment out the w25q code and replace it with memcpy to work with internal memory, this works as expected, but how do I use external SPI flash memory as the MSC?

1 ACCEPTED SOLUTION

Accepted Solutions

Thanks for your reply! Yes, I found the problem. It seems OS doesn’t like delays inside the driver functions. My driver has HAL_Delay() for a few ticks while using spi transmit/receive. When I comment out all delays I got MSC working

View solution in original post

3 REPLIES 3

Test the IO routines thoroughly

Instrument interaction

See what OS is doing when failed.

Make sure sector size and capacity are correctly reported. ie INQURY and READ CAPACITY/MODE PAGE

Can't use FatFS and MSC concurrently, but can use f_mkfs() to format suitably first time around.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Thanks for your reply! Yes, I found the problem. It seems OS doesn’t like delays inside the driver functions. My driver has HAL_Delay() for a few ticks while using spi transmit/receive. When I comment out all delays I got MSC working

RB4020
Associate II

Hi @carlogulliani 
Could you please share the code for this? I'm working on same but w25q32 and STM32U0 series MCU. It would be very helpful if you could share the code so that I can use that as a reference.

Regards,
Bharath