cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 USB OTG MSC

b2399
Associate II
Posted on April 19, 2016 at 18:00

Hi,

I'm using an STm32F107 and I trie to configure the USB interface as host to access to an key.

This is the initialization code :

MX_USB_HOST_Init();
MX_FATFS_Init();

with :

void MX_USB_HOST_Init(void)
{
/* Init Host Library,Add Supported Class and Start the library*/
USBH_Init(&hUsbHostFS, USBH_UserProcess, HOST_FS);
USBH_RegisterClass(&hUsbHostFS, USBH_MSC_CLASS);
USBH_Start(&hUsbHostFS);
}

and :

void MX_FATFS_Init(void) 
{
/*## FatFS: Link the USBH driver ###########################*/
retUSBH = FATFS_LinkDriver(&USBH_Driver, USBH_Path);
/* USER CODE BEGIN Init */
/* additional user code for init */
if (retUSBH == 0)
{
HAL_GPIO_WritePin(GPIOD, LED1_Pin, GPIO_PIN_SET);//Succes
}
else
{
HAL_GPIO_WritePin(GPIOD, LED3_Pin, GPIO_PIN_SET);//Error
}
/* USER CODE END Init */
}

After execute, the led1 is on. A try to access to

/* Register the file system object to the FatFs module */
if (f_mount(&USBH_fatfs, '''', 0) != FR_OK)
{
/* ERROR : Cannot Initialize FatFs. */
...
}

This call to function ''

f_mount()''

always returns FR_OK but after :

while (1)
{
/* USER CODE END WHILE */
MX_USB_HOST_Process();
/* USER CODE BEGIN 3 */
HAL_Delay(500);
res = f_open(&MyFile, ''0:USBHost.txt'', FA_CREATE_ALWAYS | FA_WRITE);
if (res != FR_OK)
{
// Error
// ...
}
...
}

Here, the function ''

f_open()''

never returns

FR_OK.

Is there someone who can give me some explanation of this behaviour ? Thank you very much. Bernard. #stm32-usb-otg-msc
4 REPLIES 4
Posted on April 19, 2016 at 18:59

What result code does it return? That might provide additional insight into where things are failing.

If it is with the diskio layer, you might want to instrument that to see where it fails.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
b2399
Associate II
Posted on April 21, 2016 at 19:01

Hi Clive, thank you for your response.

The 

function ''

f_open()'' always 

returns 

FR_DISK_ERR.

I've observed that this happens because the USB host handle takes the following states :

gstate : HOST_IDLE, HOST_DEV_ATTACHED and then HOST_ENUMERATION

EnumState : ENUM_IDLE

RequestState : CMD_IDLE, CMD_SEND and then CMD_WAIT

with

/* USB Host handle structure */

typedef struct _USBH_HandleTypeDef

{

  __IO HOST_StateTypeDef     gState;       /*  Host State Machine Value */

  ENUM_StateTypeDef     EnumState;    /* Enumeration state Machine */

  CMD_StateTypeDef      RequestState;       

  USBH_CtrlTypeDef      Control;

  USBH_DeviceTypeDef    device;

  USBH_ClassTypeDef*    pClass[USBH_MAX_NUM_SUPPORTED_CLASS];

  USBH_ClassTypeDef*    pActiveClass;

  uint32_t              ClassNumber;

  uint32_t              Pipes[15];

  __IO uint32_t         Timer;

  uint8_t               id;  

  void*                 pData;                  

  void                 (* pUser )(struct _USBH_HandleTypeDef *pHandle, uint8_t id);

  

#if (USBH_USE_OS == 1)

  osMessageQId          os_event;   

  osThreadId            thread; 

#endif  

  

} USBH_HandleTypeDef;

I guess there is something missing in the initialization phase, but I cannot find what !

Do you have a track ?

Thank you for yor help.

Bernard.

Lori B.
Associate III
Posted on July 29, 2016 at 10:38

Looks like you are trying to use MSC + FatFs locally at the same time.

I found this thread while trying (and failing) to do the same.

After some researches I found this:

http://stackoverflow.com/questions/19974503/fatfs-on-st32-with-device-configured-as-usb-drive

It seems it is impossible to achive that..
Posted on July 29, 2016 at 16:39

Looks like you are trying to use MSC + FatFs locally at the same time.

The OP is *HOSTING*, ie user plugs Flash Drive into STM32, which then accesses it.

It seems it is impossible to achieve that..

The coherency issues are intractable for a PC and STM32 to be able to access the same media together.

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