Paparazzi UAS v7.0_unstable
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#define USE_I2C (USE_I2C0 || USE_I2C1 || USE_I2C2 || USE_I2C3 || USE_I2C4)
32#define USE_SOFT_I2C (USE_SOFTI2C0 || USE_SOFTI2C1)
33
34#if PERIODIC_TELEMETRY && (USE_I2C || USE_SOFT_I2C)
36
37static void send_i2cx_err(struct transport_tx *trans, struct link_device *dev, struct i2c_periph* i2c)
38{
50 uint8_t _bus = 0;
63 &_bus);
64}
65#endif
66
67
68#if USE_I2C0
69
70struct i2c_periph i2c0;
71
72void i2c0_init(void)
73{
74 i2c_init(&i2c0);
76}
77
78#endif /* USE_I2C0 */
79
80
81#if USE_I2C1
82
83struct i2c_periph i2c1;
84
85void i2c1_init(void)
86{
87 i2c_init(&i2c1);
89}
90
91#endif /* USE_I2C1 */
92
93
94#if USE_I2C2
95
96struct i2c_periph i2c2;
97
98void i2c2_init(void)
99{
100 i2c_init(&i2c2);
101 i2c2_hw_init();
102}
103
104#endif /* USE_I2C2 */
105
106#if USE_I2C3
107
108struct i2c_periph i2c3;
109
110void i2c3_init(void)
111{
112 i2c_init(&i2c3);
113 i2c3_hw_init();
114}
115
116#endif /* USE_I2C3 */
117
118#if USE_I2C4
119
120struct i2c_periph i2c4;
121
122void i2c4_init(void)
123{
124 i2c_init(&i2c4);
125 i2c4_hw_init();
126}
127
128#endif /* USE_I2C4 */
129
130#if USE_SOFTI2C0
131extern void send_softi2c0_err(struct transport_tx *trans, struct link_device *dev);
132#endif /* USE_SOFTI2C0 */
133
134#if USE_SOFTI2C1
135extern void send_softi2c1_err(struct transport_tx *trans, struct link_device *dev);
136#endif /* USE_SOFTI2C1 */
137
138#if PERIODIC_TELEMETRY && (USE_I2C || USE_SOFT_I2C)
139static void send_i2c_err(struct transport_tx *trans __attribute__((unused)),
140 struct link_device *dev __attribute__((unused)))
141{
142 static uint8_t _i2c_nb_cnt = 0;
143 switch (_i2c_nb_cnt) {
144 case 0:
145#if USE_I2C0
147#endif
148 break;
149 case 1:
150#if USE_I2C1
152#endif
153 break;
154 case 2:
155#if USE_I2C2
157#endif
158 break;
159 case 3:
160#if USE_I2C3
162#endif
163 break;
164 case 4:
165#if USE_I2C4
167#endif
168 case 5:
169#if USE_SOFTI2C0
171#endif
172 case 6:
173#if USE_SOFTI2C1
175#endif
176 break;
177 default:
178 break;
179 }
180 _i2c_nb_cnt++;
181 if (_i2c_nb_cnt == 7) {
182 _i2c_nb_cnt = 0;
183 }
184}
185#endif
186
187
188void i2c_init(struct i2c_periph *p)
189{
190 p->trans_insert_idx = 0;
191 p->trans_extract_idx = 0;
192 p->status = I2CIdle;
193 p->reg_addr = NULL;
194
195#if PERIODIC_TELEMETRY && (USE_I2C || USE_SOFT_I2C)
196 // the first to register do it for the others
198#endif
199}
200
201
204{
205 t->type = I2CTransTx;
206 t->slave_addr = s_addr;
207 t->len_w = len;
208 t->len_r = 0;
209 return i2c_submit(p, t);
210}
211
214{
215 t->type = I2CTransRx;
216 t->slave_addr = s_addr;
217 t->len_w = 0;
218 t->len_r = len;
219 return i2c_submit(p, t);
220}
221
223 uint8_t s_addr, uint8_t len_w, uint16_t len_r)
224{
225 t->type = I2CTransTxRx;
226 t->slave_addr = s_addr;
227 t->len_w = len_w;
228 t->len_r = len_r;
229 return i2c_submit(p, t);
230}
231
233#ifndef I2C_BLOCKING_TIMEOUT
234#define I2C_BLOCKING_TIMEOUT 1.f
235#endif
236
239{
240 t->type = I2CTransTx;
241 t->slave_addr = s_addr;
242 t->len_w = len;
243 t->len_r = 0;
244 if (!i2c_submit(p, t)) {
245 return false;
246 }
247
248 // Wait for transaction to complete
249 float start_t = get_sys_time_float();
250 while (t->status == I2CTransPending || t->status == I2CTransRunning) {
251 if (p->spin) p->spin(p);
253 break; // timeout after 1 second
254 }
255 }
256 return true;
257}
258
261{
262 t->type = I2CTransRx;
263 t->slave_addr = s_addr;
264 t->len_w = 0;
265 t->len_r = len;
266 if (!i2c_submit(p, t)) {
267 return false;
268 }
269
270 // Wait for transaction to complete
271 float start_t = get_sys_time_float();
272 while (t->status == I2CTransPending || t->status == I2CTransRunning) {
273 if (p->spin) p->spin(p);
275 break; // timeout after 1 second
276 }
277 }
278 return true;
279}
280
282 uint8_t s_addr, uint8_t len_w, uint16_t len_r)
283{
284 t->type = I2CTransTxRx;
285 t->slave_addr = s_addr;
286 t->len_w = len_w;
287 t->len_r = len_r;
288 if (!i2c_submit(p, t)) {
289 return false;
290 }
291
292 // Wait for transaction to complete
293 float start_t = get_sys_time_float();
294 while (t->status == I2CTransPending || t->status == I2CTransRunning) {
295 if (p->spin) p->spin(p);
297 break; // timeout after 1 second
298 }
299 }
300 return true;
301}
volatile uint16_t pec_recep_cnt
Definition i2c.h:172
volatile uint16_t smbus_alert_cnt
Definition i2c.h:174
struct i2c_errors * errors
Definition i2c.h:159
volatile uint16_t ack_fail_cnt
Definition i2c.h:168
volatile uint16_t timeout_tlow_cnt
Definition i2c.h:173
volatile uint16_t over_under_cnt
Definition i2c.h:171
volatile uint16_t unexpected_event_cnt
Definition i2c.h:175
volatile uint32_t last_unexpected_event
Definition i2c.h:176
volatile uint16_t wd_reset_cnt
Definition i2c.h:166
volatile uint16_t queue_full_cnt
Definition i2c.h:167
struct i2c_transaction * trans[I2C_TRANSACTION_QUEUE_LEN]
Definition i2c.h:151
volatile uint16_t miss_start_stop_cnt
Definition i2c.h:169
volatile uint16_t arb_lost_cnt
Definition i2c.h:170
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:281
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:259
static bool i2c_submit(struct i2c_periph *p, struct i2c_transaction *t)
Submit a I2C transaction.
Definition i2c.h:266
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:202
void i2c_init(struct i2c_periph *p)
Initialize I2C peripheral.
Definition i2c.c:188
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:212
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:237
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:222
@ I2CIdle
Definition i2c.h:66
@ I2CTransRunning
transaction is currently ongoing
Definition i2c.h:56
@ I2CTransPending
transaction is pending in queue
Definition i2c.h:55
@ I2CTransRx
receive only transaction
Definition i2c.h:48
@ I2CTransTx
transmit only transaction
Definition i2c.h:47
@ I2CTransTxRx
transmit and receive transaction
Definition i2c.h:49
I2C transaction structure.
Definition i2c.h:93
#define I2C_BLOCKING_TIMEOUT
Default timeout for blocking I2C transactions.
Definition i2c.c:234
Architecture independent I2C (Inter-Integrated Circuit Bus) API.
static float p[2][2]
uint16_t foo
Definition main_demo5.c:58
static const struct usb_device_descriptor dev
Definition usb_ser_hw.c:74
Architecture independent timing functions.
static float get_sys_time_float(void)
Get the time in seconds since startup.
Definition sys_time.h:138
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition telemetry.c:51
Periodic telemetry system header (includes downlink utility and generated code).
#define DefaultPeriodic
Set default periodic telemetry.
Definition telemetry.h:66
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.