Added generated timer definitions for STM32F7X2 universal target.
[betaflight.git] / src / main / pg / bus_i2c.c
blobf2d281c32ea859355507dc147b6ed5fb6f0f938b
1 /*
2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
8 * any later version.
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
22 * Created by jflyper
25 #include <stdbool.h>
26 #include <stddef.h>
27 #include <string.h>
29 #include "platform.h"
31 #if defined(USE_I2C) && !defined(SOFT_I2C)
33 #include "common/utils.h"
35 #include "drivers/io.h"
37 #include "pg/pg.h"
38 #include "pg/pg_ids.h"
40 #include "pg/bus_i2c.h"
42 #ifdef I2C_FULL_RECONFIGURABILITY
43 #if I2CDEV_COUNT >= 1
44 #ifndef I2C1_SCL
45 #define I2C1_SCL NONE
46 #endif
47 #ifndef I2C1_SDA
48 #define I2C1_SDA NONE
49 #endif
50 #endif
52 #if I2CDEV_COUNT >= 2
53 #ifndef I2C2_SCL
54 #define I2C2_SCL NONE
55 #endif
56 #ifndef I2C2_SDA
57 #define I2C2_SDA NONE
58 #endif
59 #endif
61 #if I2CDEV_COUNT >= 3
62 #ifndef I2C3_SCL
63 #define I2C3_SCL NONE
64 #endif
65 #ifndef I2C3_SDA
66 #define I2C3_SDA NONE
67 #endif
68 #endif
70 #if I2CDEV_COUNT >= 4
71 #ifndef I2C4_SCL
72 #define I2C4_SCL NONE
73 #endif
74 #ifndef I2C4_SDA
75 #define I2C4_SDA NONE
76 #endif
77 #endif
79 #else // I2C_FULL_RECONFIGURABILITY
81 // Backward compatibility for exisiting targets
83 #ifdef STM32F1
84 #ifndef I2C1_SCL
85 #define I2C1_SCL PB8
86 #endif
87 #ifndef I2C1_SDA
88 #define I2C1_SDA PB9
89 #endif
90 #ifndef I2C2_SCL
91 #define I2C2_SCL PB10
92 #endif
93 #ifndef I2C2_SDA
94 #define I2C2_SDA PB11
95 #endif
96 #endif // STM32F1
98 #ifdef STM32F3
99 #ifndef I2C1_SCL
100 #define I2C1_SCL PB6
101 #endif
102 #ifndef I2C1_SDA
103 #define I2C1_SDA PB7
104 #endif
105 #ifndef I2C2_SCL
106 #define I2C2_SCL PA9
107 #endif
108 #ifndef I2C2_SDA
109 #define I2C2_SDA PA10
110 #endif
111 #endif // STM32F3
113 #ifdef STM32F4
114 #ifndef I2C1_SCL
115 #define I2C1_SCL PB6
116 #endif
117 #ifndef I2C1_SDA
118 #define I2C1_SDA PB7
119 #endif
120 #ifndef I2C2_SCL
121 #define I2C2_SCL PB10
122 #endif
123 #ifndef I2C2_SDA
124 #define I2C2_SDA PB11
125 #endif
126 #ifndef I2C3_SCL
127 #define I2C3_SCL PA8
128 #endif
129 #ifndef I2C3_SDA
130 #define I2C3_SDA PC9
131 #endif
132 #endif // STM32F4
134 #ifdef STM32F7
135 #ifndef I2C1_SCL
136 #define I2C1_SCL PB6
137 #endif
138 #ifndef I2C1_SDA
139 #define I2C1_SDA PB7
140 #endif
141 #ifndef I2C2_SCL
142 #define I2C2_SCL PB10
143 #endif
144 #ifndef I2C2_SDA
145 #define I2C2_SDA PB11
146 #endif
147 #ifndef I2C3_SCL
148 #define I2C3_SCL PA8
149 #endif
150 #ifndef I2C3_SDA
151 #define I2C3_SDA PB4
152 #endif
153 #ifndef I2C4_SCL
154 #define I2C4_SCL PD12
155 #endif
156 #ifndef I2C4_SDA
157 #define I2C4_SDA PD13
158 #endif
159 #endif // STM32F7
161 #endif // I2C_FULL_RECONFIGURABILITY
163 // Default values for internal pullup
165 #if defined(USE_I2C_PULLUP)
166 #define I2C1_PULLUP true
167 #define I2C2_PULLUP true
168 #define I2C3_PULLUP true
169 #define I2C4_PULLUP true
170 #else
171 #define I2C1_PULLUP false
172 #define I2C2_PULLUP false
173 #define I2C3_PULLUP false
174 #define I2C4_PULLUP false
175 #endif
177 typedef struct i2cDefaultConfig_s {
178 I2CDevice device;
179 ioTag_t ioTagScl, ioTagSda;
180 bool overClock;
181 bool pullUp;
182 } i2cDefaultConfig_t;
184 static const i2cDefaultConfig_t i2cDefaultConfig[] = {
185 #ifdef USE_I2C_DEVICE_1
186 { I2CDEV_1, IO_TAG(I2C1_SCL), IO_TAG(I2C1_SDA), I2C1_OVERCLOCK, I2C1_PULLUP },
187 #endif
188 #ifdef USE_I2C_DEVICE_2
189 { I2CDEV_2, IO_TAG(I2C2_SCL), IO_TAG(I2C2_SDA), I2C2_OVERCLOCK, I2C2_PULLUP },
190 #endif
191 #ifdef USE_I2C_DEVICE_3
192 { I2CDEV_3, IO_TAG(I2C3_SCL), IO_TAG(I2C3_SDA), I2C3_OVERCLOCK, I2C3_PULLUP },
193 #endif
194 #ifdef USE_I2C_DEVICE_4
195 { I2CDEV_4, IO_TAG(I2C4_SCL), IO_TAG(I2C4_SDA), I2C4_OVERCLOCK, I2C4_PULLUP },
196 #endif
199 void pgResetFn_i2cConfig(i2cConfig_t *i2cConfig)
201 memset(i2cConfig, 0, sizeof(*i2cConfig));
203 for (size_t index = 0 ; index < ARRAYLEN(i2cDefaultConfig) ; index++) {
204 const i2cDefaultConfig_t *defconf = &i2cDefaultConfig[index];
205 i2cConfig->ioTagScl[defconf->device] = defconf->ioTagScl;
206 i2cConfig->ioTagSda[defconf->device] = defconf->ioTagSda;
207 i2cConfig->overClock[defconf->device] = defconf->overClock;
208 i2cConfig->pullUp[defconf->device] = defconf->pullUp;
212 PG_REGISTER_WITH_RESET_FN(i2cConfig_t, i2cConfig, PG_I2C_CONFIG, 0);
214 #endif // defined(USE_I2C) && !defined(USE_SOFT_I2C)