From db033c7ee6fa44400da46e0e7384b7e422d14cfc Mon Sep 17 00:00:00 2001 From: J / Jacob Babich Date: Sun, 17 Apr 2022 13:18:43 -0400 Subject: [PATCH] trying out uart and asm --- src/main.rs | 70 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 24 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0d69828..d374c29 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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,20 +44,26 @@ 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) // ptr::write_volatile(UART0_IBRD_R, 8); ptr::write_volatile(UART0_IBRD_R, 8); - + // ignore: // FBRD = int(0.1267 * 64 + 0.5) = 8 // FBRD = round(0.5104 * 64 ) = 33 --- that ain't the number you wrote but ok // ptr::write_volatile(UART0_FBRD_R, 44); @@ -65,25 +72,32 @@ 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 { + 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"); + } + } } }