imix/test/
i2c_dummy.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

//! A dummy I2C client

use core::cell::Cell;
use core::ptr::addr_of_mut;
use kernel::debug;
use kernel::hil;
use kernel::hil::i2c::{Error, I2CMaster};

// ===========================================
// Scan for I2C Slaves
// ===========================================

struct ScanClient {
    dev_id: Cell<u8>,
    i2c_master: &'static dyn I2CMaster<'static>,
}

impl ScanClient {
    pub fn new(i2c_master: &'static dyn I2CMaster<'static>) -> Self {
        Self {
            dev_id: Cell::new(1),
            i2c_master,
        }
    }
}

impl hil::i2c::I2CHwMasterClient for ScanClient {
    fn command_complete(&self, buffer: &'static mut [u8], status: Result<(), Error>) {
        let mut dev_id = self.dev_id.get();

        if status == Ok(()) {
            debug!("{:#x}", dev_id);
        }

        let dev: &dyn I2CMaster<'static> = self.i2c_master;
        if dev_id < 0x7F {
            dev_id += 1;
            self.dev_id.set(dev_id);
            dev.write(dev_id, buffer, 2).unwrap();
        } else {
            debug!(
                "Done scanning for I2C devices. Buffer len: {}",
                buffer.len()
            );
        }
    }
}

/// This test should be called with I2C2, specifically
pub fn i2c_scan_slaves(i2c_master: &'static dyn I2CMaster<'static>) {
    static mut DATA: [u8; 255] = [0; 255];

    let dev = i2c_master;

    let i2c_client = unsafe { kernel::static_init!(ScanClient, ScanClient::new(dev)) };
    dev.set_master_client(i2c_client);

    dev.enable();

    debug!("Scanning for I2C devices...");
    dev.write(
        i2c_client.dev_id.get(),
        unsafe { &mut *addr_of_mut!(DATA) },
        2,
    )
    .unwrap();
}

// ===========================================
// Test FXOS8700CQ
// ===========================================

#[derive(Copy, Clone)]
enum AccelClientState {
    ReadingWhoami,
    Activating,
    Deactivating,
    ReadingAccelData,
}

struct AccelClient {
    state: Cell<AccelClientState>,
    i2c_master: &'static dyn I2CMaster<'static>,
}

impl AccelClient {
    pub fn new(i2c_master: &'static dyn I2CMaster<'static>) -> Self {
        Self {
            state: Cell::new(AccelClientState::ReadingWhoami),
            i2c_master,
        }
    }
}

impl hil::i2c::I2CHwMasterClient for AccelClient {
    fn command_complete(&self, buffer: &'static mut [u8], status: Result<(), Error>) {
        let dev = self.i2c_master;

        match self.state.get() {
            AccelClientState::ReadingWhoami => {
                debug!("WHOAMI Register 0x{:x} ({:?})", buffer[0], status);
                debug!("Activating Sensor...");
                buffer[0] = 0x2A_u8; // CTRL_REG1
                buffer[1] = 1; // Bit 1 sets `active`
                dev.write(0x1e, buffer, 2).unwrap();
                self.state.set(AccelClientState::Activating);
            }
            AccelClientState::Activating => {
                debug!("Sensor Activated ({:?})", status);
                buffer[0] = 0x01_u8; // X-MSB register
                                     // Reading 6 bytes will increment the register pointer through
                                     // X-MSB, X-LSB, Y-MSB, Y-LSB, Z-MSB, Z-LSB
                dev.write_read(0x1e, buffer, 1, 6).unwrap();
                self.state.set(AccelClientState::ReadingAccelData);
            }
            AccelClientState::ReadingAccelData => {
                let x = (((buffer[0] as u16) << 8) | buffer[1] as u16) as usize;
                let y = (((buffer[2] as u16) << 8) | buffer[3] as u16) as usize;
                let z = (((buffer[4] as u16) << 8) | buffer[5] as u16) as usize;

                let x = ((x >> 2) * 976) / 1000;
                let y = ((y >> 2) * 976) / 1000;
                let z = ((z >> 2) * 976) / 1000;

                debug!(
                    "Accel data ready x: {}, y: {}, z: {} ({:?})",
                    x >> 2,
                    y >> 2,
                    z >> 2,
                    status
                );

                buffer[0] = 0x01_u8; // X-MSB register
                                     // Reading 6 bytes will increment the register pointer through
                                     // X-MSB, X-LSB, Y-MSB, Y-LSB, Z-MSB, Z-LSB
                dev.write_read(0x1e, buffer, 1, 6).unwrap();
                self.state.set(AccelClientState::ReadingAccelData);
            }
            AccelClientState::Deactivating => {
                debug!("Sensor deactivated ({:?})", status);
                debug!("Reading Accel's WHOAMI...");
                buffer[0] = 0x0D_u8; // 0x0D == WHOAMI register
                dev.write_read(0x1e, buffer, 1, 1).unwrap();
                self.state.set(AccelClientState::ReadingWhoami);
            }
        }
    }
}

