Skip to content

Commit 4f1e219

Browse files
authored
Merge pull request #40 from stm32-rs/rustfmt
Enforce rustfmt
2 parents fed133d + ee50414 commit 4f1e219

File tree

8 files changed

+153
-117
lines changed

8 files changed

+153
-117
lines changed

.github/workflows/rustfmt.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
on:
2+
push:
3+
branches: [ staging, trying, master ]
4+
pull_request:
5+
6+
name: Code formatting check
7+
8+
jobs:
9+
fmt:
10+
name: Rustfmt
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
- uses: dtolnay/rust-toolchain@stable
15+
with:
16+
components: rustfmt
17+
- run: cargo fmt --all -- --check

src/bus.rs

Lines changed: 71 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1+
use crate::ral::{
2+
modify_reg, otg_device, otg_global, otg_global_dieptxfx, otg_pwrclk, read_reg, write_reg,
3+
};
4+
use crate::transition::{EndpointConfig, EndpointDescriptor};
15
use core::marker::PhantomData;
26
use embedded_hal::blocking::delay::DelayMs;
7+
use usb_device::bus::{PollResult, UsbBusAllocator};
8+
use usb_device::endpoint::{EndpointAddress, EndpointType};
39
use usb_device::{Result, UsbDirection, UsbError};
4-
use usb_device::bus::{UsbBusAllocator, PollResult};
5-
use usb_device::endpoint::{EndpointType, EndpointAddress};
6-
use crate::transition::{EndpointConfig, EndpointDescriptor};
7-
use crate::ral::{read_reg, write_reg, modify_reg, otg_global, otg_device, otg_pwrclk, otg_global_dieptxfx};
810

9-
use crate::target::UsbRegisters;
10-
use crate::target::interrupt::{self, Mutex, CriticalSection};
1111
use crate::endpoint::{EndpointIn, EndpointOut};
12-
use crate::endpoint_memory::{EndpointMemoryAllocator, EndpointBufferState};
13-
use crate::{UsbPeripheral, PhyType};
12+
use crate::endpoint_memory::{EndpointBufferState, EndpointMemoryAllocator};
13+
use crate::target::interrupt::{self, CriticalSection, Mutex};
14+
use crate::target::UsbRegisters;
15+
use crate::{PhyType, UsbPeripheral};
1416

