From 3e0ca03d6102b4ee8c887d1fa8b1a8669c8294eb Mon Sep 17 00:00:00 2001 From: xdavidwu Date: Mon, 16 May 2022 14:50:38 +0800 Subject: [PATCH] add libi2c bindings --- i2c/smbus/libi2c.ha | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 i2c/smbus/libi2c.ha diff --git a/i2c/smbus/libi2c.ha b/i2c/smbus/libi2c.ha new file mode 100644 index 0000000..7c55aad --- /dev/null +++ b/i2c/smbus/libi2c.ha @@ -0,0 +1,27 @@ +use io; +use rt; + +export fn read_byte_data(file: io::file, command: u8) (u8 | rt::errno) = { + const res = c_i2c_smbus_read_byte_data(file: int, command); + return if (res < 0) res: rt::errno else res: u8; +}; + +export fn read_word_data(file: io::file, command: u8) (u16 | rt::errno) = { + const res = c_i2c_smbus_read_word_data(file: int, command); + return if (res < 0) res: rt::errno else res: u16; +}; + +export fn write_byte_data(file: io::file, command: u8, value: u8) (void | rt::errno) = { + const res = c_i2c_smbus_write_byte_data(file: int, command, value); + return if (res < 0) res: rt::errno else void; +}; + +export fn write_word_data(file: io::file, command: u8, value: u16) (void | rt::errno) = { + const res = c_i2c_smbus_write_word_data(file: int, command, value); + return if (res < 0) res: rt::errno else void; +}; + +@symbol("i2c_smbus_read_byte_data") fn c_i2c_smbus_read_byte_data(file: int, command: u8) i32; +@symbol("i2c_smbus_read_word_data") fn c_i2c_smbus_read_word_data(file: int, command: u8) i32; +@symbol("i2c_smbus_write_byte_data") fn c_i2c_smbus_write_byte_data(file: int, command: u8, value: u8) i32; +@symbol("i2c_smbus_write_word_data") fn c_i2c_smbus_write_word_data(file: int, command: u8, value: u16) i32; -- 2.45.2