2020-12-28 06:04 PM
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.
2020-12-29 02:25 AM
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.
2020-12-29 09:47 AM
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...
2020-12-30 06:43 AM
Hi ibo24,
Can you share with me your .ioc file ?
Best Regards,
Ons.
2020-12-30 07:17 AM
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,...
I have been trying to solve this issue for weeks now. Unsuccessfully. Thank you again very much!
Best regards,
Ibo
2021-03-16 02:05 AM
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.
2021-04-01 09:13 AM
Hi ibo24,
Can you please provide the project without FREERTOS enabled ? In fact, I mean the project that works.
Best Regards,
Ons.
2021-04-05 02:41 AM
Hi ibo24,
Any updates on this question ?
Best Regards,
Ons.
2023-01-23 06:46 AM
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?
2023-03-31 04:34 AM
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.