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);
93 if (next_idx ==
p->trans_extract_idx) {
95 p->errors->queue_full_cnt++;
97 pthread_mutex_unlock(
mutex);
104 p->trans[
p->trans_insert_idx] = t;
105 p->trans_insert_idx = next_idx;
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);
136 if (
p->trans_insert_idx ==
p->trans_extract_idx) {
140 int fd = (int)
p->reg_addr;
142 pthread_mutex_unlock(
mutex);
150 if (write(
fd, (
uint8_t *)t->buf, t->len_w) < 0) {
152 pthread_mutex_lock(
mutex);
153 p->errors->ack_fail_cnt++;
154 pthread_mutex_unlock(
mutex);
164 if (read(
fd, (
uint8_t *)t->buf, t->len_r) < 0) {
166 pthread_mutex_lock(
mutex);
167 p->errors->arb_lost_cnt++;
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;
186 pthread_mutex_lock(
mutex);
187 p->errors->miss_start_stop_cnt++;
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);