Skip to content

Commit d23f02f

Browse files
Examples: Add USB CDC example (currently only works on DK board).
1 parent 3de3654 commit d23f02f

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

examples/if_usbcdc.cpp

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2022 Micro:bit Educational Foundation and contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
#include "mbed.h"
18+
#include "USBSerial.h"
19+
#include "USBCDC.h"
20+
21+
// WARNING! This example currently only works with the nRF52833 DK board!
22+
23+
/**
24+
* USBSerial or USBCDC can both be used (not simultaneously) and they have
25+
* the same constructor.
26+
*/
27+
// USBSerial usb_serial(
28+
USBCDC usb_serial (
29+
false, // connect_blocking
30+
0x0d28, // vendor_id (test value from DAPLink)
31+
0x0204, // product_id (test value from DAPLink)
32+
0x0001 // product_release
33+
);
34+
35+
int main(void) {
36+
char msgUSB[] = "Hello from nRF52 USB CDC!\r\n";
37+
DigitalOut led_orange(LED1, 0);
38+
DigitalOut led_red(LED2, 0);
39+
40+
// Blink both LEDs to show the programme is running
41+
ThisThread::sleep_for(250);
42+
led_orange = 1;
43+
led_red = 1;
44+
ThisThread::sleep_for(250);
45+
led_orange = 0;
46+
led_red = 0;
47+
48+
// Block here and blink the red LED until the CDC port is connected
49+
usb_serial.connect();
50+
while (!usb_serial.ready()) {
51+
ThisThread::sleep_for(250);
52+
led_red = !led_red;
53+
}
54+
led_red = 1;
55+
56+
// Send a message via USB CDC and blink the orange LED
57+
while (true) {
58+
ThisThread::sleep_for(1000);
59+
led_orange = !led_orange;
60+
usb_serial.send((uint8_t *)msgUSB, strlen(msgUSB));
61+
//usb_serial.printf(msgUSB); // printf only available with USBSerial constructor
62+
}
63+
}

0 commit comments

Comments
 (0)