FarFs f_open freeze

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

sam76
Posts: 15
Joined: Fri May 03, 2013 8:07 am

FarFs f_open freeze

Post by sam76 »

Hi,

I have another little problem. I work on a file logger and I can't open a file, I just completed the InsertHandler for ttsting purpose :

Code: Select all

static void InsertHandler(eventid_t id) {
  FRESULT err;

  (void)id;
  /*
   * On insertion SDC initialization and FS mount.
   */
  if (sdcConnect(&SDCD1))
    return;

  err = f_mount(0, &SDC_FS);
  if (err != FR_OK) {
    sdcDisconnect(&SDCD1);
    return;
  }

  FIL fp;

  if (  f_open(&fp, "0:/test.log", FA_READ | FA_WRITE | FA_OPEN_ALWAYS) == FR_OK )
  {
    uint8_t nb = 0;
    struct tm timp;
    rtcGetTimeTm(&RTCD1, &timp);

    f_printf(&fp, "[TEST][%s]", asctime(&timp));
    f_write(&fp, "\n", 1, &nb);
    f_close(&fp);
  }
  f_mount(0,0);
  fs_ready = TRUE;
}


The f_open function freeze.

Code: Select all

chk_mounted (path=0x200004c0, rfs=0x200004d8, chk_wp=18 '\022') at ../ChibiOS_2.5.1/ext/fatfs/src/ff.c:2019
2019      const TCHAR *p = *path;
(gdb) n
2023      vol = p[0] - '0';               /* Is there a drive number? */
(gdb)
2024      if (vol <= 9 && p[1] == ':') {      /* Found a drive number, get and strip it */
(gdb)
2025         p += 2; *path = p;            /* Return pointer to the path name */
(gdb)
2035      if (vol >= _VOLUMES)             /* Is the drive number valid? */
(gdb)
2037      *rfs = fs = FatFs[vol];            /* Return pointer to the corresponding file system object */
(gdb)
2038      if (!fs) return FR_NOT_ENABLED;      /* Is the file system object available? */
(gdb) p fs
$1 = (FATFS *) 0x2000bea8
(gdb) p *fs
$2 = {fs_type = 0 '\000', drv = 0 '\000', csize = 0 '\000', n_fats = 0 '\000', wflag = 0 '\000', fsi_flag = 0 '\000', id = 0, n_rootdir = 0, sobj = 0x200073cc,
  last_clust = 0, free_clust = 0, fsi_sector = 0, n_fatent = 0, fsize = 0, fatbase = 0, dirbase = 0, database = 0, winsect = 0, win = '\000' <repeats 511 times>}
(gdb) n
2040      ENTER_FF(fs);                  /* Lock file system */
(gdb)
2042      if (fs->fs_type) {               /* If the logical drive has been mounted */
(gdb)
2054      fs->fs_type = 0;               /* Clear the file system object */
(gdb) n
2055      fs->drv = LD2PD(vol);            /* Bind the logical drive and a physical drive */
(gdb)
2056      stat = disk_initialize(fs->drv);   /* Initialize low level disk I/O layer */


The fs_type attribute should not be 0, may be the mount function fail ?

Without my writing test, the f_getfree function works. Do I miss something ?

I use a sandisk sdhc 4go.

Board : Olimex stm32h407
ChibiOS 2.5.1
sam76
Posts: 15
Joined: Fri May 03, 2013 8:07 am

Re: FarFs f_open freeze

Post by sam76 »

This is the origin :

Code: Select all

(gdb) b sdc_lld_send_cmd_short_crc
Breakpoint 1 at 0x8008b20: file ../ChibiOS_2.5.1/os/hal/platforms/STM32/sdc_lld.c, line 542.
(gdb) target remote :3333
Remote debugging using :3333
ResetHandler () at ../ChibiOS_2.5.1/os/ports/GCC/ARMCMx/crt0.c:253
253     asm volatile ("cpsid   i");
(gdb) c
Continuing.
Note: automatically using hardware breakpoints for read-only addresses.
WARNING! The target is already running. All changes GDB did to registers will be discarded! Waiting for target to halt.

Breakpoint 1, sdc_lld_send_cmd_short_crc (sdcp=0x2000858c, cmd=8 '\b', arg=426, resp=0x20000504) at ../ChibiOS_2.5.1/os/hal/platforms/STM32/sdc_lld.c:542
542     SDIO->CMD = (uint32_t)cmd | SDIO_CMD_WAITRESP_0 | SDIO_CMD_CPSMEN;
(gdb) c
Continuing.

Breakpoint 1, sdc_lld_send_cmd_short_crc (sdcp=0x2000858c, cmd=55 '7', arg=0, resp=0x20000504) at ../ChibiOS_2.5.1/os/hal/platforms/STM32/sdc_lld.c:542
542     SDIO->CMD = (uint32_t)cmd | SDIO_CMD_WAITRESP_0 | SDIO_CMD_CPSMEN;
(gdb)
Continuing.

Breakpoint 1, sdc_lld_send_cmd_short_crc (sdcp=0x2000858c, cmd=55 '7', arg=0, resp=0x20000504) at ../ChibiOS_2.5.1/os/hal/platforms/STM32/sdc_lld.c:542
542     SDIO->CMD = (uint32_t)cmd | SDIO_CMD_WAITRESP_0 | SDIO_CMD_CPSMEN;
(gdb)
Continuing.

Breakpoint 1, sdc_lld_send_cmd_short_crc (sdcp=0x2000858c, cmd=55 '7', arg=0, resp=0x20000504) at ../ChibiOS_2.5.1/os/hal/platforms/STM32/sdc_lld.c:542
542     SDIO->CMD = (uint32_t)cmd | SDIO_CMD_WAITRESP_0 | SDIO_CMD_CPSMEN;
(gdb)
Continuing.

Breakpoint 1, sdc_lld_send_cmd_short_crc (sdcp=0x2000858c, cmd=3 '\003', arg=0, resp=0x200085c4) at ../ChibiOS_2.5.1/os/hal/platforms/STM32/sdc_lld.c:542
542     SDIO->CMD = (uint32_t)cmd | SDIO_CMD_WAITRESP_0 | SDIO_CMD_CPSMEN;
(gdb)
Continuing.

Breakpoint 1, sdc_lld_send_cmd_short_crc (sdcp=0x2000858c, cmd=7 '\a', arg=3009938720, resp=0x20000504) at ../ChibiOS_2.5.1/os/hal/platforms/STM32/sdc_lld.c:542
542     SDIO->CMD = (uint32_t)cmd | SDIO_CMD_WAITRESP_0 | SDIO_CMD_CPSMEN;
(gdb)
Continuing.

Breakpoint 1, sdc_lld_send_cmd_short_crc (sdcp=0x2000858c, cmd=16 '\020', arg=512, resp=0x20000504) at ../ChibiOS_2.5.1/os/hal/platforms/STM32/sdc_lld.c:542
542     SDIO->CMD = (uint32_t)cmd | SDIO_CMD_WAITRESP_0 | SDIO_CMD_CPSMEN;
(gdb)
Continuing.

Breakpoint 1, sdc_lld_send_cmd_short_crc (sdcp=0x2000858c, cmd=55 '7', arg=3009938720, resp=0x20000504) at ../ChibiOS_2.5.1/os/hal/platforms/STM32/sdc_lld.c:542
542     SDIO->CMD = (uint32_t)cmd | SDIO_CMD_WAITRESP_0 | SDIO_CMD_CPSMEN;
(gdb)
Continuing.

Breakpoint 1, sdc_lld_send_cmd_short_crc (sdcp=0x2000858c, cmd=6 '\006', arg=2, resp=0x20000504) at ../ChibiOS_2.5.1/os/hal/platforms/STM32/sdc_lld.c:542
542     SDIO->CMD = (uint32_t)cmd | SDIO_CMD_WAITRESP_0 | SDIO_CMD_CPSMEN;
(gdb)
Continuing.

Breakpoint 1, sdc_lld_send_cmd_short_crc (sdcp=0x2000858c, cmd=13 '\r', arg=3009938720, resp=0x200003d4) at ../ChibiOS_2.5.1/os/hal/platforms/STM32/sdc_lld.c:542
542     SDIO->CMD = (uint32_t)cmd | SDIO_CMD_WAITRESP_0 | SDIO_CMD_CPSMEN;
(gdb)
Continuing.

Breakpoint 1, sdc_lld_send_cmd_short_crc (sdcp=0x2000858c, cmd=17 '\021', arg=0, resp=0x200003f4) at ../ChibiOS_2.5.1/os/hal/platforms/STM32/sdc_lld.c:542
542     SDIO->CMD = (uint32_t)cmd | SDIO_CMD_WAITRESP_0 | SDIO_CMD_CPSMEN;
(gdb) n
543     while (((sta = SDIO->STA) & (SDIO_STA_CMDREND | SDIO_STA_CTIMEOUT | SDIO_STA_CCRCFAIL)) == 0) ;
(gdb)
544     SDIO->ICR = SDIO_ICR_CMDRENDC | SDIO_ICR_CTIMEOUTC | SDIO_ICR_CCRCFAILC;
(gdb) p sta
$1 = 64
(gdb) n #the system freeze here


from stm32f2xx.h :

Code: Select all

#define  SDIO_STA_CMDREND                    ((uint32_t)0x00000040)        /*!<Command response received (CRC check passed) 

So the state is ok but next command hang.
Anyone understand ? The problem is fatfs or an unstability from outthere ?
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: FarFs f_open freeze

Post by Giovanni »

Hi,

I think it is not related to FatFS, it hangs in the SDC driver which should work correctly. It could be an electrical problem or wrong I/O pins setup for your board.

Giovanni
sam76
Posts: 15
Joined: Fri May 03, 2013 8:07 am

Re: FarFs f_open freeze

Post by sam76 »

I can accept that, but it is not a custom board, I think Olimex test their board ?

And why can I list file on SD ?
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: FarFs f_open freeze

Post by Tectu »

Does the SD card work when you use it on a card reader attached to a computer?
There are plenty of demos showing how to list files (create a file tree). I'd suggest you to take the ARMCM4x_STM32F407_LWIP_FATFS_USB demo and strip away the LWIP stuff (make sure you disable the MAC subsystem in the halconf.h, you cannot pass the halInit() successfully otherwise).

Also try another SD card if you have any around.


~ Tectu
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: FarFs f_open freeze

Post by Tectu »

One more thing:

Code: Select all

f_open(&fp, "0:/test.log", FA_READ | FA_WRITE | FA_OPEN_ALWAYS) == FR_OK

Can you please remove the / from there and try again? Also, for testing, leave the partition away and simply use "test.log" as an argument. I had some issues with partitioning myself, but at the end it worked out when I didn't use the slash and simply 0:foo.txt.


~ Tectu
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: FarFs f_open freeze

Post by Giovanni »

Can you pack a small application to test the problem? the simplest code you can put together.

Giovanni
sam76
Posts: 15
Joined: Fri May 03, 2013 8:07 am

Re: FarFs f_open freeze

Post by sam76 »

I have some news.

So I just added a small opening/writing code in the main thread of ARMCM4x_STM32F407_LWIP_FATFS_USB and it works (with "0:/test.log" and "test.log")

Then I have sripped all my modifications in my project (based on ARMCM4x_STM32F407_LWIP_FATFS_USB too) but I miss something because it still don't work

Another strange thing, my project works when I compile with -O2 ! So the problem occurs only with -O0 optimisation. this problem dones't occur with the original demo (-O0 and -O2 works).

I investiguate to find the différence between these 2 projects.
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: FarFs f_open freeze

Post by Giovanni »

Hi,

Probably I can give you an hint, the code compiled using -O0 consumes a lot of stack space, you should verify stack sizes. If in doubt increase all stacks then inspect the actual usage.

Giovanni
sam76
Posts: 15
Joined: Fri May 03, 2013 8:07 am

Re: FarFs f_open freeze

Post by sam76 »

Yes there is a difference, I have changed the logger stack size :

-O2 :

Code: Select all

    ChibiOS/RT Shell
ch> mem
core free memory : 73848 bytes
heap fragments   : 1
heap free total  : 512 bytes
wa_logger_daemon total : 1199 bytes
wa_logger_daemon free  : 884 bytes
wa_lwip_thread total : 612 bytes
wa_lwip_thread free  : 180 bytes
ch>

-O0

Code: Select all

     ChibiOS/RT Shell
ch> mem
core free memory : 73752 bytes
heap fragments   : 1
heap free total  : 512 bytes
wa_logger_daemon total : 1199 bytes
wa_logger_daemon free  : 755 bytes
wa_lwip_thread total : 612 bytes
wa_lwip_thread free  : 120 bytes
ch>


Where can I change the main thread size ? And How to check it ?

Another thing, fatfs don't work in event handler, is it a stack size problem too ?
Post Reply