-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
i2c_arch.c
278 lines (233 loc) · 7.03 KB
/
i2c_arch.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
*
* Copyright (C) 2014 Freek van Tienen <freek.v.tienen@gmail.com>
* 2018 Kirk Scheper <kirkscheper@gmail.com>
*
* This file is part of paparazzi.
*
* paparazzi is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* paparazzi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with paparazzi; see the file COPYING. If not, write to
* the Free Software Foundation, 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
*/
/** @file arch/linux/mcu_periph/i2c_arch.c
* I2C functionality
*/
#include "mcu_periph/i2c.h"
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <errno.h>
#ifndef _GNU_SOURCE
// for pthread_setname_np
#define _GNU_SOURCE
#endif
#include <pthread.h>
#include "rt_priority.h"
#ifndef I2C_THREAD_PRIO
#define I2C_THREAD_PRIO 10
#endif
static void *i2c_thread(void *thread_data);
// private I2C init structure
struct i2c_thread_t {
pthread_mutex_t mutex;
pthread_cond_t condition;
};
static void i2c_arch_init(struct i2c_periph *p)
{
pthread_t tid;
if (pthread_create(&tid, NULL, i2c_thread, (void*)p) != 0) {
fprintf(stderr, "i2c_arch_init: Could not create I2C thread.\n");
return;
}
pthread_setname_np(tid, "pprz_i2c_thread");
}
void i2c_event(void)
{
}
void i2c_setbitrate(struct i2c_periph *p __attribute__((unused)), int bitrate __attribute__((unused)))
{
}
bool i2c_idle(struct i2c_periph *p __attribute__((unused)))
{
return true;
}
bool i2c_submit(struct i2c_periph *p, struct i2c_transaction *t)
{
// get mutex lock and condition
pthread_mutex_t *mutex = &(((struct i2c_thread_t*)(p->init_struct))->mutex);
pthread_cond_t *condition = &(((struct i2c_thread_t*)(p->init_struct))->condition);
uint8_t next_idx;
pthread_mutex_lock(mutex);
next_idx = (p->trans_insert_idx + 1) % I2C_TRANSACTION_QUEUE_LEN;
if (next_idx == p->trans_extract_idx) {
// queue full
p->errors->queue_full_cnt++;
t->status = I2CTransFailed;
pthread_mutex_unlock(mutex);
return false;
}
t->status = I2CTransPending;
/* put transaction in queue */
p->trans[p->trans_insert_idx] = t;
p->trans_insert_idx = next_idx;
/* wake handler thread */
pthread_cond_signal(condition);
pthread_mutex_unlock(mutex);
return true;
}
/*
* Transactions handler thread
*/
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-qual"
static void *i2c_thread(void *data)
{
struct i2c_msg trx_msgs[2];
struct i2c_rdwr_ioctl_data trx_data = {
.msgs = trx_msgs,
.nmsgs = 2
};
get_rt_prio(I2C_THREAD_PRIO);
struct i2c_periph *p = (struct i2c_periph*)data;
pthread_mutex_t *mutex = &(((struct i2c_thread_t*)(p->init_struct))->mutex);
pthread_cond_t *condition = &(((struct i2c_thread_t*)(p->init_struct))->condition);
while (1)
{
/* wait for data to transfer */
pthread_mutex_lock(mutex);
if (p->trans_insert_idx == p->trans_extract_idx) {
pthread_cond_wait(condition, mutex);
}
int fd = (int)p->reg_addr;
struct i2c_transaction *t = p->trans[p->trans_extract_idx];
pthread_mutex_unlock(mutex);
t->status = I2CTransSuccess;
// Switch the different transaction types
switch (t->type) {
// Just transmitting
case I2CTransTx:
// Set the slave address, converted to 7 bit
ioctl(fd, I2C_SLAVE, t->slave_addr >> 1);
if (write(fd, (uint8_t *)t->buf, t->len_w) < 0) {
/* if write failed, increment error counter ack_fail_cnt */
pthread_mutex_lock(mutex);
p->errors->ack_fail_cnt++;
pthread_mutex_unlock(mutex);
t->status = I2CTransFailed;
}
break;
// Just reading
case I2CTransRx:
// Set the slave address, converted to 7 bit
ioctl(fd, I2C_SLAVE, t->slave_addr >> 1);
if (read(fd, (uint8_t *)t->buf, t->len_r) < 0) {
/* if read failed, increment error counter arb_lost_cnt */
pthread_mutex_lock(mutex);
p->errors->arb_lost_cnt++;
pthread_mutex_unlock(mutex);
t->status = I2CTransFailed;
}
break;
// First Transmit and then read with repeated start
case I2CTransTxRx:
trx_msgs[0].addr = t->slave_addr >> 1;
trx_msgs[0].flags = 0; /* tx */
trx_msgs[0].len = t->len_w;
trx_msgs[0].buf = (void*) t->buf;
trx_msgs[1].addr = t->slave_addr >> 1;
trx_msgs[1].flags = I2C_M_RD;
trx_msgs[1].len = t->len_r;
trx_msgs[1].buf = (void*) t->buf;
if (ioctl(fd, I2C_RDWR, &trx_data) < 0) {
/* if write/read failed, increment error counter miss_start_stop_cnt */
pthread_mutex_lock(mutex);
p->errors->miss_start_stop_cnt++;
pthread_mutex_unlock(mutex);
t->status = I2CTransFailed;
}
break;
default:
break;
}
pthread_mutex_lock(mutex);
p->trans_extract_idx = (p->trans_extract_idx + 1) % I2C_TRANSACTION_QUEUE_LEN;
pthread_mutex_unlock(mutex);
}
return NULL;
}
#pragma GCC diagnostic pop
#if USE_I2C0
struct i2c_errors i2c0_errors;
struct i2c_thread_t i2c0_thread;
void i2c0_hw_init(void)
{
i2c0.reg_addr = (void *)open("/dev/i2c-0", O_RDWR);
i2c0.errors = &i2c0_errors;
/* zeros error counter */
ZEROS_ERR_COUNTER(i2c0_errors);
pthread_mutex_init(&i2c0_thread.mutex, NULL);
pthread_cond_init(&i2c0_thread.condition, NULL);
i2c0.init_struct = (void*)(&i2c0_thread);
i2c_arch_init(&i2c0);
}
#endif
#if USE_I2C1
struct i2c_errors i2c1_errors;
struct i2c_thread_t i2c1_thread;
void i2c1_hw_init(void)
{
i2c1.reg_addr = (void *)open("/dev/i2c-1", O_RDWR);
i2c1.errors = &i2c1_errors;
/* zeros error counter */
ZEROS_ERR_COUNTER(i2c1_errors);
pthread_mutex_init(&i2c1_thread.mutex, NULL);
pthread_cond_init(&i2c1_thread.condition, NULL);
i2c1.init_struct = (void*)(&i2c1_thread);
i2c_arch_init(&i2c1);
}
#endif
#if USE_I2C2
struct i2c_errors i2c2_errors;
struct i2c_thread_t i2c2_thread;
void i2c2_hw_init(void)
{
i2c2.reg_addr = (void *)open("/dev/i2c-2", O_RDWR);
i2c2.errors = &i2c2_errors;
/* zeros error counter */
ZEROS_ERR_COUNTER(i2c2_errors);
pthread_mutex_init(&i2c2_thread.mutex, NULL);
pthread_cond_init(&i2c2_thread.condition, NULL);
i2c2.init_struct = (void*)(&i2c2_thread);
i2c_arch_init(&i2c2);
}
#endif
#if USE_I2C3
struct i2c_errors i2c3_errors;
struct i2c_thread_t i2c3_thread;
void i2c3_hw_init(void)
{
i2c3.reg_addr = (void *)open("/dev/i2c-3", O_RDWR);
i2c3.errors = &i2c3_errors;
/* zeros error counter */
ZEROS_ERR_COUNTER(i2c3_errors);
pthread_mutex_init(&i2c3_thread.mutex, NULL);
pthread_cond_init(&i2c3_thread.condition, NULL);
i2c3.init_struct = (void*)(&i2c3_thread);
i2c_arch_init(&i2c3);
}
#endif