@@ -5,6 +5,7 @@ use panic_halt as _;
5
5
6
6
#[ rtic:: app( device = stm32f4xx_hal:: pac, peripherals = true , dispatchers = [ USART1 ] ) ]
7
7
mod app {
8
+ use static_cell:: { ConstStaticCell , StaticCell } ;
8
9
use stm32f4xx_hal:: {
9
10
gpio:: { Output , PC13 } ,
10
11
otg_fs:: { UsbBus , UsbBusType , USB } ,
@@ -33,8 +34,9 @@ mod app {
33
34
34
35
#[ init]
35
36
fn init ( ctx : init:: Context ) -> ( Shared , Local , init:: Monotonics ) {
36
- static mut EP_MEMORY : [ u32 ; 1024 ] = [ 0 ; 1024 ] ;
37
- static mut USB_BUS : Option < usb_device:: bus:: UsbBusAllocator < UsbBusType > > = None ;
37
+ static EP_MEMORY : ConstStaticCell < [ u32 ; 1024 ] > = ConstStaticCell :: new ( [ 0 ; 1024 ] ) ;
38
+ static USB_BUS : StaticCell < usb_device:: bus:: UsbBusAllocator < UsbBusType > > =
39
+ StaticCell :: new ( ) ;
38
40
39
41
let dp = ctx. device ;
40
42
@@ -59,22 +61,17 @@ mod app {
59
61
pin_dp : gpioa. pa12 . into ( ) ,
60
62
hclk : rcc. clocks . hclk ( ) ,
61
63
} ;
62
- unsafe {
63
- USB_BUS . replace ( UsbBus :: new ( usb, & mut EP_MEMORY ) ) ;
64
- }
65
-
66
- let usb_serial = usbd_serial:: SerialPort :: new ( unsafe { USB_BUS . as_ref ( ) . unwrap ( ) } ) ;
67
- let usb_dev = UsbDeviceBuilder :: new (
68
- unsafe { USB_BUS . as_ref ( ) . unwrap ( ) } ,
69
- UsbVidPid ( 0x16c0 , 0x27dd ) ,
70
- )
71
- . device_class ( usbd_serial:: USB_CLASS_CDC )
72
- . strings ( & [ StringDescriptors :: default ( )
73
- . manufacturer ( "Fake Company" )
74
- . product ( "Product" )
75
- . serial_number ( "TEST" ) ] )
76
- . unwrap ( )
77
- . build ( ) ;
64
+ let usb_bus = USB_BUS . init ( UsbBus :: new ( usb, EP_MEMORY . take ( ) ) ) ;
65
+
66
+ let usb_serial = usbd_serial:: SerialPort :: new ( usb_bus) ;
67
+ let usb_dev = UsbDeviceBuilder :: new ( usb_bus, UsbVidPid ( 0x16c0 , 0x27dd ) )
68
+ . device_class ( usbd_serial:: USB_CLASS_CDC )
69
+ . strings ( & [ StringDescriptors :: default ( )
70
+ . manufacturer ( "Fake Company" )
71
+ . product ( "Product" )
72
+ . serial_number ( "TEST" ) ] )
73
+ . unwrap ( )
74
+ . build ( ) ;
78
75
79
76
(
80
77
Shared {
0 commit comments