Skip to content
Permalink
Browse files

samples: usb: wpanusb: Refactor sample using new API

Refactor wpanusb sample using new descriptor API, use endpoint index
instead of hardcoded endpoint address.

Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
  • Loading branch information...
finikorg authored and nashif committed May 7, 2019
1 parent 1b64e53 commit 953728b241ec1d850cd8dd94bcfb390dc479f9f6
Showing with 83 additions and 142 deletions.
  1. +1 −0 samples/net/wpanusb/CMakeLists.txt
  2. +82 −142 samples/net/wpanusb/src/wpanusb.c
@@ -6,6 +6,7 @@ project(wpanusb)

target_include_directories(app PRIVATE $ENV{ZEPHYR_BASE}/subsys/net/ip)
target_include_directories(app PRIVATE $ENV{ZEPHYR_BASE}/subsys/net/l2)
target_include_directories(app PRIVATE $ENV{ZEPHYR_BASE}/subsys/usb)

FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})
@@ -10,6 +10,7 @@ LOG_MODULE_REGISTER(wpanusb);

#include <usb/usb_device.h>
#include <usb/usb_common.h>
#include <usb_descriptor.h>

#include <net/buf.h>
#include <net/ieee802154_radio.h>
@@ -24,7 +25,7 @@ LOG_MODULE_REGISTER(wpanusb);
/* Max packet size for endpoints */
#define WPANUSB_BULK_EP_MPS 64

#define WPANUSB_ENDP_BULK_IN 0x81
#define WPANUSB_IN_EP_IDX 0

static struct ieee802154_radio_api *radio_api;
static struct device *ieee802154_dev;
@@ -40,80 +41,43 @@ u8_t tx_buf[IEEE802154_MTU + 1 + 1];
static K_THREAD_STACK_DEFINE(tx_stack, 1024);
static struct k_thread tx_thread_data;

