Merge pull request #11297 from SteveCEvans/baro_state
[betaflight.git] / src / main / drivers / bus_i2c.h
blob4ab2d5e596806a1bbb7bbbdb2f21cc03eeb65de7
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 #ifndef I2C_DEVICE
29 #define I2C_DEVICE I2CINVALID
30 #endif
32 typedef enum I2CDevice {
33 I2CINVALID = -1,
34 I2CDEV_1 = 0,
35 I2CDEV_2,
36 I2CDEV_3,
37 I2CDEV_4,
38 } I2CDevice;
40 #if defined(STM32F1) || defined(STM32F3)
41 #define I2CDEV_COUNT 2
42 #elif defined(STM32F4)
43 #define I2CDEV_COUNT 3
44 #elif defined(STM32F7)
45 #define I2CDEV_COUNT 4
46 #else
47 #define I2CDEV_COUNT 4
48 #endif
50 // Macros to convert between CLI bus number and I2CDevice.
51 #define I2C_CFG_TO_DEV(x) ((x) - 1)
52 #define I2C_DEV_TO_CFG(x) ((x) + 1)
54 // I2C device address range in 7-bit address mode
55 #define I2C_ADDR7_MIN 8
56 #define I2C_ADDR7_MAX 119
58 struct i2cConfig_s;
59 void i2cHardwareConfigure(const struct i2cConfig_s *i2cConfig);
60 void i2cInit(I2CDevice device);
61 bool i2cWriteBuffer(I2CDevice device, uint8_t addr_, uint8_t reg_, uint8_t len_, uint8_t *data);
62 bool i2cWrite(I2CDevice device, uint8_t addr_, uint8_t reg, uint8_t data);
63 bool i2cReadBuffer(I2CDevice device, uint8_t addr_, uint8_t reg, uint8_t len, uint8_t* buf);
64 bool i2cRead(I2CDevice device, uint8_t addr_, uint8_t reg, uint8_t len, uint8_t* buf);
65 bool i2cBusy(I2CDevice device, bool *error);
67 uint16_t i2cGetErrorCounter(void);
68 uint8_t i2cGetRegisteredDeviceCount(void);