diff options
author | Daniel Friesel <derf@finalrewind.org> | 2021-11-21 22:33:33 +0100 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2021-11-21 22:33:33 +0100 |
commit | 8d92813827c5979c9298cbdd98b0503c427fd42a (patch) | |
tree | b6c101c47476e841020d7d2454d7325cf89f05ca /src/arch/posix/driver/i2c.cc | |
parent | 560b35559fb810ba3c0a85c24de59735c74e784e (diff) |
POSIX: Add proper I2C driver (via /dev/i2c)
Diffstat (limited to 'src/arch/posix/driver/i2c.cc')
-rw-r--r-- | src/arch/posix/driver/i2c.cc | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/arch/posix/driver/i2c.cc b/src/arch/posix/driver/i2c.cc new file mode 100644 index 0000000..b408320 --- /dev/null +++ b/src/arch/posix/driver/i2c.cc @@ -0,0 +1,60 @@ +/* + * Copyright 2021 Daniel Friesel + * + * SPDX-License-Identifier: BSD-2-Clause + */ +#include "driver/i2c.h" +#include "arch.h" +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <unistd.h> +#include <sys/ioctl.h> +#include <linux/i2c-dev.h> +#include <i2c/smbus.h> + + +signed char I2C::setup() +{ + return 0; +} + +signed char I2C::xmit(unsigned char address, + unsigned char tx_len, unsigned char *tx_buf, + unsigned char rx_len, unsigned char *rx_buf) +{ + struct i2c_msg messages[2]; + struct i2c_rdwr_ioctl_data transaction; + transaction.nmsgs = 0; + transaction.msgs = messages; + + if (tx_len) { + messages[transaction.nmsgs].addr = address; + messages[transaction.nmsgs].flags = 0; + messages[transaction.nmsgs].len = tx_len; + messages[transaction.nmsgs].buf = tx_buf; + transaction.nmsgs++; + } + if (rx_len) { + messages[transaction.nmsgs].addr = address; + messages[transaction.nmsgs].flags = I2C_M_RD; + messages[transaction.nmsgs].len = rx_len; + messages[transaction.nmsgs].buf = rx_buf; + transaction.nmsgs++; + } + + int fh = open(i2cbus, O_RDWR); + + if (fh < 0) { + return -1; + } + + if (ioctl(fh, I2C_RDWR, &transaction) == -1) { + return -1; + } + + close(fh); + return 0; +} + +I2C i2c("/dev/i2c-1"); |