Skip to content

Examples: Add USB CDC example (currently only works on DK board). #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions examples/if_usbcdc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2022 Micro:bit Educational Foundation and contributors
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mbed.h"
#include "USBSerial.h"
#include "USBCDC.h"

// WARNING! This example currently only works with the nRF52833 DK board!

/**
* USBSerial or USBCDC can both be used (not simultaneously) and they have
* the same constructor.
*/
// USBSerial usb_serial(
USBCDC usb_serial (
false, // connect_blocking
0x0d28, // vendor_id (test value from DAPLink)
0x0204, // product_id (test value from DAPLink)
0x0001 // product_release
);

int main(void) {
char msgUSB[] = "Hello from nRF52 USB CDC!\r\n";
DigitalOut led_orange(LED1, 0);
DigitalOut led_red(LED2, 0);

// Blink both LEDs to show the programme is running
ThisThread::sleep_for(250);
led_orange = 1;
led_red = 1;
ThisThread::sleep_for(250);
led_orange = 0;
led_red = 0;

// Block here and blink the red LED until the CDC port is connected
usb_serial.connect();
while (!usb_serial.ready()) {
ThisThread::sleep_for(250);
led_red = !led_red;
}
led_red = 1;

// Send a message via USB CDC and blink the orange LED
while (true) {
ThisThread::sleep_for(1000);
led_orange = !led_orange;
usb_serial.send((uint8_t *)msgUSB, strlen(msgUSB));
//usb_serial.printf(msgUSB); // printf only available with USBSerial constructor
}
}