dmitrystu / libusb_stm32

Lightweight USB device Stack for STM32 microcontrollers
Apache License 2.0
696 stars 160 forks source link

Best way to control EP data send flow (ACK/NACK)? #96

Closed GrantMTG closed 2 years ago

GrantMTG commented 2 years ago

Regarding sending data to the host PC. I wish to have my IN endpoints NAK when there is no data to send and ACK when there is data to send.

Using the "demo" as an example (I am using STM32F103), the endpoint event callback is called every time that a transfer is complete (usbd_evt_eptx). In this callback there is ALWAYS data ready for the next transfer and so the endpoint is always provided with data. For example:

static void cdc_txonly(usbd_device *dev, uint8_t event, uint8_t ep) {
uint8_t _t = dev->driver->frame_no();
memset(fifo, _t, CDC_DATA_SZ);
usbd_ep_write(dev, ep, fifo, CDC_DATA_SZ);   // Always sends/ACKs
}

or

static void hid_mouse_move(usbd_device *dev, uint8_t event, uint8_t ep) {
static uint8_t t = 0;
:
snip
:
t = (t + 1) & 0x7F;
usbd_ep_write(dev, ep, &hid_report_data, sizeof(hid_report_data));   // Always sends/ACKs
}

This works "ok" and if one stops writing to the EP, then the ACKing turns correctly to NAKing. My problem is that some time later I need to start sending data again and I don't understand how to restart the process where I can send data to the endpoint.

From the main level, do I just do this (note &udev since this is not in the callback anymore) ??

usbd_ep_write(&udev, HID_RIN_EP, &hid_report_data, sizeof(hid_report_data));

Are there issues with interrupts happening while doing this? I could maybe use the SOF event (usbd_evt_sof), but I don't see where I have access to that in any callback that I can see. Should/can this be added to the core?

Maybe I have missed something, but I'm not sure how to proceed.

GrantMTG commented 2 years ago

I think I have a workable solution. I removed any data activities from the "usbd_evt_eptx" Endpoint Event callback (what you called hid_mouse_move() in the demo and what I call hid_endpoint_eventCb() ). In it's place I just set a flag that tells the application when it is safe to write more data to the HID IN Endpoint.

So now the callback looks like:

static void hid_endpoint_event_Cb(usbd_device *dev, uint8_t event, uint8_t ep)
{
switch(event)
   {
   case usbd_evt_sof:                 // Not supported by core at this time.
        break;

   case usbd_evt_eptx:                 // Data packet transmit complete.
        if(ep == IN_EP1)               // In my case HID is on EP1 IN (there is no CDC)
           {
           if(bLoadEp1 == 0)
              bLoadEp1 = 1;            // Tell main code that we are ready for more.
           }
        break;

   default:
        break;
   }
}

And in the main code:

 if(bLoadEp1)
   {
   bLoadEp1 = 0;                    // Note that it has been sent.
   usbd_ep_write(&udev, HID_RPT_EP, &hid_report_data, EP1_PACKET_SIZE);
   }

Note that this section in the main code only runs if there is actual data to send. This way the Endpoint is NAKing any time there is nothing to send to the host.