1#![no_std]
6#![no_main]
7#![deny(missing_docs)]
8
9mod io;
12
13kernel::stack_size! {0x2000}
14
15use core::ptr::addr_of_mut;
16
17use capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm;
18use components::led::LedsComponent;
19use kernel::component::Component;
20use kernel::hil::led::LedHigh;
21use kernel::platform::{KernelResources, SyscallDriverLookup};
22use kernel::process::ProcessArray;
23use kernel::scheduler::round_robin::RoundRobinSched;
24use kernel::{capabilities, create_capability, static_init};
25
26#[allow(unused)]
27use psoc62xa::{
28    chip::{PsoC62xaDefaultPeripherals, Psoc62xa},
29    gpio::GpioPin,
30    tcpwm::Tcpwm0,
31    BASE_VECTORS,
32};
33
34const FAULT_RESPONSE: capsules_system::process_policies::PanicFaultPolicy =
37    capsules_system::process_policies::PanicFaultPolicy {};
38
39const NUM_PROCS: usize = 4;
41
42type ChipHw = Psoc62xa<'static, PsoC62xaDefaultPeripherals<'static>>;
43
44static mut PROCESSES: Option<&'static ProcessArray<NUM_PROCS>> = None;
46static mut CHIP: Option<&'static Psoc62xa<PsoC62xaDefaultPeripherals>> = None;
47static mut PROCESS_PRINTER: Option<&'static capsules_system::process_printer::ProcessPrinterText> =
48    None;
49
50pub struct Cy8cproto0624343w {
52    console: &'static capsules_core::console::Console<'static>,
53    alarm: &'static capsules_core::alarm::AlarmDriver<
54        'static,
55        VirtualMuxAlarm<'static, psoc62xa::tcpwm::Tcpwm0<'static>>,
56    >,
57    led: &'static capsules_core::led::LedDriver<'static, LedHigh<'static, GpioPin<'static>>, 1>,
58    button: &'static capsules_core::button::Button<'static, GpioPin<'static>>,
59    gpio: &'static capsules_core::gpio::GPIO<'static, psoc62xa::gpio::GpioPin<'static>>,
60    scheduler: &'static RoundRobinSched<'static>,
61    systick: cortexm0p::systick::SysTick,
62}
63
64impl SyscallDriverLookup for Cy8cproto0624343w {
65    fn with_driver<F, R>(&self, driver_num: usize, f: F) -> R
66    where
67        F: FnOnce(Option<&dyn kernel::syscall::SyscallDriver>) -> R,
68    {
69        match driver_num {
70            capsules_core::console::DRIVER_NUM => f(Some(self.console)),
71            capsules_core::alarm::DRIVER_NUM => f(Some(self.alarm)),
72            capsules_core::led::DRIVER_NUM => f(Some(self.led)),
73            capsules_core::button::DRIVER_NUM => f(Some(self.button)),
74            capsules_core::gpio::DRIVER_NUM => f(Some(self.gpio)),
75            _ => f(None),
76        }
77    }
78}
79
80impl KernelResources<Psoc62xa<'static, PsoC62xaDefaultPeripherals<'static>>> for Cy8cproto0624343w {
81    type SyscallDriverLookup = Self;
82    type SyscallFilter = ();
83    type ProcessFault = ();
84    type Scheduler = RoundRobinSched<'static>;
85    type SchedulerTimer = cortexm0p::systick::SysTick;
86    type WatchDog = ();
87    type ContextSwitchCallback = ();
88
89    fn syscall_driver_lookup(&self) -> &Self::SyscallDriverLookup {
90        self
91    }
92    fn syscall_filter(&self) -> &Self::SyscallFilter {
93        &()
94    }
95    fn process_fault(&self) -> &Self::ProcessFault {
96        &()
97    }
98    fn scheduler(&self) -> &Self::Scheduler {
99        self.scheduler
100    }
101    fn scheduler_timer(&self) -> &Self::SchedulerTimer {
102        &self.systick
103    }
104    fn watchdog(&self) -> &Self::WatchDog {
105        &()
106    }
107    fn context_switch_callback(&self) -> &Self::ContextSwitchCallback {
108        &()
109    }
110}
111
112fn init_clocks(peripherals: &PsoC62xaDefaultPeripherals) {
113    peripherals.srss.init_clock();
114    peripherals.cpuss.init_clock();
115    peripherals.peri.init_uart_clock();
116    peripherals.peri.init_alarm_clock();
117}
118
119#[no_mangle]
121pub unsafe fn main() {
122    cortexm0p::scb::set_vector_table_offset(0x10000000 as *const ());
124
125    let peripherals = static_init!(
126        PsoC62xaDefaultPeripherals,
127        PsoC62xaDefaultPeripherals::new()
128    );
129
130    init_clocks(peripherals);
132
133    peripherals.cpuss.enable_int_for_scb5();
135    peripherals.cpuss.enable_int_for_tcpwm00();
136    peripherals.cpuss.enable_int_for_gpio0();
137    cortexm0p::nvic::enable_all();
138
139    peripherals.scb.set_standard_uart_mode();
144    peripherals.scb.enable_scb();
145    peripherals.hsiom.enable_uart();
146    let uart_tx_pin = peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P5_1);
147    uart_tx_pin.configure_drive_mode(psoc62xa::gpio::DriveMode::Strong);
148    uart_tx_pin.configure_input(false);
149    let uart_rx_pin = peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P5_0);
150    uart_rx_pin.configure_drive_mode(psoc62xa::gpio::DriveMode::HighZ);
151    uart_rx_pin.configure_input(true);
152    let chip = static_init!(
153        Psoc62xa<PsoC62xaDefaultPeripherals>,
154        Psoc62xa::new(peripherals)
155    );
156
157    let processes = components::process_array::ProcessArrayComponent::new()
159        .finalize(components::process_array_component_static!(NUM_PROCS));
160    PROCESSES = Some(processes);
161
162    let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(processes.as_slice()));
164
165    let uart_mux = components::console::UartMuxComponent::new(&peripherals.scb, 115200)
167        .finalize(components::uart_mux_component_static!());
168
169    let console = components::console::ConsoleComponent::new(
170        board_kernel,
171        capsules_core::console::DRIVER_NUM,
172        uart_mux,
173    )
174    .finalize(components::console_component_static!());
175    components::debug_writer::DebugWriterComponent::new_unsafe(
176        uart_mux,
177        create_capability!(capabilities::SetDebugWriterCapability),
178        || unsafe {
179            kernel::debug::initialize_debug_writer_wrapper_unsafe::<
180                <ChipHw as kernel::platform::chip::Chip>::ThreadIdProvider,
181            >();
182        },
183    )
184    .finalize(components::debug_writer_component_static!());
185
186    peripherals.tcpwm.init_timer();
190
191    let mux_alarm = components::alarm::AlarmMuxComponent::new(&peripherals.tcpwm)
192        .finalize(components::alarm_mux_component_static!(Tcpwm0));
193
194    let alarm = components::alarm::AlarmDriverComponent::new(
195        board_kernel,
196        capsules_core::alarm::DRIVER_NUM,
197        mux_alarm,
198    )
199    .finalize(components::alarm_component_static!(Tcpwm0));
200
201    let process_printer = components::process_printer::ProcessPrinterTextComponent::new()
205        .finalize(components::process_printer_text_component_static!());
206    PROCESS_PRINTER = Some(process_printer);
207
208    let process_console = components::process_console::ProcessConsoleComponent::new(
209        board_kernel,
210        uart_mux,
211        mux_alarm,
212        process_printer,
213        Some(cortexm0p::support::reset),
214    )
215    .finalize(components::process_console_component_static!(Tcpwm0));
216    let _ = process_console.start();
217
218    let led_pin = peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P13_7);
219
220    let led = LedsComponent::new().finalize(components::led_component_static!(
221        LedHigh<'static, GpioPin>,
222        LedHigh::new(led_pin)
223    ));
224
225    let gpio = components::gpio::GpioComponent::new(
230        board_kernel,
231        capsules_core::gpio::DRIVER_NUM,
232        components::gpio_component_helper!(
233            psoc62xa::gpio::GpioPin,
234            5 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P0_5),
235            44 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P5_4),
236            45 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P5_5),
237            46 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P5_6),
238            47 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P5_7),
239            50 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P6_2),
240            51 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P6_3),
241            52 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P6_4),
242            53 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P6_5),
243            64 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P8_0),
244            72 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P9_0),
245            73 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P9_1),
246            74 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P9_2),
247            76 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P9_4),
248            77 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P9_5),
249            78 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P9_6),
250            79 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P9_7),
251            96 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P12_0),
252            97 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P12_1),
253            99 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P12_3),
254            108 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P13_4),
255            110 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P13_6),
256        ),
257    )
258    .finalize(components::gpio_component_static!(psoc62xa::gpio::GpioPin));
259
260    let button_pin = peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P0_4);
265
266    let button = components::button::ButtonComponent::new(
267        board_kernel,
268        capsules_core::button::DRIVER_NUM,
269        components::button_component_helper!(
270            GpioPin,
271            (
272                button_pin,
273                kernel::hil::gpio::ActivationMode::ActiveLow,
274                kernel::hil::gpio::FloatingState::PullNone
275            ),
276        ),
277    )
278    .finalize(components::button_component_static!(GpioPin));
279
280    let scheduler = components::sched::round_robin::RoundRobinComponent::new(processes)
285        .finalize(components::round_robin_component_static!(NUM_PROCS));
286
287    let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
288
289    let cy8cproto0624343w = Cy8cproto0624343w {
290        console,
291        alarm,
292        scheduler,
293        led,
294        button,
295        gpio,
296        systick: cortexm0p::systick::SysTick::new_with_calibration(8_000_000),
298    };
299
300    CHIP = Some(chip);
301    (*addr_of_mut!(io::WRITER)).set_scb(&peripherals.scb);
302
303    kernel::debug!("Initialization complete. Entering main loop.");
304
305    extern "C" {
311        static _sapps: u8;
313        static _eapps: u8;
315        static mut _sappmem: u8;
317        static _eappmem: u8;
319    }
320
321    let process_management_capability =
322        create_capability!(capabilities::ProcessManagementCapability);
323
324    kernel::process::load_processes(
325        board_kernel,
326        chip,
327        core::slice::from_raw_parts(
328            core::ptr::addr_of!(_sapps),
329            core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
330        ),
331        core::slice::from_raw_parts_mut(
332            core::ptr::addr_of_mut!(_sappmem),
333            core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
334        ),
335        &FAULT_RESPONSE,
336        &process_management_capability,
337    )
338    .unwrap_or_else(|err| {
339        kernel::debug!("Error loading processes!");
340        kernel::debug!("{:?}", err);
341    });
342
343    board_kernel.kernel_loop(
344        &cy8cproto0624343w,
345        chip,
346        None::<kernel::ipc::IPC<{ NUM_PROCS as u8 }>>.as_ref(),
347        &main_loop_capability,
348    );
349}