components/
isl29035.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

//! Components for the ISL29035 sensor.
//!
//! This provides two Components, Isl29035Component, which provides
//! direct access to the ISL29035 within the kernel, and
//! AmbientLightComponent, which provides the ambient light system
//! call interface to the ISL29035. Note that only one of these
//! Components should be instantiated, as AmbientLightComponent itself
//! creates an Isl29035Component, which depends on a static buffer: if you
//! allocate both, then the two instances of Isl29035Component will conflict
//! on the buffer.
//!
//! Usage
//! -----
//! ```rust
//! let isl29035 = Isl29035Component::new(mux_i2c, mux_alarm)
//!     .finalize(components::isl29035_component_static!(sam4l::ast::Ast));
//! let ambient_light =
//!     AmbientLightComponent::new(board_kernel, capsules_extra::ambient_light::DRIVER_NUM, isl29035)
//!         .finalize(components::ambient_light_component_static!());
//! ```

// Author: Philip Levis <pal@cs.stanford.edu>
// Last modified: 6/20/2018

use capsules_core::virtualizers::virtual_alarm::{MuxAlarm, VirtualMuxAlarm};
use capsules_core::virtualizers::virtual_i2c::{I2CDevice, MuxI2C};
use capsules_extra::ambient_light::AmbientLight;
use capsules_extra::isl29035::Isl29035;
use core::mem::MaybeUninit;
use kernel::capabilities;
use kernel::component::Component;
use kernel::create_capability;
use kernel::hil;
use kernel::hil::i2c;
use kernel::hil::time::{self, Alarm};

// Setup static space for the objects.
#[macro_export]
macro_rules! isl29035_component_static {
    ($A:ty, $I:ty $(,)?) => {{
        let alarm = kernel::static_buf!(
            capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm<'static, $A>
        );
        let i2c_device =
            kernel::static_buf!(capsules_core::virtualizers::virtual_i2c::I2CDevice<'static, $I>);
        let i2c_buffer = kernel::static_buf!([u8; capsules_extra::isl29035::BUF_LEN]);
        let isl29035 = kernel::static_buf!(
            capsules_extra::isl29035::Isl29035<
                'static,
                capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm<'static, $A>,
            >
        );

        (alarm, i2c_device, i2c_buffer, isl29035)
    };};
}

#[macro_export]
macro_rules! ambient_light_component_static {
    () => {{
        kernel::static_buf!(capsules_extra::ambient_light::AmbientLight<'static>)
    };};
}

pub struct Isl29035Component<
    A: 'static + time::Alarm<'static>,
    I: 'static + i2c::I2CMaster<'static>,
> {
    i2c_mux: &'static MuxI2C<'static, I>,
    alarm_mux: &'static MuxAlarm<'static, A>,
}

impl<A: 'static + time::Alarm<'static>, I: 'static + i2c::I2CMaster<'static>>
    Isl29035Component<A, I>
{
    pub fn new(i2c: &'static MuxI2C<'static, I>, alarm: &'static MuxAlarm<'static, A>) -> Self {
        Isl29035Component {
            i2c_mux: i2c,
            alarm_mux: alarm,
        }
    }
}

impl<A: 'static + time::Alarm<'static>, I: 'static + i2c::I2CMaster<'static>> Component
    for Isl29035Component<A, I>
{
    type StaticInput = (
        &'static mut MaybeUninit<VirtualMuxAlarm<'static, A>>,
        &'static mut MaybeUninit<I2CDevice<'static, I>>,
        &'static mut MaybeUninit<[u8; capsules_extra::isl29035::BUF_LEN]>,
        &'static mut MaybeUninit<Isl29035<'static, VirtualMuxAlarm<'static, A>>>,
    );
    type Output = &'static Isl29035<'static, VirtualMuxAlarm<'static, A>>;

    fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
        let isl29035_i2c = static_buffer.1.write(I2CDevice::new(self.i2c_mux, 0x44));
        let isl29035_i2c_buffer = static_buffer
            .2
            .write([0; capsules_extra::isl29035::BUF_LEN]);
        let isl29035_virtual_alarm = static_buffer.0.write(VirtualMuxAlarm::new(self.alarm_mux));
        isl29035_virtual_alarm.setup();

        let isl29035 = static_buffer.3.write(Isl29035::new(
            isl29035_i2c,
            isl29035_virtual_alarm,
            isl29035_i2c_buffer,
        ));
        isl29035_i2c.set_client(isl29035);
        isl29035_virtual_alarm.set_alarm_client(isl29035);
        isl29035
    }
}

pub struct AmbientLightComponent<L: 'static + hil::sensors::AmbientLight<'static>> {
    board_kernel: &'static kernel::Kernel,
    driver_num: usize,
    light_sensor: &'static L,
}

impl<L: 'static + hil::sensors::AmbientLight<'static>> AmbientLightComponent<L> {
    pub fn new(
        board_kernel: &'static kernel::Kernel,
        driver_num: usize,
        light_sensor: &'static L,
    ) -> Self {
        AmbientLightComponent {
            board_kernel,
            driver_num,
            light_sensor,
        }
    }
}

impl<L: 'static + hil::sensors::AmbientLight<'static>> Component for AmbientLightComponent<L> {
    type StaticInput = &'static mut MaybeUninit<AmbientLight<'static>>;
    type Output = &'static AmbientLight<'static>;

    fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
        let grant_cap = create_capability!(capabilities::MemoryAllocationCapability);

        let ambient_light = static_buffer.write(AmbientLight::new(
            self.light_sensor,
            self.board_kernel.create_grant(self.driver_num, &grant_cap),
        ));
        hil::sensors::AmbientLight::set_client(self.light_sensor, ambient_light);
        ambient_light
    }
}