Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
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 typedef bool i2c_idle_fn_t(struct i2c_periph *p);
140 typedef bool i2c_submit_fn_t(struct i2c_periph *p, struct i2c_transaction *t);
141 typedef void i2c_setbitrate_fn_t(struct i2c_periph *p, int bitrate);
142 typedef 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 
144 struct i2c_periph {
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;
158  void *init_struct;
160  volatile int16_t watchdog;
161 };
162 
165 struct i2c_errors {
178 };
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 
199 extern struct i2c_periph i2c0;
200 extern void i2c0_init(void);
201 
202 #endif /* USE_I2C0 */
203 
204 
205 #if USE_I2C1
206 
207 extern struct i2c_periph i2c1;
208 extern void i2c1_init(void);
209 
210 #endif /* USE_I2C1 */
211 
212 
213 #if USE_I2C2
214 
215 extern struct i2c_periph i2c2;
216 extern void i2c2_init(void);
217 
218 #endif /* USE_I2C2 */
219 
220 
221 #if USE_I2C3
222 
223 extern struct i2c_periph i2c3;
224 extern void i2c3_init(void);
225 
226 #endif /* USE_I2C3 */
227 
228 
229 #if USE_I2C4
230 
231 extern struct i2c_periph i2c4;
232 extern void i2c4_init(void);
233 
234 #endif /* USE_I2C4 */
235 
236 
237 #if USE_SOFTI2C0
238 extern struct i2c_periph softi2c0;
239 extern void softi2c0_init(void);
240 #endif /* USE_SOFTI2C0 */
241 
242 
243 #if USE_SOFTI2C1
244 extern struct i2c_periph softi2c1;
245 extern void softi2c1_init(void);
246 #endif /* USE_SOFTI2C1 */
247 
248 
250 extern void i2c_init(struct i2c_periph *p);
251 
256 static inline bool i2c_idle(struct i2c_periph *p) {
257  return p->idle(p);
258 }
259 
266 static inline bool i2c_submit(struct i2c_periph *p, struct i2c_transaction *t) {
267  return p->submit(p, t);
268 }
269 
274 static inline void i2c_setbitrate(struct i2c_periph *p, int bitrate) {
275  p->setbitrate(p, bitrate);
276 }
277 
278 extern 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  */
296 extern bool i2c_transmit(struct i2c_periph *p, struct i2c_transaction *t,
297  uint8_t s_addr, uint8_t len);
298 
308 extern bool i2c_receive(struct i2c_periph *p, struct i2c_transaction *t,
309  uint8_t s_addr, uint16_t len);
310 
321 extern 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 
333 bool i2c_blocking_transmit(struct i2c_periph *p, struct i2c_transaction *t,
334  uint8_t s_addr, uint8_t len);
335 
345 bool i2c_blocking_receive(struct i2c_periph *p, struct i2c_transaction *t,
346  uint8_t s_addr, uint16_t len);
347 
358 bool i2c_blocking_transceive(struct i2c_periph *p, struct i2c_transaction *t,
359  uint8_t s_addr, uint8_t len_w, uint16_t len_r);
363 #endif /* I2C_H */
I2CRestartRequested
@ I2CRestartRequested
Definition: i2c.h:75
I2CTransactionType
I2CTransactionType
I2C transaction type.
Definition: i2c.h:46
i2c_errors::ack_fail_cnt
volatile uint16_t ack_fail_cnt
Definition: i2c.h:168
uint16_t
unsigned short uint16_t
Definition: types.h:16
i2c_transaction::buf
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
i2c_errors::miss_start_stop_cnt
volatile uint16_t miss_start_stop_cnt
Definition: i2c.h:169
I2CReadingByte
@ I2CReadingByte
Definition: i2c.h:72
i2c_periph::spin
i2c_spin_fn_t * spin
Definition: i2c.h:149
i2c_transaction::len_r
uint16_t len_r
Number of bytes to read/receive.
Definition: i2c.h:110
i2c_errors::arb_lost_cnt
volatile uint16_t arb_lost_cnt
Definition: i2c.h:170
i2c_setbitrate_fn_t
void i2c_setbitrate_fn_t(struct i2c_periph *p, int bitrate)
Definition: i2c.h:141
I2CStartRequested
@ I2CStartRequested
Definition: i2c.h:67
I2CTransTx
@ I2CTransTx
transmit only transaction
Definition: i2c.h:47
i2c_init
void i2c_init(struct i2c_periph *p)
Initialize I2C peripheral.
Definition: i2c.c:310
i2c_event
void i2c_event(void)
i2c_event() function
Definition: i2c_arch.c:428
i2c_errors::queue_full_cnt
volatile uint16_t queue_full_cnt
Definition: i2c.h:167
i2c_periph::errors
struct i2c_errors * errors
Definition: i2c.h:159
I2CTransFailed
@ I2CTransFailed
transaction failed
Definition: i2c.h:58
uint32_t
unsigned long uint32_t
Definition: types.h:18
i2c_blocking_transceive
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:403
I2CFailed
@ I2CFailed
Definition: i2c.h:77
i2c_periph::trans_extract_idx
uint8_t trans_extract_idx
Definition: i2c.h:153
i2c_idle
static bool i2c_idle(struct i2c_periph *p)
Check if I2C bus is idle.
Definition: i2c.h:256
I2CComplete
@ I2CComplete
Definition: i2c.h:76
i2c_errors
I2C errors counter.
Definition: i2c.h:165
i2c_periph::reg_addr
void * reg_addr
Definition: i2c.h:157
i2c_transaction::len_w
uint8_t len_w
Number of bytes to write/transmit.
Definition: i2c.h:116
i2c_periph::status
enum I2CStatus status
Definition: i2c.h:155
i2c_errors::er_irq_cnt
volatile uint32_t er_irq_cnt
Definition: i2c.h:177
I2CTransSuccess
@ I2CTransSuccess
transaction successfully finished by I2C driver
Definition: i2c.h:57
std.h
i2c_idle_fn_t
bool i2c_idle_fn_t(struct i2c_periph *p)
Definition: i2c.h:139
i2c_periph::setbitrate
i2c_setbitrate_fn_t * setbitrate
Definition: i2c.h:148
i2c_transmit
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:324
I2CSendingByte
@ I2CSendingByte
Definition: i2c.h:70
I2CTransRx
@ I2CTransRx
receive only transaction
Definition: i2c.h:48
I2CAddrWrSent
@ I2CAddrWrSent
Definition: i2c.h:68
I2C_BUF_LEN
#define I2C_BUF_LEN
I2C buffer length.
Definition: i2c.h:84
I2CStopRequested
@ I2CStopRequested
Definition: i2c.h:74
i2c_periph::trans
struct i2c_transaction * trans[I2C_TRANSACTION_QUEUE_LEN]
Definition: i2c.h:151
i2c_transceive
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:344
I2CTransRunning
@ I2CTransRunning
transaction is currently ongoing
Definition: i2c.h:56
int16_t
signed short int16_t
Definition: types.h:17
uint8_t
unsigned char uint8_t
Definition: types.h:14
i2c_transaction::status
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
i2c_periph::init_struct
void * init_struct
Definition: i2c.h:158
i2c_errors::unexpected_event_cnt
volatile uint16_t unexpected_event_cnt
Definition: i2c.h:175
i2c_errors::over_under_cnt
volatile uint16_t over_under_cnt
Definition: i2c.h:171
I2CIdle
@ I2CIdle
Definition: i2c.h:66
i2c_blocking_receive
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:381
I2CTransTxRx
@ I2CTransTxRx
transmit and receive transaction
Definition: i2c.h:49
i2c_transaction
I2C transaction structure.
Definition: i2c.h:93
i2c_transaction::slave_addr
uint8_t slave_addr
Slave address.
Definition: i2c.h:104
i2c_setbitrate
static void i2c_setbitrate(struct i2c_periph *p, int bitrate)
Set I2C bitrate.
Definition: i2c.h:274
i2c_periph::submit
i2c_submit_fn_t * submit
Definition: i2c.h:147
I2CTransactionStatus
I2CTransactionStatus
I2C transaction status.
Definition: i2c.h:54
I2CReadingLastByte
@ I2CReadingLastByte
Definition: i2c.h:73
I2C_TRANSACTION_QUEUE_LEN
#define I2C_TRANSACTION_QUEUE_LEN
I2C transaction queue length.
Definition: i2c.h:133
I2CAddrRdSent
@ I2CAddrRdSent
Definition: i2c.h:69
i2c_errors::smbus_alert_cnt
volatile uint16_t smbus_alert_cnt
Definition: i2c.h:174
I2CTransPending
@ I2CTransPending
transaction is pending in queue
Definition: i2c.h:55
i2c_spin_fn_t
void i2c_spin_fn_t(struct i2c_periph *p)
Definition: i2c.h:142
I2CStatus
I2CStatus
I2C peripheral status.
Definition: i2c.h:65
i2c_submit_fn_t
bool i2c_submit_fn_t(struct i2c_periph *p, struct i2c_transaction *t)
Definition: i2c.h:140
i2c_periph::idle
i2c_idle_fn_t * idle
Definition: i2c.h:146
I2CTransDone
@ I2CTransDone
transaction set to done by user level
Definition: i2c.h:59
i2c_periph::watchdog
volatile int16_t watchdog
Definition: i2c.h:160
i2c_errors::last_unexpected_event
volatile uint32_t last_unexpected_event
Definition: i2c.h:176
i2c_submit
static bool i2c_submit(struct i2c_periph *p, struct i2c_transaction *t)
Submit a I2C transaction.
Definition: i2c.h:266
i2c_errors::wd_reset_cnt
volatile uint16_t wd_reset_cnt
Definition: i2c.h:166
i2c_periph
Definition: i2c.h:144
i2c_periph::trans_insert_idx
uint8_t trans_insert_idx
Definition: i2c.h:152
i2c_transaction::type
enum I2CTransactionType type
Transaction type.
Definition: i2c.h:98
i2c_blocking_transmit
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:359
i2c_errors::timeout_tlow_cnt
volatile uint16_t timeout_tlow_cnt
Definition: i2c.h:173
i2c_errors::pec_recep_cnt
volatile uint16_t pec_recep_cnt
Definition: i2c.h:172
p
static float p[2][2]
Definition: ins_alt_float.c:268
i2c_receive
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:334
i2c_periph::idx_buf
volatile uint8_t idx_buf
Definition: i2c.h:156