Paparazzi UAS  v5.15_devel-109-gee85905
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
i2c.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010-2012 The Paparazzi Team
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  */
22 
28 #include "mcu_periph/i2c.h"
29 #include "mcu_periph/sys_time.h"
30 
31 #if PERIODIC_TELEMETRY
33 #endif
34 
35 #if USE_I2C0
36 
37 struct i2c_periph i2c0;
38 
39 #if PERIODIC_TELEMETRY
40 static void send_i2c0_err(struct transport_tx *trans, struct link_device *dev)
41 {
42  uint16_t i2c0_wd_reset_cnt = i2c0.errors->wd_reset_cnt;
43  uint16_t i2c0_queue_full_cnt = i2c0.errors->queue_full_cnt;
44  uint16_t i2c0_ack_fail_cnt = i2c0.errors->ack_fail_cnt;
45  uint16_t i2c0_miss_start_stop_cnt = i2c0.errors->miss_start_stop_cnt;
46  uint16_t i2c0_arb_lost_cnt = i2c0.errors->arb_lost_cnt;
47  uint16_t i2c0_over_under_cnt = i2c0.errors->over_under_cnt;
48  uint16_t i2c0_pec_recep_cnt = i2c0.errors->pec_recep_cnt;
49  uint16_t i2c0_timeout_tlow_cnt = i2c0.errors->timeout_tlow_cnt;
50  uint16_t i2c0_smbus_alert_cnt = i2c0.errors->smbus_alert_cnt;
51  uint16_t i2c0_unexpected_event_cnt = i2c0.errors->unexpected_event_cnt;
52  uint32_t i2c0_last_unexpected_event = i2c0.errors->last_unexpected_event;
53  uint8_t _bus0 = 0;
54  pprz_msg_send_I2C_ERRORS(trans, dev, AC_ID,
55  &i2c0_wd_reset_cnt,
56  &i2c0_queue_full_cnt,
57  &i2c0_ack_fail_cnt,
58  &i2c0_miss_start_stop_cnt,
59  &i2c0_arb_lost_cnt,
60  &i2c0_over_under_cnt,
61  &i2c0_pec_recep_cnt,
62  &i2c0_timeout_tlow_cnt,
63  &i2c0_smbus_alert_cnt,
64  &i2c0_unexpected_event_cnt,
65  &i2c0_last_unexpected_event,
66  &_bus0);
67 }
68 #endif
69 
70 void i2c0_init(void)
71 {
72  i2c_init(&i2c0);
73  i2c0_hw_init();
74 }
75 
76 #endif /* USE_I2C0 */
77 
78 
79 #if USE_I2C1
80 
81 struct i2c_periph i2c1;
82 
83 #if PERIODIC_TELEMETRY
84 static void send_i2c1_err(struct transport_tx *trans, struct link_device *dev)
85 {
86  uint16_t i2c1_wd_reset_cnt = i2c1.errors->wd_reset_cnt;
87  uint16_t i2c1_queue_full_cnt = i2c1.errors->queue_full_cnt;
88  uint16_t i2c1_ack_fail_cnt = i2c1.errors->ack_fail_cnt;
89  uint16_t i2c1_miss_start_stop_cnt = i2c1.errors->miss_start_stop_cnt;
90  uint16_t i2c1_arb_lost_cnt = i2c1.errors->arb_lost_cnt;
91  uint16_t i2c1_over_under_cnt = i2c1.errors->over_under_cnt;
92  uint16_t i2c1_pec_recep_cnt = i2c1.errors->pec_recep_cnt;
93  uint16_t i2c1_timeout_tlow_cnt = i2c1.errors->timeout_tlow_cnt;
94  uint16_t i2c1_smbus_alert_cnt = i2c1.errors->smbus_alert_cnt;
95  uint16_t i2c1_unexpected_event_cnt = i2c1.errors->unexpected_event_cnt;
96  uint32_t i2c1_last_unexpected_event = i2c1.errors->last_unexpected_event;
97  uint8_t _bus1 = 1;
98  pprz_msg_send_I2C_ERRORS(trans, dev, AC_ID,
99  &i2c1_wd_reset_cnt,
100  &i2c1_queue_full_cnt,
101  &i2c1_ack_fail_cnt,
102  &i2c1_miss_start_stop_cnt,
103  &i2c1_arb_lost_cnt,
104  &i2c1_over_under_cnt,
105  &i2c1_pec_recep_cnt,
106  &i2c1_timeout_tlow_cnt,
107  &i2c1_smbus_alert_cnt,
108  &i2c1_unexpected_event_cnt,
109  &i2c1_last_unexpected_event,
110  &_bus1);
111 }
112 #endif
113 
114 void i2c1_init(void)
115 {
116  i2c_init(&i2c1);
117  i2c1_hw_init();
118 }
119 
120 #endif /* USE_I2C1 */
121 
122 
123 #if USE_I2C2
124 
125 struct i2c_periph i2c2;
126 
127 #if PERIODIC_TELEMETRY
128 static void send_i2c2_err(struct transport_tx *trans, struct link_device *dev)
129 {
130  uint16_t i2c2_wd_reset_cnt = i2c2.errors->wd_reset_cnt;
131  uint16_t i2c2_queue_full_cnt = i2c2.errors->queue_full_cnt;
132  uint16_t i2c2_ack_fail_cnt = i2c2.errors->ack_fail_cnt;
133  uint16_t i2c2_miss_start_stop_cnt = i2c2.errors->miss_start_stop_cnt;
134  uint16_t i2c2_arb_lost_cnt = i2c2.errors->arb_lost_cnt;
135  uint16_t i2c2_over_under_cnt = i2c2.errors->over_under_cnt;
136  uint16_t i2c2_pec_recep_cnt = i2c2.errors->pec_recep_cnt;
137  uint16_t i2c2_timeout_tlow_cnt = i2c2.errors->timeout_tlow_cnt;
138  uint16_t i2c2_smbus_alert_cnt = i2c2.errors->smbus_alert_cnt;
139  uint16_t i2c2_unexpected_event_cnt = i2c2.errors->unexpected_event_cnt;
140  uint32_t i2c2_last_unexpected_event = i2c2.errors->last_unexpected_event;
141  uint8_t _bus2 = 2;
142  pprz_msg_send_I2C_ERRORS(trans, dev, AC_ID,
143  &i2c2_wd_reset_cnt,
144  &i2c2_queue_full_cnt,
145  &i2c2_ack_fail_cnt,
146  &i2c2_miss_start_stop_cnt,
147  &i2c2_arb_lost_cnt,
148  &i2c2_over_under_cnt,
149  &i2c2_pec_recep_cnt,
150  &i2c2_timeout_tlow_cnt,
151  &i2c2_smbus_alert_cnt,
152  &i2c2_unexpected_event_cnt,
153  &i2c2_last_unexpected_event,
154  &_bus2);
155 }
156 #endif
157 
158 void i2c2_init(void)
159 {
160  i2c_init(&i2c2);
161  i2c2_hw_init();
162 }
163 
164 #endif /* USE_I2C2 */
165 
166 #if USE_I2C3
167 
168 struct i2c_periph i2c3;
169 
170 void i2c3_init(void)
171 {
172  i2c_init(&i2c3);
173  i2c3_hw_init();
174 }
175 
176 #if PERIODIC_TELEMETRY
177 static void send_i2c3_err(struct transport_tx *trans, struct link_device *dev)
178 {
179  uint16_t i2c3_wd_reset_cnt = i2c3.errors->wd_reset_cnt;
180  uint16_t i2c3_queue_full_cnt = i2c3.errors->queue_full_cnt;
181  uint16_t i2c3_ack_fail_cnt = i2c3.errors->ack_fail_cnt;
182  uint16_t i2c3_miss_start_stop_cnt = i2c3.errors->miss_start_stop_cnt;
183  uint16_t i2c3_arb_lost_cnt = i2c3.errors->arb_lost_cnt;
184  uint16_t i2c3_over_under_cnt = i2c3.errors->over_under_cnt;
185  uint16_t i2c3_pec_recep_cnt = i2c3.errors->pec_recep_cnt;
186  uint16_t i2c3_timeout_tlow_cnt = i2c3.errors->timeout_tlow_cnt;
187  uint16_t i2c3_smbus_alert_cnt = i2c3.errors->smbus_alert_cnt;
188  uint16_t i2c3_unexpected_event_cnt = i2c3.errors->unexpected_event_cnt;
189  uint32_t i2c3_last_unexpected_event = i2c3.errors->last_unexpected_event;
190  uint8_t _bus3 = 3;
191  pprz_msg_send_I2C_ERRORS(trans, dev, AC_ID,
192  &i2c3_wd_reset_cnt,
193  &i2c3_queue_full_cnt,
194  &i2c3_ack_fail_cnt,
195  &i2c3_miss_start_stop_cnt,
196  &i2c3_arb_lost_cnt,
197  &i2c3_over_under_cnt,
198  &i2c3_pec_recep_cnt,
199  &i2c3_timeout_tlow_cnt,
200  &i2c3_smbus_alert_cnt,
201  &i2c3_unexpected_event_cnt,
202  &i2c3_last_unexpected_event,
203  &_bus3);
204 }
205 #endif
206 
207 #endif /* USE_I2C3 */
208 
209 #if USE_I2C4
210 
211 struct i2c_periph i2c4;
212 
213 void i2c4_init(void)
214 {
215  i2c_init(&i2c4);
216  i2c4_hw_init();
217 }
218 
219 #if PERIODIC_TELEMETRY
220 static void send_i2c4_err(struct transport_tx *trans, struct link_device *dev)
221 {
222  uint16_t i2c4_wd_reset_cnt = i2c4.errors->wd_reset_cnt;
223  uint16_t i2c4_queue_full_cnt = i2c4.errors->queue_full_cnt;
224  uint16_t i2c4_ack_fail_cnt = i2c4.errors->ack_fail_cnt;
225  uint16_t i2c4_miss_start_stop_cnt = i2c4.errors->miss_start_stop_cnt;
226  uint16_t i2c4_arb_lost_cnt = i2c4.errors->arb_lost_cnt;
227  uint16_t i2c4_over_under_cnt = i2c4.errors->over_under_cnt;
228  uint16_t i2c4_pec_recep_cnt = i2c4.errors->pec_recep_cnt;
229  uint16_t i2c4_timeout_tlow_cnt = i2c4.errors->timeout_tlow_cnt;
230  uint16_t i2c4_smbus_alert_cnt = i2c4.errors->smbus_alert_cnt;
231  uint16_t i2c4_unexpected_event_cnt = i2c4.errors->unexpected_event_cnt;
232  uint32_t i2c4_last_unexpected_event = i2c4.errors->last_unexpected_event;
233  uint8_t _bus4 = 4;
234  pprz_msg_send_I2C_ERRORS(trans, dev, AC_ID,
235  &i2c4_wd_reset_cnt,
236  &i2c4_queue_full_cnt,
237  &i2c4_ack_fail_cnt,
238  &i2c4_miss_start_stop_cnt,
239  &i2c4_arb_lost_cnt,
240  &i2c4_over_under_cnt,
241  &i2c4_pec_recep_cnt,
242  &i2c4_timeout_tlow_cnt,
243  &i2c4_smbus_alert_cnt,
244  &i2c4_unexpected_event_cnt,
245  &i2c4_last_unexpected_event,
246  &_bus4);
247 }
248 #endif
249 
250 #endif /* USE_I2C4 */
251 
252 #if PERIODIC_TELEMETRY
253 static void send_i2c_err(struct transport_tx *trans __attribute__((unused)),
254  struct link_device *dev __attribute__((unused)))
255 {
256  static uint8_t _i2c_nb_cnt = 0;
257  switch (_i2c_nb_cnt) {
258  case 0:
259 #if USE_I2C0
260  send_i2c0_err(trans, dev);
261 #endif
262  break;
263  case 1:
264 #if USE_I2C1
265  send_i2c1_err(trans, dev);
266 #endif
267  break;
268  case 2:
269 #if USE_I2C2
270  send_i2c2_err(trans, dev);
271 #endif
272  break;
273  case 3:
274 #if USE_I2C3
275  send_i2c3_err(trans, dev);
276 #endif
277  break;
278  case 4:
279 #if USE_I2C4
280  send_i2c4_err(trans, dev);
281 #endif
282  break;
283  default:
284  break;
285  }
286  _i2c_nb_cnt++;
287  if (_i2c_nb_cnt == 4) {
288  _i2c_nb_cnt = 0;
289  }
290 }
291 #endif
292 
293 
294 void i2c_init(struct i2c_periph *p)
295 {
296  p->trans_insert_idx = 0;
297  p->trans_extract_idx = 0;
298  p->status = I2CIdle;
299  p->reg_addr = NULL;
300 
301 #if PERIODIC_TELEMETRY
302  // the first to register do it for the others
303  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_I2C_ERRORS, send_i2c_err);
304 #endif
305 }
306 
307 
308 bool i2c_transmit(struct i2c_periph *p, struct i2c_transaction *t,
309  uint8_t s_addr, uint8_t len)
310 {
311  t->type = I2CTransTx;
312  t->slave_addr = s_addr;
313  t->len_w = len;
314  t->len_r = 0;
315  return i2c_submit(p, t);
316 }
317 
318 bool i2c_receive(struct i2c_periph *p, struct i2c_transaction *t,
319  uint8_t s_addr, uint16_t len)
320 {
321  t->type = I2CTransRx;
322  t->slave_addr = s_addr;
323  t->len_w = 0;
324  t->len_r = len;
325  return i2c_submit(p, t);
326 }
327 
328 bool i2c_transceive(struct i2c_periph *p, struct i2c_transaction *t,
329  uint8_t s_addr, uint8_t len_w, uint16_t len_r)
330 {
331  t->type = I2CTransTxRx;
332  t->slave_addr = s_addr;
333  t->len_w = len_w;
334  t->len_r = len_r;
335  return i2c_submit(p, t);
336 }
337 
339 #ifndef I2C_BLOCKING_TIMEOUT
340 #define I2C_BLOCKING_TIMEOUT 1.f
341 #endif
342 
344  uint8_t s_addr, uint8_t len)
345 {
346  t->type = I2CTransTx;
347  t->slave_addr = s_addr;
348  t->len_w = len;
349  t->len_r = 0;
350  if (!i2c_submit(p, t)) {
351  return false;
352  }
353 
354  // Wait for transaction to complete
355  float start_t = get_sys_time_float();
356  while (t->status == I2CTransPending || t->status == I2CTransRunning) {
357  if (get_sys_time_float() - start_t > I2C_BLOCKING_TIMEOUT) {
358  break; // timeout after 1 second
359  }
360  }
361  return true;
362 }
363 
365  uint8_t s_addr, uint16_t len)
366 {
367  t->type = I2CTransRx;
368  t->slave_addr = s_addr;
369  t->len_w = 0;
370  t->len_r = len;
371  if (!i2c_submit(p, t)) {
372  return false;
373  }
374 
375  // Wait for transaction to complete
376  float start_t = get_sys_time_float();
377  while (t->status == I2CTransPending || t->status == I2CTransRunning) {
378  if (get_sys_time_float() - start_t > I2C_BLOCKING_TIMEOUT) {
379  break; // timeout after 1 second
380  }
381  }
382  return true;
383 }
384 
386  uint8_t s_addr, uint8_t len_w, uint16_t len_r)
387 {
388  t->type = I2CTransTxRx;
389  t->slave_addr = s_addr;
390  t->len_w = len_w;
391  t->len_r = len_r;
392  if (!i2c_submit(p, t)) {
393  return false;
394  }
395 
396  // Wait for transaction to complete
397  float start_t = get_sys_time_float();
398  while (t->status == I2CTransPending || t->status == I2CTransRunning) {
399  if (get_sys_time_float() - start_t > I2C_BLOCKING_TIMEOUT) {
400  break; // timeout after 1 second
401  }
402  }
403  return true;
404 }
unsigned short uint16_t
Definition: types.h:16
uint16_t len_r
Number of bytes to read/receive.
Definition: i2c.h:110
Periodic telemetry system header (includes downlink utility and generated code).
transmit and receive transaction
Definition: i2c.h:49
uint8_t trans_extract_idx
Definition: i2c.h:142
void i2c_init(struct i2c_periph *p)
Initialize I2C peripheral.
Definition: i2c.c:294
Definition: i2c.h:66
struct i2c_errors * errors
Definition: i2c.h:148
enum I2CStatus status
Definition: i2c.h:144
static float get_sys_time_float(void)
Get the time in seconds since startup.
Definition: sys_time.h:129
void * reg_addr
Definition: i2c.h:146
bool i2c_blocking_receive(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint16_t len)
Submit a read only transaction and wait for it to complete.
Definition: i2c.c:364
bool i2c_blocking_transceive(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len_w, uint16_t len_r)
Submit a write/read transaction and wait for it to complete.
Definition: i2c.c:385
uint8_t len_w
Number of bytes to write/transmit.
Definition: i2c.h:116
Architecture independent timing functions.
unsigned long uint32_t
Definition: types.h:18
struct i2c_transaction * trans[I2C_TRANSACTION_QUEUE_LEN]
Definition: i2c.h:140
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
bool i2c_submit(struct i2c_periph *p, struct i2c_transaction *t)
i2c_submit() function
Definition: i2c_arch.c:433
bool i2c_transceive(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len_w, uint16_t len_r)
Submit a write/read transaction.
Definition: i2c.c:328
I2C transaction structure.
Definition: i2c.h:93
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
bool i2c_transmit(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len)
Submit a write only transaction.
Definition: i2c.c:308
static void send_i2c_err(struct transport_tx *trans, struct link_device *dev)
Definition: i2c.c:253
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:73
uint8_t slave_addr
Slave address.
Definition: i2c.h:104
I2C peripheral structure.
Definition: i2c.h:138
unsigned char uint8_t
Definition: types.h:14
bool i2c_receive(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint16_t len)
Submit a read only transaction.
Definition: i2c.c:318
transaction is currently ongoing
Definition: i2c.h:56
transmit only transaction
Definition: i2c.h:47
#define I2C_BLOCKING_TIMEOUT
Default timeout for blocking I2C transactions.
Definition: i2c.c:340
static float p[2][2]
volatile uint16_t wd_reset_cnt
Definition: i2c.h:155
enum I2CTransactionType type
Transaction type.
Definition: i2c.h:98
uint8_t trans_insert_idx
Definition: i2c.h:141
receive only transaction
Definition: i2c.h:48
transaction is pending in queue
Definition: i2c.h:55
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition: telemetry.c:46
bool i2c_blocking_transmit(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len)
Submit a write only transaction and wait for it to complete.
Definition: i2c.c:343
Architecture independent I2C (Inter-Integrated Circuit Bus) API.