Blackbox device type 'file' (SITL) considered working when file handler is available
[inav.git] / src / main / drivers / bus_i2c.h
blobbf685b1bfae072eb0aaf478eec850df3d3c4d077
1 /*
2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
18 #pragma once
21 #define I2C_SHORT_TIMEOUT ((uint32_t)0x1000)
22 #define I2C_LONG_TIMEOUT ((uint32_t)(10 * I2C_SHORT_TIMEOUT))
23 #define I2C_DEFAULT_TIMEOUT I2C_LONG_TIMEOUT
25 #define I2C_TIMEOUT (10000)
27 #include "drivers/io_types.h"
28 #include "drivers/rcc_types.h"
30 typedef enum { // Weird mapping to keep config compatible with previos version
31 I2C_SPEED_100KHZ = 2,
32 I2C_SPEED_200KHZ = 3,
33 I2C_SPEED_400KHZ = 0,
34 I2C_SPEED_800KHZ = 1,
35 } I2CSpeed;
37 typedef enum I2CDevice {
38 I2CINVALID = -1,
39 I2CDEV_EMULATED = -1, // Hack until we have proper I2C abstraction
40 I2CDEV_1 = 0,
41 I2CDEV_2,
42 I2CDEV_3,
43 #ifdef USE_I2C_DEVICE_4
44 I2CDEV_4,
45 #endif
46 I2CDEV_COUNT
47 } I2CDevice;
49 typedef struct i2cDevice_s {
50 #if defined(AT32F43x)
51 i2c_type *dev;
52 #else
53 I2C_TypeDef *dev;
54 #endif
55 ioTag_t scl;
56 ioTag_t sda;
57 rccPeriphTag_t rcc;
58 I2CSpeed speed;
59 #if defined(STM32F7) || defined(STM32H7) || defined(AT32F43x)
60 uint8_t ev_irq;
61 uint8_t er_irq;
62 uint8_t af;
63 #endif
64 } i2cDevice_t;
66 void i2cSetSpeed(uint8_t speed);
67 void i2cInit(I2CDevice device);
68 bool i2cWriteBuffer(I2CDevice device, uint8_t addr_, uint8_t reg_, uint8_t len_, const uint8_t *data, bool allowRawAccess);
69 bool i2cWrite(I2CDevice device, uint8_t addr_, uint8_t reg, uint8_t data, bool allowRawAccess);
70 bool i2cRead(I2CDevice device, uint8_t addr_, uint8_t reg, uint8_t len, uint8_t* buf, bool allowRawAccess);
71 bool i2cBusy(I2CDevice device, bool *error);
73 uint16_t i2cGetErrorCounter(void);