74be9e5c3f79447b27e8cbc8bdc69f70b9c50d0a
[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 /* EV6 */
148 #define  I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED        ((uint32_t)0x00070082)  /* BUSY, MSL, ADDR, TXE and TRA flags */
149 #define  I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED           ((uint32_t)0x00030002)  /* BUSY, MSL and ADDR flags */
150
151 /* EV7 */
152 #define  I2C_EVENT_MASTER_BYTE_RECEIVED                    ((uint32_t)0x00030040)  /* BUSY, MSL and RXNE flags */
153
154 /* EV8 */
155 #define I2C_EVENT_MASTER_BYTE_TRANSMITTING                 ((uint32_t)0x00070080) /* TRA, BUSY, MSL, TXE flags */
156
157 /* EV8_2 */
158 #define  I2C_EVENT_MASTER_BYTE_TRANSMITTED                 ((uint32_t)0x00070084)  /* TRA, BUSY, MSL, TXE and BTF flags */
159
160 /* EV9 */
161 #define  I2C_EVENT_MASTER_MODE_ADDRESS10                   ((uint32_t)0x00030008)  /* BUSY, MSL and ADD10 flags */
162
163 /* EV3_2 */
164 #define  I2C_EVENT_SLAVE_ACK_FAILURE                       ((uint32_t)0x00000400)  /* AF flag */
165
166
167
168 /* I2C SPE mask */
169 #define CR1_PE_SET              ((uint16_t)0x0001)
170 #define CR1_PE_RESET            ((uint16_t)0xFFFE)
171
172 /* I2C START mask */
173 #define CR1_START_SET           ((uint16_t)0x0100)
174 #define CR1_START_RESET         ((uint16_t)0xFEFF)
175
176 /* I2C STOP mask */
177 #define CR1_STOP_SET            ((uint16_t)0x0200)
178 #define CR1_STOP_RESET          ((uint16_t)0xFDFF)
179
180 /* I2C ACK mask */
181 #define CR1_ACK_SET             ((uint16_t)0x0400)
182 #define CR1_ACK_RESET           ((uint16_t)0xFBFF)
183
184 /* I2C ENGC mask */
185 #define CR1_ENGC_SET            ((uint16_t)0x0040)
186 #define CR1_ENGC_RESET          ((uint16_t)0xFFBF)
187
188 /* I2C SWRST mask */
189 #define CR1_SWRST_SET           ((uint16_t)0x8000)
190 #define CR1_SWRST_RESET         ((uint16_t)0x7FFF)
191
192 /* I2C PEC mask */
193 #define CR1_PEC_SET             ((uint16_t)0x1000)
194 #define CR1_PEC_RESET           ((uint16_t)0xEFFF)
195
196 /* I2C ENPEC mask */
197 #define CR1_ENPEC_SET           ((uint16_t)0x0020)
198 #define CR1_ENPEC_RESET         ((uint16_t)0xFFDF)
199
200 /* I2C ENARP mask */
201 #define CR1_ENARP_SET           ((uint16_t)0x0010)
202 #define CR1_ENARP_RESET         ((uint16_t)0xFFEF)
203
204 /* I2C NOSTRETCH mask */
205 #define CR1_NOSTRETCH_SET       ((uint16_t)0x0080)
206 #define CR1_NOSTRETCH_RESET     ((uint16_t)0xFF7F)
207
208 /* I2C registers Masks */
209 #define CR1_CLEAR_MASK          ((uint16_t)0xFBF5)
210
211 /* I2C DMAEN mask */
212 #define CR2_DMAEN_SET           ((uint16_t)0x0800)
213 #define CR2_DMAEN_RESET         ((uint16_t)0xF7FF)
214
215 /* I2C LAST mask */
216 #define CR2_LAST_SET            ((uint16_t)0x1000)
217 #define CR2_LAST_RESET          ((uint16_t)0xEFFF)
218
219 /* I2C FREQ mask */
220 #define CR2_FREQ_RESET          ((uint16_t)0xFFC0)
221
222 /* I2C ADD0 mask */
223 #define OAR1_ADD0_SET           ((uint16_t)0x0001)
224 #define OAR1_ADD0_RESET         ((uint16_t)0xFFFE)
225
226 /* I2C ENDUAL mask */
227 #define OAR2_ENDUAL_SET         ((uint16_t)0x0001)
228 #define OAR2_ENDUAL_RESET       ((uint16_t)0xFFFE)
229
230 /* I2C ADD2 mask */
231 #define OAR2_ADD2_RESET         ((uint16_t)0xFF01)
232
233 /* I2C F/S mask */
234 #define CCR_FS_SET              ((uint16_t)0x8000)
235
236 /* I2C CCR mask */
237 #define CCR_CCR_SET             ((uint16_t)0x0FFF)
238
239 /* I2C FLAG mask */
240 #define FLAG_MASK               ((uint32_t)0x00FFFFFF)
241
242 /* I2C Interrupt Enable mask */
243 #define ITEN_MASK               ((uint32_t)0x07000000)
244
245
246 struct stm32_i2c
247 {
248         reg16_t CR1;
249         uint16_t  RESERVED0;
250         reg16_t CR2;
251         uint16_t  RESERVED1;
252         reg16_t OAR1;
253         uint16_t  RESERVED2;
254         reg16_t OAR2;
255         uint16_t  RESERVED3;
256         reg16_t DR;
257         uint16_t  RESERVED4;
258         reg16_t SR1;
259         uint16_t  RESERVED5;
260         reg16_t SR2;
261         uint16_t  RESERVED6;
262         reg16_t CCR;
263         uint16_t  RESERVED7;
264         reg16_t TRISE;
265         uint16_t  RESERVED8;
266         reg16_t DR;
267 };
268
269 #endif /* STM32_I2C_H */