usb: samples: Application calling usb_enable by itself

User app is reponsible for issuing usb_enable and
making USB hardware operative.

Signed-off-by: Emil Obalski <emil.obalski@nordicsemi.no>
This commit is contained in:
Emil Obalski 2019-10-22 09:06:54 +02:00 committed by Carles Cufí
commit d65027d8c0
18 changed files with 142 additions and 13 deletions

View file

@ -33,6 +33,7 @@
#ifdef CONFIG_UART_CONSOLE_MCUMGR #ifdef CONFIG_UART_CONSOLE_MCUMGR
#include "mgmt/serial.h" #include "mgmt/serial.h"
#endif #endif
#include <usb/usb_device.h>
static struct device *uart_console_dev; static struct device *uart_console_dev;
@ -596,6 +597,13 @@ static int uart_console_init(struct device *arg)
uart_console_dev = device_get_binding(CONFIG_UART_CONSOLE_ON_DEV_NAME); uart_console_dev = device_get_binding(CONFIG_UART_CONSOLE_ON_DEV_NAME);
#if defined(CONFIG_USB_UART_CONSOLE) && defined(CONFIG_USB_UART_DTR_WAIT) #if defined(CONFIG_USB_UART_CONSOLE) && defined(CONFIG_USB_UART_DTR_WAIT)
int ret;
ret = usb_enable();
if (ret != 0) {
return ret;
}
while (1) { while (1) {
u32_t dtr = 0U; u32_t dtr = 0U;

View file

@ -217,12 +217,15 @@ int usb_set_config(const u8_t *usb_descriptor);
int usb_deconfig(void); int usb_deconfig(void);
/** /**
* @brief Enable USB for host/device connection * @brief Enable the USB subsystem and associated hardware
* *
* Function to enable USB for host/device connection. * This function initializes the USB core subsystem and enables the
* Upon success, the USB module is no longer clock gated in hardware, * corresponding hardware so that it can begin transmitting and receiving
* it is now capable of transmitting and receiving on the USB bus and * on the USB bus, as well as generating interrupts.
* of generating interrupts. *
* Class-specific initialization and registration must be performed by the user
* before invoking this, so that any data or events on the bus are processed
* correctly by the associated class handling code.
* *
* @return 0 on success, negative errno code on fail. * @return 0 on success, negative errno code on fail.
*/ */

View file

@ -6,8 +6,17 @@
#include <zephyr.h> #include <zephyr.h>
#include <sys/printk.h> #include <sys/printk.h>
#include <usb/usb_device.h>
void main(void) void main(void)
{ {
int ret;
ret = usb_enable();
if (ret != 0) {
printk("Failed to enable USB");
return;
}
printk("Bluetooth over USB sample\n"); printk("Bluetooth over USB sample\n");
} }

View file

@ -82,6 +82,8 @@ static struct device *hid_device;
int usb_transport_init(usb_transport_receive_callback_t callback) int usb_transport_init(usb_transport_receive_callback_t callback)
{ {
int ret;
hid_device = device_get_binding("HID_0"); hid_device = device_get_binding("HID_0");
if (hid_device == NULL) { if (hid_device == NULL) {
@ -96,6 +98,12 @@ int usb_transport_init(usb_transport_receive_callback_t callback)
receive_data_cb = callback; receive_data_cb = callback;
ret = usb_enable();
if (ret != 0) {
LOG_ERR("Failed to enable USB");
return;
}
/* initialize USB interface and HID class */ /* initialize USB interface and HID class */
return usb_hid_init(hid_device); return usb_hid_init(hid_device);
} }

View file

@ -401,6 +401,7 @@ out:
void main(void) void main(void)
{ {
int ret;
LOG_INF("Starting wpanusb"); LOG_INF("Starting wpanusb");
ieee802154_dev = device_get_binding(CONFIG_NET_CONFIG_IEEE802154_DEV_NAME); ieee802154_dev = device_get_binding(CONFIG_NET_CONFIG_IEEE802154_DEV_NAME);
@ -417,6 +418,11 @@ void main(void)
radio_api = (struct ieee802154_radio_api *)ieee802154_dev->driver_api; radio_api = (struct ieee802154_radio_api *)ieee802154_dev->driver_api;
ret = usb_enable(NULL);
if (ret != 0) {
LOG_ERR("Failed to enable USB");
return;
}
/* TODO: Initialize more */ /* TODO: Initialize more */
LOG_DBG("radio_api %p initialized", radio_api); LOG_DBG("radio_api %p initialized", radio_api);

View file

@ -184,6 +184,7 @@ static void trigger_handler(struct device *dev, struct sensor_trigger *tr)
void main(void) void main(void)
{ {
int ret;
u8_t report[4] = { 0x00 }; u8_t report[4] = { 0x00 };
u8_t toggle = 0U; u8_t toggle = 0U;
struct device *led_dev, *accel_dev, *hid_dev; struct device *led_dev, *accel_dev, *hid_dev;
@ -245,6 +246,13 @@ void main(void)
usb_hid_register_device(hid_dev, hid_report_desc, usb_hid_register_device(hid_dev, hid_report_desc,
sizeof(hid_report_desc), NULL); sizeof(hid_report_desc), NULL);
ret = usb_enable();
if (ret != 0) {
LOG_ERR("Failed to enable USB");
return;
}
usb_hid_init(hid_dev); usb_hid_init(hid_dev);
while (true) { while (true) {

View file

@ -19,6 +19,7 @@
#include <zephyr.h> #include <zephyr.h>
#include <sys/ring_buffer.h> #include <sys/ring_buffer.h>
#include <usb/usb_device.h>
#include <logging/log.h> #include <logging/log.h>
LOG_MODULE_REGISTER(cdc_acm_echo, LOG_LEVEL_INF); LOG_MODULE_REGISTER(cdc_acm_echo, LOG_LEVEL_INF);
@ -81,6 +82,12 @@ void main(void)
return; return;
} }
ret = usb_enable();
if (ret != 0) {
LOG_ERR("Failed to enable USB");
return;
}
ring_buf_init(&ringbuf, sizeof(ring_buffer), ring_buffer); ring_buf_init(&ringbuf, sizeof(ring_buffer), ring_buffer);
LOG_INF("Wait for DTR"); LOG_INF("Wait for DTR");

View file

@ -19,6 +19,7 @@
#include <zephyr.h> #include <zephyr.h>
#include <sys/ring_buffer.h> #include <sys/ring_buffer.h>
#include <usb/usb_device.h>
#include <logging/log.h> #include <logging/log.h>
LOG_MODULE_REGISTER(cdc_acm_composite, LOG_LEVEL_INF); LOG_MODULE_REGISTER(cdc_acm_composite, LOG_LEVEL_INF);
@ -111,6 +112,8 @@ static void uart_line_set(struct device *dev)
void main(void) void main(void)
{ {
int ret;
struct serial_data *dev_data0 = &peers[0]; struct serial_data *dev_data0 = &peers[0];
struct serial_data *dev_data1 = &peers[1]; struct serial_data *dev_data1 = &peers[1];
struct device *dev0, *dev1; struct device *dev0, *dev1;
@ -128,6 +131,12 @@ void main(void)
return; return;
} }
ret = usb_enable();
if (ret != 0) {
LOG_ERR("Failed to enable USB");
return;
}
LOG_INF("Wait for DTR"); LOG_INF("Wait for DTR");
while (1) { while (1) {

View file

@ -9,12 +9,18 @@
#include <zephyr.h> #include <zephyr.h>
#include <logging/log.h> #include <logging/log.h>
#include <usb/usb_device.h>
LOG_MODULE_REGISTER(main); LOG_MODULE_REGISTER(main);
void main(void) void main(void)
{ {
/* Nothing to be done other than the selecting appropriate build int ret;
* config options. Use dfu-util to update the device.
*/ ret = usb_enable();
if (ret != 0) {
LOG_ERR("Failed to enable USB");
return;
}
LOG_INF("This device supports USB DFU class.\n"); LOG_INF("This device supports USB DFU class.\n");
} }

View file

@ -553,6 +553,8 @@ int callbacks_configure(struct device *gpio, u32_t pin, int flags,
void main(void) void main(void)
{ {
int ret;
struct device *hid0_dev, *hid1_dev, *cdc0_dev, *cdc1_dev; struct device *hid0_dev, *hid1_dev, *cdc0_dev, *cdc1_dev;
u32_t dtr = 0U; u32_t dtr = 0U;
struct app_evt_t *ev; struct app_evt_t *ev;
@ -620,9 +622,16 @@ void main(void)
usb_hid_register_device(hid1_dev, hid_kbd_report_desc, usb_hid_register_device(hid1_dev, hid_kbd_report_desc,
sizeof(hid_kbd_report_desc), &ops); sizeof(hid_kbd_report_desc), &ops);
usb_hid_init(hid0_dev); usb_hid_init(hid0_dev);
usb_hid_init(hid1_dev); usb_hid_init(hid1_dev);
ret = usb_enable();
if (ret != 0) {
LOG_ERR("Failed to enable USB");
return;
}
/* Initialize CDC ACM */ /* Initialize CDC ACM */
LOG_INF("Wait for DTR on CDC ACM 0"); LOG_INF("Wait for DTR on CDC ACM 0");

View file

@ -213,6 +213,8 @@ int callbacks_configure(struct device *gpio, u32_t pin, int flags,
void main(void) void main(void)
{ {
int ret;
u8_t report[4] = { 0x00 }; u8_t report[4] = { 0x00 };
u8_t toggle = 0U; u8_t toggle = 0U;
struct device *led_dev, *hid_dev; struct device *led_dev, *hid_dev;
@ -267,8 +269,15 @@ void main(void)
usb_hid_register_device(hid_dev, usb_hid_register_device(hid_dev,
hid_report_desc, sizeof(hid_report_desc), hid_report_desc, sizeof(hid_report_desc),
&ops); &ops);
usb_hid_init(hid_dev); usb_hid_init(hid_dev);
ret = usb_enable();
if (ret != 0) {
LOG_ERR("Failed to enable USB");
return;
}
while (true) { while (true) {
k_sem_take(&sem, K_FOREVER); k_sem_take(&sem, K_FOREVER);

View file

@ -116,8 +116,16 @@ static const struct hid_ops ops = {
void main(void) void main(void)
{ {
int ret;
LOG_DBG("Starting application"); LOG_DBG("Starting application");
ret = usb_enable();
if (ret != 0) {
LOG_ERR("Failed to enable USB");
return;
}
k_delayed_work_init(&delayed_report_send, send_report); k_delayed_work_init(&delayed_report_send, send_report);
} }

View file

@ -9,6 +9,7 @@
#include <zephyr.h> #include <zephyr.h>
#include <logging/log.h> #include <logging/log.h>
#include <usb/usb_device.h>
LOG_MODULE_REGISTER(main); LOG_MODULE_REGISTER(main);
#if CONFIG_DISK_ACCESS_FLASH && CONFIG_FAT_FILESYSTEM_ELM #if CONFIG_DISK_ACCESS_FLASH && CONFIG_FAT_FILESYSTEM_ELM
@ -27,9 +28,14 @@ static struct fs_mount_t fatfs_mnt = {
void main(void) void main(void)
{ {
/* Nothing to be done other than the selecting appropriate build int ret;
* config options. Everything is driven from the USB host side.
*/ ret = usb_enable();
if (ret != 0) {
LOG_ERR("Failed to enable USB");
return;
}
LOG_INF("The device is put in USB mass storage mode.\n"); LOG_INF("The device is put in USB mass storage mode.\n");
#if CONFIG_DISK_ACCESS_FLASH && CONFIG_FAT_FILESYSTEM_ELM #if CONFIG_DISK_ACCESS_FLASH && CONFIG_FAT_FILESYSTEM_ELM

View file

@ -6,9 +6,19 @@
#include <zephyr.h> #include <zephyr.h>
#include <logging/log.h> #include <logging/log.h>
#include <usb/usb_device.h>
LOG_MODULE_REGISTER(main); LOG_MODULE_REGISTER(main);
void main(void) void main(void)
{ {
int ret;
ret = usb_enable();
if (ret != 0) {
LOG_ERR("Failed to enable USB");
return;
}
LOG_INF("entered main."); LOG_INF("entered main.");
} }

View file

@ -283,11 +283,20 @@ static struct webusb_req_handlers req_handlers = {
void main(void) void main(void)
{ {
int ret;
LOG_DBG(""); LOG_DBG("");
/* Set the custom and vendor request handlers */ /* Set the custom and vendor request handlers */
webusb_register_request_handlers(&req_handlers); webusb_register_request_handlers(&req_handlers);
ret = usb_enable();
if (ret != 0) {
LOG_ERR("Failed to enable USB");
return;
}
usb_bos_register_cap((void *)&bos_cap_webusb); usb_bos_register_cap((void *)&bos_cap_webusb);
usb_bos_register_cap((void *)&bos_cap_msosv2); usb_bos_register_cap((void *)&bos_cap_msosv2);
} }

View file

@ -125,10 +125,18 @@ bool netusb_enabled(void)
static void netusb_init(struct net_if *iface) static void netusb_init(struct net_if *iface)
{ {
int ret;
static u8_t mac[6] = { 0x00, 0x00, 0x5E, 0x00, 0x53, 0x00 }; static u8_t mac[6] = { 0x00, 0x00, 0x5E, 0x00, 0x53, 0x00 };
LOG_DBG("netusb device initialization"); LOG_DBG("netusb device initialization");
ret = usb_enable();
if (ret != 0) {
LOG_ERR("Failed to enable USB");
return;
}
netusb.iface = iface; netusb.iface = iface;
ethernet_init(iface); ethernet_init(iface);

View file

@ -1645,8 +1645,6 @@ static int usb_device_init(struct device *dev)
usb_set_config(device_descriptor); usb_set_config(device_descriptor);
usb_enable();
return 0; return 0;
} }

View file

@ -190,6 +190,14 @@ static void test_usb_dc_api_read_write(void)
/*test case main entry*/ /*test case main entry*/
void test_main(void) void test_main(void)
{ {
int ret;
ret = usb_enable(NULL);
if (ret != 0) {
printk("Failed to enable USB\n");
return;
}
ztest_test_suite(test_device, ztest_test_suite(test_device,
/* Test API for not USB attached state */ /* Test API for not USB attached state */
ztest_unit_test(test_usb_dc_api_invalid), ztest_unit_test(test_usb_dc_api_invalid),