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/>.
22 #include "build_config.h"
25 #include "drivers/rx_nrf24l01.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
) {
52 if (nrf24NewPacketAvailable
) {
53 protocolSetRcDataFromPayload(nrf24RcData
, nrf24Payload
);
54 nrf24NewPacketAvailable
= false;
56 return nrf24RcData
[channel
];
59 STATIC_UNIT_TESTED
bool rxNrf24SetProtocol(nrf24_protocol_t protocol
)
63 case NRF24RX_V202_250K
:
65 protocolInit
= v202Init
;
66 protocolDataReceived
= v202DataReceived
;
67 protocolSetRcDataFromPayload
= v202SetRcDataFromPayload
;
72 case NRF24RX_SYMA_X5C
:
73 protocolInit
= symaInit
;
74 protocolDataReceived
= symaDataReceived
;
75 protocolSetRcDataFromPayload
= symaSetRcDataFromPayload
;
81 protocolInit
= cx10Init
;
82 protocolDataReceived
= cx10DataReceived
;
83 protocolSetRcDataFromPayload
= cx10SetRcDataFromPayload
;
88 protocolInit
= h8_3dInit
;
89 protocolDataReceived
= h8_3dDataReceived
;
90 protocolSetRcDataFromPayload
= h8_3dSetRcDataFromPayload
;
95 protocolInit
= refInit
;
96 protocolDataReceived
= refDataReceived
;
97 protocolSetRcDataFromPayload
= refSetRcDataFromPayload
;
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;
122 * Set and initialize the NRF24 protocol
124 bool rxNrf24Init(nfr24l01_spi_type_e spiType
, const rxConfig_t
*rxConfig
, rxRuntimeConfig_t
*rxRuntimeConfig
, rcReadRawDataPtr
*callback
)
127 NRF24L01_SpiInit(spiType
);
128 if (rxNrf24SetProtocol(rxConfig
->nrf24rx_protocol
)) {
129 protocolInit(rxConfig
, rxRuntimeConfig
);
132 nrf24NewPacketAvailable
= false;
134 *callback
= rxNrf24ReadRawRC
;