From ff9c7e533471d28240158d00e3b9c5dcc33b82b3 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Tue, 14 Jun 2022 21:13:26 +0200 Subject: [PATCH 1/2] Use `critical_section` crate for critical sections. --- CHANGELOG.md | 7 +++++++ Cargo.toml | 5 ++--- src/bus.rs | 22 +++++++++++----------- src/endpoint.rs | 16 ++++++++-------- src/target.rs | 5 ----- 5 files changed, 28 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c8adf45..43ffdcc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,9 +7,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] + +### Changed + +* Use `critical-section` crate for critical sections. + + ## [v0.4.0] - 2023-11-18 ### Changed + * `usb-device` version bumped to v0.3.0 diff --git a/Cargo.toml b/Cargo.toml index 05713fd..4e9099d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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.3" [package.metadata.docs.rs] -features = ['cortex-m', 'fs'] +features = ['fs'] [features] hs = [] diff --git a/src/bus.rs b/src/bus.rs index eaa4a0b..7e484d2 100644 --- a/src/bus.rs +++ b/src/bus.rs @@ -3,6 +3,7 @@ use crate::ral::{ }; use crate::transition::{EndpointConfig, EndpointDescriptor}; use core::marker::PhantomData; +use critical_section::{CriticalSection, Mutex}; use embedded_hal::blocking::delay::DelayMs; use usb_device::bus::{PollResult, UsbBusAllocator}; use usb_device::endpoint::{EndpointAddress, EndpointType}; @@ -10,7 +11,6 @@ use usb_device::{Result, UsbDirection, UsbError}; use crate::endpoint::{EndpointIn, EndpointOut}; use crate::endpoint_memory::{EndpointBufferState, EndpointMemoryAllocator}; -use crate::target::interrupt::{self, CriticalSection, Mutex}; use crate::target::UsbRegisters; use crate::{PhyType, UsbPeripheral}; @@ -37,7 +37,7 @@ impl UsbBus { self.peripheral } - fn configure_all(&self, cs: &CriticalSection) { + fn configure_all(&self, cs: CriticalSection<'_>) { let regs = self.regs.borrow(cs); // Rx FIFO @@ -111,7 +111,7 @@ impl UsbBus { } } - fn deconfigure_all(&self, cs: &CriticalSection) { + fn deconfigure_all(&self, cs: CriticalSection<'_>) { let regs = self.regs.borrow(cs); // disable interrupts @@ -131,7 +131,7 @@ impl UsbBus { } pub fn force_reset(&self, delay: &mut impl DelayMs) -> 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); @@ -165,7 +165,7 @@ impl UsbBus { 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 @@ -200,7 +200,7 @@ impl UsbBus { 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 @@ -367,7 +367,7 @@ impl usb_device::bus::UsbBus for UsbBus { // 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); @@ -517,7 +517,7 @@ impl usb_device::bus::UsbBus for UsbBus { } fn reset(&self) { - interrupt::free(|cs| { + critical_section::with(|cs| { let regs = self.regs.borrow(cs); self.configure_all(cs); @@ -527,7 +527,7 @@ impl usb_device::bus::UsbBus for UsbBus { } 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); @@ -584,7 +584,7 @@ impl usb_device::bus::UsbBus for UsbBus { } 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); @@ -703,7 +703,7 @@ impl usb_device::bus::UsbBus for UsbBus { if status == 0x02 || status == 0x06 { if let Some(ep) = &self.allocator.endpoints_out[epnum as usize] { - let mut buffer = ep.buffer.borrow(cs).borrow_mut(); + let mut buffer = ep.buffer.borrow_ref_mut(cs); if buffer.state() == EndpointBufferState::Empty { read_reg!(otg_global, regs.global(), GRXSTSP); // pop GRXSTSP diff --git a/src/endpoint.rs b/src/endpoint.rs index 8eb3a7d..c6e3be1 100644 --- a/src/endpoint.rs +++ b/src/endpoint.rs @@ -1,16 +1,16 @@ use crate::endpoint_memory::{EndpointBuffer, EndpointBufferState}; use crate::ral::{endpoint0_out, endpoint_in, endpoint_out, modify_reg, read_reg, write_reg}; -use crate::target::interrupt::{self, CriticalSection, Mutex}; use crate::target::{fifo_write, UsbRegisters}; use crate::transition::EndpointDescriptor; use crate::UsbPeripheral; use core::cell::RefCell; use core::ops::{Deref, DerefMut}; +use critical_section::{CriticalSection, Mutex}; use usb_device::endpoint::EndpointAddress; use usb_device::{Result, UsbDirection, UsbError}; pub fn set_stalled(usb: UsbRegisters, address: EndpointAddress, stalled: bool) { - interrupt::free(|_| match address.direction() { + critical_section::with(|_| match address.direction() { UsbDirection::Out => { let ep = usb.endpoint_out(address.index() as usize); modify_reg!(endpoint_out, ep, DOEPCTL, STALL: stalled as u32); @@ -71,7 +71,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, @@ -97,7 +97,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 @@ -163,7 +163,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, @@ -189,7 +189,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 @@ -205,11 +205,11 @@ impl EndpointOut { } pub fn read(&self, buf: &mut [u8]) -> Result { - interrupt::free(|cs| self.buffer.borrow(cs).borrow_mut().read_packet(buf)) + critical_section::with(|cs| self.buffer.borrow_ref_mut(cs).read_packet(buf)) } pub fn buffer_state(&self) -> EndpointBufferState { - interrupt::free(|cs| self.buffer.borrow(cs).borrow().state()) + critical_section::with(|cs| self.buffer.borrow_ref(cs).state()) } } diff --git a/src/target.rs b/src/target.rs index 71fa8dd..728c2b6 100644 --- a/src/target.rs +++ b/src/target.rs @@ -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::register::RWRegister; use crate::ral::{ endpoint0_out, endpoint_in, endpoint_out, otg_device, otg_global, otg_global_dieptxfx, From d2b86ead3da49cc0c1eba9a69a5615c24ab6db9f Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Wed, 22 Nov 2023 23:39:49 +0100 Subject: [PATCH 2/2] Fix CI matrix. --- .github/workflows/ci.yml | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7d888e6..d7967d9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,14 +23,11 @@ jobs: with: toolchain: ${{ matrix.rust }} - - name: Check code (cortex-m fs) - run: cargo check --features "cortex-m fs" + - name: Check code (fs) + run: cargo check --features fs - - name: Check code (cortex-m hs) - run: cargo check --features "cortex-m hs" + - name: Check code (hs) + run: cargo check --features hs - - name: Check code (cortex-m hs xcvrdly) - run: cargo check --features "cortex-m hs xcvrdly" - - - name: Check code (riscv fs) - run: cargo check --features "riscv fs" + - name: Check code (hs xcvrdly) + run: cargo check --features "hs xcvrdly"