Skip to content

Commit 97ff2ef

Browse files
authored
Merge pull request #528 from Tnze/master
Remove unsafe code from usb_serial_rtic example
2 parents e90577f + e5ae73e commit 97ff2ef

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
3434
- Updated `usb-device` and `usbd-serial` to latest versions [#510]
3535
- Rework pin remaps, fix CAN1 remap [#511]
3636
- Rework USART remap,
37+
- Remove unsafe code from usb_serial_rtic example [#528]
3738

3839
### Added
3940

@@ -71,6 +72,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
7172
[#516]: https://github.com/stm32-rs/stm32f1xx-hal/pull/516
7273
[#520]: https://github.com/stm32-rs/stm32f1xx-hal/pull/520
7374
[#526]: https://github.com/stm32-rs/stm32f1xx-hal/pull/526
75+
[#528]: https://github.com/stm32-rs/stm32f1xx-hal/pull/528
7476

7577
## [v0.10.0] - 2022-12-12
7678

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)