Paparazzi UAS v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
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"
36
51
61
79
83#ifndef I2C_BUF_LEN
84#define I2C_BUF_LEN 32
85#endif
86
128
132#ifndef I2C_TRANSACTION_QUEUE_LEN
133#define I2C_TRANSACTION_QUEUE_LEN 8
134#endif
135
138struct i2c_periph;
139typedef bool i2c_idle_fn_t(struct i2c_periph *p);
140typedef bool i2c_submit_fn_t(struct i2c_periph *p, struct i2c_transaction *t);
141typedef void i2c_setbitrate_fn_t(struct i2c_periph *p, int bitrate);
142typedef 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.
143
145 /* architecture-specific functions */
150 /* circular buffer holding transactions */
154 /* internal state of the peripheral */
155 volatile enum I2CStatus status;
156 volatile uint8_t idx_buf;
157 void *reg_addr;
161};
162
179
180
181#define ZEROS_ERR_COUNTER(_i2c_err) { \
182 _i2c_err.wd_reset_cnt = 0; \
183 _i2c_err.queue_full_cnt = 0; \
184 _i2c_err.ack_fail_cnt = 0; \
185 _i2c_err.miss_start_stop_cnt = 0; \
186 _i2c_err.arb_lost_cnt = 0; \
187 _i2c_err.over_under_cnt = 0; \
188 _i2c_err.pec_recep_cnt = 0; \
189 _i2c_err.timeout_tlow_cnt = 0; \
190 _i2c_err.smbus_alert_cnt = 0; \
191 _i2c_err.unexpected_event_cnt = 0; \
192 _i2c_err.last_unexpected_event = 0; \
193 _i2c_err.er_irq_cnt = 0; \
194 }
195
196
197#if USE_I2C0
198
199extern struct i2c_periph i2c0;
200extern void i2c0_init(void);
201
202#endif /* USE_I2C0 */
203
204
205#if USE_I2C1
206
207extern struct i2c_periph i2c1;
208extern void i2c1_init(void);
209
210#endif /* USE_I2C1 */
211
212
213#if USE_I2C2
214
215extern struct i2c_periph i2c2;
216extern void i2c2_init(void);
217
218#endif /* USE_I2C2 */
219
220
221#if USE_I2C3
222
223extern struct i2c_periph i2c3;
224extern void i2c3_init(void);
225
226#endif /* USE_I2C3 */
227
228
229#if USE_I2C4
230
231extern struct i2c_periph i2c4;
232extern void i2c4_init(void);
233
234#endif /* USE_I2C4 */
235
236
237#if USE_SOFTI2C0
238extern struct i2c_periph softi2c0;
239extern void softi2c0_init(void);
240#endif /* USE_SOFTI2C0 */
241
242
243#if USE_SOFTI2C1
244extern struct i2c_periph softi2c1;
245extern void softi2c1_init(void);
246#endif /* USE_SOFTI2C1 */
247
248
250extern void i2c_init(struct i2c_periph *p);
251
256static inline bool i2c_idle(struct i2c_periph *p) {
257 return p->idle(p);
258}
259
266static inline bool i2c_submit(struct i2c_periph *p, struct i2c_transaction *t) {
267 return p->submit(p, t);
268}
269
274static inline void i2c_setbitrate(struct i2c_periph *p, int bitrate) {
275 p->setbitrate(p, bitrate);
276}
277
278extern void i2c_event(void);
279
280/*
281 * Convenience functions.
282 * Usually these are preferred over i2c_submit,
283 * as they explicitly set the transaction type again.
284 *
285 * Return FALSE if submitting the transaction failed.
286 */
296extern bool i2c_transmit(struct i2c_periph *p, struct i2c_transaction *t,
297 uint8_t s_addr, uint8_t len);
298
308extern bool i2c_receive(struct i2c_periph *p, struct i2c_transaction *t,
309 uint8_t s_addr, uint16_t len);
310
321extern bool i2c_transceive(struct i2c_periph *p, struct i2c_transaction *t,
322 uint8_t s_addr, uint8_t len_w, uint16_t len_r);
323
334 uint8_t s_addr, uint8_t len);
335
346 uint8_t s_addr, uint16_t len);
347
359 uint8_t s_addr, uint8_t len_w, uint16_t len_r);
363#endif /* I2C_H */
volatile uint16_t pec_recep_cnt
Definition i2c.h:172
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition i2c.h:122
volatile uint16_t smbus_alert_cnt
Definition i2c.h:174
struct i2c_errors * errors
Definition i2c.h:159
i2c_spin_fn_t * spin
Definition i2c.h:149
uint8_t trans_extract_idx
Definition i2c.h:153
uint16_t len_r
Number of bytes to read/receive.
Definition i2c.h:110
volatile uint16_t ack_fail_cnt
Definition i2c.h:168
i2c_submit_fn_t * submit
Definition i2c.h:147
uint8_t trans_insert_idx
Definition i2c.h:152
volatile uint16_t timeout_tlow_cnt
Definition i2c.h:173
void * init_struct
Definition i2c.h:158
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 uint32_t er_irq_cnt
Definition i2c.h:177
i2c_setbitrate_fn_t * setbitrate
Definition i2c.h:148
volatile uint16_t wd_reset_cnt
Definition i2c.h:166
enum I2CTransactionType type
Transaction type.
Definition i2c.h:98
void * reg_addr
Definition i2c.h:157
enum I2CTransactionStatus status
Transaction status.
Definition i2c.h:126
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
i2c_idle_fn_t * idle
Definition i2c.h:146
volatile int16_t watchdog
Definition i2c.h:160
volatile uint8_t idx_buf
Definition i2c.h:156
uint8_t slave_addr
Slave address.
Definition i2c.h:104
enum I2CStatus status
Definition i2c.h:155
volatile uint16_t arb_lost_cnt
Definition i2c.h:170
uint8_t len_w
Number of bytes to write/transmit.
Definition i2c.h:116
void i2c_event(void)
i2c_event() function
Definition i2c_arch.c:393
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
#define I2C_TRANSACTION_QUEUE_LEN
I2C transaction queue length.
Definition i2c.h:133
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
#define I2C_BUF_LEN
I2C buffer length.
Definition i2c.h:84
void i2c_spin_fn_t(struct i2c_periph *p)
Definition i2c.h:142
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
static bool i2c_idle(struct i2c_periph *p)
Check if I2C bus is idle.
Definition i2c.h:256
bool i2c_submit_fn_t(struct i2c_periph *p, struct i2c_transaction *t)
Definition i2c.h:140
void i2c_init(struct i2c_periph *p)
Initialize I2C peripheral.
Definition i2c.c:188
I2CStatus
I2C peripheral status.
Definition i2c.h:65
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:54
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_idle_fn_t(struct i2c_periph *p)
Definition i2c.h:139
void i2c_setbitrate_fn_t(struct i2c_periph *p, int bitrate)
Definition i2c.h:141
I2CTransactionType
I2C transaction type.
Definition i2c.h:46
static void i2c_setbitrate(struct i2c_periph *p, int bitrate)
Set I2C bitrate.
Definition i2c.h:274
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:74
@ I2CAddrRdSent
Definition i2c.h:69
@ I2CFailed
Definition i2c.h:77
@ I2CComplete
Definition i2c.h:76
@ I2CAddrWrSent
Definition i2c.h:68
@ I2CIdle
Definition i2c.h:66
@ I2CStartRequested
Definition i2c.h:67
@ I2CReadingByte
Definition i2c.h:72
@ I2CSendingByte
Definition i2c.h:70
@ I2CReadingLastByte
Definition i2c.h:73
@ I2CRestartRequested
Definition i2c.h:75
@ I2CTransRunning
transaction is currently ongoing
Definition i2c.h:56
@ I2CTransSuccess
transaction successfully finished by I2C driver
Definition i2c.h:57
@ I2CTransFailed
transaction failed
Definition i2c.h:58
@ I2CTransDone
transaction set to done by user level
Definition i2c.h:59
@ 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 errors counter.
Definition i2c.h:165
I2C transaction structure.
Definition i2c.h:93
static float p[2][2]
uint16_t foo
Definition main_demo5.c:58
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.