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;
110 pthread_mutex_unlock(
mutex);
118#pragma GCC diagnostic push
119#pragma GCC diagnostic ignored "-Wcast-qual"
123 struct i2c_rdwr_ioctl_data trx_data = {
136 pthread_mutex_lock(
mutex);
137 if (
p->trans_insert_idx ==
p->trans_extract_idx) {
141 int fd = (int)
p->reg_addr;
143 pthread_mutex_unlock(
mutex);
153 pthread_mutex_lock(
mutex);
154 p->errors->ack_fail_cnt++;
155 pthread_mutex_unlock(
mutex);
167 pthread_mutex_lock(
mutex);
168 p->errors->arb_lost_cnt++;
169 pthread_mutex_unlock(
mutex);
178 trx_msgs[0].
flags = 0;
180 trx_msgs[0].
buf = (
void *) t->
buf;
184 trx_msgs[1].
buf = (
void *) t->
buf;
187 pthread_mutex_lock(
mutex);
188 p->errors->miss_start_stop_cnt++;
189 pthread_mutex_unlock(
mutex);
200 pthread_mutex_lock(
mutex);
202 pthread_mutex_unlock(
mutex);
207#pragma GCC diagnostic pop
213void i2c0_hw_init(
void)
219 i2c0.reg_addr = (
void *)open(
"/dev/i2c-0", O_RDWR);
220 i2c0.errors = &i2c0_errors;
225 pthread_mutex_init(&i2c0_thread.mutex, NULL);
226 pthread_cond_init(&i2c0_thread.condition, NULL);
227 i2c0.init_struct = (
void *)(&i2c0_thread);
237void i2c1_hw_init(
void)
243 i2c1.reg_addr = (
void *)open(
"/dev/i2c-1", O_RDWR);
244 i2c1.errors = &i2c1_errors;
249 pthread_mutex_init(&i2c1_thread.mutex, NULL);
250 pthread_cond_init(&i2c1_thread.condition, NULL);
251 i2c1.init_struct = (
void *)(&i2c1_thread);
261void i2c2_hw_init(
void)
267 i2c2.reg_addr = (
void *)open(
"/dev/i2c-2", O_RDWR);
268 i2c2.errors = &i2c2_errors;
273 pthread_mutex_init(&i2c2_thread.mutex, NULL);
274 pthread_cond_init(&i2c2_thread.condition, NULL);
275 i2c2.init_struct = (
void *)(&i2c2_thread);
285void i2c3_hw_init(
void)
291 i2c3.reg_addr = (
void *)open(
"/dev/i2c-3", O_RDWR);
292 i2c3.errors = &i2c3_errors;
297 pthread_mutex_init(&i2c3_thread.mutex, NULL);
298 pthread_cond_init(&i2c3_thread.condition, NULL);
299 i2c3.init_struct = (
void *)(&i2c3_thread);
void pprz_bsem_init(pprz_bsem_t *bsem, bool taken)
void pprz_bsem_signal(pprz_bsem_t *bsem)
static pthread_mutex_t mutex
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
uint16_t len_r
Number of bytes to read/receive.
enum I2CTransactionType type
Transaction type.
enum I2CTransactionStatus status
Transaction status.
pprz_bsem_t bsem
binary semaphore for blocking functions
uint8_t slave_addr
Slave address.
uint8_t len_w
Number of bytes to write/transmit.
void i2c_event(void)
i2c_event() function
#define ZEROS_ERR_COUNTER(_i2c_err)
#define I2C_TRANSACTION_QUEUE_LEN
I2C transaction queue length.
@ I2CTransSuccess
transaction successfully finished by I2C driver
@ I2CTransFailed
transaction failed
@ I2CTransPending
transaction is pending in queue
@ I2CTransRx
receive only transaction
@ I2CTransTx
transmit only transaction
@ I2CTransTxRx
transmit and receive transaction
I2C transaction structure.
Architecture independent I2C (Inter-Integrated Circuit Bus) API.
static void UNUSED i2c_arch_init(struct i2c_periph *p)
static bool i2c_linux_submit(struct i2c_periph *p, struct i2c_transaction *t)
static void * i2c_thread(void *thread_data)
static bool i2c_linux_idle(struct i2c_periph *p)
static void i2c_linux_setbitrate(struct i2c_periph *p, int bitrate)
Functions to obtain rt priority or set the nice level.
static int get_rt_prio(int prio)
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.