From aee96acc9d11ef7d873d5016a9f59f6520fb676e Mon Sep 17 00:00:00 2001 From: K Date: Sat, 4 Feb 2023 19:01:52 +0500 Subject: [PATCH 1/2] longan-nano: Update dependencies --- example-longan-nano-board/Cargo.toml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/example-longan-nano-board/Cargo.toml b/example-longan-nano-board/Cargo.toml index 37c8492..2cda920 100644 --- a/example-longan-nano-board/Cargo.toml +++ b/example-longan-nano-board/Cargo.toml @@ -5,9 +5,9 @@ authors = ["Vadim Kaushan "] edition = "2018" [dependencies] -riscv = "0.5.4" -riscv-rt = "0.6.1" -gd32vf103xx-hal = "0.2.3" +riscv = "0.6.0" +riscv-rt = "0.11.0" +gd32vf103xx-hal = "0.5.0" panic-halt = "0.2.0" -usb-device = "0.2.5" -synopsys-usb-otg = { version = "0.2.1", features = ["riscv", "fs"] } +usb-device = "0.2.9" +synopsys-usb-otg = { version = "0.3.2", features = ["riscv", "fs"] } From b8e80215a0b2bd2b6028bc4ed9b763995bd0175d Mon Sep 17 00:00:00 2001 From: K Date: Sat, 4 Feb 2023 19:03:29 +0500 Subject: [PATCH 2/2] longan-nano: Add serial example --- example-longan-nano-board/Cargo.toml | 2 + example-longan-nano-board/examples/serial.rs | 88 ++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 example-longan-nano-board/examples/serial.rs diff --git a/example-longan-nano-board/Cargo.toml b/example-longan-nano-board/Cargo.toml index 2cda920..3e757db 100644 --- a/example-longan-nano-board/Cargo.toml +++ b/example-longan-nano-board/Cargo.toml @@ -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"] } diff --git a/example-longan-nano-board/examples/serial.rs b/example-longan-nano-board/examples/serial.rs new file mode 100644 index 0000000..3e869ee --- /dev/null +++ b/example-longan-nano-board/examples/serial.rs @@ -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 + } +}