Added nrf24 reference protocol. Added CC3D targets.
[betaflight.git] / src / main / rx / nrf24.c
blob5404147fa206fd1038cdfb7d887635ac8cc98cd7
1 /*
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 #include <stdbool.h>
19 #include <stdint.h>
21 #include <platform.h>
22 #include "build_config.h"
24 #ifdef USE_RX_NRF24
25 #include "drivers/rx_nrf24l01.h"
26 #include "rx/rx.h"
27 #include "rx/nrf24.h"
28 #include "rx/nrf24_cx10.h"
29 #include "rx/nrf24_syma.h"
30 #include "rx/nrf24_v202.h"
31 #include "rx/nrf24_h8_3d.h"
32 #include "rx/nrf24_ref.h"
35 uint16_t nrf24RcData[MAX_SUPPORTED_RC_CHANNEL_COUNT];
36 STATIC_UNIT_TESTED uint8_t nrf24Payload[NRF24L01_MAX_PAYLOAD_SIZE];
37 STATIC_UNIT_TESTED uint8_t nrf24NewPacketAvailable; // set true when a new packet is received
39 typedef void (*protocolInitPtr)(const rxConfig_t *rxConfig, rxRuntimeConfig_t *rxRuntimeConfig);
40 typedef nrf24_received_t (*protocolDataReceivedPtr)(uint8_t *payload);
41 typedef void (*protocolSetRcDataFromPayloadPtr)(uint16_t *rcData, const uint8_t *payload);
43 static protocolInitPtr protocolInit;
44 static protocolDataReceivedPtr protocolDataReceived;
45 static protocolSetRcDataFromPayloadPtr protocolSetRcDataFromPayload;
47 STATIC_UNIT_TESTED uint16_t rxNrf24ReadRawRC(rxRuntimeConfig_t *rxRuntimeConfig, uint8_t channel)
49 if (channel >= rxRuntimeConfig->channelCount) {
50 return 0;
52 if (nrf24NewPacketAvailable) {
53 protocolSetRcDataFromPayload(nrf24RcData, nrf24Payload);
54 nrf24NewPacketAvailable = false;
56 return nrf24RcData[channel];
59 STATIC_UNIT_TESTED bool rxNrf24SetProtocol(nrf24_protocol_t protocol)
61 switch (protocol) {
62 #ifdef USE_RX_V202
63 case NRF24RX_V202_250K:
64 case NRF24RX_V202_1M:
65 protocolInit = v202Init;
66 protocolDataReceived = v202DataReceived;
67 protocolSetRcDataFromPayload = v202SetRcDataFromPayload;
68 break;
69 #endif
70 #ifdef USE_RX_SYMA
71 case NRF24RX_SYMA_X:
72 case NRF24RX_SYMA_X5C:
73 protocolInit = symaInit;
74 protocolDataReceived = symaDataReceived;
75 protocolSetRcDataFromPayload = symaSetRcDataFromPayload;
76 break;
77 #endif
78 #ifdef USE_RX_CX10
79 case NRF24RX_CX10:
80 case NRF24RX_CX10A:
81 protocolInit = cx10Init;
82 protocolDataReceived = cx10DataReceived;
83 protocolSetRcDataFromPayload = cx10SetRcDataFromPayload;
84 break;
85 #endif
86 #ifdef USE_RX_H8_3D
87 case NRF24RX_H8_3D:
88 protocolInit = h8_3dInit;
89 protocolDataReceived = h8_3dDataReceived;
90 protocolSetRcDataFromPayload = h8_3dSetRcDataFromPayload;
91 break;
92 #endif
93 #ifdef USE_RX_REF
94 case NRF24RX_REF:
95 protocolInit = refInit;
96 protocolDataReceived = refDataReceived;
97 protocolSetRcDataFromPayload = refSetRcDataFromPayload;
98 break;
99 #endif
100 default:
101 return false;
102 break;
104 return true;
108 * Returns true if the NRF24L01 has received new data.
109 * Called from updateRx in rx.c, updateRx called from taskUpdateRxCheck.
110 * If taskUpdateRxCheck returns true, then taskUpdateRxMain will shortly be called.
112 bool rxNrf24DataReceived(void)
114 if (protocolDataReceived(nrf24Payload) == NRF24_RECEIVED_DATA) {
115 nrf24NewPacketAvailable = true;
116 return true;
118 return false;
122 * Set and initialize the NRF24 protocol
124 bool rxNrf24Init(nfr24l01_spi_type_e spiType, const rxConfig_t *rxConfig, rxRuntimeConfig_t *rxRuntimeConfig, rcReadRawDataPtr *callback)
126 bool ret = false;
127 NRF24L01_SpiInit(spiType);
128 if (rxNrf24SetProtocol(rxConfig->nrf24rx_protocol)) {
129 protocolInit(rxConfig, rxRuntimeConfig);
130 ret = true;
132 nrf24NewPacketAvailable = false;
133 if (callback) {
134 *callback = rxNrf24ReadRawRC;
136 return ret;
138 #endif