nrf52840/
interrupt_service.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 Tock Contributors 2022.
4
5use kernel::hil::time::Alarm;
6use nrf52::chip::Nrf52DefaultPeripherals;
7
8/// This struct, when initialized, instantiates all peripheral drivers for the nrf52840.
9///
10/// If a board wishes to use only a subset of these peripherals, this
11/// should not be used or imported, and a modified version should be
12/// constructed manually in main.rs.
13//create all base nrf52 peripherals
14pub struct Nrf52840DefaultPeripherals<'a> {
15    pub nrf52: Nrf52DefaultPeripherals<'a>,
16    pub ieee802154_radio: crate::ieee802154_radio::Radio<'a>,
17    pub usbd: crate::usbd::Usbd<'a>,
18    pub gpio_port: crate::gpio::Port<'a, { crate::gpio::NUM_PINS }>,
19}
20
21impl Nrf52840DefaultPeripherals<'_> {
22    pub unsafe fn new(
23        ieee802154_radio_ack_buf: &'static mut [u8; crate::ieee802154_radio::ACK_BUF_SIZE],
24    ) -> Self {
25        Self {
26            nrf52: Nrf52DefaultPeripherals::new(),
27            ieee802154_radio: crate::ieee802154_radio::Radio::new(ieee802154_radio_ack_buf),
28            usbd: crate::usbd::Usbd::new(),
29            gpio_port: crate::gpio::nrf52840_gpio_create(),
30        }
31    }
32    // Necessary for setting up circular dependencies
33    pub fn init(&'static self) {
34        self.ieee802154_radio.set_timer_ref(&self.nrf52.timer0);
35        self.nrf52.timer0.set_alarm_client(&self.ieee802154_radio);
36        self.nrf52.pwr_clk.set_usb_client(&self.usbd);
37        self.usbd.set_power_ref(&self.nrf52.pwr_clk);
38        kernel::deferred_call::DeferredCallClient::register(&self.ieee802154_radio);
39        self.nrf52.init();
40    }
41}
42impl kernel::platform::chip::InterruptService for Nrf52840DefaultPeripherals<'_> {
43    unsafe fn service_interrupt(&self, interrupt: u32) -> bool {
44        match interrupt {
45            crate::peripheral_interrupts::USBD => self.usbd.handle_interrupt(),
46            nrf52::peripheral_interrupts::GPIOTE => self.gpio_port.handle_interrupt(),
47            nrf52::peripheral_interrupts::RADIO => {
48                match (
49                    self.ieee802154_radio.is_enabled(),
50                    self.nrf52.ble_radio.is_enabled(),
51                ) {
52                    (false, false) => (),
53                    (true, false) => self.ieee802154_radio.handle_interrupt(),
54                    (false, true) => self.nrf52.ble_radio.handle_interrupt(),
55                    (true, true) => kernel::debug!(
56                        "nRF 802.15.4 and BLE radios cannot be simultaneously enabled!"
57                    ),
58                }
59            }
60            _ => return self.nrf52.service_interrupt(interrupt),
61        }
62        true
63    }
64}