nrf52833/
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.
13pub struct Nrf52833DefaultPeripherals<'a> {
14    pub nrf52: Nrf52DefaultPeripherals<'a>,
15    pub ieee802154_radio: crate::ieee802154_radio::Radio<'a>,
16    pub gpio_port: crate::gpio::Port<'a, { crate::gpio::NUM_PINS }>,
17}
18impl Nrf52833DefaultPeripherals<'_> {
19    pub unsafe fn new(
20        ieee802154_radio_ack_buf: &'static mut [u8; crate::ieee802154_radio::ACK_BUF_SIZE],
21    ) -> Self {
22        Self {
23            nrf52: Nrf52DefaultPeripherals::new(),
24            ieee802154_radio: crate::ieee802154_radio::Radio::new(ieee802154_radio_ack_buf),
25            gpio_port: crate::gpio::nrf52833_gpio_create(),
26        }
27    }
28    // Necessary for setting up circular dependencies
29    pub fn init(&'static self) {
30        self.ieee802154_radio.set_timer_ref(&self.nrf52.timer0);
31        self.nrf52.timer0.set_alarm_client(&self.ieee802154_radio);
32        kernel::deferred_call::DeferredCallClient::register(&self.ieee802154_radio);
33        self.nrf52.init();
34    }
35}
36impl kernel::platform::chip::InterruptService for Nrf52833DefaultPeripherals<'_> {
37    unsafe fn service_interrupt(&self, interrupt: u32) -> bool {
38        match interrupt {
39            nrf52::peripheral_interrupts::GPIOTE => self.gpio_port.handle_interrupt(),
40            nrf52::peripheral_interrupts::RADIO => {
41                match (
42                    self.ieee802154_radio.is_enabled(),
43                    self.nrf52.ble_radio.is_enabled(),
44                ) {
45                    (false, false) => (),
46                    (true, false) => self.ieee802154_radio.handle_interrupt(),
47                    (false, true) => self.nrf52.ble_radio.handle_interrupt(),
48                    (true, true) => kernel::debug!(
49                        "nRF 802.15.4 and BLE radios cannot be simultaneously enabled!"
50                    ),
51                }
52            }
53            _ => return self.nrf52.service_interrupt(interrupt),
54        }
55        true
56    }
57}