Woyten | 141e48a | 2020-01-12 21:01:25 +0100 | [diff] [blame] | 1 | use crate::adc::AdcDriverFactory; |
Woyten | 70c09f9 | 2019-12-01 15:17:32 +0100 | [diff] [blame] | 2 | use crate::buttons::ButtonsDriverFactory; |
torfmaster | 63b0ad2 | 2019-12-26 18:11:00 +0100 | [diff] [blame] | 3 | use crate::console::ConsoleDriver; |
Alistair Francis | 0f7416e | 2020-09-10 18:54:51 -0700 | [diff] [blame] | 4 | use crate::ctap::CtapDriverFactory; |
Woyten | b50a987 | 2019-12-01 01:12:57 +0100 | [diff] [blame] | 5 | use crate::gpio::GpioDriverFactory; |
Alistair Francis | be40918 | 2020-03-24 10:54:32 -0700 | [diff] [blame] | 6 | use crate::hmac::HmacDriverFactory; |
Alistair Francis | dbf0528 | 2020-11-04 16:31:23 -0800 | [diff] [blame] | 7 | use crate::i2c_master::I2cDriverFactory; |
Alistair Francis | f99feff | 2020-11-04 16:30:51 -0800 | [diff] [blame] | 8 | use crate::i2c_master_slave::I2cMSDriverFactory; |
Woyten | 70c09f9 | 2019-12-01 15:17:32 +0100 | [diff] [blame] | 9 | use crate::leds::LedsDriverFactory; |
torfmaster | 63b0ad2 | 2019-12-26 18:11:00 +0100 | [diff] [blame] | 10 | use crate::result::OtherError; |
| 11 | use crate::result::TockError; |
torfmaster | 0c1aead | 2019-12-27 18:03:10 +0100 | [diff] [blame] | 12 | use crate::rng::RngDriver; |
torfmaster | d1db751 | 2019-12-27 18:39:09 +0100 | [diff] [blame] | 13 | use crate::sensors::ninedof::NinedofDriver; |
| 14 | use crate::sensors::AmbientLightSensor; |
| 15 | use crate::sensors::HumiditySensor; |
| 16 | use crate::sensors::TemperatureSensor; |
torfmaster | 76c8194 | 2020-02-23 17:31:59 +0100 | [diff] [blame] | 17 | use crate::simple_ble::BleAdvertisingDriverFactory; |
| 18 | use crate::simple_ble::BleScanningDriverFactory; |
Woyten | ad54969 | 2019-12-01 18:55:32 +0100 | [diff] [blame] | 19 | use crate::temperature::TemperatureDriverFactory; |
torfmaster | 38727a1 | 2019-12-26 18:43:34 +0100 | [diff] [blame] | 20 | use crate::timer::DriverContext; |
| 21 | use core::cell::Cell; |
torfmaster | 63b0ad2 | 2019-12-26 18:11:00 +0100 | [diff] [blame] | 22 | |
torfmaster | b73c5ef | 2020-01-02 20:47:42 +0100 | [diff] [blame] | 23 | /// Struct containing all drivers constructible through [retrieve_drivers()] |
torfmaster | aeccf75 | 2019-12-27 21:00:13 +0100 | [diff] [blame] | 24 | #[non_exhaustive] |
torfmaster | b73c5ef | 2020-01-02 20:47:42 +0100 | [diff] [blame] | 25 | pub struct Drivers { |
Woyten | b122620 | 2020-01-13 22:47:48 +0100 | [diff] [blame] | 26 | pub console: ConsoleDriver, |
Alistair Francis | 0f7416e | 2020-09-10 18:54:51 -0700 | [diff] [blame] | 27 | pub ctap: CtapDriverFactory, |
Woyten | b122620 | 2020-01-13 22:47:48 +0100 | [diff] [blame] | 28 | pub leds: LedsDriverFactory, |
| 29 | pub timer: DriverContext, |
| 30 | pub gpio: GpioDriverFactory, |
Alistair Francis | be40918 | 2020-03-24 10:54:32 -0700 | [diff] [blame] | 31 | pub hmac: HmacDriverFactory, |
Woyten | b122620 | 2020-01-13 22:47:48 +0100 | [diff] [blame] | 32 | pub temperature: TemperatureDriverFactory, |
| 33 | pub buttons: ButtonsDriverFactory, |
| 34 | pub adc: AdcDriverFactory, |
Alistair Francis | f99feff | 2020-11-04 16:30:51 -0800 | [diff] [blame] | 35 | pub i2c_ms: I2cMSDriverFactory, |
Alistair Francis | dbf0528 | 2020-11-04 16:31:23 -0800 | [diff] [blame] | 36 | pub i2c: I2cDriverFactory, |
Woyten | b122620 | 2020-01-13 22:47:48 +0100 | [diff] [blame] | 37 | pub rng: RngDriver, |
torfmaster | 76c8194 | 2020-02-23 17:31:59 +0100 | [diff] [blame] | 38 | pub ble_advertising: BleAdvertisingDriverFactory, |
| 39 | pub ble_scanning: BleScanningDriverFactory, |
torfmaster | d1db751 | 2019-12-27 18:39:09 +0100 | [diff] [blame] | 40 | pub ambient_light_sensor: AmbientLightSensor, |
| 41 | pub temperature_sensor: TemperatureSensor, |
| 42 | pub humidity_sensor: HumiditySensor, |
Woyten | b122620 | 2020-01-13 22:47:48 +0100 | [diff] [blame] | 43 | pub ninedof: NinedofDriver, |
torfmaster | 63b0ad2 | 2019-12-26 18:11:00 +0100 | [diff] [blame] | 44 | } |
| 45 | |
torfmaster | b73c5ef | 2020-01-02 20:47:42 +0100 | [diff] [blame] | 46 | /// Retrieve [Drivers] struct. Returns struct only once. |
Woyten | ce2bc6a | 2020-01-19 00:33:30 +0100 | [diff] [blame] | 47 | pub fn retrieve_drivers() -> Result<Drivers, DriversAlreadyTakenError> { |
Woyten | b49dd52 | 2020-02-16 21:22:07 +0100 | [diff] [blame] | 48 | static mut DRIVER_TAKEN: bool = false; |
| 49 | |
| 50 | unsafe { |
| 51 | if DRIVER_TAKEN { |
Woyten | ce2bc6a | 2020-01-19 00:33:30 +0100 | [diff] [blame] | 52 | Err(DriversAlreadyTakenError) |
Woyten | b49dd52 | 2020-02-16 21:22:07 +0100 | [diff] [blame] | 53 | } else { |
| 54 | DRIVER_TAKEN = true; |
| 55 | Ok(retrieve_drivers_unsafe()) |
| 56 | } |
torfmaster | 63b0ad2 | 2019-12-26 18:11:00 +0100 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
torfmaster | b73c5ef | 2020-01-02 20:47:42 +0100 | [diff] [blame] | 60 | /// Retrieve [Drivers] struct without check whether it has already been taken |
torfmaster | 63b0ad2 | 2019-12-26 18:11:00 +0100 | [diff] [blame] | 61 | /// 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. |
torfmaster | b73c5ef | 2020-01-02 20:47:42 +0100 | [diff] [blame] | 66 | pub unsafe fn retrieve_drivers_unsafe() -> Drivers { |
| 67 | DRIVERS |
torfmaster | 63b0ad2 | 2019-12-26 18:11:00 +0100 | [diff] [blame] | 68 | } |
| 69 | |
torfmaster | 38727a1 | 2019-12-26 18:43:34 +0100 | [diff] [blame] | 70 | #[allow(clippy::declare_interior_mutable_const)] |
torfmaster | b73c5ef | 2020-01-02 20:47:42 +0100 | [diff] [blame] | 71 | const DRIVERS: Drivers = Drivers { |
Woyten | b122620 | 2020-01-13 22:47:48 +0100 | [diff] [blame] | 72 | adc: AdcDriverFactory, |
torfmaster | 76c8194 | 2020-02-23 17:31:59 +0100 | [diff] [blame] | 73 | ble_advertising: BleAdvertisingDriverFactory, |
| 74 | ble_scanning: BleScanningDriverFactory, |
Woyten | b122620 | 2020-01-13 22:47:48 +0100 | [diff] [blame] | 75 | buttons: ButtonsDriverFactory, |
| 76 | console: ConsoleDriver, |
Alistair Francis | 0f7416e | 2020-09-10 18:54:51 -0700 | [diff] [blame] | 77 | ctap: CtapDriverFactory, |
Woyten | b122620 | 2020-01-13 22:47:48 +0100 | [diff] [blame] | 78 | leds: LedsDriverFactory, |
| 79 | timer: DriverContext { |
torfmaster | 38727a1 | 2019-12-26 18:43:34 +0100 | [diff] [blame] | 80 | active_timer: Cell::new(None), |
| 81 | }, |
Woyten | b122620 | 2020-01-13 22:47:48 +0100 | [diff] [blame] | 82 | gpio: GpioDriverFactory, |
Alistair Francis | be40918 | 2020-03-24 10:54:32 -0700 | [diff] [blame] | 83 | hmac: HmacDriverFactory, |
Alistair Francis | f99feff | 2020-11-04 16:30:51 -0800 | [diff] [blame] | 84 | i2c_ms: I2cMSDriverFactory, |
Alistair Francis | dbf0528 | 2020-11-04 16:31:23 -0800 | [diff] [blame] | 85 | i2c: I2cDriverFactory, |
Woyten | b122620 | 2020-01-13 22:47:48 +0100 | [diff] [blame] | 86 | temperature: TemperatureDriverFactory, |
| 87 | rng: RngDriver, |
torfmaster | 97c5e72 | 2020-01-02 21:30:16 +0100 | [diff] [blame] | 88 | ambient_light_sensor: AmbientLightSensor, |
| 89 | temperature_sensor: TemperatureSensor, |
| 90 | humidity_sensor: HumiditySensor, |
Woyten | b122620 | 2020-01-13 22:47:48 +0100 | [diff] [blame] | 91 | ninedof: NinedofDriver, |
torfmaster | 63b0ad2 | 2019-12-26 18:11:00 +0100 | [diff] [blame] | 92 | }; |
Woyten | ce2bc6a | 2020-01-19 00:33:30 +0100 | [diff] [blame] | 93 | |
| 94 | pub struct DriversAlreadyTakenError; |
| 95 | |
| 96 | impl From<DriversAlreadyTakenError> for TockError { |
| 97 | fn from(_: DriversAlreadyTakenError) -> Self { |
| 98 | TockError::Other(OtherError::DriversAlreadyTaken) |
| 99 | } |
| 100 | } |