Paparazzi UAS  v5.14.0_stable-0-g3f680d1
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 PERIODIC_TELEMETRY
210 static void send_i2c_err(struct transport_tx *trans __attribute__((unused)),
211  struct link_device *dev __attribute__((unused)))
212 {
213  static uint8_t _i2c_nb_cnt = 0;
214  switch (_i2c_nb_cnt) {
215  case 0:
216 #if USE_I2C0
217  send_i2c0_err(trans, dev);
218 #endif
219  break;
220  case 1:
221 #if USE_I2C1
222  send_i2c1_err(trans, dev);
223 #endif
224  break;
225  case 2:
226 #if USE_I2C2
227  send_i2c2_err(trans, dev);
228 #endif
229  break;
230  case 3:
231 #if USE_I2C3
232  send_i2c3_err(trans, dev);
233 #endif
234  break;
235  default:
236  break;
237  }
238  _i2c_nb_cnt++;
239  if (_i2c_nb_cnt == 4) {
240  _i2c_nb_cnt = 0;
241  }
242 }
243 #endif
244 
245 
246 void i2c_init(struct i2c_periph *p)
247 {
248  p->trans_insert_idx = 0;
249  p->trans_extract_idx = 0;
250  p->status = I2CIdle;
251  p->reg_addr = NULL;
252 
253 #if PERIODIC_TELEMETRY
254  // the first to register do it for the others
255  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_I2C_ERRORS, send_i2c_err);
256 #endif
257 }
258 
259 
260 bool i2c_transmit(struct i2c_periph *p, struct i2c_transaction *t,
261  uint8_t s_addr, uint8_t len)
262 {
263  t->type = I2CTransTx;
264  t->slave_addr = s_addr;
265  t->len_w = len;
266  t->len_r = 0;
267  return i2c_submit(p, t);
268 }
269 
270 bool i2c_receive(struct i2c_periph *p, struct i2c_transaction *t,
271  uint8_t s_addr, uint16_t len)
272 {
273  t->type = I2CTransRx;
274  t->slave_addr = s_addr;
275  t->len_w = 0;
276  t->len_r = len;
277  return i2c_submit(p, t);
278 }
279 
280 bool i2c_transceive(struct i2c_periph *p, struct i2c_transaction *t,
281  uint8_t s_addr, uint8_t len_w, uint16_t len_r)
282 {
283  t->type = I2CTransTxRx;
284  t->slave_addr = s_addr;
285  t->len_w = len_w;
286  t->len_r = len_r;
287  return i2c_submit(p, t);
288 }
289 
291 #ifndef I2C_BLOCKING_TIMEOUT
292 #define I2C_BLOCKING_TIMEOUT 1.f
293 #endif
294 
296  uint8_t s_addr, uint8_t len)
297 {
298  t->type = I2CTransTx;
299  t->slave_addr = s_addr;
300  t->len_w = len;
301  t->len_r = 0;
302  if (!i2c_submit(p, t)) {
303  return false;
304  }
305 
306  // Wait for transaction to complete
307  float start_t = get_sys_time_float();
308  while (t->status == I2CTransPending || t->status == I2CTransRunning) {
309  if (get_sys_time_float() - start_t > I2C_BLOCKING_TIMEOUT) {
310  break; // timeout after 1 second
311  }
312  }
313  return true;
314 }
315 
317  uint8_t s_addr, uint16_t len)
318 {
319  t->type = I2CTransRx;
320  t->slave_addr = s_addr;
321  t->len_w = 0;
322  t->len_r = len;
323  if (!i2c_submit(p, t)) {
324  return false;
325  }
326 
327  // Wait for transaction to complete
328  float start_t = get_sys_time_float();
329  while (t->status == I2CTransPending || t->status == I2CTransRunning) {
330  if (get_sys_time_float() - start_t > I2C_BLOCKING_TIMEOUT) {
331  break; // timeout after 1 second
332  }
333  }
334  return true;
335 }
336 
338  uint8_t s_addr, uint8_t len_w, uint16_t len_r)
339 {
340  t->type = I2CTransTxRx;
341  t->slave_addr = s_addr;
342  t->len_w = len_w;
343  t->len_r = len_r;
344  if (!i2c_submit(p, t)) {
345  return false;
346  }
347 
348  // Wait for transaction to complete
349  float start_t = get_sys_time_float();
350  while (t->status == I2CTransPending || t->status == I2CTransRunning) {
351  if (get_sys_time_float() - start_t > I2C_BLOCKING_TIMEOUT) {
352  break; // timeout after 1 second
353  }
354  }
355  return true;
356 }
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:246
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:316
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:337
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:374
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:280
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:260
static void send_i2c_err(struct transport_tx *trans, struct link_device *dev)
Definition: i2c.c:210
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:270
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:292
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:295
Architecture independent I2C (Inter-Integrated Circuit Bus) API.