Skip to content
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

Longan nano updates #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
12 changes: 7 additions & 5 deletions example-longan-nano-board/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ authors = ["Vadim Kaushan <admin@disasm.info>"]
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"
embedded-hal = "0.2.7"
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"
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
}
}