usb.c ep0 buffer manipulation

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
JerryR
Posts: 9
Joined: Sun Apr 08, 2012 6:51 am

usb.c ep0 buffer manipulation

Post 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.
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: usb.c ep0 buffer manipulation

Post 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
JerryR
Posts: 9
Joined: Sun Apr 08, 2012 6:51 am

Re: usb.c ep0 buffer manipulation

Post 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?
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: usb.c ep0 buffer manipulation

Post 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
Post Reply