I2C configurable clockspeed
[betaflight.git] / src / main / drivers / bus_i2c_config.c
blob8cf3e92937c36f2ed3f93f7b744df41820e3ecd7
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 <stdint.h>
27 #include <string.h>
29 #include "platform.h"
31 #if defined(USE_I2C) && !defined(SOFT_I2C)
33 #include "build/build_config.h"
34 #include "build/debug.h"
36 #include "drivers/bus_i2c.h"
37 #include "drivers/bus_i2c_impl.h"
38 #include "drivers/io.h"
40 #include "pg/bus_i2c.h"
42 void i2cHardwareConfigure(const i2cConfig_t *i2cConfig)
44 for (int index = 0 ; index < I2CDEV_COUNT ; index++) {
45 const i2cHardware_t *hardware = &i2cHardware[index];
47 if (!hardware->reg) {
48 continue;
51 I2CDevice device = hardware->device;
52 i2cDevice_t *pDev = &i2cDevice[device];
54 memset(pDev, 0, sizeof(*pDev));
56 for (int pindex = 0 ; pindex < I2C_PIN_SEL_MAX ; pindex++) {
57 if (i2cConfig[device].ioTagScl == hardware->sclPins[pindex].ioTag) {
58 pDev->scl = IOGetByTag(i2cConfig[device].ioTagScl);
59 #if defined(STM32F4) || defined(STM32H7) || defined(STM32G4)
60 pDev->sclAF = hardware->sclPins[pindex].af;
61 #endif
63 if (i2cConfig[device].ioTagSda == hardware->sdaPins[pindex].ioTag) {
64 pDev->sda = IOGetByTag(i2cConfig[device].ioTagSda);
65 #if defined(STM32F4) || defined(STM32H7) || defined(STM32G4)
66 pDev->sdaAF = hardware->sdaPins[pindex].af;
67 #endif
71 if (pDev->scl && pDev->sda) {
72 pDev->hardware = hardware;
73 pDev->reg = hardware->reg;
74 pDev->pullUp = i2cConfig[device].pullUp;
75 pDev->clockSpeed = i2cConfig[device].clockSpeed;
80 #endif // defined(USE_I2C) && !defined(USE_SOFT_I2C)