#![allow(dead_code)]
#[repr(u8)]
#[derive(Copy, Clone)]
pub enum PinB {
PB0 = 0, PB1 = 1, PB2 = 2, PB3 = 3, PB4 = 4, PB5 = 5, }
#[repr(u8)]
#[derive(Copy, Clone)]
pub enum PinC {
PC0 = 0, PC1 = 1, PC2 = 2, PC3 = 3, PC4 = 4, PC5 = 5, }
#[repr(u8)]
#[derive(Copy, Clone)]
pub enum PinD {
PD0 = 0, PD1 = 1, PD2 = 2, PD3 = 3, PD4 = 4, PD5 = 5, PD6 = 6, PD7 = 7, }
pub struct PortB {
_priv: (),
}
pub struct PortC {
_priv: (),
}
pub struct PortD {
_priv: (),
}
static mut B_TAKING: bool = false;
static mut C_TAKING: bool = false;
static mut D_TAKING: bool = false;
impl PortB {
const DDRB: *mut u8 = 0x24 as *mut u8;
const PINB: *mut u8 = 0x23 as *mut u8;
const PORTB: *mut u8 = 0x25 as *mut u8;
pub fn take() -> Option<Self> {
unsafe {
if B_TAKING {
None
} else {
B_TAKING = true;
Some(PortB { _priv: () })
}
}
}
pub fn toggle(&self, pin: PinB) {
unsafe {
core::ptr::write_volatile(Self::PINB, 1 << pin as u8);
}
}
pub fn set_output(&self, pin: PinB) -> PinB {
unsafe {
let val = core::ptr::read_volatile(Self::DDRB);
core::ptr::write_volatile(Self::DDRB, val | 1 << pin as u8);
}
pin
}
pub fn set_input(&self, pin: PinB, use_pullup: bool) -> PinB {
unsafe {
let port_val = core::ptr::read_volatile(Self::PORTB);
if use_pullup {
core::ptr::write_volatile(Self::PORTB, port_val | (1 << pin as u8));
} else {
core::ptr::write_volatile(Self::PORTB, port_val & !(1 << pin as u8));
}
let val = core::ptr::read_volatile(Self::DDRB);
core::ptr::write_volatile(Self::DDRB, val & !(1 << pin as u8));
}
pin
}
pub fn set_high(&self, pin: PinB) {
unsafe {
let val = core::ptr::read_volatile(Self::PORTB);
core::ptr::write_volatile(Self::PORTB, val | 1 << pin as u8);
}
}
pub fn set_low(&self, pin: PinB) {
unsafe {
let val = core::ptr::read_volatile(Self::PORTB);
core::ptr::write_volatile(Self::PORTB, val & !(1 << pin as u8));
}
}
pub fn is_high(&self, pin: PinB) -> bool {
unsafe {
(core::ptr::read_volatile(Self::PINB) & (1 << pin as u8)) != 0
}
}
}
impl Drop for PortB {
fn drop(&mut self) {
unsafe {
B_TAKING = false;
}
}
}
impl PortC {
const DDRC: *mut u8 = 0x27 as *mut u8;
const PINC: *mut u8 = 0x26 as *mut u8;
const PORTC: *mut u8 = 0x28 as *mut u8;
pub fn take() -> Option<Self> {
unsafe {
if C_TAKING {
None
} else {
C_TAKING = true;
Some(PortC { _priv: () })
}
}
}
pub fn toggle(&self, pin: PinC) {
unsafe {
core::ptr::write_volatile(Self::PINC, 1 << pin as u8);
}
}
pub fn set_output(&self, pin: PinC) -> PinC {
unsafe {
let val = core::ptr::read_volatile(Self::DDRC);
core::ptr::write_volatile(Self::DDRC, val | 1 << pin as u8);
}
pin
}
pub fn set_input(&self, pin: PinC, use_pullup: bool) -> PinC {
unsafe {
let port_val = core::ptr::read_volatile(Self::PORTC);
if use_pullup {
core::ptr::write_volatile(Self::PORTC, port_val | (1 << pin as u8));
} else {
core::ptr::write_volatile(Self::PORTC, port_val & !(1 << pin as u8));
}
let val = core::ptr::read_volatile(Self::DDRC);
core::ptr::write_volatile(Self::DDRC, val & !(1 << pin as u8));
}
pin
}
pub fn set_high(&self, pin: PinC) {
unsafe {
let val = core::ptr::read_volatile(Self::PORTC);
core::ptr::write_volatile(Self::PORTC, val | 1 << pin as u8);
}
}
pub fn set_low(&self, pin: PinC) {
unsafe {
let val = core::ptr::read_volatile(Self::PORTC);
core::ptr::write_volatile(Self::PORTC, val & !(1 << pin as u8));
}
}
pub fn is_high(&self, pin: PinC) -> bool {
unsafe {
(core::ptr::read_volatile(Self::PINC) & (1 << pin as u8)) != 0
}
}
}
impl Drop for PortC {
fn drop(&mut self) {
unsafe {
C_TAKING = false;
}
}
}
impl PortD {
const DDRD: *mut u8 = 0x2A as *mut u8;
const PIND: *mut u8 = 0x29 as *mut u8;
const PORTD: *mut u8 = 0x2B as *mut u8;
pub fn take() -> Option<Self> {
unsafe {
if D_TAKING {
None
} else {
D_TAKING = true;
Some(PortD { _priv: () })
}
}
}
pub fn toggle(&self, pin: PinD) {
unsafe {
core::ptr::write_volatile(Self::PIND, 1 << pin as u8);
}
}
pub fn set_output(&self, pin: PinD) -> PinD {
unsafe {
let val = core::ptr::read_volatile(Self::DDRD);
core::ptr::write_volatile(Self::DDRD, val | 1 << pin as u8);
}
pin
}
pub fn set_input(&self, pin: PinD, use_pullup: bool) -> PinD {
unsafe {
let port_val = core::ptr::read_volatile(Self::PORTD);
if use_pullup {
core::ptr::write_volatile(Self::PORTD, port_val | (1 << pin as u8));
} else {
core::ptr::write_volatile(Self::PORTD, port_val & !(1 << pin as u8));
}
let val = core::ptr::read_volatile(Self::DDRD);
core::ptr::write_volatile(Self::DDRD, val & !(1 << pin as u8));
}
pin
}
pub fn set_high(&self, pin: PinD) {
unsafe {
let val = core::ptr::read_volatile(Self::PORTD);
core::ptr::write_volatile(Self::PORTD, val | 1 << pin as u8);
}
}
pub fn set_low(&self, pin: PinD) {
unsafe {
let val = core::ptr::read_volatile(Self::PORTD);
core::ptr::write_volatile(Self::PORTD, val & !(1 << pin as u8));
}
}
pub fn is_high(&self, pin: PinD) -> bool {
unsafe {
(core::ptr::read_volatile(Self::PIND) & (1 << pin as u8)) != 0
}
}
}
impl Drop for PortD {
fn drop(&mut self) {
unsafe {
D_TAKING = false;
}
}
}