tof_control/device/
cy8c9560a.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#![allow(unused)]
use crate::constant::*;

use i2c_linux_sys::i2c_smbus_read_i2c_block_data;
use i2cdev::core::*;
use i2cdev::linux::{LinuxI2CDevice, LinuxI2CError};

// Register
const INPUT_PORT_0: u16 = 0x00;
const INPUT_PORT_1: u16 = 0x01;
const INPUT_PORT_2: u16 = 0x02;
const INPUT_PORT_3: u16 = 0x03;
const INPUT_PORT_4: u16 = 0x04;
const INPUT_PORT_5: u16 = 0x05;
const INPUT_PORT_6: u16 = 0x06;
const INPUT_PORT_7: u16 = 0x07;
const OUTPUT_PORT_0: u16 = 0x08;
const OUTPUT_PORT_1: u16 = 0x09;
const OUTPUT_PORT_2: u16 = 0x0A;
const OUTPUT_PORT_3: u16 = 0x0B;
const OUTPUT_PORT_4: u16 = 0x0C;
const OUTPUT_PORT_5: u16 = 0x0D;
const OUTPUT_PORT_6: u16 = 0x0E;
const OUTPUT_PORT_7: u16 = 0x0F;
const INTERRUPT_STATUS_PORT_0: u16 = 0x10;
const INTERRUPT_STATUS_PORT_1: u16 = 0x11;
const INTERRUPT_STATUS_PORT_2: u16 = 0x12;
const INTERRUPT_STATUS_PORT_3: u16 = 0x13;
const INTERRUPT_STATUS_PORT_4: u16 = 0x14;
const INTERRUPT_STATUS_PORT_5: u16 = 0x15;
const INTERRUPT_STATUS_PORT_6: u16 = 0x16;
const INTERRUPT_STATUS_PORT_7: u16 = 0x17;
const PORT_SELECT: u16 = 0x18;
const INTERRUPT_MASK: u16 = 0x19;
const PIN_DIRECTION: u16 = 0x1C;
const DRIVE_MODE_PULL_UP: u16 = 0x1D;
const DRIVE_MODE_PULL_DOWN: u16 = 0x1E;
const DRIVE_MODE_OPEN_DRAIN_HIGH: u16 = 0x1F;
const DRIVE_MODE_OPEN_DRAIN_LOW: u16 = 0x20;
const DRIVE_MODE_STRONG: u16 = 0x21;
const DRIVE_MODE_SLOW_STRONG: u16 = 0x22;
const DRIVE_MODE_HIGH_Z: u16 = 0x23;
const ENABLE_REGISTER: u16 = 0x2D;
const DEVICE_INFO: u16 = 0x2E;
const COMMAND: u16 = 0x30;

#[derive(Copy, Clone)]
pub struct CY8C9560A {
    bus: u8,
    address: u16,
}

