pub trait Bank<R: register::GpioRegisters> {
const BASE_ADDRESS: usize;
fn get_handle() -> io::Gpio<R> {
io::Gpio::new(Self::addr())
}
fn addr() -> *mut R {
Self::BASE_ADDRESS as *mut R
}
}
pub mod register {
use crate::{Interrupt, IoDir, Level};
pub unsafe trait GpioRegisters {
fn set_dir(ptr: *mut Self, pin: u32, dir: IoDir);
fn set_active_state(ptr: *mut Self, pin: u32, active_state: crate::Level);
fn set_interrupt(ptr: *mut Self, pin: u32, interrupt: Interrupt);
fn read(ptr: *const Self, pin: u32) -> Level;
fn write(ptr: *mut Self, pin: u32, level: Level);
fn interrupt_pending(ptr: *mut Self, pin: u32) -> bool;
}
}
pub mod io {
use super::register::GpioRegisters;
use crate::{Interrupt, IoDir, Level};
#[doc(hidden)]
pub struct Gpio<R: GpioRegisters> {
registers: *mut R,
}
impl<R> Gpio<R>
where
R: GpioRegisters,
{
pub(super) fn new(registers: *mut R) -> Self {
Self { registers }
}
#[inline]
pub fn set_dir(&mut self, pin: u32, dir: IoDir) {
<R as GpioRegisters>::set_dir(self.registers, pin, dir);
}
#[inline]
pub fn set_active_state(&mut self, pin: u32, active_state: Level) {
<R as GpioRegisters>::set_active_state(self.registers, pin, active_state);
}
#[inline]
pub fn set_interrupt(&mut self, pin: u32, interrupt: Interrupt) {
<R as GpioRegisters>::set_interrupt(self.registers, pin, interrupt);
}
#[inline]
pub fn read(&self, pin: u32) -> Level {
<R as GpioRegisters>::read(self.registers, pin)
}
#[inline]
pub fn write(&mut self, pin: u32, level: Level) {
<R as GpioRegisters>::write(self.registers, pin, level);
}
pub fn interrupt_pending(&mut self, pin: u32) -> bool {
<R as GpioRegisters>::interrupt_pending(self.registers, pin)
}
}
}