1.. zephyr:code-sample:: usb-hid-cdc
2   :name: USB HID and CDC ACM
3   :relevant-api: _usb_device_core_api usb_hid_class input_interface
4
5   Expose multiple USB HID and CDC ACM instances.
6
7Overview
8********
9
10This sample app demonstrates use of multiple USB classes with multiple
11instances. It combines two HID instances and two CDC ACM instances.
12This sample can be found under :zephyr_file:`samples/subsys/usb/hid-cdc` in the
13Zephyr project tree.
14
15Requirements
16************
17
18This project requires an USB device driver and multiple endpoints.
19
20The board hardware must have a push button connected via a GPIO pin. These are
21called "User buttons" on many of Zephyr's :ref:`boards`.
22
23The button must be configured using the ``sw0`` :ref:`devicetree <dt-guide>`
24alias, usually in the :ref:`BOARD.dts file <devicetree-in-out-files>`. You will
25see this error if you try to build this sample for an unsupported board:
26
27.. code-block:: none
28
29   Unsupported board: sw0 devicetree alias is not defined
30
31You may see additional build errors if the ``sw0`` alias exists, but is not
32properly defined.
33
34If the devicetree aliases ``sw1``, ``sw2``, and ``sw3`` are defined, they will
35also be used as additional buttons as described below.
36
37Building and Running
38********************
39
40This sample can be built for multiple boards. To build and flash it
41for the :ref:`nrf52840dk_nrf52840` board:
42
43.. zephyr-app-commands::
44	:zephyr-app: samples/subsys/usb/hid-cdc
45	:board: nrf52840dk_nrf52840
46	:goals: build flash
47	:compact:
48
49After you have built and flashed the sample app image to your board, plug the
50board into a host device, for example, a PC running Linux.
51Two CDC ACM interfaces (for example /dev/ttyACM1 and /dev/ttyACM2)
52and two HID devices will be detected:
53
54.. code-block:: console
55
56	usb 2-2: new full-speed USB device number 3 using ohci-pci
57	usb 2-2: New USB device found, idVendor=2fe3, idProduct=0003, bcdDevice= 2.03
58	usb 2-2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
59	usb 2-2: Product: Zephyr HID and CDC ACM sample
60	usb 2-2: Manufacturer: ZEPHYR
61	usb 2-2: SerialNumber: 86FE679A598AC47A
62	cdc_acm 2-2:1.0: ttyACM1: USB ACM device
63	input: ZEPHYR Zephyr HID and CDC ACM sample as /devices/pci0000:00/0000:00:06.0/usb2/2-2/2-2:1.2/0003:2FE3:0100.0002/input/input8
64	hid-generic 0003:2FE3:0100.0002: input,hidraw1: USB HID v1.10 Mouse [ZEPHYR Zephyr HID and CDC ACM sample] on usb-0000:00:06.0-2/input2
65	cdc_acm 2-2:1.3: ttyACM2: USB ACM device
66	input: ZEPHYR Zephyr HID and CDC ACM sample as /devices/pci0000:00/0000:00:06.0/usb2/2-2/2-2:1.5/0003:2FE3:0100.0003/input/input9
67	hid-generic 0003:2FE3:0100.0003: input,hidraw2: USB HID v1.10 Keyboard [ZEPHYR Zephyr HID and CDC ACM sample] on usb-0000:00:06.0-2/input5
68
69You can now connect to both CDC ACM ports:
70
71.. code-block:: console
72
73	minicom -D /dev/ttyACM1 -b 115200
74
75.. code-block:: console
76
77	minicom -D /dev/ttyACM2 -b 115200
78
79After both ports have been connected to, messages explaining usage of each port will be displayed:
80
81.. code-block:: console
82
83	Welcome to CDC_ACM_0
84	Supported commands:
85	up    - moves the mouse up
86	down  - moves the mouse down
87	right - moves the mouse to right
88	left  - moves the mouse to left
89
90.. code-block:: console
91
92	Welcome to CDC_ACM_1
93	Enter a string and terminate it with ENTER.
94	It will be sent via HID when BUTTON 2 is pressed.
95	You can modify it by sending a new one here.
96
97CDC ACM 0 may be used to control the mouse by typing a command and pressing :kbd:`ENTER`.
98
99CDC ACM 1 is used to control the keyboard - any string typed into it and finished with :kbd:`ENTER` will be saved
100on the device and typed back to the host when BUTTON 2 is pressed.
101
102Buttons have following functions:
103
104- Button 0 moves HID mouse in random direction
105- Button 1 is a left HID mouse button
106- Button 2 types the string sent with CDC ACM 1 using HID keyboard
107- Button 3 is a CAPS LOCK on HID keyboard
108