blob: 860469168bdac0a2ef54a1834558b61bfe382682 [file] [log] [blame]
Woyten141e48a2020-01-12 21:01:25 +01001use crate::adc::AdcDriverFactory;
Woyten70c09f92019-12-01 15:17:32 +01002use crate::buttons::ButtonsDriverFactory;
torfmaster63b0ad22019-12-26 18:11:00 +01003use crate::console::ConsoleDriver;
Alistair Francis0f7416e2020-09-10 18:54:51 -07004use crate::ctap::CtapDriverFactory;
Woytenb50a9872019-12-01 01:12:57 +01005use crate::gpio::GpioDriverFactory;
Alistair Francisbe409182020-03-24 10:54:32 -07006use crate::hmac::HmacDriverFactory;
Alistair Francisdbf05282020-11-04 16:31:23 -08007use crate::i2c_master::I2cDriverFactory;
Alistair Francisf99feff2020-11-04 16:30:51 -08008use crate::i2c_master_slave::I2cMSDriverFactory;
Woyten70c09f92019-12-01 15:17:32 +01009use crate::leds::LedsDriverFactory;
torfmaster63b0ad22019-12-26 18:11:00 +010010use crate::result::OtherError;
11use crate::result::TockError;
torfmaster0c1aead2019-12-27 18:03:10 +010012use crate::rng::RngDriver;
torfmasterd1db7512019-12-27 18:39:09 +010013use crate::sensors::ninedof::NinedofDriver;
14use crate::sensors::AmbientLightSensor;
15use crate::sensors::HumiditySensor;
16use crate::sensors::TemperatureSensor;
torfmaster76c81942020-02-23 17:31:59 +010017use crate::simple_ble::BleAdvertisingDriverFactory;
18use crate::simple_ble::BleScanningDriverFactory;
Woytenad549692019-12-01 18:55:32 +010019use crate::temperature::TemperatureDriverFactory;
torfmaster38727a12019-12-26 18:43:34 +010020use crate::timer::DriverContext;
21use core::cell::Cell;
torfmaster63b0ad22019-12-26 18:11:00 +010022
torfmasterb73c5ef2020-01-02 20:47:42 +010023/// Struct containing all drivers constructible through [retrieve_drivers()]
torfmasteraeccf752019-12-27 21:00:13 +010024#[non_exhaustive]
torfmasterb73c5ef2020-01-02 20:47:42 +010025pub struct Drivers {
Woytenb1226202020-01-13 22:47:48 +010026 pub console: ConsoleDriver,
Alistair Francis0f7416e2020-09-10 18:54:51 -070027 pub ctap: CtapDriverFactory,
Woytenb1226202020-01-13 22:47:48 +010028 pub leds: LedsDriverFactory,
29 pub timer: DriverContext,
30 pub gpio: GpioDriverFactory,
Alistair Francisbe409182020-03-24 10:54:32 -070031 pub hmac: HmacDriverFactory,
Woytenb1226202020-01-13 22:47:48 +010032 pub temperature: TemperatureDriverFactory,
33 pub buttons: ButtonsDriverFactory,
34 pub adc: AdcDriverFactory,
Alistair Francisf99feff2020-11-04 16:30:51 -080035 pub i2c_ms: I2cMSDriverFactory,
Alistair Francisdbf05282020-11-04 16:31:23 -080036 pub i2c: I2cDriverFactory,
Woytenb1226202020-01-13 22:47:48 +010037 pub rng: RngDriver,
torfmaster76c81942020-02-23 17:31:59 +010038 pub ble_advertising: BleAdvertisingDriverFactory,
39 pub ble_scanning: BleScanningDriverFactory,
torfmasterd1db7512019-12-27 18:39:09 +010040 pub ambient_light_sensor: AmbientLightSensor,
41 pub temperature_sensor: TemperatureSensor,
42 pub humidity_sensor: HumiditySensor,
Woytenb1226202020-01-13 22:47:48 +010043 pub ninedof: NinedofDriver,
torfmaster63b0ad22019-12-26 18:11:00 +010044}
45
torfmasterb73c5ef2020-01-02 20:47:42 +010046/// Retrieve [Drivers] struct. Returns struct only once.
Woytence2bc6a2020-01-19 00:33:30 +010047pub fn retrieve_drivers() -> Result<Drivers, DriversAlreadyTakenError> {
Woytenb49dd522020-02-16 21:22:07 +010048 static mut DRIVER_TAKEN: bool = false;
49
50 unsafe {
51 if DRIVER_TAKEN {
Woytence2bc6a2020-01-19 00:33:30 +010052 Err(DriversAlreadyTakenError)
Woytenb49dd522020-02-16 21:22:07 +010053 } else {
54 DRIVER_TAKEN = true;
55 Ok(retrieve_drivers_unsafe())
56 }
torfmaster63b0ad22019-12-26 18:11:00 +010057 }
58}
59
torfmasterb73c5ef2020-01-02 20:47:42 +010060/// Retrieve [Drivers] struct without check whether it has already been taken
torfmaster63b0ad22019-12-26 18:11:00 +010061/// at a different point.
62/// # Safety
63/// This shall only used in special situations where drivers cannot be passed as arguments
64/// as in the panic handler. Otherwise global mutable state (as shared buffers) may be exposed
65/// in an unsafe manner.
torfmasterb73c5ef2020-01-02 20:47:42 +010066pub unsafe fn retrieve_drivers_unsafe() -> Drivers {
67 DRIVERS
torfmaster63b0ad22019-12-26 18:11:00 +010068}
69
torfmaster38727a12019-12-26 18:43:34 +010070#[allow(clippy::declare_interior_mutable_const)]
torfmasterb73c5ef2020-01-02 20:47:42 +010071const DRIVERS: Drivers = Drivers {
Woytenb1226202020-01-13 22:47:48 +010072 adc: AdcDriverFactory,
torfmaster76c81942020-02-23 17:31:59 +010073 ble_advertising: BleAdvertisingDriverFactory,
74 ble_scanning: BleScanningDriverFactory,
Woytenb1226202020-01-13 22:47:48 +010075 buttons: ButtonsDriverFactory,
76 console: ConsoleDriver,
Alistair Francis0f7416e2020-09-10 18:54:51 -070077 ctap: CtapDriverFactory,
Woytenb1226202020-01-13 22:47:48 +010078 leds: LedsDriverFactory,
79 timer: DriverContext {
torfmaster38727a12019-12-26 18:43:34 +010080 active_timer: Cell::new(None),
81 },
Woytenb1226202020-01-13 22:47:48 +010082 gpio: GpioDriverFactory,
Alistair Francisbe409182020-03-24 10:54:32 -070083 hmac: HmacDriverFactory,
Alistair Francisf99feff2020-11-04 16:30:51 -080084 i2c_ms: I2cMSDriverFactory,
Alistair Francisdbf05282020-11-04 16:31:23 -080085 i2c: I2cDriverFactory,
Woytenb1226202020-01-13 22:47:48 +010086 temperature: TemperatureDriverFactory,
87 rng: RngDriver,
torfmaster97c5e722020-01-02 21:30:16 +010088 ambient_light_sensor: AmbientLightSensor,
89 temperature_sensor: TemperatureSensor,
90 humidity_sensor: HumiditySensor,
Woytenb1226202020-01-13 22:47:48 +010091 ninedof: NinedofDriver,
torfmaster63b0ad22019-12-26 18:11:00 +010092};
Woytence2bc6a2020-01-19 00:33:30 +010093
94pub struct DriversAlreadyTakenError;
95
96impl From<DriversAlreadyTakenError> for TockError {
97 fn from(_: DriversAlreadyTakenError) -> Self {
98 TockError::Other(OtherError::DriversAlreadyTaken)
99 }
100}