This commit is contained in:
J / Jacob Babich
2022-04-14 16:03:09 -04:00
parent 273d9308ec
commit d60800ae22
2 changed files with 66 additions and 22 deletions

View File

@@ -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 /// The memory address of the data (DATA) register for this port
fn data(&self) -> *mut u32 { fn data(&self) -> *mut u32 {
match self.port { match self.port {
@@ -145,6 +154,7 @@ impl PortIO {
} }
impl PortIO { impl PortIO {
// TODO: refactor into private setup_pins function
pub fn setup_readable_pins<const N: usize>( pub fn setup_readable_pins<const N: usize>(
&self, &self,
bits: &[Bit; N], bits: &[Bit; N],
@@ -197,13 +207,36 @@ impl PortIO {
} }
} }
// Configure pull-up and pull-down resistors
// TODO: finish
match options.pull_up { match options.pull_up {
Some(true) => todo!(), Some(true) => {
Some(false) => todo!(), unsafe {
None => todo!(), 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(); 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 data_address = self.data();
let pins: [WritablePin; N] = bits.map(|bit| WritablePin { data_address, bit }); let pins: [WritablePin; N] = bits.map(|bit| WritablePin { data_address, bit });

View File

@@ -78,26 +78,26 @@ fn setup_port_f() {
// ptr::write_volatile(GPIO_PORTF_AFSEL_R, 0x00); // ptr::write_volatile(GPIO_PORTF_AFSEL_R, 0x00);
// } // }
// enable pull-up on PF0 and PF4 // enable pull-up on PF0 and PF4
unsafe { // unsafe {
ptr::write_volatile(GPIO_PORTF_PUR_R, 0x11); // ptr::write_volatile(GPIO_PORTF_PUR_R, 0x11);
} // }
// 7) enable digital I/O on PF4-0 // 7) enable digital I/O on PF4-0
unsafe { // unsafe {
ptr::write_volatile(GPIO_PORTF_DEN_R, 0x1F); // ptr::write_volatile(GPIO_PORTF_DEN_R, 0x1F);
} // }
} }
fn input_from_port_f() -> u32 { // fn input_from_port_f() -> u32 {
unsafe { // unsafe {
ptr::read_volatile(GPIO_PORTF_DATA_R) & u32::from(SW1 | SW2) // ptr::read_volatile(GPIO_PORTF_DATA_R) & u32::from(SW1 | SW2)
} // }
} // }
fn output_to_port_f(value: u8) { // fn output_to_port_f(value: u8) {
unsafe { // unsafe {
ptr::write_volatile(GPIO_PORTF_DATA_R, u32::from(value)); // ptr::write_volatile(GPIO_PORTF_DATA_R, u32::from(value));
} // }
} // }
#[entry] #[entry]
fn main() -> ! { fn main() -> ! {
@@ -106,6 +106,7 @@ fn main() -> ! {
let switches = port_f.setup_readable_pins(&[Bit::Zero, Bit::Four], ReadablePinSetup { let switches = port_f.setup_readable_pins(&[Bit::Zero, Bit::Four], ReadablePinSetup {
function: Function::Digital, function: Function::Digital,
pull_up: Some(true),
}); });
let [sw1, sw2] = switches.pins(); let [sw1, sw2] = switches.pins();