/********************************************************* Multiprotocol Tx code by Midelic and Pascal Langer(hpnuts) http://www.rcgroups.com/forums/showthread.php?t=2165676 https://github.com/pascallanger/DIY-Multiprotocol-TX-Module/edit/master/README.md Thanks to PhracturedBlue, Hexfet, Goebish, Victzh and all protocol developers Ported from deviation firmware This project is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Multiprotocol is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Multiprotocol. If not, see . */ #include #include //#define DEBUG_TX #include "Multiprotocol.h" //Multiprotocol module configuration file #include "_Config.h" #include "TX_Def.h" //Global constants/variables uint32_t MProtocol_id;//tx id, uint32_t MProtocol_id_master; uint32_t blink=0; uint8_t prev_option; uint8_t prev_power=0xFD; // unused power value // uint16_t counter; uint8_t channel; uint8_t packet[40]; #define NUM_CHN 16 // Servo data uint16_t Servo_data[NUM_CHN]; uint8_t Servo_AUX; uint16_t servo_max_100,servo_min_100,servo_max_125,servo_min_125; // Protocol variables uint8_t cyrfmfg_id[6];//for dsm2 and devo uint8_t rx_tx_addr[5]; uint8_t phase; uint16_t bind_counter; uint8_t bind_phase; uint8_t binding_idx; uint16_t packet_period; uint8_t packet_count; uint8_t packet_sent; uint8_t packet_length; uint8_t hopping_frequency[23]; uint8_t *hopping_frequency_ptr; uint8_t hopping_frequency_no=0; uint8_t rf_ch_num; uint8_t throttle, rudder, elevator, aileron; uint8_t flags; uint16_t crc; // uint16_t state; uint8_t len; uint8_t RX_num; #if defined(FRSKYX_CC2500_INO) || defined(SFHSS_CC2500_INO) uint8_t calData[48]; #endif //Channel mapping for protocols const uint8_t CH_AETR[]={AILERON, ELEVATOR, THROTTLE, RUDDER, AUX1, AUX2, AUX3, AUX4, AUX5, AUX6, AUX7, AUX8}; const uint8_t CH_TAER[]={THROTTLE, AILERON, ELEVATOR, RUDDER, AUX1, AUX2, AUX3, AUX4, AUX5, AUX6, AUX7, AUX8}; const uint8_t CH_RETA[]={RUDDER, ELEVATOR, THROTTLE, AILERON, AUX1, AUX2, AUX3, AUX4, AUX5, AUX6, AUX7, AUX8}; const uint8_t CH_EATR[]={ELEVATOR, AILERON, THROTTLE, RUDDER, AUX1, AUX2, AUX3, AUX4, AUX5, AUX6, AUX7, AUX8}; // Mode_select variables uint8_t mode_select; uint8_t protocol_flags=0,protocol_flags2=0; // PPM variable volatile uint16_t PPM_data[NUM_CHN]; // Serial variables #ifdef INVERT_TELEMETRY // enable bit bash for serial #define BASH_SERIAL 1 #define INVERT_SERIAL 1 #endif #define BAUD 100000 #define RXBUFFER_SIZE 25 #define TXBUFFER_SIZE 32 volatile uint8_t rx_buff[RXBUFFER_SIZE]; volatile uint8_t rx_ok_buff[RXBUFFER_SIZE]; #ifndef BASH_SERIAL volatile uint8_t tx_buff[TXBUFFER_SIZE]; #endif volatile uint8_t discard_frame = 0; //Serial protocol uint8_t sub_protocol; uint8_t option; uint8_t cur_protocol[2]; uint8_t prev_protocol=0; // Telemetry #define MAX_PKT 27 uint8_t pkt[MAX_PKT];//telemetry receiving packets #if defined(TELEMETRY) uint8_t pass = 0; uint8_t pktt[MAX_PKT];//telemetry receiving packets #ifndef BASH_SERIAL volatile uint8_t tx_head=0; volatile uint8_t tx_tail=0; #endif // BASH_SERIAL uint8_t v_lipo; int16_t RSSI_dBm; //const uint8_t RSSI_offset=72;//69 71.72 values db uint8_t telemetry_link=0; uint8_t telemetry_counter=0; #endif // Callback typedef uint16_t (*void_function_t) (void);//pointer to a function with no parameters which return an uint16_t integer void_function_t remote_callback = 0; static void CheckTimer(uint16_t (*cb)(void)); // Init void setup() { #ifdef XMEGA PORTD.OUTSET = 0x17 ; PORTD.DIRSET = 0xB2 ; PORTD.DIRCLR = 0x4D ; PORTD.PIN0CTRL = 0x18 ; PORTD.PIN2CTRL = 0x18 ; PORTE.DIRSET = 0x01 ; PORTE.DIRCLR = 0x02 ; PORTE.OUTSET = 0x01 ; for ( uint8_t count = 0 ; count < 20 ; count += 1 ) asm("nop") ; PORTE.OUTCLR = 0x01 ; #else // General pinout DDRD = (1<>2)&0x07 ) | ( (PINC<<3)&0x08) );//encoder dip switches 1,2,4,8=>B2,B3,B4,C0 #endif // Update LED LED_OFF; LED_SET_OUTPUT; // Read or create protocol id MProtocol_id_master=random_id(10,false); //Init RF modules modules_reset(); #ifdef ENABLE_PPM //Protocol and interrupts initialization if(mode_select != MODE_SERIAL) { // PPM mode_select--; cur_protocol[0] = PPM_prot[mode_select].protocol; sub_protocol = PPM_prot[mode_select].sub_proto; RX_num = PPM_prot[mode_select].rx_num; MProtocol_id = RX_num + MProtocol_id_master; option = PPM_prot[mode_select].option; if(PPM_prot[mode_select].power) POWER_FLAG_on; if(PPM_prot[mode_select].autobind) AUTOBIND_FLAG_on; mode_select++; servo_max_100=PPM_MAX_100; servo_min_100=PPM_MIN_100; servo_max_125=PPM_MAX_125; servo_min_125=PPM_MIN_125; protocol_init(); #ifndef XMEGA //Configure PPM interrupt EICRA |=(1<2*200) { do { Update_All(); } while(remote_callback==0); } #ifdef XMEGA if( (TCC1.INTFLAGS & TC1_CCAIF_bm) != 0) { cli(); // Disable global int due to RW of 16 bits registers TCC1.CCA = TCC1.CNT ; // Callback should already have been called... Use "now" as new sync point. sei(); // Enable global int } else while((TCC1.INTFLAGS & TC1_CCAIF_bm) == 0); // wait before callback #else if( (TIFR1 & (1<4000) { // start to wait here as much as we can... next_callback-=2000; // We will wait below for 2ms #ifdef XMEGA cli(); // Disable global int due to RW of 16 bits registers TCC1.CCA +=2000*2; // set compare A for callback TCC1.INTFLAGS = TC1_CCAIF_bm ; // clear compare A=callback flag sei(); // enable global int Update_All(); if(IS_CHANGE_PROTOCOL_FLAG_on) break; // Protocol has been changed while((TCC1.INTFLAGS & TC1_CCAIF_bm) == 0); // wait 2ms... #else cli(); // Disable global int due to RW of 16 bits registers OCR1A += 2000*2 ; // set compare A for callback TIFR1=(1<PPM_MAX_125) temp_ppm=PPM_MAX_125; Servo_data[i]= temp_ppm ; } update_aux_flags(); PPM_FLAG_off; // wait for next frame before update } #endif //ENABLE_PPM update_led_status(); #if defined(TELEMETRY) uint8_t protocol=cur_protocol[0]&0x1F; if( (protocol==MODE_FRSKY) || (protocol==MODE_HUBSAN) || (protocol==MODE_FRSKYX) || (protocol==MODE_DSM2) ) TelemetryUpdate(); #endif TX_ON; NOP(); TX_OFF; } // Update Servo_AUX flags based on servo AUX positions static void update_aux_flags(void) { Servo_AUX=0; for(uint8_t i=0;i<8;i++) if(Servo_data[AUX1+i]>PPM_SWITCH) Servo_AUX|=1< led on else blink+=BLINK_BIND_TIME; //blink fastly during binding LED_TOGGLE; } } inline void tx_pause() { #ifdef TELEMETRY #ifdef XMEGA USARTC0.CTRLA &= ~0x03 ; // Pause telemetry by disabling transmitter interrupt #else #ifndef BASH_SERIAL UCSR0B &= ~(1<32000) { // next_callback should not be more than 32767 so we will wait here... uint16_t temp=(next_callback>>10)-2; delayMilliseconds(temp); next_callback-=temp<<10; // between 2-3ms left at this stage } cli(); // disable global int #ifdef XMEGA TCC1.CCA = TCC1.CNT + next_callback*2; // set compare A for callback sei(); // enable global int TCC1.INTFLAGS = TC1_CCAIF_bm ; // clear compare A flag #else OCR1A=TCNT1+next_callback*2; // set compare A for callback sei(); // enable global int TIFR1=(1<>4)& 0x07; //subprotocol no (0-7) bits 4-6 RX_num=rx_ok_buff[1]& 0x0F; MProtocol_id=MProtocol_id_master+RX_num;//personalized RX bind + rx num // rx_num bits 0---3 } else if( ((rx_ok_buff[0]&0x80)!=0) && ((cur_protocol[0]&0x80)==0) ) // Bind flag has been set CHANGE_PROTOCOL_FLAG_on; //restart protocol with bind else CHANGE_PROTOCOL_FLAG_off; //no need to restart cur_protocol[0] = rx_ok_buff[0]; //store current protocol // decode channel values volatile uint8_t *p=rx_ok_buff+2; uint8_t dec=-3; for(uint8_t i=0;i=8) { dec-=8; p++; } p++; Servo_data[i]=((((*((uint32_t *)p))>>dec)&0x7FF)*5)/8+860; //value range 860<->2140 -125%<->+125% } RX_DONOTUPDTAE_off; #ifdef XMEGA cli(); #else UCSR0B &= ~(1<>8); } // Channel value is limited to PPM_100 uint16_t limit_channel_100(uint8_t ch) { if(Servo_data[ch]>servo_max_100) return servo_max_100; else if (Servo_data[ch] UBRR0H = UBRRH_VALUE; UBRR0L = UBRRL_VALUE; UCSR0A = 0 ; // Clear X2 bit //Set frame format to 8 data bits, even parity, 2 stop bits UCSR0C = (1<> 24) & 0xFF; rx_tx_addr[1] = (id >> 16) & 0xFF; rx_tx_addr[2] = (id >> 8) & 0xFF; rx_tx_addr[3] = (id >> 0) & 0xFF; rx_tx_addr[4] = 0xC1; // for YD717: always uses first data port } static uint32_t random_id(uint16_t adress, uint8_t create_new) { uint32_t id; uint8_t txid[4]; if (eeprom_read_byte((uint8_t*)(adress+10))==0xf0 && !create_new) { // TXID exists in EEPROM eeprom_read_block((void*)txid,(const void*)adress,4); id=(txid[0] | ((uint32_t)txid[1]<<8) | ((uint32_t)txid[2]<<16) | ((uint32_t)txid[3]<<24)); } else { // if not generate a random ID randomSeed((uint32_t)analogRead(A6)<<10|analogRead(A7));//seed // id = random(0xfefefefe) + ((uint32_t)random(0xfefefefe) << 16); txid[0]= (id &0xFF); txid[1] = ((id >> 8) & 0xFF); txid[2] = ((id >> 16) & 0xFF); txid[3] = ((id >> 24) & 0xFF); eeprom_write_block((const void*)txid,(void*)adress,4); eeprom_write_byte((uint8_t*)(adress+10),0xf0);//write bind flag in eeprom. } return id; } /********************/ /** SPI routines **/ /********************/ void SPI_Write(uint8_t command) { uint8_t n=8; SCK_off;//SCK start low SDI_off; do { if(command&0x80) SDI_on; else SDI_off; SCK_on; command = command << 1; SCK_off; } while(--n) ; SDI_on; } uint8_t SPI_Read(void) { uint8_t result=0,i; for(i=0;i<8;i++) { result=result<<1; if(SDO_1) result |= 0x01; SCK_on; NOP(); SCK_off; } return result; } /************************************/ /** Arduino replacement routines **/ /************************************/ // replacement millis() and micros() // These work polled, no interrupts // micros() MUST be called at least once every 32 milliseconds #ifndef XMEGA uint16_t MillisPrecount ; uint16_t lastTimerValue ; uint32_t TotalMicros ; uint32_t TotalMillis ; uint8_t Correction ; uint32_t micros() { uint16_t elapsed ; uint8_t millisToAdd ; uint8_t oldSREG = SREG ; cli() ; uint16_t time = TCNT1 ; // Read timer 1 SREG = oldSREG ; elapsed = time - lastTimerValue ; elapsed += Correction ; Correction = elapsed & 0x01 ; elapsed >>= 1 ; uint32_t ltime = TotalMicros ; ltime += elapsed ; cli() ; TotalMicros = ltime ; // Done this way for RPM to work correctly lastTimerValue = time ; SREG = oldSREG ; // Still valid from above elapsed += MillisPrecount; millisToAdd = 0 ; if ( elapsed > 15999 ) { millisToAdd = 16 ; elapsed -= 16000 ; } if ( elapsed > 7999 ) { millisToAdd += 8 ; elapsed -= 8000 ; } if ( elapsed > 3999 ) { millisToAdd += 4 ; elapsed -= 4000 ; } if ( elapsed > 1999 ) { millisToAdd += 2 ; elapsed -= 2000 ; } if ( elapsed > 999 ) { millisToAdd += 1 ; elapsed -= 1000 ; } TotalMillis += millisToAdd ; MillisPrecount = elapsed ; return TotalMicros ; } uint32_t millis() { micros() ; return TotalMillis ; } void delayMilliseconds(unsigned long ms) { uint16_t start = (uint16_t)micros(); uint16_t lms = ms ; while (lms > 0) { if (((uint16_t)micros() - start) >= 1000) { lms--; start += 1000; } } } /* Important notes: - Max value is 16000µs - delay is not accurate due to interrupts happening */ void delayMicroseconds(unsigned int us) { if (--us == 0) return; us <<= 2; // * 4 us -= 2; // - 2 __asm__ __volatile__ ( "1: sbiw %0,1" "\n\t" // 2 cycles "brne 1b" : "=w" (us) : "0" (us) // 2 cycles ); } void init() { // this needs to be called before setup() or some functions won't work there sei(); } #endif //XMEGA /**************************/ /**************************/ /** Interrupt routines **/ /**************************/ /**************************/ //PPM #ifdef ENABLE_PPM #ifdef XMEGA ISR(PORTD_INT0_vect) #else ISR(INT1_vect, ISR_NOBLOCK) #endif { // Interrupt on PPM pin static int8_t chan=-1; static uint16_t Prev_TCNT1=0; uint16_t Cur_TCNT1; #ifdef XMEGA Cur_TCNT1 = TCC1.CNT - Prev_TCNT1 ; // Capture current Timer1 value #else Cur_TCNT1=TCNT1-Prev_TCNT1; // Capture current Timer1 value #endif if(Cur_TCNT1<1000) chan=-1; // bad frame else if(Cur_TCNT1>4840) { chan=0; // start of frame PPM_FLAG_on; // full frame present (even at startup since PPM_data has been initialized) } else if(chan!=-1) // need to wait for start of frame { //servo values between 500us and 2420us will end up here PPM_data[chan]= Cur_TCNT1>>1;; if(chan++>=NUM_CHN) chan=-1; // don't accept any new channels } Prev_TCNT1+=Cur_TCNT1; } #endif //ENABLE_PPM //Serial RX #ifdef ENABLE_SERIAL #ifdef XMEGA ISR(USARTC0_RXC_vect) #else ISR(USART_RX_vect) #endif { // RX interrupt static uint8_t idx=0; #ifdef XMEGA if((USARTC0.STATUS & 0x1C)==0) // Check frame error, data overrun and parity error #else UCSR0B &= ~(1<RXBUFFER_SIZE) { // A full frame has been received if(!IS_RX_DONOTUPDTAE_on) { //Good frame received and main is not working on the buffer memcpy((void*)rx_ok_buff,(const void*)rx_buff,RXBUFFER_SIZE);// Duplicate the buffer RX_FLAG_on; // flag for main to process servo data } else RX_MISSED_BUFF_on; // notify that rx_buff is good discard_frame=1; // start again } } } else { #ifdef XMEGA idx = USARTC0.DATA; // Dummy read #else idx=UDR0; // Dummy read #endif discard_frame=1; // Error encountered discard full frame... } if(discard_frame==1) { #ifdef XMEGA TCC1.INTCTRLB &=0xF3; // Disable interrupt on compare B match #else TIMSK1 &=~(1<