diff options
| author | Dario Nieuwenhuis <[email protected]> | 2021-07-19 23:54:18 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2021-07-19 23:54:18 +0200 |
| commit | 17999381877714ccc6bfbb81320a0e51821cdd58 (patch) | |
| tree | b5971852da976a30b30696fc09c62c7fe08eb526 /examples | |
| parent | 40ea8298eedbe2430b647ab969d4a2e627b6ece8 (diff) | |
rp/examples: add spi example
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/rp/src/bin/spi.rs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/examples/rp/src/bin/spi.rs b/examples/rp/src/bin/spi.rs new file mode 100644 index 000000000..26faa2ba5 --- /dev/null +++ b/examples/rp/src/bin/spi.rs | |||
| @@ -0,0 +1,50 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(asm)] | ||
| 4 | #![feature(min_type_alias_impl_trait)] | ||
| 5 | #![feature(impl_trait_in_bindings)] | ||
| 6 | #![feature(type_alias_impl_trait)] | ||
| 7 | #![allow(incomplete_features)] | ||
| 8 | |||
| 9 | #[path = "../example_common.rs"] | ||
| 10 | mod example_common; | ||
| 11 | |||
| 12 | use defmt::*; | ||
| 13 | use embassy::executor::Spawner; | ||
| 14 | use embassy_rp::gpio::NoPin; | ||
| 15 | use embassy_rp::spi; | ||
| 16 | use embassy_rp::spi::Spi; | ||
| 17 | use embassy_rp::{gpio, Peripherals}; | ||
| 18 | use gpio::{Level, Output}; | ||
| 19 | |||
| 20 | #[embassy::main] | ||
| 21 | async fn main(_spawner: Spawner, p: Peripherals) { | ||
| 22 | info!("Hello World!"); | ||
| 23 | |||
| 24 | // Example for resistive touch sensor in Waveshare Pico-ResTouch | ||
| 25 | |||
| 26 | let miso = p.PIN_12; | ||
| 27 | let mosi = p.PIN_11; | ||
| 28 | let clk = p.PIN_10; | ||
| 29 | let touch_cs = p.PIN_16; | ||
| 30 | |||
| 31 | // create SPI | ||
| 32 | let mut config = spi::Config::default(); | ||
| 33 | config.frequency = 2_000_000; | ||
| 34 | let mut spi = Spi::new(p.SPI1, clk, mosi, miso, NoPin, config); | ||
| 35 | |||
| 36 | // Configure CS | ||
| 37 | let mut cs = Output::new(touch_cs, Level::Low); | ||
| 38 | |||
| 39 | loop { | ||
| 40 | cs.set_low(); | ||
| 41 | let mut buf = [0x90, 0x00, 0x00, 0xd0, 0x00, 0x00]; | ||
| 42 | spi.transfer(&mut buf); | ||
| 43 | cs.set_high(); | ||
| 44 | |||
| 45 | let x = (buf[1] as u32) << 5 | (buf[2] as u32) >> 3; | ||
| 46 | let y = (buf[4] as u32) << 5 | (buf[5] as u32) >> 3; | ||
| 47 | |||
| 48 | info!("touch: {=u32} {=u32}", x, y); | ||
| 49 | } | ||
| 50 | } | ||
