2019-01-20 11:34 PM
Hi all,
I am having troubles mounting a USB using the "f_mount" function in the fatfs library.
My usb does mount but only after many attempts, using a simple counter I have found it to be 81, every single time I run the code it is the same amount of attempts.
Once mounted it works flawlessly and as I expect, however I am struggling to understand why it is taking so many attempts to mount.
I have a "dirty" function to test writing to a file, shown below:
void USB_WriteTest(void){
// Attempt to mount usb
if(f_mount(&USBHFatFS, USBHPath, 1)==FR_OK){
HAL_Delay(100);
HAL_UART_Transmit_IT(&huart3, (uint8_t*)"Mount Success...\r\n", strlen("Mount Success...\r\n"));
//Name of file being created
char myFileName [] = "config.txt";
// Open a file to write to
if(f_openAppend(&USBHFile, myFileName)==FR_OK){
HAL_Delay(100);
HAL_UART_Transmit_IT(&huart3, (uint8_t*)"Open Success...\r\n", strlen("Open Success...\r\n"));
// Message to save to usb
char logMsg[] = "Hello world!!\r\n"
"Sent from nucleo to USB\r\n";
// Length of message
int logLength = strlen(logMsg);
// Write data to the usb
if(f_write(&USBHFile, logMsg, logLength, &USBBytes)==FR_OK){
HAL_Delay(100);
HAL_UART_Transmit_IT(&huart3, (uint8_t*)"Write Success...\r\n", strlen("Write Success...\r\n"));
}
else{
HAL_Delay(100);
HAL_UART_Transmit_IT(&huart3, (uint8_t*)"Write Failed!\r\n", strlen("Write Failed!\r\n"));
}
//close the file after use
f_close(&USBHFile);
} // end f_open
else{
HAL_Delay(100);
HAL_UART_Transmit_IT(&huart3, (uint8_t*)"Open Failed!\r\n", strlen("Open Failed!\r\n"));
}
HAL_Delay(100);
f_mount(0,"", 0);
} // end f_mount
else{
HAL_Delay(100);
HAL_UART_Transmit_IT(&huart3, (uint8_t*)"Mount Failed!\r\n", strlen("Mount Failed!\r\n"));
}
I have called it in my main loop in an if statement so it will only run when a USB is connected, example below:
// Check if a USB is connected
if(hUsbHostFS.device.is_connected==1){
// USB write test function. Writes a string to a txt file
USB_WriteTest();
}
Any help would be greatly appreciated!
2019-01-21 05:38 AM
Consider doing this as a subroutine, lot of repetitive code
HAL_Delay(100);
HAL_UART_Transmit_IT(&huart3, (uint8_t*)"Mount Success...\r\n", strlen("Mount Success...\r\n"));
The top level interactions aren't going to be very instructive. Instrument the DISKIO layer, and look at what is happening there, especially the errors and the data returned from MBR and BPB.
2019-01-21 07:16 AM
I've noticed a similar issue too, with STM32F4 (custom board).
After poll for if(hUsbHostFS.device.is_connected==1) succeeds, there are few "glitches" and is_connected changes state, until it stabilizes. On my device the number of these "glitches" is much less, about 2-3,
So I just retry several times until mount and file operations succeed.
If someone could explain this, would be great.
-- pa
2019-04-10 12:54 AM
@Pavel A.
Sorry to bring this up again but did you further your understanding as to why this is happening?
I still haven't :(
2019-04-13 02:13 PM
> Sorry to bring this up again but did you further your understanding as to why this is happening?
No I have not returned to this project since then. Other threads advice to update the fatfs library from upstream.
-- pa