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
56 if (pthread_create(&tid, NULL,
i2c_thread, (
void *)p) != 0) {
57 fprintf(stderr,
"i2c_arch_init: Could not create I2C thread.\n");
61 pthread_setname_np(tid,
"i2c");
85 pthread_mutex_lock(mutex);
91 pthread_mutex_unlock(mutex);
102 pthread_cond_signal(condition);
103 pthread_mutex_unlock(mutex);
111 #pragma GCC diagnostic push
112 #pragma GCC diagnostic ignored "-Wcast-qual"
116 struct i2c_rdwr_ioctl_data trx_data = {
129 pthread_mutex_lock(mutex);
131 pthread_cond_wait(condition, mutex);
136 pthread_mutex_unlock(mutex);
143 ioctl(fd,
I2C_SLAVE, t->slave_addr >> 1);
144 if (write(fd, (
uint8_t *)t->buf, t->len_w) < 0) {
146 pthread_mutex_lock(mutex);
148 pthread_mutex_unlock(mutex);
157 ioctl(fd,
I2C_SLAVE, t->slave_addr >> 1);
158 if (read(fd, (
uint8_t *)t->buf, t->len_r) < 0) {
160 pthread_mutex_lock(mutex);
162 pthread_mutex_unlock(mutex);
170 trx_msgs[0].
addr = t->slave_addr >> 1;
171 trx_msgs[0].
flags = 0;
172 trx_msgs[0].
len = t->len_w;
173 trx_msgs[0].
buf = (
void *) t->buf;
174 trx_msgs[1].
addr = t->slave_addr >> 1;
176 trx_msgs[1].
len = t->len_r;
177 trx_msgs[1].
buf = (
void *) t->buf;
178 if (ioctl(fd,
I2C_RDWR, &trx_data) < 0) {
180 pthread_mutex_lock(mutex);
182 pthread_mutex_unlock(mutex);
193 pthread_mutex_lock(mutex);
195 pthread_mutex_unlock(mutex);
199 #pragma GCC diagnostic pop
205 void i2c0_hw_init(
void)
207 i2c0.reg_addr = (
void *)open(
"/dev/i2c-0", O_RDWR);
208 i2c0.errors = &i2c0_errors;
213 pthread_mutex_init(&i2c0_thread.mutex, NULL);
214 pthread_cond_init(&i2c0_thread.condition, NULL);
215 i2c0.init_struct = (
void *)(&i2c0_thread);
225 void i2c1_hw_init(
void)
227 i2c1.reg_addr = (
void *)open(
"/dev/i2c-1", O_RDWR);
228 i2c1.errors = &i2c1_errors;
233 pthread_mutex_init(&i2c1_thread.mutex, NULL);
234 pthread_cond_init(&i2c1_thread.condition, NULL);
235 i2c1.init_struct = (
void *)(&i2c1_thread);
245 void i2c2_hw_init(
void)
247 i2c2.reg_addr = (
void *)open(
"/dev/i2c-2", O_RDWR);
248 i2c2.errors = &i2c2_errors;
253 pthread_mutex_init(&i2c2_thread.mutex, NULL);
254 pthread_cond_init(&i2c2_thread.condition, NULL);
255 i2c2.init_struct = (
void *)(&i2c2_thread);
265 void i2c3_hw_init(
void)
267 i2c3.reg_addr = (
void *)open(
"/dev/i2c-3", O_RDWR);
268 i2c3.errors = &i2c3_errors;
273 pthread_mutex_init(&i2c3_thread.mutex, NULL);
274 pthread_cond_init(&i2c3_thread.condition, NULL);
275 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
transmit and receive transaction
uint8_t trans_extract_idx
bool i2c_idle(struct i2c_periph *p)
i2c_idle() function
volatile uint16_t queue_full_cnt
void i2c_setbitrate(struct i2c_periph *p, int bitrate)
i2c_setbitrate() function
struct i2c_errors * errors
Functions to obtain rt priority or set the nice level.
#define ZEROS_ERR_COUNTER(_i2c_err)
static void UNUSED i2c_arch_init(struct i2c_periph *p)
struct i2c_transaction * trans[I2C_TRANSACTION_QUEUE_LEN]
bool i2c_submit(struct i2c_periph *p, struct i2c_transaction *t)
i2c_submit() function
I2C transaction structure.
enum I2CTransactionStatus status
Transaction status.
I2C peripheral structure.
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.