Merge pull request #11297 from SteveCEvans/baro_state
[betaflight.git] / src / main / drivers / bus_i2c_impl.h
blob57a303ebdabbd83a9eafd74e4825e6228ddc4148
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)
36 uint8_t af;
37 #endif
38 } i2cPinDef_t;
40 #if defined(STM32F4) || defined(STM32H7) || defined(STM32G4)
41 #define I2CPINDEF(pin, af) { DEFIO_TAG_E(pin), af }
42 #elif defined(STM32F1)
43 #define I2CPINDEF(pin, af) { DEFIO_TAG_E(pin) }
44 #else
45 #define I2CPINDEF(pin) { DEFIO_TAG_E(pin) }
46 #endif
48 typedef struct i2cHardware_s {
49 I2CDevice device;
50 I2C_TypeDef *reg;
51 i2cPinDef_t sclPins[I2C_PIN_SEL_MAX];
52 i2cPinDef_t sdaPins[I2C_PIN_SEL_MAX];
53 rccPeriphTag_t rcc;
54 #if !defined(STM32F303xC)
55 uint8_t ev_irq;
56 uint8_t er_irq;
57 #endif
58 } i2cHardware_t;
60 extern const i2cHardware_t i2cHardware[];
62 #if defined(STM32F1) || defined(STM32F4)
63 typedef struct i2cState_s {
64 volatile bool error;
65 volatile bool busy;
66 volatile uint8_t addr;
67 volatile uint8_t reg;
68 volatile uint8_t bytes;
69 volatile uint8_t writing;
70 volatile uint8_t reading;
71 volatile uint8_t* write_p;
72 volatile uint8_t* read_p;
73 } i2cState_t;
74 #endif
76 typedef struct i2cDevice_s {
77 const i2cHardware_t *hardware;
78 I2C_TypeDef *reg;
79 IO_t scl;
80 IO_t sda;
81 #if defined(STM32F4) || defined(STM32H7) || defined(STM32G4)
82 uint8_t sclAF;
83 uint8_t sdaAF;
84 #endif
85 bool pullUp;
86 uint16_t clockSpeed;
88 // MCU/Driver dependent member follows
89 #if defined(STM32F1) || defined(STM32F4)
90 i2cState_t state;
91 #endif
92 #ifdef USE_HAL_DRIVER
93 I2C_HandleTypeDef handle;
94 #endif
95 } i2cDevice_t;
97 extern i2cDevice_t i2cDevice[];