From c636252335d06d27eff6d84bd066aca14a8e5731 Mon Sep 17 00:00:00 2001 From: Gabriel Dobato Date: Mon, 13 May 2024 23:22:10 +0200 Subject: [PATCH] Remove ref to static mut --- examples/usb_echo.rs | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/examples/usb_echo.rs b/examples/usb_echo.rs index f7ed433..b109ee9 100644 --- a/examples/usb_echo.rs +++ b/examples/usb_echo.rs @@ -6,6 +6,8 @@ #![no_std] #![no_main] +use core::mem::MaybeUninit; + use cortex_m::singleton; use portenta_h7::{ board::{self, Board, UsbBus, USB}, @@ -17,15 +19,15 @@ use rtic_monotonics::systick::*; use usb_device::prelude::*; use usbd_serial::CdcAcmClass; -const USB_BUS_BUFFER_SIZE: usize = 1024; -static mut USB_BUS_BUFFER: [u32; USB_BUS_BUFFER_SIZE] = [0u32; USB_BUS_BUFFER_SIZE]; -const USB_HS_MAX_PACKET_SIZE: usize = 512; -static mut USB_APP_BUFFER: [u8; USB_HS_MAX_PACKET_SIZE] = [0u8; USB_HS_MAX_PACKET_SIZE]; +const USB_HS_MAX_PACKET_SIZE: usize = 64; #[app(device = portenta_h7::hal::pac, peripherals = false)] mod app { use super::*; + const USB_BUS_BUFFER_SIZE: usize = 1024; + static mut USB_BUS_BUFFER: MaybeUninit<[u32; USB_BUS_BUFFER_SIZE]> = MaybeUninit::uninit(); + #[shared] struct Shared {} @@ -41,6 +43,16 @@ mod app { log_init!(); let systick_mono_token = rtic_monotonics::create_systick_token!(); + + // Init usb buffer + unsafe { + let usb_buffer: &mut [MaybeUninit; USB_BUS_BUFFER_SIZE] = + &mut *(core::ptr::addr_of_mut!(USB_BUS_BUFFER) as *mut _); + for element in usb_buffer.iter_mut() { + element.as_mut_ptr().write(0); + } + } + Systick::start( cx.core.SYST, board::CORE_FREQUENCY.raw(), @@ -50,12 +62,7 @@ mod app { // Get board resources let Board { led_blue, usb, .. } = Board::take(); - // Init USB stack - let usb_bus = singleton!( - : usb_device::class_prelude::UsbBusAllocator> = - UsbBus::new(usb, unsafe { &mut USB_BUS_BUFFER }) - ) - .unwrap(); + let usb_bus = singleton!(:usb_device::class_prelude::UsbBusAllocator> = UsbBus::new(usb, unsafe {USB_BUS_BUFFER.assume_init_mut()})).unwrap(); let usb_serial_port = usbd_serial::CdcAcmClass::new(usb_bus, USB_HS_MAX_PACKET_SIZE as u16); let usb_dev = UsbDeviceBuilder::new(usb_bus, UsbVidPid(0x1234, 0xABCD)) @@ -87,18 +94,19 @@ mod app { // Trigger internal state machine. It should be called either from ISR on USB event, // or every 10 ms from normal execution context if usb_dev.poll(&mut [usb_serial_port]) { + let mut app_buff = [0u8; USB_HS_MAX_PACKET_SIZE]; + // Read from reception fifo - match usb_serial_port.read_packet(unsafe { &mut USB_APP_BUFFER[..] }) { + match usb_serial_port.read_packet(&mut app_buff) { Ok(cnt) if cnt > 0 => { #[cfg(debug_assertions)] log!( "Received {} bytes: {}", cnt, - core::str::from_utf8(unsafe { &USB_APP_BUFFER[..cnt] }) - .unwrap_or("not valid") + core::str::from_utf8(&app_buff[..cnt]).unwrap_or("not valid") ); // Send back received data - match usb_serial_port.write_packet(unsafe { &USB_APP_BUFFER[..cnt] }) { + match usb_serial_port.write_packet(&app_buff[..cnt]) { Ok(_) => (), Err(err) => { log!("Error in transmission: {:?}", err)