Skip to content

Commit

Permalink
replace macro with struct
Browse files Browse the repository at this point in the history
  • Loading branch information
hudson-ayers committed Sep 9, 2020
1 parent d7645a2 commit 681832b
Show file tree
Hide file tree
Showing 5 changed files with 271 additions and 290 deletions.
11 changes: 5 additions & 6 deletions boards/hail/src/main.rs
Expand Up @@ -23,6 +23,7 @@ use kernel::Platform;
#[allow(unused_imports)]
use kernel::{create_capability, debug, debug_gpio, static_init};
use sam4l::adc::Channel;
use sam4l::chip::Sam4lDefaultPeripherals;

/// Support routines for debugging I/O.
///
Expand All @@ -40,9 +41,7 @@ const NUM_PROCS: usize = 20;
static mut PROCESSES: [Option<&'static dyn kernel::procs::ProcessType>; NUM_PROCS] =
[None; NUM_PROCS];

sam4l::create_default_sam4l_peripherals!(Sam4lPeripherals);

static mut CHIP: Option<&'static sam4l::chip::Sam4l<Sam4lPeripherals>> = None;
static mut CHIP: Option<&'static sam4l::chip::Sam4l<Sam4lDefaultPeripherals>> = None;

/// Dummy buffer that causes the linker to reserve enough space for the stack.
#[no_mangle]
Expand Down Expand Up @@ -107,7 +106,7 @@ impl Platform for Hail {
}

/// Helper function called during bring-up that configures multiplexed I/O.
unsafe fn set_pin_primary_functions(peripherals: &Sam4lPeripherals) {
unsafe fn set_pin_primary_functions(peripherals: &Sam4lDefaultPeripherals) {
use sam4l::gpio::PeripheralFunction::{A, B};

peripherals.pa[04].configure(Some(A)); // A0 - ADC0
Expand Down Expand Up @@ -177,7 +176,7 @@ unsafe fn set_pin_primary_functions(peripherals: &Sam4lPeripherals) {
pub unsafe fn reset_handler() {
sam4l::init();
let pm = static_init!(sam4l::pm::PowerManager, sam4l::pm::PowerManager::new());
let peripherals = static_init!(Sam4lPeripherals, Sam4lPeripherals::new(pm));
let peripherals = static_init!(Sam4lDefaultPeripherals, Sam4lDefaultPeripherals::new(pm));

pm.setup_system_clock(
sam4l::pm::SystemClockSource::PllExternalOscillatorAt48MHz {
Expand All @@ -193,7 +192,7 @@ pub unsafe fn reset_handler() {
set_pin_primary_functions(peripherals);
peripherals.setup_dma();
let chip = static_init!(
sam4l::chip::Sam4l<Sam4lPeripherals>,
sam4l::chip::Sam4l<Sam4lDefaultPeripherals>,
sam4l::chip::Sam4l::new(pm, peripherals)
);
CHIP = Some(chip);
Expand Down
11 changes: 5 additions & 6 deletions boards/imix/src/main.rs
Expand Up @@ -27,6 +27,7 @@ use kernel::hil::radio::{RadioConfig, RadioData};
use kernel::hil::Controller;
#[allow(unused_imports)]
use kernel::{create_capability, debug, debug_gpio, static_init};
use sam4l::chip::Sam4lDefaultPeripherals;

use components;
use components::alarm::{AlarmDriverComponent, AlarmMuxComponent};
Expand Down Expand Up @@ -84,9 +85,7 @@ const FAULT_RESPONSE: kernel::procs::FaultResponse = kernel::procs::FaultRespons
static mut PROCESSES: [Option<&'static dyn kernel::procs::ProcessType>; NUM_PROCS] =
[None; NUM_PROCS];

sam4l::create_default_sam4l_peripherals!(Sam4lPeripherals);

static mut CHIP: Option<&'static sam4l::chip::Sam4l<Sam4lPeripherals>> = None;
static mut CHIP: Option<&'static sam4l::chip::Sam4l<Sam4lDefaultPeripherals>> = None;

/// Dummy buffer that causes the linker to reserve enough space for the stack.
#[no_mangle]
Expand Down Expand Up @@ -170,7 +169,7 @@ impl kernel::Platform for Imix {
}
}

unsafe fn set_pin_primary_functions(peripherals: &Sam4lPeripherals) {
unsafe fn set_pin_primary_functions(peripherals: &Sam4lDefaultPeripherals) {
use sam4l::gpio::PeripheralFunction::{A, B, C, E};

// Right column: Imix pin name
Expand Down Expand Up @@ -250,7 +249,7 @@ unsafe fn set_pin_primary_functions(peripherals: &Sam4lPeripherals) {
pub unsafe fn reset_handler() {
sam4l::init();
let pm = static_init!(sam4l::pm::PowerManager, sam4l::pm::PowerManager::new());
let peripherals = static_init!(Sam4lPeripherals, Sam4lPeripherals::new(pm));
let peripherals = static_init!(Sam4lDefaultPeripherals, Sam4lDefaultPeripherals::new(pm));

pm.setup_system_clock(
sam4l::pm::SystemClockSource::PllExternalOscillatorAt48MHz {
Expand All @@ -267,7 +266,7 @@ pub unsafe fn reset_handler() {

peripherals.setup_dma();
let chip = static_init!(
sam4l::chip::Sam4l<Sam4lPeripherals>,
sam4l::chip::Sam4l<Sam4lDefaultPeripherals>,
sam4l::chip::Sam4l::new(pm, peripherals)
);
CHIP = Some(chip);
Expand Down
10 changes: 4 additions & 6 deletions boards/redboard_artemis_nano/src/main.rs
Expand Up @@ -9,6 +9,7 @@
#![feature(const_in_array_repeat_expressions)]
#![deny(missing_docs)]

use apollo3::chip::Apollo3DefaultPeripherals;
use capsules::virtual_alarm::VirtualMuxAlarm;
use kernel::capabilities;
use kernel::common::dynamic_deferred_call::DynamicDeferredCall;
Expand All @@ -28,11 +29,8 @@ const NUM_PROCS: usize = 4;
// Actual memory for holding the active process structures.
static mut PROCESSES: [Option<&'static dyn kernel::procs::ProcessType>; NUM_PROCS] = [None; 4];

// Instantiate Apollo3Peripherals struct
apollo3::create_default_apollo3_peripherals!(Apollo3Peripherals);

// Static reference to chip for panic dumps.
static mut CHIP: Option<&'static apollo3::chip::Apollo3<Apollo3Peripherals>> = None;
static mut CHIP: Option<&'static apollo3::chip::Apollo3<Apollo3DefaultPeripherals>> = None;

// How should the kernel respond when a process faults.
const FAULT_RESPONSE: kernel::procs::FaultResponse = kernel::procs::FaultResponse::Panic;
Expand Down Expand Up @@ -88,7 +86,7 @@ impl Platform for RedboardArtemisNano {
pub unsafe fn reset_handler() {
apollo3::init();

let peripherals = static_init!(Apollo3Peripherals, Apollo3Peripherals::new());
let peripherals = static_init!(Apollo3DefaultPeripherals, Apollo3DefaultPeripherals::new());

// No need to statically allocate mcu/pwr/clk_ctrl because they are only used in main!
let mcu_ctrl = apollo3::mcuctrl::McuCtrl::new();
Expand Down Expand Up @@ -231,7 +229,7 @@ pub unsafe fn reset_handler() {
);

let chip = static_init!(
apollo3::chip::Apollo3<Apollo3Peripherals>,
apollo3::chip::Apollo3<Apollo3DefaultPeripherals>,
apollo3::chip::Apollo3::new(peripherals)
);
CHIP = Some(chip);
Expand Down
117 changes: 56 additions & 61 deletions chips/apollo3/src/chip.rs
Expand Up @@ -23,69 +23,64 @@ impl<I: InterruptService<()> + 'static> Apollo3<I> {
}
}

/// This macro defines a struct that, when initialized,
/// instantiates all peripheral drivers for the apollo3. If a board
/// wishes to use only a subset of these peripherals, this
/// macro cannot be used, and this struct should be
/// constructed manually in main.rs. The input to the macro is the name of the struct
/// that will hold the peripherals, which can be chosen by the board.
#[macro_export]
macro_rules! create_default_apollo3_peripherals {
($N:ident) => {
struct $N {
stimer: apollo3::stimer::STimer<'static>,
uart0: apollo3::uart::Uart<'static>,
uart1: apollo3::uart::Uart<'static>,
gpio_port: apollo3::gpio::Port<'static>,
iom0: apollo3::iom::Iom<'static>,
iom1: apollo3::iom::Iom<'static>,
iom2: apollo3::iom::Iom<'static>,
iom3: apollo3::iom::Iom<'static>,
iom4: apollo3::iom::Iom<'static>,
iom5: apollo3::iom::Iom<'static>,
ble: apollo3::ble::Ble<'static>,
}
impl $N {
unsafe fn new() -> Self {
Self {
stimer: apollo3::stimer::STimer::new(),
uart0: apollo3::uart::Uart::new_uart_0(),
uart1: apollo3::uart::Uart::new_uart_1(),
gpio_port: apollo3::gpio::Port::new(),
iom0: apollo3::iom::Iom::new0(),
iom1: apollo3::iom::Iom::new1(),
iom2: apollo3::iom::Iom::new2(),
iom3: apollo3::iom::Iom::new3(),
iom4: apollo3::iom::Iom::new4(),
iom5: apollo3::iom::Iom::new5(),
ble: apollo3::ble::Ble::new(),
}
}
/// This struct, when initialized, instantiates all peripheral drivers for the apollo3.
/// If a board wishes to use only a subset of these peripherals, this
/// should not be used or imported, and a modified version should be
/// constructed manually in main.rs.
pub struct Apollo3DefaultPeripherals {
pub stimer: crate::stimer::STimer<'static>,
pub uart0: crate::uart::Uart<'static>,
pub uart1: crate::uart::Uart<'static>,
pub gpio_port: crate::gpio::Port<'static>,
pub iom0: crate::iom::Iom<'static>,
pub iom1: crate::iom::Iom<'static>,
pub iom2: crate::iom::Iom<'static>,
pub iom3: crate::iom::Iom<'static>,
pub iom4: crate::iom::Iom<'static>,
pub iom5: crate::iom::Iom<'static>,
pub ble: crate::ble::Ble<'static>,
}

impl Apollo3DefaultPeripherals {
pub fn new() -> Self {
Self {
stimer: crate::stimer::STimer::new(),
uart0: crate::uart::Uart::new_uart_0(),
uart1: crate::uart::Uart::new_uart_1(),
gpio_port: crate::gpio::Port::new(),
iom0: crate::iom::Iom::new0(),
iom1: crate::iom::Iom::new1(),
iom2: crate::iom::Iom::new2(),
iom3: crate::iom::Iom::new3(),
iom4: crate::iom::Iom::new4(),
iom5: crate::iom::Iom::new5(),
ble: crate::ble::Ble::new(),
}
impl kernel::InterruptService<()> for $N {
unsafe fn service_interrupt(&self, interrupt: u32) -> bool {
use apollo3::nvic;
match interrupt {
nvic::STIMER..=nvic::STIMER_CMPR7 => self.stimer.handle_interrupt(),
nvic::UART0 => self.uart0.handle_interrupt(),
nvic::UART1 => self.uart1.handle_interrupt(),
nvic::GPIO => self.gpio_port.handle_interrupt(),
nvic::IOMSTR0 => self.iom0.handle_interrupt(),
nvic::IOMSTR1 => self.iom1.handle_interrupt(),
nvic::IOMSTR2 => self.iom2.handle_interrupt(),
nvic::IOMSTR3 => self.iom3.handle_interrupt(),
nvic::IOMSTR4 => self.iom4.handle_interrupt(),
nvic::IOMSTR5 => self.iom5.handle_interrupt(),
nvic::BLE => self.ble.handle_interrupt(),
_ => return false,
}
true
}
unsafe fn service_deferred_call(&self, _: ()) -> bool {
false
}
}
}

impl kernel::InterruptService<()> for Apollo3DefaultPeripherals {
unsafe fn service_interrupt(&self, interrupt: u32) -> bool {
use crate::nvic;
match interrupt {
nvic::STIMER..=nvic::STIMER_CMPR7 => self.stimer.handle_interrupt(),
nvic::UART0 => self.uart0.handle_interrupt(),
nvic::UART1 => self.uart1.handle_interrupt(),
nvic::GPIO => self.gpio_port.handle_interrupt(),
nvic::IOMSTR0 => self.iom0.handle_interrupt(),
nvic::IOMSTR1 => self.iom1.handle_interrupt(),
nvic::IOMSTR2 => self.iom2.handle_interrupt(),
nvic::IOMSTR3 => self.iom3.handle_interrupt(),
nvic::IOMSTR4 => self.iom4.handle_interrupt(),
nvic::IOMSTR5 => self.iom5.handle_interrupt(),
nvic::BLE => self.ble.handle_interrupt(),
_ => return false,
}
};
true
}
unsafe fn service_deferred_call(&self, _: ()) -> bool {
false
}
}

impl<I: InterruptService<()> + 'static> Chip for Apollo3<I> {
Expand Down

0 comments on commit 681832b

Please sign in to comment.