8000 Component: Update bmX280 components by bradjc · Pull Request #3232 · tock/tock · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Component: Update bmX280 components #3232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 26 additions & 19 deletions boards/components/src/bme280.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,42 @@
//! -----
//! ```rust
//! let bme280 =
//! Bme280Component::new(mux_i2c, 0x77).finalize(components::bme280_component_helper!());
//! Bme280Component::new(mux_i2c, 0x77).finalize(components::bme280_component_static!());
//! let temperature = components::temperature::TemperatureComponent::new(
//! board_kernel,
//! capsules::temperature::DRIVER_NUM,
//! bme280,
//! )
//! .finalize(());
//! .finalize(components::temperature_component_static!());
//! let humidity = components::humidity::HumidityComponent::new(
//! board_kernel,
//! capsules::humidity::DRIVER_NUM,
//! bme280,
//! )
//! .finalize(());
//! .finalize(components::humidity_component_static!());
//! ```

use capsules::bme280::Bme280;
use capsules::virtual_i2c::{I2CDevice, MuxI2C};
use core::mem::MaybeUninit;
use kernel::component::Component;
use kernel::{static_init, static_init_half};

// Setup static space for the objects.
#[macro_export]
macro_rules! bme280_component_helper {
macro_rules! bme280_component_static {
() => {{
use capsules::bme280::Bme280;
use core::mem::MaybeUninit;
static mut BUF1: MaybeUninit<Bme280<'static>> = MaybeUninit::uninit();
&mut BUF1
let i2c_device = kernel::static_buf!(capsules::virtual_i2c::I2CDevice<'static>);
let i2c_buffer = kernel::static_buf!([u8; 26]);
let bme280 = kernel::static_buf!(capsules::bme280::Bme280<'static>);

(i2c_device, i2c_buffer, bme280)
};};
}

#[macro_export]
macro_rules! temperature_component_static {
() => {{
kernel::static_buf!(capsules::temperature::TemperatureSensor<'static>)
};};
}

Expand All @@ -50,19 +57,19 @@ impl Bme280Component {
}
}

static mut I2C_BUF: [u8; 26] = [0; 26];

impl Component for Bme280Component {
type StaticInput = &'static mut MaybeUninit<Bme280<'static>>;
type StaticInput = (
&'static mut MaybeUninit<I2CDevice<'static>>,
&'static mut MaybeUninit<[u8; 26]>,
&'static mut MaybeUninit<Bme280<'static>>,
);
type Output = &'static Bme280<'static>;

unsafe fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
let bme280_i2c = static_init!(I2CDevice, I2CDevice::new(self.i2c_mux, self.i2c_address));
let bme280 = static_init_half!(
static_buffer,
Bme280<'static>,
Bme280::new(bme280_i2c, &mut I2C_BUF)
);
unsafe fn finalize(self, s: Self::StaticInput) -> Self::Output {
let bme280_i2c = s.0.write(I2CDevice::new(self.i2c_mux, self.i2c_address));
let i2c_buffer = s.1.write([0; 26]);

let bme280 = s.2.write(Bme280::new(bme280_i2c, i2c_buffer));

bme280_i2c.set_client(bme280);
bme280.startup();
Expand Down
70 changes: 27 additions & 43 deletions boards/components/src/bmp280.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,92 +10,76 @@
//! With the default i2c address
//! ```rust
//! let bmp280 = components::bmp280::Bmp280Component::new(sensors_i2c_bus, mux_alarm).finalize(
//! components::bmp280_component_helper!(nrf52::rtc::Rtc<'static>),
//! components::bmp280_component_static!(nrf52::rtc::Rtc<'static>),
//! );
//! bmp280.begin_reset();
//! ```
//!
//! With a specified i2c address
//! ```rust
//! let bmp280 = components::bmp280::Bmp280Component::new(sensors_i2c_bus, mux_alarm).finalize(
//! components::bmp280_component_helper!(nrf52::rtc::Rtc<'static>, capsules::bmp280::BASE_ADDR),
//! components::bmp280_component_static!(nrf52::rtc::Rtc<'static>, capsules::bmp280::BASE_ADDR),
//! );
//! bmp280.begin_reset();
//! ```

use crate::i2c::I2CComponent;
use crate::i2c_component_helper;
use capsules::bmp280::Bmp280;
use capsules::virtual_alarm::{MuxAlarm, VirtualMuxAlarm};
use capsules::virtual_i2c::MuxI2C;
use capsules::virtual_i2c::{I2CDevice, MuxI2C};
use core::mem::MaybeUninit;
use kernel::component::Component;
use kernel::hil::time::Alarm;

use kernel::static_init_half;

/// Setup static space for the driver and its requirements.
#[macro_export]
macro_rules! bmp280_component_helper {
($A:ty) => {{
use capsules::bmp280;
$crate::bmp280_component_helper!($A, bmp280::BASE_ADDR)
}};

// used for specifically stating the i2c address
// as some boards (like nrf52) require a shift
($A:ty, $address: expr) => {{
use capsules::bmp280::Bmp280;
use core::mem::MaybeUninit;

static mut BUFFER: [u8; bmp280::BUFFER_SIZE] = [0; bmp280::BUFFER_SIZE];
macro_rules! bmp280_component_static {
($A:ty $(,)?) => {{
let i2c_device = kernel::static_buf!(capsules::virtual_i2c::I2CDevice<'static>);
let alarm = kernel::static_buf!(capsules::virtual_alarm::VirtualMuxAlarm<'static, $A>);
let buffer = kernel::static_buf!([u8; capsules::bmp280::BUFFER_SIZE]);
let bmp280 =
kernel::static_buf!(capsules::bmp280::Bmp280<'static, VirtualMuxAlarm<'static, $A>>);

static mut BMP280: MaybeUninit<Bmp280<'static, VirtualMuxAlarm<'static, $A>>> =
MaybeUninit::uninit();
static mut BMP280_ALARM: MaybeUninit<VirtualMuxAlarm<'static, $A>> = MaybeUninit::uninit();
(&mut BMP280_ALARM, &mut BUFFER, &mut BMP280, $address)
}};
(i2c_device, alarm, buffer, bmp280)
};};
}

pub struct Bmp280Component<A: 'static + Alarm<'static>> {
i2c_mux: &'static MuxI2C<'static>,
i2c_address: u8,
alarm_mux: &'static MuxAlarm<'static, A>,
}

impl<A: 'static + Alarm<'static>> Bmp280Component<A> {
pub fn new(
i2c_mux: &'static MuxI2C<'static>,
i2c_address: u8,
alarm_mux: &'static MuxAlarm<'static, A>,
) -> Bmp280Component<A> {
Bmp280Component { i2c_mux, alarm_mux }
Bmp280Component {
i2c_mux,
i2c_address,
alarm_mux,
}
}
}

impl<A: 'static + Alarm<'static>> Component for Bmp280Component<A> {
type StaticInput = (
&'static mut MaybeUninit<I2CDevice<'static>>,
&'static mut MaybeUninit<VirtualMuxAlarm<'static, A>>,
&'static mut [u8],
&'static mut MaybeUninit<[u8; capsules::bmp280::BUFFER_SIZE]>,
&'static mut MaybeUninit<Bmp280<'static, VirtualMuxAlarm<'static, A>>>,
u8,
);
type Output = &'static Bmp280<'static, VirtualMuxAlarm<'static, A>>;

unsafe fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
let bmp280_i2c =
I2CComponent::new(self.i2c_mux, static_buffer.3).finalize(i2c_component_helper!());

let bmp280_alarm = static_init_half!(
static_buffer.0,
VirtualMuxAlarm<'static, A>,
VirtualMuxAlarm::new(self.alarm_mux)
);
unsafe fn finalize(self, s: Self::StaticInput) -> Self::Output {
let bmp280_i2c = s.0.write(I2CDevice::new(self.i2c_mux, self.i2c_address));
let bmp280_alarm = s.1.write(VirtualMuxAlarm::new(self.alarm_mux));
bmp280_alarm.setup();

let bmp280 = static_init_half!(
static_buffer.2,
Bmp280<'static, VirtualMuxAlarm<'static, A>>,
Bmp280::new(bmp280_i2c, static_buffer.1, bmp280_alarm)
);
let buffer = s.2.write([0; capsules::bmp280::BUFFER_SIZE]);

let bmp280 = s.3.write(Bmp280::new(bmp280_i2c, buffer, bmp280_alarm));
bmp280_i2c.set_client(bmp280);
bmp280_alarm.set_alarm_client(bmp280);

Expand Down
2 changes: 1 addition & 1 deletion boards/redboard_artemis_nano/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ unsafe fn setup() -> (
.finalize(components::i2c_mux_component_helper!());

let bme280 =
Bme280Component::new(mux_i2c, 0x77).finalize(components::bme280_component_helper!());
Bme280Component::new(mux_i2c, 0x77).finalize(components::bme280_component_static!());
let temperature = components::temperature::TemperatureComponent::new(
board_kernel,
capsules::temperature::DRIVER_NUM,
Expand Down
12 changes: 8 additions & 4 deletions boards/sma_q3/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

use capsules::virtual_aes_ccm::MuxAES128CCM;
use capsules::virtual_alarm::VirtualMuxAlarm;
use components::bmp280::Bmp280Component;
use components::bmp280_component_helper;
use kernel::component::Component;
use kernel::dynamic_deferred_call::{DynamicDeferredCall, DynamicDeferredCallClientState};
use kernel::hil::i2c::I2CMaster;
Expand Down Expand Up @@ -364,8 +362,14 @@ pub unsafe fn main() {
);
base_peripherals.twi1.set_master_client(sensors_i2c_bus);

let bmp280 = Bmp280Component::new(sensors_i2c_bus, mux_alarm)
.finalize(bmp280_component_helper!(nrf52840::rtc::Rtc<'static>));
let bmp280 = components::bmp280::Bmp280Component::new(
sensors_i2c_bus,
capsules::bmp280::BASE_ADDR,
mux_alarm,
)
.finalize(components::bmp280_component_static!(
nrf52840::rtc::Rtc<'static>
));

let temperature = components::temperature::TemperatureComponent::new(
board_kernel,
Expand Down
0