|
| 1 | +.. _usb_device_cdc_acm: |
| 2 | + |
| 3 | +USB device stack CDC ACM support |
| 4 | +################################ |
| 5 | + |
| 6 | +The CDC ACM class is used as backend for different subsystems in Zephyr. |
| 7 | +However, its configuration may not be easy for the inexperienced user. |
| 8 | +Below is a description of the different use cases and some pitfalls. |
| 9 | + |
| 10 | +The interface for CDC ACM user is :ref:`uart_api` driver API. |
| 11 | +But there are two important differences in behavior to a real UART controller: |
| 12 | + |
| 13 | +* Data transfer is only possible after the USB device stack has been initialized and started, |
| 14 | + until then any data is discarded |
| 15 | +* If device is connected to the host, it still needs an application |
| 16 | + on the host side which requests the data |
| 17 | + |
| 18 | +The devicetree compatible property for CDC ACM UART is |
| 19 | +:dtcompatible:`zephyr,cdc-acm-uart`. |
| 20 | +CDC ACM support is automatically selected when USB device support is enabled |
| 21 | +and a compatible node in the devicetree sources is present. If necessary, |
| 22 | +CDC ACM support can be explicitly disabled by :kconfig:`CONFIG_USB_CDC_ACM`. |
| 23 | +About four CDC ACM UART instances can be defined and used, |
| 24 | +limited by the maximum number of supported endpoints on the controller. |
| 25 | + |
| 26 | +CDC ACM UART node is supposed to be child of a USB device controller node. |
| 27 | +Since the designation of the controller nodes varies from vendor to vendor, |
| 28 | +and our samples and application should be as generic as possible, |
| 29 | +the default USB device controller is usually assigned an ``zephyr_udc0`` |
| 30 | +node label. Often, CDC ACM UART is described in a devicetree overlay file |
| 31 | +and looks like this: |
| 32 | + |
| 33 | +.. code-block:: devicetree |
| 34 | +
|
| 35 | + &zephyr_udc0 { |
| 36 | + cdc_acm_uart0: cdc_acm_uart0 { |
| 37 | + compatible = "zephyr,cdc-acm-uart"; |
| 38 | + label = "CDC_ACM_0"; |
| 39 | + }; |
| 40 | + }; |
| 41 | +
|
| 42 | +Samples :ref:`usb_cdc-acm` and :ref:`usb_hid-cdc` have similar overlay files. |
| 43 | +And since no special properties are present, it may seem overkill to use |
| 44 | +devicetree to describe CDC ACM UART. The motivation behind using devicetree |
| 45 | +is the easy interchangeability of a real UART controller and CDC ACM UART |
| 46 | +in applications. |
| 47 | + |
| 48 | +Console over CDC ACM UART |
| 49 | +************************* |
| 50 | + |
| 51 | +With the CDC ACM UART node from above and ``zephyr,console`` property of the |
| 52 | +chosen node, we can describe that CDC ACM UART is to be used with the console. |
| 53 | +A similar overlay file is used by :ref:`cdc-acm-console`. |
| 54 | +If USB device support is enabled in the application, as in the console sample, |
| 55 | +:kconfig:`CONFIG_USB_UART_CONSOLE` must be enabled, |
| 56 | +which does nothing but change the initialization time of the console driver. |
| 57 | + |
| 58 | +.. code-block:: devicetree |
| 59 | +
|
| 60 | + / { |
| 61 | + chosen { |
| 62 | + zephyr,console = &cdc_acm_uart0; |
| 63 | + }; |
| 64 | + }; |
| 65 | +
|
| 66 | + &zephyr_udc0 { |
| 67 | + cdc_acm_uart0: cdc_acm_uart0 { |
| 68 | + compatible = "zephyr,cdc-acm-uart"; |
| 69 | + label = "CDC_ACM_0"; |
| 70 | + }; |
| 71 | + }; |
| 72 | +
|
| 73 | +Before the application uses the console, it is recommended to wait for |
| 74 | +the DTR signal: |
| 75 | + |
| 76 | +.. code-block:: c |
| 77 | +
|
| 78 | + const struct device *dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_console)); |
| 79 | + uint32_t dtr = 0; |
| 80 | +
|
| 81 | + if (usb_enable(NULL)) { |
| 82 | + return; |
| 83 | + } |
| 84 | +
|
| 85 | + while (!dtr) { |
| 86 | + uart_line_ctrl_get(dev, UART_LINE_CTRL_DTR, &dtr); |
| 87 | + k_sleep(K_MSEC(100)); |
| 88 | + } |
| 89 | +
|
| 90 | + printk("nuqneH\n"); |
| 91 | +
|
| 92 | +CDC ACM UART as backend |
| 93 | +*********************** |
| 94 | + |
| 95 | +As for the console sample, it is possible to configure CDC ACM UART as |
| 96 | +backend for other subsystems by setting :ref:`devicetree-chosen-nodes` |
| 97 | +properties. |
| 98 | + |
| 99 | +List of few Zephyr specific chosen properties which can be used to select |
| 100 | +CDC ACM UART as backend for a subsystem or application: |
| 101 | + |
| 102 | +* ``zephyr,bt-c2h-uart`` used in Bluetooth, |
| 103 | + for example see :ref:`bluetooth-hci-uart-sample` |
| 104 | +* ``zephyr,ot-uart`` used in OpenThread, |
| 105 | + for example see :ref:`coprocessor-sample` |
| 106 | +* ``zephyr,shell-uart`` used by shell for serial backend, |
| 107 | + for example see :zephyr_file:`samples/subsys/shell/shell_module` |
| 108 | +* ``zephyr,uart-mcumgr`` used by :ref:`smp_svr_sample` |
0 commit comments