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/>.
25 #include "drivers/bus_i2c.h"
26 #include "drivers/io_types.h"
27 #include "drivers/dma.h"
33 BUS_TYPE_MPU_SLAVE
, // Slave I2C on SPI master
34 BUS_TYPE_GYRO_AUTO
, // Only used by acc/gyro bus auto detection code
47 // Bus interface, independent of connected device
48 typedef struct busDevice_s
{
52 SPI_TypeDef
*instance
;
59 struct busMpuSlave_s
{
60 struct extDevice_s
*master
;
65 dmaChannelDescriptor_t
*dmaTx
;
66 dmaChannelDescriptor_t
*dmaRx
;
68 // Use a reference here as this saves RAM for unused descriptors
69 #if defined(USE_FULL_LL_DRIVER)
70 LL_DMA_InitTypeDef
*initTx
;
71 LL_DMA_InitTypeDef
*initRx
;
73 DMA_InitTypeDef
*initTx
;
74 DMA_InitTypeDef
*initRx
;
77 volatile struct busSegment_s
* volatile curSegment
;
78 volatile struct extDevice_s
*csLockDevice
;
82 // External device has an associated bus and bus dependent address
83 typedef struct extDevice_s
{
94 struct extMpuSlave_s
{
99 // Cache the init structure for the next DMA transfer to reduce inter-segment delay
100 #if defined(USE_FULL_LL_DRIVER)
101 LL_DMA_InitTypeDef initTx
;
102 LL_DMA_InitTypeDef initRx
;
104 DMA_InitTypeDef initTx
;
105 DMA_InitTypeDef initRx
;
108 // Support disabling DMA on a per device basis
110 // Per device buffer reference if needed
111 uint8_t *txBuf
, *rxBuf
;
112 // Connected devices on the same bus may support different speeds
113 uint32_t callbackArg
;
116 /* Each SPI access may comprise multiple parts, for example, wait/write enable/write/data each of which
117 * is defined by a segment, with optional callback after each is completed.
119 * If there are more than one segments, or a single segment with negateCS negated then DMA will be used irrespective of length
122 typedef struct busSegment_s
{
127 // Receive buffer, or in the case of the final segment to
131 // Link to the device associated with the next transfer
132 const extDevice_t
*dev
;
133 // Segments to process in the next transfer.
134 volatile struct busSegment_s
*segments
;
138 bool negateCS
; // Should CS be negated at the end of this segment
139 busStatus_e (*callback
)(uint32_t arg
);
142 #ifdef TARGET_BUS_INIT
143 void targetBusInit(void);
146 // Access routines where the register is accessed directly
147 bool busRawWriteRegister(const extDevice_t
*dev
, uint8_t reg
, uint8_t data
);
148 bool busRawWriteRegisterStart(const extDevice_t
*dev
, uint8_t reg
, uint8_t data
);
149 bool busRawReadRegisterBuffer(const extDevice_t
*dev
, uint8_t reg
, uint8_t *data
, uint8_t length
);
150 bool busRawReadRegisterBufferStart(const extDevice_t
*dev
, uint8_t reg
, uint8_t *data
, uint8_t length
);
151 // Write routines where the register is masked with 0x7f
152 bool busWriteRegister(const extDevice_t
*dev
, uint8_t reg
, uint8_t data
);
153 bool busWriteRegisterStart(const extDevice_t
*dev
, uint8_t reg
, uint8_t data
);
154 // Read routines where the register is ORed with 0x80
155 bool busReadRegisterBuffer(const extDevice_t
*dev
, uint8_t reg
, uint8_t *data
, uint8_t length
);
156 bool busReadRegisterBufferStart(const extDevice_t
*dev
, uint8_t reg
, uint8_t *data
, uint8_t length
);
157 uint8_t busReadRegister(const extDevice_t
*dev
, uint8_t reg
);
159 bool busBusy(const extDevice_t
*dev
, bool *error
);
160 void busDeviceRegister(const extDevice_t
*dev
);