Serial Driver flush buffer

Discussions and support about ChibiOS/HAL, the MCU Hardware Abstraction Layer.
vrollei
Posts: 163
Joined: Sun Nov 13, 2016 8:44 am
Been thanked: 26 times

Serial Driver flush buffer

Post by vrollei »

Hi!

Does it possible to flush serial driver buffer?

Thanks in advance!
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Serial Driver flush buffer

Post by Giovanni »

Hi,

If you want to erase all the buffered content there are two ways:

1) Read and discard all content using TIME_IMMEDIATE, you get MSG_TIMEOUT when the queue is empty.
2) Reset the queue directly using iqResetI().

osalSysLock();
isResetI();
osalOsRescheduleS();
osalSysUnlock();

You may use "ch" function instead of "osal" functions, it is the same.

Giovanni
vrollei
Posts: 163
Joined: Sun Nov 13, 2016 8:44 am
Been thanked: 26 times

Re: Serial Driver flush buffer

Post by vrollei »

Hi,

I mean output buffer, is there any method to force driver process all output buffer to implement this scenario:

sdStart();
chprintf("log message");
// force driver to process log message before stop it
sdStop();
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Serial Driver flush buffer

Post by Giovanni »

Hi,

There is a specific event flag for transmission complete, you need to wait for that.

Giovanni
vrollei
Posts: 163
Joined: Sun Nov 13, 2016 8:44 am
Been thanked: 26 times

Re: Serial Driver flush buffer

Post by vrollei »

Thanks!
vrollei
Posts: 163
Joined: Sun Nov 13, 2016 8:44 am
Been thanked: 26 times

Re: Serial Driver flush buffer

Post by vrollei »

Hi, Giovanni!

Is this right way to wait transmission complete:


[font=Courier New]event_listener_t serial_listener;

chEvtRegisterMaskWithFlags(&(SD2.event), &serial_listener, EVENT_MASK(0), CHN_TRANSMISSION_END);

sdStart(&STDOUT_SD, &SD2Config);

<write to STDOUT_SD>

while (true) {
chEvtWaitAny(EVENT_MASK(0));
if (chEvtGetAndClearFlags(&serial_listener) & CHN_TRANSMISSION_END) {
break;
}
}

sdStop(&STDOUT_SD);[/font]
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Serial Driver flush buffer

Post by Giovanni »

Hi,

It looks OK.

Giovanni
steved
Posts: 834
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 138 times

Re: Serial Driver flush buffer

Post by steved »

At risk of stating the obvious, dependent on your application you may need to check that there is a transmission in progress before waiting for it to complete. Or just add a timeout into the event wait.
vrollei
Posts: 163
Joined: Sun Nov 13, 2016 8:44 am
Been thanked: 26 times

Re: Serial Driver flush buffer

Post by vrollei »

Hi,

is there no queue for events? In this case you are right, there is a chance to wait forever.
Can not figure out how to check that transmission is in progress....
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Serial Driver flush buffer

Post by Giovanni »

Hi,

Events are bit masks, there is no queuing. You need to make sure there are no events pending before starting transmission.

Giovanni
Post Reply