Page 1 of 1

thread blocked by chprintf

Posted: Wed Jun 08, 2016 12:33 pm
by icastell
Hello forum.

This is my first post to this forum, hope this questions will be useful to someone else...

My STM32F0 board has a magnetometer/accelerometer sensor connected. I'm using a Chibios static thread to read sensor information every 20ms and write it to a Linux host, using the USB-CDC serial driver (/dev/ttyACM0).

To debug I am using a blinking led inside the thread, and "chprintf" syscall to write the information from Chibios to Linux side:

chprintf((BaseSequentialStream *)&SDU1,
"%d %d %d %d %d %d\r\n", xa, ya, za, xm, ym, zm);

While minicom console app (Linux side) is connected and printing chars sent by Chibios to USB port, this solution works great, the led toggles on/off every loop. However, after closing minicom console app, board running chibios seems to block at "chprintf" after a second or so, led stops blinking, and everything continues stalled until minicom app is executed again (chprintf seems to unblock and led starts blinking again).

What is wrong with this solution and how this problem should be fixed?

Thank you in advance!

Re: thread blocked by chprintf

Posted: Wed Jun 08, 2016 1:06 pm
by RoccoMarco
I don't know how internally works SDU1 but (as far I can remember) if the connection is closed the thread goes in suspend mode. Thread becomes ready as soon as you open the COM port on Linux/Windows side.

I suppose that the SDU write function (used internally by chprintf) is blocking (I will take a look to the code). Why this is a problem for you?

Ciao,
RM

Re: thread blocked by chprintf

Posted: Wed Jun 08, 2016 1:10 pm
by RoccoMarco
Maybe I get it. Are you using the same thread to read data and print data? In this case use two separate threads. The printing thread will run only when connection is open.
Ciao,
RM

Re: thread blocked by chprintf

Posted: Wed Jun 08, 2016 1:41 pm
by Giovanni
Hi,

Which ChibiOS version are you using?

Giovanni

Re: thread blocked by chprintf

Posted: Wed Jun 08, 2016 3:20 pm
by icastell
I'm using the "stable_16.1.x" branch.

Yes, the same thread is reading from sensor and writing to the console. I will try suggested solution.

Thanks a lot for your fast answer.

Re: thread blocked by chprintf

Posted: Wed Jun 08, 2016 3:28 pm
by Giovanni
Hi,

Version 16.1 should not be affected by this problem, could you post your usbcfg.c file?

Giovanni

Re: thread blocked by chprintf

Posted: Wed Jun 08, 2016 3:38 pm
by icastell
My usbcfg.c file has the same md5sum as the file I took as example to start writing my project:

2b2187c33fc8f8cef726be72f0b5fcdf usbcfg.c
2b2187c33fc8f8cef726be72f0b5fcdf ../ChibiOS/testhal/STM32/STM32F0xx/USB_CDC/usbcfg.c

Re: thread blocked by chprintf

Posted: Wed Jun 08, 2016 4:41 pm
by Giovanni
At line 292 there is the workaround, on USB disconnect the SDU driver is reset and threads are freed from write operations. The problem is that you don't disconnect the USB peripheral, you simply stop consuming the sent data, when buffers are filled the application hangs, the device has no way to know that you closed the application on the host side.

What you could do is to add a small timeout to write operations, however chprintf() does not support this directly. You could print using chprintf to a memory stream then write the buffer with timeout using chnWriteTimeout().

Giovanni

Re: thread blocked by chprintf

Posted: Thu Jun 09, 2016 8:24 am
by icastell
Yes, that fixes my issue.
Thank you very much for the fast support and congratulations for the fantastic work you are doing for the Chibios community, Mr. Giovanni.