From cfe35f8fd4f5c689dc1a92f7462f135df8af21ae Mon Sep 17 00:00:00 2001 From: Vadim Kaushan Date: Fri, 8 Nov 2019 00:37:51 +0300 Subject: [PATCH 01/25] Introduce UsbPeripheral trait --- src/lib.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 5bd5692..29e6aa2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,3 +31,11 @@ pub use crate::target::usb_pins::UsbPinsType; pub type UsbBusType = UsbBus; mod pac; + +/// A trait for device-specific USB peripherals. Implement this to add support for a new hardware +/// platform. Peripherals that have this trait must have the same register block as STM32 USBFS +/// peripherals. +pub unsafe trait UsbPeripheral: Send + Sync { + /// Pointer to the register block + const REGISTERS: *const (); +} From e534d70b79a6cb6f95f2c0ab86f83e01eeb81749 Mon Sep 17 00:00:00 2001 From: Vadim Kaushan Date: Fri, 8 Nov 2019 00:40:00 +0300 Subject: [PATCH 02/25] Use UsbPeripheral instead of USB from pac --- src/bus.rs | 29 +++++++++++++++++------------ src/endpoint.rs | 17 +++++++++++------ src/lib.rs | 2 ++ src/registers.rs | 28 ++++++++++++++++++++++++++++ src/target.rs | 35 ----------------------------------- 5 files changed, 58 insertions(+), 53 deletions(-) create mode 100644 src/registers.rs diff --git a/src/bus.rs b/src/bus.rs index 5edde03..18bcd49 100644 --- a/src/bus.rs +++ b/src/bus.rs @@ -1,6 +1,5 @@ //! USB peripheral driver. -use core::marker::PhantomData; use core::mem; use cortex_m::asm::delay; use cortex_m::interrupt::{self, Mutex}; @@ -10,28 +9,31 @@ use usb_device::{Result, UsbDirection, UsbError}; use crate::endpoint::{calculate_count_rx, Endpoint, EndpointStatus}; use crate::endpoint_memory::EndpointMemoryAllocator; -use crate::target::{apb_usb_enable, UsbPins, UsbRegisters, NUM_ENDPOINTS, USB}; +use crate::registers::UsbRegisters; +use crate::target::{apb_usb_enable, NUM_ENDPOINTS}; +use crate::UsbPeripheral; /// USB peripheral driver for STM32 microcontrollers. -pub struct UsbBus { - regs: Mutex, - endpoints: [Endpoint; NUM_ENDPOINTS], +pub struct UsbBus { + peripheral: USB, + regs: Mutex>, + endpoints: [Endpoint; NUM_ENDPOINTS], ep_allocator: EndpointMemoryAllocator, max_endpoint: usize, - pins: PhantomData, } -impl UsbBus { +impl UsbBus { /// Constructs a new USB peripheral driver. - pub fn new(regs: USB, _pins: PINS) -> UsbBusAllocator { + pub fn new(peripheral: USB) -> UsbBusAllocator { apb_usb_enable(); let bus = UsbBus { - regs: Mutex::new(UsbRegisters::new(regs)), + peripheral, + regs: Mutex::new(UsbRegisters::new()), ep_allocator: EndpointMemoryAllocator::new(), max_endpoint: 0, endpoints: unsafe { - let mut endpoints: [Endpoint; NUM_ENDPOINTS] = mem::uninitialized(); + let mut endpoints: [Endpoint; NUM_ENDPOINTS] = mem::uninitialized(); for i in 0..NUM_ENDPOINTS { endpoints[i] = Endpoint::new(i as u8); @@ -39,12 +41,15 @@ impl UsbBus { endpoints }, - pins: PhantomData, }; UsbBusAllocator::new(bus) } + pub fn free(self) -> USB { + self.peripheral + } + /// Simulates a disconnect from the USB bus, causing the host to reset and re-enumerate the /// device. /// @@ -69,7 +74,7 @@ impl UsbBus { } } -impl usb_device::bus::UsbBus for UsbBus { +impl usb_device::bus::UsbBus for UsbBus { fn alloc_ep( &mut self, ep_dir: UsbDirection, diff --git a/src/endpoint.rs b/src/endpoint.rs index 8d2aa34..1c46ac9 100644 --- a/src/endpoint.rs +++ b/src/endpoint.rs @@ -1,5 +1,8 @@ use crate::endpoint_memory::{BufferDescriptor, EndpointBuffer, EndpointMemoryAllocator}; -use crate::target::{usb, UsbAccessType, UsbRegisters}; +use crate::registers::UsbRegisters; +use crate::target::{usb, UsbAccessType}; +use crate::UsbPeripheral; +use core::marker::PhantomData; use core::mem; use cortex_m::interrupt::{self, CriticalSection, Mutex}; use usb_device::endpoint::EndpointType; @@ -7,11 +10,12 @@ use usb_device::{Result, UsbError}; /// Arbitrates access to the endpoint-specific registers and packet buffer memory. #[derive(Default)] -pub struct Endpoint { +pub struct Endpoint { out_buf: Option>, in_buf: Option>, ep_type: Option, index: u8, + _marker: PhantomData, } pub fn calculate_count_rx(mut size: usize) -> Result<(usize, u16)> { @@ -34,13 +38,14 @@ pub fn calculate_count_rx(mut size: usize) -> Result<(usize, u16)> { } } -impl Endpoint { - pub fn new(index: u8) -> Endpoint { - Endpoint { +impl Endpoint { + pub fn new(index: u8) -> Self { + Self { out_buf: None, in_buf: None, ep_type: None, index, + _marker: PhantomData, } } @@ -83,7 +88,7 @@ impl Endpoint { } fn reg(&self) -> &'static usb::EPR { - UsbRegisters::ep_register(self.index) + UsbRegisters::::ep_register(self.index) } #[rustfmt::skip] diff --git a/src/lib.rs b/src/lib.rs index 29e6aa2..809893a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,6 +17,8 @@ mod endpoint; #[cfg(feature = "family-selected")] mod endpoint_memory; +mod registers; + #[cfg(feature = "family-selected")] mod target; diff --git a/src/registers.rs b/src/registers.rs new file mode 100644 index 0000000..51b2bc7 --- /dev/null +++ b/src/registers.rs @@ -0,0 +1,28 @@ +use crate::pac::usb::{RegisterBlock, EPR}; +use crate::UsbPeripheral; +use core::marker::PhantomData; + +/// Wrapper around device-specific peripheral that provides unified register interface +pub struct UsbRegisters { + _marker: PhantomData, +} + +impl core::ops::Deref for UsbRegisters { + type Target = RegisterBlock; + + fn deref(&self) -> &Self::Target { + let ptr = USB::REGISTERS as *const Self::Target; + unsafe { &*ptr } + } +} + +impl UsbRegisters { + pub fn new() -> Self { + Self { _marker: PhantomData } + } + + pub fn ep_register(index: u8) -> &'static EPR { + let usb_ptr = USB::REGISTERS as *const RegisterBlock; + unsafe { &(*usb_ptr).epr[index as usize] } + } +} diff --git a/src/target.rs b/src/target.rs index e493fcb..f7edac1 100644 --- a/src/target.rs +++ b/src/target.rs @@ -12,18 +12,6 @@ pub use stm32l0xx_hal as hal; #[cfg(feature = "stm32l4")] pub use stm32l4xx_hal as hal; -// USB PAC reexports -#[cfg(feature = "stm32f0")] -pub use hal::stm32::USB; -#[cfg(feature = "stm32f1")] -pub use hal::stm32::USB; -#[cfg(feature = "stm32f3")] -pub use hal::stm32::USB; -#[cfg(feature = "stm32l0")] -pub use hal::stm32::USB; -#[cfg(feature = "stm32l4")] -pub use hal::stm32::USB; - // Use bundled register definitions instead of device-specific ones // This should work because register definitions from newer chips seem to be // compatible with definitions for older ones. @@ -59,29 +47,6 @@ pub fn apb_usb_enable() { }); } -/// Wrapper around device-specific peripheral that provides unified register interface -pub struct UsbRegisters(USB); - -impl core::ops::Deref for UsbRegisters { - type Target = usb::RegisterBlock; - - fn deref(&self) -> &Self::Target { - let ptr = USB::ptr() as *const Self::Target; - unsafe { &*ptr } - } -} - -impl UsbRegisters { - pub fn new(usb: USB) -> Self { - Self(usb) - } - - pub fn ep_register(index: u8) -> &'static usb::EPR { - let usb_ptr = USB::ptr() as *const usb::RegisterBlock; - unsafe { &(*usb_ptr).epr[index as usize] } - } -} - pub trait UsbPins: Send {} #[cfg(feature = "stm32f0")] From e7f44a12458a0086e4b715a53a0366625fd781ab Mon Sep 17 00:00:00 2001 From: Vadim Kaushan Date: Fri, 8 Nov 2019 00:55:28 +0300 Subject: [PATCH 03/25] Remove UsbPins trait --- src/lib.rs | 4 ---- src/target.rs | 50 -------------------------------------------------- 2 files changed, 54 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 809893a..d4c843d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,10 +27,6 @@ pub mod bus; #[cfg(feature = "family-selected")] pub use crate::bus::UsbBus; -#[cfg(feature = "family-selected")] -pub use crate::target::usb_pins::UsbPinsType; -#[cfg(feature = "family-selected")] -pub type UsbBusType = UsbBus; mod pac; diff --git a/src/target.rs b/src/target.rs index f7edac1..52cb70d 100644 --- a/src/target.rs +++ b/src/target.rs @@ -46,53 +46,3 @@ pub fn apb_usb_enable() { } }); } - -pub trait UsbPins: Send {} - -#[cfg(feature = "stm32f0")] -pub mod usb_pins { - use super::hal::gpio::gpioa::{PA11, PA12}; - use super::hal::gpio::{Floating, Input}; - - pub type UsbPinsType = (PA11>, PA12>); - impl super::UsbPins for UsbPinsType {} -} - -#[cfg(feature = "stm32f1")] -pub mod usb_pins { - use super::hal::gpio::gpioa::{PA11, PA12}; - use super::hal::gpio::{Floating, Input}; - - pub type UsbPinsType = (PA11>, PA12>); - impl super::UsbPins for UsbPinsType {} -} - -#[cfg(feature = "stm32f3")] -pub mod usb_pins { - use super::hal::gpio::gpioa::{PA11, PA12}; - use super::hal::gpio::AF14; - - pub type UsbPinsType = (PA11, PA12); - impl super::UsbPins for UsbPinsType {} -} - -#[cfg(feature = "stm32l0")] -pub mod usb_pins { - use super::hal::gpio::gpioa::{PA11, PA12}; - use super::hal::gpio::{Floating, Input}; - - pub type UsbPinsType = (PA11>, PA12>); - impl super::UsbPins for UsbPinsType {} -} - -#[cfg(feature = "stm32l4")] -pub mod usb_pins { - use super::hal::gpio::gpioa::{PA11, PA12}; - use super::hal::gpio::{Alternate, Floating, Input, AF10}; - - pub type UsbPinsType = ( - PA11>>, - PA12>>, - ); - impl super::UsbPins for UsbPinsType {} -} From 797a054f9ef3d4ddfe014867f963107d7e318761 Mon Sep 17 00:00:00 2001 From: Vadim Kaushan Date: Fri, 8 Nov 2019 01:00:10 +0300 Subject: [PATCH 04/25] Move apb_usb_enable() to the UsbPeripheral trait --- src/bus.rs | 4 ++-- src/lib.rs | 3 +++ src/target.rs | 13 ------------- 3 files changed, 5 insertions(+), 15 deletions(-) diff --git a/src/bus.rs b/src/bus.rs index 18bcd49..0eef0b8 100644 --- a/src/bus.rs +++ b/src/bus.rs @@ -10,7 +10,7 @@ use usb_device::{Result, UsbDirection, UsbError}; use crate::endpoint::{calculate_count_rx, Endpoint, EndpointStatus}; use crate::endpoint_memory::EndpointMemoryAllocator; use crate::registers::UsbRegisters; -use crate::target::{apb_usb_enable, NUM_ENDPOINTS}; +use crate::target::NUM_ENDPOINTS; use crate::UsbPeripheral; /// USB peripheral driver for STM32 microcontrollers. @@ -25,7 +25,7 @@ pub struct UsbBus { impl UsbBus { /// Constructs a new USB peripheral driver. pub fn new(peripheral: USB) -> UsbBusAllocator { - apb_usb_enable(); + USB::enable(); let bus = UsbBus { peripheral, diff --git a/src/lib.rs b/src/lib.rs index d4c843d..31fc0d6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,4 +36,7 @@ mod pac; pub unsafe trait UsbPeripheral: Send + Sync { /// Pointer to the register block const REGISTERS: *const (); + + /// Enables USB device on its peripheral bus + fn enable(); } diff --git a/src/target.rs b/src/target.rs index 52cb70d..76cea04 100644 --- a/src/target.rs +++ b/src/target.rs @@ -33,16 +33,3 @@ pub const EP_MEM_SIZE: usize = 512; pub const EP_MEM_SIZE: usize = 1024; pub const NUM_ENDPOINTS: usize = 8; - -/// Enables USB peripheral -pub fn apb_usb_enable() { - cortex_m::interrupt::free(|_| { - let rcc = unsafe { (&*hal::stm32::RCC::ptr()) }; - match () { - #[cfg(any(feature = "stm32f0", feature = "stm32f1", feature = "stm32f3", feature = "stm32l0"))] - () => rcc.apb1enr.modify(|_, w| w.usben().set_bit()), - #[cfg(feature = "stm32l4")] - () => rcc.apb1enr1.modify(|_, w| w.usbfsen().set_bit()), - } - }); -} From 29c5f2fb6934e4e9133777fdd76e1a994e6827e0 Mon Sep 17 00:00:00 2001 From: Vadim Kaushan Date: Fri, 8 Nov 2019 01:04:20 +0300 Subject: [PATCH 05/25] Remove hal dependencies --- Cargo.toml | 31 +++++++++++++------------------ src/target.rs | 12 ------------ 2 files changed, 13 insertions(+), 30 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8e02f57..0617c2d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,20 +12,15 @@ keywords = ["no-std", "embedded", "usb"] [dependencies] vcell = "0.1.2" cortex-m = "0.6.1" -stm32f0xx-hal = { version = "0.15", features = ["rt"], optional = true } -stm32f1xx-hal = { version = "0.4", features = ["rt"], optional = true } -stm32f3xx-hal = { version = "0.3", features = ["rt"], optional = true } -stm32l0xx-hal = { version = "0.3", features = ["rt"], optional = true } -stm32l4xx-hal = { version = "0.5", features = ["rt"], optional = true } usb-device = "0.2.3" [features] # Device family -stm32f0 = ['stm32f0xx-hal', 'family-selected'] -stm32f1 = ['stm32f1xx-hal', 'family-selected'] -stm32f3 = ['stm32f3xx-hal', 'family-selected'] -stm32l0 = ['stm32l0xx-hal', 'family-selected'] -stm32l4 = ['stm32l4xx-hal', 'family-selected'] +stm32f0 = ['family-selected'] +stm32f1 = ['family-selected'] +stm32f3 = ['family-selected'] +stm32l0 = ['family-selected'] +stm32l4 = ['family-selected'] # Dedicated USB RAM size ram_size_512 = [] @@ -45,14 +40,14 @@ ram_addr_40006c00 = [] delay_workaround = [] # deprecated: cortex-m 0.6.1 contains a fix # Known devices -stm32f042xx = ['stm32f0', 'stm32f0xx-hal/stm32f042', 'new_gen'] -stm32f048xx = ['stm32f0', 'stm32f0xx-hal/stm32f048', 'new_gen'] -stm32f072xx = ['stm32f0', 'stm32f0xx-hal/stm32f072', 'new_gen'] -stm32f078xx = ['stm32f0', 'stm32f0xx-hal/stm32f078', 'new_gen'] -stm32f103xx = ['stm32f1', 'stm32f1xx-hal/stm32f103', 'old_gen'] -stm32f303xc = ['stm32f3', 'stm32f3xx-hal/stm32f303', 'old_gen'] -stm32l0x2xx = ['stm32l0', 'stm32l0xx-hal/stm32l0x2', 'new_gen'] -stm32l4x2xx = ['stm32l4', 'stm32l4xx-hal/stm32l4x2', 'new_gen', 'ram_addr_40006c00'] +stm32f042xx = ['stm32f0', 'new_gen'] +stm32f048xx = ['stm32f0', 'new_gen'] +stm32f072xx = ['stm32f0', 'new_gen'] +stm32f078xx = ['stm32f0', 'new_gen'] +stm32f103xx = ['stm32f1', 'old_gen'] +stm32f303xc = ['stm32f3', 'old_gen'] +stm32l0x2xx = ['stm32l0', 'new_gen'] +stm32l4x2xx = ['stm32l4', 'new_gen', 'ram_addr_40006c00'] # Auxiliary features old_gen = ['ram_size_512', 'ram_access_1x16'] diff --git a/src/target.rs b/src/target.rs index 76cea04..49f7e9c 100644 --- a/src/target.rs +++ b/src/target.rs @@ -1,17 +1,5 @@ //! Target-specific definitions -// Export HAL -#[cfg(feature = "stm32f0")] -pub use stm32f0xx_hal as hal; -#[cfg(feature = "stm32f1")] -pub use stm32f1xx_hal as hal; -#[cfg(feature = "stm32f3")] -pub use stm32f3xx_hal as hal; -#[cfg(feature = "stm32l0")] -pub use stm32l0xx_hal as hal; -#[cfg(feature = "stm32l4")] -pub use stm32l4xx_hal as hal; - // Use bundled register definitions instead of device-specific ones // This should work because register definitions from newer chips seem to be // compatible with definitions for older ones. From 2ce8a2754a74a0ee159b4d98000418f3f330ca49 Mon Sep 17 00:00:00 2001 From: Vadim Kaushan Date: Fri, 8 Nov 2019 01:18:44 +0300 Subject: [PATCH 06/25] Move chip specific delay() into the UsbPeripheral trait --- src/bus.rs | 5 +---- src/lib.rs | 6 ++++++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/bus.rs b/src/bus.rs index 0eef0b8..c806536 100644 --- a/src/bus.rs +++ b/src/bus.rs @@ -1,7 +1,6 @@ //! USB peripheral driver. use core::mem; -use cortex_m::asm::delay; use cortex_m::interrupt::{self, Mutex}; use usb_device::bus::{PollResult, UsbBusAllocator}; use usb_device::endpoint::{EndpointAddress, EndpointType}; @@ -141,9 +140,7 @@ impl usb_device::bus::UsbBus for UsbBus { regs.cntr.modify(|_, w| w.pdwn().clear_bit()); - // There is a chip specific startup delay. For STM32F103xx it's 1µs and this should wait for - // at least that long. - delay(72); + USB::startup_delay(); regs.btable.modify(|_, w| w.btable().bits(0)); regs.cntr.modify(|_, w| { w diff --git a/src/lib.rs b/src/lib.rs index 31fc0d6..fb7b55a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,4 +39,10 @@ pub unsafe trait UsbPeripheral: Send + Sync { /// Enables USB device on its peripheral bus fn enable(); + + /// Performs a chip specific startup delay + /// + /// This function is called in `UsbBus::enable()` after deasserting the `pdwn` bit and before + /// peripheral initialization. + fn startup_delay(); } From 316bf8b499ed1c68fcaf51ee746ef2ee8f82f283 Mon Sep 17 00:00:00 2001 From: Vadim Kaushan Date: Fri, 8 Nov 2019 01:21:43 +0300 Subject: [PATCH 07/25] Refactoring --- src/endpoint.rs | 7 ++++++- src/target.rs | 5 ----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/endpoint.rs b/src/endpoint.rs index 1c46ac9..1d1a861 100644 --- a/src/endpoint.rs +++ b/src/endpoint.rs @@ -1,6 +1,6 @@ use crate::endpoint_memory::{BufferDescriptor, EndpointBuffer, EndpointMemoryAllocator}; use crate::registers::UsbRegisters; -use crate::target::{usb, UsbAccessType}; +use crate::target::UsbAccessType; use crate::UsbPeripheral; use core::marker::PhantomData; use core::mem; @@ -8,6 +8,11 @@ use cortex_m::interrupt::{self, CriticalSection, Mutex}; use usb_device::endpoint::EndpointType; use usb_device::{Result, UsbError}; +// Use bundled register definitions instead of device-specific ones +// This should work because register definitions from newer chips seem to be +// compatible with definitions for older ones. +pub use crate::pac::usb; + /// Arbitrates access to the endpoint-specific registers and packet buffer memory. #[derive(Default)] pub struct Endpoint { diff --git a/src/target.rs b/src/target.rs index 49f7e9c..3c19b8d 100644 --- a/src/target.rs +++ b/src/target.rs @@ -1,10 +1,5 @@ //! Target-specific definitions -// Use bundled register definitions instead of device-specific ones -// This should work because register definitions from newer chips seem to be -// compatible with definitions for older ones. -pub use crate::pac::usb; - #[cfg(feature = "ram_access_1x16")] pub type UsbAccessType = u32; #[cfg(feature = "ram_access_2x16")] From 8fc41abc7971ae34479042adbcd8f229c135cf6e Mon Sep 17 00:00:00 2001 From: Vadim Kaushan Date: Fri, 8 Nov 2019 01:26:08 +0300 Subject: [PATCH 08/25] Remove device family features --- Cargo.toml | 23 ++++++++--------------- src/lib.rs | 18 +----------------- 2 files changed, 9 insertions(+), 32 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0617c2d..378209d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,13 +15,6 @@ cortex-m = "0.6.1" usb-device = "0.2.3" [features] -# Device family -stm32f0 = ['family-selected'] -stm32f1 = ['family-selected'] -stm32f3 = ['family-selected'] -stm32l0 = ['family-selected'] -stm32l4 = ['family-selected'] - # Dedicated USB RAM size ram_size_512 = [] ram_size_1024 = [] @@ -40,14 +33,14 @@ ram_addr_40006c00 = [] delay_workaround = [] # deprecated: cortex-m 0.6.1 contains a fix # Known devices -stm32f042xx = ['stm32f0', 'new_gen'] -stm32f048xx = ['stm32f0', 'new_gen'] -stm32f072xx = ['stm32f0', 'new_gen'] -stm32f078xx = ['stm32f0', 'new_gen'] -stm32f103xx = ['stm32f1', 'old_gen'] -stm32f303xc = ['stm32f3', 'old_gen'] -stm32l0x2xx = ['stm32l0', 'new_gen'] -stm32l4x2xx = ['stm32l4', 'new_gen', 'ram_addr_40006c00'] +stm32f042xx = ['new_gen'] +stm32f048xx = ['new_gen'] +stm32f072xx = ['new_gen'] +stm32f078xx = ['new_gen'] +stm32f103xx = ['old_gen'] +stm32f303xc = ['old_gen'] +stm32l0x2xx = ['new_gen'] +stm32l4x2xx = ['new_gen', 'ram_addr_40006c00'] # Auxiliary features old_gen = ['ram_size_512', 'ram_access_1x16'] diff --git a/src/lib.rs b/src/lib.rs index fb7b55a..f311fee 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,27 +5,11 @@ #![no_std] -#[cfg(not(feature = "family-selected"))] -compile_error!( - "This crate requires one of the device family features enabled. -Check Cargo.toml for supported families ('Device family' section)" -); - -#[cfg(feature = "family-selected")] +pub mod bus; mod endpoint; - -#[cfg(feature = "family-selected")] mod endpoint_memory; - mod registers; - -#[cfg(feature = "family-selected")] mod target; - -#[cfg(feature = "family-selected")] -pub mod bus; - -#[cfg(feature = "family-selected")] pub use crate::bus::UsbBus; mod pac; From 728cb58d8868098069f999390438010f9207e4ae Mon Sep 17 00:00:00 2001 From: Vadim Kaushan Date: Fri, 8 Nov 2019 01:32:10 +0300 Subject: [PATCH 09/25] Remove deprecated delay_workaround feature --- Cargo.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 378209d..d3f59df 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,7 +30,6 @@ dp_pull_up_support = [] # Hacks ram_addr_40006c00 = [] -delay_workaround = [] # deprecated: cortex-m 0.6.1 contains a fix # Known devices stm32f042xx = ['new_gen'] From 22cefbe079547c3f0e1d5a2f419e53fd3bef4eb1 Mon Sep 17 00:00:00 2001 From: Vadim Kaushan Date: Fri, 8 Nov 2019 01:36:27 +0300 Subject: [PATCH 10/25] Move dp_pull_up_support feature into the UsbPeripheral trait --- Cargo.toml | 3 +-- src/bus.rs | 5 +++-- src/lib.rs | 3 +++ 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d3f59df..e5950ba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,6 @@ ram_access_2x16 = [] # USB peripheral features lpm_support = [] bcd_support = [] -dp_pull_up_support = [] # Hacks ram_addr_40006c00 = [] @@ -43,7 +42,7 @@ stm32l4x2xx = ['new_gen', 'ram_addr_40006c00'] # Auxiliary features old_gen = ['ram_size_512', 'ram_access_1x16'] -new_gen = ['ram_size_1024', 'ram_access_2x16', 'lpm_support', 'bcd_support', 'dp_pull_up_support'] +new_gen = ['ram_size_1024', 'ram_access_2x16', 'lpm_support', 'bcd_support'] family-selected = [] [package.metadata.docs.rs] diff --git a/src/bus.rs b/src/bus.rs index c806536..c0bbb95 100644 --- a/src/bus.rs +++ b/src/bus.rs @@ -152,8 +152,9 @@ impl usb_device::bus::UsbBus for UsbBus { }); regs.istr.modify(|_, w| unsafe { w.bits(0) }); - #[cfg(feature = "dp_pull_up_support")] - regs.bcdr.modify(|_, w| w.dppu().set_bit()); + if USB::DP_PULL_UP_FEATURE { + regs.bcdr.modify(|_, w| w.dppu().set_bit()); + } }); } diff --git a/src/lib.rs b/src/lib.rs index f311fee..a979d28 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,6 +21,9 @@ pub unsafe trait UsbPeripheral: Send + Sync { /// Pointer to the register block const REGISTERS: *const (); + /// Embedded pull-up resistor on USB_DP line + const DP_PULL_UP_FEATURE: bool; + /// Enables USB device on its peripheral bus fn enable(); From 60f537509ed19cdf33089e3050e5901da970a944 Mon Sep 17 00:00:00 2001 From: Vadim Kaushan Date: Fri, 8 Nov 2019 01:39:55 +0300 Subject: [PATCH 11/25] Remove unused lpm_support and bcd_support features --- Cargo.toml | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e5950ba..75337cf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,10 +23,6 @@ ram_size_1024 = [] ram_access_1x16 = [] ram_access_2x16 = [] -# USB peripheral features -lpm_support = [] -bcd_support = [] - # Hacks ram_addr_40006c00 = [] @@ -42,7 +38,7 @@ stm32l4x2xx = ['new_gen', 'ram_addr_40006c00'] # Auxiliary features old_gen = ['ram_size_512', 'ram_access_1x16'] -new_gen = ['ram_size_1024', 'ram_access_2x16', 'lpm_support', 'bcd_support'] +new_gen = ['ram_size_1024', 'ram_access_2x16'] family-selected = [] [package.metadata.docs.rs] From 27c21f9acf88f63aaefbd61fbbd43f032bcb78b5 Mon Sep 17 00:00:00 2001 From: Vadim Kaushan Date: Fri, 8 Nov 2019 01:42:31 +0300 Subject: [PATCH 12/25] Remove family-selected feature --- Cargo.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 75337cf..1bdcc02 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,7 +39,6 @@ stm32l4x2xx = ['new_gen', 'ram_addr_40006c00'] # Auxiliary features old_gen = ['ram_size_512', 'ram_access_1x16'] new_gen = ['ram_size_1024', 'ram_access_2x16'] -family-selected = [] [package.metadata.docs.rs] features = ["stm32f103xx"] From 96232aa130e1ed6d1b9a2f0b9fa78c68303313a0 Mon Sep 17 00:00:00 2001 From: Vadim Kaushan Date: Fri, 8 Nov 2019 02:01:36 +0300 Subject: [PATCH 13/25] Move EP_MEM_ADDR and EP_MEM_SIZE into the UsbPeripheral trait --- Cargo.toml | 13 +++---------- src/bus.rs | 2 +- src/endpoint.rs | 6 +++--- src/endpoint_memory.rs | 28 ++++++++++++++++------------ src/lib.rs | 6 ++++++ src/target.rs | 10 ---------- 6 files changed, 29 insertions(+), 36 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1bdcc02..3fb5cf1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,17 +15,10 @@ cortex-m = "0.6.1" usb-device = "0.2.3" [features] -# Dedicated USB RAM size -ram_size_512 = [] -ram_size_1024 = [] - # USB RAM access scheme ram_access_1x16 = [] ram_access_2x16 = [] -# Hacks -ram_addr_40006c00 = [] - # Known devices stm32f042xx = ['new_gen'] stm32f048xx = ['new_gen'] @@ -34,11 +27,11 @@ stm32f078xx = ['new_gen'] stm32f103xx = ['old_gen'] stm32f303xc = ['old_gen'] stm32l0x2xx = ['new_gen'] -stm32l4x2xx = ['new_gen', 'ram_addr_40006c00'] +stm32l4x2xx = ['new_gen'] # Auxiliary features -old_gen = ['ram_size_512', 'ram_access_1x16'] -new_gen = ['ram_size_1024', 'ram_access_2x16'] +old_gen = ['ram_access_1x16'] +new_gen = ['ram_access_2x16'] [package.metadata.docs.rs] features = ["stm32f103xx"] diff --git a/src/bus.rs b/src/bus.rs index c0bbb95..2f6945e 100644 --- a/src/bus.rs +++ b/src/bus.rs @@ -17,7 +17,7 @@ pub struct UsbBus { peripheral: USB, regs: Mutex>, endpoints: [Endpoint; NUM_ENDPOINTS], - ep_allocator: EndpointMemoryAllocator, + ep_allocator: EndpointMemoryAllocator, max_endpoint: usize, } diff --git a/src/endpoint.rs b/src/endpoint.rs index 1d1a861..14ec13e 100644 --- a/src/endpoint.rs +++ b/src/endpoint.rs @@ -67,7 +67,7 @@ impl Endpoint { } pub fn set_out_buf(&mut self, buffer: EndpointBuffer, size_bits: u16) { - let offset = buffer.offset(); + let offset = buffer.offset::(); self.out_buf = Some(Mutex::new(buffer)); let descr = self.descr(); @@ -80,7 +80,7 @@ impl Endpoint { } pub fn set_in_buf(&mut self, buffer: EndpointBuffer) { - let offset = buffer.offset(); + let offset = buffer.offset::(); self.in_buf = Some(Mutex::new(buffer)); let descr = self.descr(); @@ -89,7 +89,7 @@ impl Endpoint { } fn descr(&self) -> &'static BufferDescriptor { - EndpointMemoryAllocator::buffer_descriptor(self.index) + EndpointMemoryAllocator::::buffer_descriptor(self.index) } fn reg(&self) -> &'static usb::EPR { diff --git a/src/endpoint_memory.rs b/src/endpoint_memory.rs index e96b540..3c6e032 100644 --- a/src/endpoint_memory.rs +++ b/src/endpoint_memory.rs @@ -1,14 +1,16 @@ -use crate::target::{UsbAccessType, EP_MEM_ADDR, EP_MEM_SIZE, NUM_ENDPOINTS}; +use crate::target::{UsbAccessType, NUM_ENDPOINTS}; +use crate::UsbPeripheral; +use core::marker::PhantomData; use core::{mem, slice}; use usb_device::{Result, UsbError}; use vcell::VolatileCell; -const EP_MEM_PTR: *mut VolatileCell = EP_MEM_ADDR as *mut VolatileCell; - pub struct EndpointBuffer(&'static mut [VolatileCell]); impl EndpointBuffer { - pub fn new(offset_bytes: usize, size_bytes: usize) -> Self { + pub fn new(offset_bytes: usize, size_bytes: usize) -> Self { + let EP_MEM_PTR = USB::EP_MEMORY as *mut VolatileCell; + let mem = unsafe { slice::from_raw_parts_mut(EP_MEM_PTR.offset((offset_bytes >> 1) as isize), size_bytes >> 1) }; Self(mem) @@ -59,9 +61,9 @@ impl EndpointBuffer { } } - pub fn offset(&self) -> usize { + pub fn offset(&self) -> usize { let buffer_address = self.0.as_ptr() as usize; - let index = (buffer_address - EP_MEM_ADDR) / mem::size_of::(); + let index = (buffer_address - USB::EP_MEMORY as usize) / mem::size_of::(); index << 1 } @@ -78,32 +80,34 @@ pub struct BufferDescriptor { pub count_rx: VolatileCell, } -pub struct EndpointMemoryAllocator { +pub struct EndpointMemoryAllocator { next_free_offset: usize, + _marker: PhantomData, } -impl EndpointMemoryAllocator { +impl EndpointMemoryAllocator { pub fn new() -> Self { Self { next_free_offset: NUM_ENDPOINTS * 8, + _marker: PhantomData, } } pub fn allocate_buffer(&mut self, size: usize) -> Result { assert!(size & 1 == 0); - assert!(size < EP_MEM_SIZE); + assert!(size < USB::EP_MEMORY_SIZE); let offset = self.next_free_offset; - if offset as usize + size > EP_MEM_SIZE { + if offset as usize + size > USB::EP_MEMORY_SIZE { return Err(UsbError::EndpointMemoryOverflow); } self.next_free_offset += size; - Ok(EndpointBuffer::new(offset, size)) + Ok(EndpointBuffer::new::(offset, size)) } pub fn buffer_descriptor(index: u8) -> &'static BufferDescriptor { - unsafe { &*(EP_MEM_ADDR as *const BufferDescriptor).offset(index as isize) } + unsafe { &*(USB::EP_MEMORY as *const BufferDescriptor).offset(index as isize) } } } diff --git a/src/lib.rs b/src/lib.rs index a979d28..412d427 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,6 +24,12 @@ pub unsafe trait UsbPeripheral: Send + Sync { /// Embedded pull-up resistor on USB_DP line const DP_PULL_UP_FEATURE: bool; + /// Pointer to the endpoint memory + const EP_MEMORY: *const (); + + /// Endpoint memory size in bytes + const EP_MEMORY_SIZE: usize; + /// Enables USB device on its peripheral bus fn enable(); diff --git a/src/target.rs b/src/target.rs index 3c19b8d..e67cad2 100644 --- a/src/target.rs +++ b/src/target.rs @@ -5,14 +5,4 @@ pub type UsbAccessType = u32; #[cfg(feature = "ram_access_2x16")] pub type UsbAccessType = u16; -#[cfg(not(feature = "ram_addr_40006c00"))] -pub const EP_MEM_ADDR: usize = 0x4000_6000; -#[cfg(feature = "ram_addr_40006c00")] -pub const EP_MEM_ADDR: usize = 0x4000_6C00; - -#[cfg(feature = "ram_size_512")] -pub const EP_MEM_SIZE: usize = 512; -#[cfg(feature = "ram_size_1024")] -pub const EP_MEM_SIZE: usize = 1024; - pub const NUM_ENDPOINTS: usize = 8; From f652574845379ab794767e46643e12f33540c253 Mon Sep 17 00:00:00 2001 From: Vadim Kaushan Date: Fri, 8 Nov 2019 02:02:37 +0300 Subject: [PATCH 14/25] Use assert_eq --- src/endpoint_memory.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/endpoint_memory.rs b/src/endpoint_memory.rs index 3c6e032..2e44927 100644 --- a/src/endpoint_memory.rs +++ b/src/endpoint_memory.rs @@ -94,7 +94,7 @@ impl EndpointMemoryAllocator { } pub fn allocate_buffer(&mut self, size: usize) -> Result { - assert!(size & 1 == 0); + assert_eq!(size & 1, 0); assert!(size < USB::EP_MEMORY_SIZE); let offset = self.next_free_offset; From eebef0272f83e9fcbf0374ad7ef70ad845bc1a89 Mon Sep 17 00:00:00 2001 From: Vadim Kaushan Date: Fri, 8 Nov 2019 02:05:52 +0300 Subject: [PATCH 15/25] Remove old_gen and new_gen features --- Cargo.toml | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3fb5cf1..8c56808 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,18 +20,14 @@ ram_access_1x16 = [] ram_access_2x16 = [] # Known devices -stm32f042xx = ['new_gen'] -stm32f048xx = ['new_gen'] -stm32f072xx = ['new_gen'] -stm32f078xx = ['new_gen'] -stm32f103xx = ['old_gen'] -stm32f303xc = ['old_gen'] -stm32l0x2xx = ['new_gen'] -stm32l4x2xx = ['new_gen'] - -# Auxiliary features -old_gen = ['ram_access_1x16'] -new_gen = ['ram_access_2x16'] +stm32f042xx = ['ram_access_2x16'] +stm32f048xx = ['ram_access_2x16'] +stm32f072xx = ['ram_access_2x16'] +stm32f078xx = ['ram_access_2x16'] +stm32f103xx = ['ram_access_1x16'] +stm32f303xc = ['ram_access_1x16'] +stm32l0x2xx = ['ram_access_2x16'] +stm32l4x2xx = ['ram_access_2x16'] [package.metadata.docs.rs] features = ["stm32f103xx"] From c6b36d1cc444e77467fc535be94d1834da06b138 Mon Sep 17 00:00:00 2001 From: Vadim Kaushan Date: Fri, 8 Nov 2019 02:16:57 +0300 Subject: [PATCH 16/25] Fix warning --- src/endpoint_memory.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/endpoint_memory.rs b/src/endpoint_memory.rs index 2e44927..15f42a0 100644 --- a/src/endpoint_memory.rs +++ b/src/endpoint_memory.rs @@ -9,10 +9,10 @@ pub struct EndpointBuffer(&'static mut [VolatileCell]); impl EndpointBuffer { pub fn new(offset_bytes: usize, size_bytes: usize) -> Self { - let EP_MEM_PTR = USB::EP_MEMORY as *mut VolatileCell; + let ep_mem_ptr = USB::EP_MEMORY as *mut VolatileCell; let mem = - unsafe { slice::from_raw_parts_mut(EP_MEM_PTR.offset((offset_bytes >> 1) as isize), size_bytes >> 1) }; + unsafe { slice::from_raw_parts_mut(ep_mem_ptr.offset((offset_bytes >> 1) as isize), size_bytes >> 1) }; Self(mem) } From 6c1f9465dcaea6f0880e52f6301c633c8a1f40c2 Mon Sep 17 00:00:00 2001 From: Vadim Kaushan Date: Fri, 8 Nov 2019 02:17:42 +0300 Subject: [PATCH 17/25] Replace mem::uninitialized() with MaybeUninit --- src/bus.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/bus.rs b/src/bus.rs index 2f6945e..336aa39 100644 --- a/src/bus.rs +++ b/src/bus.rs @@ -1,6 +1,6 @@ //! USB peripheral driver. -use core::mem; +use core::mem::{self, MaybeUninit}; use cortex_m::interrupt::{self, Mutex}; use usb_device::bus::{PollResult, UsbBusAllocator}; use usb_device::endpoint::{EndpointAddress, EndpointType}; @@ -31,14 +31,15 @@ impl UsbBus { regs: Mutex::new(UsbRegisters::new()), ep_allocator: EndpointMemoryAllocator::new(), max_endpoint: 0, - endpoints: unsafe { - let mut endpoints: [Endpoint; NUM_ENDPOINTS] = mem::uninitialized(); + endpoints: { + let mut endpoints: [MaybeUninit>; NUM_ENDPOINTS] = + unsafe { MaybeUninit::uninit().assume_init() }; for i in 0..NUM_ENDPOINTS { - endpoints[i] = Endpoint::new(i as u8); + endpoints[i] = MaybeUninit::new(Endpoint::new(i as u8)); } - endpoints + unsafe { mem::transmute::<_, [Endpoint; NUM_ENDPOINTS]>(endpoints) } }, }; From a5a7e6c92cdf5a5fc9ebe509d38624a22aa4aca1 Mon Sep 17 00:00:00 2001 From: Vadim Kaushan Date: Fri, 8 Nov 2019 02:22:34 +0300 Subject: [PATCH 18/25] Move things around --- src/bus.rs | 3 +-- src/endpoint.rs | 5 +++-- src/endpoint_memory.rs | 7 ++++++- src/lib.rs | 1 - src/target.rs | 8 -------- 5 files changed, 10 insertions(+), 14 deletions(-) delete mode 100644 src/target.rs diff --git a/src/bus.rs b/src/bus.rs index 336aa39..931f916 100644 --- a/src/bus.rs +++ b/src/bus.rs @@ -6,10 +6,9 @@ use usb_device::bus::{PollResult, UsbBusAllocator}; use usb_device::endpoint::{EndpointAddress, EndpointType}; use usb_device::{Result, UsbDirection, UsbError}; -use crate::endpoint::{calculate_count_rx, Endpoint, EndpointStatus}; +use crate::endpoint::{calculate_count_rx, Endpoint, EndpointStatus, NUM_ENDPOINTS}; use crate::endpoint_memory::EndpointMemoryAllocator; use crate::registers::UsbRegisters; -use crate::target::NUM_ENDPOINTS; use crate::UsbPeripheral; /// USB peripheral driver for STM32 microcontrollers. diff --git a/src/endpoint.rs b/src/endpoint.rs index 14ec13e..22ed60f 100644 --- a/src/endpoint.rs +++ b/src/endpoint.rs @@ -1,6 +1,5 @@ -use crate::endpoint_memory::{BufferDescriptor, EndpointBuffer, EndpointMemoryAllocator}; +use crate::endpoint_memory::{BufferDescriptor, EndpointBuffer, EndpointMemoryAllocator, UsbAccessType}; use crate::registers::UsbRegisters; -use crate::target::UsbAccessType; use crate::UsbPeripheral; use core::marker::PhantomData; use core::mem; @@ -13,6 +12,8 @@ use usb_device::{Result, UsbError}; // compatible with definitions for older ones. pub use crate::pac::usb; +pub const NUM_ENDPOINTS: usize = 8; + /// Arbitrates access to the endpoint-specific registers and packet buffer memory. #[derive(Default)] pub struct Endpoint { diff --git a/src/endpoint_memory.rs b/src/endpoint_memory.rs index 15f42a0..ea62f86 100644 --- a/src/endpoint_memory.rs +++ b/src/endpoint_memory.rs @@ -1,10 +1,15 @@ -use crate::target::{UsbAccessType, NUM_ENDPOINTS}; +use crate::endpoint::NUM_ENDPOINTS; use crate::UsbPeripheral; use core::marker::PhantomData; use core::{mem, slice}; use usb_device::{Result, UsbError}; use vcell::VolatileCell; +#[cfg(feature = "ram_access_1x16")] +pub type UsbAccessType = u32; +#[cfg(feature = "ram_access_2x16")] +pub type UsbAccessType = u16; + pub struct EndpointBuffer(&'static mut [VolatileCell]); impl EndpointBuffer { diff --git a/src/lib.rs b/src/lib.rs index 412d427..95cb002 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,7 +9,6 @@ pub mod bus; mod endpoint; mod endpoint_memory; mod registers; -mod target; pub use crate::bus::UsbBus; mod pac; diff --git a/src/target.rs b/src/target.rs deleted file mode 100644 index e67cad2..0000000 --- a/src/target.rs +++ /dev/null @@ -1,8 +0,0 @@ -//! Target-specific definitions - -#[cfg(feature = "ram_access_1x16")] -pub type UsbAccessType = u32; -#[cfg(feature = "ram_access_2x16")] -pub type UsbAccessType = u16; - -pub const NUM_ENDPOINTS: usize = 8; From 292ccee5678722c8e0dcdf3a895661819f634a36 Mon Sep 17 00:00:00 2001 From: Vadim Kaushan Date: Fri, 8 Nov 2019 12:10:14 +0300 Subject: [PATCH 19/25] Check features --- src/lib.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 95cb002..8836c22 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,11 @@ #![no_std] +#[cfg(not(any(feature = "ram_access_1x16", feature = "ram_access_2x16")))] +compile_error!("This crate requires one of the ram_access features enabled"); +#[cfg(all(feature = "ram_access_1x16", feature = "ram_access_2x16"))] +compile_error!("Multiple ram_access features are specified. Only a single feature can be specified."); + pub mod bus; mod endpoint; mod endpoint_memory; From 33958c120a490e1bd51f5668dbb7e04dfe0697da Mon Sep 17 00:00:00 2001 From: Vadim Kaushan Date: Mon, 11 Nov 2019 18:01:48 +0300 Subject: [PATCH 20/25] Remove 'Known devices' features --- Cargo.toml | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8c56808..62484bf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,15 +19,5 @@ usb-device = "0.2.3" ram_access_1x16 = [] ram_access_2x16 = [] -# Known devices -stm32f042xx = ['ram_access_2x16'] -stm32f048xx = ['ram_access_2x16'] -stm32f072xx = ['ram_access_2x16'] -stm32f078xx = ['ram_access_2x16'] -stm32f103xx = ['ram_access_1x16'] -stm32f303xc = ['ram_access_1x16'] -stm32l0x2xx = ['ram_access_2x16'] -stm32l4x2xx = ['ram_access_2x16'] - [package.metadata.docs.rs] -features = ["stm32f103xx"] +features = ["ram_access_2x16"] From 33f7a2126b10c11606c26258bf855ae651efa9e8 Mon Sep 17 00:00:00 2001 From: Vadim Kaushan Date: Mon, 11 Nov 2019 18:19:05 +0300 Subject: [PATCH 21/25] Add hal example --- Cargo.toml | 7 +++++++ examples/hal.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 examples/hal.rs diff --git a/Cargo.toml b/Cargo.toml index 62484bf..4cc3196 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,9 @@ vcell = "0.1.2" cortex-m = "0.6.1" usb-device = "0.2.3" +[dev-dependencies] +stm32f1xx-hal = { version = "0.4.0", features = ["stm32f103"] } + [features] # USB RAM access scheme ram_access_1x16 = [] @@ -21,3 +24,7 @@ ram_access_2x16 = [] [package.metadata.docs.rs] features = ["ram_access_2x16"] + +[[example]] +name = "hal" +required-features = ["ram_access_1x16"] diff --git a/examples/hal.rs b/examples/hal.rs new file mode 100644 index 0000000..e76e97f --- /dev/null +++ b/examples/hal.rs @@ -0,0 +1,49 @@ +//! USB peripheral + +use stm32f1xx_hal::pac::{USB, RCC}; +use stm32_usbd::UsbPeripheral; + +use stm32f1xx_hal::gpio::gpioa::{PA11, PA12}; +use stm32f1xx_hal::gpio::{Floating, Input}; +pub use stm32_usbd::UsbBus; + +pub struct Peripheral { + pub usb: USB, + pub pin_dm: PA11>, + pub pin_dp: PA12>, +} + +unsafe impl Sync for Peripheral {} + +unsafe impl UsbPeripheral for Peripheral { + const REGISTERS: *const () = USB::ptr() as *const (); + const DP_PULL_UP_FEATURE: bool = false; + const EP_MEMORY: *const () = 0x4000_6000 as _; + const EP_MEMORY_SIZE: usize = 512; + + fn enable() { + let rcc = unsafe { (&*RCC::ptr()) }; + + cortex_m::interrupt::free(|_| { + // Enable USB peripheral + rcc.apb1enr.modify(|_, w| w.usben().set_bit()); + + // Reset USB peripheral + rcc.apb1rstr.modify(|_, w| w.usbrst().set_bit()); + rcc.apb1rstr.modify(|_, w| w.usbrst().clear_bit()); + }); + } + + fn startup_delay() { + // There is a chip specific startup delay. For STM32F103xx it's 1µs and this should wait for + // at least that long. + cortex_m::asm::delay(72); + } +} + +pub type UsbBusType = UsbBus; + + +fn main() -> ! { + loop {} +} From aa5d5eef3f609d05bc935da486577b15082fae59 Mon Sep 17 00:00:00 2001 From: Vadim Kaushan Date: Mon, 11 Nov 2019 18:21:25 +0300 Subject: [PATCH 22/25] Update README --- README.md | 55 ++++++++----------------------------------------------- 1 file changed, 8 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index 607b01f..bbf6647 100644 --- a/README.md +++ b/README.md @@ -21,57 +21,18 @@ This repository is a fork of the [mvirkkunen/stm32f103xx-usb](https://github.com * `STM32L4x2xx` * And others... -## Feature flags +## Usage -To use this crate you need to set proper feature flags. -All the feature flags are listed in `Cargo.toml` under different sections. - -If your target MCU matches one of the feature flags listed under the -`Known devices` section of `Cargo.toml`, you can use just that feature flag. For example: - -```toml -[dependencies] -stm32-usbd = { version = "0.3", features = ["stm32f103xx"] } -``` - -For other cases, you have to figure out different properties -of your MCU and USB peripheral implemented in it. -Each property has a corresponding feature flag that you should -set to indicate that property. - -Device family: -* `stm32f0` -* `stm32f1` -* `stm32f3` -* `stm32l0` -* `stm32l4` - -Size of dedicated packet buffer memory SRAM: -* `ram_size_512` -* `ram_size_1024` - -Dedicated packet buffer memory SRAM access scheme: +This driver is intended for use through a device hal library. +Such hal library should implement `UsbPeripheral` for the corresponding USB peripheral object. +This trait declares all the peripheral properties that may vary from one device family to the other. +Additionally, hal should pass `ram_access_1x16` of `ram_access_2x16` feature to the `stm32-usbd` library to +define endpoint memory access scheme: * `ram_access_1x16` - for "1x16 bits/word" access scheme * `ram_access_2x16` - for "2x16 bits/word" access scheme -USB peripheral features: -* `lpm_support` - USB 2.0 Link Power Management (LPM) support -* `bcd_support` - Battery Charging Detection (BCD) support -* `dp_pull_up_support` - Embedded pull-up resistor on USB_DP line - -Various hacks: -* `ram_addr_40006c00` if dedicated SRAM address is equal to `0x4000_6c00` -instead of `0x4000_6000` - -```toml -[dependencies] -# An example feature set for STM32F303CB MCU -stm32-usbd = { version = "0.3", features = ["stm32f3", "ram_size_512", "ram_access_1x16"] } -stm32f3xx-hal = { version = "0.1.4", features = ["rt", "stm32f3xx-hal/stm32f303"] } -``` - -Note that you also need to set the device feature for `stm32*-hal` crate. - ## Examples See the [stm32-usbd-examples](https://github.com/stm32-rs/stm32-usbd-examples) repo for different device-specific examples. + +See the `hal` example for the reference hal implementation. From 5ee7e3019402b5d687a61425dc8ca2d027d7b867 Mon Sep 17 00:00:00 2001 From: Vadim Kaushan Date: Mon, 11 Nov 2019 18:32:36 +0300 Subject: [PATCH 23/25] Update CI rules --- ci/script.sh | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/ci/script.sh b/ci/script.sh index 20869be..83d549a 100755 --- a/ci/script.sh +++ b/ci/script.sh @@ -2,11 +2,6 @@ set -euxo pipefail -cargo check --no-default-features --features stm32f042xx -#cargo check --no-default-features --features stm32f048xx -#cargo check --no-default-features --features stm32f072xx -#cargo check --no-default-features --features stm32f078xx -cargo check --no-default-features --features stm32f103xx -cargo check --no-default-features --features stm32f303xc -cargo check --no-default-features --features stm32l0x2xx -cargo check --no-default-features --features stm32l4x2xx +cargo check --features ram_access_1x16 +cargo check --features ram_access_2x16 +cargo check --example hal --features ram_access_1x16 From 7c3d4cd39c9e528e7f8447bce7674f1b5daec490 Mon Sep 17 00:00:00 2001 From: Vadim Kaushan Date: Mon, 11 Nov 2019 18:45:59 +0300 Subject: [PATCH 24/25] rustfmt --- examples/hal.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/examples/hal.rs b/examples/hal.rs index e76e97f..833cb8a 100644 --- a/examples/hal.rs +++ b/examples/hal.rs @@ -1,11 +1,11 @@ //! USB peripheral -use stm32f1xx_hal::pac::{USB, RCC}; use stm32_usbd::UsbPeripheral; +use stm32f1xx_hal::pac::{RCC, USB}; +pub use stm32_usbd::UsbBus; use stm32f1xx_hal::gpio::gpioa::{PA11, PA12}; use stm32f1xx_hal::gpio::{Floating, Input}; -pub use stm32_usbd::UsbBus; pub struct Peripheral { pub usb: USB, @@ -43,7 +43,6 @@ unsafe impl UsbPeripheral for Peripheral { pub type UsbBusType = UsbBus; - fn main() -> ! { loop {} } From b802384432cb3be843fa746d5c77de8bf0bfbbb7 Mon Sep 17 00:00:00 2001 From: Vadim Kaushan Date: Tue, 12 Nov 2019 18:38:11 +0300 Subject: [PATCH 25/25] Fix comment --- src/registers.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/registers.rs b/src/registers.rs index 51b2bc7..5df40c9 100644 --- a/src/registers.rs +++ b/src/registers.rs @@ -2,7 +2,7 @@ use crate::pac::usb::{RegisterBlock, EPR}; use crate::UsbPeripheral; use core::marker::PhantomData; -/// Wrapper around device-specific peripheral that provides unified register interface +/// A proxy type that provides unified register interface pub struct UsbRegisters { _marker: PhantomData, }