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)
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/>.
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
)
34 if (dev
->bus
->busType
== BUS_TYPE_SPI
) {
35 return spiWriteRegRB(dev
, reg
, data
);
39 return busWriteRegister(dev
, reg
, data
);
43 bool busRawWriteRegisterStart(const extDevice_t
*dev
, uint8_t reg
, uint8_t data
)
46 if (dev
->bus
->busType
== BUS_TYPE_SPI
) {
47 return spiWriteRegRB(dev
, reg
, data
);
51 return busWriteRegisterStart(dev
, reg
, data
);
55 bool busRawReadRegisterBuffer(const extDevice_t
*dev
, uint8_t reg
, uint8_t *data
, uint8_t length
)
58 if (dev
->bus
->busType
== BUS_TYPE_SPI
) {
59 return spiReadRegBufRB(dev
, reg
, data
, length
);
63 return busReadRegisterBuffer(dev
, reg
, data
, length
);
67 bool busRawReadRegisterBufferStart(const extDevice_t
*dev
, uint8_t reg
, uint8_t *data
, uint8_t length
)
70 if (dev
->bus
->busType
== BUS_TYPE_SPI
) {
71 return spiReadRegBufRB(dev
, reg
, data
, length
);
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)
86 switch (dev
->bus
->busType
) {
89 return spiWriteRegRB(dev
, reg
& 0x7f, data
);
93 return i2cBusWriteRegister(dev
, reg
, data
);
100 bool busWriteRegisterStart(const extDevice_t
*dev
, uint8_t reg
, uint8_t data
)
102 #if !defined(USE_SPI) && !defined(USE_I2C)
106 switch (dev
->bus
->busType
) {
109 return spiWriteRegRB(dev
, reg
& 0x7f, data
);
113 return i2cBusWriteRegisterStart(dev
, reg
, data
);
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)
128 switch (dev
->bus
->busType
) {
131 return spiReadRegMskBufRB(dev
, reg
| 0x80, data
, length
);
135 return i2cBusReadRegisterBuffer(dev
, reg
, data
, length
);
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)
150 switch (dev
->bus
->busType
) {
153 // For SPI allow the transaction to complete
154 return spiReadRegMskBufRB(dev
, reg
| 0x80, data
, length
);
158 // Initiate the read access
159 return i2cBusReadRegisterBufferStart(dev
, reg
, data
, length
);
166 // Returns true if bus is still busy
167 bool busBusy(const extDevice_t
*dev
, bool *error
)
169 #if !defined(USE_I2C)
172 switch (dev
->bus
->busType
) {
181 return i2cBusBusy(dev
, error
);
189 uint8_t busReadRegister(const extDevice_t
*dev
, uint8_t reg
)
191 #if !defined(USE_SPI) && !defined(USE_I2C)
197 busReadRegisterBuffer(dev
, reg
, &data
, 1);
202 void busDeviceRegister(const extDevice_t
*dev
)
204 #if !defined(USE_SPI) && !defined(USE_I2C)
208 switch (dev
->bus
->busType
) {
211 spiBusDeviceRegister(dev
);
217 i2cBusDeviceRegister(dev
);