Created
February 28, 2020 02:05
-
-
Save thalesfragoso/e46b68f4d1687713a943ef5d90317b24 to your computer and use it in GitHub Desktop.
PoolSerial Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//! CDC-ACM serial port example using polling in a busy loop. | |
#![no_std] | |
#![no_main] | |
use core::panic::PanicInfo; | |
use core::sync::atomic::{self, Ordering}; | |
use cortex_m::asm::delay; | |
use cortex_m_rt::entry; | |
use embedded_hal::digital::v2::OutputPin; | |
use stm32f1xx_hal::usb::{Peripheral, UsbBus}; | |
use stm32f1xx_hal::{pac, prelude::*}; | |
use usb_device::prelude::*; | |
use usbd_serial::{ | |
heapless::{pool, Pool}, | |
typenum::consts::*, | |
PoolNode, PoolPort, USB_CLASS_CDC, | |
}; | |
const NR_NODES: usize = 10; | |
const POOL_MEM: usize = core::mem::size_of::<PoolNode>() * NR_NODES; | |
pool!( | |
#[allow(non_upper_case_globals)] | |
SerialPool: PoolNode | |
); | |
#[entry] | |
fn main() -> ! { | |
static mut MEMORY: [u8; POOL_MEM] = [0; POOL_MEM]; | |
let dp = pac::Peripherals::take().unwrap(); | |
let mut flash = dp.FLASH.constrain(); | |
let mut rcc = dp.RCC.constrain(); | |
let clocks = rcc | |
.cfgr | |
.use_hse(8.mhz()) | |
.sysclk(48.mhz()) | |
.pclk1(24.mhz()) | |
.freeze(&mut flash.acr); | |
assert!(clocks.usbclk_valid()); | |
// Configure the on-board LED (PC13, green) | |
let mut gpioc = dp.GPIOC.split(&mut rcc.apb2); | |
let mut _led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh); | |
let mut gpioa = dp.GPIOA.split(&mut rcc.apb2); | |
// BluePill board has a pull-up resistor on the D+ line. | |
// Pull the D+ pin down to send a RESET condition to the USB bus. | |
// This forced reset is needed only for development, without it host | |
// will not reset your device when you upload new firmware. | |
let mut usb_dp = gpioa.pa12.into_push_pull_output(&mut gpioa.crh); | |
usb_dp.set_low().ok(); | |
delay(clocks.sysclk().0 / 100); | |
let usb = Peripheral { | |
usb: dp.USB, | |
pin_dm: gpioa.pa11, | |
pin_dp: usb_dp.into_floating_input(&mut gpioa.crh), | |
}; | |
let usb_bus = UsbBus::new(usb); | |
SerialPool::grow(MEMORY); | |
let wb = SerialPool::alloc().unwrap().init(PoolNode::new()); | |
let rb = SerialPool::alloc().unwrap().init(PoolNode::new()); | |
let mut serial = PoolPort::new(&usb_bus, Some(wb), Some(rb)).unwrap(); | |
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; | |
} | |
match serial.process() { | |
Ok(c) if c > 0 => { | |
let rb = serial.replace_reader(None).unwrap(); | |
serial.replace_writer(Some(rb)); | |
serial.flush().ok(); | |
} | |
_ => {} | |
} | |
} | |
} | |
#[inline(never)] | |
#[panic_handler] | |
fn panic(_info: &PanicInfo) -> ! { | |
let gpioc = unsafe { &*pac::GPIOC::ptr() }; | |
gpioc.bsrr.write(|w| w.bs13().set_bit()); | |
loop { | |
atomic::compiler_fence(Ordering::SeqCst); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment