2 * This file is part of INAV.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
6 * You can obtain one at http://mozilla.org/MPL/2.0/.
8 * Alternatively, the contents of this file may be used under the terms
9 * of the GNU General Public License Version 3, as described below:
11 * This file is free software: you may copy, redistribute and/or modify
12 * it under the terms of the GNU General Public License as published by the
13 * Free Software Foundation, either version 3 of the License, or (at your
14 * option) any later version.
16 * This file is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
19 * Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see http://www.gnu.org/licenses/.
32 #include "drivers/resource.h"
33 #include "drivers/bus_i2c.h"
34 #include "drivers/bus_spi.h"
36 // FIXME: Hack until we rework SPI driver
37 #define BUS_SPI1 SPIDEV_1
38 #define BUS_SPI2 SPIDEV_2
39 #define BUS_SPI3 SPIDEV_3
40 #define BUS_SPI4 SPIDEV_4
42 #define BUS_I2C1 I2CDEV_1
43 #define BUS_I2C2 I2CDEV_2
44 #define BUS_I2C3 I2CDEV_3
45 #define BUS_I2C4 I2CDEV_4
46 #define BUS_I2C_EMULATED I2CINVALID
48 #define BUS_SCRATCHPAD_MEMORY_SIZE (20)
51 BUS_SPEED_INITIALIZATION
= 0,
53 BUS_SPEED_STANDARD
= 2,
55 BUS_SPEED_ULTRAFAST
= 4
73 /* Ultimately all hardware descriptors will go to target definition files.
74 * Driver code will merely query for it's HW descriptor and initialize it */
78 /* Combined ACC/GYRO chips */
88 /* Combined ACC/GYRO/MAG chips */
116 /* Temp sensor chips */
126 /* 1-wire interface chips */
132 /* Rangefinder modules */
138 DEVHW_TERARANGER_EVO_I2C
,
141 DEVHW_MS4525
, // Pitot meter
142 DEVHW_DLVR
, // Pitot meter
143 DEVHW_M25P16
, // SPI NOR flash
144 DEVHW_W25N01G
, // SPI 128MB flash
145 DEVHW_UG2864
, // I2C OLED display
146 DEVHW_SDCARD
, // Generic SD-Card
147 DEVHW_IRLOCK
, // IR-Lock visual positioning hardware
148 DEVHW_PCF8574
, // 8-bit I/O expander
153 DEVFLAGS_USE_RAW_REGISTERS
= (1 << 0), // Don't manipulate MSB for R/W selection (SPI), allow using 0xFF register to raw i2c reads/writes
156 DEVFLAGS_USE_MANUAL_DEVICE_SELECT
= (1 << 1), // (SPI only) Don't automatically select/deselect device
157 DEVFLAGS_SPI_MODE_0
= (1 << 2), // (SPI only) Use CPOL=0/CPHA=0 (if unset MODE3 is used - CPOL=1/CPHA=1)
160 typedef struct busDeviceDescriptor_s
{
163 devHardwareType_e devHwType
;
166 uint8_t param
; // Driver-specific parameter
182 } busDeviceDescriptor_t
;
184 typedef struct busDevice_s
{
185 const busDeviceDescriptor_t
* descriptorPtr
;
186 busType_e busType
; // Copy of busType to avoid additional pointer dereferencing
187 uint32_t flags
; // Copy of flags
188 uint32_t param
; // Copy of param
192 SPIDevice spiBus
; // SPI bus ID
193 IO_t csnPin
; // IO for CS# pin
198 I2CDevice i2cBus
; // I2C bus ID
199 uint8_t address
; // I2C bus device address
203 IO_t irqPin
; // Device IRQ pin. Bus system will only assign IO_t object to this var. Initialization is up to device driver
204 uint32_t * scratchpad
; // Memory where device driver can store persistent data. Zeroed out when initializing the device
205 // for the first time. Useful when once device is shared between several sensors
206 // (like MPU/ICM acc-gyro sensors)
210 extern const busDeviceDescriptor_t __busdev_registry_start
[] __asm("section$start$__DATA$__busdev_registry");
211 extern const busDeviceDescriptor_t __busdev_registry_end
[] __asm("section$end$__DATA$__busdev_registry");
212 #define BUSDEV_REGISTER_ATTRIBUTES __attribute__ ((section("__DATA,__busdev_registry"), used, aligned(4)))
214 extern const busDeviceDescriptor_t __busdev_registry_start
[];
215 extern const busDeviceDescriptor_t __busdev_registry_end
[];
216 #define BUSDEV_REGISTER_ATTRIBUTES __attribute__ ((section(".busdev_registry"), used, aligned(4)))
220 #define BUSDEV_REGISTER_SPI_F(_name, _devHw, _spiBus, _csnPin, _irqPin, _tag, _flags, _param) \
221 extern const busDeviceDescriptor_t _name ## _registry; \
222 static busDevice_t _name ## _memory; \
223 const busDeviceDescriptor_t _name ## _registry BUSDEV_REGISTER_ATTRIBUTES = { \
224 .devicePtr = (void *) & _name ## _memory, \
225 .busType = BUSTYPE_SPI, \
226 .devHwType = _devHw, \
232 .csnPin = IO_TAG(_csnPin) \
234 .irqPin = IO_TAG(_irqPin) \
239 #define BUSDEV_REGISTER_SPI(_name, _devHw, _spiBus, _csnPin, _irqPin, _flags, _param) \
240 BUSDEV_REGISTER_SPI_F(_name, _devHw, _spiBus, _csnPin, _irqPin, 0, _flags, _param)
242 #define BUSDEV_REGISTER_SPI_TAG(_name, _devHw, _spiBus, _csnPin, _irqPin, _tag, _flags, _param) \
243 BUSDEV_REGISTER_SPI_F(_name, _devHw, _spiBus, _csnPin, _irqPin, _tag, _flags, _param)
245 #define BUSDEV_REGISTER_SPI(_name, _devHw, _spiBus, _csnPin, _irqPin, _flags, _param) // NO-OP
246 #define BUSDEV_REGISTER_SPI_TAG(_name, _devHw, _spiBus, _csnPin, _irqPin, _tag, _flags, _param) // NO-OP
250 #define BUSDEV_REGISTER_I2C_F(_name, _devHw, _i2cBus, _devAddr, _irqPin, _tag, _flags, _param) \
251 extern const busDeviceDescriptor_t _name ## _registry; \
252 static busDevice_t _name ## _memory; \
253 const busDeviceDescriptor_t _name ## _registry BUSDEV_REGISTER_ATTRIBUTES = { \
254 .devicePtr = (void *) & _name ## _memory, \
255 .busType = BUSTYPE_I2C, \
256 .devHwType = _devHw, \
262 .address = _devAddr \
264 .irqPin = IO_TAG(_irqPin) \
269 #define BUSDEV_REGISTER_I2C(_name, _devHw, _i2cBus, _devAddr, _irqPin, _flags, _param) \
270 BUSDEV_REGISTER_I2C_F(_name, _devHw, _i2cBus, _devAddr, _irqPin, 0, _flags, _param)
272 #define BUSDEV_REGISTER_I2C_TAG(_name, _devHw, _i2cBus, _devAddr, _irqPin, _tag, _flags, _param)\
273 BUSDEV_REGISTER_I2C_F(_name, _devHw, _i2cBus, _devAddr, _irqPin, _tag, _flags, _param)
275 #define BUSDEV_REGISTER_I2C(_name, _devHw, _i2cBus, _devAddr, _irqPin, _flags, _param) // NO-OP
276 #define BUSDEV_REGISTER_I2C_TAG(_name, _devHw, _i2cBus, _devAddr, _irqPin, _tag, _flags, _param) // NO-OP
279 // busTransfer and busTransferMultiple are supported only on full-duplex SPI bus
280 typedef struct busTransferDescriptor_s
{
282 const uint8_t * txBuf
;
284 } busTransferDescriptor_t
;
286 /* Internal abstraction function */
287 bool i2cBusWriteBuffer(const busDevice_t
* dev
, uint8_t reg
, const uint8_t * data
, uint8_t length
);
288 bool i2cBusWriteRegister(const busDevice_t
* dev
, uint8_t reg
, uint8_t data
);
289 bool i2cBusReadBuffer(const busDevice_t
* dev
, uint8_t reg
, uint8_t * data
, uint8_t length
);
290 bool i2cBusReadRegister(const busDevice_t
* dev
, uint8_t reg
, uint8_t * data
);
291 bool i2cBusBusy(const busDevice_t
*dev
, bool *error
);
293 bool spiBusInitHost(const busDevice_t
* dev
);
294 bool spiBusIsBusy(const busDevice_t
* dev
);
295 void spiBusSetSpeed(const busDevice_t
* dev
, busSpeed_e speed
);
296 bool spiBusTransfer(const busDevice_t
* dev
, uint8_t * rxBuf
, const uint8_t * txBuf
, int length
);
297 bool spiBusTransferMultiple(const busDevice_t
* dev
, busTransferDescriptor_t
* dsc
, int count
);
298 bool spiBusWriteBuffer(const busDevice_t
* dev
, uint8_t reg
, const uint8_t * data
, uint8_t length
);
299 bool spiBusWriteRegister(const busDevice_t
* dev
, uint8_t reg
, uint8_t data
);
300 bool spiBusReadBuffer(const busDevice_t
* dev
, uint8_t reg
, uint8_t * data
, uint8_t length
);
301 bool spiBusReadRegister(const busDevice_t
* dev
, uint8_t reg
, uint8_t * data
);
302 void spiBusSelectDevice(const busDevice_t
* dev
);
303 void spiBusDeselectDevice(const busDevice_t
* dev
);
305 /* Pre-initialize all known device descriptors to make sure hardware state is consistent and known
306 * Initialize bus hardware */
309 /* Finds a device in registry. First matching device is returned. Also performs the low-level initialization of the hardware (CS line for SPI) */
310 busDevice_t
* busDeviceInit(busType_e bus
, devHardwareType_e hw
, uint8_t tag
, resourceOwner_e owner
);
311 busDevice_t
* busDeviceOpen(busType_e bus
, devHardwareType_e hw
, uint8_t tag
);
312 void busDeviceDeInit(busDevice_t
* dev
);
314 uint32_t busDeviceReadScratchpad(const busDevice_t
* dev
);
315 void busDeviceWriteScratchpad(busDevice_t
* dev
, uint32_t value
);
316 void * busDeviceGetScratchpadMemory(const busDevice_t
* dev
);
318 void busSetSpeed(const busDevice_t
* dev
, busSpeed_e speed
);
320 /* Select/Deselect device will allow code to do something during device transfer or do transfer in chunks over some time */
321 void busSelectDevice(const busDevice_t
* dev
);
322 void busDeselectDevice(const busDevice_t
* dev
);
324 bool busWriteBuf(const busDevice_t
* busdev
, uint8_t reg
, const uint8_t * data
, uint8_t length
);
325 bool busReadBuf(const busDevice_t
* busdev
, uint8_t reg
, uint8_t * data
, uint8_t length
);
326 bool busRead(const busDevice_t
* busdev
, uint8_t reg
, uint8_t * data
);
327 bool busWrite(const busDevice_t
* busdev
, uint8_t reg
, uint8_t data
);
329 bool busTransfer(const busDevice_t
* dev
, uint8_t * rxBuf
, const uint8_t * txBuf
, int length
);
330 bool busTransferMultiple(const busDevice_t
* dev
, busTransferDescriptor_t
* buffers
, int count
);
332 bool busIsBusy(const busDevice_t
* dev
);