usb-cdc : how to know when connexion is effective ?

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

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
User avatar
alex31
Posts: 445
Joined: Fri May 25, 2012 10:23 am
Has thanked: 57 times
Been thanked: 88 times

usb-cdc : how to know when connexion is effective ?

Post by alex31 »

I want to know know if serial connection if effective with other side.
I can know that usb is connected via testing like that :

Code: Select all

SDU1.config->usbp->state == USB_ACTIVE


but if a want to know if i have a terminal connected on the serial over usb link, so that the bytes i output will be received on
the other side.

Thanks
Alexandre
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: usb-cdc : how to know when connexion is effective ?

Post by Giovanni »

Hi,

The host sends CDC messages that you can detect in the messages callback. For example, most communication programs send the message to setup the communication speed after opening the port.

Giovanni
User avatar
alex31
Posts: 445
Joined: Fri May 25, 2012 10:23 am
Has thanked: 57 times
Been thanked: 88 times

Re: usb-cdc : how to know when connexion is effective ?

Post by alex31 »

Hi,

unfortunately, le terminal i use don't send anything at startup, but i will try to see if
i can configure it to do so.

well, if i hit keyboard to send a char,
i can detect the first char via

Code: Select all

 chSequentialStreamGet(&SDU1);
the first char is lost, but then i know i am connected.
i have tested to do

Code: Select all

 while (sdGetWouldBlock(&SDU1)) {chThdSleepMilliseconds(100);}

but sdGetWouldBlock always return TRUE, sdGetWouldBlock could not be used with usb_cdc ?

I have also noticed that channel abstraction function for blocking test which was chIOPutWouldBlock()
and chIOGetWouldBlock() in v2.4.x does not exists anymore in 2.5.0, even if there are still referenced
in the commentary section of os/hal/include/serial.h.

If it's possible, the best for user of chibios would be to know the connection status without the need to receive a byte first.


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

Re: usb-cdc : how to know when connexion is effective ?

Post by Giovanni »

The serial driver also generates an even on incoming data and sets the flag CHN_INPUT_AVAILABLE, you can detect the event without having to consume data from the queue.

sdGetWouldBlock() cannot be used, note the prefix is the one of the normal serial driver, it is not part of the standard interface.

Giovanni
User avatar
alex31
Posts: 445
Joined: Fri May 25, 2012 10:23 am
Has thanked: 57 times
Been thanked: 88 times

Re: usb-cdc : how to know when connexion is effective ?

Post by alex31 »

The serial driver also generates an even on incoming data and sets the flag CHN_INPUT_AVAILABLE


Testing the flag works only if somewhere a read is done, until a read is made, flag is not set ... :

Code: Select all

     while (!(((SerialUSBDriver *)(USBD2.param))->flags & CHN_INPUT_AVAILABLE)) {
   chThdSleepMilliseconds(100);
   //chSequentialStreamGet(&SDU1);
      }


works if chSequentialStreamGet is uncommented, never go out the loop otherwise. (and, by the way, no event is emitted)

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

Re: usb-cdc : how to know when connexion is effective ?

Post by Giovanni »

Accessing fields is not correct, functionalities have function or macros, for example flags are accessed using chnGetAndClearFlags().

alex31 wrote:Testing the flag works only if somewhere a read is done, until a read is made, flag is not set


This is probably because an USB read transaction is not started until a read is performed at least once, I will look into this. You are not getting the event for the same reason, the event is generated when the flag is set.

You should be able to workaround this by calling:

Code: Select all

chnGetTimeout(&SDU1, TIME_IMMEDIATE);


this will "trigger" the first transaction and will not wait inside.

Giovanni
User avatar
alex31
Posts: 445
Joined: Fri May 25, 2012 10:23 am
Has thanked: 57 times
Been thanked: 88 times

Re: usb-cdc : how to know when connexion is effective ?

Post by alex31 »

Accessing fields is not correct, functionalities have function or macros, for example flags are accessed using chnGetAndClearFlags().


I had seen that, but since it also clear the flag, i didn't want to modify it, but following your advice i change to use the api and it works.

You should be able to workaround this by calling:

Code: Select all

chnGetTimeout(&SDU1, TIME_IMMEDIATE);


thanks, it works perfectly !

Alexandre
Post Reply