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(
171        uart_mux,
172        create_capability!(capabilities::SetDebugWriterCapability),
173    )
174    .finalize(components::debug_writer_component_static!());
175
176    //--------------------------------------------------------------------------
177    // ALARM & TIMER
178    //--------------------------------------------------------------------------
179    peripherals.tcpwm.init_timer();
180
181    let mux_alarm = components::alarm::AlarmMuxComponent::new(&peripherals.tcpwm)
182        .finalize(components::alarm_mux_component_static!(Tcpwm0));
183
184    let alarm = components::alarm::AlarmDriverComponent::new(
185        board_kernel,
186        capsules_core::alarm::DRIVER_NUM,
187        mux_alarm,
188    )
189    .finalize(components::alarm_component_static!(Tcpwm0));
190
191    //--------------------------------------------------------------------------
192    // PROCESS CONSOLE
193    //--------------------------------------------------------------------------
194    let process_printer = components::process_printer::ProcessPrinterTextComponent::new()
195        .finalize(components::process_printer_text_component_static!());
196    PROCESS_PRINTER = Some(process_printer);
197
198    let process_console = components::process_console::ProcessConsoleComponent::new(
199        board_kernel,
200        uart_mux,
201        mux_alarm,
202        process_printer,
203        Some(cortexm0p::support::reset),
204    )
205    .finalize(components::process_console_component_static!(Tcpwm0));
206    let _ = process_console.start();
207
208    let led_pin = peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P13_7);
209
210    let led = LedsComponent::new().finalize(components::led_component_static!(
211        LedHigh<'static, GpioPin>,
212        LedHigh::new(led_pin)
213    ));
214
215    //--------------------------------------------------------------------------
216    // GPIO
217    //--------------------------------------------------------------------------
218
219    let gpio = components::gpio::GpioComponent::new(
220        board_kernel,
221        capsules_core::gpio::DRIVER_NUM,
222        components::gpio_component_helper!(
223            psoc62xa::gpio::GpioPin,
224            5 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P0_5),
225            44 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P5_4),
226            45 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P5_5),
227            46 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P5_6),
228            47 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P5_7),
229            50 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P6_2),
230            51 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P6_3),
231            52 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P6_4),
232            53 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P6_5),
233            64 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P8_0),
234            72 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P9_0),
235            73 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P9_1),
236            74 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P9_2),
237            76 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P9_4),
238            77 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P9_5),
239            78 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P9_6),
240            79 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P9_7),
241            96 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P12_0),
242            97 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P12_1),
243            99 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P12_3),
244            108 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P13_4),
245            110 => peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P13_6),
246        ),
247    )
248    .finalize(components::gpio_component_static!(psoc62xa::gpio::GpioPin));
249
250    //--------------------------------------------------------------------------
251    // BUTTON
252    //--------------------------------------------------------------------------
253
254    let button_pin = peripherals.gpio.get_pin(psoc62xa::gpio::PsocPin::P0_4);
255
256    let button = components::button::ButtonComponent::new(
257        board_kernel,
258        capsules_core::button::DRIVER_NUM,
259        components::button_component_helper!(
260            GpioPin,
261            (
262                button_pin,
263                kernel::hil::gpio::ActivationMode::ActiveLow,
264                kernel::hil::gpio::FloatingState::PullNone
265            ),
266        ),
267    )
268    .finalize(components::button_component_static!(GpioPin));
269
270    //--------------------------------------------------------------------------
271    // FINAL SETUP AND BOARD BOOT
272    //--------------------------------------------------------------------------
273
274    let scheduler = components::sched::round_robin::RoundRobinComponent::new(&*addr_of!(PROCESSES))
275        .finalize(components::round_robin_component_static!(NUM_PROCS));
276
277    let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
278
279    let cy8cproto0624343w = Cy8cproto0624343w {
280        console,
281        alarm,
282        scheduler,
283        led,
284        button,
285        gpio,
286        // Currently, the CPU runs at 8MHz, that being the frequency of the IMO.
287        systick: cortexm0p::systick::SysTick::new_with_calibration(8_000_000),
288    };
289
290    CHIP = Some(chip);
291    (*addr_of_mut!(io::WRITER)).set_scb(&peripherals.scb);
292
293    kernel::debug!("Initialization complete. Entering main loop.");
294
295    //--------------------------------------------------------------------------
296    // PROCESSES AND MAIN LOOP
297    //--------------------------------------------------------------------------
298
299    // These symbols are defined in the linker script.
300    extern "C" {
301        /// Beginning of the ROM region containing app images.
302        static _sapps: u8;
303        /// End of the ROM region containing app images.
304        static _eapps: u8;
305        /// Beginning of the RAM region for app memory.
306        static mut _sappmem: u8;
307        /// End of the RAM region for app memory.
308        static _eappmem: u8;
309    }
310
311    let process_management_capability =
312        create_capability!(capabilities::ProcessManagementCapability);
313
314    kernel::process::load_processes(
315        board_kernel,
316        chip,
317        core::slice::from_raw_parts(
318            core::ptr::addr_of!(_sapps),
319            core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
320        ),
321        core::slice::from_raw_parts_mut(
322            core::ptr::addr_of_mut!(_sappmem),
323            core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
324        ),
325        &mut *addr_of_mut!(PROCESSES),
326        &FAULT_RESPONSE,
327        &process_management_capability,
328    )
329    .unwrap_or_else(|err| {
330        kernel::debug!("Error loading processes!");
331        kernel::debug!("{:?}", err);
332    });
333
334    board_kernel.kernel_loop(
335        &cy8cproto0624343w,
336        chip,
337        None::<kernel::ipc::IPC<{ NUM_PROCS as u8 }>>.as_ref(),
338        &main_loop_capability,
339    );
340}