diff --git a/Multiprotocol/Bayang_Rx_nrf24l01.ino b/Multiprotocol/Bayang_Rx_nrf24l01.ino index 759100e..ed60493 100644 --- a/Multiprotocol/Bayang_Rx_nrf24l01.ino +++ b/Multiprotocol/Bayang_Rx_nrf24l01.ino @@ -15,20 +15,176 @@ Multiprotocol is distributed in the hope that it will be useful, // Compatible with EAchine H8 mini, H10, BayangToys X6/X7/X9, JJRC JJ850 ... // Last sync with hexfet new_protocols/bayang_nrf24l01.c dated 2015-12-22 -#if defined(BAYANG_NRF24L01_INO) +#if defined(BAYANG_RX_NRF24L01_INO) #include "iface_nrf24l01.h" -uint16_t Bayang_Rx_callback() +#define BAYANG_RX_PACKET_SIZE 15 +#define BAYANG_RX_RF_NUM_CHANNELS 4 +#define BAYANG_RX_RF_BIND_CHANNEL 0 +#define BAYANG_RX_ADDRESS_LENGTH 5 + +enum { + BAYANG_RX_BIND = 0, + BAYANG_RX_DATA +}; + +static void __attribute__((unused)) Bayang_Rx_init_nrf24l01() { - return 1000; + const uint8_t bind_address[BAYANG_RX_ADDRESS_LENGTH] = { 0,0,0,0,0 }; + NRF24L01_Initialize(); + XN297_SetTXAddr(bind_address, BAYANG_RX_ADDRESS_LENGTH); + XN297_SetRXAddr(bind_address, BAYANG_RX_ADDRESS_LENGTH); + NRF24L01_FlushRx(); + NRF24L01_WriteReg(NRF24L01_07_STATUS, 0x70); // Clear data ready, data sent, and retransmit + NRF24L01_WriteReg(NRF24L01_01_EN_AA, 0x00); // No Auto Acknowldgement on all data pipes + NRF24L01_WriteReg(NRF24L01_02_EN_RXADDR, 0x01); // Enable data pipe 0 only + NRF24L01_WriteReg(NRF24L01_11_RX_PW_P0, BAYANG_RX_PACKET_SIZE + 2); // 2 extra bytes for xn297 crc + NRF24L01_WriteReg(NRF24L01_05_RF_CH, BAYANG_RX_RF_BIND_CHANNEL); + NRF24L01_SetBitrate(NRF24L01_BR_1M); // 1Mbps + NRF24L01_SetPower(); + NRF24L01_Activate(0x73); // Activate feature register + NRF24L01_WriteReg(NRF24L01_1C_DYNPD, 0x00); // Disable dynamic payload length on all pipes + NRF24L01_WriteReg(NRF24L01_1D_FEATURE, 0x01); + NRF24L01_Activate(0x73); + NRF24L01_SetTxRxMode(TXRX_OFF); + NRF24L01_FlushRx(); + NRF24L01_SetTxRxMode(RX_EN); + XN297_Configure(_BV(NRF24L01_00_EN_CRC) | _BV(NRF24L01_00_CRCO) | _BV(NRF24L01_00_PWR_UP) | _BV(NRF24L01_00_PRIM_RX)); +} + +static uint8_t __attribute__((unused)) Bayang_Rx_check_validity() { + uint8_t sum = 0; + for (uint8_t i = 0; i < BAYANG_RX_PACKET_SIZE - 1; i++) + sum += packet[i]; + return sum == packet[14]; +} + +static void __attribute__((unused)) Bayang_Rx_build_telemetry_packet() +{ + uint32_t bits = 0; + uint8_t bitsavailable = 0; + uint8_t idx = 0; + + packet_in[idx++] = RX_LQI; + packet_in[idx++] = 100; // no RSSI + packet_in[idx++] = 0; // start channel + packet_in[idx++] = 8; // number of channels in packet + + // convert & pack channels + for (uint8_t i = 0; i < 14; i++) { + uint32_t val = 0; + if (i < 4) { + // AETR + val = (((packet[4 + i * 2] & ~0x7C) << 8) | packet[5 + i * 2]) << 1; + } + else if ((i == 4) && (packet[2] & 0x08) || // flip + (i == 5) && (packet[2] & 0x01) || // rth + (i == 6) && (packet[2] & 0x20) || // picture + (i == 7) && (packet[2] & 0x10)) { // video + // set channel to 100% if feature is enabled + val = 2047; + } + bits |= val << bitsavailable; + bitsavailable += 11; + while (bitsavailable >= 8) { + packet_in[idx++] = bits & 0xff; + bits >>= 8; + bitsavailable -= 8; + } + } } uint16_t initBayang_Rx() { + uint8_t i; + Bayang_Rx_init_nrf24l01(); + hopping_frequency_no = 0; + rx_data_started = false; + + if (IS_BIND_IN_PROGRESS) { + phase = BAYANG_RX_BIND; + } + else { + uint16_t temp = BAYANG_RX_EEPROM_OFFSET; + for (i = 0; i < 5; i++) + rx_tx_addr[i] = eeprom_read_byte((EE_ADDR)temp++); + for (i = 0; i < BAYANG_RX_RF_NUM_CHANNELS; i++) + hopping_frequency[i] = eeprom_read_byte((EE_ADDR)temp++); + XN297_SetTXAddr(rx_tx_addr, BAYANG_RX_ADDRESS_LENGTH); + XN297_SetRXAddr(rx_tx_addr, BAYANG_RX_ADDRESS_LENGTH); + phase = BAYANG_RX_DATA; + } return 1000; } +uint16_t Bayang_Rx_callback() +{ + static uint8_t i; + static int8_t read_retry; + static uint16_t pps_counter; + static uint32_t pps_timer = 0; + + switch (phase) { + case BAYANG_RX_BIND: + if (NRF24L01_ReadReg(NRF24L01_07_STATUS) & _BV(NRF24L01_07_RX_DR)) { + // data received from TX + if (XN297_ReadPayload(packet, BAYANG_RX_PACKET_SIZE) && packet[0] == 0xA4 && Bayang_Rx_check_validity()) { + // store tx info into eeprom + uint16_t temp = BAYANG_RX_EEPROM_OFFSET; + for (i = 0; i < 5; i++) { + rx_tx_addr[i] = packet[i + 1]; + eeprom_write_byte((EE_ADDR)temp++, rx_tx_addr[i]); + } + for (i = 0; i < 4; i++) { + hopping_frequency[i] = packet[i + 6]; + eeprom_write_byte((EE_ADDR)temp++, hopping_frequency[i]); + } + XN297_SetTXAddr(rx_tx_addr, BAYANG_RX_ADDRESS_LENGTH); + XN297_SetRXAddr(rx_tx_addr, BAYANG_RX_ADDRESS_LENGTH); + BIND_DONE; + phase = BAYANG_RX_DATA; + } + NRF24L01_FlushRx(); + NRF24L01_WriteReg(NRF24L01_07_STATUS, 0x70); + } + break; + case BAYANG_RX_DATA: + if (NRF24L01_ReadReg(NRF24L01_07_STATUS) & _BV(NRF24L01_07_RX_DR)) { + if (XN297_ReadPayload(packet, BAYANG_RX_PACKET_SIZE) && packet[0] == 0xA5 && Bayang_Rx_check_validity()) { + if (telemetry_link == 0) { + Bayang_Rx_build_telemetry_packet(); + telemetry_link = 1; + } + rx_data_started = true; + read_retry = 4; + pps_counter++; + } + } + // packets per second + if (millis() - pps_timer >= 1000) { + pps_timer = millis(); + debugln("%d pps", pps_counter); + RX_LQI = pps_counter / 2; + pps_counter = 0; + } + + // frequency hopping + if (read_retry++ >= 4) { + hopping_frequency_no++; + if (hopping_frequency_no >= BAYANG_RX_RF_NUM_CHANNELS) + hopping_frequency_no = 0; + NRF24L01_WriteReg(NRF24L01_05_RF_CH, hopping_frequency[hopping_frequency_no]); + NRF24L01_FlushRx(); + NRF24L01_WriteReg(NRF24L01_07_STATUS, 0x70); + if (rx_data_started) + read_retry = 0; + else + read_retry = -16; // retry longer until first packet is caught + } + return 250; + } + return 1000; +} #endif - diff --git a/Multiprotocol/Multiprotocol.h b/Multiprotocol/Multiprotocol.h index 92182cd..5f80408 100644 --- a/Multiprotocol/Multiprotocol.h +++ b/Multiprotocol/Multiprotocol.h @@ -602,8 +602,8 @@ enum { #define AFHDS2A_RX_EEPROM_OFFSET 230 // (4) TX ID + (16) channels, 20 bytes, end is 230+20=250 #define AFHDS2A_EEPROM_OFFSET2 250 // RX ID, 4 bytes per model id, end is 250+192=442 #define HOTT_EEPROM_OFFSET 442 // RX ID, 5 bytes per model id, end is 320+442=762 -#define BAYANG_RX_EEPROM_OFFSET 762 // (5) TX ID + (4) channels + (1) extra AUX, 10 bytes, end is 772 -//#define CONFIG_EEPROM_OFFSET 772 // Current configuration of the multimodule +#define BAYANG_RX_EEPROM_OFFSET 762 // (5) TX ID + (4) channels, 9 bytes, end is 771 +//#define CONFIG_EEPROM_OFFSET 771 // Current configuration of the multimodule //**************************************** //*** MULTI protocol serial definition *** diff --git a/Multiprotocol/Multiprotocol.ino b/Multiprotocol/Multiprotocol.ino index b58eb97..5103fcf 100644 --- a/Multiprotocol/Multiprotocol.ino +++ b/Multiprotocol/Multiprotocol.ino @@ -227,7 +227,7 @@ uint8_t packet_in[TELEMETRY_BUFFER_SIZE];//telemetry receiving packets #endif //RX protocols - #if defined(AFHDS2A_RX_A7105_INO) || defined(FRSKY_RX_CC2500_INO) + #if defined(AFHDS2A_RX_A7105_INO) || defined(FRSKY_RX_CC2500_INO) || defined(BAYANG_RX_NRF24L01_INO) bool rx_data_started; bool rx_disable_lna; uint16_t rx_rc_chan[16]; diff --git a/Multiprotocol/Telemetry.ino b/Multiprotocol/Telemetry.ino index cb6fc7a..892fcf1 100644 --- a/Multiprotocol/Telemetry.ino +++ b/Multiprotocol/Telemetry.ino @@ -931,10 +931,10 @@ void TelemetryUpdate() #endif #if defined (FRSKY_RX_TELEMETRY) || defined(AFHDS2A_RX_TELEMETRY) || defined (BAYANG_RX_TELEMETRY) - if (telemetry_link && (protocol == PROTO_FRSKY_RX || protocol == PROTO_AFHDS2A_RX || protocol == PROTO_BAYANG_RX)) + if ((telemetry_link & 1) && (protocol == PROTO_FRSKY_RX || protocol == PROTO_AFHDS2A_RX || protocol == PROTO_BAYANG_RX)) { receiver_channels_frame(); - telemetry_link = 0; + telemetry_link &= ~1; return; } #endif