Merge pull request #11297 from SteveCEvans/baro_state
[betaflight.git] / src / main / drivers / rx / rx_xn297.c
bloba925d7668cd6c75e3a96043467cac584ce68464e
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 // This file borrows heavily from project Deviation,
22 // see http://deviationtx.com
24 #include <stdbool.h>
25 #include <stdint.h>
27 #include "platform.h"
29 #if defined(USE_RX_XN297)
31 #include "common/crc.h"
33 #include "pg/rx.h"
35 #include "drivers/rx/rx_nrf24l01.h"
36 #include "drivers/rx/rx_spi.h"
39 static const uint8_t xn297_data_scramble[30] = {
40 0xbc, 0xe5, 0x66, 0x0d, 0xae, 0x8c, 0x88, 0x12,
41 0x69, 0xee, 0x1f, 0xc7, 0x62, 0x97, 0xd5, 0x0b,
42 0x79, 0xca, 0xcc, 0x1b, 0x5d, 0x19, 0x10, 0x24,
43 0xd3, 0xdc, 0x3f, 0x8e, 0xc5, 0x2f
46 static const uint16_t xn297_crc_xorout[26] = {
47 0x9BA7, 0x8BBB, 0x85E1, 0x3E8C, 0x451E, 0x18E6, 0x6B24, 0xE7AB,
48 0x3828, 0x814B, 0xD461, 0xF494, 0x2503, 0x691D, 0xFE8B, 0x9BA7,
49 0x8B17, 0x2920, 0x8B5F, 0x61B1, 0xD391, 0x7401, 0x2138, 0x129F,
50 0xB3A0, 0x2988
53 static uint8_t bitReverse(uint8_t bIn)
55 uint8_t bOut = 0;
56 for (int ii = 0; ii < 8; ++ii) {
57 bOut = (bOut << 1) | (bIn & 1);
58 bIn >>= 1;
60 return bOut;
64 #define RX_TX_ADDR_LEN 5
66 uint16_t XN297_UnscramblePayload(uint8_t *data, int len, const uint8_t *rxAddr)
68 uint16_t crc = 0xb5d2;
69 if (rxAddr) {
70 for (int ii = 0; ii < RX_TX_ADDR_LEN; ++ii) {
71 crc = crc16_ccitt(crc, rxAddr[RX_TX_ADDR_LEN - 1 - ii]);
74 for (int ii = 0; ii < len; ++ii) {
75 crc = crc16_ccitt(crc, data[ii]);
76 data[ii] = bitReverse(data[ii] ^ xn297_data_scramble[ii]);
78 crc ^= xn297_crc_xorout[len];
79 return crc;
82 void XN297_WritePayload(uint8_t *data, int len, const uint8_t *rxAddr)
84 uint8_t packet[NRF24L01_MAX_PAYLOAD_SIZE];
85 uint16_t crc = 0xb5d2;
86 for (int ii = 0; ii < RX_TX_ADDR_LEN; ++ii) {
87 packet[ii] = rxAddr[RX_TX_ADDR_LEN - 1 - ii];
88 crc = crc16_ccitt(crc, packet[ii]);
90 for (int ii = 0; ii < len; ++ii) {
91 const uint8_t bOut = bitReverse(data[ii]);
92 packet[ii + RX_TX_ADDR_LEN] = bOut ^ xn297_data_scramble[ii];
93 crc = crc16_ccitt(crc, packet[ii + RX_TX_ADDR_LEN]);
95 crc ^= xn297_crc_xorout[len];
96 packet[RX_TX_ADDR_LEN + len] = crc >> 8;
97 packet[RX_TX_ADDR_LEN + len + 1] = crc & 0xff;
98 NRF24L01_WritePayload(packet, RX_TX_ADDR_LEN + len + 2);
100 #endif