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/.
28 #include "build/debug.h"
30 #include "common/memory.h"
32 #include "drivers/bus.h"
33 #include "drivers/io.h"
35 #define BUSDEV_MAX_DEVICES 16
38 static void busDevPreInit_SPI(const busDeviceDescriptor_t
* descriptor
)
40 // Pre-initialize SPI device chip-select line to input with weak pull-up
41 IO_t io
= IOGetByTag(descriptor
->busdev
.spi
.csnPin
);
43 IOInit(io
, OWNER_SPI_PREINIT
, RESOURCE_SPI_CS
, 0);
44 IOConfigGPIO(io
, IOCFG_IPU
);
49 static void busDevPreInit(const busDeviceDescriptor_t
* descriptor
)
51 switch (descriptor
->busType
) {
61 busDevPreInit_SPI(descriptor
);
69 #if !defined(SITL_BUILD)
70 /* Pre-initialize bus devices */
71 for (const busDeviceDescriptor_t
* descriptor
= __busdev_registry_start
; (descriptor
) < __busdev_registry_end
; descriptor
++) {
72 busDevPreInit(descriptor
);
78 static bool busDevInit_I2C(busDevice_t
* dev
, const busDeviceDescriptor_t
* descriptor
)
80 dev
->busType
= descriptor
->busType
;
81 dev
->irqPin
= IOGetByTag(descriptor
->irqPin
);
82 dev
->busdev
.i2c
.i2cBus
= descriptor
->busdev
.i2c
.i2cBus
;
83 dev
->busdev
.i2c
.address
= descriptor
->busdev
.i2c
.address
;
89 static bool busDevInit_SPI(busDevice_t
* dev
, const busDeviceDescriptor_t
* descriptor
, resourceOwner_e owner
)
91 dev
->busType
= descriptor
->busType
;
92 dev
->irqPin
= IOGetByTag(descriptor
->irqPin
);
93 dev
->busdev
.spi
.spiBus
= descriptor
->busdev
.spi
.spiBus
;
94 dev
->busdev
.spi
.csnPin
= IOGetByTag(descriptor
->busdev
.spi
.csnPin
);
96 if (dev
->busdev
.spi
.csnPin
&& spiBusInitHost(dev
)) {
98 IOInit(dev
->busdev
.spi
.csnPin
, owner
, RESOURCE_SPI_CS
, 0);
99 IOConfigGPIO(dev
->busdev
.spi
.csnPin
, SPI_IO_CS_CFG
);
100 IOHi(dev
->busdev
.spi
.csnPin
);
108 void busDeviceDeInit(busDevice_t
* dev
)
110 busDevPreInit(dev
->descriptorPtr
);
111 dev
->descriptorPtr
= NULL
;
112 dev
->busType
= BUSTYPE_NONE
;
115 busDevice_t
* busDeviceInit(busType_e bus
, devHardwareType_e hw
, uint8_t tag
, resourceOwner_e owner
)
118 #if defined(SITL_BUILD)
124 for (const busDeviceDescriptor_t
* descriptor
= __busdev_registry_start
; (descriptor
) < __busdev_registry_end
; descriptor
++) {
125 if (hw
== descriptor
->devHwType
&& (bus
== descriptor
->busType
|| bus
== BUSTYPE_ANY
) && (tag
== descriptor
->tag
)) {
126 // We have a candidate - initialize device context memory
127 busDevice_t
* dev
= descriptor
->devicePtr
;
129 memset(dev
, 0, sizeof(busDevice_t
));
131 dev
->descriptorPtr
= descriptor
;
132 dev
->busType
= descriptor
->busType
;
133 dev
->flags
= descriptor
->flags
;
134 dev
->param
= descriptor
->param
;
136 switch (descriptor
->busType
) {
143 if (!busDevInit_I2C(dev
, descriptor
)) {
144 busDeviceDeInit(dev
);
149 busDeviceDeInit(dev
);
155 if (!busDevInit_SPI(dev
, descriptor
, owner
)) {
156 busDeviceDeInit(dev
);
161 busDeviceDeInit(dev
);
177 busDevice_t
* busDeviceOpen(busType_e bus
, devHardwareType_e hw
, uint8_t tag
)
179 #if defined(SITL_BUILD)
184 for (const busDeviceDescriptor_t
* descriptor
= __busdev_registry_start
; (descriptor
) < __busdev_registry_end
; descriptor
++) {
185 if (hw
== descriptor
->devHwType
&& (bus
== descriptor
->busType
|| bus
== BUSTYPE_ANY
) && (tag
== descriptor
->tag
)) {
186 // Found a hardware descriptor. Now check if device context is valid
187 busDevice_t
* dev
= descriptor
->devicePtr
;
188 if (dev
->busType
== descriptor
->busType
&& dev
->descriptorPtr
== descriptor
) {
197 void busSetSpeed(const busDevice_t
* dev
, busSpeed_e speed
)
201 switch (dev
->busType
) {
208 spiBusSetSpeed(dev
, speed
);
212 // Do nothing for I2C
217 uint32_t busDeviceReadScratchpad(const busDevice_t
* dev
)
219 uint32_t * mem
= busDeviceGetScratchpadMemory(dev
);
220 return (mem
!= NULL
) ? mem
[0] : 0;
223 void busDeviceWriteScratchpad(busDevice_t
* dev
, uint32_t value
)
225 uint32_t * mem
= busDeviceGetScratchpadMemory(dev
);
232 void * busDeviceGetScratchpadMemory(const busDevice_t
* dev
)
234 if (dev
->scratchpad
== NULL
) {
235 ((busDevice_t
*)dev
)->scratchpad
= memAllocate(BUS_SCRATCHPAD_MEMORY_SIZE
, OWNER_SYSTEM
);
238 return (void *)dev
->scratchpad
;
241 bool busTransfer(const busDevice_t
* dev
, uint8_t * rxBuf
, const uint8_t * txBuf
, int length
)
244 return spiBusTransfer(dev
, rxBuf
, txBuf
, length
);
255 bool busTransferMultiple(const busDevice_t
* dev
, busTransferDescriptor_t
* dsc
, int count
)
258 // busTransfer function is only supported on SPI bus
259 if (dev
->busType
== BUSTYPE_SPI
) {
260 return spiBusTransferMultiple(dev
, dsc
, count
);
271 bool busWriteBuf(const busDevice_t
* dev
, uint8_t reg
, const uint8_t * data
, uint8_t length
)
273 #if !defined(USE_SPI) && !defined(USE_I2C)
279 switch (dev
->busType
) {
282 if (dev
->flags
& DEVFLAGS_USE_RAW_REGISTERS
) {
283 return spiBusWriteBuffer(dev
, reg
, data
, length
);
286 return spiBusWriteBuffer(dev
, reg
| 0x80, data
, length
);
294 return i2cBusWriteBuffer(dev
, reg
, data
, length
);
304 bool busWrite(const busDevice_t
* dev
, uint8_t reg
, uint8_t data
)
306 #if !defined(USE_SPI) && !defined(USE_I2C)
311 switch (dev
->busType
) {
314 if (dev
->flags
& DEVFLAGS_USE_RAW_REGISTERS
) {
315 return spiBusWriteRegister(dev
, reg
, data
);
318 return spiBusWriteRegister(dev
, reg
& 0x7F, data
);
326 return i2cBusWriteRegister(dev
, reg
, data
);
336 bool busReadBuf(const busDevice_t
* dev
, uint8_t reg
, uint8_t * data
, uint8_t length
)
338 #if !defined(USE_SPI) && !defined(USE_I2C)
343 switch (dev
->busType
) {
346 if (dev
->flags
& DEVFLAGS_USE_RAW_REGISTERS
) {
347 return spiBusReadBuffer(dev
, reg
, data
, length
);
350 return spiBusReadBuffer(dev
, reg
| 0x80, data
, length
);
358 return i2cBusReadBuffer(dev
, reg
, data
, length
);
368 bool busRead(const busDevice_t
* dev
, uint8_t reg
, uint8_t * data
)
370 #if !defined(USE_SPI) && !defined(USE_I2C)
375 switch (dev
->busType
) {
378 if (dev
->flags
& DEVFLAGS_USE_RAW_REGISTERS
) {
379 return spiBusReadRegister(dev
, reg
, data
);
382 return spiBusReadRegister(dev
, reg
| 0x80, data
);
390 return i2cBusReadRegister(dev
, reg
, data
);
399 void busSelectDevice(const busDevice_t
* dev
)
402 if (dev
->busType
== BUSTYPE_SPI
&& (dev
->flags
& DEVFLAGS_USE_MANUAL_DEVICE_SELECT
)) {
403 spiBusSelectDevice(dev
);
410 void busDeselectDevice(const busDevice_t
* dev
)
413 if (dev
->busType
== BUSTYPE_SPI
&& (dev
->flags
& DEVFLAGS_USE_MANUAL_DEVICE_SELECT
)) {
414 spiBusDeselectDevice(dev
);
421 bool busIsBusy(const busDevice_t
* dev
)
423 switch (dev
->busType
) {
426 return spiBusIsBusy(dev
);
433 return i2cBusBusy(dev
,NULL
);