USB_over_Serial Understanding

Discussions and support about ChibiOS/HAL, the MCU Hardware Abstraction Layer.
yosoufe
Posts: 9
Joined: Thu Jan 26, 2017 1:34 pm
Has thanked: 1 time

USB_over_Serial Understanding

Postby yosoufe » Fri Apr 13, 2018 9:41 am

Hi,
I am totally new to chibios and I have learned MCU programming by myself. So totally newbie.

I am trying to use only HAL and USB over Serial with STM32F407_discovery board. I have checked the USC_CDC example as well but that one has ch.h.

I have managed to send data PC like a hello world example and I can see the board as ttyACMx device with only hal.h.

The problem is I do not have any understanding of the callbacks in the examples. I would highly appreciate if somebody describe when the following callback are called in the example:

  • usb_event,
  • get_descriptor,
  • sduRequestsHook,
  • sof_handler

My goal is:
for first step, when a letter received from the PC, a callback will be called, and inside that callback I respond according to the letter.

If what I want to do is totally against USB specifications, let me know.

I highly appreciate any link to any tutorials about the followings:

  • USB specification,
  • General Chibi/HAL tutorial,
  • General osalSys

I am googling a lot :D

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

Re: USB_over_Serial Understanding

Postby Giovanni » Fri Apr 13, 2018 10:10 am

Hi,

Many tutorials are located on www.playembedded.org, you may start from there.

About USB callbacks, the meaning of many of those thing is buried in thousands pages of the USB specifications, what I can say is that if you use the provided usbcfg.c/.h then you should be fine. Changes are only required if you need to use custom descriptors and/or add non-standard endpoints, very complex matter.

For example SOF is called at 1mS intervals and is triggered by the USB host polling the device.

Giovanni

yosoufe
Posts: 9
Joined: Thu Jan 26, 2017 1:34 pm
Has thanked: 1 time

Re: USB_over_Serial Understanding

Postby yosoufe » Mon Apr 16, 2018 10:10 am

Thanks,
which callback is called when a letter arrived over usb??

yosoufe
Posts: 9
Joined: Thu Jan 26, 2017 1:34 pm
Has thanked: 1 time

Re: USB_over_Serial Understanding

Postby yosoufe » Wed Apr 18, 2018 7:30 am

I still do not understand how this works??
The documentation is not enough for me to understand if this is the only documentation
http://chibios.sourceforge.net/docs3/ha ... u_s_b.html

Any link or guide how this works?? How should it be used???

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

Re: USB_over_Serial Understanding

Postby Giovanni » Wed Apr 18, 2018 7:33 am

Hi,

The way to proceed is to understand the various USB demos, if you use the Serial_USB driver the use is very simple, you don't have to mess with callbacks at all, it is all done internally. Those callbacks are used by the Serial_USB driver so there is no need to change anything.

Using callbacks is only required if you want to implement a custom protocol or device type which should not be your case.

Giovanni

yosoufe
Posts: 9
Joined: Thu Jan 26, 2017 1:34 pm
Has thanked: 1 time

Re: USB_over_Serial Understanding

Postby yosoufe » Mon Apr 23, 2018 7:55 am

I have it somehow working. I would like to know your opinion about the solution because I had to make a small change in hal_serial_usb.c in function sduDataReceived I changed the position of chnAddFlagsI and ibqPostFullBufferI function calls.

Now it fills the buffer first, then it adds the flag. Now in the flag I can use a callback to read data from the filled buffer. (before it was first activating the flag and then filling the buffer, Therefore the callback was being called before filling the buffer. So reading the buffer was 1 step behind.)
Now it looks like this:

Code: Select all

void sduDataReceived(USBDriver *usbp, usbep_t ep) {
  SerialUSBDriver *sdup = usbp->out_params[ep - 1U];
  if (sdup == NULL) {
    return;
  }

  osalSysLockFromISR();

  /* Posting the filled buffer in the queue.*/
  ibqPostFullBufferI(&sdup->ibqueue,
                     usbGetReceiveTransactionSizeX(sdup->config->usbp,
                                                   sdup->config->bulk_out));

  /* Signaling that data is available in the input queue.*/
  chnAddFlagsI(sdup, CHN_INPUT_AVAILABLE);

  /* The endpoint cannot be busy, we are in the context of the callback,
     so a packet is in the buffer for sure. Trying to get a free buffer
     for the next transaction.*/
  (void) sdu_start_receive(sdup);

  osalSysUnlockFromISR();
}



What do you think about this change in HAL source code??

Now in initialization I have like this.

Code: Select all

   sduObjectInit(&PORTAB_SDU1);
   sduStart(&PORTAB_SDU1, &serusbcfg);
   PORTAB_SDU1.event.cb = &sduCB; // <<---------------------------------------------------- attention

   usbDisconnectBus(serusbcfg.usbp);
   osalThreadSleepMilliseconds(1500);
   usbStart(serusbcfg.usbp, &usbcfg);
   usbConnectBus(serusbcfg.usbp);


where

Code: Select all

unsigned char buffer[5];
void sduCB (event_source_t *esp){
   volatile unsigned int temp = esp->flags & CHN_INPUT_AVAILABLE;
   if (temp != CHN_INPUT_AVAILABLE)
      return;

   volatile unsigned int size_read;
   volatile unsigned int size_written;
   while( (size_read = chnReadTimeout(&PORTAB_SDU1, (unsigned char*)buffer, 1, TIME_IMMEDIATE)) )
   {
      size_written = chnWriteTimeout(&PORTAB_SDU1, (unsigned char*)buffer, 1, TIME_IMMEDIATE);
   }
   (void)size_written;
}


Do you see any problem in the change I made in HAL source code and my approach??

How can I find out how many char in buffer is available?
Last edited by yosoufe on Mon Apr 23, 2018 8:02 am, edited 1 time in total.

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

Re: USB_over_Serial Understanding

Postby Giovanni » Mon Apr 23, 2018 7:59 am

Hi,

You are callinng blocking functions from a callback, that is forbidden. Enable the state checker in chconf.h and it would stop you.

Giovanni

yosoufe
Posts: 9
Joined: Thu Jan 26, 2017 1:34 pm
Has thanked: 1 time

Re: USB_over_Serial Understanding

Postby yosoufe » Mon Apr 23, 2018 8:11 am

Giovanni wrote:Hi,

You are callinng blocking functions from a callback, that is forbidden. Enable the state checker in chconf.h and it would stop you.

Giovanni


I do not have chconf.h. I am only using HAL, no RT. Is it still problematic?
which function is blocking??

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

Re: USB_over_Serial Understanding

Postby Giovanni » Mon Apr 23, 2018 8:27 am

From callbacks and ISRs you should not use functions that don't have a final "I". All rules still apply even without RT.

Giovanni


Return to “ChibiOS/HAL”

Who is online

Users browsing this forum: No registered users and 5 guests