cancel
Showing results for 
Search instead for 
Did you mean: 

FATFs Using USB Mass Storage Key

JC Lebreton
Associate II
Posted on February 28, 2018 at 12:19

Hi,

I've been playing around with FatFs for about 1 mounth now. 

I succefully used SDMMC and SPI protocol to save and read data from an SD card.

I order to improve the system (it's a improvement for the project) 

We decided to add an UDB Mass Storage Key. (approx 8Giga)

I found this : 

https://community.st.com/message/92492

 

And it describe exaclty my error.

I'm able to mount the USB stick but I couldn't f_open() it. 

most of my code is a copy paste of the example provided by ST.

I copy what i made at the end of the message. 

if some of you have or had the same issue could you provide me some help. or in other case explain me more precisely what 

Fehrs.Kai

 describes in the post i link at the begining.

thanks in advance

edit : forget to say that i m working with an STM32H743zi on a nucleo board

------

if(FATFS_LinkDriver(&USBH_Driver, '') ==0)

{

MX_USB_HOST_Init();

while(1)

{

/* USB Host Background task */

MX_USB_HOST_Process();

/* Mass Storage Application State Machine */

switch(Appli_state)

{

case APPLICATION_START:

FS_FileOperations(); <-- I got issues here

Appli_state = APPLICATION_IDLE;

break;

case APPLICATION_IDLE:

default:

break;

}

}

}

else

{

HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET); //put it on if linkdriver fail

}

static void FS_FileOperations(void)

{

FRESULT res; /* FatFs function common result code */

uint32_t byteswritten;

uint8_t wtext[] = 'This is STM32 working with FatFs + USB/FS'; /* File write buffer */

/* Register the file system object to the FatFs module */

if(f_mount(&USBHFatFS, '', 0) == FR_OK)

{

/* Create and Open a new text file object with write access */

res = f_open(&USBHFile, 'STM32.TXT', FA_CREATE_NEW); <- Biggest issue is here

datasend(res);

if(res == FR_OK)

{

/* Write data to the text file */

res = f_write(&USBHFile, wtext, sizeof(wtext), (void *)&byteswritten);

if((byteswritten > 0) && (res == FR_OK))

{

/* Close the open text file */

f_close(&USBHFile);

//HAL_GPIO_WritePin(GPIOB,GPIO_PIN_7,GPIO_PIN_SET); //Led de debug pour confimer la bonne fermeture du fichier

return;

}

}

}

/* Error */

// HAL_GPIO_WritePin(GPIOB,GPIO_PIN_14,GPIO_PIN_SET); //Led de debug pour confimer la bonne fermeture du fichier

}

#stm32h7 #h7 #usb #fatfs
7 REPLIES 7
Pavel A.
Evangelist III
Posted on February 28, 2018 at 15:05

case APPLICATION_START:

FS_FileOperations(); <--

I got issues here

If I remember correctly, state '

APPLICATION_START' is too early to use the USB device, this state only indicates that the class driver starts sending its sequence of requests. The device is ready to use in state APPLICATION_READY.

-- pa

JC Lebreton
Associate II
Posted on February 28, 2018 at 16:30

So far when i dig into USBH_Process()

(i give code at the end of the post)

At the beginning, i'm in HOST_IDLE and the variable 'phost->device.is_connected' is set to '1' (so their is a device)

Then it stays in a mode called  HOST_DEV_WAIT_FOR_ATTACHMENT. And it seems like i cannot go futher ... 

HOST_DEV_WAIT_FOR_ATTACHMENT is changed at the call of USBH_LL_Connect()

USBH_StatusTypeDef USBH_Process(USBH_HandleTypeDef *phost)

{

__IO USBH_StatusTypeDef status = USBH_FAIL;

uint8_t idx = 0;

switch (phost->gState)

{

case HOST_IDLE :

if (phost->device.is_connected)

{

/* Wait for 200 ms after connection */

phost->gState = HOST_DEV_WAIT_FOR_ATTACHMENT;

USBH_Delay(200);

USBH_LL_ResetPort(phost);

#if (USBH_USE_OS == 1)

osMessagePut ( phost->os_event, USBH_PORT_EVENT, 0);

#endif

}

break;

case HOST_DEV_WAIT_FOR_ATTACHMENT:

break;

case HOST_DEV_ATTACHED :

...... more if needed ...........

Posted on February 28, 2018 at 16:21

i just change APPLICATION_START  into APPLICATION_READY  

But it seems that i'm taking a step back...

I don't acces to my FS_FileOperation function 

My variable 'Appli_state' stay at APPLICATION_START 

 and is not triggered to APPLICATION_READY

I am missing something?

Posted on March 01, 2018 at 00:53

Do you enable 5V voltage on the USB port? This is in usbh_platform.c (usually toggling some GPIO pin ...)

-- pa

Posted on March 05, 2018 at 09:35

yes i do

i have the ld8 on when i run my code
El Taura
Associate II

Will FatFs and USB code access SD card at the same time? Will you use a sort of mutex to prevent this to happen?

The OP is hosting a USB Flash drive, not using SD Card

To your question concurrent operation as a USB DEVICE accessing an SD CARD, and FATFS locally is very difficult to achieve without data loss. Consider being a USB MTP DEVICE rather than a USB MSC DEVICE.

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