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.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 
50 };
51 
60 };
61 
65 enum I2CStatus {
71  /* I2CSendingLastByte, */
78 };
79 
83 #ifndef I2C_BUF_LEN
84 #define I2C_BUF_LEN 32
85 #endif
86 
99 
105 
111 
117 
123 
127 };
128 
132 #ifndef I2C_TRANSACTION_QUEUE_LEN
133 #define I2C_TRANSACTION_QUEUE_LEN 8
134 #endif
135 
138 struct i2c_periph {
139  /* circular buffer holding transactions */
143  /* internal state of the peripheral */
144  volatile enum I2CStatus status;
145  volatile uint8_t idx_buf;
146  void *reg_addr;
147  void *init_struct;
149  volatile int16_t watchdog;
150 };
151 
154 struct i2c_errors {
167 };
168 
169 
170 #define ZEROS_ERR_COUNTER(_i2c_err) { \
171  _i2c_err.wd_reset_cnt = 0; \
172  _i2c_err.queue_full_cnt = 0; \
173  _i2c_err.ack_fail_cnt = 0; \
174  _i2c_err.miss_start_stop_cnt = 0; \
175  _i2c_err.arb_lost_cnt = 0; \
176  _i2c_err.over_under_cnt = 0; \
177  _i2c_err.pec_recep_cnt = 0; \
178  _i2c_err.timeout_tlow_cnt = 0; \
179  _i2c_err.smbus_alert_cnt = 0; \
180  _i2c_err.unexpected_event_cnt = 0; \
181  _i2c_err.last_unexpected_event = 0; \
182  _i2c_err.er_irq_cnt = 0; \
183  }
184 
185 
186 #if USE_I2C0
187 
188 extern struct i2c_periph i2c0;
189 extern void i2c0_init(void);
190 
191 #endif /* USE_I2C0 */
192 
193 
194 #if USE_I2C1
195 
196 extern struct i2c_periph i2c1;
197 extern void i2c1_init(void);
198 
199 #endif /* USE_I2C1 */
200 
201 
202 #if USE_I2C2
203 
204 extern struct i2c_periph i2c2;
205 extern void i2c2_init(void);
206 
207 #endif /* USE_I2C2 */
208 
209 
210 #if USE_I2C3
211 
212 extern struct i2c_periph i2c3;
213 extern void i2c3_init(void);
214 
215 #endif /* USE_I2C3 */
216 
217 
219 extern void i2c_init(struct i2c_periph *p);
220 
225 extern bool i2c_idle(struct i2c_periph *p);
226 
233 extern bool i2c_submit(struct i2c_periph *p, struct i2c_transaction *t);
234 
239 extern void i2c_setbitrate(struct i2c_periph *p, int bitrate);
240 extern void i2c_event(void);
241 
242 /*
243  * Convenience functions.
244  * Usually these are preferred over i2c_submit,
245  * as they explicitly set the transaction type again.
246  *
247  * Return FALSE if submitting the transaction failed.
248  */
258 extern bool i2c_transmit(struct i2c_periph *p, struct i2c_transaction *t,
259  uint8_t s_addr, uint8_t len);
260 
270 extern bool i2c_receive(struct i2c_periph *p, struct i2c_transaction *t,
271  uint8_t s_addr, uint16_t len);
272 
283 extern bool i2c_transceive(struct i2c_periph *p, struct i2c_transaction *t,
284  uint8_t s_addr, uint8_t len_w, uint16_t len_r);
285 
295 bool i2c_blocking_transmit(struct i2c_periph *p, struct i2c_transaction *t,
296  uint8_t s_addr, uint8_t len);
297 
307 bool i2c_blocking_receive(struct i2c_periph *p, struct i2c_transaction *t,
308  uint8_t s_addr, uint16_t len);
309 
320 bool i2c_blocking_transceive(struct i2c_periph *p, struct i2c_transaction *t,
321  uint8_t s_addr, uint8_t len_w, uint16_t len_r);
325 #endif /* I2C_H */
unsigned short uint16_t
Definition: types.h:16
volatile uint16_t arb_lost_cnt
Definition: i2c.h:159
volatile uint8_t idx_buf
Definition: i2c.h:145
I2CTransactionStatus
I2C transaction status.
Definition: i2c.h:54
#define I2C_TRANSACTION_QUEUE_LEN
I2C transaction queue length.
Definition: i2c.h:133
I2C errors counter.
Definition: i2c.h:154
I2CTransactionType
I2C transaction type.
Definition: i2c.h:46
volatile uint16_t miss_start_stop_cnt
Definition: i2c.h:158
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
uint16_t len_r
Number of bytes to read/receive.
Definition: i2c.h:110
transaction successfully finished by I2C driver
Definition: i2c.h:57
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
bool i2c_idle(struct i2c_periph *p)
Check if I2C bus is idle.
Definition: i2c_arch.c:413
Definition: i2c.h:66
volatile uint16_t queue_full_cnt
Definition: i2c.h:156
void i2c_setbitrate(struct i2c_periph *p, int bitrate)
Set I2C bitrate.
Definition: i2c_arch.c:355
struct i2c_errors * errors
Definition: i2c.h:148
enum I2CStatus status
Definition: i2c.h:144
volatile uint32_t er_irq_cnt
Definition: i2c.h:166
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
transaction set to done by user level
Definition: i2c.h:59
unsigned long uint32_t
Definition: types.h:18
signed short int16_t
Definition: types.h:17
struct i2c_transaction * trans[I2C_TRANSACTION_QUEUE_LEN]
Definition: i2c.h:140
volatile uint16_t over_under_cnt
Definition: i2c.h:160
bool i2c_submit(struct i2c_periph *p, struct i2c_transaction *t)
Submit a I2C transaction.
Definition: i2c_arch.c:374
transaction failed
Definition: i2c.h:58
Definition: i2c.h:77
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
void * init_struct
Definition: i2c.h:147
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
volatile uint16_t unexpected_event_cnt
Definition: i2c.h:164
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
#define I2C_BUF_LEN
I2C buffer length.
Definition: i2c.h:84
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
volatile int16_t watchdog
Definition: i2c.h:149
volatile uint32_t last_unexpected_event
Definition: i2c.h:165
static float p[2][2]
volatile uint16_t wd_reset_cnt
Definition: i2c.h:155
volatile uint16_t smbus_alert_cnt
Definition: i2c.h:163
void i2c_event(void)
i2c_event() function
Definition: i2c_arch.c:347
enum I2CTransactionType type
Transaction type.
Definition: i2c.h:98
uint8_t trans_insert_idx
Definition: i2c.h:141
volatile uint16_t pec_recep_cnt
Definition: i2c.h:161
receive only transaction
Definition: i2c.h:48
transaction is pending in queue
Definition: i2c.h:55
I2CStatus
I2C peripheral status.
Definition: i2c.h:65
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
volatile uint16_t ack_fail_cnt
Definition: i2c.h:157
volatile uint16_t timeout_tlow_cnt
Definition: i2c.h:162