#![no_std]
#![cfg_attr(not(doc), no_main)]
use core::ptr::{addr_of, addr_of_mut};
use capsules_core::virtualizers::virtual_alarm::{MuxAlarm, VirtualMuxAlarm};
use kernel::capabilities;
use kernel::component::Component;
use kernel::hil;
use kernel::platform::{KernelResources, SyscallDriverLookup};
use kernel::scheduler::cooperative::CooperativeSched;
use kernel::utilities::registers::interfaces::ReadWriteable;
use kernel::{create_capability, debug, static_init};
use rv32i::csr;
use swervolf_eh1::chip::SweRVolfDefaultPeripherals;
pub mod io;
pub const NUM_PROCS: usize = 4;
static mut PROCESSES: [Option<&'static dyn kernel::process::Process>; NUM_PROCS] =
[None; NUM_PROCS];
static mut CHIP: Option<&'static swervolf_eh1::chip::SweRVolf<SweRVolfDefaultPeripherals>> = None;
static mut PROCESS_PRINTER: Option<&'static kernel::process::ProcessPrinterText> = None;
const FAULT_RESPONSE: kernel::process::PanicFaultPolicy = kernel::process::PanicFaultPolicy {};
#[no_mangle]
#[link_section = ".stack_buffer"]
pub static mut STACK_MEMORY: [u8; 0x900] = [0; 0x900];
struct SweRVolf {
console: &'static capsules_core::console::Console<'static>,
alarm: &'static capsules_core::alarm::AlarmDriver<
'static,
VirtualMuxAlarm<'static, swervolf_eh1::syscon::SysCon<'static>>,
>,
scheduler: &'static CooperativeSched<'static>,
scheduler_timer: &'static swerv::eh1_timer::Timer<'static>,
}
impl SyscallDriverLookup for SweRVolf {
fn with_driver<F, R>(&self, driver_num: usize, f: F) -> R
where
F: FnOnce(Option<&dyn kernel::syscall::SyscallDriver>) -> R,
{
match driver_num {
capsules_core::console::DRIVER_NUM => f(Some(self.console)),
capsules_core::alarm::DRIVER_NUM => f(Some(self.alarm)),
_ => f(None),
}
}
}
impl KernelResources<swervolf_eh1::chip::SweRVolf<'static, SweRVolfDefaultPeripherals<'static>>>
for SweRVolf
{
type SyscallDriverLookup = Self;
type SyscallFilter = ();
type ProcessFault = ();
type Scheduler = CooperativeSched<'static>;
type SchedulerTimer = swerv::eh1_timer::Timer<'static>;
type WatchDog = ();
type ContextSwitchCallback = ();
fn syscall_driver_lookup(&self) -> &Self::SyscallDriverLookup {
self
}
fn syscall_filter(&self) -> &Self::SyscallFilter {
&()
}
fn process_fault(&self) -> &Self::ProcessFault {
&()
}
fn scheduler(&self) -> &Self::Scheduler {
self.scheduler
}
fn scheduler_timer(&self) -> &Self::SchedulerTimer {
self.scheduler_timer
}
fn watchdog(&self) -> &Self::WatchDog {
&()
}
fn context_switch_callback(&self) -> &Self::ContextSwitchCallback {
&()
}
}
#[no_mangle]
pub unsafe fn main() {
rv32i::configure_trap_handler(rv32i::PermissionMode::Machine);
let peripherals = static_init!(
SweRVolfDefaultPeripherals,
SweRVolfDefaultPeripherals::new()
);
let process_mgmt_cap = create_capability!(capabilities::ProcessManagementCapability);
let memory_allocation_cap = create_capability!(capabilities::MemoryAllocationCapability);
let main_loop_cap = create_capability!(capabilities::MainLoopCapability);
let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(&*addr_of!(PROCESSES)));
kernel::debug::assign_gpios(None, None, None);
let uart_mux = components::console::UartMuxComponent::new(&peripherals.uart, 115200)
.finalize(components::uart_mux_component_static!());
let mtimer = static_init!(
swervolf_eh1::syscon::SysCon,
swervolf_eh1::syscon::SysCon::new()
);
let mux_alarm = static_init!(
MuxAlarm<'static, swervolf_eh1::syscon::SysCon>,
MuxAlarm::new(mtimer)
);
hil::time::Alarm::set_alarm_client(mtimer, mux_alarm);
let virtual_alarm_user = static_init!(
VirtualMuxAlarm<'static, swervolf_eh1::syscon::SysCon>,
VirtualMuxAlarm::new(mux_alarm)
);
virtual_alarm_user.setup();
let alarm = static_init!(
capsules_core::alarm::AlarmDriver<
'static,
VirtualMuxAlarm<'static, swervolf_eh1::syscon::SysCon>,
>,
capsules_core::alarm::AlarmDriver::new(
virtual_alarm_user,
board_kernel.create_grant(capsules_core::alarm::DRIVER_NUM, &memory_allocation_cap)
)
);
hil::time::Alarm::set_alarm_client(virtual_alarm_user, alarm);
let chip = static_init!(
swervolf_eh1::chip::SweRVolf<
SweRVolfDefaultPeripherals,
>,
swervolf_eh1::chip::SweRVolf::new(peripherals, mtimer)
);
CHIP = Some(chip);
let process_printer = components::process_printer::ProcessPrinterTextComponent::new()
.finalize(components::process_printer_text_component_static!());
PROCESS_PRINTER = Some(process_printer);
chip.enable_pic_interrupts();
csr::CSR.mie.modify(
csr::mie::mie::mext::SET
+ csr::mie::mie::msoft::SET
+ csr::mie::mie::mtimer::SET
+ csr::mie::mie::BIT28::SET
+ csr::mie::mie::BIT29::SET,
);
csr::CSR.mstatus.modify(csr::mstatus::mstatus::mie::SET);
let console = components::console::ConsoleComponent::new(
board_kernel,
capsules_core::console::DRIVER_NUM,
uart_mux,
)
.finalize(components::console_component_static!());
components::debug_writer::DebugWriterComponent::new(uart_mux)
.finalize(components::debug_writer_component_static!());
debug!("SweRVolf initialisation complete.");
debug!("Entering main loop.");
extern "C" {
static _sapps: u8;
static _eapps: u8;
static mut _sappmem: u8;
static _eappmem: u8;
}
let scheduler =
components::sched::cooperative::CooperativeComponent::new(&*addr_of!(PROCESSES))
.finalize(components::cooperative_component_static!(NUM_PROCS));
let swervolf = SweRVolf {
console,
alarm,
scheduler,
scheduler_timer: chip.get_scheduler_timer(),
};
kernel::process::load_processes(
board_kernel,
chip,
core::slice::from_raw_parts(
core::ptr::addr_of!(_sapps),
core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
),
core::slice::from_raw_parts_mut(
core::ptr::addr_of_mut!(_sappmem),
core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
),
&mut *addr_of_mut!(PROCESSES),
&FAULT_RESPONSE,
&process_mgmt_cap,
)
.unwrap_or_else(|err| {
debug!("Error loading processes!");
debug!("{:?}", err);
});
board_kernel.kernel_loop(
&swervolf,
chip,
None::<&kernel::ipc::IPC<0>>,
&main_loop_cap,
);
}