thread blocked by chprintf Topic is solved

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

Moderators: RoccoMarco, lbednarz, utzig, tfAteba, barthess

icastell
Posts: 10
Joined: Wed Jun 08, 2016 11:43 am
Has thanked: 1 time
Been thanked: 1 time

thread blocked by chprintf

Postby icastell » Wed Jun 08, 2016 12:33 pm

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!

User avatar
RoccoMarco
Posts: 655
Joined: Wed Apr 24, 2013 4:11 pm
Location: Munich (Germany)
Has thanked: 83 times
Been thanked: 67 times
Contact:

Re: thread blocked by chprintf

Postby RoccoMarco » Wed Jun 08, 2016 1:06 pm

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

User avatar
RoccoMarco
Posts: 655
Joined: Wed Apr 24, 2013 4:11 pm
Location: Munich (Germany)
Has thanked: 83 times
Been thanked: 67 times
Contact:

Re: thread blocked by chprintf

Postby RoccoMarco » Wed Jun 08, 2016 1:10 pm

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

User avatar
Giovanni
Site Admin
Posts: 14457
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: thread blocked by chprintf

Postby Giovanni » Wed Jun 08, 2016 1:41 pm

Hi,

Which ChibiOS version are you using?

Giovanni

icastell
Posts: 10
Joined: Wed Jun 08, 2016 11:43 am
Has thanked: 1 time
Been thanked: 1 time

Re: thread blocked by chprintf

Postby icastell » Wed Jun 08, 2016 3:20 pm

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.

User avatar
Giovanni
Site Admin
Posts: 14457
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: thread blocked by chprintf

Postby Giovanni » Wed Jun 08, 2016 3:28 pm

Hi,

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

Giovanni

icastell
Posts: 10
Joined: Wed Jun 08, 2016 11:43 am
Has thanked: 1 time
Been thanked: 1 time

Re: thread blocked by chprintf

Postby icastell » Wed Jun 08, 2016 3:38 pm

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

User avatar
Giovanni
Site Admin
Posts: 14457
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: thread blocked by chprintf  Topic is solved

Postby Giovanni » Wed Jun 08, 2016 4:41 pm

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

icastell
Posts: 10
Joined: Wed Jun 08, 2016 11:43 am
Has thanked: 1 time
Been thanked: 1 time

Re: thread blocked by chprintf

Postby icastell » Thu Jun 09, 2016 8:24 am

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.


Return to “General Support”

Who is online

Users browsing this forum: No registered users and 52 guests