33 #include <sys/ioctl.h>
34 #include <linux/i2c.h>
35 #include <linux/i2c-dev.h>
41 #ifndef I2C_THREAD_PRIO
42 #define I2C_THREAD_PRIO 10
62 if (pthread_create(&tid, NULL,
i2c_thread, (
void *)p) != 0) {
63 fprintf(stderr,
"i2c_arch_init: Could not create I2C thread.\n");
67 pthread_setname_np(tid,
"i2c");
91 pthread_mutex_lock(mutex);
97 pthread_mutex_unlock(mutex);
108 pthread_cond_signal(condition);
109 pthread_mutex_unlock(mutex);
117 #pragma GCC diagnostic push
118 #pragma GCC diagnostic ignored "-Wcast-qual"
122 struct i2c_rdwr_ioctl_data trx_data = {
135 pthread_mutex_lock(mutex);
137 pthread_cond_wait(condition, mutex);
142 pthread_mutex_unlock(mutex);
149 ioctl(fd,
I2C_SLAVE, t->slave_addr >> 1);
150 if (write(fd, (
uint8_t *)t->buf, t->len_w) < 0) {
152 pthread_mutex_lock(mutex);
154 pthread_mutex_unlock(mutex);
163 ioctl(fd,
I2C_SLAVE, t->slave_addr >> 1);
164 if (read(fd, (
uint8_t *)t->buf, t->len_r) < 0) {
166 pthread_mutex_lock(mutex);
168 pthread_mutex_unlock(mutex);
176 trx_msgs[0].
addr = t->slave_addr >> 1;
177 trx_msgs[0].
flags = 0;
178 trx_msgs[0].
len = t->len_w;
179 trx_msgs[0].
buf = (
void *) t->buf;
180 trx_msgs[1].
addr = t->slave_addr >> 1;
182 trx_msgs[1].
len = t->len_r;
183 trx_msgs[1].
buf = (
void *) t->buf;
184 if (ioctl(fd,
I2C_RDWR, &trx_data) < 0) {
186 pthread_mutex_lock(mutex);
188 pthread_mutex_unlock(mutex);
199 pthread_mutex_lock(mutex);
201 pthread_mutex_unlock(mutex);
205 #pragma GCC diagnostic pop
211 void i2c0_hw_init(
void)
217 i2c0.reg_addr = (
void *)open(
"/dev/i2c-0", O_RDWR);
218 i2c0.errors = &i2c0_errors;
223 pthread_mutex_init(&i2c0_thread.mutex, NULL);
224 pthread_cond_init(&i2c0_thread.condition, NULL);
225 i2c0.init_struct = (
void *)(&i2c0_thread);
235 void i2c1_hw_init(
void)
241 i2c1.reg_addr = (
void *)open(
"/dev/i2c-1", O_RDWR);
242 i2c1.errors = &i2c1_errors;
247 pthread_mutex_init(&i2c1_thread.mutex, NULL);
248 pthread_cond_init(&i2c1_thread.condition, NULL);
249 i2c1.init_struct = (
void *)(&i2c1_thread);
259 void i2c2_hw_init(
void)
265 i2c2.reg_addr = (
void *)open(
"/dev/i2c-2", O_RDWR);
266 i2c2.errors = &i2c2_errors;
271 pthread_mutex_init(&i2c2_thread.mutex, NULL);
272 pthread_cond_init(&i2c2_thread.condition, NULL);
273 i2c2.init_struct = (
void *)(&i2c2_thread);
283 void i2c3_hw_init(
void)
289 i2c3.reg_addr = (
void *)open(
"/dev/i2c-3", O_RDWR);
290 i2c3.errors = &i2c3_errors;
295 pthread_mutex_init(&i2c3_thread.mutex, NULL);
296 pthread_cond_init(&i2c3_thread.condition, NULL);
297 i2c3.init_struct = (
void *)(&i2c3_thread);
volatile uint16_t arb_lost_cnt
#define I2C_TRANSACTION_QUEUE_LEN
I2C transaction queue length.
volatile uint16_t miss_start_stop_cnt
transaction successfully finished by I2C driver
static bool i2c_linux_idle(struct i2c_periph *p)
static void i2c_linux_setbitrate(struct i2c_periph *p, int bitrate)
transmit and receive transaction
uint8_t trans_extract_idx
volatile uint16_t queue_full_cnt
static pthread_mutex_t mutex
struct i2c_errors * errors
Functions to obtain rt priority or set the nice level.
#define ZEROS_ERR_COUNTER(_i2c_err)
static bool i2c_linux_submit(struct i2c_periph *p, struct i2c_transaction *t)
static void UNUSED i2c_arch_init(struct i2c_periph *p)
struct i2c_transaction * trans[I2C_TRANSACTION_QUEUE_LEN]
I2C transaction structure.
enum I2CTransactionStatus status
Transaction status.
transmit only transaction
static void * i2c_thread(void *thread_data)
void i2c_event(void)
i2c_event() function
static int get_rt_prio(int prio)
transaction is pending in queue
volatile uint16_t ack_fail_cnt
Architecture independent I2C (Inter-Integrated Circuit Bus) API.