Merge pull request #11297 from SteveCEvans/baro_state
[betaflight.git] / src / main / drivers / rx / rx_cyrf6936.c
blobce496ecb8a12785245c946e9cd3274eb2ce19e78
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_SPEKTRUM
29 #include "drivers/bus_spi.h"
30 #include "drivers/io.h"
31 #include "drivers/io_impl.h"
32 #include "drivers/rx/rx_cyrf6936.h"
33 #include "drivers/rx/rx_spi.h"
35 bool isError = false;
37 bool cyrf6936RxFinished(timeUs_t *timeStamp)
39 if (rxSpiPollExti()) {
40 if (rxSpiGetLastExtiTimeUs()) {
41 *timeStamp = rxSpiGetLastExtiTimeUs();
44 uint8_t rxIrqStatus = cyrf6936ReadRegister(CYRF6936_RX_IRQ_STATUS);
45 if ((rxIrqStatus & CYRF6936_RXC_IRQ) || (rxIrqStatus & CYRF6936_RXE_IRQ)) {
46 isError = (rxIrqStatus & CYRF6936_RXE_IRQ) > 0x0;
49 rxSpiResetExti();
51 return true;
53 return false;
56 bool cyrf6936Init(void)
58 if (!rxSpiExtiConfigured()) {
59 return false;
62 uint16_t timeout = 1000;
63 do { // Check if chip has waken up
64 cyrf6936WriteRegister(CYRF6936_XACT_CFG, 0x82);
65 } while ((cyrf6936ReadRegister(CYRF6936_XACT_CFG) != 0x82) && timeout--);
67 // Soft reset
68 cyrf6936WriteRegister(CYRF6936_MODE_OVERRIDE, CYRF6936_RST);
70 // Verify the CYRF chip is responding
71 return cyrf6936ReadRegister(CYRF6936_FRAMING_CFG) == 0xA5;
74 void cyrf6936WriteRegister(const uint8_t address, const uint8_t data)
76 rxSpiWriteCommand(CYRF6936_DIR | address, data);
79 void cyrf6936WriteBlock(const uint8_t address, const uint8_t *data, const uint8_t length)
81 rxSpiWriteCommandMulti(CYRF6936_DIR | address, &data[0], length);
84 uint8_t cyrf6936ReadRegister(const uint8_t address)
86 return rxSpiReadCommand(address, 0xFF);
89 void cyrf6936ReadBlock(const uint8_t address, uint8_t data[], const uint8_t length)
91 rxSpiReadCommandMulti(address, 0xFF, &data[0], length);
94 uint8_t cyrf6936GetRssi(void)
96 return cyrf6936ReadRegister(CYRF6936_RSSI) & 0x1F; //5 bit value 0 - 31
99 uint8_t cyrf6936GetRxStatus(void)
101 return cyrf6936ReadRegister(CYRF6936_RX_STATUS);
104 void cyrf6936SetConfigLen(const uint8_t config[][2], const uint8_t length)
106 for (unsigned i = 0; i < length; i++) {
107 cyrf6936WriteRegister(config[i][0], config[i][1]);
111 void cyrf6936SetChannel(const uint8_t chan)
113 cyrf6936WriteRegister(CYRF6936_CHANNEL, chan);
116 void cyrf6936SetMode(const uint8_t mode, const bool force)
118 if (force) {
119 cyrf6936WriteRegister(CYRF6936_XACT_CFG, mode | CYRF6936_FRC_END);
120 } else {
121 cyrf6936WriteRegister(CYRF6936_XACT_CFG, mode);
125 void cyrf6936SetCrcSeed(const uint16_t crc)
127 cyrf6936WriteRegister(CYRF6936_CRC_SEED_LSB, crc & 0xff);
128 cyrf6936WriteRegister(CYRF6936_CRC_SEED_MSB, crc >> 8);
131 void cyrf6936SetSopCode(const uint8_t *sopcode)
133 cyrf6936WriteBlock(CYRF6936_SOP_CODE, sopcode, 8);
136 void cyrf6936SetDataCode(const uint8_t *datacode)
138 cyrf6936WriteBlock(CYRF6936_DATA_CODE, datacode, 16);
141 void cyrf6936SendLen(const uint8_t *data, const uint8_t length)
143 cyrf6936WriteRegister(CYRF6936_TX_LENGTH, length);
144 cyrf6936WriteRegister(CYRF6936_TX_CTRL, CYRF6936_TX_CLR);
145 cyrf6936WriteBlock(CYRF6936_TX_BUFFER, data, length);
146 cyrf6936WriteRegister(CYRF6936_TX_CTRL, CYRF6936_TX_GO);
149 void cyrf6936StartRecv(void)
151 cyrf6936WriteRegister(CYRF6936_RX_IRQ_STATUS, CYRF6936_RXOW_IRQ);
152 cyrf6936WriteRegister(CYRF6936_RX_CTRL, CYRF6936_RX_GO | CYRF6936_RXC_IRQEN | CYRF6936_RXE_IRQEN);
155 void cyrf6936RecvLen(uint8_t *data, const uint8_t length)
157 cyrf6936ReadBlock(CYRF6936_RX_BUFFER, data, length);
160 #endif /* USE_RX_SPEKTRUM */