Skip to content

Commit 255b040

Browse files
authored
Merge pull request #28 from reitermarkus/critical-section
Use `critical_section` crate.
2 parents 6bed82a + d2b86ea commit 255b040

File tree

6 files changed

+34
-36
lines changed

6 files changed

+34
-36
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,11 @@ jobs:
2323
with:
2424
toolchain: ${{ matrix.rust }}
2525

26-
- name: Check code (cortex-m fs)
27-
run: cargo check --features "cortex-m fs"
26+
- name: Check code (fs)
27+
run: cargo check --features fs
2828

29-
- name: Check code (cortex-m hs)
30-
run: cargo check --features "cortex-m hs"
29+
- name: Check code (hs)
30+
run: cargo check --features hs
3131

32-
- name: Check code (cortex-m hs xcvrdly)
33-
run: cargo check --features "cortex-m hs xcvrdly"
34-
35-
- name: Check code (riscv fs)
36-
run: cargo check --features "riscv fs"
32+
- name: Check code (hs xcvrdly)
33+
run: cargo check --features "hs xcvrdly"

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77

88
## [Unreleased]
99

10+
11+
### Changed
12+
13+
* Use `critical-section` crate for critical sections.
14+
15+
1016
## [v0.4.0] - 2023-11-18
1117

1218
### Changed
19+
1320
* `usb-device` version bumped to v0.3.0
1421

1522

Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@ readme = "README.md"
1010
keywords = ["no-std", "embedded", "usb"]
1111

1212
[dependencies]
13-
riscv = { version = "0.6.0", optional = true }
14-
cortex-m = { version = "0.7.0", optional = true }
13+
critical-section = "1.0"
1514
embedded-hal = "0.2.4"
1615
vcell = "0.1.0"
1716
usb-device = "0.3"
1817

1918
[package.metadata.docs.rs]
20-
features = ['cortex-m', 'fs']
19+
features = ['fs']
2120

2221
[features]
2322
hs = []

src/bus.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ use crate::ral::{
33
};
44
use crate::transition::{EndpointConfig, EndpointDescriptor};
55
use core::marker::PhantomData;
6+
use critical_section::{CriticalSection, Mutex};
67
use embedded_hal::blocking::delay::DelayMs;
78
use usb_device::bus::{PollResult, UsbBusAllocator};
89
use usb_device::endpoint::{EndpointAddress, EndpointType};
910
use usb_device::{Result, UsbDirection, UsbError};
1011

1112
use crate::endpoint::{EndpointIn, EndpointOut};
1213
use crate::endpoint_memory::{EndpointBufferState, EndpointMemoryAllocator};
13-
use crate::target::interrupt::{self, CriticalSection, Mutex};
1414
use crate::target::UsbRegisters;
1515
use crate::{PhyType, UsbPeripheral};
1616

@@ -37,7 +37,7 @@ impl<USB: UsbPeripheral> UsbBus<USB> {
3737
self.peripheral
3838
}
3939

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

4343
// Rx FIFO
@@ -111,7 +111,7 @@ impl<USB: UsbPeripheral> UsbBus<USB> {
111111
}
112112
}
113113

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

117117
// disable interrupts
@@ -131,7 +131,7 @@ impl<USB: UsbPeripheral> UsbBus<USB> {
131131
}
132132

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

