Paparazzi UAS  v4.2.2_stable-4-gcc32f65
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
i2c.h
Go to the documentation of this file.
1 #ifndef MCU_PERIPH_I2C_H
2 #define MCU_PERIPH_I2C_H
3 
4 #include "std.h"
5 
6 #include "mcu_periph/i2c_arch.h"
7 
12 };
13 
20 };
21 
22 enum I2CStatus {
28  /* I2CSendingLastByte, */
35 };
36 
37 #ifndef I2C_BUF_LEN
38 #define I2C_BUF_LEN 32
39 #endif
40 
46  volatile uint8_t buf[I2C_BUF_LEN];
48 };
49 
50 #ifndef I2C_TRANSACTION_QUEUE_LEN
51 #define I2C_TRANSACTION_QUEUE_LEN 8
52 #endif
53 
54 struct i2c_periph {
55  /* circular buffer holding transactions */
59  /* internal state of the peripheral */
60  volatile enum I2CStatus status;
61  volatile uint8_t idx_buf;
62  void* reg_addr;
63  void *init_struct;
66  struct i2c_errors *errors;
67 };
68 
69 
70 struct i2c_errors {
80  volatile uint32_t er_irq_cnt;
81  volatile uint32_t irq_cnt;
82  volatile uint32_t event_chain[16];
83  volatile enum I2CStatus status_chain[16];
84 };
85 
86 
87 #include <string.h>
88 #define I2C_ZERO_EVENTS(_err) { \
89  _err.irq_cnt = 0; \
90  memset((void*)_err.event_chain, 0, sizeof(_err.event_chain)); \
91  memset((void*)_err.status_chain, 0, sizeof(_err.status_chain)); \
92  }
93 
94 #define ZEROS_ERR_COUNTER(_i2c_err) { \
95  _i2c_err.ack_fail_cnt = 0; \
96  _i2c_err.miss_start_stop_cnt = 0; \
97  _i2c_err.arb_lost_cnt = 0; \
98  _i2c_err.over_under_cnt = 0; \
99  _i2c_err.pec_recep_cnt = 0; \
100  _i2c_err.timeout_tlow_cnt = 0; \
101  _i2c_err.smbus_alert_cnt = 0; \
102  _i2c_err.unexpected_event_cnt = 0; \
103  _i2c_err.last_unexpected_event = 0; \
104  _i2c_err.er_irq_cnt = 0; \
105  }
106 
107 
108 #ifdef USE_I2C0
109 
110 extern struct i2c_periph i2c0;
111 extern void i2c0_init(void);
112 
113 #endif /* USE_I2C0 */
114 
115 
116 
117 #ifdef USE_I2C1
118 
119 extern struct i2c_periph i2c1;
120 extern void i2c1_init(void);
121 
122 #endif /* USE_I2C1 */
123 
124 
125 
126 #ifdef USE_I2C2
127 
128 extern struct i2c_periph i2c2;
129 extern void i2c2_init(void);
130 
131 #endif /* USE_I2C2 */
132 
133 extern void i2c_init(struct i2c_periph* p);
134 extern bool_t i2c_idle(struct i2c_periph* p);
135 extern bool_t i2c_submit(struct i2c_periph* p, struct i2c_transaction* t);
136 extern void i2c_setbitrate(struct i2c_periph* p, int bitrate);
137 extern void i2c_event(void);
138 
139 
140 #define I2CReceive(_p, _t, _s_addr, _len) { \
141  _t.type = I2CTransRx; \
142  _t.slave_addr = _s_addr; \
143  _t.len_r = _len; \
144  _t.len_w = 0; \
145  i2c_submit(&(_p),&(_t)); \
146 }
147 
148 #define I2CTransmit(_p, _t, _s_addr, _len) { \
149  _t.type = I2CTransTx; \
150  _t.slave_addr = _s_addr; \
151  _t.len_r = 0; \
152  _t.len_w = _len; \
153  i2c_submit(&(_p),&(_t)); \
154 }
155 
156 #define I2CTransceive(_p, _t, _s_addr, _len_w, _len_r) { \
157  _t.type = I2CTransTxRx; \
158  _t.slave_addr = _s_addr; \
159  _t.len_r = _len_r; \
160  _t.len_w = _len_w; \
161  i2c_submit(&(_p),&(_t)); \
162 }
163 
164 
165 #endif /* I2C_H */
unsigned short uint16_t
Definition: types.h:16
I2CTransactionStatus
Definition: i2c.h:14
#define I2C_BUF_LEN
Definition: i2c.h:38
bool_t i2c_idle(struct i2c_periph *p)
Definition: i2c_arch.c:317
bool_t i2c_submit(struct i2c_periph *p, struct i2c_transaction *t)
Definition: i2c_arch.c:321
I2CStatus
Definition: i2c.h:22
void * reg_addr
Definition: i2c.h:62
uint8_t trans_extract_idx
Definition: i2c.h:58
volatile uint16_t ack_fail_cnt
Definition: i2c.h:71
void i2c_event(void)
Definition: i2c_arch.c:344
volatile uint32_t event_chain[16]
Definition: i2c.h:82
volatile uint8_t idx_buf
Definition: i2c.h:61
enum I2CStatus status
Definition: i2c.h:60
uint8_t slave_addr
Definition: i2c.h:43
volatile uint16_t smbus_alert_cnt
Definition: i2c.h:77
volatile uint16_t timeout_tlow_cnt
Definition: i2c.h:76
void * init_struct
Definition: i2c.h:63
Definition: i2c.h:23
volatile uint16_t miss_start_stop_cnt
Definition: i2c.h:72
uint16_t scl_pin
Definition: i2c.h:64
unsigned long uint32_t
Definition: types.h:18
void i2c_setbitrate(struct i2c_periph *p, int bitrate)
Definition: i2c_arch.c:346
uint16_t sda_pin
Definition: i2c.h:65
volatile uint16_t unexpected_event_cnt
Definition: i2c.h:78
Definition: i2c.h:70
enum I2CTransactionStatus status
Definition: i2c.h:47
volatile uint16_t over_under_cnt
Definition: i2c.h:74
volatile uint16_t pec_recep_cnt
Definition: i2c.h:75
volatile uint32_t er_irq_cnt
Definition: i2c.h:80
volatile uint8_t buf[I2C_BUF_LEN]
Definition: i2c.h:46
uint8_t len_w
Definition: i2c.h:45
uint16_t len_r
Definition: i2c.h:44
unsigned char uint8_t
Definition: types.h:14
volatile uint16_t arb_lost_cnt
Definition: i2c.h:73
Definition: i2c.h:34
uint8_t trans_insert_idx
Definition: i2c.h:57
struct i2c_errors * errors
Definition: i2c.h:66
Definition: i2c.h:54
enum I2CStatus status_chain[16]
Definition: i2c.h:83
I2CTransactionType
Definition: i2c.h:8
enum I2CTransactionType type
Definition: i2c.h:42
Definition: i2c.h:10
void i2c_init(struct i2c_periph *p)
Definition: i2c.c:38
#define I2C_TRANSACTION_QUEUE_LEN
Definition: i2c.h:51
volatile uint32_t irq_cnt
Definition: i2c.h:81
Definition: i2c.h:9
volatile uint32_t last_unexpected_event
Definition: i2c.h:79
struct i2c_transaction * trans[I2C_TRANSACTION_QUEUE_LEN]
Definition: i2c.h:56