Page 1 of 1

usb.c ep0 buffer manipulation

Posted: Tue Apr 14, 2015 10:43 am
by JerryR
Hello.
I do not understand how initialized pointer ep0next and size ep0n the exchange via USB.
file usb.c line 682: usbPrepareReceive (usbp, 0, usbp-> ep0next, usbp-> ep0n);
Needs to receive data in a custom application HID.
user-defined function bool_t hidRequestsHook (USBDriver * usbp);
data from: case HID_SET_REPORT_REQUEST: ... return TRUE;
Thank you.

Re: usb.c ep0 buffer manipulation

Posted: Tue Apr 14, 2015 2:23 pm
by Giovanni
Hi,

I don't understand your question well.

About usbPrepareReceive():
param 1: USB driver pointer
param 2: the endpoint number
param 3: pointer to a buffer where to put data (you decide this)
param 4: number of bytes to receive

You don't need to use ep0n and ep0next, those are part of the state machine handling EP0.

Giovanni

Re: usb.c ep0 buffer manipulation

Posted: Wed Apr 15, 2015 4:03 am
by JerryR
your original code file usb.c line 682: usbPrepareReceive (usbp, 0, usbp-> ep0next, usbp-> ep0n);
use usbp-> ep0next pointer and usbp-> ep0n.
1) How you initialized pointer ep0next and size ep0n ( original inside Chibios in this part)?
2) There is a custom callback at the end of the actual data reception?
3) I had to edit usb.c to retrieve data. There is a way without editing?

Re: usb.c ep0 buffer manipulation

Posted: Wed Apr 15, 2015 6:08 pm
by Giovanni
Hi,

About 1 and 2:

Code: Select all

/**
 * @brief   Request transfer setup.
 * @details This macro is used by the request handling callbacks in order to
 *          prepare a transaction over the endpoint zero.
 *
 * @param[in] usbp      pointer to the @p USBDriver object
 * @param[in] buf       pointer to a buffer for the transaction data
 * @param[in] n         number of bytes to be transferred
 * @param[in] endcb     callback to be invoked after the transfer or @p NULL
 *
 * @api
 */
#define usbSetupTransfer(usbp, buf, n, endcb) {                             \
  (usbp)->ep0next  = (buf);                                                 \
  (usbp)->ep0n     = (n);                                                   \
  (usbp)->ep0endcb = (endcb);                                               \
}


About 3, you don't need to modify usb.c, you need to implement a custom message handler for the EP0. This is done using an hook callback.

Code: Select all

  /* First verify if the application has an handler installed for this
     request.*/
  if ((usbp->config->requests_hook_cb == NULL) ||
      !(usbp->config->requests_hook_cb(usbp))) {
    /* Invoking the default handler, if this fails then stalls the
       endpoint zero as error.*/
    if (((usbp->setup[0] & USB_RTYPE_TYPE_MASK) != USB_RTYPE_TYPE_STD) ||
        !default_handler(usbp)) {


First "requests" is invoked, you need to check in that handler if it is a message that you need to handle, if so then return "true", by returning "false" the default messages handler is called, it manages the normal EP0 messages.

Giovanni