starting uart and refactoring
This commit is contained in:
125
src/main.rs
125
src/main.rs
@@ -54,7 +54,8 @@ static HEAP: BumpPointerAlloc = BumpPointerAlloc {
|
||||
end: 0x2000_0200,
|
||||
};
|
||||
|
||||
#[macro_use]
|
||||
// TODO: remove or fix
|
||||
// #[macro_use]
|
||||
extern crate alloc;
|
||||
use alloc::string::String;
|
||||
|
||||
@@ -63,39 +64,31 @@ use panic_halt as _; // you can put a breakpoint on `rust_begin_unwind` to catch
|
||||
|
||||
use cortex_m_rt::entry;
|
||||
use driver_and_task_library::{
|
||||
setup_board, Function, Pin, Port, PortOptions, ReadablePinOptions, UsableBoard,
|
||||
WritablePinOptions, H, L, Pull,
|
||||
setup_board, Function, GPIOPortOptions, Pin, Port, Pull, ReadablePinOptions,
|
||||
WritablePinOptions, H, L,
|
||||
};
|
||||
|
||||
const SYSCTL_RCGC1_R: *mut u32 = 0x400FE104 as *mut u32;
|
||||
const SYSCTL_RCGC2_R: *mut u32 = 0x400FE108 as *mut u32;
|
||||
|
||||
/// UART0 data register
|
||||
const UART0_DR_R: *mut u32 = 0x4000C000 as *mut u32;
|
||||
/// UART0 flag register
|
||||
const UART0_FR_R: *mut u32 = 0x4000C018 as *mut u32;
|
||||
/// UART0 integer baud rate register
|
||||
const UART0_IBRD_R: *mut u32 = 0x4000C024 as *mut u32;
|
||||
/// UART0 fractional baud rate register
|
||||
const UART0_FBRD_R: *mut u32 = 0x4000C028 as *mut u32;
|
||||
/// UART0 line control register
|
||||
const UART0_LCRH_R: *mut u32 = 0x4000C02C as *mut u32;
|
||||
/// UART0 control register
|
||||
const UART0_CTL_R: *mut u32 = 0x4000C030 as *mut u32;
|
||||
|
||||
const GPIO_PORTA_AFSEL_R: *mut u32 = 0x40004420 as *mut u32;
|
||||
const GPIO_PORTA_DEN_R: *mut u32 = 0x4000451C as *mut u32;
|
||||
|
||||
// page 219
|
||||
/// 16 MHz
|
||||
const SYSTEM_OSC_CLOCK_SPEED: u32 = 16_000_000;
|
||||
// the MOSC is variable frequeny (5 MHz to 25 MHz)
|
||||
|
||||
// the XOSC can act as a real time clock as well!
|
||||
|
||||
// The internal system clock (SysClk), is derived from any of the above sources plus two others: the
|
||||
// output of the main internal PLL and the precision internal oscillator divided by four (4 MHz ± 1%).
|
||||
// The frequency of the PLL clock reference must be in the range of 5 MHz to 25 MHz (inclusive).
|
||||
// Table 5-3 on page 220 shows how the various clock sources can be used in a system
|
||||
|
||||
/// UART0 Clock Gating Control
|
||||
const SYSCTL_RCGC1_UART0: u32 = 0x00000001;
|
||||
/// port A Clock Gating Control
|
||||
const SYSCTL_RCGC2_GPIOA: u32 = 0x00000001;
|
||||
/// UART Enable
|
||||
const UART_CTL_UARTEN: u32 = 0x00000001;
|
||||
/// 8 bit word length
|
||||
@@ -109,18 +102,21 @@ const UART_FR_RXFE: u32 = 0x00000010;
|
||||
/// Pins 0 and 1
|
||||
const PINS_0_AND_1: u32 = 0b0000_0011;
|
||||
|
||||
fn uart0_init(board: &mut UsableBoard) {
|
||||
fn uart0_init() {
|
||||
unsafe {
|
||||
// activate UART0
|
||||
ptr::write_volatile(
|
||||
SYSCTL_RCGC1_R,
|
||||
ptr::read_volatile(SYSCTL_RCGC1_R) | SYSCTL_RCGC1_UART0,
|
||||
);
|
||||
// write_color(MAGENTA);
|
||||
|
||||
// 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);
|
||||
// For some reason, 7 no-ops are needed to stall the CPU while UART is enabled
|
||||
for _ in 0..7 {
|
||||
asm!("nop");
|
||||
}
|
||||
|
||||
// TODO / WIP: done up to here
|
||||
|
||||
// disable UART while setting it up
|
||||
ptr::write_volatile(
|
||||
@@ -128,7 +124,6 @@ fn uart0_init(board: &mut UsableBoard) {
|
||||
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);
|
||||
@@ -140,8 +135,7 @@ fn uart0_init(board: &mut 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(
|
||||
@@ -162,7 +156,7 @@ fn uart0_init(board: &mut UsableBoard) {
|
||||
}
|
||||
}
|
||||
|
||||
fn uart0_out_char(c: u8) {
|
||||
fn uart0_out_char_blocking(c: u8) {
|
||||
loop {
|
||||
let fr = unsafe { ptr::read_volatile(UART0_FR_R) };
|
||||
|
||||
@@ -176,12 +170,24 @@ fn uart0_out_char(c: u8) {
|
||||
}
|
||||
}
|
||||
|
||||
fn uart0_out_string(s: &str) {
|
||||
fn uart0_out_string_blocking(s: &str) {
|
||||
for c in s.bytes() {
|
||||
uart0_out_char(c);
|
||||
uart0_out_char_blocking(c);
|
||||
}
|
||||
}
|
||||
|
||||
fn uart0_in_char_blocking() -> u8 {
|
||||
loop {
|
||||
let fr = unsafe { ptr::read_volatile(UART0_FR_R) };
|
||||
|
||||
if (fr & UART_FR_RXFE) == 0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
unsafe { ptr::read_volatile(UART0_DR_R) as u8 }
|
||||
}
|
||||
|
||||
const WHITE: [bool; 3] = [H, H, H];
|
||||
const BLACK: [bool; 3] = [L, L, L];
|
||||
|
||||
@@ -195,25 +201,8 @@ const MAGENTA: [bool; 3] = [H, L, H];
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
let mut board = setup_board();
|
||||
let mut port_a = board.setup_gpio_port(Port::A, PortOptions);
|
||||
let mut port_f = board.setup_gpio_port(Port::F, PortOptions);
|
||||
|
||||
uart0_init(&mut board);
|
||||
|
||||
// WIP: page 682
|
||||
port_a.setup_writable_pins(
|
||||
[Pin::One],
|
||||
WritablePinOptions {
|
||||
function: Function::UART,
|
||||
},
|
||||
);
|
||||
|
||||
uart0_out_string("Hi, this is after uart setup_writable_pins\r\n\r\n");
|
||||
// TODO: finish this
|
||||
// port_a.setup_readable_pins([Pin::Zero], WritablePinOptions {
|
||||
// function: Function::UART,
|
||||
// });
|
||||
// uart0_out_string("Hi, this is after uart setup_readable_pins\r\n\r\n");
|
||||
let mut port_a = board.setup_gpio_port(Port::A, GPIOPortOptions);
|
||||
let mut port_f = board.setup_gpio_port(Port::F, GPIOPortOptions);
|
||||
|
||||
let switches = port_f.setup_readable_pins(
|
||||
[Pin::Zero, Pin::Four],
|
||||
@@ -222,8 +211,6 @@ fn main() -> ! {
|
||||
pull: Pull::Up,
|
||||
},
|
||||
);
|
||||
let [_sw1, _sw2] = switches.pins();
|
||||
|
||||
let mut rgb_led = port_f.setup_writable_pins(
|
||||
[Pin::One, Pin::Three, Pin::Two],
|
||||
WritablePinOptions {
|
||||
@@ -231,18 +218,33 @@ fn main() -> ! {
|
||||
},
|
||||
);
|
||||
|
||||
// TODO: finish this
|
||||
uart0_init();
|
||||
// WIP: page 682
|
||||
port_a.setup_writable_pins(
|
||||
[Pin::One],
|
||||
WritablePinOptions {
|
||||
function: Function::UART,
|
||||
},
|
||||
);
|
||||
uart0_out_string_blocking("Hi, this is after!! uart setup_writable_pins\r\n\r\n");
|
||||
port_a.setup_readable_pins(
|
||||
[Pin::Zero],
|
||||
ReadablePinOptions {
|
||||
function: Function::UART,
|
||||
pull: Pull::Neither,
|
||||
},
|
||||
);
|
||||
uart0_out_string_blocking("Hi, this is after uart setup_readable_pins\r\n\r\n");
|
||||
|
||||
let rainbow = [RED, YELLOW, GREEN, CYAN, BLUE, MAGENTA];
|
||||
|
||||
// TODO: WIP: debugging
|
||||
// let s = format!("Rainbow: {:?}\r\n", rainbow);
|
||||
let s = String::from("\r\ntesting a static string!!!\r\n\r\n\r\n");
|
||||
// let s = format!("Format");
|
||||
// let s: String = rainbow.into();
|
||||
uart0_out_string(&s);
|
||||
uart0_out_string_blocking(&s);
|
||||
|
||||
loop {
|
||||
uart0_out_string("Hi the program is still running down here!\r\n");
|
||||
// uart0_out_string_blocking("Hi still running down here!\r\n");
|
||||
match switches.read_all() {
|
||||
[L, L] => rgb_led.write_all(WHITE),
|
||||
[L, H] => rgb_led.write_all(BLUE),
|
||||
@@ -252,10 +254,15 @@ fn main() -> ! {
|
||||
|
||||
// uart0_out_string(&format!("The switches read {:?}", switches.read_all()));
|
||||
|
||||
for _ in 0..1000000 {
|
||||
unsafe {
|
||||
asm!("nop");
|
||||
}
|
||||
}
|
||||
// for _ in 0..1000000 {
|
||||
// unsafe {
|
||||
// asm!("nop");
|
||||
// }
|
||||
// }
|
||||
|
||||
let new_char = uart0_in_char_blocking();
|
||||
uart0_out_string_blocking("New character received: ");
|
||||
uart0_out_char_blocking(new_char);
|
||||
uart0_out_string_blocking("\r\n");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user