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.h
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
30#ifndef MCU_PERIPH_I2C_H
31#define MCU_PERIPH_I2C_H
32
33#include "std.h"
34
35#include "mcu_periph/i2c_arch.h"
37
52
62
80
84#ifndef I2C_BUF_LEN
85#define I2C_BUF_LEN 32
86#endif
87
133
137#ifndef I2C_TRANSACTION_QUEUE_LEN
138#define I2C_TRANSACTION_QUEUE_LEN 8
139#endif
140
143struct i2c_periph;
144typedef bool i2c_idle_fn_t(struct i2c_periph *p);
145typedef bool i2c_submit_fn_t(struct i2c_periph *p, struct i2c_transaction *t);
146typedef void i2c_setbitrate_fn_t(struct i2c_periph *p, int bitrate);
147typedef void i2c_spin_fn_t(struct i2c_periph *p); // To update peripherals within tight loops, e.g. the blocking functions. Leave NULL if not required.
148
150 /* architecture-specific functions */
155 /* circular buffer holding transactions */
159 /* internal state of the peripheral */
160 volatile enum I2CStatus status;
161 volatile uint8_t idx_buf;
162 void *reg_addr;
166};
167
184
185
186#define ZEROS_ERR_COUNTER(_i2c_err) { \
187 _i2c_err.wd_reset_cnt = 0; \
188 _i2c_err.queue_full_cnt = 0; \
189 _i2c_err.ack_fail_cnt = 0; \
190 _i2c_err.miss_start_stop_cnt = 0; \
191 _i2c_err.arb_lost_cnt = 0; \
192 _i2c_err.over_under_cnt = 0; \
193 _i2c_err.pec_recep_cnt = 0; \
194 _i2c_err.timeout_tlow_cnt = 0; \
195 _i2c_err.smbus_alert_cnt = 0; \
196 _i2c_err.unexpected_event_cnt = 0; \
197 _i2c_err.last_unexpected_event = 0; \
198 _i2c_err.er_irq_cnt = 0; \
199 }
200
201
202#if USE_I2C0
203
204extern struct i2c_periph i2c0;
205extern void i2c0_init(void);
206
207#endif /* USE_I2C0 */
208
209
210#if USE_I2C1
211
212extern struct i2c_periph i2c1;
213extern void i2c1_init(void);
214
215#endif /* USE_I2C1 */
216
217
218#if USE_I2C2
219
220extern struct i2c_periph i2c2;
221extern void i2c2_init(void);
222
223#endif /* USE_I2C2 */
224
225
226#if USE_I2C3
227
228extern struct i2c_periph i2c3;
229extern void i2c3_init(void);
230
231#endif /* USE_I2C3 */
232
233
234#if USE_I2C4
235
236extern struct i2c_periph i2c4;
237extern void i2c4_init(void);
238
239#endif /* USE_I2C4 */
240
241
242#if USE_SOFTI2C0
243extern struct i2c_periph softi2c0;
244extern void softi2c0_init(void);
245#endif /* USE_SOFTI2C0 */
246
247
248#if USE_SOFTI2C1
249extern struct i2c_periph softi2c1;
250extern void softi2c1_init(void);
251#endif /* USE_SOFTI2C1 */
252
253
255extern void i2c_init(struct i2c_periph *p);
256
261static inline bool i2c_idle(struct i2c_periph *p) {
262 return p->idle(p);
263}
264
271static inline bool i2c_submit(struct i2c_periph *p, struct i2c_transaction *t) {
272 return p->submit(p, t);
273}
274
279static inline void i2c_setbitrate(struct i2c_periph *p, int bitrate) {
280 p->setbitrate(p, bitrate);
281}
282
283extern void i2c_event(void);
284
285/*
286 * Convenience functions.
287 * Usually these are preferred over i2c_submit,
288 * as they explicitly set the transaction type again.
289 *
290 * Return FALSE if submitting the transaction failed.
291 */
301extern bool i2c_transmit(struct i2c_periph *p, struct i2c_transaction *t,
302 uint8_t s_addr, uint8_t len);
303
313extern bool i2c_receive(struct i2c_periph *p, struct i2c_transaction *t,
314 uint8_t s_addr, uint16_t len);
315
326extern bool i2c_transceive(struct i2c_periph *p, struct i2c_transaction *t,
327 uint8_t s_addr, uint8_t len_w, uint16_t len_r);
328
340 uint8_t s_addr, uint8_t len, float timeout);
341
353 uint8_t s_addr, uint16_t len, float timeout);
354
367 uint8_t s_addr, uint8_t len_w, uint16_t len_r, float timeout);
371#endif /* I2C_H */
volatile uint16_t pec_recep_cnt
Definition i2c.h:177
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition i2c.h:123
volatile uint16_t smbus_alert_cnt
Definition i2c.h:179
struct i2c_errors * errors
Definition i2c.h:164
i2c_spin_fn_t * spin
Definition i2c.h:154
uint8_t trans_extract_idx
Definition i2c.h:158
uint16_t len_r
Number of bytes to read/receive.
Definition i2c.h:111
volatile uint16_t ack_fail_cnt
Definition i2c.h:173
i2c_submit_fn_t * submit
Definition i2c.h:152
uint8_t trans_insert_idx
Definition i2c.h:157
volatile uint16_t timeout_tlow_cnt
Definition i2c.h:178
void * init_struct
Definition i2c.h:163
volatile uint16_t over_under_cnt
Definition i2c.h:176
volatile uint16_t unexpected_event_cnt
Definition i2c.h:180
volatile uint32_t last_unexpected_event
Definition i2c.h:181
volatile uint32_t er_irq_cnt
Definition i2c.h:182
i2c_setbitrate_fn_t * setbitrate
Definition i2c.h:153
volatile uint16_t wd_reset_cnt
Definition i2c.h:171
enum I2CTransactionType type
Transaction type.
Definition i2c.h:99
void * reg_addr
Definition i2c.h:162
enum I2CTransactionStatus status
Transaction status.
Definition i2c.h:127
volatile uint16_t queue_full_cnt
Definition i2c.h:172
struct i2c_transaction * trans[I2C_TRANSACTION_QUEUE_LEN]
Definition i2c.h:156
volatile uint16_t miss_start_stop_cnt
Definition i2c.h:174
i2c_idle_fn_t * idle
Definition i2c.h:151
volatile int16_t watchdog
Definition i2c.h:165
volatile uint8_t idx_buf
Definition i2c.h:161
pprz_bsem_t bsem
binary semaphore for blocking functions
Definition i2c.h:131
uint8_t slave_addr
Slave address.
Definition i2c.h:105
enum I2CStatus status
Definition i2c.h:160
volatile uint16_t arb_lost_cnt
Definition i2c.h:175
uint8_t len_w
Number of bytes to write/transmit.
Definition i2c.h:117
void i2c_event(void)
i2c_event() function
Definition i2c_arch.c:394
#define I2C_TRANSACTION_QUEUE_LEN
I2C transaction queue length.
Definition i2c.h:138
#define I2C_BUF_LEN
I2C buffer length.
Definition i2c.h:85
void i2c_spin_fn_t(struct i2c_periph *p)
Definition i2c.h:147
static bool i2c_submit(struct i2c_periph *p, struct i2c_transaction *t)
Submit a I2C transaction.
Definition i2c.h:271
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
static bool i2c_idle(struct i2c_periph *p)
Check if I2C bus is idle.
Definition i2c.h:261
bool i2c_submit_fn_t(struct i2c_periph *p, struct i2c_transaction *t)
Definition i2c.h:145
void i2c_init(struct i2c_periph *p)
Initialize I2C peripheral.
Definition i2c.c:188
enum I2CTransactionStatus i2c_blocking_receive(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint16_t len, float timeout)
Submit a read only transaction and wait for it to complete.
Definition i2c.c:255
I2CStatus
I2C peripheral status.
Definition i2c.h:66
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
I2CTransactionStatus
I2C transaction status.
Definition i2c.h:55
enum I2CTransactionStatus i2c_blocking_transmit(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len, float timeout)
Submit a write only transaction and wait for it to complete.
Definition i2c.c:245
bool i2c_idle_fn_t(struct i2c_periph *p)
Definition i2c.h:144
enum I2CTransactionStatus i2c_blocking_transceive(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len_w, uint16_t len_r, float timeout)
Submit a write/read transaction and wait for it to complete.
Definition i2c.c:265
void i2c_setbitrate_fn_t(struct i2c_periph *p, int bitrate)
Definition i2c.h:146
I2CTransactionType
I2C transaction type.
Definition i2c.h:47
static void i2c_setbitrate(struct i2c_periph *p, int bitrate)
Set I2C bitrate.
Definition i2c.h:279
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
@ I2CStopRequested
Definition i2c.h:75
@ I2CAddrRdSent
Definition i2c.h:70
@ I2CFailed
Definition i2c.h:78
@ I2CComplete
Definition i2c.h:77
@ I2CAddrWrSent
Definition i2c.h:69
@ I2CIdle
Definition i2c.h:67
@ I2CStartRequested
Definition i2c.h:68
@ I2CReadingByte
Definition i2c.h:73
@ I2CSendingByte
Definition i2c.h:71
@ I2CReadingLastByte
Definition i2c.h:74
@ I2CRestartRequested
Definition i2c.h:76
@ I2CTransRunning
transaction is currently ongoing
Definition i2c.h:57
@ I2CTransSuccess
transaction successfully finished by I2C driver
Definition i2c.h:58
@ I2CTransFailed
transaction failed
Definition i2c.h:59
@ I2CTransDone
transaction set to done by user level
Definition i2c.h:60
@ I2CTransPending
transaction is pending in queue
Definition i2c.h:56
@ I2CTransRx
receive only transaction
Definition i2c.h:49
@ I2CTransTx
transmit only transaction
Definition i2c.h:48
@ I2CTransTxRx
transmit and receive transaction
Definition i2c.h:50
I2C errors counter.
Definition i2c.h:170
I2C transaction structure.
Definition i2c.h:94
static float p[2][2]
uint16_t foo
Definition main_demo5.c:58
static float timeout
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
short int16_t
Typedef defining 16 bit short type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.