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"
31 // Access routines where the register is accessed directly
32 bool busRawWriteRegister(const extDevice_t
*dev
, uint8_t reg
, uint8_t data
)
35 if (dev
->bus
->busType
== BUS_TYPE_SPI
) {
36 return spiWriteRegRB(dev
, reg
, data
);
40 return busWriteRegister(dev
, reg
, data
);
44 bool busRawWriteRegisterStart(const extDevice_t
*dev
, uint8_t reg
, uint8_t data
)
47 if (dev
->bus
->busType
== BUS_TYPE_SPI
) {
48 return spiWriteRegRB(dev
, reg
, data
);
52 return busWriteRegisterStart(dev
, reg
, data
);
56 bool busRawReadRegisterBuffer(const extDevice_t
*dev
, uint8_t reg
, uint8_t *data
, uint8_t length
)
59 if (dev
->bus
->busType
== BUS_TYPE_SPI
) {
60 return spiReadRegBufRB(dev
, reg
, data
, length
);
64 return busReadRegisterBuffer(dev
, reg
, data
, length
);
68 bool busRawReadRegisterBufferStart(const extDevice_t
*dev
, uint8_t reg
, uint8_t *data
, uint8_t length
)
71 if (dev
->bus
->busType
== BUS_TYPE_SPI
) {
72 return spiReadRegBufRB(dev
, reg
, data
, length
);
76 return busReadRegisterBufferStart(dev
, reg
, data
, length
);
81 // Write routines where the register is masked with 0x7f
82 bool busWriteRegister(const extDevice_t
*dev
, uint8_t reg
, uint8_t data
)
84 #if !defined(USE_SPI) && !defined(USE_I2C)
88 switch (dev
->bus
->busType
) {
91 return spiWriteRegRB(dev
, reg
& 0x7f, data
);
95 return i2cBusWriteRegister(dev
, reg
, data
);
102 bool busWriteRegisterStart(const extDevice_t
*dev
, uint8_t reg
, uint8_t data
)
104 #if !defined(USE_SPI) && !defined(USE_I2C)
108 switch (dev
->bus
->busType
) {
111 return spiWriteRegRB(dev
, reg
& 0x7f, data
);
115 return i2cBusWriteRegisterStart(dev
, reg
, data
);
122 // Read routines where the register is ORed with 0x80
123 bool busReadRegisterBuffer(const extDevice_t
*dev
, uint8_t reg
, uint8_t *data
, uint8_t length
)
125 #if !defined(USE_SPI) && !defined(USE_I2C)
130 switch (dev
->bus
->busType
) {
133 return spiReadRegMskBufRB(dev
, reg
| 0x80, data
, length
);
137 return i2cBusReadRegisterBuffer(dev
, reg
, data
, length
);
144 // Start the I2C read, but do not wait for completion
145 bool busReadRegisterBufferStart(const extDevice_t
*dev
, uint8_t reg
, uint8_t *data
, uint8_t length
)
147 #if !defined(USE_SPI) && !defined(USE_I2C)
152 switch (dev
->bus
->busType
) {
155 // For SPI allow the transaction to complete
156 return spiReadRegMskBufRB(dev
, reg
| 0x80, data
, length
);
160 // Initiate the read access
161 return i2cBusReadRegisterBufferStart(dev
, reg
, data
, length
);
168 // Returns true if bus is still busy
169 bool busBusy(const extDevice_t
*dev
, bool *error
)
171 #if !defined(USE_I2C)
174 switch (dev
->bus
->busType
) {
183 return i2cBusBusy(dev
, error
);
191 uint8_t busReadRegister(const extDevice_t
*dev
, uint8_t reg
)
193 #if !defined(USE_SPI) && !defined(USE_I2C)
199 busReadRegisterBuffer(dev
, reg
, &data
, 1);
204 void busDeviceRegister(const extDevice_t
*dev
)
206 #if !defined(USE_SPI) && !defined(USE_I2C)
210 switch (dev
->bus
->busType
) {
213 spiBusDeviceRegister(dev
);
219 i2cBusDeviceRegister(dev
);