add stuff

This commit is contained in:
J / Jacob Babich
2022-04-13 14:13:33 -04:00
parent 816520ed77
commit 273d9308ec
14 changed files with 212 additions and 616 deletions

View File

@@ -2,7 +2,7 @@
use core::ptr;
use crate::{Bit, L};
use crate::L;
pub unsafe fn read(address: *mut u32) -> u32 {
ptr::read_volatile(address)
@@ -41,11 +41,15 @@ pub unsafe fn write_bits<const N: usize>(address: *mut u32, bits: &[u32; N], val
})
}
pub unsafe fn update_bits<const N: usize, Updater: Fn([bool; N]) -> [bool; N]>(address: *mut u32, bits: &[Bit; N], updater: Updater) {
pub unsafe fn update_bits<Updater: Fn([bool; N]) -> [bool; N], const N: usize>(
address: *mut u32,
bits: &[u32; N],
updater: Updater,
) {
write_bits(address, bits, updater(read_bits(address, bits)))
}
pub unsafe fn set_bits(address: *mut u32, bits: &[Bit]) {
pub unsafe fn set_bits(address: *mut u32, bits: &[u32]) {
update(address, |current| {
let mut new = current;
@@ -57,7 +61,7 @@ pub unsafe fn set_bits(address: *mut u32, bits: &[Bit]) {
new
})
}
pub unsafe fn clear_bits(address: *mut u32, bits: &[Bit]) {
pub unsafe fn clear_bits(address: *mut u32, bits: &[u32]) {
update(address, |current| {
let mut new = current;
@@ -69,7 +73,7 @@ pub unsafe fn clear_bits(address: *mut u32, bits: &[Bit]) {
new
})
}
pub unsafe fn toggle_bits(address: *mut u32, bits: &[Bit]) {
pub unsafe fn toggle_bits(address: *mut u32, bits: &[u32]) {
update(address, |current| {
let mut new = current;

View File

@@ -15,9 +15,12 @@ impl Board {
let port_io = PortIO { port };
unsafe {
memory::set_bits(registers::system::RCGCGPIO, &[port_io.run_mode_clock_gate_control()]);
memory::set_bits(
registers::system::RCGCGPIO,
&[port_io.run_mode_clock_gate_control() as u32],
);
}
port_io
}
}
@@ -40,7 +43,17 @@ pub struct PortIO {
port: Port,
}
// TODO: refactor to just be self.base() + offset all the time - no matching
impl PortIO {
/// The memory address of the alternate function select (AFSEL) register for this port
fn alternate_function_select(&self) -> *mut u32 {
match self.port {
Port::A => registers::gpio::afsel::PORT_A,
Port::F => registers::gpio::afsel::PORT_F,
_ => todo!(),
}
}
/// The memory address of the analog mode select (AMSEL) register for this port
fn analog_mode_select(&self) -> *mut u32 {
match self.port {
@@ -92,7 +105,6 @@ impl PortIO {
Port::A => registers::gpio::pctl::PORT_A,
Port::F => registers::gpio::pctl::PORT_F,
_ => todo!(),
}
}
@@ -118,7 +130,6 @@ impl PortIO {
// Apparently also for ADC!
}
impl PortIO {
/// The corresponding bit for this port in system's run-mode clock gate control (RCGC) register
fn run_mode_clock_gate_control(&self) -> Bit {
@@ -143,25 +154,58 @@ impl PortIO {
unsafe {
memory::write(self.lock(), UNLOCK);
memory::set_bits(self.commit(), bits);
memory::set_bits(self.commit(), &bits.map(|bit| bit as u32));
}
// Disable analog when it's not selected (and enable analog if it is)
match options.function {
Function::Analog => unsafe {
memory::set_bits(self.analog_mode_select(), bits);
memory::set_bits(self.analog_mode_select(), &bits.map(|bit| bit as u32));
},
_ => unsafe {
memory::clear_bits(self.analog_mode_select(), bits);
memory::clear_bits(self.analog_mode_select(), &bits.map(|bit| bit as u32));
},
}
unsafe {
memory::clear_bits(self.direction(), bits);
memory::clear_bits(self.direction(), &bits.map(|bit| bit as u32));
}
for bit in bits {
let mut memory_bits = [0; 4];
let min = (*bit as u32) * 4;
let max = min + 3;
let range = min..=max;
for (i, memory_bit) in range.enumerate() {
memory_bits[i] = memory_bit;
}
let values = match options.function {
Function::Analog => todo!(),
Function::Digital => [L, L, L, L],
Function::CAN => todo!(),
Function::I2C => todo!(),
Function::PWM => todo!(),
Function::UART => todo!(),
};
unsafe {
memory::write_bits(self.port_control(), &memory_bits, values);
}
}
// TODO: finish
match options.pull_up {
Some(true) => todo!(),
Some(false) => todo!(),
None => todo!(),
}
let data_address = self.data();
let pins: [ReadablePin; N] = bits.map(|bit| ReadablePin { data_address, bit });
@@ -178,35 +222,44 @@ impl PortIO {
unsafe {
memory::write(self.lock(), UNLOCK);
memory::set_bits(self.commit(), bits);
memory::set_bits(self.commit(), &bits.map(|bit| bit as u32));
}
// Disable analog when it's not selected (and enable analog if it is)
match options.function {
Function::Analog => unsafe {
memory::set_bits(self.analog_mode_select(), bits);
memory::set_bits(self.analog_mode_select(), &bits.map(|bit| bit as u32));
},
_ => unsafe {
memory::clear_bits(self.analog_mode_select(), bits);
memory::clear_bits(self.analog_mode_select(), &bits.map(|bit| bit as u32));
},
}
unsafe {
memory::set_bits(self.direction(), bits);
memory::set_bits(self.direction(), &bits.map(|bit| bit as u32));
}
unsafe {
for bit in bits {
let memory_bits = [0; N];
let values = match options.function {
Function::Analog => todo!(),
Function::Digital => todo!(),
Function::CAN => todo!(),
Function::I2C => todo!(),
Function::PWM => todo!(),
Function::UART => todo!(),
};
memory::write_bits(self.port_control(), memory_bits, values);
for bit in bits {
let mut memory_bits = [0; 4];
let min = (*bit as u32) * 4;
let max = min + 3;
let range = min..=max;
for (i, memory_bit) in range.enumerate() {
memory_bits[i] = memory_bit;
}
let values = match options.function {
Function::Analog => todo!(),
Function::Digital => [L, L, L, L],
Function::CAN => todo!(),
Function::I2C => todo!(),
Function::PWM => todo!(),
Function::UART => todo!(),
};
unsafe {
memory::write_bits(self.port_control(), &memory_bits, values);
}
}
@@ -214,6 +267,19 @@ impl PortIO {
// TODO: finish
match options.function {
Function::Analog | Function::Digital => {
unsafe {
memory::clear_bits(self.alternate_function_select(), &bits.map(|bit| bit as u32));
}
},
_ => {
unsafe {
memory::set_bits(self.alternate_function_select(), &bits.map(|bit| bit as u32));
}
},
}
let data_address = self.data();
let pins: [WritablePin; N] = bits.map(|bit| WritablePin { data_address, bit });
@@ -246,6 +312,7 @@ pub enum Function {
pub struct ReadablePinSetup {
pub function: Function,
pub pull_up: Option<bool>,
}
pub struct ReadablePins<const N: usize> {
data_address: *mut u32,
@@ -257,7 +324,7 @@ impl<const N: usize> ReadablePins<N> {
}
pub fn read_all(&self) -> [bool; N] {
unsafe { memory::read_bits(self.data_address, &self.pins.map(|pin| pin.bit)) }
unsafe { memory::read_bits(self.data_address, &self.pins.map(|pin| pin.bit as u32)) }
}
}
#[derive(Clone, Copy)]
@@ -285,10 +352,16 @@ impl<const N: usize> WritablePins<N> {
}
pub fn read_all(&self) -> [bool; N] {
unsafe { memory::read_bits(self.data_address, &self.pins.map(|pin| pin.bit)) }
unsafe { memory::read_bits(self.data_address, &self.pins.map(|pin| pin.bit as u32)) }
}
pub fn write_all(&mut self, values: [bool; N]) {
unsafe { memory::write_bits(self.data_address, &self.pins.map(|pin| pin.bit), values) }
unsafe {
memory::write_bits(
self.data_address,
&self.pins.map(|pin| pin.bit as u32),
values,
)
}
}
pub fn update_all<Updater: Fn([bool; N]) -> [bool; N]>(&mut self, updater: Updater) {
self.write_all(updater(self.read_all()));
@@ -296,17 +369,17 @@ impl<const N: usize> WritablePins<N> {
pub fn clear_all(&mut self) {
unsafe {
memory::clear_bits(self.data_address, &self.pins.map(|pin| pin.bit));
memory::clear_bits(self.data_address, &self.pins.map(|pin| pin.bit as u32));
}
}
pub fn set_all(&mut self) {
unsafe {
memory::set_bits(self.data_address, &self.pins.map(|pin| pin.bit));
memory::set_bits(self.data_address, &self.pins.map(|pin| pin.bit as u32));
}
}
pub fn toggle_all(&mut self) {
unsafe {
memory::toggle_bits(self.data_address, &self.pins.map(|pin| pin.bit));
memory::toggle_bits(self.data_address, &self.pins.map(|pin| pin.bit as u32));
}
}
}
@@ -323,17 +396,17 @@ impl WritablePin {
}
pub fn clear(&mut self) {
unsafe {
memory::clear_bits(self.data_address, &[self.bit]);
memory::clear_bits(self.data_address, &[self.bit as u32]);
}
}
pub fn set(&mut self) {
unsafe {
memory::set_bits(self.data_address, &[self.bit]);
memory::set_bits(self.data_address, &[self.bit as u32]);
}
}
pub fn toggle(&mut self) {
unsafe {
memory::toggle_bits(self.data_address, &[self.bit]);
memory::toggle_bits(self.data_address, &[self.bit as u32]);
}
}
}

59
src/main--ideas.rs Normal file
View File

@@ -0,0 +1,59 @@
#![no_std]
#![no_main]
use panic_halt as _;
use cortex_m_rt::entry;
use my_library::{Bit, Color, setup_board};
const H: bool = true;
const L: bool = false;
#[entry]
fn main() -> ! {
let board = setup_board();
let port_f = board.setup_port(Port::F);
let switches = port_f.setup_readable_pins([Bit::Zero, Bit::Four], PinSetup {
alternate_function: false,
analog: false,
pullup: true,
pctl: false,
});
let rgb_led = port_f.setup_writable_pins([Bit::One, Bit::Three, Bit::Two], PinSetup {
alternate_function: false,
analog: false,
pctl: false,
});
// Integrate PWM for arbitrary color support
let rgb_led_driver = rgb_led.driver();
// Maybe?
let every_5_seconds = board.time_trigger(5);
// Example of adding tasks
board.add_task(
some_kind_of_task,
10, // priority maybe?
every_5_seconds, // trigger every 5 seconds
);
loop {
match switches.read_all() {
[L, L] => rgb_led_driver.set_color(Color::Green),
[L, H] => rgb_led_driver.set_color(Color::Blue),
[H, L] => rgb_led_driver.set_color(Color::Red),
[H, H] => rgb_led_driver.set_color(Color::Black),
}
}
}
fn some_kind_of_task() {
// ...
}

View File

@@ -65,18 +65,18 @@ fn setup_port_f() {
// unsafe { ptr::write_volatile(GPIOAMSEL_PORT_F, 0x00) }
// 4) PCTL GPIO on PF4-0
unsafe {
ptr::write_volatile(GPIO_PORTF_PCTL_R, 0x00000000);
}
// unsafe {
// ptr::write_volatile(GPIO_PORTF_PCTL_R, 0x00000000);
// }
// 5) PF4,PF0 in, PF3-1 out
// 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);
}
// unsafe {
// ptr::write_volatile(GPIO_PORTF_AFSEL_R, 0x00);
// }
// enable pull-up on PF0 and PF4
unsafe {
ptr::write_volatile(GPIO_PORTF_PUR_R, 0x11);
@@ -99,42 +99,44 @@ 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();
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 {
function: Function::Digital,
});
let [sw1, sw2] = switches.pins();
let mut rgb_led = port_f.setup_writable_pins(&[Bit::One, Bit::Three, Bit::Two], WritablePinSetup { function: Function::Digital });
loop {
let status = input_from_port_f();
match status {
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(BLUE),
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([H, L, L]),
}
}
}
// #[entry]
// fn main() -> ! {
// setup_port_f();
// loop {
// let status = input_from_port_f();
// match status {
// 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(BLUE),
// }
// }
// }
// TODO: implement an extremely simple example of the task system (using a timer trigger) that is a traffic light (green -> yellow -> red -> green...)