Skip to content

Commit

Permalink
Update to use critical_section crate.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed Aug 23, 2022
1 parent b53dbe7 commit 988c6c8
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 27 deletions.
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ readme = "README.md"
keywords = ["no-std", "embedded", "usb"]

[dependencies]
riscv = { version = "0.6.0", optional = true }
cortex-m = { version = "0.7.0", optional = true }
critical-section = "1.0"
embedded-hal = "0.2.4"
vcell = "0.1.0"
usb-device = "0.2.3"

[package.metadata.docs.rs]
features = ['cortex-m', 'fs']
features = ['fs']

[features]
hs = []
Expand Down
22 changes: 11 additions & 11 deletions src/bus.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use core::marker::PhantomData;
use critical_section::{Mutex, CriticalSection};
use embedded_hal::blocking::delay::DelayMs;
use usb_device::{Result, UsbDirection, UsbError};
use usb_device::bus::{UsbBusAllocator, PollResult};
use usb_device::endpoint::{EndpointType, EndpointAddress};

use crate::transition::{EndpointConfig, EndpointDescriptor};
use crate::ral::{read_reg, write_reg, modify_reg, otg_global, otg_device, otg_pwrclk, otg_global_dieptxfx};

use crate::target::UsbRegisters;
use crate::target::interrupt::{self, Mutex, CriticalSection};
use crate::endpoint::{EndpointIn, EndpointOut};
use crate::endpoint_memory::{EndpointMemoryAllocator, EndpointBufferState};
use crate::{UsbPeripheral, PhyType};
Expand Down Expand Up @@ -35,7 +35,7 @@ impl<USB: UsbPeripheral> UsbBus<USB> {
self.peripheral
}

