Blackbox device type 'file' (SITL) considered working when file handler is available
[inav.git] / src / main / drivers / bus.c
blob0da3f76d307612d80e9ec19ae5f422adc5f814de
1 /*
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/.
25 #include <string.h>
27 #include "platform.h"
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
37 #ifdef USE_SPI
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);
42 if (io) {
43 IOInit(io, OWNER_SPI_PREINIT, RESOURCE_SPI_CS, 0);
44 IOConfigGPIO(io, IOCFG_IPU);
47 #endif
49 static void busDevPreInit(const busDeviceDescriptor_t * descriptor)
51 switch (descriptor->busType) {
52 default:
53 case BUSTYPE_NONE:
54 break;
56 case BUSTYPE_I2C:
57 break;
59 case BUSTYPE_SPI:
60 #ifdef USE_SPI
61 busDevPreInit_SPI(descriptor);
62 #endif
63 break;
67 void busInit(void)
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);
74 #endif
77 #ifdef USE_I2C
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;
84 return true;
86 #endif
88 #ifdef USE_SPI
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)) {
97 // Init CSN pin
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);
101 return true;
104 return false;
106 #endif
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)
117 UNUSED(owner);
118 #if defined(SITL_BUILD)
119 UNUSED(bus);
120 UNUSED(hw);
121 UNUSED(tag);
122 #else
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;
128 if (dev) {
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) {
137 default:
138 case BUSTYPE_NONE:
139 return NULL;
141 case BUSTYPE_I2C:
142 #ifdef USE_I2C
143 if (!busDevInit_I2C(dev, descriptor)) {
144 busDeviceDeInit(dev);
145 return NULL;
147 break;
148 #else
149 busDeviceDeInit(dev);
150 return NULL;
151 #endif
153 case BUSTYPE_SPI:
154 #ifdef USE_SPI
155 if (!busDevInit_SPI(dev, descriptor, owner)) {
156 busDeviceDeInit(dev);
157 return NULL;
159 break;
160 #else
161 busDeviceDeInit(dev);
162 return NULL;
163 #endif
166 return dev;
168 else {
169 return NULL;
173 #endif
174 return NULL;
177 busDevice_t * busDeviceOpen(busType_e bus, devHardwareType_e hw, uint8_t tag)
179 #if defined(SITL_BUILD)
180 UNUSED(bus);
181 UNUSED(hw);
182 UNUSED(tag);
183 #else
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) {
189 return dev;
193 #endif
194 return NULL;
197 void busSetSpeed(const busDevice_t * dev, busSpeed_e speed)
199 UNUSED(speed);
201 switch (dev->busType) {
202 default:
203 case BUSTYPE_NONE:
204 // Not available
205 break;
206 case BUSTYPE_SPI:
207 #ifdef USE_SPI
208 spiBusSetSpeed(dev, speed);
209 #endif
210 break;
211 case BUSTYPE_I2C:
212 // Do nothing for I2C
213 break;
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);
227 if (mem != NULL) {
228 mem[0] = value;
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)
243 #ifdef USE_SPI
244 return spiBusTransfer(dev, rxBuf, txBuf, length);
245 #else
246 UNUSED(dev);
247 UNUSED(rxBuf);
248 UNUSED(txBuf);
249 UNUSED(length);
250 #endif
252 return false;
255 bool busTransferMultiple(const busDevice_t * dev, busTransferDescriptor_t * dsc, int count)
257 #ifdef USE_SPI
258 // busTransfer function is only supported on SPI bus
259 if (dev->busType == BUSTYPE_SPI) {
260 return spiBusTransferMultiple(dev, dsc, count);
262 #else
263 UNUSED(dev);
264 UNUSED(dsc);
265 UNUSED(count);
266 #endif
268 return false;
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)
274 UNUSED(reg);
275 UNUSED(data);
276 UNUSED(length);
277 #endif
279 switch (dev->busType) {
280 case BUSTYPE_SPI:
281 #ifdef USE_SPI
282 if (dev->flags & DEVFLAGS_USE_RAW_REGISTERS) {
283 return spiBusWriteBuffer(dev, reg, data, length);
285 else {
286 return spiBusWriteBuffer(dev, reg | 0x80, data, length);
288 #else
289 return false;
290 #endif
292 case BUSTYPE_I2C:
293 #ifdef USE_I2C
294 return i2cBusWriteBuffer(dev, reg, data, length);
295 #else
296 return false;
297 #endif
299 default:
300 return false;
304 bool busWrite(const busDevice_t * dev, uint8_t reg, uint8_t data)
306 #if !defined(USE_SPI) && !defined(USE_I2C)
307 UNUSED(reg);
308 UNUSED(data);
309 #endif
311 switch (dev->busType) {
312 case BUSTYPE_SPI:
313 #ifdef USE_SPI
314 if (dev->flags & DEVFLAGS_USE_RAW_REGISTERS) {
315 return spiBusWriteRegister(dev, reg, data);
317 else {
318 return spiBusWriteRegister(dev, reg & 0x7F, data);
320 #else
321 return false;
322 #endif
324 case BUSTYPE_I2C:
325 #ifdef USE_I2C
326 return i2cBusWriteRegister(dev, reg, data);
327 #else
328 return false;
329 #endif
331 default:
332 return false;
336 bool busReadBuf(const busDevice_t * dev, uint8_t reg, uint8_t * data, uint8_t length)
338 #if !defined(USE_SPI) && !defined(USE_I2C)
339 UNUSED(reg);
340 UNUSED(data);
341 UNUSED(length);
342 #endif
343 switch (dev->busType) {
344 case BUSTYPE_SPI:
345 #ifdef USE_SPI
346 if (dev->flags & DEVFLAGS_USE_RAW_REGISTERS) {
347 return spiBusReadBuffer(dev, reg, data, length);
349 else {
350 return spiBusReadBuffer(dev, reg | 0x80, data, length);
352 #else
353 return false;
354 #endif
356 case BUSTYPE_I2C:
357 #ifdef USE_I2C
358 return i2cBusReadBuffer(dev, reg, data, length);
359 #else
360 return false;
361 #endif
363 default:
364 return false;
368 bool busRead(const busDevice_t * dev, uint8_t reg, uint8_t * data)
370 #if !defined(USE_SPI) && !defined(USE_I2C)
371 UNUSED(reg);
372 UNUSED(data);
373 #endif
375 switch (dev->busType) {
376 case BUSTYPE_SPI:
377 #ifdef USE_SPI
378 if (dev->flags & DEVFLAGS_USE_RAW_REGISTERS) {
379 return spiBusReadRegister(dev, reg, data);
381 else {
382 return spiBusReadRegister(dev, reg | 0x80, data);
384 #else
385 return false;
386 #endif
388 case BUSTYPE_I2C:
389 #ifdef USE_I2C
390 return i2cBusReadRegister(dev, reg, data);
391 #else
392 return false;
393 #endif
394 default:
395 return false;
399 void busSelectDevice(const busDevice_t * dev)
401 #ifdef USE_SPI
402 if (dev->busType == BUSTYPE_SPI && (dev->flags & DEVFLAGS_USE_MANUAL_DEVICE_SELECT)) {
403 spiBusSelectDevice(dev);
405 #else
406 UNUSED(dev);
407 #endif
410 void busDeselectDevice(const busDevice_t * dev)
412 #ifdef USE_SPI
413 if (dev->busType == BUSTYPE_SPI && (dev->flags & DEVFLAGS_USE_MANUAL_DEVICE_SELECT)) {
414 spiBusDeselectDevice(dev);
416 #else
417 UNUSED(dev);
418 #endif
421 bool busIsBusy(const busDevice_t * dev)
423 switch (dev->busType) {
424 case BUSTYPE_SPI:
425 #ifdef USE_SPI
426 return spiBusIsBusy(dev);
427 #else
428 UNUSED(dev);
429 return false;
430 #endif
431 case BUSTYPE_I2C:
432 #ifdef USE_I2C
433 return i2cBusBusy(dev,NULL);
434 #else
435 UNUSED(dev);
436 return false;
437 #endif
438 default:
439 return false;