Skip to content

Instantly share code, notes, and snippets.

@Spindel
Created November 13, 2017 17:29
Show Gist options
  • Save Spindel/5e1707b74d899a92c7f511999c517f4a to your computer and use it in GitHub Desktop.
Save Spindel/5e1707b74d899a92c7f511999c517f4a to your computer and use it in GitHub Desktop.
i2c and rust
use std::io::Write;
use std::os::unix::io::AsRawFd;
use std::fs::OpenOptions;
extern crate libc;
fn main() {
const I2C_SLAVE: u64 = 0x0703;
const DEVICE: u32 = 0x2a;
const I2C_DATA: [u8;2] = [0x2a, 0x2a];
let mut file = OpenOptions::new().read(false).write(true).create(false).open("/dev/i2c-1").expect("File not found?");
unsafe {
let is_i2cslave: i32 = libc::ioctl(file.as_raw_fd(), I2C_SLAVE, DEVICE);
println!("Return value: {}", is_i2cslave);
if is_i2cslave > 0 {
println!("Writing magic numbers...");
file.write_all(&I2C_DATA).expect("Failed to write magic sequence");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment