Skip to content

Commit

Permalink
samples: add ble hci_usb sample.
Browse files Browse the repository at this point in the history
Signed-off-by: Zelin Cai <[email protected]>
  • Loading branch information
caizelin committed Apr 22, 2024
1 parent 49bfbe4 commit 2a88780
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 0 deletions.
7 changes: 7 additions & 0 deletions samples/ble/hci_usb/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(hci_usb)

target_sources(app PRIVATE src/main.c)
17 changes: 17 additions & 0 deletions samples/ble/hci_usb/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CONFIG_BT=y
CONFIG_BT_HCI_RAW=y

CONFIG_USB_DEVICE_STACK=y
CONFIG_USB_DEVICE_PID=0x000B
CONFIG_USB_DEVICE_BLUETOOTH=y
CONFIG_USB_DEVICE_BLUETOOTH_VS_H4=n
CONFIG_USB_DEVICE_INITIALIZE_AT_BOOT=n

# We dont want any console or CDC ACM that may cause BlueZ to not detect hci_usb
CONFIG_SERIAL=n
CONFIG_CONSOLE=n
CONFIG_UART_CONSOLE=n

# Workaround: Unable to allocate command buffer when using K_NO_WAIT since
# Host number of completed commands does not follow normal flow control.
CONFIG_BT_BUF_CMD_TX_COUNT=10
11 changes: 11 additions & 0 deletions samples/ble/hci_usb/sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
sample:
name: Bluetooth over USB sample
common:
build_only: true
integration_platforms:
- connectkit_nrf52840
- dongle_nrf52840
tests:
sample.bluetooth.hci_usb: {}
sample.bluetooth.hci_usb.device_next:
extra_args: CONF_FILE="usbd_next_prj.conf"
92 changes: 92 additions & 0 deletions samples/ble/hci_usb/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright (c) 2018 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/kernel.h>
#include <zephyr/sys/printk.h>
#include <zephyr/usb/usb_device.h>
#include <zephyr/usb/usbd.h>

#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
USBD_CONFIGURATION_DEFINE(config_1,
USB_SCD_SELF_POWERED,
200);

USBD_DESC_LANG_DEFINE(sample_lang);
USBD_DESC_MANUFACTURER_DEFINE(sample_mfr, "ZEPHYR");
USBD_DESC_PRODUCT_DEFINE(sample_product, "Zephyr USBD BT HCI");
USBD_DESC_SERIAL_NUMBER_DEFINE(sample_sn, "0123456789ABCDEF");


USBD_DEVICE_DEFINE(sample_usbd,
DEVICE_DT_GET(DT_NODELABEL(zephyr_udc0)),
0x2fe3, 0x000b);

static int enable_usb_device_next(void)
{
int err;

err = usbd_add_descriptor(&sample_usbd, &sample_lang);
if (err) {
return err;
}

err = usbd_add_descriptor(&sample_usbd, &sample_mfr);
if (err) {
return err;
}

err = usbd_add_descriptor(&sample_usbd, &sample_product);
if (err) {
return err;
}

err = usbd_add_descriptor(&sample_usbd, &sample_sn);
if (err) {
return err;
}

err = usbd_add_configuration(&sample_usbd, &config_1);
if (err) {
return err;
}

err = usbd_register_class(&sample_usbd, "bt_hci_0", 1);
if (err) {
return err;
}

err = usbd_init(&sample_usbd);
if (err) {
return err;
}

err = usbd_enable(&sample_usbd);
if (err) {
return err;
}

return 0;
}
#endif /* CONFIG_USB_DEVICE_STACK_NEXT */

int main(void)
{
int ret;

#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
ret = enable_usb_device_next();
#else
ret = usb_enable(NULL);
#endif

if (ret != 0) {
printk("Failed to enable USB");
return 0;
}

printk("Bluetooth over USB sample\n");
return 0;
}
15 changes: 15 additions & 0 deletions samples/ble/hci_usb/usbd_next_prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CONFIG_STDOUT_CONSOLE=y
CONFIG_GPIO=y
CONFIG_SERIAL=y
CONFIG_UART_INTERRUPT_DRIVEN=y
CONFIG_USB_DEVICE_INITIALIZE_AT_BOOT=n

CONFIG_BT=y
CONFIG_BT_HCI_RAW=y

CONFIG_USB_DEVICE_STACK_NEXT=y
CONFIG_USBD_BT_HCI=y

CONFIG_LOG=y
CONFIG_USBD_LOG_LEVEL_WRN=y
CONFIG_UDC_DRIVER_LOG_LEVEL_WRN=y

0 comments on commit 2a88780

Please sign in to comment.