forked from dysonltd/tmag5273
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
93 lines (84 loc) · 2.86 KB
/
lib.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
#[cfg(not(feature = "rpi"))]
use ftdi::Device;
use ftdi_embedded_hal::I2c as FtdiI2c;
use ftdi_embedded_hal::{self as hal};
use std::error::Error;
#[cfg(feature = "rpi")]
use rppal::i2c as PiI2c;
#[cfg(feature = "rpi")]
/// Set up the I2C bus for the Raspberry Pi
pub fn setup_i2c() -> Result<PiI2c::I2c, Box<dyn Error>> {
let mut i2c = PiI2c::I2c::new()?;
Ok(i2c)
}
#[cfg(feature = "rpi")]
/// Sets up the I2C bus and the GPIO for the Raspberry Pi The GPIO pin used is the BCM GPIO 4 pin
pub fn setup_i2c_and_gpio() -> Result<(PiI2c::I2c, rppal::gpio::InputPin), Box<dyn Error>> {
let mut i2c = PiI2c::I2c::new()?;
let pin = rppal::gpio::Gpio::new()?.get(4)?.into_input();
Ok((i2c, pin))
}
#[cfg(not(feature = "rpi"))]
/// Set up the I2C bus for the FTDI Interface
pub fn setup_i2c() -> Result<FtdiI2c<Device>, Box<dyn Error>> {
const BAUDRATE: u32 = 400_000;
// Change these for your device
const DEVICE_VID: u16 = 0x0403;
const DEVICE_PID: u16 = 0x6014;
let device = ftdi::find_by_vid_pid(DEVICE_VID, DEVICE_PID)
.interface(ftdi::Interface::A)
.open()?;
// Next initialise the HAL with the device and the Baudrate
let hal = match hal::FtHal::init_freq(device, BAUDRATE) {
Ok(hal) => hal,
Err(err) => {
eprintln!("Failed to initialise HAL: {}", err);
return Err(Box::new(err));
}
};
// Finally initialise the I2C with the HAL
let i2c = match hal.i2c() {
Ok(i2c) => i2c,
Err(err) => {
eprintln!("Failed to initialise I2C: {}", err);
return Err(Box::new(err));
}
};
Ok(i2c)
}
#[cfg(not(feature = "rpi"))]
/// Sets up the I2C bus and the GPIO for the FTDI Interface The GPIO pin used is the CI0 pin
pub fn setup_i2c_and_gpio() -> Result<(FtdiI2c<Device>, hal::InputPin<Device>), Box<dyn Error>> {
const BAUDRATE: u32 = 400_000;
// Change these for your device
const DEVICE_VID: u16 = 0x0403;
const DEVICE_PID: u16 = 0x6014;
let device = ftdi::find_by_vid_pid(DEVICE_VID, DEVICE_PID)
.interface(ftdi::Interface::A)
.open()?;
// Next initialise the HAL with the device and the Baudrate
let hal = match hal::FtHal::init_freq(device, BAUDRATE) {
Ok(hal) => hal,
Err(err) => {
eprintln!("Failed to initialise HAL: {}", err);
return Err(Box::new(err));
}
};
let pin = match hal.ci0() {
// The FAKE Interrupt pin
Ok(pin) => pin,
Err(err) => {
eprintln!("Failed to initialise GPIO: {}", err);
return Err(Box::new(err));
}
};
// Finally initialise the I2C with the HAL
let i2c = match hal.i2c() {
Ok(i2c) => i2c,
Err(err) => {
eprintln!("Failed to initialise I2C: {}", err);
return Err(Box::new(err));
}
};
Ok((i2c, pin))
}