2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
18 // This file borrows heavily from project Deviation,
19 // see http://deviationtx.com
29 #include "build/build_config.h"
31 #include "drivers/io.h"
32 #include "drivers/rx/rx_nrf24l01.h"
33 #include "drivers/rx/rx_xn297.h"
34 #include "drivers/time.h"
37 #include "rx/rx_spi.h"
38 #include "rx/nrf24_cx10.h"
41 * Deviation transmitter
42 * Bind phase lasts 6 seconds for CX10, for CX10A it lasts until an acknowledgment is received.
43 * Other transmitters may vary but should have similar characteristics.
44 * For CX10A protocol: after receiving a bind packet, the receiver must send back a data packet with byte[9] = 1 as acknowledgment
49 * No auto acknowledgment
50 * Payload size is 19 and static for CX10A variant, 15 and static for CX10 variant.
53 * uses address {0xcc, 0xcc, 0xcc, 0xcc, 0xcc}, converted by XN297
56 * uses same address as bind phase
57 * hops between 4 channels that are set from the txId sent in the bind packet
60 #define RC_CHANNEL_COUNT 9
68 #define FLAG_FLIP 0x10 // goes to rudder channel
70 #define FLAG_MODE_MASK 0x03
71 #define FLAG_HEADLESS 0x04
73 #define FLAG_VIDEO 0x02
74 #define FLAG_PICTURE 0x04
76 static rx_spi_protocol_e cx10Protocol
;
84 STATIC_UNIT_TESTED protocol_state_t protocolState
;
86 #define CX10_PROTOCOL_PAYLOAD_SIZE 15
87 #define CX10A_PROTOCOL_PAYLOAD_SIZE 19
88 static uint8_t payloadSize
;
89 #define ACK_TO_SEND_COUNT 8
92 #define RX_TX_ADDR_LEN 5
93 STATIC_UNIT_TESTED
uint8_t txAddr
[RX_TX_ADDR_LEN
] = {0x55, 0x0F, 0x71, 0x0C, 0x00}; // converted XN297 address, 0xC710F55 (28 bit)
94 STATIC_UNIT_TESTED
uint8_t rxAddr
[RX_TX_ADDR_LEN
] = {0x49, 0x26, 0x87, 0x7d, 0x2f}; // converted XN297 address
96 STATIC_UNIT_TESTED
uint8_t txId
[TX_ID_LEN
];
98 #define CX10_RF_BIND_CHANNEL 0x02
99 #define RF_CHANNEL_COUNT 4
100 STATIC_UNIT_TESTED
uint8_t cx10RfChannelIndex
= 0;
101 STATIC_UNIT_TESTED
uint8_t cx10RfChannels
[RF_CHANNEL_COUNT
]; // channels are set using txId from bind packet
103 #define CX10_PROTOCOL_HOP_TIMEOUT 1500 // 1.5ms
104 #define CX10A_PROTOCOL_HOP_TIMEOUT 6500 // 6.5ms
105 static uint32_t hopTimeout
;
106 static uint32_t timeOfLastHop
;
109 * Returns true if it is a bind packet.
111 STATIC_UNIT_TESTED
bool cx10CheckBindPacket(const uint8_t *packet
)
113 const bool bindPacket
= (packet
[0] == 0xaa); // 10101010
124 STATIC_UNIT_TESTED
uint16_t cx10ConvertToPwmUnsigned(const uint8_t *pVal
)
126 uint16_t ret
= (*(pVal
+ 1)) & 0x7f; // mask out top bit which is used for a flag for the rudder
127 ret
= (ret
<< 8) | *pVal
;
131 void cx10Nrf24SetRcDataFromPayload(uint16_t *rcData
, const uint8_t *payload
)
133 const uint8_t offset
= (cx10Protocol
== RX_SPI_NRF24_CX10
) ? 0 : 4;
134 rcData
[RC_SPI_ROLL
] = (PWM_RANGE_MAX
+ PWM_RANGE_MIN
) - cx10ConvertToPwmUnsigned(&payload
[5 + offset
]); // aileron
135 rcData
[RC_SPI_PITCH
] = (PWM_RANGE_MAX
+ PWM_RANGE_MIN
) - cx10ConvertToPwmUnsigned(&payload
[7 + offset
]); // elevator
136 rcData
[RC_SPI_THROTTLE
] = cx10ConvertToPwmUnsigned(&payload
[9 + offset
]); // throttle
137 rcData
[RC_SPI_YAW
] = cx10ConvertToPwmUnsigned(&payload
[11 + offset
]); // rudder
138 const uint8_t flags1
= payload
[13 + offset
];
139 const uint8_t rate
= flags1
& FLAG_MODE_MASK
; // takes values 0, 1, 2
140 if (rate
== RATE_LOW
) {
141 rcData
[RC_CHANNEL_RATE
] = PWM_RANGE_MIN
;
142 } else if (rate
== RATE_MID
) {
143 rcData
[RC_CHANNEL_RATE
] = PWM_RANGE_MIDDLE
;
145 rcData
[RC_CHANNEL_RATE
] = PWM_RANGE_MAX
;
147 // flip flag is in YAW byte
148 rcData
[RC_CHANNEL_FLIP
] = payload
[12 + offset
] & FLAG_FLIP
? PWM_RANGE_MAX
: PWM_RANGE_MIN
;
149 const uint8_t flags2
= payload
[14 + offset
];
150 rcData
[RC_CHANNEL_PICTURE
] = flags2
& FLAG_PICTURE
? PWM_RANGE_MAX
: PWM_RANGE_MIN
;
151 rcData
[RC_CHANNEL_VIDEO
] = flags2
& FLAG_VIDEO
? PWM_RANGE_MAX
: PWM_RANGE_MIN
;
152 rcData
[RC_CHANNEL_HEADLESS
] = flags1
& FLAG_HEADLESS
? PWM_RANGE_MAX
: PWM_RANGE_MIN
;
155 static void cx10HopToNextChannel(void)
157 ++cx10RfChannelIndex
;
158 if (cx10RfChannelIndex
>= RF_CHANNEL_COUNT
) {
159 cx10RfChannelIndex
= 0;
161 NRF24L01_SetChannel(cx10RfChannels
[cx10RfChannelIndex
]);
164 // The hopping channels are determined by the txId
165 STATIC_UNIT_TESTED
void cx10SetHoppingChannels(const uint8_t *txId
)
167 cx10RfChannelIndex
= 0;
168 cx10RfChannels
[0] = 0x03 + (txId
[0] & 0x0F);
169 cx10RfChannels
[1] = 0x16 + (txId
[0] >> 4);
170 cx10RfChannels
[2] = 0x2D + (txId
[1] & 0x0F);
171 cx10RfChannels
[3] = 0x40 + (txId
[1] >> 4);
174 static bool cx10CrcOK(uint16_t crc
, const uint8_t *payload
)
176 if (payload
[payloadSize
] != (crc
>> 8)) {
179 if (payload
[payloadSize
+ 1] != (crc
& 0xff)) {
185 static bool cx10ReadPayloadIfAvailable(uint8_t *payload
)
187 if (NRF24L01_ReadPayloadIfAvailable(payload
, payloadSize
+ CRC_LEN
)) {
188 const uint16_t crc
= XN297_UnscramblePayload(payload
, payloadSize
, rxAddr
);
189 if (cx10CrcOK(crc
, payload
)) {
197 * This is called periodically by the scheduler.
198 * Returns RX_SPI_RECEIVED_DATA if a data packet was received.
200 rx_spi_received_e
cx10Nrf24DataReceived(uint8_t *payload
)
202 static uint8_t ackCount
;
203 rx_spi_received_e ret
= RX_SPI_RECEIVED_NONE
;
207 switch (protocolState
) {
209 if (cx10ReadPayloadIfAvailable(payload
)) {
210 const bool bindPacket
= cx10CheckBindPacket(payload
);
212 // set the hopping channels as determined by the txId received in the bind packet
213 cx10SetHoppingChannels(txId
);
214 ret
= RX_SPI_RECEIVED_BIND
;
215 protocolState
= STATE_ACK
;
221 // transmit an ACK packet
224 // send out an ACK on the bind channel, required by deviationTx
226 NRF24L01_SetChannel(CX10_RF_BIND_CHANNEL
);
228 XN297_WritePayload(payload
, payloadSize
, rxAddr
);
229 NRF24L01_SetTxMode();// enter transmit mode to send the packet
230 // wait for the ACK packet to send before changing channel
231 static const int fifoDelayUs
= 100;
232 while (!(NRF24L01_ReadReg(NRF24L01_17_FIFO_STATUS
) & BV(NRF24L01_17_FIFO_STATUS_TX_EMPTY
))) {
233 delayMicroseconds(fifoDelayUs
);
234 totalDelayUs
+= fifoDelayUs
;
236 // send out an ACK on each of the hopping channels, required by CX10 transmitter
237 for (int ii
= 0; ii
< RF_CHANNEL_COUNT
; ++ii
) {
238 NRF24L01_SetChannel(cx10RfChannels
[ii
]);
239 XN297_WritePayload(payload
, payloadSize
, rxAddr
);
240 NRF24L01_SetTxMode();// enter transmit mode to send the packet
241 // wait for the ACK packet to send before changing channel
242 while (!(NRF24L01_ReadReg(NRF24L01_17_FIFO_STATUS
) & BV(NRF24L01_17_FIFO_STATUS_TX_EMPTY
))) {
243 delayMicroseconds(fifoDelayUs
);
244 totalDelayUs
+= fifoDelayUs
;
247 static const int delayBetweenPacketsUs
= 1000;
248 if (totalDelayUs
< delayBetweenPacketsUs
) {
249 delayMicroseconds(delayBetweenPacketsUs
- totalDelayUs
);
251 NRF24L01_SetRxMode();//reenter receive mode after sending ACKs
252 if (ackCount
> ACK_TO_SEND_COUNT
) {
253 NRF24L01_SetChannel(cx10RfChannels
[0]);
254 // and go into data state to wait for first data packet
255 protocolState
= STATE_DATA
;
259 timeNowUs
= micros();
260 // read the payload, processing of payload is deferred
261 if (cx10ReadPayloadIfAvailable(payload
)) {
262 cx10HopToNextChannel();
263 timeOfLastHop
= timeNowUs
;
264 ret
= RX_SPI_RECEIVED_DATA
;
266 if (timeNowUs
> timeOfLastHop
+ hopTimeout
) {
267 cx10HopToNextChannel();
268 timeOfLastHop
= timeNowUs
;
274 static void cx10Nrf24Setup(rx_spi_protocol_e protocol
)
276 cx10Protocol
= protocol
;
277 protocolState
= STATE_BIND
;
278 payloadSize
= (protocol
== RX_SPI_NRF24_CX10
) ? CX10_PROTOCOL_PAYLOAD_SIZE
: CX10A_PROTOCOL_PAYLOAD_SIZE
;
279 hopTimeout
= (protocol
== RX_SPI_NRF24_CX10
) ? CX10_PROTOCOL_HOP_TIMEOUT
: CX10A_PROTOCOL_HOP_TIMEOUT
;
281 NRF24L01_Initialize(0); // sets PWR_UP, no CRC
282 NRF24L01_SetupBasic();
284 NRF24L01_SetChannel(CX10_RF_BIND_CHANNEL
);
286 NRF24L01_WriteReg(NRF24L01_06_RF_SETUP
, NRF24L01_06_RF_SETUP_RF_DR_1Mbps
| NRF24L01_06_RF_SETUP_RF_PWR_n12dbm
);
287 // RX_ADDR for pipes P2 to P5 are left at default values
289 NRF24L01_WriteRegisterMulti(NRF24L01_10_TX_ADDR
, txAddr
, RX_TX_ADDR_LEN
);
290 NRF24L01_WriteRegisterMulti(NRF24L01_0A_RX_ADDR_P0
, rxAddr
, RX_TX_ADDR_LEN
);
292 NRF24L01_WriteReg(NRF24L01_11_RX_PW_P0
, payloadSize
+ CRC_LEN
); // payload + 2 bytes CRC
294 NRF24L01_SetRxMode(); // enter receive mode to start listening for packets
297 bool cx10Nrf24Init(const rxConfig_t
*rxConfig
, rxRuntimeConfig_t
*rxRuntimeConfig
)
299 rxRuntimeConfig
->channelCount
= RC_CHANNEL_COUNT
;
300 cx10Nrf24Setup((rx_spi_protocol_e
)rxConfig
->rx_spi_protocol
);