static const struct dev_common_descriptor {
struct usb_device_descriptor device_descriptor;
struct usb_cfg_descriptor configuration_descr;
struct usb_device_config {
struct usb_if_descriptor if0;
struct usb_ep_descriptor if0_in_ep;
} __packed device_configuration;
/*
* String descriptors not enabled at the moment
*/
} __packed wpanusb_desc = {
/* Device descriptor */
.device_descriptor = {
.bLength = sizeof(struct usb_device_descriptor),
.bDescriptorType = USB_DEVICE_DESC,
.bcdUSB = sys_cpu_to_le16(USB_1_1),
.bDeviceClass = CUSTOM_CLASS,
.bDeviceSubClass = 0,
.bDeviceProtocol = 0,
.bMaxPacketSize0 = USB_MAX_CTRL_MPS,
.idVendor = sys_cpu_to_le16((u16_t)CONFIG_USB_DEVICE_VID),
.idProduct = sys_cpu_to_le16((u16_t)CONFIG_USB_DEVICE_PID),
.bcdDevice = sys_cpu_to_le16(BCDDEVICE_RELNUM),
.iManufacturer = 0,
.iProduct = 0,
.iSerialNumber = 0,
.bNumConfigurations = 1,
},
#define INITIALIZER_IF(num_ep, iface_class) \
{ \
.bLength = sizeof(struct usb_if_descriptor), \
.bDescriptorType = USB_INTERFACE_DESC, \
.bInterfaceNumber = 0, \
.bAlternateSetting = 0, \
.bNumEndpoints = num_ep, \
.bInterfaceClass = iface_class, \
.bInterfaceSubClass = 0, \
.bInterfaceProtocol = 0, \
.iInterface = 0, \
}

/* Configuration descriptor */
.configuration_descr = {
.bLength = sizeof(struct usb_cfg_descriptor),
.bDescriptorType = USB_CONFIGURATION_DESC,
.wTotalLength = sizeof(struct dev_common_descriptor)
- sizeof(struct usb_device_descriptor),
.bNumInterfaces = 1,
.bConfigurationValue = 1,
.iConfiguration = 0,
.bmAttributes = USB_CONFIGURATION_ATTRIBUTES,
.bMaxPower = MAX_LOW_POWER,
},
#define INITIALIZER_IF_EP(addr, attr, mps, interval) \
{ \
.bLength = sizeof(struct usb_ep_descriptor), \
.bDescriptorType = USB_ENDPOINT_DESC, \
.bEndpointAddress = addr, \
.bmAttributes = attr, \
.wMaxPacketSize = sys_cpu_to_le16(mps), \
.bInterval = interval, \
}

/* Device configuration */
.device_configuration = {
/* Interface descriptor */
.if0 = {
.bLength = sizeof(struct usb_if_descriptor),
.bDescriptorType = USB_INTERFACE_DESC,
.bInterfaceNumber = 0,
.bAlternateSetting = 0,
.bNumEndpoints = 1,
.bInterfaceClass = CUSTOM_CLASS,
.bInterfaceSubClass = WPANUSB_SUBCLASS,
.bInterfaceProtocol = WPANUSB_PROTOCOL,
.iInterface = 0,
},

/* Endpoint IN */
.if0_in_ep = {
.bLength = sizeof(struct usb_ep_descriptor),
.bDescriptorType = USB_ENDPOINT_DESC,
.bEndpointAddress = WPANUSB_ENDP_BULK_IN,
.bmAttributes = USB_DC_EP_BULK,
.wMaxPacketSize = sys_cpu_to_le16(WPANUSB_BULK_EP_MPS),
.bInterval = 0x00,
},
},
USBD_CLASS_DESCR_DEFINE(primary, 0) struct {
struct usb_if_descriptor if0;
struct usb_ep_descriptor if0_in_ep;
} __packed wpanusb_desc = {
.if0 = INITIALIZER_IF(1, CUSTOM_CLASS),
.if0_in_ep = INITIALIZER_IF_EP(AUTO_EP_IN, USB_DC_EP_BULK,
WPANUSB_BULK_EP_MPS, 0),
};

/* Describe EndPoints configuration */
static struct usb_ep_cfg_data wpanusb_ep[] = {
{
.ep_cb = usb_transfer_ep_callback,
.ep_addr = WPANUSB_ENDP_BULK_IN
.ep_addr = AUTO_EP_IN,
},
};

@@ -154,6 +118,51 @@ static void wpanusb_status_cb(struct usb_cfg_data *cfg,
}
}

/**
* Vendor handler is executed in the ISR context, queue data for
* later processing
*/
static int wpanusb_vendor_handler(struct usb_setup_packet *setup,
s32_t *len, u8_t **data)
{
struct net_pkt *pkt;

/* Maximum 2 bytes are added to the len */
pkt = net_pkt_alloc_with_buffer(NULL, *len + 2, AF_UNSPEC, 0,
K_NO_WAIT);
if (!pkt) {
return -ENOMEM;
}

net_pkt_write_u8(pkt, setup->bRequest);

/* Add seq to TX */
if (setup->bRequest == TX) {
net_pkt_write_u8(pkt, setup->wIndex);
}

net_pkt_write(pkt, *data, *len);

LOG_DBG("pkt %p len %u seq %u", pkt, *len, setup->wIndex);

k_fifo_put(&tx_queue, pkt);

return 0;
}

USBD_CFG_DATA_DEFINE(wpanusb) struct usb_cfg_data wpanusb_config = {
.usb_device_description = NULL,
.interface_descriptor = &wpanusb_desc.if0,
.cb_usb_status = wpanusb_status_cb,
.interface = {
.vendor_handler = wpanusb_vendor_handler,
.class_handler = NULL,
.custom_handler = NULL,
},
.num_endpoints = ARRAY_SIZE(wpanusb_ep),
.endpoint = wpanusb_ep,
};

/* Decode wpanusb commands */

static int set_channel(void *data, int len)
@@ -242,6 +251,7 @@ static int stop(void)

static int tx(struct net_pkt *pkt)
{
u8_t ep = wpanusb_config.endpoint[WPANUSB_IN_EP_IDX].ep_addr;
struct net_buf *buf = net_buf_frag_last(pkt->buffer);
u8_t seq = net_buf_pull_u8(buf);
int retries = 3;
@@ -259,8 +269,7 @@ static int tx(struct net_pkt *pkt)
seq = 0U;
}

