Add dummy code to read from the external ADC

This commit is contained in:
Thomas Kolb 2023-01-03 22:29:01 +01:00
parent f172e071f8
commit 1a6e442815
3 changed files with 145 additions and 7 deletions

98
Cargo.lock generated
View File

@ -8,6 +8,15 @@ version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8da52d66c7071e2e3fa2a1e5c6d088fec47b593032b254f5e980de8ea54454d6"
[[package]]
name = "atomic-polyfill"
version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3ff7eb3f316534d83a8a2c3d1674ace8a5a71198eba31e2e2b597833f699b28"
dependencies = [
"critical-section",
]
[[package]]
name = "autocfg"
version = "1.1.0"
@ -20,7 +29,7 @@ version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5deb64efa5bd81e31fcd1938615a6d98c82eafcbcd787162b6f63b91d6bac5b3"
dependencies = [
"rustc_version",
"rustc_version 0.2.3",
]
[[package]]
@ -35,6 +44,12 @@ version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "byteorder"
version = "1.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
[[package]]
name = "chrono"
version = "0.4.23"
@ -186,6 +201,7 @@ dependencies = [
"embedded-hal 0.2.7",
"embedded-hal 1.0.0-alpha.9",
"fugit",
"heapless",
"itertools",
"nb 1.0.0",
"panic-halt",
@ -194,7 +210,7 @@ dependencies = [
"rand_core",
"rp2040-boot2",
"rp2040-hal",
"rp2040-hal-macros",
"rp2040-hal-macros 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rp2040-pac",
"rtic-monotonic",
"usb-device",
@ -202,6 +218,28 @@ dependencies = [
"void",
]
[[package]]
name = "hash32"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0c35f58762feb77d74ebe43bdbc3210f09be9fe6742234d573bacc26ed92b67"
dependencies = [
"byteorder",
]
[[package]]
name = "heapless"
version = "0.7.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db04bc24a18b9ea980628ecf00e6c0264f3c1426dac36c00cb49b6fbad8b0743"
dependencies = [
"atomic-polyfill",
"hash32",
"rustc_version 0.4.0",
"spin",
"stable_deref_trait",
]
[[package]]
name = "itertools"
version = "0.10.5"
@ -211,6 +249,16 @@ dependencies = [
"either",
]
[[package]]
name = "lock_api"
version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "435011366fe56583b16cf956f9df0095b405b82d76425bc8981c0e22e60ec4df"
dependencies = [
"autocfg",
"scopeguard",
]
[[package]]
name = "nb"
version = "0.1.3"
@ -348,11 +396,10 @@ dependencies = [
[[package]]
name = "rp2040-hal"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ecf1b975581f0cac465247c464e7d2b8d93c7a5fceb4eb13b7b8517f4f85f6d"
dependencies = [
"cortex-m",
"critical-section",
"embedded-dma",
"embedded-hal 0.2.7",
"fugit",
"itertools",
@ -360,13 +407,22 @@ dependencies = [
"paste",
"pio",
"rand_core",
"rp2040-hal-macros",
"rp2040-hal-macros 0.1.0",
"rp2040-pac",
"usb-device",
"vcell",
"void",
]
[[package]]
name = "rp2040-hal-macros"
version = "0.1.0"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "rp2040-hal-macros"
version = "0.1.0"
@ -402,9 +458,24 @@ version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
dependencies = [
"semver",
"semver 0.9.0",
]
[[package]]
name = "rustc_version"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366"
dependencies = [
"semver 1.0.16",
]
[[package]]
name = "scopeguard"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "semver"
version = "0.9.0"
@ -414,12 +485,27 @@ dependencies = [
"semver-parser",
]
[[package]]
name = "semver"
version = "1.0.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58bc9567378fc7690d6b2addae4e60ac2eeea07becb2c64b9f218b53865cba2a"
[[package]]
name = "semver-parser"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
[[package]]
name = "spin"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f6002a767bff9e83f8eeecf883ecb8011875a21ae8da43bffb817a57e78cc09"
dependencies = [
"lock_api",
]
[[package]]
name = "stable_deref_trait"
version = "1.2.0"

View File

@ -21,13 +21,14 @@ nb = "1.0"
rp2040-pac = "0.4.0"
paste = "1.0"
pio = "0.2.0"
rp2040-hal = "0.7.0"
rp2040-hal = { path = "../rp-hal/rp2040-hal/" }
rp2040-hal-macros = "0.1.0"
usb-device = "0.2.9"
vcell = "0.1"
void = { version = "1.0.2", default-features = false }
rand_core = "0.6.3"
critical-section = { version = "1.0.0" }
heapless = "0.7.16"
chrono = { version = "0.4", default-features = false, optional = true }

View File

@ -10,6 +10,7 @@
#![no_std]
#![no_main]
use embedded_hal::digital::v2::OutputPin;
// Ensure we halt the program on panic (if we don't mention this crate it won't
// be linked)
use panic_halt as _;
@ -18,6 +19,7 @@ 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;
@ -26,9 +28,14 @@ use rp2040_hal::clocks::Clock;
// register access
use hal::pac;
// SPI + DMA
use hal::dma::{bidirectional, DMAExt};
// UART related types
use hal::uart::{DataBits, StopBits, UartConfig};
use heapless::String;
/// The linker will place this boot block at the start of our program image. We
/// need this to help the ROM bootloader get our code up and running.
/// Note: This boot block is not necessary when using a rp-hal based BSP
@ -141,6 +148,50 @@ fn main() -> ! {
uart.write_full_blocking(b"Initialization complete!\r\n");
// SPI CS pin is controlled by software
let mut spi_cs = pins.gpio5.into_push_pull_output();
spi_cs.set_high().unwrap();
// These are implicitly used by the spi driver if they are in the correct mode
let _spi_sclk = pins.gpio2.into_mode::<hal::gpio::FunctionSpi>();
let _spi_mosi = pins.gpio3.into_mode::<hal::gpio::FunctionSpi>();
let _spi_miso = pins.gpio4.into_mode::<hal::gpio::FunctionSpi>();
let spi = hal::spi::Spi::<_, _, 8>::new(pac.SPI0);
// Exchange the uninitialised SPI driver for an initialised one
let spi = spi.init(
&mut pac.RESETS,
clocks.peripheral_clock.freq(),
1_000_000u32.Hz(),
&embedded_hal::spi::MODE_0,
);
// Initialize DMA.
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();
// 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 data: String<16> = String::from(adc_value);
uart.write_full_blocking(b"Read ADC value: ");
uart.write_full_blocking(data.as_bytes());
uart.write_full_blocking(b"\r\n");
}
// Infinite loop, fading LED up and down
loop {
for i in 0..ch_val.len() {