[FLYWOOF411] add board documentation
[inav/snaewe.git] / src / main / drivers / bus_i2c.h
blob55b07b8e7f043e55ac528988b8166d6efd150d84
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 I2C_TypeDef *dev;
51 ioTag_t scl;
52 ioTag_t sda;
53 rccPeriphTag_t rcc;
54 I2CSpeed speed;
55 #if defined(STM32F7)
56 uint8_t ev_irq;
57 uint8_t er_irq;
58 uint8_t af;
59 #endif
60 } i2cDevice_t;
62 void i2cSetSpeed(uint8_t speed);
63 void i2cInit(I2CDevice device);
64 bool i2cWriteBuffer(I2CDevice device, uint8_t addr_, uint8_t reg_, uint8_t len_, const uint8_t *data, bool allowRawAccess);
65 bool i2cWrite(I2CDevice device, uint8_t addr_, uint8_t reg, uint8_t data, bool allowRawAccess);
66 bool i2cRead(I2CDevice device, uint8_t addr_, uint8_t reg, uint8_t len, uint8_t* buf, bool allowRawAccess);
68 uint16_t i2cGetErrorCounter(void);