Paparazzi UAS  v5.2.2_stable-0-gd6b9f29
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures 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 {
166  volatile uint32_t irq_cnt;
167  volatile uint32_t event_chain[16];
168  volatile enum I2CStatus status_chain[16];
169 };
170 
171 
172 #include <string.h>
173 #define I2C_ZERO_EVENTS(_err) { \
174  _err.irq_cnt = 0; \
175  memset((void*)_err.event_chain, 0, sizeof(_err.event_chain)); \
176  memset((void*)_err.status_chain, 0, sizeof(_err.status_chain)); \
177  }
178 
179 #define ZEROS_ERR_COUNTER(_i2c_err) { \
180  _i2c_err.queue_full_cnt = 0; \
181  _i2c_err.ack_fail_cnt = 0; \
182  _i2c_err.miss_start_stop_cnt = 0; \
183  _i2c_err.arb_lost_cnt = 0; \
184  _i2c_err.over_under_cnt = 0; \
185  _i2c_err.pec_recep_cnt = 0; \
186  _i2c_err.timeout_tlow_cnt = 0; \
187  _i2c_err.smbus_alert_cnt = 0; \
188  _i2c_err.unexpected_event_cnt = 0; \
189  _i2c_err.last_unexpected_event = 0; \
190  _i2c_err.er_irq_cnt = 0; \
191  }
192 
193 
194 #if USE_I2C0
195 
196 extern struct i2c_periph i2c0;
197 extern void i2c0_init(void);
198 
199 #endif /* USE_I2C0 */
200 
201 
202 #if USE_I2C1
203 
204 extern struct i2c_periph i2c1;
205 extern void i2c1_init(void);
206 
207 #endif /* USE_I2C1 */
208 
209 
210 #if USE_I2C2
211 
212 extern struct i2c_periph i2c2;
213 extern void i2c2_init(void);
214 
215 #endif /* USE_I2C2 */
216 
217 
218 #if USE_I2C3
219 
220 extern struct i2c_periph i2c3;
221 extern void i2c3_init(void);
222 
223 #endif /* USE_I2C3 */
224 
225 
227 extern void i2c_init(struct i2c_periph* p);
228 
233 extern bool_t i2c_idle(struct i2c_periph* p);
234 
241 extern bool_t i2c_submit(struct i2c_periph* p, struct i2c_transaction* t);
242 
247 extern void i2c_setbitrate(struct i2c_periph* p, int bitrate);
248 extern void i2c_event(void);
249 
250 /*
251  * Convenience functions.
252  * Usually these are preferred over i2c_submit,
253  * as they explicitly set the transaction type again.
254  *
255  * Return FALSE if submitting the transaction failed.
256  */
266 extern bool_t i2c_transmit(struct i2c_periph* p, struct i2c_transaction* t,
267  uint8_t s_addr, uint8_t len);
268 
278 extern bool_t i2c_receive(struct i2c_periph* p, struct i2c_transaction* t,
279  uint8_t s_addr, uint16_t len);
280 
291 extern bool_t i2c_transceive(struct i2c_periph* p, struct i2c_transaction* t,
292  uint8_t s_addr, uint8_t len_w, uint16_t len_r);
293 
297 #endif /* I2C_H */
unsigned short uint16_t
Definition: types.h:16
I2CTransactionStatus
I2C transaction status.
Definition: i2c.h:54
#define I2C_TRANSACTION_QUEUE_LEN
I2C transaction queue length.
Definition: i2c.h:133
I2CTransactionType
I2C transaction type.
Definition: i2c.h:46
bool_t i2c_submit(struct i2c_periph *p, struct i2c_transaction *t)
Submit a I2C transaction.
Definition: i2c_arch.c:335
volatile uint16_t queue_full_cnt
Definition: i2c.h:155
transaction successfully finished by I2C driver
Definition: i2c.h:57
I2C transaction structure.
Definition: i2c.h:93
void * reg_addr
Definition: i2c.h:146
uint8_t trans_extract_idx
Definition: i2c.h:142
volatile uint16_t ack_fail_cnt
Definition: i2c.h:156
transmit and receive transaction
Definition: i2c.h:49
void i2c_init(struct i2c_periph *p)
Initialize I2C peripheral.
Definition: i2c.c:226
volatile uint32_t event_chain[16]
Definition: i2c.h:167
Definition: i2c.h:66
void i2c_setbitrate(struct i2c_periph *p, int bitrate)
Set I2C bitrate.
Definition: i2c_arch.c:370
bool_t 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:258
volatile uint8_t idx_buf
Definition: i2c.h:145
enum I2CStatus status
Definition: i2c.h:144
uint8_t slave_addr
Slave address.
Definition: i2c.h:104
volatile uint16_t smbus_alert_cnt
Definition: i2c.h:162
volatile uint16_t timeout_tlow_cnt
Definition: i2c.h:161
void * init_struct
Definition: i2c.h:147
transaction set to done by user level
Definition: i2c.h:59
volatile uint16_t miss_start_stop_cnt
Definition: i2c.h:157
bool_t 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:238
unsigned long uint32_t
Definition: types.h:18
signed short int16_t
Definition: types.h:17
volatile uint16_t unexpected_event_cnt
Definition: i2c.h:163
transaction failed
Definition: i2c.h:58
Definition: i2c.h:77
I2C errors counter.
Definition: i2c.h:154
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
volatile uint16_t over_under_cnt
Definition: i2c.h:159
volatile uint16_t pec_recep_cnt
Definition: i2c.h:160
volatile uint32_t er_irq_cnt
Definition: i2c.h:165
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
uint8_t len_w
Number of bytes to write/transmit.
Definition: i2c.h:116
uint16_t len_r
Number of bytes to read/receive.
Definition: i2c.h:110
unsigned char uint8_t
Definition: types.h:14
volatile uint16_t arb_lost_cnt
Definition: i2c.h:158
uint8_t trans_insert_idx
Definition: i2c.h:141
#define I2C_BUF_LEN
I2C buffer length.
Definition: i2c.h:84
struct i2c_errors * errors
Definition: i2c.h:148
I2C peripheral structure.
Definition: i2c.h:138
transaction is currently ongoing
Definition: i2c.h:56
enum I2CStatus status_chain[16]
Definition: i2c.h:168
transmit only transaction
Definition: i2c.h:47
enum I2CTransactionType type
Transaction type.
Definition: i2c.h:98
static float p[2][2]
void i2c_event(void)
Definition: i2c_arch.c:368
volatile uint32_t irq_cnt
Definition: i2c.h:166
receive only transaction
Definition: i2c.h:48
bool_t i2c_idle(struct i2c_periph *p)
Check if I2C bus is idle.
Definition: i2c_arch.c:331
volatile uint32_t last_unexpected_event
Definition: i2c.h:164
transaction is pending in queue
Definition: i2c.h:55
bool_t 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:248
I2CStatus
I2C peripheral status.
Definition: i2c.h:65
struct i2c_transaction * trans[I2C_TRANSACTION_QUEUE_LEN]
Definition: i2c.h:140
volatile int16_t watchdog
Definition: i2c.h:149