Ditching default target for `make` in favour of `make all` (#14099)
[betaflight.git] / src / main / drivers / bus.c
blob36ce3cff5512334c144fc18c3e276be57ddc8566
1 /*
2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
8 * any later version.
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
21 #include <stdbool.h>
22 #include <stdint.h>
24 #include "platform.h"
26 #include "drivers/bus.h"
27 #include "drivers/bus_i2c_busdev.h"
28 #include "drivers/bus_spi.h"
30 // Access routines where the register is accessed directly
31 bool busRawWriteRegister(const extDevice_t *dev, uint8_t reg, uint8_t data)
33 #ifdef USE_SPI
34 if (dev->bus->busType == BUS_TYPE_SPI) {
35 return spiWriteRegRB(dev, reg, data);
36 } else
37 #endif
39 return busWriteRegister(dev, reg, data);
43 bool busRawWriteRegisterStart(const extDevice_t *dev, uint8_t reg, uint8_t data)
45 #ifdef USE_SPI
46 if (dev->bus->busType == BUS_TYPE_SPI) {
47 return spiWriteRegRB(dev, reg, data);
48 } else
49 #endif
51 return busWriteRegisterStart(dev, reg, data);
55 bool busRawReadRegisterBuffer(const extDevice_t *dev, uint8_t reg, uint8_t *data, uint8_t length)
57 #ifdef USE_SPI
58 if (dev->bus->busType == BUS_TYPE_SPI) {
59 return spiReadRegBufRB(dev, reg, data, length);
60 } else
61 #endif
63 return busReadRegisterBuffer(dev, reg, data, length);
67 bool busRawReadRegisterBufferStart(const extDevice_t *dev, uint8_t reg, uint8_t *data, uint8_t length)
69 #ifdef USE_SPI
70 if (dev->bus->busType == BUS_TYPE_SPI) {
71 return spiReadRegBufRB(dev, reg, data, length);
72 } else
73 #endif
75 return busReadRegisterBufferStart(dev, reg, data, length);
79 // Write routines where the register is masked with 0x7f
80 bool busWriteRegister(const extDevice_t *dev, uint8_t reg, uint8_t data)
82 #if !defined(USE_SPI) && !defined(USE_I2C)
83 UNUSED(reg);
84 UNUSED(data);
85 #endif
86 switch (dev->bus->busType) {
87 #ifdef USE_SPI
88 case BUS_TYPE_SPI:
89 return spiWriteRegRB(dev, reg & 0x7f, data);
90 #endif
91 #ifdef USE_I2C
92 case BUS_TYPE_I2C:
93 return i2cBusWriteRegister(dev, reg, data);
94 #endif
95 default:
96 return false;
100 bool busWriteRegisterStart(const extDevice_t *dev, uint8_t reg, uint8_t data)
102 #if !defined(USE_SPI) && !defined(USE_I2C)
103 UNUSED(reg);
104 UNUSED(data);
105 #endif
106 switch (dev->bus->busType) {
107 #ifdef USE_SPI
108 case BUS_TYPE_SPI:
109 return spiWriteRegRB(dev, reg & 0x7f, data);
110 #endif
111 #ifdef USE_I2C
112 case BUS_TYPE_I2C:
113 return i2cBusWriteRegisterStart(dev, reg, data);
114 #endif
115 default:
116 return false;
120 // Read routines where the register is ORed with 0x80
121 bool busReadRegisterBuffer(const extDevice_t *dev, uint8_t reg, uint8_t *data, uint8_t length)
123 #if !defined(USE_SPI) && !defined(USE_I2C)
124 UNUSED(reg);
125 UNUSED(data);
126 UNUSED(length);
127 #endif
128 switch (dev->bus->busType) {
129 #ifdef USE_SPI
130 case BUS_TYPE_SPI:
131 return spiReadRegMskBufRB(dev, reg | 0x80, data, length);
132 #endif
133 #ifdef USE_I2C
134 case BUS_TYPE_I2C:
135 return i2cBusReadRegisterBuffer(dev, reg, data, length);
136 #endif
137 default:
138 return false;
142 // Start the I2C read, but do not wait for completion
143 bool busReadRegisterBufferStart(const extDevice_t *dev, uint8_t reg, uint8_t *data, uint8_t length)
145 #if !defined(USE_SPI) && !defined(USE_I2C)
146 UNUSED(reg);
147 UNUSED(data);
148 UNUSED(length);
149 #endif
150 switch (dev->bus->busType) {
151 #ifdef USE_SPI
152 case BUS_TYPE_SPI:
153 // For SPI allow the transaction to complete
154 return spiReadRegMskBufRB(dev, reg | 0x80, data, length);
155 #endif
156 #ifdef USE_I2C
157 case BUS_TYPE_I2C:
158 // Initiate the read access
159 return i2cBusReadRegisterBufferStart(dev, reg, data, length);
160 #endif
161 default:
162 return false;
166 // Returns true if bus is still busy
167 bool busBusy(const extDevice_t *dev, bool *error)
169 #if !defined(USE_I2C)
170 UNUSED(error);
171 #endif
172 switch (dev->bus->busType) {
173 #ifdef USE_SPI
174 case BUS_TYPE_SPI:
175 // No waiting on SPI
176 return false;
177 #endif
179 #ifdef USE_I2C
180 case BUS_TYPE_I2C:
181 return i2cBusBusy(dev, error);
182 #endif
184 default:
185 return false;
189 uint8_t busReadRegister(const extDevice_t *dev, uint8_t reg)
191 #if !defined(USE_SPI) && !defined(USE_I2C)
192 UNUSED(dev);
193 UNUSED(reg);
194 return false;
195 #else
196 uint8_t data;
197 busReadRegisterBuffer(dev, reg, &data, 1);
198 return data;
199 #endif
202 void busDeviceRegister(const extDevice_t *dev)
204 #if !defined(USE_SPI) && !defined(USE_I2C)
205 UNUSED(dev);
206 #endif
208 switch (dev->bus->busType) {
209 #if defined(USE_SPI)
210 case BUS_TYPE_SPI:
211 spiBusDeviceRegister(dev);
213 break;
214 #endif
215 #if defined(USE_I2C)
216 case BUS_TYPE_I2C:
217 i2cBusDeviceRegister(dev);
219 break;
220 #endif
221 default:
222 break;