get_adc_channel_blocking: pass everything by-value to make it compile

Still not really the final solution…
This commit is contained in:
Thomas Kolb 2023-01-04 23:33:52 +01:00
parent 86691868ea
commit 44132e00fe

View file

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