diff --git a/src/main.rs b/src/main.rs index 5ac4e64..4db7494 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,7 +19,6 @@ use panic_halt as _; use rp2040_hal as hal; // Some traits we need -use cortex_m::singleton; use embedded_hal::PwmPin; use fugit::RateExtU32; use rp2040_hal::clocks::Clock; @@ -54,24 +53,26 @@ const HIGH: u16 = 25000; /// if your board has a different frequency const XTAL_FREQ_HZ: u32 = 12_000_000u32; -fn get_adc_channel_blocking(dma: &hal::dma::Channels, spi: &mut BIDI, cs_pin: &dyn embedded_hal::digital::v2::OutputPin) -> u16 +fn get_adc_channel_blocking(dma: hal::dma::Channels, spi: rp2040_hal::Spi, cs_pin: &mut dyn embedded_hal::digital::v2::OutputPin) -> u16 where - BIDI: hal::dma::ReadTarget + hal::dma::WriteTarget, + S: hal::spi::SpiDevice, E: core::fmt::Debug { - let mut tx_buf: [u8; 3] = [0x06, 0x40, 0x00]; - let mut rx_buf: [u8; 3] = [0; 3]; + static TX_BUF: [u8; 3] = [0x06, 0x40, 0x00]; + static mut RX_BUF: [u8; 3] = [0; 3]; // clear chip select cs_pin.set_low().unwrap(); // Use BidirectionalConfig to simultaneously write to spi from tx_buf and read into rx_buf - let transfer = bidirectional::Config::new((dma.ch0, dma.ch1), &tx_buf, spi, &mut rx_buf).start(); - // Wait for both DMA channels to finish - let ((_ch0, _ch1), tx_buf, _spi, rx_buf) = transfer.wait(); - cs_pin.set_high().unwrap(); + unsafe { // necessary due to mutable-static use + let transfer = bidirectional::Config::new((dma.ch0, dma.ch1), &TX_BUF, spi, &mut RX_BUF).start(); + // Wait for both DMA channels to finish + let ((_ch0, _ch1), _tx_buf, _spi, _rx_buf) = transfer.wait(); + cs_pin.set_high().unwrap(); - (((rx_buf[1] & 0x0F) as u16) << 8) | rx_buf[2] as u16 + (((RX_BUF[1] & 0x0F) as u16) << 8) | RX_BUF[2] as u16 + } } /// Entry point to our bare-metal application. @@ -193,7 +194,7 @@ fn main() -> ! { //let tx_buf = singleton!(: [u8; 3] = [0x06, 0x40, 0x00]).unwrap(); //let rx_buf = singleton!(: [u8; 3] = [0; 3]).unwrap(); - let adc_value = get_adc_channel_blocking(&dma, &mut spi, &spi_cs); + let adc_value = get_adc_channel_blocking(dma, spi, &mut spi_cs); { let data: String<16> = String::from(adc_value);