This commit is contained in:
J / Jacob Babich
2022-04-13 01:49:16 -04:00
parent 79de6fab06
commit 816520ed77
9 changed files with 588 additions and 88 deletions

View File

@@ -6,6 +6,8 @@ use core::ptr;
use panic_halt as _; // you can put a breakpoint on `rust_begin_unwind` to catch panics
use cortex_m_rt::entry;
use test_cortex_m4_rust::{Function, Bit, ReadablePinSetup, WritablePinSetup, PortSetup, Port, setup_board, H, L};
// use test_cortex_m4_rust::setup_board;
// Begin .h file contents
@@ -15,9 +17,9 @@ const GPIO_PORTF_DIR_R: *mut u32 = 0x40025400 as *mut u32;
const GPIO_PORTF_AFSEL_R: *mut u32 = 0x40025420 as *mut u32;
const GPIO_PORTF_PUR_R: *mut u32 = 0x40025510 as *mut u32;
const GPIO_PORTF_DEN_R: *mut u32 = 0x4002551C as *mut u32;
const GPIO_PORTF_LOCK_R: *mut u32 = 0x40025520 as *mut u32;
const GPIO_PORTF_CR_R: *mut u32 = 0x40025524 as *mut u32;
const GPIO_PORTF_AMSEL_R: *mut u32 = 0x40025528 as *mut u32;
const GPIOLOCK_PORT_F: *mut u32 = 0x40025520 as *mut u32;
const GPIOCR_PORT_F: *mut u32 = 0x40025524 as *mut u32;
const GPIOAMSEL_PORT_F: *mut u32 = 0x40025528 as *mut u32;
const GPIO_PORTF_PCTL_R: *mut u32 = 0x4002552C as *mut u32;
const SYSCTL_RCGCPIO_R: *mut u32 = 0x400FE608 as *mut u32;
@@ -41,26 +43,26 @@ const BLUE: u8 = 0b0000_0100;
fn setup_port_f() {
// 1) activate clock for Port F
unsafe {
ptr::write_volatile(
SYSCTL_RCGCPIO_R,
ptr::read_volatile(SYSCTL_RCGCPIO_R) | 0x00_00_00_20,
);
}
// unsafe {
// ptr::write_volatile(
// SYSCTL_RCGCPIO_R,
// ptr::read_volatile(SYSCTL_RCGCPIO_R) | 0x00_00_00_20,
// );
// }
// Delay
for _ in 0u8..2u8 {}
// for _ in 0u8..2u8 {}
// 2) unlock GPIO Port F
unsafe {
ptr::write_volatile(GPIO_PORTF_LOCK_R, 0x4C4F434B);
// allow changes to PF4-0
// only PF0 needs to be unlocked, other bits can't be locked
ptr::write_volatile(GPIO_PORTF_CR_R, 0b0001_1111);
}
// unsafe {
// ptr::write_volatile(GPIOLOCK_PORT_F, 0x4C4F434B);
// // allow changes to PF4-0
// // only PF0 needs to be unlocked, other bits can't be locked
// ptr::write_volatile(GPIOCR_PORT_F, 0b0001_1111);
// }
// 3) disable analog on PF
unsafe { ptr::write_volatile(GPIO_PORTF_AMSEL_R, 0x00) }
// unsafe { ptr::write_volatile(GPIOAMSEL_PORT_F, 0x00) }
// 4) PCTL GPIO on PF4-0
unsafe {
@@ -68,9 +70,9 @@ fn setup_port_f() {
}
// 5) PF4,PF0 in, PF3-1 out
unsafe {
ptr::write_volatile(GPIO_PORTF_DIR_R, 0x0E);
}
// unsafe {
// ptr::write_volatile(GPIO_PORTF_DIR_R, 0x0E);
// }
// 6) disable alt funct on PF7-0
unsafe {
ptr::write_volatile(GPIO_PORTF_AFSEL_R, 0x00);
@@ -97,6 +99,26 @@ fn output_to_port_f(value: u8) {
}
}
// #[entry]
// fn main() -> ! {
// let board = setup_board();
// let port_f = board.setup_gpio_port(Port::F, PortSetup);
// let switches = port_f.setup_readable_pins([Bit::Zero, Bit::Four], ReadablePinSetup);
// let [sw1, sw2] = switches.pins();
// let mut rgb_led = port_f.setup_writable_pins(&[Bit::One, Bit::Three, Bit::Two], WritablePinSetup { drive: PinDrive::Digital });
// loop {
// match switches.read_all() {
// [L, L] => rgb_led.write_all([L, H, L]),
// [L, H] => rgb_led.write_all([L, L, H]),
// [H, L] => rgb_led.write_all([L, H, L]),
// [H, H] => rgb_led.write_all([L, L, L]),
// }
// }
// }
#[entry]
fn main() -> ! {
setup_port_f();
@@ -105,12 +127,14 @@ fn main() -> ! {
let status = input_from_port_f();
match status {
0x01 => output_to_port_f(BLUE),
0x10 => output_to_port_f(RED),
0x00 => output_to_port_f(GREEN),
0x00 => output_to_port_f(RED | GREEN),
0x01 => output_to_port_f(RED),
0x10 => output_to_port_f(GREEN),
0x11 => output_to_port_f(BLACK),
// Impossible case
_ => output_to_port_f(RED | BLUE),
_ => output_to_port_f(BLUE),
}
}
}
// TODO: implement an extremely simple example of the task system (using a timer trigger) that is a traffic light (green -> yellow -> red -> green...)