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
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<const N: usize>(
&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 });

View File

@@ -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();