cy8cproto_62_4243_w/
main.rs

1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright OxidOS Automotive 2025 SRL.
4
5#![no_std]
6#![no_main]
7#![deny(missing_docs)]
8
9//! Tock kernel for the CY8CPROTO-062-4343W.
10
11mod io;
12
13/// Kernel stack memory
14#[no_mangle]
15#[link_section = ".stack_buffer"]
16pub static mut STACK_MEMORY: [u8; 0x2000] = [0; 0x2000];
17
18use capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm;
19use components::led::LedsComponent;
20use core::ptr::{addr_of, addr_of_mut};
21use kernel::component::Component;
22use kernel::hil::led::LedHigh;
23use kernel::platform::{KernelResources, SyscallDriverLookup};
24use kernel::scheduler::round_robin::RoundRobinSched;
25use kernel::{capabilities, create_capability, static_init, Kernel};
26
27#[allow(unused)]
28use psoc62xa::{
29    chip::{PsoC62xaDefaultPeripherals, Psoc62xa},
30    gpio::GpioPin,
31    tcpwm::Tcpwm0,
32    BASE_VECTORS,
33};
34
35// State for loading and holding applications.
36// How should the kernel respond when a process faults.
37const FAULT_RESPONSE: capsules_system::process_policies::PanicFaultPolicy =
38    capsules_system::process_policies::PanicFaultPolicy {};
39
40// Number of concurrent processes this platform supports.
41const NUM_PROCS: usize = 4;
42
43static mut PROCESSES: [Option<&'static dyn kernel::process::Process>; NUM_PROCS] =
44    [None; NUM_PROCS];
45
46static mut CHIP: Option<&'static Psoc62xa<PsoC62xaDefaultPeripherals>> = None;
47
48static mut PROCESS_PRINTER: Option<&'static capsules_system::process_printer::ProcessPrinterText> =
49    None;
50
51/// Supported drivers by the platform
52pub struct Cy8cproto0624343w {
53    console: &'static capsules_core::console::Console<'static>,
54    alarm: &'static capsules_core::alarm::AlarmDriver<
55        'static,
56        VirtualMuxAlarm<'static, psoc62xa::tcpwm::Tcpwm0<'static>>,
57    >,
58    led: &'static capsules_core::led::LedDriver<'static, LedHigh<'static, GpioPin<'static>>, 1>,
59    button: &'static capsules_core::button::Button<'static, GpioPin<'static>>,
60    gpio: &'static capsules_core::gpio::GPIO<'static, psoc62xa::gpio::GpioPin<'static>>,
61    scheduler: &'static RoundRobinSched<'static>,
62    systick: cortexm0p::systick::SysTick,
63}
64
65impl SyscallDriverLookup for Cy8cproto0624343w {
66    fn with_driver<F, R>(&self, driver_num: usize, f: F) -> R
67    where
68        F: FnOnce(Option<&dyn kernel::syscall::SyscallDriver>) -> R,
69    {
70        match driver_num {
71            capsules_core::console::DRIVER_NUM => f(Some(self.console)),
72            capsules_core::alarm::DRIVER_NUM => f(Some(self.alarm)),
73            capsules_core::led::DRIVER_NUM => f(Some(self.led)),
74            capsules_core::button::DRIVER_NUM => f(Some(self.button)),
75            capsules_core::gpio::DRIVER_NUM => f(Some(self.gpio)),
76            _ => f(None),
77        }
78    }
79}
80
81impl KernelResources<Psoc62xa<'static, PsoC62xaDefaultPeripherals<'static>>> for Cy8cproto0624343w {
82    type SyscallDriverLookup = Self;
83    type SyscallFilter = ();
84    type ProcessFault = ();
85    type Scheduler = RoundRobinSched<'static>;
86    type SchedulerTimer = cortexm0p::systick::SysTick;
87    type WatchDog = ();
88    type ContextSwitchCallback = ();
89
90    fn syscall_driver_lookup(&self) -> &Self::SyscallDriverLookup {
91        self
92    }
93    fn syscall_filter(&self) -> &Self::SyscallFilter {
94        &()
95    }
96    fn process_fault(&self) -> &Self::ProcessFault {
97        &()
98    }
99    fn scheduler(&self) -> &Self::Scheduler {
100        self.scheduler
101    }
102    fn scheduler_timer(&self) -> &Self::SchedulerTimer {
103        &self.systick
104    }
105    fn watchdog(&self) -> &Self::WatchDog {
106        &()
107    }
108    fn context_switch_callback(&self) -> &Self::ContextSwitchCallback {
109        &()
110    }
111}
112
113fn init_clocks(peripherals: &PsoC62xaDefaultPeripherals) {
114    peripherals.srss.init_clock();
115    peripherals.cpuss.init_clock();
116    peripherals.peri.init_uart_clock();
117    peripherals.peri.init_alarm_clock();
118}
119
120/// Main function called after RAM initialized.
121#[no_mangle]
122pub unsafe fn main() {
123    // Set the offset of the vector table
124    cortexm0p::scb::set_vector_table_offset(0x10000000 as *const ());
125
126    let peripherals = static_init!(
127        PsoC62xaDefaultPeripherals,
128        PsoC62xaDefaultPeripherals::new()
129    );
130
131    // Initialize clocks
132    init_clocks(peripherals);
133
134    // Enable interrupts
135    peripherals.cpuss.enable_int_for_scb5();
136    peripherals.cpuss.enable_int_for_tcpwm00();
137    peripherals.cpuss.enable_int_for_gpio0();
138    cortexm0p::nvic::enable_all();
139
140    //--------------------------------------------------------------------------
141    // UART & CONSOLE & DEBUG
142    //--------------------------------------------------------------------------
143
144    peripherals.scb.set_standard_uart_mode();
145    peripherals.scb.enable_scb();
146    peripherals.hsiom.enable_uart();
147    let uart_tx_pin = peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P5_1);
148    uart_tx_pin.configure_drive_mode(psoc62xa::gpio::DriveMode::Strong);
149    uart_tx_pin.configure_input(false);
150    let uart_rx_pin = peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P5_0);
151    uart_rx_pin.configure_drive_mode(psoc62xa::gpio::DriveMode::HighZ);
152    uart_rx_pin.configure_input(true);
153    let chip = static_init!(
154        Psoc62xa<PsoC62xaDefaultPeripherals>,
155        Psoc62xa::new(peripherals)
156    );
157
158    let board_kernel = static_init!(Kernel, Kernel::new(&*addr_of!(PROCESSES)));
159
160    // Create a shared UART channel for kernel debug.
161    let uart_mux = components::console::UartMuxComponent::new(&peripherals.scb, 115200)
162        .finalize(components::uart_mux_component_static!());
163
164    let console = components::console::ConsoleComponent::new(
165        board_kernel,
166        capsules_core::console::DRIVER_NUM,
167        uart_mux,
168    )
169    .finalize(components::console_component_static!());
170    components::debug_writer::DebugWriterComponent::new(uart_mux)
171        .finalize(components::debug_writer_component_static!());
172
173    //--------------------------------------------------------------------------
174    // ALARM & TIMER
175    //--------------------------------------------------------------------------
176    peripherals.tcpwm.init_timer();
177
178    let mux_alarm = components::alarm::AlarmMuxComponent::new(&peripherals.tcpwm)
179        .finalize(components::alarm_mux_component_static!(Tcpwm0));
180
181    let alarm = components::alarm::AlarmDriverComponent::new(
182        board_kernel,
183        capsules_core::alarm::DRIVER_NUM,
184        mux_alarm,
185    )
186    .finalize(components::alarm_component_static!(Tcpwm0));
187
188    //--------------------------------------------------------------------------
189    // PROCESS CONSOLE
190    //--------------------------------------------------------------------------
191    let process_printer = components::process_printer::ProcessPrinterTextComponent::new()
192        .finalize(components::process_printer_text_component_static!());
193    PROCESS_PRINTER = Some(process_printer);
194
195    let process_console = components::process_console::ProcessConsoleComponent::new(
196        board_kernel,
197        uart_mux,
198        mux_alarm,
199        process_printer,
200        Some(cortexm0p::support::reset),
201    )
202    .finalize(components::process_console_component_static!(Tcpwm0));
203    let _ = process_console.start();
204
205    let led_pin = peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P13_7);
206
207    let led = LedsComponent::new().finalize(components::led_component_static!(
208        LedHigh<'static, GpioPin>,
209        LedHigh::new(led_pin)
210    ));
211
212    //--------------------------------------------------------------------------
213    // GPIO
214    //--------------------------------------------------------------------------
215
216    let gpio = components::gpio::GpioComponent::new(
217        board_kernel,
218        capsules_core::gpio::DRIVER_NUM,
219        components::gpio_component_helper!(
220            psoc62xa::gpio::GpioPin,
221            5 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P0_5),
222            44 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P5_4),
223            45 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P5_5),
224            46 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P5_6),
225            47 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P5_7),
226            50 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P6_2),
227            51 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P6_3),
228            52 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P6_4),
229            53 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P6_5),
230            64 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P8_0),
231            72 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P9_0),
232            73 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P9_1),
233            74 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P9_2),
234            76 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P9_4),
235            77 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P9_5),
236            78 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P9_6),
237            79 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P9_7),
238            96 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P12_0),
239            97 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P12_1),
240            99 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P12_3),
241            108 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P13_4),
242            110 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P13_6),
243        ),
244    )
245    .finalize(components::gpio_component_static!(psoc62xa::gpio::GpioPin));
246
247    //--------------------------------------------------------------------------
248    // BUTTON
249    //--------------------------------------------------------------------------
250
251    let button_pin = peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P0_4);
252
253    let button = components::button::ButtonComponent::new(
254        board_kernel,
255        capsules_core::button::DRIVER_NUM,
256        components::button_component_helper!(
257            GpioPin,
258            (
259                button_pin,
260                kernel::hil::gpio::ActivationMode::ActiveLow,
261                kernel::hil::gpio::FloatingState::PullNone
262            ),
263        ),
264    )
265    .finalize(components::button_component_static!(GpioPin));
266
267    //--------------------------------------------------------------------------
268    // FINAL SETUP AND BOARD BOOT
269    //--------------------------------------------------------------------------
270
271    let scheduler = components::sched::round_robin::RoundRobinComponent::new(&*addr_of!(PROCESSES))
272        .finalize(components::round_robin_component_static!(NUM_PROCS));
273
274    let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
275
276    let cy8cproto0624343w = Cy8cproto0624343w {
277        console,
278        alarm,
279        scheduler,
280        led,
281        button,
282        gpio,
283        // Currently, the CPU runs at 8MHz, that being the frequency of the IMO.
284        systick: cortexm0p::systick::SysTick::new_with_calibration(8_000_000),
285    };
286
287    CHIP = Some(chip);
288    (*addr_of_mut!(io::WRITER)).set_scb(&peripherals.scb);
289
290    kernel::debug!("Initialization complete. Entering main loop.");
291
292    //--------------------------------------------------------------------------
293    // PROCESSES AND MAIN LOOP
294    //--------------------------------------------------------------------------
295
296    // These symbols are defined in the linker script.
297    extern "C" {
298        /// Beginning of the ROM region containing app images.
299        static _sapps: u8;
300        /// End of the ROM region containing app images.
301        static _eapps: u8;
302        /// Beginning of the RAM region for app memory.
303        static mut _sappmem: u8;
304        /// End of the RAM region for app memory.
305        static _eappmem: u8;
306    }
307
308    let process_management_capability =
309        create_capability!(capabilities::ProcessManagementCapability);
310
311    kernel::process::load_processes(
312        board_kernel,
313        chip,
314        core::slice::from_raw_parts(
315            core::ptr::addr_of!(_sapps),
316            core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
317        ),
318        core::slice::from_raw_parts_mut(
319            core::ptr::addr_of_mut!(_sappmem),
320            core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
321        ),
322        &mut *addr_of_mut!(PROCESSES),
323        &FAULT_RESPONSE,
324        &process_management_capability,
325    )
326    .unwrap_or_else(|err| {
327        kernel::debug!("Error loading processes!");
328        kernel::debug!("{:?}", err);
329    });
330
331    board_kernel.kernel_loop(
332        &cy8cproto0624343w,
333        chip,
334        None::<kernel::ipc::IPC<{ NUM_PROCS as u8 }>>.as_ref(),
335        &main_loop_capability,
336    );
337}