cancel
Showing results for 
Search instead for 
Did you mean: 

FATFS Unlink driver

Nico Pat_2
Associate
Posted on March 12, 2018 at 16:54

Hello,

I have problem when I use FATFS with my STM32F2.

My code come from example STM32-Tools\STM32_FW_libraries\HAL_CUBEMX_libs\STM32Cube_FW_F2_V1.6.0\Projects\STM32F207ZG-Nucleo\Applications\FatFs\FatFs_USBDisk\

It's works very well but only one time (with this example and in my code)

Normally

these functions should allow a reset of the FAT ?

  • FATFS_UnLinkDriver(USBDISKPath);
  • f_mount(NULL, (TCHAR const*)'', 0);

When I plug again my USB key (after the first good test) , I have problem with function

res = f_write(&MyFile, wtext, sizeof(wtext), (void *)&byteswritten);

res return FR_NO_FILESYSTEM

it seems that the unlink function doesn't work correctly

if(FATFS_LinkDriver(&USBH_Driver, USBDISKPath) == 0)

{

USBH_Init(&hUSB_Host, USBH_UserProcess, 0);

USBH_RegisterClass(&hUSB_Host, USBH_MSC_CLASS);

USBH_Start(&hUSB_Host);

while (1)

{

USBH_Process(&hUSB_Host);

switch(Appli_state)

{

case APPLICATION_START:

MSC_Application();

Appli_state = APPLICATION_IDLE;

break;

case APPLICATION_IDLE:

default:

break;

}

}

}

static void MSC_Application(void)

{

FRESULT res; /* FatFs function common result code */

uint32_t byteswritten, bytesread; /* File write/read counts */

uint8_t wtext[] = 'This is STM32 working with FatFs'; /* File write buffer */

uint8_t rtext[100]; /* File read buffer */

/* Register the file system object to the FatFs module */

if(f_mount(&USBDISKFatFs, (TCHAR const*)USBDISKPath, 0) != FR_OK)

{

/* FatFs Initialization Error */

Error_Handler();

}

else

{

/* Create and Open a new text file object with write access */

if(f_open(&MyFile, 'STMTXT', FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)

{

/* 'STMTXT' file Open for write Error */

Error_Handler();

}

else

{

/* Write data to the text file */

res = f_write(&MyFile, wtext, sizeof(wtext), (void *)&byteswritten);

....

.....

.....

/* Unlink the USB disk I/O driver */

FATFS_UnLinkDriver(USBDISKPath);

}

static void USBH_UserProcess(USBH_HandleTypeDef *phost, uint8_t id)

{

switch(id)

{

case HOST_USER_SELECT_CONFIGURATION:

break;

case HOST_USER_DISCONNECTION:

Appli_state = APPLICATION_IDLE;

BSP_LED_Off(LED1);

BSP_LED_Off(LED3);

f_mount(NULL, (TCHAR const*)'', 0);

break;

case HOST_USER_CLASS_ACTIVE:

Appli_state = APPLICATION_START;

break;

default:

break;

}

}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

FATFS USBDISKFatFs; /* File system object for USB disk logical drive */

FIL MyFile; /* File object */

char USBDISKPath[4]; /* USB Host logical drive path */

USBH_HandleTypeDef hUSB_Host; /* USB Host handle */

typedef enum {

APPLICATION_IDLE = 0,

APPLICATION_START,

APPLICATION_RUNNING,

}MSC_ApplicationTypeDef;

MSC_ApplicationTypeDef Appli_state = APPLICATION_IDLE;�?�?�?�?�?�?�?�?�?�?�?�?

I would like to avoid a reset if possible,

Thanks,

Nico

1 REPLY 1
Posted on March 12, 2018 at 18:36

Ok, but FR_NO_FILESYSTEM is indicative that the media isn't returning usable data or an error. You're going to have to dig deeper into the abstraction to understand what actually isn't working. I'd suggest instrumenting the diskio.c layer.

You might for example need to zero out structures, FATFS (USBDISKFatFs) being one I'd be most concerned about if trying to establish a connection to a new media device.

Try looking at the most current F4 release, and back porting to the F2, the hardware is nearly identical, if you rebuild with the correct CPU target it has a high probability of running out of the box. Likely to save you a lot of time fighting issues that have already been solved/addressed.

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