Trailing whitespace
[bertos.git] / bertos / cpu / cortex-m3 / io / stm32_i2c.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos 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 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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 this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief STM32F103xx I2C definition.
34  */
35
36 #ifndef STM32_I2C_H
37 #define STM32_I2C_H
38
39 #include <cpu/types.h>
40
41
42 /* I2C_modes */
43 #define I2C_MODE_I2C                    ((uint16_t)0x0000)
44 #define I2C_MODE_SMBUSDEVICE            ((uint16_t)0x0002)
45 #define I2C_MODE_SMBUSHOST              ((uint16_t)0x000A)
46
47 /* I2C_duty_cycle_in_fast_mode */
48 #define I2C_DUTYCYCLE_16_9              ((uint16_t)0x4000)
49 #define I2C_DUTYCYCLE_2                 ((uint16_t)0xBFFF)
50
51 /* I2C_cknowledgementy */
52 #define I2C_ACK_ENABLE                  ((uint16_t)0x0400)
53 #define I2C_ACK_DISABLE                 ((uint16_t)0x0000)
54
55 /* I2C_transfer_direction */
56 #define I2C_DIRECTION_TRANSMITTER      ((uint8_t)0x00)
57 #define I2C_DIRECTION_RECEIVER         ((uint8_t)0x01)
58
59 /* I2C_acknowledged_address_defines  */
60 #define I2C_ACKNOWLEDGEDADDRESS_7BIT    ((uint16_t)0x4000)
61 #define I2C_ACKNOWLEDGEDADDRESS_10BIT   ((uint16_t)0xC000)
62
63 /* I2C_registers */
64 #define I2C_REGISTER_CR1                ((uint8_t)0x00)
65 #define I2C_REGISTER_CR2                ((uint8_t)0x04)
66 #define I2C_REGISTER_OAR1               ((uint8_t)0x08)
67 #define I2C_REGISTER_OAR2               ((uint8_t)0x0C)
68 #define I2C_REGISTER_DR                 ((uint8_t)0x10)
69 #define I2C_REGISTER_SR1                ((uint8_t)0x14)
70 #define I2C_REGISTER_SR2                ((uint8_t)0x18)
71 #define I2C_REGISTER_CCR                ((uint8_t)0x1C)
72 #define I2C_REGISTER_TRISE              ((uint8_t)0x20)
73
74 /* I2C_SMBus_alert_pin_level  */
75 #define I2C_SMBUSALERT_LOW              ((uint16_t)0x2000)
76 #define I2C_SMBUSALERT_HIGH             ((uint16_t)0xDFFF)
77
78 /* I2C_PEC_position  */
79 #define I2C_PECPOSITION_NEXT            ((uint16_t)0x0800)
80 #define I2C_PECPOSITION_CURRENT         ((uint16_t)0xF7FF)
81
82 /* I2C_interrupts_definition  */
83 #define I2C_IT_BUF                      ((uint16_t)0x0400)
84 #define I2C_IT_EVT                      ((uint16_t)0x0200)
85 #define I2C_IT_ERR                      ((uint16_t)0x0100)
86
87 /* I2C_interrupts_definition  */
88 #define I2C_IT_SMBALERT                 ((uint32_t)0x01008000)
89 #define I2C_IT_TIMEOUT                  ((uint32_t)0x01004000)
90 #define I2C_IT_PECERR                   ((uint32_t)0x01001000)
91 #define I2C_IT_OVR                      ((uint32_t)0x01000800)
92 #define I2C_IT_AF                       ((uint32_t)0x01000400)
93 #define I2C_IT_ARLO                     ((uint32_t)0x01000200)
94 #define I2C_IT_BERR                     ((uint32_t)0x01000100)
95 #define I2C_IT_TXE                      ((uint32_t)0x06000080)
96 #define I2C_IT_RXNE                     ((uint32_t)0x06000040)
97 #define I2C_IT_STOPF                    ((uint32_t)0x02000010)
98 #define I2C_IT_ADD10                    ((uint32_t)0x02000008)
99 #define I2C_IT_BTF                      ((uint32_t)0x02000004)
100 #define I2C_IT_ADDR                     ((uint32_t)0x02000002)
101 #define I2C_IT_SB                       ((uint32_t)0x02000001)
102
103 /* SR2 register flags  */
104 #define I2C_FLAG_DUALF                  ((uint32_t)0x00800000)
105 #define I2C_FLAG_SMBHOST                ((uint32_t)0x00400000)
106 #define I2C_FLAG_SMBDEFAULT             ((uint32_t)0x00200000)
107 #define I2C_FLAG_GENCALL                ((uint32_t)0x00100000)
108 #define I2C_FLAG_TRA                    ((uint32_t)0x00040000)
109 #define I2C_FLAG_BUSY                   ((uint32_t)0x00020000)
110 #define I2C_FLAG_MSL                    ((uint32_t)0x00010000)
111
112 /* SR1 register flags  */
113 #define I2C_FLAG_SMBALERT               ((uint32_t)0x10008000)
114 #define I2C_FLAG_TIMEOUT                ((uint32_t)0x10004000)
115 #define I2C_FLAG_PECERR                 ((uint32_t)0x10001000)
116 #define I2C_FLAG_OVR                    ((uint32_t)0x10000800)
117 #define I2C_FLAG_AF                     ((uint32_t)0x10000400)
118 #define I2C_FLAG_ARLO                   ((uint32_t)0x10000200)
119 #define I2C_FLAG_BERR                   ((uint32_t)0x10000100)
120 #define I2C_FLAG_TXE                    ((uint32_t)0x10000080)
121 #define I2C_FLAG_RXNE                   ((uint32_t)0x10000040)
122 #define I2C_FLAG_STOPF                  ((uint32_t)0x10000010)
123 #define I2C_FLAG_ADD10                  ((uint32_t)0x10000008)
124 #define I2C_FLAG_BTF                    ((uint32_t)0x10000004)
125 #define I2C_FLAG_ADDR                   ((uint32_t)0x10000002)
126 #define I2C_FLAG_SB                     ((uint32_t)0x10000001)
127
128 /* EV1  */
129 #define I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED       ((uint32_t)0x00060082) /* TRA, BUSY, TXE and ADDR flags */
130 #define I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED          ((uint32_t)0x00020002) /* BUSY and ADDR flags */
131 #define I2C_EVENT_SLAVE_TRANSMITTER_SECONDADDRESS_MATCHED ((uint32_t)0x00860080)  /* DUALF, TRA, BUSY and TXE flags */
132 #define I2C_EVENT_SLAVE_RECEIVER_SECONDADDRESS_MATCHED    ((uint32_t)0x00820000)  /* DUALF and BUSY flags */
133 #define I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED        ((uint32_t)0x00120000)  /* GENCALL and BUSY flags */
134
135 /* EV2  */
136 #define I2C_EVENT_SLAVE_BYTE_RECEIVED                     ((uint32_t)0x00020040)  /* BUSY and RXNE flags */
137
138 /* EV3 */
139 #define I2C_EVENT_SLAVE_BYTE_TRANSMITTED                  ((uint32_t)0x00060084)  /* TRA, BUSY, TXE and BTF flags */
140
141 /* EV4 */
142 #define I2C_EVENT_SLAVE_STOP_DETECTED                     ((uint32_t)0x00000010)  /* STOPF flag */
143
144 /* EV5 */
145 #define I2C_EVENT_MASTER_MODE_SELECT                      ((uint32_t)0x00030001)  /* BUSY, MSL and SB flag */
146
147
148 /* EV6 */
149 #define I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED        ((uint32_t)0x00070082)  /* BUSY, MSL, ADDR, TXE and TRA flags */
150 #define I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED           ((uint32_t)0x00030002)  /* BUSY, MSL and ADDR flags */
151
152 /* EV7 */
153 #define I2C_EVENT_MASTER_BYTE_RECEIVED                    ((uint32_t)0x00030040)  /* BUSY, MSL and RXNE flags */
154
155 /* EV8 */
156 #define I2C_EVENT_MASTER_BYTE_TRANSMITTING                 ((uint32_t)0x00070080) /* TRA, BUSY, MSL, TXE flags */
157
158 /* EV8_2 */
159 #define I2C_EVENT_MASTER_BYTE_TRANSMITTED                 ((uint32_t)0x00070084)  /* TRA, BUSY, MSL, TXE and BTF flags */
160
161 /* EV9 */
162 #define I2C_EVENT_MASTER_MODE_ADDRESS10                   ((uint32_t)0x00030008)  /* BUSY, MSL and ADD10 flags */
163
164 /* EV3_2 */
165 #define I2C_EVENT_SLAVE_ACK_FAILURE                       ((uint32_t)0x00000400)  /* AF flag */
166
167
168
169 /* I2C SPE mask */
170 #define CR1_PE_SET              ((uint16_t)0x0001)
171 #define CR1_PE_RESET            ((uint16_t)0xFFFE)
172
173 /* I2C START mask */
174 #define CR1_START_SET           ((uint16_t)0x0100)
175 #define CR1_START_RESET         ((uint16_t)0xFEFF)
176
177 /* I2C STOP mask */
178 #define CR1_STOP_SET            ((uint16_t)0x0200)
179 #define CR1_STOP_RESET          ((uint16_t)0xFDFF)
180
181 /* I2C ACK mask */
182 #define CR1_ACK_SET             ((uint16_t)0x0400)
183 #define CR1_ACK_RESET           ((uint16_t)0xFBFF)
184
185 /* I2C ENGC mask */
186 #define CR1_ENGC_SET            ((uint16_t)0x0040)
187 #define CR1_ENGC_RESET          ((uint16_t)0xFFBF)
188
189 /* I2C SWRST mask */
190 #define CR1_SWRST_SET           ((uint16_t)0x8000)
191 #define CR1_SWRST_RESET         ((uint16_t)0x7FFF)
192
193 /* I2C PEC mask */
194 #define CR1_PEC_SET             ((uint16_t)0x1000)
195 #define CR1_PEC_RESET           ((uint16_t)0xEFFF)
196
197 /* I2C ENPEC mask */
198 #define CR1_ENPEC_SET           ((uint16_t)0x0020)
199 #define CR1_ENPEC_RESET         ((uint16_t)0xFFDF)
200
201 /* I2C ENARP mask */
202 #define CR1_ENARP_SET           ((uint16_t)0x0010)
203 #define CR1_ENARP_RESET         ((uint16_t)0xFFEF)
204
205 /* I2C NOSTRETCH mask */
206 #define CR1_NOSTRETCH_SET       ((uint16_t)0x0080)
207 #define CR1_NOSTRETCH_RESET     ((uint16_t)0xFF7F)
208
209 /* I2C registers Masks */
210 #define CR1_CLEAR_MASK          ((uint16_t)0xFBF5)
211
212 /* I2C DMAEN mask */
213 #define CR2_DMAEN_SET           ((uint16_t)0x0800)
214 #define CR2_DMAEN_RESET         ((uint16_t)0xF7FF)
215
216 /* I2C LAST mask */
217 #define CR2_LAST_SET            ((uint16_t)0x1000)
218 #define CR2_LAST_RESET          ((uint16_t)0xEFFF)
219
220 /* I2C FREQ mask */
221 #define CR2_FREQ_RESET          ((uint16_t)0xFFC0)
222
223 #define CR2_FREQ_36MHZ          ((uint16_t)0x100100)
224
225 /* I2C ADD0 mask */
226 #define OAR1_ADD0_SET           ((uint16_t)0x0001)
227 #define OAR1_ADD0_RESET         ((uint16_t)0xFFFE)
228
229 /* I2C ENDUAL mask */
230 #define OAR2_ENDUAL_SET         ((uint16_t)0x0001)
231 #define OAR2_ENDUAL_RESET       ((uint16_t)0xFFFE)
232
233 /* I2C ADD2 mask */
234 #define OAR2_ADD2_RESET         ((uint16_t)0xFF01)
235
236 /* I2C F/S mask */
237 #define CCR_FS_SET              ((uint16_t)0x8000)
238
239 /* I2C CCR mask */
240 #define CCR_CCR_SET             ((uint16_t)0x0FFF)
241
242
243 /* I2C CR1 */
244 #define CR1_POS_SET                ((uint16_t)0x800)
245 #define CR1_POS_RESET                 (~CR1_POS_SET)
246
247 /* I2C Status */
248 #define SR1_PECERR                              12
249 #define SR1_OVR                                 11
250 #define SR1_AF                                  10
251 #define SR1_ARLO                                 9
252 #define SR1_BERR                                 8
253 #define SR1_TXE                                  7
254 #define SR1_RXNE                                 6
255 #define SR1_ADDR                                 1
256 #define SR1_BTF                                  2
257 #define SR1_SB                                   0
258
259 #define SR2_TRA                                  2
260 #define SR2_BUSY                                 1
261 #define SR2_MSL                                  0
262
263
264 #define SR1_ERR_MASK        (BV(SR1_PECERR) | \
265                                                          BV(SR1_OVR)    | \
266                                                          BV(SR1_AF)     | \
267                                                          BV(SR1_ARLO)   | \
268                                                          BV(SR1_BERR))
269
270 struct stm32_i2c
271 {
272         reg16_t CR1;
273         uint16_t  RESERVED0;
274         reg16_t CR2;
275         uint16_t  RESERVED1;
276         reg16_t OAR1;
277         uint16_t  RESERVED2;
278         reg16_t OAR2;
279         uint16_t  RESERVED3;
280         reg16_t DR;
281         uint16_t  RESERVED4;
282         reg16_t SR1;
283         uint16_t  RESERVED5;
284         reg16_t SR2;
285         uint16_t  RESERVED6;
286         reg16_t CCR;
287         uint16_t  RESERVED7;
288         reg16_t TRISE;
289         uint16_t  RESERVED8;
290 };
291
292 #endif /* STM32_I2C_H */