Skip to content

Commit

Permalink
Merge pull request #886 from quartiq/usb-setup
Browse files Browse the repository at this point in the history
usb setup
  • Loading branch information
ryan-summers authored Apr 24, 2024
2 parents becc1c7 + 3eb6ce6 commit 5a69823
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 83 deletions.
1 change: 0 additions & 1 deletion src/bin/dual-iir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ mod app {
struct Shared {
usb: UsbDevice,
network: NetworkUsers<DualIir, 3>,

settings: Settings,
active_settings: DualIir,
telemetry: TelemetryBuffer,
Expand Down
17 changes: 7 additions & 10 deletions src/hardware/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,7 @@ macro_rules! adc_input {
// never actually modified. It technically only needs to be immutably
// borrowed, but the current HAL API only supports mutable borrows.
let spi_eot_clear = unsafe {
SPI_EOT_CLEAR.write([1 << 3]);
SPI_EOT_CLEAR.assume_init_mut()
SPI_EOT_CLEAR.write([1 << 3])
};

// Generate DMA events when the timer hits zero (roll-over). This must be before
Expand Down Expand Up @@ -334,13 +333,12 @@ macro_rules! adc_input {

// Note(unsafe): This word is initialized once per ADC initialization to verify
// it is initialized properly.
// Note(unsafe): Because this is a Memory->Peripheral transfer, this data is never
// actually modified. It technically only needs to be immutably borrowed, but the
// current HAL API only supports mutable borrows.
// Note(unsafe): Because this is a Memory->Peripheral transfer, this data is never
// actually modified. It technically only needs to be immutably borrowed, but the
// current HAL API only supports mutable borrows.
// Write a binary code into the SPI control register to initiate a transfer.
let spi_start = unsafe {
// Write a binary code into the SPI control register to initiate a transfer.
SPI_START.write([0x201]);
SPI_START.assume_init_mut()
SPI_START.write([0x201])
};

// Construct the trigger stream to write from memory to the peripheral.
Expand Down Expand Up @@ -377,8 +375,7 @@ macro_rules! adc_input {
spi.listen(hal::spi::Event::Error);

let adc_buf = unsafe {
ADC_BUF.write(Default::default());
ADC_BUF.assume_init_mut()
ADC_BUF.write(Default::default())
};
let adc_bufs = adc_buf[$index].split_at_mut(1);

Expand Down
4 changes: 2 additions & 2 deletions src/hardware/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ pub type I2c1Proxy =
pub type SerialPort = usbd_serial::SerialPort<
'static,
crate::hardware::UsbBus,
&'static mut setup::SerialBufferStore,
&'static mut setup::SerialBufferStore,
&'static mut [u8],
&'static mut [u8],
>;

pub type SerialTerminal<C, const Y: usize> = serial_settings::Runner<
Expand Down
103 changes: 33 additions & 70 deletions src/hardware/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use bit_field::BitField;
use core::mem::MaybeUninit;
use core::sync::atomic::{self, AtomicBool, Ordering};
use core::{fmt::Write, ptr, slice};
use heapless::String;
use stm32h7xx_hal::{
self as hal,
ethernet::{self, PHY},
Expand All @@ -22,34 +23,13 @@ use super::{
platform, pounder, pounder::dds_output::DdsOutput, shared_adc::SharedAdc,
timers, DigitalInput0, DigitalInput1, EemDigitalInput0, EemDigitalInput1,
EemDigitalOutput0, EemDigitalOutput1, EthernetPhy, HardwareVersion,
NetworkStack, SerialTerminal, SystemTimer, Systick, UsbBus, UsbDevice,
AFE0, AFE1,
NetworkStack, SerialTerminal, SystemTimer, Systick, UsbDevice, AFE0, AFE1,
};

const NUM_TCP_SOCKETS: usize = 4;
const NUM_UDP_SOCKETS: usize = 1;
const NUM_SOCKETS: usize = NUM_UDP_SOCKETS + NUM_TCP_SOCKETS;

pub struct SerialBufferStore([u8; 1024]);

impl Default for SerialBufferStore {
fn default() -> Self {
Self([0u8; 1024])
}
}

impl core::borrow::Borrow<[u8]> for &mut SerialBufferStore {
fn borrow(&self) -> &[u8] {
&self.0
}
}

impl core::borrow::BorrowMut<[u8]> for &mut SerialBufferStore {
fn borrow_mut(&mut self) -> &mut [u8] {
&mut self.0
}
}

pub struct NetStorage {
pub ip_addrs: [smoltcp::wire::IpCidr; 1],

Expand Down Expand Up @@ -690,10 +670,7 @@ where
(ref_clk, mdio, mdc, crs_dv, rxd0, rxd1, tx_en, txd0, txd1)
};

let ring = unsafe {
DES_RING.write(ethernet::DesRing::new());
DES_RING.assume_init_mut()
};
let ring = unsafe { DES_RING.write(ethernet::DesRing::new()) };

// Configure the ethernet controller
let (mut eth_dma, eth_mac) = ethernet::new(
Expand Down Expand Up @@ -1083,14 +1060,9 @@ where
};

let (usb_device, usb_serial) = {
let usb_bus = cortex_m::singleton!(: Option<usb_device::bus::UsbBusAllocator<UsbBus>> = None).unwrap();
let endpoint_memory =
cortex_m::singleton!(: [u32; 1024] = [0; 1024]).unwrap();

//let usb_id = gpioa.pa10.into_alternate::<8>();
let _usb_id = gpioa.pa10.into_alternate::<10>();
let usb_n = gpioa.pa11.into_alternate();
let usb_p = gpioa.pa12.into_alternate();

let usb = stm32h7xx_hal::usb_hs::USB2::new(
device.OTG2_HS_GLOBAL,
device.OTG2_HS_DEVICE,
Expand All @@ -1101,52 +1073,43 @@ where
&ccdr.clocks,
);

// Generate a device serial number from the MAC address.
let serial_number =
cortex_m::singleton!(: Option<heapless::String<17>> = None)
.unwrap();
{
let mut serial_string: heapless::String<17> =
heapless::String::new();
let octets = mac_addr.0;

write!(
serial_string,
"{:02x}-{:02x}-{:02x}-{:02x}-{:02x}-{:02x}",
octets[0],
octets[1],
octets[2],
octets[3],
octets[4],
octets[5]
)
.unwrap();
serial_number.replace(serial_string);
}

usb_bus.replace(stm32h7xx_hal::usb_hs::UsbBus::new(
let endpoint_memory =
cortex_m::singleton!(: Option<&'static mut [u32]> = None).unwrap();
endpoint_memory.replace(
&mut cortex_m::singleton!(: [u32; 1024] = [0; 1024]).unwrap()[..],
);
let usb_bus = cortex_m::singleton!(: usb_device::bus::UsbBusAllocator<super::UsbBus> =
stm32h7xx_hal::usb_hs::UsbBus::new(
usb,
&mut endpoint_memory[..],
));
endpoint_memory.take().unwrap(),
))
.unwrap();

let rx_buffer = cortex_m::singleton!(: SerialBufferStore = SerialBufferStore::default()).unwrap();
let tx_buffer = cortex_m::singleton!(: SerialBufferStore = SerialBufferStore::default()).unwrap();
let read_store = cortex_m::singleton!(: [u8; 128] = [0; 128]).unwrap();
let write_store =
cortex_m::singleton!(: [u8; 1024] = [0; 1024]).unwrap();
let serial = usbd_serial::SerialPort::new_with_store(
usb_bus,
&mut read_store[..],
&mut write_store[..],
);

// Generate a device serial number from the MAC address.
let serial_number = cortex_m::singleton!(: String<17> = {
let mut s = String::new();
write!(s, "{mac_addr}").unwrap();
s
})
.unwrap();

let serial = {
usbd_serial::SerialPort::new_with_store(
usb_bus.as_ref().unwrap(),
rx_buffer,
tx_buffer,
)
};
let usb_device = usb_device::device::UsbDeviceBuilder::new(
usb_bus.as_ref().unwrap(),
usb_bus,
usb_device::device::UsbVidPid(0x1209, 0x392F),
)
.strings(&[usb_device::device::StringDescriptors::default()
.manufacturer("ARTIQ/Sinara")
.product("Stabilizer")
.serial_number(serial_number.as_ref().unwrap())])
.serial_number(serial_number)])
.unwrap()
.device_class(usbd_serial::USB_CLASS_CDC)
.build();
Expand All @@ -1156,7 +1119,7 @@ where

let usb_terminal = {
let input_buffer =
cortex_m::singleton!(: [u8; 256] = [0u8; 256]).unwrap();
cortex_m::singleton!(: [u8; 128] = [0u8; 128]).unwrap();
let serialize_buffer =
cortex_m::singleton!(: [u8; 512] = [0u8; 512]).unwrap();

Expand Down

0 comments on commit 5a69823

Please sign in to comment.