Page 1 of 2

SDU and callbacks for received data

Posted: Sat Sep 15, 2012 9:48 am
by kulve
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.

Re: SDU and callbacks for received data

Posted: Sat Sep 15, 2012 10:03 am
by Giovanni
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

Re: SDU and callbacks for received data

Posted: Sat Sep 15, 2012 10:12 am
by kulve
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

Re: SDU and callbacks for received data

Posted: Sat Sep 15, 2012 10:42 am
by Giovanni
The serial drivers are descendant of IO Channels, look into io_channel.h.

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

Posted: Sat Sep 15, 2012 4:09 pm
by kulve
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

Re: SDU and callbacks for received data

Posted: Sat Sep 15, 2012 5:57 pm
by Giovanni
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

Re: SDU and callbacks for received data

Posted: Sat Sep 15, 2012 6:03 pm
by kulve
No, looks like it's never called.

Re: SDU and callbacks for received data

Posted: Sat Sep 15, 2012 6:20 pm
by Giovanni
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

Re: SDU and callbacks for received data

Posted: Sat Sep 15, 2012 7:07 pm
by Giovanni
I committed the following change to serial_usb.c:

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

Posted: Sat Sep 15, 2012 8:17 pm
by kulve
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.