From 6f32f18967191b2289c2d47bb966c593c856f800 Mon Sep 17 00:00:00 2001 From: Tnze Date: Thu, 23 Jan 2025 14:17:06 +0800 Subject: [PATCH 1/2] Remove unsafe code from usb_serial_rtic example --- examples/usb_serial_rtic.rs | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/examples/usb_serial_rtic.rs b/examples/usb_serial_rtic.rs index fc49de47..b4d8ce3e 100644 --- a/examples/usb_serial_rtic.rs +++ b/examples/usb_serial_rtic.rs @@ -3,6 +3,7 @@ #![no_main] #![no_std] #![allow(non_snake_case)] +#![deny(unsafe_code)] use panic_semihosting as _; @@ -22,10 +23,8 @@ mod app { #[local] struct Local {} - #[init] + #[init(local = [usb_bus: Option> = None])] fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) { - static mut USB_BUS: Option> = None; - let mut flash = cx.device.FLASH.constrain(); let rcc = cx.device.RCC.constrain(); @@ -57,23 +56,18 @@ mod app { pin_dp: usb_dp, }; - unsafe { - USB_BUS.replace(UsbBus::new(usb)); - } - - let serial = usbd_serial::SerialPort::new(unsafe { USB_BUS.as_ref().unwrap() }); - - let usb_dev = UsbDeviceBuilder::new( - unsafe { USB_BUS.as_ref().unwrap() }, - UsbVidPid(0x16c0, 0x27dd), - ) - .device_class(usbd_serial::USB_CLASS_CDC) - .strings(&[StringDescriptors::default() - .manufacturer("Fake Company") - .product("Serial port") - .serial_number("TEST")]) - .unwrap() - .build(); + cx.local.usb_bus.replace(UsbBus::new(usb)); + let usb_bus = cx.local.usb_bus.as_ref().unwrap(); + + let serial = usbd_serial::SerialPort::new(usb_bus); + let usb_dev = UsbDeviceBuilder::new(usb_bus, UsbVidPid(0x16c0, 0x27dd)) + .device_class(usbd_serial::USB_CLASS_CDC) + .strings(&[StringDescriptors::default() + .manufacturer("Fake Company") + .product("Serial port") + .serial_number("TEST")]) + .unwrap() + .build(); (Shared { usb_dev, serial }, Local {}, init::Monotonics()) } From e5ae73e45cc70dd90746604de5d9c77fb9902e01 Mon Sep 17 00:00:00 2001 From: Tnze Date: Thu, 23 Jan 2025 14:23:33 +0800 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 82c0caee..c6b5dd57 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Updated `usb-device` and `usbd-serial` to latest versions [#510] - Rework pin remaps, fix CAN1 remap [#511] - Rework USART remap, +- Remove unsafe code from usb_serial_rtic example [#528] ### Added @@ -71,6 +72,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). [#516]: https://github.com/stm32-rs/stm32f1xx-hal/pull/516 [#520]: https://github.com/stm32-rs/stm32f1xx-hal/pull/520 [#526]: https://github.com/stm32-rs/stm32f1xx-hal/pull/526 +[#528]: https://github.com/stm32-rs/stm32f1xx-hal/pull/528 ## [v0.10.0] - 2022-12-12