1517
/// USB peripheral driver for STM32 microcontrollers.
1618
pub struct UsbBus<USB> {
@@ -90,7 +92,8 @@ impl<USB: UsbPeripheral> UsbBus<USB> {
9092
for ep in &self.allocator.endpoints_in {
9193
if let Some(ep) = ep {
9294
// enabling EP TX interrupt
93-
modify_reg!(otg_device, regs.device(), DAINTMSK, |v| v | (0x0001 << ep.address().index()));
95+
modify_reg!(otg_device, regs.device(), DAINTMSK, |v| v
96+
| (0x0001 << ep.address().index()));
9497

9598
ep.configure(cs);
9699
}
@@ -249,7 +252,7 @@ impl<USB: UsbPeripheral> EndpointAllocator<USB> {
249252
endpoints_in: [None, None, None, None, None, None, None, None, None],
250253
endpoints_out: [None, None, None, None, None, None, None, None, None],
251254
memory_allocator: EndpointMemoryAllocator::new(memory),
252-
_marker: PhantomData
255+
_marker: PhantomData,
253256
}
254257
}
255258

@@ -269,28 +272,33 @@ impl<USB: UsbPeripheral> EndpointAllocator<USB> {
269272
for number in 1..USB::ENDPOINT_COUNT {
270273
if *bitmap & (1 << number) == 0 {
271274
*bitmap |= 1 << number;
272-
return Ok(number as u8)
275+
return Ok(number as u8);
273276
}
274277
}
275278
Err(UsbError::EndpointOverflow)
276279
}
277280
}
278281

279-
fn alloc(bitmap: &mut u8, config: &EndpointConfig, direction: UsbDirection) -> Result<EndpointDescriptor> {
282+
fn alloc(
283+
bitmap: &mut u8,
284+
config: &EndpointConfig,
285+
direction: UsbDirection,
286+
) -> Result<EndpointDescriptor> {
280287
let number = Self::alloc_number(bitmap, config.number)?;
281288
let address = EndpointAddress::from_parts(number as usize, direction);
282289
Ok(EndpointDescriptor {
283290
address,
284291
ep_type: config.ep_type,
285292
max_packet_size: config.max_packet_size,
286-
interval: config.interval
293+
interval: config.interval,
287294
})
288295
}
289296

290297
fn alloc_in(&mut self, config: &EndpointConfig) -> Result<EndpointIn> {
291298
let descr = Self::alloc(&mut self.bitmap_in, config, UsbDirection::In)?;
292299

293-
self.memory_allocator.allocate_tx_buffer(descr.address.index() as u8, descr.max_packet_size as usize)?;
300+
self.memory_allocator
301+
.allocate_tx_buffer(descr.address.index() as u8, descr.max_packet_size as usize)?;
294302
let ep = EndpointIn::new::<USB>(descr);
295303

296304
Ok(ep)
@@ -299,7 +307,9 @@ impl<USB: UsbPeripheral> EndpointAllocator<USB> {
299307
fn alloc_out(&mut self, config: &EndpointConfig) -> Result<EndpointOut> {
300308
let descr = Self::alloc(&mut self.bitmap_out, config, UsbDirection::Out)?;
301309

302-
let buffer = self.memory_allocator.allocate_rx_buffer(descr.max_packet_size as usize)?;
310+
let buffer = self
311+
.memory_allocator
312+
.allocate_rx_buffer(descr.max_packet_size as usize)?;
303313
let ep = EndpointOut::new::<USB>(descr, buffer);
304314

305315
Ok(ep)
@@ -311,8 +321,8 @@ impl<USB: UsbPeripheral> EndpointAllocator<USB> {
311321
ep_addr: Option<EndpointAddress>,
312322
ep_type: EndpointType,
313323
max_packet_size: u16,
314-
interval: u8) -> Result<EndpointAddress>
315-
{
324+
interval: u8,
325+
) -> Result<EndpointAddress> {
316326
let ep_type = unsafe { core::mem::transmute(ep_type) };
317327
let number = ep_addr.map(|a| a.index() as u8);
318328

@@ -321,21 +331,21 @@ impl<USB: UsbPeripheral> EndpointAllocator<USB> {
321331
max_packet_size,
322332
interval,
323333
number,
324-
pair_of: None
334+
pair_of: None,
325335
};
326336
match ep_dir {
327337
UsbDirection::Out => {
328338
let ep = self.alloc_out(&config)?;
329339
let address = ep.address();
330340
self.endpoints_out[address.index()] = Some(ep);
331341
Ok(address)
332-
},
342+
}
333343
UsbDirection::In => {
334344
let ep = self.alloc_in(&config)?;
335345
let address = ep.address();
336346
self.endpoints_in[address.index()] = Some(ep);
337347
Ok(address)
338-
},
348+
}
339349
}
340350
}
341351
}
@@ -347,9 +357,10 @@ impl<USB: UsbPeripheral> usb_device::bus::UsbBus for UsbBus<USB> {
347357
ep_addr: Option<EndpointAddress>,
348358
ep_type: EndpointType,
349359
max_packet_size: u16,
350-
interval: u8) -> Result<EndpointAddress>
351-
{
352-
self.allocator.alloc_ep(ep_dir, ep_addr, ep_type, max_packet_size, interval)
360+
interval: u8,
361+
) -> Result<EndpointAddress> {
362+
self.allocator
363+
.alloc_ep(ep_dir, ep_addr, ep_type, max_packet_size, interval)
353364
}
354365

355366
fn enable(&mut self) {
@@ -383,7 +394,7 @@ impl<USB: UsbPeripheral> usb_device::bus::UsbBus for UsbBus<USB> {
383394
PhyType::InternalFullSpeed => {
384395
// Select FS Embedded PHY
385396
modify_reg!(otg_global, regs.global(), GUSBCFG, PHYSEL: 1);
386-
},
397+
}
387398
PhyType::InternalHighSpeed => {
388399
// Turn off PHY
389400
modify_reg!(otg_global, regs.global(), GCCFG, PWRDWN: 0);
@@ -578,8 +589,16 @@ impl<USB: UsbPeripheral> usb_device::bus::UsbBus for UsbBus<USB> {
578589

579590
let core_id = read_reg!(otg_global, regs.global(), CID);
580591

581-
let (wakeup, suspend, enum_done, reset, iep, rxflvl) = read_reg!(otg_global, regs.global(), GINTSTS,
582-
WKUPINT, USBSUSP, ENUMDNE, USBRST, IEPINT, RXFLVL
592+
let (wakeup, suspend, enum_done, reset, iep, rxflvl) = read_reg!(
593+
otg_global,
594+
regs.global(),
595+
GINTSTS,
596+
WKUPINT,
597+
USBSUSP,
598+
ENUMDNE,
599+
USBRST,
600+
IEPINT,
601+
RXFLVL
583602
);
584603

585604
if reset != 0 {
@@ -628,7 +647,7 @@ impl<USB: UsbPeripheral> usb_device::bus::UsbBus for UsbBus<USB> {
628647
32_000_000..=u32::MAX => 0x6,
629648
};
630649
}
631-
_ => unimplemented!()
650+
_ => unimplemented!(),
632651
}
633652
modify_reg!(otg_global, regs.global(), GUSBCFG, TRDT: trdt);
634653

@@ -651,12 +670,15 @@ impl<USB: UsbPeripheral> usb_device::bus::UsbBus for UsbBus<USB> {
651670

652671
// RXFLVL & IEPINT flags are read-only, there is no need to clear them
653672
if rxflvl != 0 {
654-
let (epnum, data_size, status) = read_reg!(otg_global, regs.global(), GRXSTSR, EPNUM, BCNT, PKTSTS);
673+
let (epnum, data_size, status) =
674+
read_reg!(otg_global, regs.global(), GRXSTSR, EPNUM, BCNT, PKTSTS);
655675
match status {
656-
0x02 => { // OUT received
676+
0x02 => {
677+
// OUT received
657678
ep_out |= 1 << epnum;
658679
}
659-
0x06 => { // SETUP received
680+
0x06 => {
681+
// SETUP received
660682
// flushing TX if something stuck in control endpoint
661683
let ep = regs.endpoint_in(epnum as usize);
662684
if read_reg!(endpoint_in, ep, DIEPTSIZ, PKTCNT) != 0 {
@@ -665,7 +687,8 @@ impl<USB: UsbPeripheral> usb_device::bus::UsbBus for UsbBus<USB> {
665687
}
666688
ep_setup |= 1 << epnum;
667689
}
668-
0x03 | 0x04 => { // OUT completed | SETUP completed
690+
0x03 | 0x04 => {
691+
// OUT completed | SETUP completed
669692
// Re-enable the endpoint, F429-like chips only
670693
if core_id == 0x0000_1200 || core_id == 0x0000_1100 {
671694
let ep = regs.endpoint_out(epnum as usize);
@@ -685,12 +708,17 @@ impl<USB: UsbPeripheral> usb_device::bus::UsbBus for UsbBus<USB> {
685708
read_reg!(otg_global, regs.global(), GRXSTSP); // pop GRXSTSP
686709

687710
let is_setup = status == 0x06;
688-
buffer.fill_from_fifo(*regs, data_size as u16, is_setup).ok();
711+
buffer
712+
.fill_from_fifo(*regs, data_size as u16, is_setup)
713+
.ok();
689714

690715
// Re-enable the endpoint, F446-like chips only
691-
if core_id == 0x0000_2000 || core_id == 0x0000_2100 ||
692-
core_id == 0x0000_2300 ||
693-
core_id == 0x0000_3000 || core_id == 0x0000_3100 {
716+
if core_id == 0x0000_2000
717+
|| core_id == 0x0000_2100
718+
|| core_id == 0x0000_2300
719+
|| core_id == 0x0000_3000
720+
|| core_id == 0x0000_3100
721+
{
694722
let ep = regs.endpoint_out(epnum as usize);
695723
modify_reg!(endpoint_out, ep, DOEPCTL, CNAK: 1, EPENA: 1);
696724
}
@@ -716,17 +744,21 @@ impl<USB: UsbPeripheral> usb_device::bus::UsbBus for UsbBus<USB> {
716744
match ep.buffer_state() {
717745
EndpointBufferState::DataOut => {
718746
ep_out |= 1 << ep.address().index();
719-
},
747+
}
720748
EndpointBufferState::DataSetup => {
721749
ep_setup |= 1 << ep.address().index();
722-
},
723-
EndpointBufferState::Empty => {},
750+
}
751+
EndpointBufferState::Empty => {}
724752
}
725753
}
726754
}
727755

728756
if (ep_in_complete | ep_out | ep_setup) != 0 {
729-
PollResult::Data { ep_out, ep_in_complete, ep_setup }
757+
PollResult::Data {
758+
ep_out,
759+
ep_in_complete,
760+
ep_setup,
761+
}
730762
} else {
731763
PollResult::None
732764
}

src/endpoint.rs

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
1-
use usb_device::{Result, UsbError, UsbDirection};
2-
use usb_device::endpoint::EndpointAddress;
31
use crate::endpoint_memory::{EndpointBuffer, EndpointBufferState};
4-
use crate::ral::{read_reg, write_reg, modify_reg, endpoint_in, endpoint_out, endpoint0_out};
5-
use crate::target::{fifo_write, UsbRegisters};
2+
use crate::ral::{endpoint0_out, endpoint_in, endpoint_out, modify_reg, read_reg, write_reg};
63
use crate::target::interrupt::{self, CriticalSection, Mutex};
7-
use core::ops::{Deref, DerefMut};
8-
use core::cell::RefCell;
4+
use crate::target::{fifo_write, UsbRegisters};
95
use crate::transition::EndpointDescriptor;
106
use crate::UsbPeripheral;
7+
use core::cell::RefCell;
8+
use core::ops::{Deref, DerefMut};
9+
use usb_device::endpoint::EndpointAddress;
10+
use usb_device::{Result, UsbDirection, UsbError};
1111

1212
pub fn set_stalled(usb: UsbRegisters, address: EndpointAddress, stalled: bool) {
13-
interrupt::free(|_| {
14-
match address.direction() {
15-
UsbDirection::Out => {
16-
let ep = usb.endpoint_out(address.index() as usize);
17-
modify_reg!(endpoint_out, ep, DOEPCTL, STALL: stalled as u32);
18-
},
19-
UsbDirection::In => {
20-
let ep = usb.endpoint_in(address.index() as usize);
21-
modify_reg!(endpoint_in, ep, DIEPCTL, STALL: stalled as u32);
22-
},
13+
interrupt::free(|_| match address.direction() {
14+
UsbDirection::Out => {
15+
let ep = usb.endpoint_out(address.index() as usize);
16+
modify_reg!(endpoint_out, ep, DOEPCTL, STALL: stalled as u32);
17+
}
18+
UsbDirection::In => {
19+
let ep = usb.endpoint_in(address.index() as usize);
20+
modify_reg!(endpoint_in, ep, DIEPCTL, STALL: stalled as u32);
2321
}
2422
})
2523
}
@@ -29,11 +27,11 @@ pub fn is_stalled(usb: UsbRegisters, address: EndpointAddress) -> bool {
2927
UsbDirection::Out => {
3028
let ep = usb.endpoint_out(address.index());
3129
read_reg!(endpoint_out, ep, DOEPCTL, STALL)
32-
},
30+
}
3331
UsbDirection::In => {
3432
let ep = usb.endpoint_in(address.index());
3533
read_reg!(endpoint_in, ep, DIEPCTL, STALL)
36-
},
34+
}
3735
};
3836
stall != 0
3937
}
@@ -48,7 +46,7 @@ impl Endpoint {
4846
pub fn new<USB: UsbPeripheral>(descriptor: EndpointDescriptor) -> Endpoint {
4947
Endpoint {
5048
descriptor,
51-
usb: UsbRegisters::new::<USB>()
49+
usb: UsbRegisters::new::<USB>(),
5250
}
5351
}
5452

@@ -62,7 +60,6 @@ impl Endpoint {
6260
}
6361
}
6462

65-
6663
pub struct EndpointIn {
6764
common: Endpoint,
6865
}
@@ -121,7 +118,7 @@ impl EndpointIn {
121118

122119
pub fn write(&self, buf: &[u8]) -> Result<()> {
123120
let ep = self.usb.endpoint_in(self.index() as usize);
124-
if self.index() != 0 && read_reg!(endpoint_in, ep, DIEPCTL, EPENA) != 0{
121+
if self.index() != 0 && read_reg!(endpoint_in, ep, DIEPCTL, EPENA) != 0 {
125122
return Err(UsbError::WouldBlock);
126123
}
127124

@@ -156,7 +153,10 @@ pub struct EndpointOut {
156153
}
157154

158155
impl EndpointOut {
159-
pub fn new<USB: UsbPeripheral>(descriptor: EndpointDescriptor, buffer: EndpointBuffer) -> EndpointOut {
156+
pub fn new<USB: UsbPeripheral>(
157+
descriptor: EndpointDescriptor,
158+
buffer: EndpointBuffer,
159+
) -> EndpointOut {
160160
EndpointOut {
161161
common: Endpoint::new::<USB>(descriptor),
162162
buffer: Mutex::new(RefCell::new(buffer)),
@@ -205,19 +205,14 @@ impl EndpointOut {
205205
}
206206

207207
pub fn read(&self, buf: &mut [u8]) -> Result<usize> {
208-
interrupt::free(|cs| {
209-
self.buffer.borrow(cs).borrow_mut().read_packet(buf)
210-
})
208+
interrupt::free(|cs| self.buffer.borrow(cs).borrow_mut().read_packet(buf))
211209
}
212210

213211
pub fn buffer_state(&self) -> EndpointBufferState {
214-
interrupt::free(|cs| {
215-
self.buffer.borrow(cs).borrow().state()
216-
})
212+
interrupt::free(|cs| self.buffer.borrow(cs).borrow().state())
217213
}
218214
}
219215

220-
221216
impl Deref for EndpointIn {
222217
type Target = Endpoint;
223218

0 commit comments

Comments
 (0)