cancel
Showing results for 
Search instead for 
Did you mean: 

How to write to a .txt file using USB Host MSC, FATFS and FreeRTOS

ibo24
Associate II

I am using the NUCLEO-L476RG and I want to write to a .txt file on the USB Stick using FreeRTOS. For that, I have enabled FreeRTOS, USB_OTG_FS, Mass Storage Host Class and FATFS (USB Disk). When I generate the project, I see MX_USB_HOST_Init(); in my DefaultTask that the Cube generates.

I have written a USB_Write function (see below) that works perfectly when I don't use FreeRTOS, so I already tested it. But when I add FreeRTOS to my project, it doesn't work.

What I realized, is that, if I use it without FreeRTOS, there is a MX_USB_HOST_Process(); function in the while loop that then calls USBH_Process(&hUsbHostFS);. But when I add FreeRTOS to the mix, this function isn't there anymore, only the Init function in the defaultTask. So I tried to call USBH_Process(&hUsbHostFS); in the Task loop myself but I honestly don't know if that is the way to go.

Without FreeRTOS, my while loop looks like this:

//USB variable

extern ApplicationTypeDef Appli_state;

USBH_HandleTypeDef hUsbHostFS;

//USB logical PATH

char USBHPath[4];

//File IO variables

FIL myFile;

FRESULT res;

UINT byteswritten, bytesread;

char USBbuffer[100];

//FATFS variable

FATFS myUSBfatFs;

while (1) {

/* USER CODE END WHILE */

MX_USB_HOST_Process(); //generated by CubeMX automatically

/* USER CODE BEGIN 3 */

switch (Appli_state) {

case APPLICATION_IDLE:

break;

case APPLICATION_START:

if (f_mount(&myUSBfatFs, (TCHAR const*) USBHPath, 0) == FR_OK) { //mount USB

//HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, 1); //LED on if USB is plugged

}

break;

case APPLICATION_READY:

USB_Write("Test\r\n");

break;

case APPLICATION_DISCONNECT:

break;

}

}

This is my USB_Write function:

bool USB_Write(char text[100]) {

/*Copy parameter to USB buffer*/

sprintf(USBbuffer, text); //'text'cannot be larger than 100 characters, otherwise buffer overflow for the sprintf() function

/*Open and Append file for writing to .txt*/

if (f_open(&myFile, "CubeIDE.txt", FA_OPEN_APPEND | FA_WRITE) != FR_OK) {

return 0;

}

//Write to text file

res = f_write(&myFile, (const void*) USBbuffer, strlen(USBbuffer),

&byteswritten);

//HAL_UART_Transmit(&huart2, (uint8_t*) "Text appended to .txt file!\r\n", 29,HAL_MAX_DELAY);

if ((res != FR_OK) || (byteswritten == 0)) {

return 0;

}

f_close(&myFile);

return 1; //Success

}

This works perefectly without FreeRTOS, so I wanted to achieve the same result using FreeRTOS, but no luck :( I have struggled with this for days now, trying to debug it for hours every day, but no success. I would greatly appreciate any help.

9 REPLIES 9
Ons KOOLI
Senior III

Hi ibo24,

There is an application provided by ST for STM32H7 product which uses USB Host Mass storage with FreeRTOS. I think it will be helpful.

You can find it in the STM32CubeH7 under : Projects\STM32H743I-EVAL\Applications\USB_Host\MSC_RTOS

Best Regards,

Ons.

ibo24
Associate II

Thank you very much Ons Kooli for that advice! Have looked into that project and how they do it there, but somehow, mine still does not work...

Hi ibo24,

Can you share with me your .ioc file ?

Best Regards,

Ons.

ibo24
Associate II

Of course! Thank you so much for taking your time, dear Ons Kooli!

I'll share the whole project with you.

In the CubeMX project,...

  1. If I disable RTOS, it works perfectly. I create a .txt file on my USB disk. There is a MX_USB_HOST_Process(); function (generated by CubeMX) in the while loop that then calls USBH_Process(&hUsbHostFS); and everything works perfectly.
  2. If I enable RTOS CMSISv2, and change absolutely nothing, it doesn't work anymore. There is no MX_USB_HOST_Process(); in the defaultTask loop anymore, only the MX_USB_HOST_Init(); function in the defaultTask.

I have been trying to solve this issue for weeks now. Unsuccessfully. Thank you again very much!

Best regards,

Ibo

Ons KOOLI
Senior III

Hi ibo24,

When you enable FreeRTOS and you don't change anything, you said it doesn't work. What do you mean ? Are there errors in the project ? Or it compiles but it doesn't run ? The project you sent contains too much of warnings.

Best Regards,

Ons.

Ons KOOLI
Senior III

Hi ibo24,

Can you please provide the project without FREERTOS enabled ? In fact, I mean the project that works.

Best Regards,

Ons.

Ons KOOLI
Senior III

Hi ibo24,

Any updates on this question ?

Best Regards,

Ons.

I have the same situations as the author of this topic! But I can provide as a project without FreeRTOS which one working well and project with FreeRTOS which one doesn't work, and it has no warnings!

Could you help me with "USB Host MSC" and Freertos?

ADevg.1
Associate II

How to write to txt file using STM32 USB MSC FATFS, using internal flash , no use any external device like SD card , pendrive or external flash.