./src/utils/ trim trailing whitestpaces (#14082)
[betaflight.git] / src / main / drivers / rx / rx_a7105.c
blob391f03176db7cbe9698bc737ed87cfe768a30eb7
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)
81 bool result = false;
83 if (consumeExti && rxSpiPollExti()) {
84 if (rxSpiGetLastExtiTimeUs()) {
85 *timeStamp = rxSpiGetLastExtiTimeUs();
88 rxSpiResetExti();
90 result = true;
92 return result;
95 void A7105SoftReset(void)
97 rxSpiWriteCommand((uint8_t)A7105_00_MODE, 0x00);
100 uint8_t A7105ReadReg(A7105Reg_t reg)
102 return rxSpiReadCommand((uint8_t)reg | 0x40, 0xFF);
105 void A7105WriteReg(A7105Reg_t reg, uint8_t data)
107 rxSpiWriteCommand((uint8_t)reg, data);
110 void A7105Strobe(A7105State_t state)
112 if (A7105_TX == state || A7105_RX == state) {
113 consumeExti = true;
114 rxSpiResetExti();
115 } else {
116 consumeExti = false;
119 if (txEnIO) {
120 if (A7105_TX == state) {
121 IOHi(txEnIO); /* enable PA */
122 } else {
123 IOLo(txEnIO); /* disable PA */
127 rxSpiWriteByte((uint8_t)state);
130 void A7105WriteID(uint32_t id)
132 uint8_t data[4];
133 data[0] = (id >> 24) & 0xFF;
134 data[1] = (id >> 16) & 0xFF;
135 data[2] = (id >> 8) & 0xFF;
136 data[3] = (id >> 0) & 0xFF;
137 rxSpiWriteCommandMulti((uint8_t)A7105_06_ID_DATA, &data[0], sizeof(data));
140 uint32_t A7105ReadID(void)
142 uint32_t id;
143 uint8_t data[4];
144 rxSpiReadCommandMulti((uint8_t)A7105_06_ID_DATA | 0x40, 0xFF, &data[0], sizeof(data));
145 id = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3] << 0;
146 return id;
149 void A7105ReadFIFO(uint8_t *data, uint8_t num)
151 if (data) {
152 if(num > 64) {
153 num = 64;
156 A7105Strobe(A7105_RST_RDPTR); /* reset read pointer */
157 rxSpiReadCommandMulti((uint8_t)A7105_05_FIFO_DATA | 0x40, 0xFF, data, num);
161 void A7105WriteFIFO(uint8_t *data, uint8_t num)
163 if (data) {
164 if(num > 64) {
165 num = 64;
168 A7105Strobe(A7105_RST_WRPTR); /* reset write pointer */
169 rxSpiWriteCommandMulti((uint8_t)A7105_05_FIFO_DATA, data, num);
173 #endif /* USE_RX_FLYSKY */