impl CY8C9560A {
    pub fn new(bus: u8, address: u16) -> Self {
        Self { bus, address }
    }
    pub fn read_device_info(&self) -> Result<(u8, u8), LinuxI2CError> {
        let mut dev = LinuxI2CDevice::new(&format!("/dev/i2c-{}", self.bus), self.address)?;
        let device_info = dev.smbus_read_byte_data(DEVICE_INFO as u8)?;
        let device_family = (device_info & 0xF0) >> 4;
        let device_setting = device_info & 0x01;

        Ok((device_family, device_setting))
    }
    pub fn read_enable_register(&self) -> Result<u8, LinuxI2CError> {
        let mut dev = LinuxI2CDevice::new(&format!("/dev/i2c-{}", self.bus), self.address)?;
        let enable_register = dev.smbus_read_byte_data(ENABLE_REGISTER as u8)?;

        Ok(enable_register)
    }
    pub fn enable_eeprom(&self) -> Result<(), LinuxI2CError> {
        let mut dev = LinuxI2CDevice::new(&format!("/dev/i2c-{}", self.bus), self.address)?;
        let eeprom_enable_sequence: [u8; 4] = [0x43, 0x4D, 0x53, 0x02];
        dev.smbus_write_i2c_block_data(ENABLE_REGISTER as u8, &eeprom_enable_sequence)?;

        Ok(())
    }
    pub fn store_config_eeprom_por(&self) -> Result<(), LinuxI2CError> {
        let mut dev = LinuxI2CDevice::new(&format!("/dev/i2c-{}", self.bus), self.address)?;
        dev.smbus_write_byte_data(COMMAND as u8, 0x01)?;

        Ok(())
    }
    pub fn reset_config_eeprom_por(&self) -> Result<(), LinuxI2CError> {
        let mut dev = LinuxI2CDevice::new(&format!("/dev/i2c-{}", self.bus), self.address)?;
        dev.smbus_write_byte_data(COMMAND as u8, 0x02)?;

        Ok(())
    }
    pub fn read_port_status(&self, port: u8) -> Result<u8, LinuxI2CError> {
        let mut dev = LinuxI2CDevice::new(&format!("/dev/i2c-{}", self.bus), self.address)?;
        let port_status = dev.smbus_read_byte_data(match port {
            0 => INPUT_PORT_0 as u8,
            1 => INPUT_PORT_1 as u8,
            2 => INPUT_PORT_2 as u8,
            3 => INPUT_PORT_3 as u8,
            4 => INPUT_PORT_4 as u8,
            5 => INPUT_PORT_5 as u8,
            6 => INPUT_PORT_6 as u8,
            7 => INPUT_PORT_7 as u8,
            _ => 0xFF,
        })?;

        Ok(port_status)
    }
    pub fn initialize_all_outputs(&self) -> Result<(), LinuxI2CError> {
        for i in 0..8 {
            self.set_output_port(i, 0xFF)?;
        }

        Ok(())
    }
    pub fn set_output_port(&self, port: u8, value: u8) -> Result<(), LinuxI2CError> {
        let mut dev = LinuxI2CDevice::new(&format!("/dev/i2c-{}", self.bus), self.address)?;
        dev.smbus_write_byte_data(
            match port {
                0 => OUTPUT_PORT_0 as u8,
                1 => OUTPUT_PORT_1 as u8,
                2 => OUTPUT_PORT_2 as u8,
                3 => OUTPUT_PORT_3 as u8,
                4 => OUTPUT_PORT_4 as u8,
                5 => OUTPUT_PORT_5 as u8,
                6 => OUTPUT_PORT_6 as u8,
                7 => OUTPUT_PORT_7 as u8,
                _ => 0xFF,
            },
            value,
        )?;

        Ok(())
    }
    pub fn select_port(&self, port: u8) -> Result<(), LinuxI2CError> {
        let mut dev = LinuxI2CDevice::new(&format!("/dev/i2c-{}", self.bus), self.address)?;
        dev.smbus_write_byte_data(PORT_SELECT as u8, port)?;

        Ok(())
    }
    pub fn set_interrupt_mask_port(&self, value: u8) -> Result<(), LinuxI2CError> {
        let mut dev = LinuxI2CDevice::new(&format!("/dev/i2c-{}", self.bus), self.address)?;
        dev.smbus_write_byte_data(INTERRUPT_MASK as u8, value)?;

        Ok(())
    }
    pub fn set_pin_direction(&self, value: u8) -> Result<(), LinuxI2CError> {
        let mut dev = LinuxI2CDevice::new(&format!("/dev/i2c-{}", self.bus), self.address)?;
        dev.smbus_write_byte_data(PIN_DIRECTION as u8, value)?;

        Ok(())
    }
    pub fn set_drive_mode(&self, mode: u8) -> Result<(), LinuxI2CError> {
        let mut dev = LinuxI2CDevice::new(&format!("/dev/i2c-{}", self.bus), self.address)?;
        dev.smbus_write_byte_data(
            match mode {
                0 => DRIVE_MODE_PULL_UP as u8,
                1 => DRIVE_MODE_PULL_DOWN as u8,
                2 => DRIVE_MODE_OPEN_DRAIN_HIGH as u8,
                3 => DRIVE_MODE_OPEN_DRAIN_LOW as u8,
                4 => DRIVE_MODE_STRONG as u8,
                5 => DRIVE_MODE_SLOW_STRONG as u8,
                6 => DRIVE_MODE_HIGH_Z as u8,
                _ => 0xFF,
            },
            0x01,
        )?;

        Ok(())
    }
    // Set RF Switch (HMC849)
    pub fn set_rf_switch(&self, channel: u8, mode: u8) -> Result<(), LinuxI2CError> {
        match channel {
            0 => match mode {
                0 => {
                    let value: u8 = (0x3F & !0x30) | 0x20;
                    self.set_output_port(7, value)?;
                }
                1 => {
                    let value: u8 = (0x3F & !0x30) | 0x10;
                    self.set_output_port(7, value)?;
                }
                2 => {
                    let value: u8 = (0x3F & !0x30) | 0x00;
                    self.set_output_port(7, value)?;
                }
                _ => {
                    self.set_output_port(7, 0x3F)?;
                }
            },
            1 => match mode {
                0 => {
                    let value: u8 = (0x3F | 0x0C) & 0x0C;
                    self.set_output_port(7, value)?;
                }
                1 => {
                    let value: u8 = (0x3F & !0x0C) | 0x04;
                    self.set_output_port(7, value)?;
                }
                2 => {
                    let value: u8 = (0x3F & !0x0C) | 0x00;
                    self.set_output_port(7, value)?;
                }
                _ => {
                    self.set_output_port(7, 0x3F)?;
                }
            },
            2 => match mode {
                0 => {
                    let value: u8 = (0x3F | 0x03) & 0x03;
                    self.set_output_port(7, value)?;
                }
                1 => {
                    let value: u8 = (0x3F & !0x03) | 0x01;
                    self.set_output_port(7, value)?;
                }
                2 => {
                    let value: u8 = (0x3F & !0x03) | 0x00;
                    self.set_output_port(7, value)?;
                }
                _ => {
                    self.set_output_port(7, 0x3F)?;
                }
            },
            3 => match mode {
                0 => {
                    let value: u8 = (0x03 | 0x03) & 0x03;
                    self.set_output_port(2, value)?;
                }
                1 => {
                    let value: u8 = (0x03 & !0x03) | 0x01;
                    self.set_output_port(2, value)?;
                }
                2 => {
                    let value: u8 = (0x03 & !0x03) | 0x00;
                    self.set_output_port(2, value)?;
                }
                _ => {
                    self.set_output_port(2, 0x03)?;
                }
            },
            4 => match mode {
                0 => {
                    let value: u8 = (0x33 | 0x03) & 0x03;
                    self.set_output_port(5, value)?;
                }
                1 => {
                    let value: u8 = (0x33 & !0x03) | 0x02;
                    self.set_output_port(5, value)?;
                }
                2 => {
                    let value: u8 = (0x33 & !0x03) | 0x00;
                    self.set_output_port(5, value)?;
                }
                _ => {
                    self.set_output_port(5, 0x33)?;
                }
            },
            5 => match mode {
                0 => {
                    let value: u8 = (0x33 | 0x30) & 0x30;
                    self.set_output_port(5, value)?;
                }
                1 => {
                    let value: u8 = (0x33 & !0x30) | 0x10;
                    self.set_output_port(5, value)?;
                }
                2 => {
                    let value: u8 = (0x33 & !0x30) | 0x00;
                    self.set_output_port(5, value)?;
                }
                _ => {
                    self.set_output_port(5, 0x33)?;
                }
            },
            6 => match mode {
                0 => {
                    let value: u8 = (0xFC | 0xC0) & 0xC0;
                    self.set_output_port(4, value)?;
                }
                1 => {
                    let value: u8 = (0xFC & !0xC0) | 0x80;
                    self.set_output_port(4, value)?;
                }
                2 => {
                    let value: u8 = (0xFC & !0xC0) | 0x00;
                    self.set_output_port(4, value)?;
                }
                _ => {
                    self.set_output_port(4, 0xFC)?;
                }
            },
            7 => match mode {
                0 => {
                    let value: u8 = (0xFC | 0x30) & 0x30;
                    self.set_output_port(4, value)?;
                }
                1 => {
                    let value: u8 = (0xFC & !0x30) | 0x20;
                    self.set_output_port(4, value)?;
                }
                2 => {
                    let value: u8 = (0xFC & !0x30) | 0x00;
                    self.set_output_port(4, value)?;
                }
                _ => {
                    self.set_output_port(4, 0xFC)?;
                }
            },
            8 => match mode {
                0 => {
                    let value: u8 = (0xFC | 0x0C) & 0x0C;
                    self.set_output_port(4, value)?;
                }
                1 => {
                    let value: u8 = (0xFC & !0x0C) | 0x08;
                    self.set_output_port(4, value)?;
                }
                2 => {
                    let value: u8 = (0xFC & !0x0C) | 0x00;
                    self.set_output_port(4, value)?;
                }
                _ => {
                    self.set_output_port(4, 0xFC)?;
                }
            },
            _ => {}
        };

        Ok(())
    }
    // neet to check!
    pub fn reset_clock_synthesizer(&self) -> Result<(), LinuxI2CError> {
        let mut value = (0x03 & !0x02) | 0 << 1;
        value = (value & !0x01) | 1;

        self.set_output_port(3, value)?;

        Ok(())
    }
    // neet to check!
    pub fn enable_tcal_clock(&self) -> Result<(), LinuxI2CError> {
        let mut value: u16 = (0x3F | 0x80) | (0x01 << 8);
        self.set_output_port(7, value as u8)?;

        value = (0x3F | 0x40) | (0x01 << 7);
        self.set_output_port(7, value as u8)?;

        Ok(())
    }
    pub fn disable_tcal_clock(&self) -> Result<(), LinuxI2CError> {
        let mut value: u16 = (0x3F | 0x80);
        self.set_output_port(7, value as u8)?;

        value = (0x3F | 0x40);
        self.set_output_port(7, value as u8)?;

        Ok(())
    }
}