/// This test should be called with I2C2, specifically
pub fn i2c_accel_test(i2c_master: &'static dyn I2CMaster<'static>) {
    static mut DATA: [u8; 255] = [0; 255];

    let dev = i2c_master;

    let i2c_client = unsafe { kernel::static_init!(AccelClient, AccelClient::new(dev)) };
    dev.set_master_client(i2c_client);
    dev.enable();

    let buf = unsafe { &mut *addr_of_mut!(DATA) };
    debug!("Reading Accel's WHOAMI...");
    buf[0] = 0x0D_u8; // 0x0D == WHOAMI register
    dev.write_read(0x1e, buf, 1, 1).unwrap();
    i2c_client.state.set(AccelClientState::ReadingWhoami);
}

// ===========================================
// Test LI
// ===========================================

#[derive(Copy, Clone)]
enum LiClientState {
    Enabling,
    ReadingLI,
}

struct LiClient {
    state: Cell<LiClientState>,
    i2c_master: &'static dyn I2CMaster<'static>,
}

impl LiClient {
    pub fn new(i2c_master: &'static dyn I2CMaster<'static>) -> Self {
        Self {
            state: Cell::new(LiClientState::Enabling),
            i2c_master,
        }
    }
}

impl hil::i2c::I2CHwMasterClient for LiClient {
    fn command_complete(&self, buffer: &'static mut [u8], status: Result<(), Error>) {
        let dev = self.i2c_master;

        match self.state.get() {
            LiClientState::Enabling => {
                debug!("Reading luminance Registers ({:?})", status);
                buffer[0] = 0x02_u8;
                buffer[0] = 0;
                dev.write_read(0x44, buffer, 1, 2).unwrap();
                self.state.set(LiClientState::ReadingLI);
            }
            LiClientState::ReadingLI => {
                let intensity = ((buffer[1] as usize) << 8) | buffer[0] as usize;
                debug!(
                    "Light Intensity: {}% ({:?})",
                    (intensity * 100) >> 16,
                    status
                );
                buffer[0] = 0x02_u8;
                dev.write_read(0x44, buffer, 1, 2).unwrap();
                self.state.set(LiClientState::ReadingLI);
            }
        }
    }
}

/// This test should be called with I2C2, specifically
pub fn i2c_li_test(i2c_master: &'static dyn I2CMaster<'static>) {
    static mut DATA: [u8; 255] = [0; 255];

    let pin = sam4l::gpio::GPIOPin::new(sam4l::gpio::Pin::PA16);
    pin.enable_output();
    pin.set();

    let dev = i2c_master;

    let i2c_client = unsafe { kernel::static_init!(LiClient, LiClient::new(dev)) };
    dev.set_master_client(i2c_client);
    dev.enable();

    let buf = unsafe { &mut *addr_of_mut!(DATA) };
    debug!("Enabling LI...");
    buf[0] = 0;
    buf[1] = 0b10100000;
    buf[2] = 0b00000000;
    dev.write(0x44, buf, 3).unwrap();
    i2c_client.state.set(LiClientState::Enabling);
}