From d60800ae22ddce64ca5e4908f37bd747dee37628 Mon Sep 17 00:00:00 2001 From: J / Jacob Babich Date: Thu, 14 Apr 2022 16:03:09 -0400 Subject: [PATCH] new --- src/lib/mod.rs | 55 ++++++++++++++++++++++++++++++++++++++++++++------ src/main.rs | 33 +++++++++++++++--------------- 2 files changed, 66 insertions(+), 22 deletions(-) diff --git a/src/lib/mod.rs b/src/lib/mod.rs index 569e474..969e2e8 100644 --- a/src/lib/mod.rs +++ b/src/lib/mod.rs @@ -72,6 +72,15 @@ impl PortIO { } } + /// The memory address of the digital enable (DEN) register for this port + fn digital_enable(&self) -> *mut u32 { + match self.port { + Port::A => registers::gpio::den::PORT_A, + Port::F => registers::gpio::den::PORT_F, + _ => todo!(), + } + } + /// The memory address of the data (DATA) register for this port fn data(&self) -> *mut u32 { match self.port { @@ -145,6 +154,7 @@ impl PortIO { } impl PortIO { + // TODO: refactor into private setup_pins function pub fn setup_readable_pins( &self, bits: &[Bit; N], @@ -197,13 +207,36 @@ impl PortIO { } } - - // TODO: finish - + // Configure pull-up and pull-down resistors match options.pull_up { - Some(true) => todo!(), - Some(false) => todo!(), - None => todo!(), + Some(true) => { + unsafe { + memory::set_bits(self.pull_up_select(), &bits.map(|bit| bit as u32)); + } + }, + Some(false) => { + unsafe { + memory::set_bits(self.pull_down_select(), &bits.map(|bit| bit as u32)); + } + }, + None => { + unsafe { + memory::clear_bits(self.pull_up_select(), &bits.map(|bit| bit as u32)); + } + unsafe { + memory::clear_bits(self.pull_down_select(), &bits.map(|bit| bit as u32)); + } + }, + } + + match options.function { + Function::Digital => unsafe { + memory::set_bits(self.digital_enable(), &bits.map(|bit| bit as u32)); + }, + Function::Analog => unsafe { + memory::clear_bits(self.digital_enable(), &bits.map(|bit| bit as u32)); + }, + _ => todo!(), } let data_address = self.data(); @@ -280,6 +313,16 @@ impl PortIO { }, } + match options.function { + Function::Digital => unsafe { + memory::set_bits(self.digital_enable(), &bits.map(|bit| bit as u32)); + }, + Function::Analog => unsafe { + memory::clear_bits(self.digital_enable(), &bits.map(|bit| bit as u32)); + }, + _ => todo!(), + } + let data_address = self.data(); let pins: [WritablePin; N] = bits.map(|bit| WritablePin { data_address, bit }); diff --git a/src/main.rs b/src/main.rs index f392c82..d927b8c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -78,26 +78,26 @@ fn setup_port_f() { // ptr::write_volatile(GPIO_PORTF_AFSEL_R, 0x00); // } // enable pull-up on PF0 and PF4 - unsafe { - ptr::write_volatile(GPIO_PORTF_PUR_R, 0x11); - } + // unsafe { + // ptr::write_volatile(GPIO_PORTF_PUR_R, 0x11); + // } // 7) enable digital I/O on PF4-0 - unsafe { - ptr::write_volatile(GPIO_PORTF_DEN_R, 0x1F); - } + // unsafe { + // ptr::write_volatile(GPIO_PORTF_DEN_R, 0x1F); + // } } -fn input_from_port_f() -> u32 { - unsafe { - ptr::read_volatile(GPIO_PORTF_DATA_R) & u32::from(SW1 | SW2) - } -} +// fn input_from_port_f() -> u32 { +// unsafe { +// ptr::read_volatile(GPIO_PORTF_DATA_R) & u32::from(SW1 | SW2) +// } +// } -fn output_to_port_f(value: u8) { - unsafe { - ptr::write_volatile(GPIO_PORTF_DATA_R, u32::from(value)); - } -} +// fn output_to_port_f(value: u8) { +// unsafe { +// ptr::write_volatile(GPIO_PORTF_DATA_R, u32::from(value)); +// } +// } #[entry] fn main() -> ! { @@ -106,6 +106,7 @@ fn main() -> ! { let switches = port_f.setup_readable_pins(&[Bit::Zero, Bit::Four], ReadablePinSetup { function: Function::Digital, + pull_up: Some(true), }); let [sw1, sw2] = switches.pins();