Merge pull request #11494 from haslinghuis/dshot_gpio
[betaflight.git] / src / main / drivers / rx / rx_a7105.c
blob9f062c22af40461078e23ad70614ab354d7104fb
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>
23 #include <stdlib.h>
25 #include "platform.h"
27 #ifdef USE_RX_FLYSKY
29 #include "drivers/bus_spi.h"
30 #include "drivers/io.h"
31 #include "drivers/io_impl.h"
32 #include "drivers/rx/rx_a7105.h"
33 #include "drivers/rx/rx_spi.h"
35 static IO_t txEnIO = IO_NONE;
37 static bool consumeExti = true;
39 void A7105Init(uint32_t id, IO_t txEnPin)
41 if (txEnPin) {
42 txEnIO = txEnPin;
43 //TODO: Create resource for this if it ever gets used
44 IOInit(txEnIO, OWNER_RX_SPI_CC2500_TX_EN, 0);
45 IOConfigGPIO(txEnIO, IOCFG_OUT_PP);
46 } else {
47 txEnIO = IO_NONE;
50 A7105SoftReset();
51 A7105WriteID(id);
54 void A7105Config(const uint8_t *regsTable, uint8_t size)
56 if (regsTable) {
57 unsigned timeout = 1000;
59 for (unsigned i = 0; i < size; i++) {
60 if (regsTable[i] != 0xFF) {
61 A7105WriteReg ((A7105Reg_t)i, regsTable[i]);
65 A7105Strobe(A7105_STANDBY);
67 A7105WriteReg(A7105_02_CALC, 0x01);
69 while ((A7105ReadReg(A7105_02_CALC) != 0) && timeout--) {}
71 A7105ReadReg(A7105_22_IF_CALIB_I);
73 A7105WriteReg(A7105_24_VCO_CURCAL, 0x13);
74 A7105WriteReg(A7105_25_VCO_SBCAL_I, 0x09);
75 A7105Strobe(A7105_STANDBY);
79 bool A7105RxTxFinished(timeUs_t *timeStamp) {
80 bool result = false;
82 if (consumeExti && rxSpiPollExti()) {
83 if (rxSpiGetLastExtiTimeUs()) {
84 *timeStamp = rxSpiGetLastExtiTimeUs();
87 rxSpiResetExti();
89 result = true;
91 return result;
94 void A7105SoftReset(void)
96 rxSpiWriteCommand((uint8_t)A7105_00_MODE, 0x00);
99 uint8_t A7105ReadReg(A7105Reg_t reg)
101 return rxSpiReadCommand((uint8_t)reg | 0x40, 0xFF);
104 void A7105WriteReg(A7105Reg_t reg, uint8_t data)
106 rxSpiWriteCommand((uint8_t)reg, data);
109 void A7105Strobe(A7105State_t state)
111 if (A7105_TX == state || A7105_RX == state) {
112 consumeExti = true;
113 rxSpiResetExti();
114 } else {
115 consumeExti = false;
118 if (txEnIO) {
119 if (A7105_TX == state) {
120 IOHi(txEnIO); /* enable PA */
121 } else {
122 IOLo(txEnIO); /* disable PA */
126 rxSpiWriteByte((uint8_t)state);
129 void A7105WriteID(uint32_t id)
131 uint8_t data[4];
132 data[0] = (id >> 24) & 0xFF;
133 data[1] = (id >> 16) & 0xFF;
134 data[2] = (id >> 8) & 0xFF;
135 data[3] = (id >> 0) & 0xFF;
136 rxSpiWriteCommandMulti((uint8_t)A7105_06_ID_DATA, &data[0], sizeof(data));
139 uint32_t A7105ReadID(void)
141 uint32_t id;
142 uint8_t data[4];
143 rxSpiReadCommandMulti((uint8_t)A7105_06_ID_DATA | 0x40, 0xFF, &data[0], sizeof(data));
144 id = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3] << 0;
145 return id;
148 void A7105ReadFIFO(uint8_t *data, uint8_t num)
150 if (data) {
151 if(num > 64) {
152 num = 64;
155 A7105Strobe(A7105_RST_RDPTR); /* reset read pointer */
156 rxSpiReadCommandMulti((uint8_t)A7105_05_FIFO_DATA | 0x40, 0xFF, data, num);
160 void A7105WriteFIFO(uint8_t *data, uint8_t num)
162 if (data) {
163 if(num > 64) {
164 num = 64;
167 A7105Strobe(A7105_RST_WRPTR); /* reset write pointer */
168 rxSpiWriteCommandMulti((uint8_t)A7105_05_FIFO_DATA, data, num);
172 #endif /* USE_RX_FLYSKY */