168-
interrupt::free(|cs| {
168+
critical_section::with(|cs| {
169169
let regs = self.regs.borrow(cs);
170170

171171
// Begin ULPI register read transaction
@@ -200,7 +200,7 @@ impl<USB: UsbPeripheral> UsbBus<USB> {
200200
panic!("ulpi_write is only supported with external ULPI PHYs");
201201
}
202202

203-
interrupt::free(|cs| {
203+
critical_section::with(|cs| {
204204
let regs = self.regs.borrow(cs);
205205

206206
// Begin ULPI register write transaction
@@ -367,7 +367,7 @@ impl<USB: UsbPeripheral> usb_device::bus::UsbBus for UsbBus<USB> {
367367
// Enable USB_OTG in RCC
368368
USB::enable();
369369

370-
interrupt::free(|cs| {
370+
critical_section::with(|cs| {
371371
let regs = self.regs.borrow(cs);
372372

373373
let core_id = read_reg!(otg_global, regs.global(), CID);
@@ -517,7 +517,7 @@ impl<USB: UsbPeripheral> usb_device::bus::UsbBus for UsbBus<USB> {
517517
}
518518

519519
fn reset(&self) {
520-
interrupt::free(|cs| {
520+
critical_section::with(|cs| {
521521
let regs = self.regs.borrow(cs);
522522

523523
self.configure_all(cs);
@@ -527,7 +527,7 @@ impl<USB: UsbPeripheral> usb_device::bus::UsbBus for UsbBus<USB> {
527527
}
528528

529529
fn set_device_address(&self, addr: u8) {
530-
interrupt::free(|cs| {
530+
critical_section::with(|cs| {
531531
let regs = self.regs.borrow(cs);
532532

533533
modify_reg!(otg_device, regs.device(), DCFG, DAD: addr as u32);
@@ -584,7 +584,7 @@ impl<USB: UsbPeripheral> usb_device::bus::UsbBus for UsbBus<USB> {
584584
}
585585

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

590590
let core_id = read_reg!(otg_global, regs.global(), CID);
@@ -703,7 +703,7 @@ impl<USB: UsbPeripheral> usb_device::bus::UsbBus for UsbBus<USB> {
703703

704704
if status == 0x02 || status == 0x06 {
705705
if let Some(ep) = &self.allocator.endpoints_out[epnum as usize] {
706-
let mut buffer = ep.buffer.borrow(cs).borrow_mut();
706+
let mut buffer = ep.buffer.borrow_ref_mut(cs);
707707
if buffer.state() == EndpointBufferState::Empty {
708708
read_reg!(otg_global, regs.global(), GRXSTSP); // pop GRXSTSP
709709

src/endpoint.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
use crate::endpoint_memory::{EndpointBuffer, EndpointBufferState};
22
use crate::ral::{endpoint0_out, endpoint_in, endpoint_out, modify_reg, read_reg, write_reg};
3-
use crate::target::interrupt::{self, CriticalSection, Mutex};
43
use crate::target::{fifo_write, UsbRegisters};
54
use crate::transition::EndpointDescriptor;
65
use crate::UsbPeripheral;
76
use core::cell::RefCell;
87
use core::ops::{Deref, DerefMut};
8+
use critical_section::{CriticalSection, Mutex};
99
use usb_device::endpoint::EndpointAddress;
1010
use usb_device::{Result, UsbDirection, UsbError};
1111

1212
pub fn set_stalled(usb: UsbRegisters, address: EndpointAddress, stalled: bool) {
13-
interrupt::free(|_| match address.direction() {
13+
critical_section::with(|_| match address.direction() {
1414
UsbDirection::Out => {
1515
let ep = usb.endpoint_out(address.index() as usize);
1616
modify_reg!(endpoint_out, ep, DOEPCTL, STALL: stalled as u32);
@@ -71,7 +71,7 @@ impl EndpointIn {
7171
}
7272
}
7373

74-
pub fn configure(&self, _cs: &CriticalSection) {
74+
pub fn configure(&self, _cs: CriticalSection<'_>) {
7575
if self.index() == 0 {
7676
let mpsiz = match self.descriptor.max_packet_size {
7777
8 => 0b11,
@@ -97,7 +97,7 @@ impl EndpointIn {
9797
}
9898
}
9999

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

103103
// deactivating endpoint
@@ -163,7 +163,7 @@ impl EndpointOut {
163163
}
164164
}
165165

166-
pub fn configure(&self, _cs: &CriticalSection) {
166+
pub fn configure(&self, _cs: CriticalSection<'_>) {
167167
if self.index() == 0 {
168168
let mpsiz = match self.descriptor.max_packet_size {
169169
8 => 0b11,
@@ -189,7 +189,7 @@ impl EndpointOut {
189189
}
190190
}
191191

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

195195
// deactivating endpoint
@@ -205,11 +205,11 @@ impl EndpointOut {
205205
}
206206

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

211211
pub fn buffer_state(&self) -> EndpointBufferState {
212-
interrupt::free(|cs| self.buffer.borrow(cs).borrow().state())
212+
critical_section::with(|cs| self.buffer.borrow_ref(cs).state())
213213
}
214214
}
215215

src/target.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22
33
use vcell::VolatileCell;
44

5-
#[cfg(feature = "cortex-m")]
6-
pub use cortex_m::interrupt;
7-
#[cfg(feature = "riscv")]
8-
pub use riscv::interrupt;
9-
105
use crate::ral::register::RWRegister;
116
use crate::ral::{
127
endpoint0_out, endpoint_in, endpoint_out, otg_device, otg_global, otg_global_dieptxfx,

0 commit comments

Comments
 (0)