SDU and callbacks for received data
Moderators: RoccoMarco, lbednarz, tfAteba
SDU and callbacks for received data
Hi
The testhal example uses Serial USB with Shell and Shell in turn uses blocking read.
I tried reading the docs and the source but couldn't figure out how could I get e.g. a callback from which I could call e.g. chSequentialStreamRead() knowing that there is data to read?
UARTConfig has rxchar_cb callback that is called for each character. I'm looking for something like that.
Thanks.
The testhal example uses Serial USB with Shell and Shell in turn uses blocking read.
I tried reading the docs and the source but couldn't figure out how could I get e.g. a callback from which I could call e.g. chSequentialStreamRead() knowing that there is data to read?
UARTConfig has rxchar_cb callback that is called for each character. I'm looking for something like that.
Thanks.
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: SDU and callbacks for received data
The serial drivers are synchronous and do not have callbacks, they generate events however, you can have a thread listening for events and doing the read/write in event handlers.
A single event allows you to handle read, write and errors, when using events you usually always use non-blocking operations by specifying TIME_IMMEDIATE.
Giovanni
A single event allows you to handle read, write and errors, when using events you usually always use non-blocking operations by specifying TIME_IMMEDIATE.
Giovanni
Re: SDU and callbacks for received data
By events you mean e.g. USBConfig's usb_event (I'm looking at the testhal's USB_CDC code)?
Does one of those events tell me that there's data to read (at least possibly)?
Thanks
Does one of those events tell me that there's data to read (at least possibly)?
Thanks
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: SDU and callbacks for received data
The serial drivers are descendant of IO Channels, look into io_channel.h.
The following events are handled:
You can register on the event using chnGetEventSource() and read the condition flags above using chnGetAndClearFlags().
Giovanni
The following events are handled:
Code: Select all
/** @brief No pending conditions.*/
#define CHN_NO_ERROR 0
/** @brief Connection happened.*/
#define CHN_CONNECTED 1
/** @brief Disconnection happened.*/
#define CHN_DISCONNECTED 2
/** @brief Data available in the input queue.*/
#define CHN_INPUT_AVAILABLE 4
/** @brief Output queue empty.*/
#define CHN_OUTPUT_EMPTY 8
/** @brief Transmission end.*/
#define CHN_TRANSMISSION_END 16
/** @} */
You can register on the event using chnGetEventSource() and read the condition flags above using chnGetAndClearFlags().
Giovanni
Re: SDU and callbacks for received data
I pretty much cut'n'pasted the USB stuff from the testhal and the event handling from demos/Posix-GCC/main.c but chEvtDispatch() doesn't seem to ever return.
Here's my code just in case somebody has any ideas:
https://github.com/snowcap-electronics/ ... sdu.c#L367
My main() calls the sc_sdu_init() in the above code which in turns starts up a thread that keeps waiting for the events.
Thanks
Here's my code just in case somebody has any ideas:
https://github.com/snowcap-electronics/ ... sdu.c#L367
My main() calls the sc_sdu_init() in the above code which in turns starts up a thread that keeps waiting for the events.
Thanks
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: SDU and callbacks for received data
Is the event handler called at least once?
If so probably it is not returning because you don't use TIME_IMMEDIATE as timeout specification, the read operation sleeps until all the specified data is read.
Hint, use BaseAsynchronousChannel methods, it is a descendant of SequentialStream, see chnReadTimeout() and chnWriteTimeout().
Giovanni
If so probably it is not returning because you don't use TIME_IMMEDIATE as timeout specification, the read operation sleeps until all the specified data is read.
Hint, use BaseAsynchronousChannel methods, it is a descendant of SequentialStream, see chnReadTimeout() and chnWriteTimeout().
Giovanni
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: SDU and callbacks for received data
I think it is because an USB read transaction is not started until a read from the SDU1 is performed so the event is never broadcasted (it is done in the USB callback), try adding a zero bytes read before entering the event wait loop.
Something similar has already been reported, I'll add a fix in next release.
Giovanni
Something similar has already been reported, I'll add a fix in next release.
Giovanni
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: SDU and callbacks for received data
I committed the following change to serial_usb.c:
It should fix the problem you experienced.
Giovanni
Code: Select all
void sduConfigureHookI(USBDriver *usbp) {
SerialUSBDriver *sdup = usbp->param;
sdup->flags = CHN_NO_ERROR;
chIQResetI(&sdup->iqueue);
chOQResetI(&sdup->oqueue);
chnAddFlagsI(sdup, CHN_CONNECTED);
/* Starts the first OUT transaction immediately.*/
usbPrepareQueuedReceive(usbp, USB_CDC_DATA_AVAILABLE_EP, &sdup->iqueue,
usbp->epc[USB_CDC_DATA_AVAILABLE_EP]->out_maxsize);
usbStartReceiveI(usbp, USB_CDC_DATA_AVAILABLE_EP);
}
It should fix the problem you experienced.
Giovanni
Re: SDU and callbacks for received data
Thanks for the patches but unfortunately my issue stays.
I tried adding chSequentialStreamRead with 0 bytes of size but it leads only to "chDbgPanic (msg=0x8007cc0 "\"chIQReadTimeout\"()") at ChibiOS/os/kernel/src/chdebug.c:267.
I'll continue tomorrow with fresh eyes and try double checking my HAL settings and the USB code.
I tried adding chSequentialStreamRead with 0 bytes of size but it leads only to "chDbgPanic (msg=0x8007cc0 "\"chIQReadTimeout\"()") at ChibiOS/os/kernel/src/chdebug.c:267.
I'll continue tomorrow with fresh eyes and try double checking my HAL settings and the USB code.