FarFs f_open freeze

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

Moderators: RoccoMarco, lbednarz, tfAteba

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

Re: FarFs f_open freeze

Post by Tectu »

The main stack size is set in the linker script (.ld file).

Code: Select all

cat os/ports/GCC/ARMCMx/STM32F4xx/ld/STM32F407xG.ld
...
__main_stack_size__     = 0x0400;



~ Tectu
DrunkenDonkey
Posts: 124
Joined: Sun Aug 28, 2011 5:16 pm

Re: FarFs f_open freeze

Post by DrunkenDonkey »

There's an excellent eclipse plugin coming with ChibiOS, you can break the program at any point and examine every thread
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: FarFs f_open freeze

Post by Tectu »

That's true, but actually that's a feature provided by OpenOCD and GDB. The eclipse plugin just provides some eye candy (at least some people say so).


~ 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 »

Not true,

The plugin does things that are not done in OpenOCD/GDB, including the residual stack size calculation.

The stack of the main() function can be changed in the linker script file. You need to copy the default linker script into your project directory then make the makefile use that one. Note that you have to change the PROCESS stack not the __main_stack_size__, the main stack is the stack for IRQs. Sorry for the confusion but it is how ARM named them.

Event handlers are executed in the context of the thread that performs chEvtDispatch(), probably the main() function stack in your case.

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

Re: FarFs f_open freeze

Post by Tectu »

I'm sorry for giving wrong information here. I thought at least the stack thing would be right since it seemed to work for me last time...


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

Re: FarFs f_open freeze

Post by sam76 »

Ok I have seen the linker script, but I think 1024 bytes is enought.

At this point, my main thread and logger thread can open files but can't write in it. (no hang, fs_type from file descriptor and device don't match). So I go back to my problem.

Unfortunately DrunkenDonkey, I haven't windows computer. I will install one.

this is what I do :

Code: Select all


//Global
EventSource inserted_event, removed_event;
extern EventSource sdc_connected_event, sdc_disconnected_event;
FATFS SDC_FS;

int main(void) {

  //INIT stuff ; usb, shell...
  //blink, lwip, http thread

  chEvtInit(&inserted_event);
  chEvtInit(&removed_event);

  chEvtRegisterMask(&inserted_event, &el0, 1);
  chEvtRegisterMask(&removed_event, &el1, 2);


  chThdCreateStatic(wa_logger_daemon, sizeof(wa_logger_daemon), NORMALPRIO + 1, logger_daemon, NULL);

  uint32_t EvtMsk = 0;
  while (TRUE) {
    //SHELL stuff....

    EvtMsk = chEvtWaitOneTimeout(1 | 2, MS2ST(500));
        // Serve event
    if(EvtMsk & 1) {
      if (!sdcConnect(&SDCD1))
      {
        if ( wf_mount(0, &SDC_FS) == FR_OK)
        {
          fs_ready = TRUE;
          chEvtBroadcast(&sdc_connected_event);//, &wa_logger_daemon);
          FIL test;
          uint8_t nb=0;
          if ( wf_open(&test, "main.log", FA_OPEN_ALWAYS) != FR_OK )
          {
            wf_write(&test, "\n", 1, &nb);
            wf_close(&test);
          }
        }
        else
          sdcDisconnect(&SDCD1);
      }
    }

    if(EvtMsk & 2) {
      sdcDisconnect(&SDCD1);
      fs_ready = FALSE;
      chEvtBroadcast(&sdc_disconnected_event);//, &wa_logger_daemon);
    }   
  }
}


Code: Select all


extern EventSource inserted_event, removed_event;
EventSource sdc_connected_event, sdc_disconnected_event;

/**
 * Stack area for the display thread.
 */
WORKING_AREA(wa_logger_daemon, 1024);

msg_t
logger_daemon(void *p)
{
  chEvtInit(&sdc_connected_event);
  chEvtInit(&sdc_disconnected_event);

  //INIT stuffs...

  chEvtRegisterMask(&inserted_event, &el0, 1);
  chEvtRegisterMask(&removed_event, &el1, 2);
  chEvtRegisterMask(&sdc_connected_event, &el3, 4);
  chEvtRegisterMask(&sdc_disconnected_event, &el4, 8);

  while (TRUE) {
    EvtMsk = chEvtWaitOneTimeout( 4 | 8, MS2ST(500));
    if( (EvtMsk & 1) || (EvtMsk & 2)){

    }   
    if(EvtMsk & 4) {
      sdc_ready = TRUE;
      FIL test;
      uint8_t nb=0;
      if ( wf_open(&test, "thread.log.", FA_OPEN_ALWAYS) != FR_OK )
      {
        wf_write(&test, "\n", 1, &nb);
        wf_close(&test);
      }
      //Log enable
    }   

    if(EvtMsk & 8) {
      sdc_ready = FALSE;
      //log disable
    }   
  }     
}

}


