Add RANGEFINDER and OPTICALFLOW MT build option (#14042)
[betaflight.git] / src / main / drivers / bus_i2c_impl.h
blob0dd1fb2c99721fa7ebeed4b5f7ab8428a2df846e
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/>.
21 #pragma once
23 #include "platform.h"
25 #include "drivers/io_types.h"
26 #include "drivers/rcc_types.h"
28 #define I2C_TIMEOUT_US 10000
29 #define I2C_TIMEOUT_SYS_TICKS (I2C_TIMEOUT_US / 1000)
31 #define I2C_PIN_SEL_MAX 4
33 typedef struct i2cPinDef_s {
34 ioTag_t ioTag;
35 #if defined(STM32F4) || defined(STM32H7) || defined(STM32G4) || defined(AT32F4) || defined(APM32F4)
36 uint8_t af;
37 #endif
38 } i2cPinDef_t;
40 #if defined(STM32F4) || defined(STM32H7) || defined(STM32G4) || defined(AT32F4) || defined(APM32F4)
41 #define I2CPINDEF(pin, af) { DEFIO_TAG_E(pin), af }
42 #else
43 #define I2CPINDEF(pin) { DEFIO_TAG_E(pin) }
44 #endif
46 typedef struct i2cHardware_s {
47 I2CDevice device;
48 I2C_TypeDef *reg;
49 i2cPinDef_t sclPins[I2C_PIN_SEL_MAX];
50 i2cPinDef_t sdaPins[I2C_PIN_SEL_MAX];
51 rccPeriphTag_t rcc;
52 uint8_t ev_irq;
53 uint8_t er_irq;
54 } i2cHardware_t;
56 extern const i2cHardware_t i2cHardware[];
58 #if defined(STM32F4)
59 typedef struct i2cState_s {
60 volatile bool error;
61 volatile bool busy;
62 volatile uint8_t addr;
63 volatile uint8_t reg;
64 volatile uint8_t bytes;
65 volatile uint8_t writing;
66 volatile uint8_t reading;
67 volatile uint8_t* write_p;
68 volatile uint8_t* read_p;
69 } i2cState_t;
70 #endif
72 typedef struct i2cDevice_s {
73 const i2cHardware_t *hardware;
74 I2C_TypeDef *reg;
75 IO_t scl;
76 IO_t sda;
77 #if defined(STM32F4) || defined(STM32H7) || defined(STM32G4) || defined(AT32F4) || defined(APM32F4)
78 uint8_t sclAF;
79 uint8_t sdaAF;
80 #endif
81 bool pullUp;
82 uint16_t clockSpeed;
84 // MCU/Driver dependent member follows
85 #if defined(STM32F4)
86 i2cState_t state;
87 #endif
88 #if defined(USE_HAL_DRIVER) || defined(AT32F4)
89 I2C_HandleTypeDef handle;
90 #endif
91 } i2cDevice_t;
93 extern i2cDevice_t i2cDevice[];