Mounting USB using fatfs
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!