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

Update USB driver #320

Merged
merged 3 commits into from
Aug 3, 2021
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Breaking changes

- Bump `stm32-usbd` dependency (`0.6.0`)
- Use bit-banding for Peripheral enable/reset.
Don't require APBs in initializers.
- Rename `gpio::Edge::{RISING, FALLING, RISING_FALLING}` to `Rising`, `Falling`, `RisingFalling`, respectively
Expand Down Expand Up @@ -36,6 +37,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Changed

- USB driver is now enabled by default for all devices supporting it
- Updated `bxcan` dependency
- Change internal implementation of pins using const generics
- Use `cortex-m-rtic` instead of `cortex-m-rtfm` in the examples
Expand Down
7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ edition = "2018"
version = "0.7.0"

[package.metadata.docs.rs]
features = ["stm32f103", "rt", "stm32-usbd"]
features = ["stm32f103", "rt"]
default-target = "x86_64-unknown-linux-gnu"

[dependencies]
Expand All @@ -36,8 +36,7 @@ version = "0.2.3"
features = ["unproven"]

[dependencies.stm32-usbd]
version = "0.5.0"
features = ["ram_access_1x16"]
version = "0.6.0"
optional = true

[dev-dependencies]
Expand Down Expand Up @@ -79,7 +78,7 @@ doc = []
rt = ["stm32f1/rt"]
stm32f100 = ["stm32f1/stm32f100", "device-selected"]
stm32f101 = ["stm32f1/stm32f101", "device-selected"]
stm32f103 = ["stm32f1/stm32f103", "device-selected", "has-can"]
stm32f103 = ["stm32f1/stm32f103", "device-selected", "has-can", "stm32-usbd"]
stm32f105 = ["stm32f1/stm32f107", "device-selected", "connectivity"]
stm32f107 = ["stm32f1/stm32f107", "device-selected", "connectivity"]

Expand Down
4 changes: 2 additions & 2 deletions examples/usb_serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extern crate panic_semihosting;
use cortex_m::asm::delay;
use cortex_m_rt::entry;
use stm32f1xx_hal::usb::{Peripheral, UsbBus};
use stm32f1xx_hal::{prelude::*, stm32};
use stm32f1xx_hal::{pac, prelude::*};
use usb_device::prelude::*;
use usbd_serial::{SerialPort, USB_CLASS_CDC};

Expand All @@ -23,7 +23,7 @@ fn main() -> ! {
let dp = pac::Peripherals::take().unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have deprecated the stm32 name in favor of pac


let mut flash = dp.FLASH.constrain();
let mut rcc = dp.RCC.constrain();
let rcc = dp.RCC.constrain();

let clocks = rcc
.cfgr
Expand Down
15 changes: 7 additions & 8 deletions examples/usb_serial_interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ extern crate panic_semihosting;

use cortex_m::asm::{delay, wfi};
use cortex_m_rt::entry;
use stm32f1xx_hal::pac::{interrupt, Interrupt};
use stm32f1xx_hal::pac::{self, interrupt, Interrupt, NVIC};
use stm32f1xx_hal::prelude::*;
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};

Expand All @@ -19,11 +19,10 @@ static mut USB_DEVICE: Option<UsbDevice<UsbBusType>> = None;

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

let mut flash = dp.FLASH.constrain();
let mut rcc = dp.RCC.constrain();
let rcc = dp.RCC.constrain();

let clocks = rcc
.cfgr
Expand Down Expand Up @@ -71,10 +70,10 @@ fn main() -> ! {
USB_DEVICE = Some(usb_dev);
}

let mut nvic = p.NVIC;

nvic.enable(Interrupt::USB_HP_CAN_TX);
nvic.enable(Interrupt::USB_LP_CAN_RX0);
unsafe {
NVIC::unmask(Interrupt::USB_HP_CAN_TX);
NVIC::unmask(Interrupt::USB_LP_CAN_RX0);
}

loop {
wfi();
Expand Down
2 changes: 1 addition & 1 deletion examples/usb_serial_rtic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const APP: () = {
static mut USB_BUS: Option<bus::UsbBusAllocator<UsbBusType>> = None;

let mut flash = cx.device.FLASH.constrain();
let mut rcc = cx.device.RCC.constrain();
let rcc = cx.device.RCC.constrain();

let clocks = rcc
.cfgr
Expand Down
5 changes: 1 addition & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,7 @@ pub mod spi;
pub mod time;
#[cfg(feature = "device-selected")]
pub mod timer;
#[cfg(all(
feature = "stm32-usbd",
any(feature = "stm32f102", feature = "stm32f103")
))]
#[cfg(all(feature = "device-selected", feature = "stm32-usbd"))]
pub mod usb;
#[cfg(feature = "device-selected")]
pub mod watchdog;
14 changes: 7 additions & 7 deletions src/usb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! for usage examples.

use crate::pac::{RCC, USB};
use crate::rcc::{Enable, Reset};
use stm32_usbd::UsbPeripheral;

use crate::gpio::gpioa::{PA11, PA12};
Expand All @@ -24,18 +25,17 @@ unsafe impl UsbPeripheral for Peripheral {
const DP_PULL_UP_FEATURE: bool = false;
const EP_MEMORY: *const () = 0x4000_6000 as _;
const EP_MEMORY_SIZE: usize = 512;
const EP_MEMORY_ACCESS_2X16: bool = false;

fn enable() {
let rcc = unsafe { (&*RCC::ptr()) };
unsafe {
let rcc = &*RCC::ptr();

cortex_m::interrupt::free(|_| {
// Enable USB peripheral
rcc.apb1enr.modify(|_, w| w.usben().set_bit());

USB::enable(rcc);
// Reset USB peripheral
rcc.apb1rstr.modify(|_, w| w.usbrst().set_bit());
rcc.apb1rstr.modify(|_, w| w.usbrst().clear_bit());
});
USB::reset(rcc);
}
}

fn startup_delay() {
Expand Down