mirror of
https://github.com/iceHtwoO/novaOS.git
synced 2026-04-16 20:22:26 +00:00
Compare commits
2 Commits
384c548557
...
0f5f942d78
| Author | SHA1 | Date | |
|---|---|---|---|
| 0f5f942d78 | |||
| 48fbc2e5fa |
@@ -4,7 +4,10 @@ mod bitmaps;
|
|||||||
|
|
||||||
use bitmaps::BASIC_LEGACY;
|
use bitmaps::BASIC_LEGACY;
|
||||||
|
|
||||||
use crate::mailbox::{read_mailbox, write_mailbox};
|
use crate::{
|
||||||
|
mailbox::{read_mailbox, write_mailbox},
|
||||||
|
println,
|
||||||
|
};
|
||||||
#[repr(align(16))]
|
#[repr(align(16))]
|
||||||
struct Mailbox([u32; 36]);
|
struct Mailbox([u32; 36]);
|
||||||
|
|
||||||
|
|||||||
280
src/interrupt_handlers.rs
Normal file
280
src/interrupt_handlers.rs
Normal file
@@ -0,0 +1,280 @@
|
|||||||
|
use core::arch::asm;
|
||||||
|
|
||||||
|
use alloc::vec::Vec;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
get_current_el,
|
||||||
|
interrupt_handlers::daif::unmask_irq,
|
||||||
|
peripherals::{
|
||||||
|
gpio::{read_gpio_event_detect_status, reset_gpio_event_detect_status},
|
||||||
|
uart::clear_uart_interrupt_state,
|
||||||
|
},
|
||||||
|
println, read_address, write_address,
|
||||||
|
};
|
||||||
|
|
||||||
|
const INTERRUPT_BASE: u32 = 0x3F00_B000;
|
||||||
|
const IRQ_PENDING_BASE: u32 = INTERRUPT_BASE + 0x204;
|
||||||
|
const ENABLE_IRQ_BASE: u32 = INTERRUPT_BASE + 0x210;
|
||||||
|
const DISABLE_IRQ_BASE: u32 = INTERRUPT_BASE + 0x21C;
|
||||||
|
|
||||||
|
const GPIO_PENDING_BIT_OFFSET: u64 = 0b1111 << 49;
|
||||||
|
|
||||||
|
struct InterruptHandlers {
|
||||||
|
source: IRQSource,
|
||||||
|
function: fn(),
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: replace with hashmap and check for better alternatives for option
|
||||||
|
static mut INTERRUPT_HANDLERS: Option<Vec<InterruptHandlers>> = None;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
#[repr(u32)]
|
||||||
|
pub enum IRQSource {
|
||||||
|
AuxInt = 29,
|
||||||
|
I2cSpiSlvInt = 44,
|
||||||
|
Pwa0 = 45,
|
||||||
|
Pwa1 = 46,
|
||||||
|
Smi = 48,
|
||||||
|
GpioInt0 = 49,
|
||||||
|
GpioInt1 = 50,
|
||||||
|
GpioInt2 = 51,
|
||||||
|
GpioInt3 = 52,
|
||||||
|
I2cInt = 53,
|
||||||
|
SpiInt = 54,
|
||||||
|
PcmInt = 55,
|
||||||
|
UartInt = 57,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Representation of the ESR_ELx registers
|
||||||
|
///
|
||||||
|
/// Reference: D1.10.4
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
#[allow(dead_code)]
|
||||||
|
struct EsrElX {
|
||||||
|
ec: u32,
|
||||||
|
il: u32,
|
||||||
|
iss: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<u32> for EsrElX {
|
||||||
|
fn from(value: u32) -> Self {
|
||||||
|
Self {
|
||||||
|
ec: value >> 26,
|
||||||
|
il: (value >> 25) & 0b1,
|
||||||
|
iss: value & 0x1FFFFFF,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
unsafe extern "C" fn rust_irq_handler() {
|
||||||
|
daif::mask_all();
|
||||||
|
let pending_irqs = get_irq_pending_sources();
|
||||||
|
|
||||||
|
if pending_irqs & GPIO_PENDING_BIT_OFFSET != 0 {
|
||||||
|
handle_gpio_interrupt();
|
||||||
|
let source_el = get_exception_return_exception_level() >> 2;
|
||||||
|
println!("Source EL: {}", source_el);
|
||||||
|
println!("Current EL: {}", get_current_el());
|
||||||
|
println!("Return register address: {:#x}", get_elr_el1());
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(handler_vec) = unsafe { INTERRUPT_HANDLERS.as_ref() } {
|
||||||
|
for handler in handler_vec {
|
||||||
|
if (pending_irqs & (1 << (handler.source.clone() as u32))) != 0 {
|
||||||
|
(handler.function)();
|
||||||
|
clear_interrupt_for_source(handler.source.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
unsafe extern "C" fn rust_synchronous_interrupt_no_el_change() {
|
||||||
|
daif::mask_all();
|
||||||
|
|
||||||
|
let source_el = get_exception_return_exception_level() >> 2;
|
||||||
|
println!("--------Sync Exception in EL{}--------", source_el);
|
||||||
|
println!("No EL change");
|
||||||
|
println!("Current EL: {}", get_current_el());
|
||||||
|
println!("{:?}", EsrElX::from(get_esr_el1()));
|
||||||
|
println!("Return register address: {:#x}", get_elr_el1());
|
||||||
|
println!("-------------------------------------");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Synchronous Exception Handler
|
||||||
|
///
|
||||||
|
/// Lower Exception level, where the implemented level
|
||||||
|
/// immediately lower than the target level is using
|
||||||
|
/// AArch64.
|
||||||
|
#[no_mangle]
|
||||||
|
unsafe extern "C" fn rust_synchronous_interrupt_imm_lower_aarch64() {
|
||||||
|
daif::mask_all();
|
||||||
|
|
||||||
|
let source_el = get_exception_return_exception_level() >> 2;
|
||||||
|
println!("--------Sync Exception in EL{}--------", source_el);
|
||||||
|
println!("Exception escalated to EL {}", get_current_el());
|
||||||
|
println!("Current EL: {}", get_current_el());
|
||||||
|
let esr = EsrElX::from(get_esr_el1());
|
||||||
|
println!("{:?}", EsrElX::from(esr));
|
||||||
|
println!("Return register address: {:#x}", get_elr_el1());
|
||||||
|
|
||||||
|
match esr.ec {
|
||||||
|
0b100100 => {
|
||||||
|
println!("Cause: Data Abort from a lower Exception level");
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
println!("-------------------------------------");
|
||||||
|
|
||||||
|
set_return_to_kernel_main();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn clear_interrupt_for_source(source: IRQSource) {
|
||||||
|
match source {
|
||||||
|
IRQSource::UartInt => clear_uart_interrupt_state(),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_return_to_kernel_main() {
|
||||||
|
unsafe {
|
||||||
|
asm!("ldr x0, =kernel_main", "msr ELR_EL1, x0");
|
||||||
|
asm!("mov x0, #(0b0101)", "msr SPSR_EL1, x0");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_exception_return_exception_level() -> u32 {
|
||||||
|
let spsr: u32;
|
||||||
|
unsafe {
|
||||||
|
asm!("mrs {0:x}, SPSR_EL1", out(reg) spsr);
|
||||||
|
}
|
||||||
|
spsr & 0b1111
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Read the syndrome information that caused an exception
|
||||||
|
///
|
||||||
|
/// ESR = Exception Syndrome Register
|
||||||
|
fn get_esr_el1() -> u32 {
|
||||||
|
let esr: u32;
|
||||||
|
unsafe {
|
||||||
|
asm!(
|
||||||
|
"mrs {esr:x}, ESR_EL1",
|
||||||
|
esr = out(reg) esr
|
||||||
|
);
|
||||||
|
}
|
||||||
|
esr
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Read the return address
|
||||||
|
///
|
||||||
|
/// ELR = Exception Link Registers
|
||||||
|
fn get_elr_el1() -> u32 {
|
||||||
|
let elr: u32;
|
||||||
|
unsafe {
|
||||||
|
asm!(
|
||||||
|
"mrs {esr:x}, ELR_EL1",
|
||||||
|
esr = out(reg) elr
|
||||||
|
);
|
||||||
|
}
|
||||||
|
elr
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_gpio_interrupt() {
|
||||||
|
println!("Interrupt");
|
||||||
|
for i in 0..=53u32 {
|
||||||
|
let val = read_gpio_event_detect_status(i);
|
||||||
|
|
||||||
|
if val {
|
||||||
|
#[allow(clippy::single_match)]
|
||||||
|
match i {
|
||||||
|
26 => {
|
||||||
|
println!("Button Pressed");
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
// Reset GPIO Interrupt handler by writing a 1
|
||||||
|
reset_gpio_event_detect_status(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
unmask_irq();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Enables IRQ Source
|
||||||
|
pub fn enable_irq_source(state: IRQSource) {
|
||||||
|
let nr = state as u32;
|
||||||
|
let register = ENABLE_IRQ_BASE + 4 * (nr / 32);
|
||||||
|
let register_offset = nr % 32;
|
||||||
|
let current = unsafe { read_address(register) };
|
||||||
|
let mask = 0b1 << register_offset;
|
||||||
|
let new_val = current | mask;
|
||||||
|
unsafe { write_address(register, new_val) };
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Disable IRQ Source
|
||||||
|
pub fn disable_irq_source(state: IRQSource) {
|
||||||
|
let nr = state as u32;
|
||||||
|
let register = DISABLE_IRQ_BASE + 4 * (nr / 32);
|
||||||
|
let register_offset = nr % 32;
|
||||||
|
let current = unsafe { read_address(register) };
|
||||||
|
let mask = 0b1 << register_offset;
|
||||||
|
let new_val = current | mask;
|
||||||
|
unsafe { write_address(register, new_val) };
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Read current IRQ Source status
|
||||||
|
pub fn read_irq_source_status(state: IRQSource) -> u32 {
|
||||||
|
let nr = state as u32;
|
||||||
|
let register = ENABLE_IRQ_BASE + 4 * (nr / 32);
|
||||||
|
let register_offset = nr % 32;
|
||||||
|
(unsafe { read_address(register) } >> register_offset) & 0b1
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Status if a IRQ Source is pending
|
||||||
|
pub fn is_irq_source_pending(state: IRQSource) -> bool {
|
||||||
|
let nr = state as u32;
|
||||||
|
let register = IRQ_PENDING_BASE + 4 * (nr / 32);
|
||||||
|
let register_offset = nr % 32;
|
||||||
|
((unsafe { read_address(register) } >> register_offset) & 0b1) != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Status if a IRQ Source is pending
|
||||||
|
pub fn get_irq_pending_sources() -> u64 {
|
||||||
|
let mut pending = unsafe { read_address(IRQ_PENDING_BASE + 4) as u64 } << 32;
|
||||||
|
pending |= unsafe { read_address(IRQ_PENDING_BASE) as u64 };
|
||||||
|
pending
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod daif {
|
||||||
|
use core::arch::asm;
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn mask_all() {
|
||||||
|
unsafe { asm!("msr DAIFSet, #0xf", options(nomem, nostack)) }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn unmask_all() {
|
||||||
|
unsafe { asm!("msr DAIFClr, #0xf", options(nomem, nostack)) }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn mask_irq() {
|
||||||
|
unsafe { asm!("msr DAIFSet, #0x2", options(nomem, nostack)) }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn unmask_irq() {
|
||||||
|
unsafe { asm!("msr DAIFClr, #0x2", options(nomem, nostack)) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn initialize_interrupt_handler() {
|
||||||
|
unsafe { INTERRUPT_HANDLERS = Some(Vec::new()) };
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn register_interrupt_handler(source: IRQSource, function: fn()) {
|
||||||
|
if let Some(handler_vec) = unsafe { INTERRUPT_HANDLERS.as_mut() } {
|
||||||
|
handler_vec.push(InterruptHandlers { source, function });
|
||||||
|
}
|
||||||
|
}
|
||||||
39
src/lib.rs
39
src/lib.rs
@@ -1,5 +1,9 @@
|
|||||||
#![no_std]
|
#![no_std]
|
||||||
#![allow(clippy::missing_safety_doc)]
|
#![allow(clippy::missing_safety_doc)]
|
||||||
|
|
||||||
|
extern crate alloc;
|
||||||
|
|
||||||
|
use alloc::boxed::Box;
|
||||||
use core::{
|
use core::{
|
||||||
arch::asm,
|
arch::asm,
|
||||||
panic::PanicInfo,
|
panic::PanicInfo,
|
||||||
@@ -8,7 +12,9 @@ use core::{
|
|||||||
|
|
||||||
use heap::Heap;
|
use heap::Heap;
|
||||||
|
|
||||||
pub static PERIPHERAL_BASE: u32 = 0x3F00_0000;
|
use crate::{interrupt_handlers::initialize_interrupt_handler, logger::DefaultLogger};
|
||||||
|
|
||||||
|
static PERIPHERAL_BASE: u32 = 0x3F00_0000;
|
||||||
|
|
||||||
unsafe extern "C" {
|
unsafe extern "C" {
|
||||||
unsafe static mut __heap_start: u8;
|
unsafe static mut __heap_start: u8;
|
||||||
@@ -33,37 +39,23 @@ fn panic(_panic: &PanicInfo) -> ! {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[macro_export]
|
|
||||||
macro_rules! print {
|
|
||||||
() => {};
|
|
||||||
($($arg:tt)*) => {
|
|
||||||
$crate::peripherals::uart::_print(format_args!($($arg)*))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#[macro_export]
|
|
||||||
macro_rules! println {
|
|
||||||
() => {};
|
|
||||||
($($arg:tt)*) => {
|
|
||||||
print!($($arg)*);
|
|
||||||
print!("\r\n");
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub mod peripherals;
|
pub mod peripherals;
|
||||||
|
|
||||||
pub mod configuration;
|
pub mod configuration;
|
||||||
pub mod framebuffer;
|
pub mod framebuffer;
|
||||||
pub mod irq_interrupt;
|
pub mod interrupt_handlers;
|
||||||
|
pub mod logger;
|
||||||
pub mod mailbox;
|
pub mod mailbox;
|
||||||
pub mod power_management;
|
pub mod power_management;
|
||||||
pub mod timer;
|
pub mod timer;
|
||||||
|
|
||||||
pub fn mmio_read(address: u32) -> u32 {
|
#[inline(always)]
|
||||||
|
pub unsafe fn read_address(address: u32) -> u32 {
|
||||||
unsafe { read_volatile(address as *const u32) }
|
unsafe { read_volatile(address as *const u32) }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn mmio_write(address: u32, data: u32) {
|
#[inline(always)]
|
||||||
|
pub unsafe fn write_address(address: u32, data: u32) {
|
||||||
unsafe { write_volatile(address as *mut u32, data) }
|
unsafe { write_volatile(address as *mut u32, data) }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,3 +70,8 @@ pub fn get_current_el() -> u64 {
|
|||||||
}
|
}
|
||||||
el >> 2
|
el >> 2
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn initialize_kernel() {
|
||||||
|
logger::set_logger(Box::new(DefaultLogger));
|
||||||
|
initialize_interrupt_handler();
|
||||||
|
}
|
||||||
|
|||||||
47
src/logger.rs
Normal file
47
src/logger.rs
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
use core::fmt::Write;
|
||||||
|
|
||||||
|
use alloc::{boxed::Box, fmt};
|
||||||
|
|
||||||
|
use crate::peripherals::uart;
|
||||||
|
|
||||||
|
static mut LOGGER: Option<Box<dyn Logger>> = None;
|
||||||
|
|
||||||
|
pub trait Logger: Write + Sync {
|
||||||
|
fn flush(&mut self);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct DefaultLogger;
|
||||||
|
|
||||||
|
impl Logger for DefaultLogger {
|
||||||
|
fn flush(&mut self) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Write for DefaultLogger {
|
||||||
|
fn write_str(&mut self, s: &str) -> core::fmt::Result {
|
||||||
|
uart::Uart.write_str(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! log {
|
||||||
|
() => {};
|
||||||
|
($($arg:tt)*) => {
|
||||||
|
$crate::logger::log(format_args!($($arg)*))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn log(args: fmt::Arguments) {
|
||||||
|
unsafe {
|
||||||
|
if let Some(logger) = LOGGER.as_mut() {
|
||||||
|
logger.write_str("\n").unwrap();
|
||||||
|
logger.write_fmt(args).unwrap();
|
||||||
|
logger.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_logger(logger: Box<dyn Logger>) {
|
||||||
|
unsafe {
|
||||||
|
LOGGER = Some(logger);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
use crate::{mmio_read, mmio_write};
|
use crate::{read_address, write_address};
|
||||||
use nova_error::NovaError;
|
use nova_error::NovaError;
|
||||||
|
|
||||||
const MBOX_BASE: u32 = 0x3F00_0000 + 0xB880;
|
const MBOX_BASE: u32 = 0x3F00_0000 + 0xB880;
|
||||||
@@ -67,8 +67,8 @@ mailbox_command!(get_display_resolution, 0x0004_0003, 0, 8);
|
|||||||
pub fn read_mailbox(channel: u32) -> u32 {
|
pub fn read_mailbox(channel: u32) -> u32 {
|
||||||
// Wait until mailbox is not empty
|
// Wait until mailbox is not empty
|
||||||
loop {
|
loop {
|
||||||
while mmio_read(MBOX_STATUS) & MAIL_EMPTY != 0 {}
|
while unsafe { read_address(MBOX_STATUS) } & MAIL_EMPTY != 0 {}
|
||||||
let mut data = mmio_read(MBOX_READ);
|
let mut data = unsafe { read_address(MBOX_READ) };
|
||||||
let read_channel = data & 0xF;
|
let read_channel = data & 0xF;
|
||||||
|
|
||||||
data >>= 4;
|
data >>= 4;
|
||||||
@@ -80,6 +80,6 @@ pub fn read_mailbox(channel: u32) -> u32 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_mailbox(channel: u32, data: u32) {
|
pub fn write_mailbox(channel: u32, data: u32) {
|
||||||
while mmio_read(MBOX_STATUS) & MAIL_FULL != 0 {}
|
while unsafe { read_address(MBOX_STATUS) } & MAIL_FULL != 0 {}
|
||||||
mmio_write(MBOX_WRITE, (data & !0xF) | (channel & 0xF));
|
unsafe { write_address(MBOX_WRITE, (data & !0xF) | (channel & 0xF)) };
|
||||||
}
|
}
|
||||||
|
|||||||
17
src/main.rs
17
src/main.rs
@@ -14,8 +14,8 @@ use alloc::boxed::Box;
|
|||||||
use nova::{
|
use nova::{
|
||||||
framebuffer::{FrameBuffer, BLUE, GREEN, RED},
|
framebuffer::{FrameBuffer, BLUE, GREEN, RED},
|
||||||
get_current_el, init_heap,
|
get_current_el, init_heap,
|
||||||
irq_interrupt::{daif, enable_irq_source},
|
interrupt_handlers::{daif, enable_irq_source, IRQSource},
|
||||||
mailbox,
|
log, mailbox,
|
||||||
peripherals::{
|
peripherals::{
|
||||||
gpio::{
|
gpio::{
|
||||||
blink_gpio, gpio_pull_up, set_falling_edge_detect, set_gpio_function, GPIOFunction,
|
blink_gpio, gpio_pull_up, set_falling_edge_detect, set_gpio_function, GPIOFunction,
|
||||||
@@ -23,7 +23,7 @@ use nova::{
|
|||||||
},
|
},
|
||||||
uart::uart_init,
|
uart::uart_init,
|
||||||
},
|
},
|
||||||
print, println,
|
println,
|
||||||
};
|
};
|
||||||
|
|
||||||
global_asm!(include_str!("vector.S"));
|
global_asm!(include_str!("vector.S"));
|
||||||
@@ -79,6 +79,7 @@ unsafe fn zero_bss() {
|
|||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn kernel_main() -> ! {
|
pub extern "C" fn kernel_main() -> ! {
|
||||||
|
nova::initialize_kernel();
|
||||||
println!("Kernel Main");
|
println!("Kernel Main");
|
||||||
println!("Exception Level: {}", get_current_el());
|
println!("Exception Level: {}", get_current_el());
|
||||||
daif::unmask_all();
|
daif::unmask_all();
|
||||||
@@ -97,11 +98,13 @@ pub extern "C" fn el0() -> ! {
|
|||||||
println!("Jumped into EL0");
|
println!("Jumped into EL0");
|
||||||
|
|
||||||
// Set GPIO 26 to Input
|
// Set GPIO 26 to Input
|
||||||
enable_irq_source(nova::irq_interrupt::IRQState::GpioInt0); //26 is on the first GPIO bank
|
enable_irq_source(IRQSource::GpioInt0); //26 is on the first GPIO bank
|
||||||
let _ = set_gpio_function(26, GPIOFunction::Input);
|
let _ = set_gpio_function(26, GPIOFunction::Input);
|
||||||
gpio_pull_up(26);
|
gpio_pull_up(26);
|
||||||
set_falling_edge_detect(26, true);
|
set_falling_edge_detect(26, true);
|
||||||
|
|
||||||
|
enable_irq_source(IRQSource::UartInt);
|
||||||
|
|
||||||
let fb = FrameBuffer::default();
|
let fb = FrameBuffer::default();
|
||||||
|
|
||||||
fb.draw_square(500, 500, 600, 700, RED);
|
fb.draw_square(500, 500, 600, 700, RED);
|
||||||
@@ -114,12 +117,12 @@ pub extern "C" fn el0() -> ! {
|
|||||||
|
|
||||||
loop {
|
loop {
|
||||||
let temp = mailbox::read_soc_temp([0]).unwrap();
|
let temp = mailbox::read_soc_temp([0]).unwrap();
|
||||||
println!("{} °C", temp[1] / 1000);
|
log!("{} °C", temp[1] / 1000);
|
||||||
|
|
||||||
blink_gpio(SpecificGpio::OnboardLed as u8, 500);
|
blink_gpio(SpecificGpio::OnboardLed as u8, 500);
|
||||||
|
|
||||||
let b = Box::new([1, 2, 3, 4]);
|
let b = Box::new([1, 2, 3, 4]);
|
||||||
println!("{:?}", b);
|
log!("{:?}", b);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -128,8 +131,8 @@ fn cos(x: u32) -> f64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn enable_uart() {
|
fn enable_uart() {
|
||||||
uart_init();
|
|
||||||
// Set GPIO Pins to UART
|
// Set GPIO Pins to UART
|
||||||
let _ = set_gpio_function(14, GPIOFunction::Alternative0);
|
let _ = set_gpio_function(14, GPIOFunction::Alternative0);
|
||||||
let _ = set_gpio_function(15, GPIOFunction::Alternative0);
|
let _ = set_gpio_function(15, GPIOFunction::Alternative0);
|
||||||
|
uart_init();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ use core::result::Result::Ok;
|
|||||||
use core::sync::atomic::{compiler_fence, Ordering};
|
use core::sync::atomic::{compiler_fence, Ordering};
|
||||||
|
|
||||||
use crate::timer::{delay_nops, sleep_ms};
|
use crate::timer::{delay_nops, sleep_ms};
|
||||||
use crate::{mmio_read, mmio_write};
|
use crate::{read_address, write_address};
|
||||||
|
|
||||||
const GPFSEL_BASE: u32 = 0x3F20_0000;
|
const GPFSEL_BASE: u32 = 0x3F20_0000;
|
||||||
const GPSET_BASE: u32 = 0x3F20_001C;
|
const GPSET_BASE: u32 = 0x3F20_001C;
|
||||||
@@ -37,14 +37,14 @@ pub fn set_gpio_function(gpio: u8, state: GPIOFunction) -> Result<(), &'static s
|
|||||||
let register_index = gpio / 10;
|
let register_index = gpio / 10;
|
||||||
let register_offset = (gpio % 10) * 3;
|
let register_offset = (gpio % 10) * 3;
|
||||||
let register_addr = GPFSEL_BASE + (register_index as u32 * 4);
|
let register_addr = GPFSEL_BASE + (register_index as u32 * 4);
|
||||||
let current = mmio_read(register_addr);
|
let current = unsafe { read_address(register_addr) };
|
||||||
|
|
||||||
let mask = !(0b111 << register_offset);
|
let mask = !(0b111 << register_offset);
|
||||||
let cleared = current & mask;
|
let cleared = current & mask;
|
||||||
|
|
||||||
let new_val = cleared | ((state as u32) << register_offset);
|
let new_val = cleared | ((state as u32) << register_offset);
|
||||||
|
|
||||||
mmio_write(register_addr, new_val);
|
unsafe { write_address(register_addr, new_val) };
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,7 +57,7 @@ pub fn gpio_high(gpio: u8) -> Result<(), &'static str> {
|
|||||||
let register_offset = gpio % 32;
|
let register_offset = gpio % 32;
|
||||||
let register_addr = GPSET_BASE + (register_index as u32 * 4);
|
let register_addr = GPSET_BASE + (register_index as u32 * 4);
|
||||||
|
|
||||||
mmio_write(register_addr, 1 << register_offset);
|
unsafe { write_address(register_addr, 1 << register_offset) };
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,7 +69,7 @@ pub fn gpio_low(gpio: u8) -> Result<(), &'static str> {
|
|||||||
let register_offset = gpio % 32;
|
let register_offset = gpio % 32;
|
||||||
let register_addr = GPCLR_BASE + (register_index as u32 * 4);
|
let register_addr = GPCLR_BASE + (register_index as u32 * 4);
|
||||||
|
|
||||||
mmio_write(register_addr, 1 << register_offset);
|
unsafe { write_address(register_addr, 1 << register_offset) };
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,7 +79,7 @@ pub fn gpio_get_state(gpio: u8) -> u8 {
|
|||||||
let register_offset = gpio % 32;
|
let register_offset = gpio % 32;
|
||||||
let register_addr = GPLEV_BASE + (register_index as u32 * 4);
|
let register_addr = GPLEV_BASE + (register_index as u32 * 4);
|
||||||
|
|
||||||
let state = mmio_read(register_addr);
|
let state = unsafe { read_address(register_addr) };
|
||||||
((state >> register_offset) & 0b1) as u8
|
((state >> register_offset) & 0b1) as u8
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,23 +103,23 @@ fn gpio_pull_up_down(gpio: u8, val: u32) {
|
|||||||
let register_offset = gpio % 32;
|
let register_offset = gpio % 32;
|
||||||
|
|
||||||
// 1. Write Pull up
|
// 1. Write Pull up
|
||||||
mmio_write(GPPUD, val);
|
unsafe { write_address(GPPUD, val) };
|
||||||
|
|
||||||
// 2. Delay 150 cycles
|
// 2. Delay 150 cycles
|
||||||
delay_nops(150);
|
delay_nops(150);
|
||||||
|
|
||||||
// 3. Write to clock
|
// 3. Write to clock
|
||||||
let new_val = 0b1 << register_offset;
|
let new_val = 0b1 << register_offset;
|
||||||
mmio_write(register_addr, new_val);
|
unsafe { write_address(register_addr, new_val) };
|
||||||
|
|
||||||
// 4. Delay 150 cycles
|
// 4. Delay 150 cycles
|
||||||
delay_nops(150);
|
delay_nops(150);
|
||||||
|
|
||||||
// 5. reset GPPUD
|
// 5. reset GPPUD
|
||||||
mmio_write(GPPUD, 0);
|
unsafe { write_address(GPPUD, 0) };
|
||||||
|
|
||||||
// 6. reset clock
|
// 6. reset clock
|
||||||
mmio_write(register_addr, 0);
|
unsafe { write_address(register_addr, 0) };
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the current status of the falling edge detection
|
/// Get the current status of the falling edge detection
|
||||||
@@ -127,7 +127,7 @@ pub fn read_falling_edge_detect(gpio: u8) -> bool {
|
|||||||
let register_addr = GPFEN_BASE + 4 * (gpio as u32 / 32);
|
let register_addr = GPFEN_BASE + 4 * (gpio as u32 / 32);
|
||||||
let register_offset = gpio % 32;
|
let register_offset = gpio % 32;
|
||||||
|
|
||||||
let current = mmio_read(register_addr);
|
let current = unsafe { read_address(register_addr) };
|
||||||
((current >> register_offset) & 0b1) != 0
|
((current >> register_offset) & 0b1) != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -136,7 +136,7 @@ pub fn read_rising_edge_detect(gpio: u8) -> bool {
|
|||||||
let register_addr = GPREN_BASE + 4 * (gpio as u32 / 32);
|
let register_addr = GPREN_BASE + 4 * (gpio as u32 / 32);
|
||||||
let register_offset = gpio % 32;
|
let register_offset = gpio % 32;
|
||||||
|
|
||||||
let current = mmio_read(register_addr);
|
let current = unsafe { read_address(register_addr) };
|
||||||
((current >> register_offset) & 0b1) != 0
|
((current >> register_offset) & 0b1) != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,7 +145,7 @@ pub fn set_falling_edge_detect(gpio: u8, enable: bool) {
|
|||||||
let register_addr = GPFEN_BASE + 4 * (gpio as u32 / 32);
|
let register_addr = GPFEN_BASE + 4 * (gpio as u32 / 32);
|
||||||
let register_offset = gpio % 32;
|
let register_offset = gpio % 32;
|
||||||
|
|
||||||
let current = mmio_read(register_addr);
|
let current = unsafe { read_address(register_addr) };
|
||||||
let mask = 0b1 << register_offset;
|
let mask = 0b1 << register_offset;
|
||||||
let new_val = if enable {
|
let new_val = if enable {
|
||||||
current | mask
|
current | mask
|
||||||
@@ -153,7 +153,7 @@ pub fn set_falling_edge_detect(gpio: u8, enable: bool) {
|
|||||||
current & !mask
|
current & !mask
|
||||||
};
|
};
|
||||||
|
|
||||||
mmio_write(register_addr, new_val);
|
unsafe { write_address(register_addr, new_val) };
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Enables rising edge detection
|
/// Enables rising edge detection
|
||||||
@@ -161,7 +161,7 @@ pub fn set_rising_edge_detect(gpio: u8, enable: bool) {
|
|||||||
let register_addr = GPREN_BASE + 4 * (gpio as u32 / 32);
|
let register_addr = GPREN_BASE + 4 * (gpio as u32 / 32);
|
||||||
let register_offset = gpio % 32;
|
let register_offset = gpio % 32;
|
||||||
|
|
||||||
let current = mmio_read(register_addr);
|
let current = unsafe { read_address(register_addr) };
|
||||||
|
|
||||||
let mask = 0b1 << register_offset;
|
let mask = 0b1 << register_offset;
|
||||||
let new_val = if enable {
|
let new_val = if enable {
|
||||||
@@ -170,7 +170,7 @@ pub fn set_rising_edge_detect(gpio: u8, enable: bool) {
|
|||||||
current & !mask
|
current & !mask
|
||||||
};
|
};
|
||||||
|
|
||||||
mmio_write(register_addr, new_val);
|
unsafe { write_address(register_addr, new_val) };
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns with the interrupt status of an GPIO.
|
/// Returns with the interrupt status of an GPIO.
|
||||||
@@ -181,7 +181,7 @@ pub fn read_gpio_event_detect_status(id: u32) -> bool {
|
|||||||
let register = GPEDS_BASE + (id / 32) * 4;
|
let register = GPEDS_BASE + (id / 32) * 4;
|
||||||
let register_offset = id % 32;
|
let register_offset = id % 32;
|
||||||
|
|
||||||
let val = mmio_read(register) >> register_offset;
|
let val = unsafe { read_address(register) } >> register_offset;
|
||||||
(val & 0b1) != 0
|
(val & 0b1) != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -190,7 +190,7 @@ pub fn reset_gpio_event_detect_status(id: u32) {
|
|||||||
let register = GPEDS_BASE + (id / 32) * 4;
|
let register = GPEDS_BASE + (id / 32) * 4;
|
||||||
let register_offset = id % 32;
|
let register_offset = id % 32;
|
||||||
|
|
||||||
mmio_write(register, 0b1 << register_offset);
|
unsafe { write_address(register, 0b1 << register_offset) };
|
||||||
compiler_fence(Ordering::SeqCst);
|
compiler_fence(Ordering::SeqCst);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ use core::{
|
|||||||
fmt::{self, Write},
|
fmt::{self, Write},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{mmio_read, mmio_write};
|
use crate::{println, read_address, write_address};
|
||||||
|
|
||||||
const BAUD: u32 = 115200;
|
const BAUD: u32 = 115200;
|
||||||
const UART_CLK: u32 = 48_000_000;
|
const UART_CLK: u32 = 48_000_000;
|
||||||
@@ -18,33 +18,53 @@ const UART0_FBRD: u32 = 0x3F20_1028;
|
|||||||
|
|
||||||
const UART0_CR: u32 = 0x3F20_1030;
|
const UART0_CR: u32 = 0x3F20_1030;
|
||||||
const UART0_CR_UARTEN: u32 = 1 << 0;
|
const UART0_CR_UARTEN: u32 = 1 << 0;
|
||||||
|
|
||||||
const UART0_CR_TXE: u32 = 1 << 8;
|
const UART0_CR_TXE: u32 = 1 << 8;
|
||||||
|
const UART0_CR_RXE: u32 = 1 << 9;
|
||||||
|
|
||||||
const UART0_LCRH: u32 = 0x3F20_102C;
|
const UART0_LCRH: u32 = 0x3F20_102C;
|
||||||
const UART0_LCRH_FEN: u32 = 1 << 4;
|
const UART0_LCRH_FEN: u32 = 1 << 4;
|
||||||
|
|
||||||
|
const UART0_IMSC: u32 = 0x3F20_1038;
|
||||||
|
const UART0_IMSC_RXIM: u32 = 1 << 4;
|
||||||
|
|
||||||
|
const UART0_ICR: u32 = 0x3F20_1044;
|
||||||
|
|
||||||
pub struct Uart;
|
pub struct Uart;
|
||||||
|
|
||||||
impl Write for Uart {
|
impl Write for Uart {
|
||||||
fn write_str(&mut self, s: &str) -> core::fmt::Result {
|
fn write_str(&mut self, s: &str) -> core::fmt::Result {
|
||||||
for byte in s.bytes() {
|
for byte in s.bytes() {
|
||||||
while (mmio_read(UART0_FR) & UART0_FR_TXFF) != 0 {
|
while (unsafe { read_address(UART0_FR) } & UART0_FR_TXFF) != 0 {
|
||||||
unsafe { asm!("nop") }
|
unsafe { asm!("nop") }
|
||||||
}
|
}
|
||||||
mmio_write(UART0_DR, byte as u32);
|
unsafe { write_address(UART0_DR, byte as u32) };
|
||||||
}
|
}
|
||||||
// wait till uart is not busy anymore
|
// wait till uart is not busy anymore
|
||||||
while ((mmio_read(UART0_FR) >> 3) & 0b1) != 0 {}
|
while ((unsafe { read_address(UART0_FR) } >> 3) & 0b1) != 0 {}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn _print(args: fmt::Arguments) {
|
#[macro_export]
|
||||||
let _ = Uart.write_fmt(args);
|
macro_rules! print {
|
||||||
|
() => {};
|
||||||
|
($($arg:tt)*) => {
|
||||||
|
$crate::peripherals::uart::_print(format_args!($($arg)*))
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn _print_str(st: &str) {
|
#[macro_export]
|
||||||
let _ = Uart.write_str(st);
|
macro_rules! println {
|
||||||
|
() => {};
|
||||||
|
($($arg:tt)*) => {
|
||||||
|
$crate::print!($($arg)*);
|
||||||
|
$crate::print!("\r\n");
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn _print(args: fmt::Arguments) {
|
||||||
|
let _ = Uart.write_fmt(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Initialize UART peripheral
|
/// Initialize UART peripheral
|
||||||
@@ -55,23 +75,26 @@ pub fn uart_init() {
|
|||||||
let fbrd = baud_div_times_64 % 64;
|
let fbrd = baud_div_times_64 % 64;
|
||||||
|
|
||||||
uart_enable(false);
|
uart_enable(false);
|
||||||
uart_fifo_enable(false);
|
uart_fifo_enable(true);
|
||||||
|
|
||||||
mmio_write(UART0_IBRD, ibrd);
|
unsafe {
|
||||||
mmio_write(UART0_FBRD, fbrd);
|
write_address(UART0_IBRD, ibrd);
|
||||||
|
write_address(UART0_FBRD, fbrd);
|
||||||
|
}
|
||||||
|
|
||||||
|
uart_enable_rx_interrupt();
|
||||||
uart_set_lcrh(0b11, true);
|
uart_set_lcrh(0b11, true);
|
||||||
|
|
||||||
// Enable transmit and uart
|
// Enable transmit, receive and uart
|
||||||
let mut cr = mmio_read(UART0_CR);
|
let mut cr = unsafe { read_address(UART0_CR) };
|
||||||
cr |= UART0_CR_UARTEN | UART0_CR_TXE;
|
cr |= UART0_CR_UARTEN | UART0_CR_TXE | UART0_CR_RXE;
|
||||||
|
|
||||||
mmio_write(UART0_CR, cr);
|
unsafe { write_address(UART0_CR, cr) };
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Enable UARTEN
|
/// Enable UARTEN
|
||||||
fn uart_enable(enable: bool) {
|
fn uart_enable(enable: bool) {
|
||||||
let mut cr = mmio_read(UART0_CR);
|
let mut cr = unsafe { read_address(UART0_CR) };
|
||||||
|
|
||||||
if enable {
|
if enable {
|
||||||
cr |= UART0_CR_UARTEN;
|
cr |= UART0_CR_UARTEN;
|
||||||
@@ -79,12 +102,12 @@ fn uart_enable(enable: bool) {
|
|||||||
cr &= !UART0_CR_UARTEN;
|
cr &= !UART0_CR_UARTEN;
|
||||||
}
|
}
|
||||||
|
|
||||||
mmio_write(UART0_CR, cr);
|
unsafe { write_address(UART0_CR, cr) };
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Enable UART FIFO
|
/// Enable UART FIFO
|
||||||
fn uart_fifo_enable(enable: bool) {
|
fn uart_fifo_enable(enable: bool) {
|
||||||
let mut lcrh = mmio_read(UART0_LCRH);
|
let mut lcrh = unsafe { read_address(UART0_LCRH) };
|
||||||
|
|
||||||
if enable {
|
if enable {
|
||||||
lcrh |= UART0_LCRH_FEN;
|
lcrh |= UART0_LCRH_FEN;
|
||||||
@@ -92,7 +115,11 @@ fn uart_fifo_enable(enable: bool) {
|
|||||||
lcrh &= !UART0_LCRH_FEN;
|
lcrh &= !UART0_LCRH_FEN;
|
||||||
}
|
}
|
||||||
|
|
||||||
mmio_write(UART0_LCRH, lcrh);
|
unsafe { write_address(UART0_LCRH, lcrh) };
|
||||||
|
}
|
||||||
|
|
||||||
|
fn uart_enable_rx_interrupt() {
|
||||||
|
unsafe { write_address(UART0_IMSC, UART0_IMSC_RXIM) };
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set UART word length and set FIFO status
|
/// Set UART word length and set FIFO status
|
||||||
@@ -101,5 +128,15 @@ fn uart_set_lcrh(wlen: u32, enable_fifo: bool) {
|
|||||||
if enable_fifo {
|
if enable_fifo {
|
||||||
value |= UART0_LCRH_FEN;
|
value |= UART0_LCRH_FEN;
|
||||||
}
|
}
|
||||||
mmio_write(UART0_LCRH, value);
|
unsafe { write_address(UART0_LCRH, value) };
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read_uart_data() -> char {
|
||||||
|
(unsafe { read_address(UART0_DR) } & 0xFF) as u8 as char
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn clear_uart_interrupt_state() {
|
||||||
|
unsafe {
|
||||||
|
write_address(UART0_ICR, 1 << 4);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
53
src/terminal.rs
Normal file
53
src/terminal.rs
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
use core::fmt::Write;
|
||||||
|
|
||||||
|
use alloc::string::String;
|
||||||
|
use nova::{
|
||||||
|
interrupt_handlers::register_interrupt_handler, logger::Logger,
|
||||||
|
peripherals::uart::read_uart_data, print, println,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct Terminal {
|
||||||
|
buffer: String,
|
||||||
|
input: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Terminal {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
buffer: String::new(),
|
||||||
|
input: String::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn flush(&mut self) {
|
||||||
|
println!("{}", self.buffer);
|
||||||
|
print!("> {}", self.input);
|
||||||
|
self.buffer.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Write for Terminal {
|
||||||
|
fn write_str(&mut self, s: &str) -> core::fmt::Result {
|
||||||
|
self.buffer.push_str(s);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Logger for Terminal {
|
||||||
|
fn flush(&mut self) {
|
||||||
|
println!("{}", self.buffer);
|
||||||
|
print!("> {}", self.input);
|
||||||
|
self.buffer.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn terminal_uart_rx_interrupt_handler() {
|
||||||
|
print!("{}", read_uart_data());
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn register_terminal_interrupt_handler() {
|
||||||
|
register_interrupt_handler(
|
||||||
|
nova::interrupt_handlers::IRQSource::UartInt,
|
||||||
|
terminal_uart_rx_interrupt_handler,
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user