rp/gpio: implement Input

This commit is contained in:
Dario Nieuwenhuis 2021-03-29 21:33:46 +02:00
parent 5f6f1c38d9
commit 403b308279
6 changed files with 124 additions and 75 deletions

View File

@ -0,0 +1,32 @@
#![no_std]
#![no_main]
#![feature(asm)]
#![feature(min_type_alias_impl_trait)]
#![feature(impl_trait_in_bindings)]
#![feature(type_alias_impl_trait)]
#![allow(incomplete_features)]
#[path = "../example_common.rs"]
mod example_common;
use defmt::*;
use embassy::executor::Spawner;
use embassy_rp::{gpio, Peripherals};
use embedded_hal::digital::v2::OutputPin;
#[embassy::main]
async fn main(_spawner: Spawner) {
let p = unwrap!(Peripherals::take());
let mut led = gpio::Output::new(p.PIN_25, gpio::Level::Low);
loop {
info!("led on!");
led.set_high().unwrap();
cortex_m::asm::delay(1_000_000);
info!("led off!");
led.set_low().unwrap();
cortex_m::asm::delay(1_000_000);
}
}

View File

@ -0,0 +1,28 @@
#![no_std]
#![no_main]
#![feature(asm)]
#![feature(min_type_alias_impl_trait)]
#![feature(impl_trait_in_bindings)]
#![feature(type_alias_impl_trait)]
#![allow(incomplete_features)]
#[path = "../example_common.rs"]
mod example_common;
use defmt::*;
use embassy::executor::Spawner;
use embassy_rp::gpio::{Input, Pull};
use embassy_rp::Peripherals;
use embedded_hal::digital::v2::InputPin;
#[embassy::main]
async fn main(_spawner: Spawner) {
let p = unwrap!(Peripherals::take());
let button = Input::new(p.PIN_28, Pull::Up);
loop {
info!("high? {=bool}", button.is_high().unwrap());
cortex_m::asm::delay(1_000_000);
}
}

View File

@ -0,0 +1,28 @@
#![no_std]
#![no_main]
#![feature(asm)]
#![feature(min_type_alias_impl_trait)]
#![feature(impl_trait_in_bindings)]
#![feature(type_alias_impl_trait)]
#![allow(incomplete_features)]
#[path = "../example_common.rs"]
mod example_common;
use defmt::*;
use embassy::executor::Spawner;
use embassy_rp::{uart, Peripherals};
#[embassy::main]
async fn main(_spanwer: Spawner) {
let p = unwrap!(Peripherals::take());
let config = uart::Config::default();
let mut uart = uart::Uart::new(p.UART0, p.PIN_0, p.PIN_1, p.PIN_2, p.PIN_3, config);
uart.send("Hello World!\r\n".as_bytes());
loop {
uart.send("hello there!\r\n".as_bytes());
cortex_m::asm::delay(1_000_000);
}
}

View File

@ -0,0 +1,16 @@
use core::sync::atomic::{AtomicUsize, Ordering};
use defmt_rtt as _;
use panic_probe as _;
#[link_section = ".boot2"]
#[used]
pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER;
defmt::timestamp! {"{=u64}", {
static COUNT: AtomicUsize = AtomicUsize::new(0);
// NOTE(no-CAS) `timestamps` runs with interrupts disabled
let n = COUNT.load(Ordering::Relaxed);
COUNT.store(n + 1, Ordering::Relaxed);
n as u64
}
}

View File

@ -1,72 +0,0 @@
#![no_std]
#![no_main]
#![feature(asm)]
#![feature(min_type_alias_impl_trait)]
#![feature(impl_trait_in_bindings)]
#![feature(type_alias_impl_trait)]
use core::sync::atomic::{AtomicUsize, Ordering};
use defmt::{panic, *};
use defmt_rtt as _;
use embassy::executor::Spawner;
use embassy::interrupt::InterruptExt;
use embassy_rp::{dma, gpio, interrupt, uart, Peripherals};
use embedded_hal::digital::v2::OutputPin;
use panic_probe as _;
use rp2040_pac2 as pac;
#[link_section = ".boot2"]
#[used]
pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER;
defmt::timestamp! {"{=u64}", {
static COUNT: AtomicUsize = AtomicUsize::new(0);
// NOTE(no-CAS) `timestamps` runs with interrupts disabled
let n = COUNT.load(Ordering::Relaxed);
COUNT.store(n + 1, Ordering::Relaxed);
n as u64
}
}
#[embassy::main]
async fn main(spanwer: Spawner) {
let p = unwrap!(Peripherals::take());
let mut config = uart::Config::default();
let mut uart = uart::Uart::new(p.UART0, p.PIN_0, p.PIN_1, p.PIN_2, p.PIN_3, config);
uart.send("Hello World!\r\n".as_bytes());
let mut led = gpio::Output::new(p.PIN_25, gpio::Level::Low);
let irq = interrupt::take!(DMA_IRQ_0);
unsafe {
//pac::DMA.inte0().write(|w| w.set_inte0(1 << 0));
}
irq.set_handler(dma_irq);
irq.unpend();
irq.enable();
let from: [u32; 4] = [1, 2, 3, 4];
let mut to: [u32; 4] = [9, 8, 7, 6];
info!("before dma: from = {:?}, to = {:?}", from, to);
cortex_m::asm::delay(4_000_000);
dma::Dma::copy(p.DMA_CH0, &from, &mut to);
cortex_m::asm::delay(4_000_000);
info!("after dma: from = {:?}, to = {:?}", from, to);
loop {
info!("led on!");
uart.send("ON!\r".as_bytes());
led.set_high().unwrap();
cortex_m::asm::delay(1_000_000);
info!("led off!");
uart.send("Off!\r".as_bytes());
led.set_low().unwrap();
cortex_m::asm::delay(4_000_000);
}
}
unsafe fn dma_irq(ctx: *mut ()) {
info!("DMA IRQ!");
}

View File

@ -40,7 +40,24 @@ impl<'d, T: Pin> Input<'d, T> {
pub fn new(pin: impl PeripheralBorrow<Target = T> + 'd, pull: Pull) -> Self {
unborrow!(pin);
// todo
unsafe {
pin.pad_ctrl().write(|w| {
w.set_ie(true);
match pull {
Pull::Up => w.set_pue(true),
Pull::Down => w.set_pde(true),
Pull::None => {}
}
});
// disable output in SIO, to use it as input
pin.sio_oe().value_clr().write_value(1 << pin.pin());
pin.io().ctrl().write(|w| {
w.set_funcsel(pac::io::vals::Gpio0CtrlFuncsel::SIO_0.0);
});
}
Self {
pin,
phantom: PhantomData,
@ -62,8 +79,8 @@ impl<'d, T: Pin> InputPin for Input<'d, T> {
}
fn is_low(&self) -> Result<bool, Self::Error> {
// todo
Ok(true)
let val = 1 << self.pin.pin();
Ok(unsafe { self.pin.sio_in().read() } & val == 0)
}
}