Paparazzi UAS  v5.0.5_stable-7-g4b8bbb7
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 
48 };
49 
56 };
57 
58 enum I2CStatus {
64  /* I2CSendingLastByte, */
71 };
72 
73 #ifndef I2C_BUF_LEN
74 #define I2C_BUF_LEN 32
75 #endif
76 
82  volatile uint8_t buf[I2C_BUF_LEN];
84 };
85 
86 #ifndef I2C_TRANSACTION_QUEUE_LEN
87 #define I2C_TRANSACTION_QUEUE_LEN 8
88 #endif
89 
90 struct i2c_periph {
91  /* circular buffer holding transactions */
95  /* internal state of the peripheral */
96  volatile enum I2CStatus status;
97  volatile uint8_t idx_buf;
98  void* reg_addr;
99  void *init_struct;
101 };
102 
103 
104 struct i2c_errors {
115  volatile uint32_t irq_cnt;
116  volatile uint32_t event_chain[16];
117  volatile enum I2CStatus status_chain[16];
118 };
119 
120 
121 #include <string.h>
122 #define I2C_ZERO_EVENTS(_err) { \
123  _err.irq_cnt = 0; \
124  memset((void*)_err.event_chain, 0, sizeof(_err.event_chain)); \
125  memset((void*)_err.status_chain, 0, sizeof(_err.status_chain)); \
126  }
127 
128 #define ZEROS_ERR_COUNTER(_i2c_err) { \
129  _i2c_err.ack_fail_cnt = 0; \
130  _i2c_err.miss_start_stop_cnt = 0; \
131  _i2c_err.arb_lost_cnt = 0; \
132  _i2c_err.over_under_cnt = 0; \
133  _i2c_err.pec_recep_cnt = 0; \
134  _i2c_err.timeout_tlow_cnt = 0; \
135  _i2c_err.smbus_alert_cnt = 0; \
136  _i2c_err.unexpected_event_cnt = 0; \
137  _i2c_err.last_unexpected_event = 0; \
138  _i2c_err.er_irq_cnt = 0; \
139  }
140 
141 
142 #ifdef USE_I2C0
143 
144 extern struct i2c_periph i2c0;
145 extern void i2c0_init(void);
146 
147 #endif /* USE_I2C0 */
148 
149 
150 #ifdef USE_I2C1
151 
152 extern struct i2c_periph i2c1;
153 extern void i2c1_init(void);
154 
155 #endif /* USE_I2C1 */
156 
157 
158 #ifdef USE_I2C2
159 
160 extern struct i2c_periph i2c2;
161 extern void i2c2_init(void);
162 
163 #endif /* USE_I2C2 */
164 
165 
166 #ifdef USE_I2C3
167 
168 extern struct i2c_periph i2c3;
169 extern void i2c3_init(void);
170 
171 #endif /* USE_I2C3 */
172 
173 
174 extern void i2c_init(struct i2c_periph* p);
175 extern bool_t i2c_idle(struct i2c_periph* p);
176 extern bool_t i2c_submit(struct i2c_periph* p, struct i2c_transaction* t);
177 extern void i2c_setbitrate(struct i2c_periph* p, int bitrate);
178 extern void i2c_event(void);
179 
180 /*
181  * Convenience functions.
182  * Usually these are preferred over i2c_submit,
183  * as they explicitly set the transaction type again.
184  *
185  * Return FALSE if submitting the transaction failed.
186  */
187 extern bool_t i2c_transmit(struct i2c_periph* p, struct i2c_transaction* t,
188  uint8_t s_addr, uint8_t len);
189 
190 extern bool_t i2c_receive(struct i2c_periph* p, struct i2c_transaction* t,
191  uint8_t s_addr, uint16_t len);
192 
193 extern bool_t i2c_transceive(struct i2c_periph* p, struct i2c_transaction* t,
194  uint8_t s_addr, uint8_t len_w, uint16_t len_r);
195 
199 #endif /* I2C_H */
unsigned short uint16_t
Definition: types.h:16
I2CTransactionStatus
Definition: i2c.h:50
#define I2C_TRANSACTION_QUEUE_LEN
Definition: i2c.h:87
I2CTransactionType
Definition: i2c.h:44
bool_t i2c_submit(struct i2c_periph *p, struct i2c_transaction *t)
Definition: i2c_arch.c:333
void * reg_addr
Definition: i2c.h:98
uint8_t trans_extract_idx
Definition: i2c.h:94
volatile uint16_t ack_fail_cnt
Definition: i2c.h:105
void i2c_init(struct i2c_periph *p)
Definition: i2c.c:78
volatile uint32_t event_chain[16]
Definition: i2c.h:116
Definition: i2c.h:59
void i2c_setbitrate(struct i2c_periph *p, int bitrate)
Definition: i2c_arch.c:366
bool_t i2c_transceive(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len_w, uint16_t len_r)
Definition: i2c.c:105
volatile uint8_t idx_buf
Definition: i2c.h:97
enum I2CStatus status
Definition: i2c.h:96
uint8_t slave_addr
Definition: i2c.h:79
volatile uint16_t smbus_alert_cnt
Definition: i2c.h:111
volatile uint16_t timeout_tlow_cnt
Definition: i2c.h:110
void * init_struct
Definition: i2c.h:99
volatile uint16_t miss_start_stop_cnt
Definition: i2c.h:106
bool_t i2c_transmit(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len)
Definition: i2c.c:85
unsigned long uint32_t
Definition: types.h:18
volatile uint16_t unexpected_event_cnt
Definition: i2c.h:112
Definition: i2c.h:70
enum I2CTransactionStatus status
Definition: i2c.h:83
volatile uint16_t over_under_cnt
Definition: i2c.h:108
volatile uint16_t pec_recep_cnt
Definition: i2c.h:109
volatile uint32_t er_irq_cnt
Definition: i2c.h:114
volatile uint8_t buf[I2C_BUF_LEN]
Definition: i2c.h:82
uint8_t len_w
Definition: i2c.h:81
uint16_t len_r
Definition: i2c.h:80
unsigned char uint8_t
Definition: types.h:14
volatile uint16_t arb_lost_cnt
Definition: i2c.h:107
uint8_t trans_insert_idx
Definition: i2c.h:93
#define I2C_BUF_LEN
Definition: i2c.h:74
struct i2c_errors * errors
Definition: i2c.h:100
Definition: i2c.h:90
enum I2CStatus status_chain[16]
Definition: i2c.h:117
Definition: i2c.h:45
enum I2CTransactionType type
Definition: i2c.h:78
static float p[2][2]
void i2c_event(void)
Definition: i2c_arch.c:364
volatile uint32_t irq_cnt
Definition: i2c.h:115
Definition: i2c.h:46
bool_t i2c_idle(struct i2c_periph *p)
Definition: i2c_arch.c:329
volatile uint32_t last_unexpected_event
Definition: i2c.h:113
bool_t i2c_receive(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint16_t len)
Definition: i2c.c:95
I2CStatus
Definition: i2c.h:58
struct i2c_transaction * trans[I2C_TRANSACTION_QUEUE_LEN]
Definition: i2c.h:92