psoc62xa/
chip.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
5use kernel::platform::chip::Chip;
6use kernel::platform::chip::InterruptService;
7
8use crate::{cpuss, gpio, hsiom, peri, scb, srss, tcpwm};
9use cortexm0p::{CortexM0P, CortexMVariant};
10
11pub struct Psoc62xa<'a, I: InterruptService + 'a> {
12    mpu: cortexm0p::mpu::MPU,
13    userspace_kernel_boundary: cortexm0p::syscall::SysCall,
14    interrupt_service: &'a I,
15}
16
17impl<'a, I: InterruptService> Psoc62xa<'a, I> {
18    pub fn new(interrupt_service: &'a I) -> Self {
19        Self {
20            mpu: unsafe { cortexm0p::mpu::MPU::new() },
21            userspace_kernel_boundary: unsafe { cortexm0p::syscall::SysCall::new() },
22            interrupt_service,
23        }
24    }
25}
26
27impl<I: InterruptService> Chip for Psoc62xa<'_, I> {
28    type MPU = cortexm0p::mpu::MPU;
29    type UserspaceKernelBoundary = cortexm0p::syscall::SysCall;
30
31    fn mpu(&self) -> &Self::MPU {
32        &self.mpu
33    }
34
35    fn sleep(&self) {
36        unsafe {
37            cortexm0p::support::wfi();
38        }
39    }
40
41    unsafe fn atomic<F, R>(&self, f: F) -> R
42    where
43        F: FnOnce() -> R,
44    {
45        cortexm0p::support::atomic(f)
46    }
47
48    unsafe fn print_state(&self, writer: &mut dyn core::fmt::Write) {
49        CortexM0P::print_cortexm_state(writer);
50    }
51
52    fn userspace_kernel_boundary(&self) -> &Self::UserspaceKernelBoundary {
53        &self.userspace_kernel_boundary
54    }
55
56    fn has_pending_interrupts(&self) -> bool {
57        unsafe { cortexm0p::nvic::has_pending() }
58    }
59
60    fn service_pending_interrupts(&self) {
61        unsafe {
62            loop {
63                if let Some(interrupt) = cortexm0p::nvic::next_pending() {
64                    if !self.interrupt_service.service_interrupt(interrupt) {
65                        panic!("unhandled interrupt {}", interrupt);
66                    }
67                    let n = cortexm0p::nvic::Nvic::new(interrupt);
68                    n.clear_pending();
69                    n.enable();
70                } else {
71                    break;
72                }
73            }
74            while let Some(interrupt) = cortexm0p::nvic::next_pending() {
75                let nvic = cortexm0p::nvic::Nvic::new(interrupt);
76                nvic.clear_pending();
77                nvic.enable();
78            }
79        }
80    }
81}
82
83pub struct PsoC62xaDefaultPeripherals<'a> {
84    pub cpuss: cpuss::Cpuss,
85    pub gpio: gpio::PsocPins<'a>,
86    pub hsiom: hsiom::Hsiom,
87    pub peri: peri::Peri,
88    pub scb: scb::Scb<'a>,
89    pub srss: srss::Srss,
90    pub tcpwm: tcpwm::Tcpwm0<'a>,
91}
92
93impl PsoC62xaDefaultPeripherals<'_> {
94    pub fn new() -> Self {
95        Self {
96            cpuss: cpuss::Cpuss::new(),
97            gpio: gpio::PsocPins::new(),
98            hsiom: hsiom::Hsiom::new(),
99            peri: peri::Peri::new(),
100            scb: scb::Scb::new(),
101            srss: srss::Srss::new(),
102            tcpwm: tcpwm::Tcpwm0::new(),
103        }
104    }
105}
106
107impl InterruptService for PsoC62xaDefaultPeripherals<'_> {
108    unsafe fn service_interrupt(&self, interrupt: u32) -> bool {
109        match interrupt {
110            0 => {
111                self.scb.handle_interrupt();
112                self.tcpwm.handle_interrupt();
113            }
114            1 => {
115                // We use interrupt number 1 for GPIO so we don't
116                // check for all of the GPIO ports on every non-GPIO
117                // releated interrupt.
118                self.gpio.handle_interrupt();
119            }
120            _ => return false,
121        }
122        true
123    }
124}