Skip to content

Commit 6f32f18

Browse files
committed
Remove unsafe code from usb_serial_rtic example
1 parent e90577f commit 6f32f18

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

examples/usb_serial_rtic.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![no_main]
44
#![no_std]
55
#![allow(non_snake_case)]
6+
#![deny(unsafe_code)]
67

78
use panic_semihosting as _;
89

@@ -22,10 +23,8 @@ mod app {
2223
#[local]
2324
struct Local {}
2425

25-
#[init]
26+
#[init(local = [usb_bus: Option<usb_device::bus::UsbBusAllocator<UsbBusType>> = None])]
2627
fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) {
27-
static mut USB_BUS: Option<usb_device::bus::UsbBusAllocator<UsbBusType>> = None;
28-
2928
let mut flash = cx.device.FLASH.constrain();
3029
let rcc = cx.device.RCC.constrain();
3130

@@ -57,23 +56,18 @@ mod app {
5756
pin_dp: usb_dp,
5857
};
5958

60-
unsafe {
61-
USB_BUS.replace(UsbBus::new(usb));
62-
}
63-
64-
let serial = usbd_serial::SerialPort::new(unsafe { USB_BUS.as_ref().unwrap() });
65-
66-
let usb_dev = UsbDeviceBuilder::new(
67-
unsafe { USB_BUS.as_ref().unwrap() },
68-
UsbVidPid(0x16c0, 0x27dd),
69-
)
70-
.device_class(usbd_serial::USB_CLASS_CDC)
71-
.strings(&[StringDescriptors::default()
72-
.manufacturer("Fake Company")
73-
.product("Serial port")
74-
.serial_number("TEST")])
75-
.unwrap()
76-
.build();
59+
cx.local.usb_bus.replace(UsbBus::new(usb));
60+
let usb_bus = cx.local.usb_bus.as_ref().unwrap();
61+
62+
let serial = usbd_serial::SerialPort::new(usb_bus);
63+
let usb_dev = UsbDeviceBuilder::new(usb_bus, UsbVidPid(0x16c0, 0x27dd))
64+
.device_class(usbd_serial::USB_CLASS_CDC)
65+
.strings(&[StringDescriptors::default()
66+
.manufacturer("Fake Company")
67+
.product("Serial port")
68+
.serial_number("TEST")])
69+
.unwrap()
70+
.build();
7771

7872
(Shared { usb_dev, serial }, Local {}, init::Monotonics())
7973
}

0 commit comments

Comments
 (0)