Compare commits

...

3 Commits

Author SHA1 Message Date
Thomas Kolb 44132e00fe get_adc_channel_blocking: pass everything by-value to make it compile
Still not really the final solution…
2023-01-04 23:33:52 +01:00
Thomas Kolb 86691868ea DMA changes; still not working 2023-01-04 22:28:43 +01:00
Thomas Kolb 8790a7ce04 Trying to move the ADC reader code to a separate function 2023-01-04 13:30:30 +01:00
1 changed files with 25 additions and 13 deletions

View File

@ -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,6 +53,28 @@ const HIGH: u16 = 25000;
/// if your board has a different frequency
const XTAL_FREQ_HZ: u32 = 12_000_000u32;
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
S: hal::spi::SpiDevice,
E: core::fmt::Debug
{
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
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
}
}
/// Entry point to our bare-metal application.
///
/// The `#[rp2040_hal::entry]` macro ensures the Cortex-M start-up code calls this function
@ -170,19 +191,10 @@ fn main() -> ! {
let dma = pac.DMA.split(&mut pac.RESETS);
// Use DMA to read ADC (0xC0 in byte 1 is the channel mask)
let tx_buf = singleton!(: [u8; 3] = [0x06, 0x40, 0x00]).unwrap();
let rx_buf = singleton!(: [u8; 3] = [0; 3]).unwrap();
//let tx_buf = singleton!(: [u8; 3] = [0x06, 0x40, 0x00]).unwrap();
//let rx_buf = singleton!(: [u8; 3] = [0; 3]).unwrap();
// clear chip select
spi_cs.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, rx_buf).start();
// Wait for both DMA channels to finish
let ((_ch0, _ch1), tx_buf, _spi, rx_buf) = transfer.wait();
spi_cs.set_high().unwrap();
let adc_value = (((rx_buf[1] & 0x0F) as u16) << 8) | rx_buf[2] as u16;
let adc_value = get_adc_channel_blocking(dma, spi, &mut spi_cs);
{
let data: String<16> = String::from(adc_value);