Skip to content

Commit

Permalink
Remove ref to static mut
Browse files Browse the repository at this point in the history
  • Loading branch information
gdobato committed May 13, 2024
1 parent 839e7f1 commit c636252
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions examples/usb_echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#![no_std]
#![no_main]

use core::mem::MaybeUninit;

use cortex_m::singleton;
use portenta_h7::{
board::{self, Board, UsbBus, USB},
Expand All @@ -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 {}

Expand All @@ -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<u32>; 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(),
Expand All @@ -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<USB>> =
UsbBus::new(usb, unsafe { &mut USB_BUS_BUFFER })
)
.unwrap();
let usb_bus = singleton!(:usb_device::class_prelude::UsbBusAllocator<UsbBus<USB>> = 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))
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit c636252

Please sign in to comment.