ionpak-thermostat/firmware/src/board/gpio.rs

79 lines
2.5 KiB
Rust
Raw Normal View History

use core::slice::{from_raw_parts, from_raw_parts_mut};
2019-07-31 00:25:35 +08:00
use embedded_hal::digital::v2::{InputPin, OutputPin};
pub trait Gpio where Self: Sized {
fn into_output(self) -> GpioOutput<Self>;
fn into_input(self) -> GpioInput<Self>;
}
pub struct GpioInput<PIN>(PIN);
pub struct GpioOutput<PIN>(PIN);
macro_rules! def_gpio {
($PORT: tt, $PIN: tt, $idx: expr) => (
impl $PIN {
fn data(&self) -> &u32 {
2019-09-02 06:01:18 +08:00
let gpio = tm4c129x::$PORT::ptr();
let data = unsafe { from_raw_parts(gpio as *const _ as *mut u32, 0x100) };
&data[(1 << $idx) as usize]
}
fn data_mut(&mut self) -> &mut u32 {
2019-09-02 06:01:18 +08:00
let gpio = tm4c129x::$PORT::ptr();
let data = unsafe { from_raw_parts_mut(gpio as *const _ as *mut u32, 0x100) };
&mut data[(1 << $idx) as usize]
}
}
2019-07-31 00:25:35 +08:00
impl Gpio for $PIN {
fn into_output(self) -> GpioOutput<Self> {
let gpio = unsafe { &*tm4c129x::$PORT::ptr() };
2019-09-02 05:47:15 +08:00
gpio.dir.modify(|r, w| w.dir().bits(r.dir().bits() | (1 << $idx)));
gpio.den.modify(|r, w| w.den().bits(r.den().bits() | (1 << $idx)));
2019-07-31 00:25:35 +08:00
GpioOutput(self)
}
fn into_input(self) -> GpioInput<Self> {
let gpio = unsafe { &*tm4c129x::$PORT::ptr() };
gpio.dir.modify(|r, w| w.dir().bits(r.dir().bits() & !(1 << $idx)));
2019-09-02 05:47:15 +08:00
gpio.den.modify(|r, w| w.den().bits(r.den().bits() | (1 << $idx)));
2019-07-31 00:25:35 +08:00
GpioInput(self)
}
}
impl InputPin for GpioInput<$PIN> {
type Error = ();
fn is_high(&self) -> Result<bool, Self::Error> {
Ok(*self.0.data() != 0)
2019-07-31 00:25:35 +08:00
}
fn is_low(&self) -> Result<bool, Self::Error> {
Ok(*self.0.data() == 0)
2019-07-31 00:25:35 +08:00
}
}
impl OutputPin for GpioOutput<$PIN> {
type Error = ();
fn set_low(&mut self) -> Result<(), Self::Error> {
*self.0.data_mut() = 0;
2019-07-31 00:25:35 +08:00
Ok(())
}
fn set_high(&mut self) -> Result<(), Self::Error> {
*self.0.data_mut() = 0xFF;
2019-07-31 00:25:35 +08:00
Ok(())
}
}
)
}
pub struct PB4;
def_gpio!(GPIO_PORTB_AHB, PB4, 4);
pub struct PB5;
def_gpio!(GPIO_PORTB_AHB, PB5, 5);
pub struct PE4;
2019-09-02 04:28:46 +08:00
def_gpio!(GPIO_PORTE_AHB, PE4, 4);
2019-07-31 00:25:35 +08:00
pub struct PE5;
def_gpio!(GPIO_PORTE_AHB, PE5, 5);
2019-11-11 05:27:20 +08:00
pub struct PP4;
def_gpio!(GPIO_PORTP, PP4, 4);
pub struct PP5;
def_gpio!(GPIO_PORTP, PP5, 5);