At the end I have 2 empty files.

Is the wf_mount is global to all thread ? does the drive mount for multiple open/close ?

Another thing, I have added my events because when I want to talk to my logger thread, I must use chEvtBroadcast because with chEvtSignal, it doesn't receive the event.
sam76
Posts: 15
Joined: Fri May 03, 2013 8:07 am

Re: FarFs f_open freeze

Post by sam76 »

I like gdb, I can see thread free memory now.

Code: Select all

Breakpoint 1, sWriteToFile (_this=0x2000087c, logLevel=Error, fmt=0x8030a84 "SD Card mounted", ap=...) at Log/file_logger.c:184
184      FileLogWriter *flw = (FileLogWriter*) _this;
(gdb) n
186      __CHECK_AND_ROLLBACK(flw);
(gdb)
191      FRESULT err = 0;
(gdb)
192      if(!_this || ( err = wf_open(&(flw->fp), thread_file_log, FA_WRITE | FA_OPEN_ALWAYS) != FR_OK ) )
(gdb)
208         uint8_t nb = 0;
(gdb)
210         rtcGetTimeTm(&RTCD1, &timp);
(gdb) p flw
$21 = (FileLogWriter *) 0x2000087c
(gdb) n
214         err = wf_write(&(flw->fp), "[TEST][TEST]", 12, &nb);//, sGetLogPrefix(logLevel), asctime(&timp));
(gdb) p flw->fp
$22 = {fs = 0x200091d0, id = 1, flag = 18 '\022', pad1 = 0 '\000', fptr = 0, fsize = 9, sclust = 7, clust = 0, dsect = 0, dir_sect = 23536,
  dir_ptr = 0x20009224 "PLSEE   LOG ", buf = '\000' <repeats 511 times>}
(gdb) n
216         err = wf_write(&(flw->fp), "\n", 1, &nb);
(gdb) p flw->fp
$23 = {fs = 0x55555555, id = 21845, flag = 85 'U', pad1 = 85 'U', fptr = 1431655765, fsize = 1431655765, sclust = 1431655765, clust = 1431655765, dsect = 1431655765,
  dir_sect = 1431655765, dir_ptr = 0x55555555 "", buf = 'U' <repeats 512 times>}
(gdb) p/x wa_logger_daemon
$24 = {0x2000846420008388, 0x20009b4c00000041, 0x20009d3820008464, 0x2000945c00000000, 0x14010001, 0x2000943c00000000, 0x2000944020009440, 0x120009bb4, 0x4100000000,
  0x55555555ffffffff, 0x5555555555555555 <repeats 213 times>, 0x802463155555555, 0x5555555500000000, 0x5555555555555555, 0x5555555555555555, 0x8000f2555555555,
  0xa006e9855555555, 0x2000941055555555, 0x8002c2f00000000, 0x802463100000000, 0x5555555500000000, 0x5555555555555555, 0x5555555555555555, 0x8000f2555555555,
  0xa006e9855555555, 0x2000941055555555, 0x8002c2f00000000, 0x20006e9800000000, 0x2000941000000000, 0x80234c920009bb8, 0xc20009c0a, 0x10000000800045e,
  0x20006e9800000000, 0x0, 0x8002c3355555555, 0x21000000080246c2, 0x8030a8420009c50, 0x2000087c04555555, 0x2300000000, 0x1700000020, 0x60000000b, 0x200000064,
  0xffffffff00000000, 0x20000000000c5555, 0x80249ad00000008, 0x20009c50000001f4, 0x400a13c08030a84, 0x20007edc, 0x802459f08024471, 0x8030a8420009410, 0x20009c50,
  0x408024555, 0x20007f3400000001, 0x55555555, 0x0, 0x2000940800000200, 0x820009410, 0x2000940c00000000, 0x420009410, 0x200091cc00000000, 0x220009410,
  0x200091c800000000, 0x120009410, 0x400000000, 0x800048f20009410, 0x80010b920009410}
(gdb) p chUnusedStack(wa_logger_daemon,sizeof(wa_logger_daemon))
$25 = 1786
(gdb) p flw
$26 = (FileLogWriter *) 0x20000000
(gdb)


So, now, I can write one time, the second call hangs. I lost a pointer ! I think the probleme is now outside the fatfs. I just correct one thing before a f_stat call : don't forget to memset the FILINFO struct to 0, elsewhere, a lfsize is unitilised and used.
sam76
Posts: 15
Joined: Fri May 03, 2013 8:07 am

Re: FarFs f_open freeze

Post by sam76 »

I have my problem ! I used a uint8_t * for the returned number of written bytes instead of uint16_t *

Now It work perfectly :)

Thanks for help ! Goto Next step :)
Post Reply