fn configure_all(&self, cs: &CriticalSection) {
fn configure_all(&self, cs: CriticalSection<'_>) {
let regs = self.regs.borrow(cs);

// Rx FIFO
Expand Down Expand Up @@ -108,7 +108,7 @@ impl<USB: UsbPeripheral> UsbBus<USB> {
}
}

fn deconfigure_all(&self, cs: &CriticalSection) {
fn deconfigure_all(&self, cs: CriticalSection<'_>) {
let regs = self.regs.borrow(cs);

// disable interrupts
Expand All @@ -128,7 +128,7 @@ impl<USB: UsbPeripheral> UsbBus<USB> {
}

pub fn force_reset(&self, delay: &mut impl DelayMs<u32>) -> Result<()> {
interrupt::free(|cs| {
critical_section::with(|cs| {
let regs = self.regs.borrow(cs);
write_reg!(otg_device, regs.device(), DCTL, SDIS: 1); // Soft disconnect
delay.delay_ms(3);
Expand Down Expand Up @@ -162,7 +162,7 @@ impl<USB: UsbPeripheral> UsbBus<USB> {
panic!("ulpi_read is only supported with external ULPI PHYs");
}

interrupt::free(|cs| {
critical_section::with(|cs| {
let regs = self.regs.borrow(cs);

// Begin ULPI register read transaction
Expand Down Expand Up @@ -197,7 +197,7 @@ impl<USB: UsbPeripheral> UsbBus<USB> {
panic!("ulpi_write is only supported with external ULPI PHYs");
}

interrupt::free(|cs| {
critical_section::with(|cs| {
let regs = self.regs.borrow(cs);

// Begin ULPI register write transaction
Expand Down Expand Up @@ -356,7 +356,7 @@ impl<USB: UsbPeripheral> usb_device::bus::UsbBus for UsbBus<USB> {
// Enable USB_OTG in RCC
USB::enable();

interrupt::free(|cs| {
critical_section::with(|cs| {
let regs = self.regs.borrow(cs);

let core_id = read_reg!(otg_global, regs.global(), CID);
Expand Down Expand Up @@ -506,7 +506,7 @@ impl<USB: UsbPeripheral> usb_device::bus::UsbBus for UsbBus<USB> {
}

fn reset(&self) {
interrupt::free(|cs| {
critical_section::with(|cs| {
let regs = self.regs.borrow(cs);

self.configure_all(cs);
Expand All @@ -516,7 +516,7 @@ impl<USB: UsbPeripheral> usb_device::bus::UsbBus for UsbBus<USB> {
}

fn set_device_address(&self, addr: u8) {
interrupt::free(|cs| {
critical_section::with(|cs| {
let regs = self.regs.borrow(cs);

modify_reg!(otg_device, regs.device(), DCFG, DAD: addr as u32);
Expand Down Expand Up @@ -573,7 +573,7 @@ impl<USB: UsbPeripheral> usb_device::bus::UsbBus for UsbBus<USB> {
}

fn poll(&self) -> PollResult {
interrupt::free(|cs| {
critical_section::with(|cs| {
let regs = self.regs.borrow(cs);

let core_id = read_reg!(otg_global, regs.global(), CID);
Expand Down
17 changes: 9 additions & 8 deletions src/endpoint.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use critical_section::{CriticalSection, Mutex};
use usb_device::{Result, UsbError, UsbDirection};
use usb_device::endpoint::EndpointAddress;

use crate::endpoint_memory::{EndpointBuffer, EndpointBufferState};
use crate::ral::{read_reg, write_reg, modify_reg, endpoint_in, endpoint_out, endpoint0_out};
use crate::target::{fifo_write, UsbRegisters};
use crate::target::interrupt::{self, CriticalSection, Mutex};
use core::ops::{Deref, DerefMut};
use core::cell::RefCell;
use crate::transition::EndpointDescriptor;
use crate::UsbPeripheral;

pub fn set_stalled(usb: UsbRegisters, address: EndpointAddress, stalled: bool) {
interrupt::free(|_| {
critical_section::with(|_| {
match address.direction() {
UsbDirection::Out => {
let ep = usb.endpoint_out(address.index() as usize);
Expand Down Expand Up @@ -74,7 +75,7 @@ impl EndpointIn {
}
}

pub fn configure(&self, _cs: &CriticalSection) {
pub fn configure(&self, _cs: CriticalSection<'_>) {
if self.index() == 0 {
let mpsiz = match self.descriptor.max_packet_size {
8 => 0b11,
Expand All @@ -100,7 +101,7 @@ impl EndpointIn {
}
}

pub fn deconfigure(&self, _cs: &CriticalSection) {
pub fn deconfigure(&self, _cs: CriticalSection<'_>) {
let regs = self.usb.endpoint_in(self.index() as usize);

// deactivating endpoint
Expand Down Expand Up @@ -163,7 +164,7 @@ impl EndpointOut {
}
}

pub fn configure(&self, _cs: &CriticalSection) {
pub fn configure(&self, _cs: CriticalSection<'_>) {
if self.index() == 0 {
let mpsiz = match self.descriptor.max_packet_size {
8 => 0b11,
Expand All @@ -189,7 +190,7 @@ impl EndpointOut {
}
}

pub fn deconfigure(&self, _cs: &CriticalSection) {
pub fn deconfigure(&self, _cs: CriticalSection<'_>) {
let regs = self.usb.endpoint_out(self.index() as usize);

// deactivating endpoint
Expand All @@ -205,13 +206,13 @@ impl EndpointOut {
}

pub fn read(&self, buf: &mut [u8]) -> Result<usize> {
interrupt::free(|cs| {
critical_section::with(|cs| {
self.buffer.borrow(cs).borrow_mut().read_packet(buf)
})
}

pub fn buffer_state(&self) -> EndpointBufferState {
interrupt::free(|cs| {
critical_section::with(|cs| {
self.buffer.borrow(cs).borrow().state()
})
}
Expand Down
5 changes: 0 additions & 5 deletions src/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

use vcell::VolatileCell;

#[cfg(feature = "cortex-m")]
pub use cortex_m::interrupt;
#[cfg(feature = "riscv")]
pub use riscv::interrupt;

use crate::ral::{otg_global, otg_device, otg_pwrclk, otg_global_dieptxfx, endpoint_in, endpoint0_out, endpoint_out};
use crate::UsbPeripheral;
use crate::ral::register::RWRegister;
Expand Down

0 comments on commit 988c6c8

Please sign in to comment.