ret = usb_transfer_sync(WPANUSB_ENDP_BULK_IN, &seq, sizeof(seq),
USB_TRANS_WRITE);
ret = usb_transfer_sync(ep, &seq, sizeof(seq), USB_TRANS_WRITE);
if (ret != sizeof(seq)) {
LOG_ERR("Error sending seq");
ret = -EINVAL;
@@ -271,38 +280,6 @@ static int tx(struct net_pkt *pkt)
return ret;
}

/**
* Vendor handler is executed in the ISR context, queue data for
* later processing
*/
static int wpanusb_vendor_handler(struct usb_setup_packet *setup,
s32_t *len, u8_t **data)
{
struct net_pkt *pkt;

/* Maximum 2 bytes are added to the len */
pkt = net_pkt_alloc_with_buffer(NULL, *len + 2, AF_UNSPEC, 0,
K_NO_WAIT);
if (!pkt) {
return -ENOMEM;
}

net_pkt_write_u8(pkt, setup->bRequest);

/* Add seq to TX */
if (setup->bRequest == TX) {
net_pkt_write_u8(pkt, setup->wIndex);
}

net_pkt_write(pkt, *data, *len);

LOG_DBG("pkt %p len %u seq %u", pkt, *len, setup->wIndex);

k_fifo_put(&tx_queue, pkt);

return 0;
}

static void tx_thread(void)
{
LOG_DBG("Tx thread started");
@@ -354,41 +331,6 @@ static void tx_thread(void)
}
}

static struct usb_cfg_data wpanusb_config = {
.usb_device_description = (u8_t *)&wpanusb_desc,
.cb_usb_status = wpanusb_status_cb,
.interface = {
.vendor_handler = wpanusb_vendor_handler,
.class_handler = NULL,
.custom_handler = NULL,
},
.num_endpoints = ARRAY_SIZE(wpanusb_ep),
.endpoint = wpanusb_ep,
};

static int wpanusb_init(void)
{
int ret;

LOG_DBG("");

/* Initialize the USB driver with the right configuration */
ret = usb_set_config(&wpanusb_config);
if (ret < 0) {
LOG_ERR("Failed to configure USB");
return ret;
}

/* Enable USB driver */
ret = usb_enable(&wpanusb_config);
if (ret < 0) {
LOG_ERR("Failed to enable USB");
return ret;
}

return 0;
}

static void init_tx_queue(void)
{
/* Transmit queue init */
@@ -409,6 +351,7 @@ int net_recv_data(struct net_if *iface, struct net_pkt *pkt)
size_t len = net_pkt_get_len(pkt);
u8_t *p = tx_buf;
int ret;
u8_t ep;

LOG_DBG("Got data, pkt %p, len %d", pkt, len);

@@ -441,7 +384,9 @@ int net_recv_data(struct net_if *iface, struct net_pkt *pkt)
*/
*p = net_pkt_ieee802154_lqi(pkt);

ret = usb_transfer_sync(WPANUSB_ENDP_BULK_IN, tx_buf, len + 2,
ep = wpanusb_config.endpoint[WPANUSB_IN_EP_IDX].ep_addr;

ret = usb_transfer_sync(ep, tx_buf, len + 2,
USB_TRANS_WRITE | USB_TRANS_NO_ZLP);
if (ret != len + 2) {
LOG_ERR("Transfer failure");
@@ -458,11 +403,6 @@ void main(void)
{
LOG_INF("Starting wpanusb");

if (wpanusb_init() < 0) {
LOG_ERR("Initialization failed");
return;
}

ieee802154_dev = device_get_binding(CONFIG_NET_CONFIG_IEEE802154_DEV_NAME);
if (!ieee802154_dev) {
LOG_ERR("Cannot get IEEE802.15.4 device");

0 comments on commit 953728b

Please sign in to comment.
You can’t perform that action at this time.