USB stall error

Discussions and support about ChibiOS/HAL, the MCU Hardware Abstraction Layer.
Viljami
Posts: 15
Joined: Tue Apr 05, 2022 3:32 pm
Has thanked: 13 times

USB stall error

Post by Viljami »

Hello!

If a USB stall error occurs is there any common procedure to clear/solve it?
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: USB stall error

Post by Giovanni »

Hi,

It depends on the cause of the stall, if it is an unimplemented command then you need to implement the command handler (not all CDC commands are implemented by default).

Giovanni
Viljami
Posts: 15
Joined: Tue Apr 05, 2022 3:32 pm
Has thanked: 13 times

Re: USB stall error

Post by Viljami »

Giovanni wrote:Hi,

It depends on the cause of the stall, if it is an unimplemented command then you need to implement the command handler (not all CDC commands are implemented by default).

Giovanni


Hello Giovanni and thanks for your quick response!

Could you describe causes which generate USB stalls?

BR

Viljami
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: USB stall error

Post by Giovanni »

Find all instances of "usb_lld_stall" in hal_usb.c, those are all the possible causes, mainly are unimplemented EP0 commands.

Giovanni
Viljami
Posts: 15
Joined: Tue Apr 05, 2022 3:32 pm
Has thanked: 13 times

Re: USB stall error

Post by Viljami »

Giovanni wrote:Find all instances of "usb_lld_stall" in hal_usb.c, those are all the possible causes, mainly are unimplemented EP0 commands.

Giovanni


Hello again!

Could you mention USB limitations? I am working with ChibiOS RT version 21.11.1.

BR

Viljami
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: USB stall error

Post by Giovanni »

Simply you have to implement the EP0 commands you need, there are no intrinsic limitations.

See the "requests_hook_cb" callback in the USBConfig structure. An example of commands implementation is in sduRequestsHook() into hal_serial_usb.c.

It is complex matter, knowledge of USB spec is required. Non answering to a command causes those stalls.

Giovanni
Viljami
Posts: 15
Joined: Tue Apr 05, 2022 3:32 pm
Has thanked: 13 times

Re: USB stall error

Post by Viljami »

Hello again!

I want tell more about this stall error.

I have a ChibiOS based system which applies my own defined messages. I apply bulk transfer in my system. My system is capable to handle (receive and transmit) the first my own defined USB message but second one fails because of the stall error. A function bool requestsHook(USBDriver *usbp) was an empty implementation which just returned FALSE.

The next step was to debug and implement code for ep0 into bool requestsHook(USBDriver *usbp). I noticed that bool requestsHook(USBDriver *usbp) receives

    * USB_REQ_SET_ADDRESS
    * USB_REQ_SET_CONFIGURATION
    * USB_REQ_GET_DESCRIPTOR

The implemented code did not solve my problem; the earlier stall problem still occurs (the first my own defined USB message is received and transmitted successfully but the second one fails because of the stall error).

Here is the code:

Code: Select all

bool requestsHook(USBDriver *usbp) {
    // (void) usbp;

    if ((usbp->setup[0] & USB_RTYPE_DIR_MASK) == USB_RTYPE_DIR_HOST2DEV)
    {
        if (usbp->setup[1] == USB_REQ_SET_ADDRESS)
        {
            set_address(usbp);
            usbSetupTransfer(usbp, NULL, 0, NULL);
            return true;
        }
        else if (usbp->setup[1] == USB_REQ_SET_CONFIGURATION)
        {
            if (usbp->configuration != usbp->setup[2])
            {
                /* If the USB device is already active then we have to perform the clear
                   procedure on the current configuration.*/
                if (usbp->state == USB_ACTIVE)
                {
                  /* Current configuration cleared.*/
                  osalSysLockFromISR ();
                  usbDisableEndpointsI(usbp);
                  osalSysUnlockFromISR ();
                  usbp->configuration = 0U;
                  usbp->state = USB_SELECTED;
                  _usb_isr_invoke_event_cb(usbp, USB_EVENT_UNCONFIGURED);
                }
                if (usbp->setup[2] != 0U)
                {
                  /* New configuration.*/
                  usbp->configuration = usbp->setup[2];
                  usbp->state = USB_ACTIVE;
                  _usb_isr_invoke_event_cb(usbp, USB_EVENT_CONFIGURED);
                }
            }
            usbSetupTransfer(usbp, NULL, 0, NULL);
            return true;
        }
    }
    else if ((usbp->setup[0] & USB_RTYPE_DIR_MASK) == USB_RTYPE_DIR_DEV2HOST)
    {
        if (usbp->setup[1] == USB_REQ_GET_DESCRIPTOR)
        {
            const USBDescriptor *dp = usbp->config->get_descriptor_cb(usbp, usbp->setup[3],
                                                                      usbp->setup[2],
                                                                      get_hword(&usbp->setup[4]));
            if (dp == NULL)
            {
                return false;
            }

            usbSetupTransfer(usbp, (uint8_t *)dp->ud_string, dp->ud_size, NULL);
            return true;
        }
    }

    return FALSE;
}


BR

Viljami
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: USB stall error

Post by Giovanni »

Hi,

You are not supposed to process default messages in that handler (USB_REQ_SET_CONFIGURATION, USB_REQ_SET_ADDRESS etc), those are processed in the driver if your installed handler returns false.

Just handle your messages and return true, else return false and the driver will handle default ones, the stall only occurs if both your handle AND the default handler do not recognize some message.

Giovanni
Viljami
Posts: 15
Joined: Tue Apr 05, 2022 3:32 pm
Has thanked: 13 times

Re: USB stall error

Post by Viljami »

Hello again Giovanni!

As I mentioned in the previous mail

I noticed that bool requestsHook(USBDriver *usbp) receives

* USB_REQ_SET_ADDRESS
* USB_REQ_SET_CONFIGURATION
* USB_REQ_GET_DESCRIPTOR


were the only messages what I catched by debugging; I did not see any my own USB messages in bool requestsHook(USBDriver *usbp).

BR

Viljami
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: USB stall error

Post by Giovanni »

Hi,

Is it possible your descriptor is wrong somehow? it happens after the host retrieves it.

Giovanni
Post Reply