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 } ;
1
5
use core:: marker:: PhantomData ;
2
6
use embedded_hal:: blocking:: delay:: DelayMs ;
7
+ use usb_device:: bus:: { PollResult , UsbBusAllocator } ;
8
+ use usb_device:: endpoint:: { EndpointAddress , EndpointType } ;
3
9
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} ;
8
10
9
- use crate :: target:: UsbRegisters ;
10
- use crate :: target:: interrupt:: { self , Mutex , CriticalSection } ;
11
11
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 } ;
14
16
15
17
/// USB peripheral driver for STM32 microcontrollers.
16
18
pub struct UsbBus < USB > {
@@ -90,7 +92,8 @@ impl<USB: UsbPeripheral> UsbBus<USB> {
90
92
for ep in & self . allocator . endpoints_in {
91
93
if let Some ( ep) = ep {
92
94
// 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( ) ) ) ;
94
97
95
98
ep. configure ( cs) ;
96
99
}
@@ -249,7 +252,7 @@ impl<USB: UsbPeripheral> EndpointAllocator<USB> {
249
252
endpoints_in : [ None , None , None , None , None , None , None , None , None ] ,
250
253
endpoints_out : [ None , None , None , None , None , None , None , None , None ] ,
251
254
memory_allocator : EndpointMemoryAllocator :: new ( memory) ,
252
- _marker : PhantomData
255
+ _marker : PhantomData ,
253
256
}
254
257
}
255
258
@@ -269,28 +272,33 @@ impl<USB: UsbPeripheral> EndpointAllocator<USB> {
269
272
for number in 1 ..USB :: ENDPOINT_COUNT {
270
273
if * bitmap & ( 1 << number) == 0 {
271
274
* bitmap |= 1 << number;
272
- return Ok ( number as u8 )
275
+ return Ok ( number as u8 ) ;
273
276
}
274
277
}
275
278
Err ( UsbError :: EndpointOverflow )
276
279
}
277
280
}
278
281
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 > {
280
287
let number = Self :: alloc_number ( bitmap, config. number ) ?;
281
288
let address = EndpointAddress :: from_parts ( number as usize , direction) ;
282
289
Ok ( EndpointDescriptor {
283
290
address,
284
291
ep_type : config. ep_type ,
285
292
max_packet_size : config. max_packet_size ,
286
- interval : config. interval
293
+ interval : config. interval ,
287
294
} )
288
295
}
289
296
290
297
fn alloc_in ( & mut self , config : & EndpointConfig ) -> Result < EndpointIn > {
291
298
let descr = Self :: alloc ( & mut self . bitmap_in , config, UsbDirection :: In ) ?;
292
299
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 ) ?;
294
302
let ep = EndpointIn :: new :: < USB > ( descr) ;
295
303
296
304
Ok ( ep)
@@ -299,7 +307,9 @@ impl<USB: UsbPeripheral> EndpointAllocator<USB> {
299
307
fn alloc_out ( & mut self , config : & EndpointConfig ) -> Result < EndpointOut > {
300
308
let descr = Self :: alloc ( & mut self . bitmap_out , config, UsbDirection :: Out ) ?;
301
309
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 ) ?;
303
313
let ep = EndpointOut :: new :: < USB > ( descr, buffer) ;
304
314
305
315
Ok ( ep)
@@ -311,8 +321,8 @@ impl<USB: UsbPeripheral> EndpointAllocator<USB> {
311
321
ep_addr : Option < EndpointAddress > ,
312
322
ep_type : EndpointType ,
313
323
max_packet_size : u16 ,
314
- interval : u8 ) -> Result < EndpointAddress >
315
- {
324
+ interval : u8 ,
325
+ ) -> Result < EndpointAddress > {
316
326
let ep_type = unsafe { core:: mem:: transmute ( ep_type) } ;
317
327
let number = ep_addr. map ( |a| a. index ( ) as u8 ) ;
318
328
@@ -321,21 +331,21 @@ impl<USB: UsbPeripheral> EndpointAllocator<USB> {
321
331
max_packet_size,
322
332
interval,
323
333
number,
324
- pair_of : None
334
+ pair_of : None ,
325
335
} ;
326
336
match ep_dir {
327
337
UsbDirection :: Out => {
328
338
let ep = self . alloc_out ( & config) ?;
329
339
let address = ep. address ( ) ;
330
340
self . endpoints_out [ address. index ( ) ] = Some ( ep) ;
331
341
Ok ( address)
332
- } ,
342
+ }
333
343
UsbDirection :: In => {
334
344
let ep = self . alloc_in ( & config) ?;
335
345
let address = ep. address ( ) ;
336
346
self . endpoints_in [ address. index ( ) ] = Some ( ep) ;
337
347
Ok ( address)
338
- } ,
348
+ }
339
349
}
340
350
}
341
351
}
@@ -347,9 +357,10 @@ impl<USB: UsbPeripheral> usb_device::bus::UsbBus for UsbBus<USB> {
347
357
ep_addr : Option < EndpointAddress > ,
348
358
ep_type : EndpointType ,
349
359
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)
353
364
}
354
365
355
366
fn enable ( & mut self ) {
@@ -383,7 +394,7 @@ impl<USB: UsbPeripheral> usb_device::bus::UsbBus for UsbBus<USB> {
383
394
PhyType :: InternalFullSpeed => {
384
395
// Select FS Embedded PHY
385
396
modify_reg ! ( otg_global, regs. global( ) , GUSBCFG , PHYSEL : 1 ) ;
386
- } ,
397
+ }
387
398
PhyType :: InternalHighSpeed => {
388
399
// Turn off PHY
389
400
modify_reg ! ( otg_global, regs. global( ) , GCCFG , PWRDWN : 0 ) ;
@@ -578,8 +589,16 @@ impl<USB: UsbPeripheral> usb_device::bus::UsbBus for UsbBus<USB> {
578
589
579
590
let core_id = read_reg ! ( otg_global, regs. global( ) , CID ) ;
580
591
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
583
602
) ;
584
603
585
604
if reset != 0 {
@@ -628,7 +647,7 @@ impl<USB: UsbPeripheral> usb_device::bus::UsbBus for UsbBus<USB> {
628
647
32_000_000 ..=u32:: MAX => 0x6 ,
629
648
} ;
630
649
}
631
- _ => unimplemented ! ( )
650
+ _ => unimplemented ! ( ) ,
632
651
}
633
652
modify_reg ! ( otg_global, regs. global( ) , GUSBCFG , TRDT : trdt) ;
634
653
@@ -651,12 +670,15 @@ impl<USB: UsbPeripheral> usb_device::bus::UsbBus for UsbBus<USB> {
651
670
652
671
// RXFLVL & IEPINT flags are read-only, there is no need to clear them
653
672
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 ) ;
655
675
match status {
656
- 0x02 => { // OUT received
676
+ 0x02 => {
677
+ // OUT received
657
678
ep_out |= 1 << epnum;
658
679
}
659
- 0x06 => { // SETUP received
680
+ 0x06 => {
681
+ // SETUP received
660
682
// flushing TX if something stuck in control endpoint
661
683
let ep = regs. endpoint_in ( epnum as usize ) ;
662
684
if read_reg ! ( endpoint_in, ep, DIEPTSIZ , PKTCNT ) != 0 {
@@ -665,7 +687,8 @@ impl<USB: UsbPeripheral> usb_device::bus::UsbBus for UsbBus<USB> {
665
687
}
666
688
ep_setup |= 1 << epnum;
667
689
}
668
- 0x03 | 0x04 => { // OUT completed | SETUP completed
690
+ 0x03 | 0x04 => {
691
+ // OUT completed | SETUP completed
669
692
// Re-enable the endpoint, F429-like chips only
670
693
if core_id == 0x0000_1200 || core_id == 0x0000_1100 {
671
694
let ep = regs. endpoint_out ( epnum as usize ) ;
@@ -685,12 +708,17 @@ impl<USB: UsbPeripheral> usb_device::bus::UsbBus for UsbBus<USB> {
685
708
read_reg ! ( otg_global, regs. global( ) , GRXSTSP ) ; // pop GRXSTSP
686
709
687
710
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 ( ) ;
689
714
690
715
// 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
+ {
694
722
let ep = regs. endpoint_out ( epnum as usize ) ;
695
723
modify_reg ! ( endpoint_out, ep, DOEPCTL , CNAK : 1 , EPENA : 1 ) ;
696
724
}
@@ -716,17 +744,21 @@ impl<USB: UsbPeripheral> usb_device::bus::UsbBus for UsbBus<USB> {
716
744
match ep. buffer_state ( ) {
717
745
EndpointBufferState :: DataOut => {
718
746
ep_out |= 1 << ep. address ( ) . index ( ) ;
719
- } ,
747
+ }
720
748
EndpointBufferState :: DataSetup => {
721
749
ep_setup |= 1 << ep. address ( ) . index ( ) ;
722
- } ,
723
- EndpointBufferState :: Empty => { } ,
750
+ }
751
+ EndpointBufferState :: Empty => { }
724
752
}
725
753
}
726
754
}
727
755
728
756
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
+ }
730
762
} else {
731
763
PollResult :: None
732
764
}
0 commit comments