Skip to content

Commit

Permalink
longan-nano: Add serial example
Browse files Browse the repository at this point in the history
  • Loading branch information
katyo committed Feb 4, 2023
1 parent aee96ac commit b8e8021
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
2 changes: 2 additions & 0 deletions example-longan-nano-board/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ edition = "2018"
[dependencies]
riscv = "0.6.0"
riscv-rt = "0.11.0"
embedded-hal = "0.2.7"
gd32vf103xx-hal = "0.5.0"
panic-halt = "0.2.0"
usb-device = "0.2.9"
usbd-serial = "0.1.1"
synopsys-usb-otg = { version = "0.3.2", features = ["riscv", "fs"] }
88 changes: 88 additions & 0 deletions example-longan-nano-board/examples/serial.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#![no_std]
#![no_main]

use panic_halt as _;

use gd32vf103xx_hal::pac;
use gd32vf103xx_hal::prelude::*;
use riscv_rt::entry;

use example_longan_nano_board::{UsbBus, USB};
use usb_device::prelude::*;
use usbd_serial::{SerialPort, USB_CLASS_CDC};

use embedded_hal::digital::v2::OutputPin;

#[entry]
fn main() -> ! {
let dp = pac::Peripherals::take().unwrap();

// Configure clocks
let mut rcu = dp
.RCU
.configure()
.ext_hf_clock(8.mhz())
.sysclk(96.mhz())
.freeze();

assert!(rcu.clocks.usbclk_valid());

let gpioc = dp.GPIOC.split(&mut rcu);
let mut led = gpioc.pc13.into_push_pull_output();
led.set_high().unwrap(); // Turn off

let gpioa = dp.GPIOA.split(&mut rcu);
let usb = USB {
usb_global: dp.USBFS_GLOBAL,
usb_device: dp.USBFS_DEVICE,
usb_pwrclk: dp.USBFS_PWRCLK,
pin_dm: gpioa.pa11,
pin_dp: gpioa.pa12,
hclk: rcu.clocks.hclk(),
};

static mut EP_MEMORY: [u32; 1024] = [0; 1024];
let usb_bus = UsbBus::new(usb, unsafe { &mut EP_MEMORY });

let mut serial = SerialPort::new(&usb_bus);
let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd))
.manufacturer("Fake company")
.product("Serial port")
.serial_number("TEST")
.device_class(USB_CLASS_CDC)
.build();

loop {
if !usb_dev.poll(&mut [&mut serial]) {
continue;
}

let mut buf = [0u8; 64];

match serial.read(&mut buf) {
Ok(count) if count > 0 => {
led.set_low().unwrap(); // Turn on

// Echo back in upper case
for c in buf[0..count].iter_mut() {
if 0x61 <= *c && *c <= 0x7a {
*c &= !0x20;
}
}

let mut write_offset = 0;
while write_offset < count {
match serial.write(&buf[write_offset..count]) {
Ok(len) if len > 0 => {
write_offset += len;
}
_ => {}
}
}
}
_ => {}
}

led.set_high().unwrap(); // Turn off
}
}

0 comments on commit b8e8021

Please sign in to comment.