trying out uart and asm

This commit is contained in:
J / Jacob Babich
2022-04-17 13:18:43 -04:00
parent ac0e1067d0
commit db033c7ee6

View File

@@ -1,13 +1,14 @@
#![no_std]
#![no_main]
use core::ptr;
use core::{arch::asm, ptr};
use panic_halt as _; // you can put a breakpoint on `rust_begin_unwind` to catch panics
use cortex_m_rt::entry;
use driver_and_task_library::{
setup_board, Function, Pin, Port, PortOptions, ReadablePinOptions, WritablePinOptions, H, L, UsableBoard,
setup_board, Function, Pin, Port, PortOptions, ReadablePinOptions, UsableBoard,
WritablePinOptions, H, L,
};
const SYSCTL_RCGC1_R: *mut u32 = 0x400FE104 as *mut u32;
@@ -43,14 +44,20 @@ const PINS_0_AND_1: u32 = 0b0000_0011;
fn uart0_init(board: UsableBoard) {
unsafe {
// activate UART0
ptr::write_volatile(SYSCTL_RCGC1_R, ptr::read_volatile(SYSCTL_RCGC1_R) | SYSCTL_RCGC1_UART0);
ptr::write_volatile(
SYSCTL_RCGC1_R,
ptr::read_volatile(SYSCTL_RCGC1_R) | SYSCTL_RCGC1_UART0,
);
// activate port A
// ptr::write_volatile(SYSCTL_RCGC2_R, ptr::read_volatile(SYSCTL_RCGC2_R) | SYSCTL_RCGC2_GPIOA);
// ^ commented in favor of v
board.setup_gpio_port(Port::A, PortOptions);
// disable UART while setting it up
ptr::write_volatile(UART0_CTL_R, ptr::read_volatile(UART0_CTL_R) & !UART_CTL_UARTEN);
ptr::write_volatile(
UART0_CTL_R,
ptr::read_volatile(UART0_CTL_R) & !UART_CTL_UARTEN,
);
// ignore: // IBRD = int(50,000,000 / (16 * 115,200)) = int(27.1267)
// IBRD = int(16,000,000 / (16 * 115,200)) = int(8.680)
@@ -65,23 +72,30 @@ fn uart0_init(board: UsableBoard) {
// 8 bit word length (no parity bits, one stop bit, FIFOs)
// ptr::write_volatile(UART0_LCRH_R, UART_LCRH_WLEN_8|UART_LCRH_FEN);
// 8 bit word length (no parity bits, one stop bit, no FIFOs)
ptr::write_volatile(UART0_LCRH_R, UART_LCRH_WLEN_8&!UART_LCRH_FEN);
ptr::write_volatile(UART0_LCRH_R, UART_LCRH_WLEN_8 & !UART_LCRH_FEN);
// enable UART since it's been set up
ptr::write_volatile(UART0_CTL_R, ptr::read_volatile(UART0_CTL_R) | UART_CTL_UARTEN);
ptr::write_volatile(
UART0_CTL_R,
ptr::read_volatile(UART0_CTL_R) | UART_CTL_UARTEN,
);
// enable alt funct on PA1-0
ptr::write_volatile(GPIO_PORTA_AFSEL_R, ptr::read_volatile(GPIO_PORTA_AFSEL_R) | PINS_0_AND_1);
ptr::write_volatile(
GPIO_PORTA_AFSEL_R,
ptr::read_volatile(GPIO_PORTA_AFSEL_R) | PINS_0_AND_1,
);
// enable digital I/O on PA1-0
ptr::write_volatile(GPIO_PORTA_DEN_R, ptr::read_volatile(GPIO_PORTA_AFSEL_R) | PINS_0_AND_1);
ptr::write_volatile(
GPIO_PORTA_DEN_R,
ptr::read_volatile(GPIO_PORTA_AFSEL_R) | PINS_0_AND_1,
);
}
}
fn uart0_out_char(c: u8) {
loop {
let fr = unsafe {
ptr::read_volatile(UART0_FR_R)
};
let fr = unsafe { ptr::read_volatile(UART0_FR_R) };
if (fr & UART_FR_TXFF) == 0 {
break;
@@ -134,27 +148,35 @@ fn main() -> ! {
rgb_led.write_all(cyan);
uart0_init(board);
rgb_led.write_all(white);
for _ in 0..2 {
for c in ['H', 'a', 'y', '!', '\n', 'H', 'e', 'y', '!', '\n', 'H', 'e', 'y', '!', '\n', 'H', 'e', 'y', '!', '\n', ] {
for c in [
'H', 'a', 'y', '!', '\r', '\n', 'H', 'e', 'y', '!', '\r', '\n', 'H', 'e', 'y', '!',
'\r', '\n', 'H', 'e', 'y', '!', '\r', '\n',
] {
uart0_out_char(c as u8);
}
}
uart0_out_string("Those example string!\n");
uart0_out_string("Those example string!\r\n");
loop {
match switches.read_all() {
[L, L] => rgb_led.write_all(white),
[L, L] => {
rgb_led.write_all(white);
uart0_out_string("Hey! You're pressing the button down!\r\n");
}
[L, H] => rgb_led.write_all(blue),
[H, L] => rgb_led.write_all(red),
[H, H] => rgb_led.write_all(green),
}
for _ in 0..1000000 {
unsafe {
asm!("nop");
}
}
}
}