-
Notifications
You must be signed in to change notification settings - Fork 184
Add USB driver #135
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
Merged
Merged
Add USB driver #135
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
//! CDC-ACM serial port example using polling in a busy loop. | ||
//! Target board: Blue Pill | ||
#![no_std] | ||
#![no_main] | ||
|
||
extern crate panic_semihosting; | ||
|
||
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::{prelude::*, stm32}; | ||
use usb_device::prelude::*; | ||
use usbd_serial::{SerialPort, USB_CLASS_CDC}; | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
let dp = stm32::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); | ||
led.set_high(); // Turn off | ||
|
||
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(); | ||
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); | ||
|
||
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(); // 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(); // Turn off | ||
} | ||
} |
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
//! CDC-ACM serial port example using interrupts. | ||
//! Target board: Blue Pill | ||
#![no_std] | ||
#![no_main] | ||
|
||
extern crate panic_semihosting; | ||
|
||
use cortex_m::asm::{delay, wfi}; | ||
use cortex_m_rt::entry; | ||
use embedded_hal::digital::v2::OutputPin; | ||
use stm32f1xx_hal::stm32::{interrupt, Interrupt}; | ||
use stm32f1xx_hal::usb::{Peripheral, UsbBus, UsbBusType}; | ||
use stm32f1xx_hal::{prelude::*, stm32}; | ||
use usb_device::{bus::UsbBusAllocator, prelude::*}; | ||
use usbd_serial::{SerialPort, USB_CLASS_CDC}; | ||
|
||
static mut USB_BUS: Option<UsbBusAllocator<UsbBusType>> = None; | ||
static mut USB_SERIAL: Option<usbd_serial::SerialPort<UsbBusType>> = None; | ||
static mut USB_DEVICE: Option<UsbDevice<UsbBusType>> = None; | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
let p = cortex_m::Peripherals::take().unwrap(); | ||
let dp = stm32::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()); | ||
|
||
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(); | ||
delay(clocks.sysclk().0 / 100); | ||
|
||
let usb_dm = gpioa.pa11; | ||
let usb_dp = usb_dp.into_floating_input(&mut gpioa.crh); | ||
|
||
let usb = Peripheral { | ||
usb: dp.USB, | ||
pin_dm: usb_dm, | ||
pin_dp: usb_dp, | ||
}; | ||
|
||
// Unsafe to allow access to static variables | ||
unsafe { | ||
let bus = UsbBus::new(usb); | ||
|
||
USB_BUS = Some(bus); | ||
|
||
USB_SERIAL = Some(SerialPort::new(USB_BUS.as_ref().unwrap())); | ||
|
||
let usb_dev = UsbDeviceBuilder::new(USB_BUS.as_ref().unwrap(), UsbVidPid(0x16c0, 0x27dd)) | ||
.manufacturer("Fake company") | ||
.product("Serial port") | ||
.serial_number("TEST") | ||
.device_class(USB_CLASS_CDC) | ||
.build(); | ||
|
||
USB_DEVICE = Some(usb_dev); | ||
} | ||
|
||
let mut nvic = p.NVIC; | ||
|
||
nvic.enable(Interrupt::USB_HP_CAN_TX); | ||
nvic.enable(Interrupt::USB_LP_CAN_RX0); | ||
|
||
loop { | ||
wfi(); | ||
} | ||
} | ||
|
||
#[interrupt] | ||
fn USB_HP_CAN_TX() { | ||
usb_interrupt(); | ||
} | ||
|
||
#[interrupt] | ||
fn USB_LP_CAN_RX0() { | ||
usb_interrupt(); | ||
} | ||
|
||
fn usb_interrupt() { | ||
let usb_dev = unsafe { USB_DEVICE.as_mut().unwrap() }; | ||
let serial = unsafe { USB_SERIAL.as_mut().unwrap() }; | ||
|
||
if !usb_dev.poll(&mut [serial]) { | ||
return; | ||
} | ||
|
||
let mut buf = [0u8; 8]; | ||
|
||
match serial.read(&mut buf) { | ||
Ok(count) if count > 0 => { | ||
// Echo back in upper case | ||
for c in buf[0..count].iter_mut() { | ||
if 0x61 <= *c && *c <= 0x7a { | ||
*c &= !0x20; | ||
} | ||
} | ||
|
||
serial.write(&buf[0..count]).ok(); | ||
} | ||
_ => {} | ||
} | ||
} |
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
//! CDC-ACM serial port example using cortex-m-rtfm. | ||
//! Target board: Blue Pill | ||
#![no_main] | ||
#![no_std] | ||
#![allow(non_snake_case)] | ||
|
||
extern crate panic_semihosting; | ||
|
||
use cortex_m::asm::delay; | ||
use embedded_hal::digital::v2::OutputPin; | ||
use rtfm::app; | ||
use stm32f1xx_hal::prelude::*; | ||
use stm32f1xx_hal::usb::{Peripheral, UsbBus, UsbBusType}; | ||
use usb_device::bus; | ||
use usb_device::prelude::*; | ||
use usbd_serial::{SerialPort, USB_CLASS_CDC}; | ||
|
||
#[app(device = stm32f1xx_hal::stm32)] | ||
const APP: () = { | ||
static mut USB_DEV: UsbDevice<'static, UsbBusType> = (); | ||
static mut SERIAL: SerialPort<'static, UsbBusType> = (); | ||
|
||
#[init] | ||
fn init() { | ||
static mut USB_BUS: Option<bus::UsbBusAllocator<UsbBusType>> = None; | ||
|
||
let mut flash = device.FLASH.constrain(); | ||
let mut rcc = device.RCC.constrain(); | ||
|
||
let clocks = rcc | ||
.cfgr | ||
.use_hse(8.mhz()) | ||
.sysclk(48.mhz()) | ||
.pclk1(24.mhz()) | ||
.freeze(&mut flash.acr); | ||
|
||
assert!(clocks.usbclk_valid()); | ||
|
||
let mut gpioa = device.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(); | ||
delay(clocks.sysclk().0 / 100); | ||
|
||
let usb_dm = gpioa.pa11; | ||
let usb_dp = usb_dp.into_floating_input(&mut gpioa.crh); | ||
|
||
let usb = Peripheral { | ||
usb: device.USB, | ||
pin_dm: usb_dm, | ||
pin_dp: usb_dp, | ||
}; | ||
|
||
*USB_BUS = Some(UsbBus::new(usb)); | ||
|
||
let serial = SerialPort::new(USB_BUS.as_ref().unwrap()); | ||
|
||
let usb_dev = UsbDeviceBuilder::new(USB_BUS.as_ref().unwrap(), UsbVidPid(0x16c0, 0x27dd)) | ||
.manufacturer("Fake company") | ||
.product("Serial port") | ||
.serial_number("TEST") | ||
.device_class(USB_CLASS_CDC) | ||
.build(); | ||
|
||
USB_DEV = usb_dev; | ||
SERIAL = serial; | ||
} | ||
|
||
#[interrupt(resources = [USB_DEV, SERIAL])] | ||
fn USB_HP_CAN_TX() { | ||
usb_poll(&mut resources.USB_DEV, &mut resources.SERIAL); | ||
} | ||
|
||
#[interrupt(resources = [USB_DEV, SERIAL])] | ||
fn USB_LP_CAN_RX0() { | ||
usb_poll(&mut resources.USB_DEV, &mut resources.SERIAL); | ||
} | ||
}; | ||
|
||
fn usb_poll<B: bus::UsbBus>( | ||
usb_dev: &mut UsbDevice<'static, B>, | ||
serial: &mut SerialPort<'static, B>, | ||
) { | ||
if !usb_dev.poll(&mut [serial]) { | ||
return; | ||
} | ||
|
||
let mut buf = [0u8; 8]; | ||
|
||
match serial.read(&mut buf) { | ||
Ok(count) if count > 0 => { | ||
// Echo back in upper case | ||
for c in buf[0..count].iter_mut() { | ||
if 0x61 <= *c && *c <= 0x7a { | ||
*c &= !0x20; | ||
} | ||
} | ||
|
||
serial.write(&buf[0..count]).ok(); | ||
} | ||
_ => {} | ||
} | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need that? I've removed it from my keyboard, and it still work fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code is needed only for development. Without it host doesn't reset your device when you upload new firmware.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe that could be documented in the comment above.