diff --git a/Multiprotocol/A7105_SPI.ino b/Multiprotocol/A7105_SPI.ino index 8327c77..251d31e 100644 --- a/Multiprotocol/A7105_SPI.ino +++ b/Multiprotocol/A7105_SPI.ino @@ -12,12 +12,9 @@ You should have received a copy of the GNU General Public License along with Multiprotocol. If not, see . */ - -//------------------------------- -//------------------------------- -//A7105 SPI routines -//------------------------------- -//------------------------------- +/********************/ +/** A7105 routines **/ +/********************/ #include "iface_a7105.h" void A7105_WriteData(uint8_t len, uint8_t channel) @@ -39,7 +36,7 @@ void A7105_ReadData() { A7105_CSN_off; SPI_Write(0x45); for (i=0;i<16;i++) - packet[i]=A7105_Read(); + packet[i]=SPI_SDIO_Read(); A7105_CSN_on; } @@ -55,28 +52,11 @@ uint8_t A7105_ReadReg(uint8_t address) { uint8_t result; A7105_CSN_off; SPI_Write(address |=0x40); //bit 6 =1 for reading - result = A7105_Read(); + result = SPI_SDIO_Read(); A7105_CSN_on; return(result); } -uint8_t A7105_Read(void) -{ - uint8_t result=0; - SDI_input; - for(uint8_t i=0;i<8;i++) - { - result=result<<1; - if(SDI_1) ///if SDIO =1 - result |= 0x01; - SCLK_on; - NOP(); - SCLK_off; - } - SDI_output; - return result; -} - //------------------------ void A7105_SetTxRxMode(uint8_t mode) { diff --git a/Multiprotocol/ASSAN_nrf24l01.ino b/Multiprotocol/ASSAN_nrf24l01.ino index 5295a07..1a94d74 100644 --- a/Multiprotocol/ASSAN_nrf24l01.ino +++ b/Multiprotocol/ASSAN_nrf24l01.ino @@ -75,7 +75,7 @@ uint16_t ASSAN_callback() phase++; case ASSAN_BIND1: //Wait for receiver to send the frames - if( NRF24L01_ReadReg(NRF24L01_07_STATUS) & BV(NRF24L01_07_RX_DR)) + if( NRF24L01_ReadReg(NRF24L01_07_STATUS) & _BV(NRF24L01_07_RX_DR)) { //Something has been received NRF24L01_ReadPayload(packet, ASSAN_PACKET_SIZE); if(packet[19]==0x13) diff --git a/Multiprotocol/Arduino.ino b/Multiprotocol/Arduino.ino new file mode 100644 index 0000000..7382ac9 --- /dev/null +++ b/Multiprotocol/Arduino.ino @@ -0,0 +1,133 @@ +/* + 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 . + */ +/************************************/ +/** Arduino replacement routines **/ +/************************************/ +// replacement millis() and micros() +// These work polled, no interrupts +// micros() MUST be called at least once every 32 milliseconds +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 +#ifdef XMEGA + __asm__ __volatile__ ( + "1: sbiw %0,1" "\n\t" // 2 cycles + "nop \n" + "nop \n" + "nop \n" + "nop \n" + "brne 1b" : "=w" (us) : "0" (us) // 2 cycles + ); +#else + __asm__ __volatile__ ( + "1: sbiw %0,1" "\n\t" // 2 cycles + "brne 1b" : "=w" (us) : "0" (us) // 2 cycles + ); +#endif +} + +#ifndef XMEGA + void init() + { + // this needs to be called before setup() or some functions won't work there + sei(); + } +#endif //XMEGA + diff --git a/Multiprotocol/Bayang_nrf24l01.ino b/Multiprotocol/Bayang_nrf24l01.ino index 84907b4..7d010d4 100644 --- a/Multiprotocol/Bayang_nrf24l01.ino +++ b/Multiprotocol/Bayang_nrf24l01.ino @@ -99,7 +99,7 @@ static void __attribute__((unused)) BAYANG_send_packet(uint8_t bind) // Power on, TX mode, 2byte CRC // Why CRC0? xn297 does not interpret it - either 16-bit CRC or nothing - XN297_Configure(BV(NRF24L01_00_EN_CRC) | BV(NRF24L01_00_CRCO) | BV(NRF24L01_00_PWR_UP)); + XN297_Configure(_BV(NRF24L01_00_EN_CRC) | _BV(NRF24L01_00_CRCO) | _BV(NRF24L01_00_PWR_UP)); NRF24L01_WriteReg(NRF24L01_05_RF_CH, bind ? BAYANG_RF_BIND_CHANNEL:hopping_frequency[hopping_frequency_no++]); hopping_frequency_no%=BAYANG_RF_NUM_CHANNELS; diff --git a/Multiprotocol/CG023_nrf24l01.ino b/Multiprotocol/CG023_nrf24l01.ino index 54f4a85..8169b8f 100644 --- a/Multiprotocol/CG023_nrf24l01.ino +++ b/Multiprotocol/CG023_nrf24l01.ino @@ -171,7 +171,7 @@ static void __attribute__((unused)) CG023_send_packet(uint8_t bind) // Power on, TX mode, 2byte CRC // Why CRC0? xn297 does not interpret it - either 16-bit CRC or nothing - XN297_Configure(BV(NRF24L01_00_EN_CRC) | BV(NRF24L01_00_CRCO) | BV(NRF24L01_00_PWR_UP)); + XN297_Configure(_BV(NRF24L01_00_EN_CRC) | _BV(NRF24L01_00_CRCO) | _BV(NRF24L01_00_PWR_UP)); if (bind) NRF24L01_WriteReg(NRF24L01_05_RF_CH, sub_protocol==H8_3D?hopping_frequency[0]:CG023_RF_BIND_CHANNEL); else diff --git a/Multiprotocol/CX10_nrf24l01.ino b/Multiprotocol/CX10_nrf24l01.ino index ae39f23..f836e02 100644 --- a/Multiprotocol/CX10_nrf24l01.ino +++ b/Multiprotocol/CX10_nrf24l01.ino @@ -148,7 +148,7 @@ static void __attribute__((unused)) CX10_Write_Packet(uint8_t bind) // Power on, TX mode, 2byte CRC // Why CRC0? xn297 does not interpret it - either 16-bit CRC or nothing - XN297_Configure(BV(NRF24L01_00_EN_CRC) | BV(NRF24L01_00_CRCO) | BV(NRF24L01_00_PWR_UP)); + XN297_Configure(_BV(NRF24L01_00_EN_CRC) | _BV(NRF24L01_00_CRCO) | _BV(NRF24L01_00_PWR_UP)); if (bind) NRF24L01_WriteReg(NRF24L01_05_RF_CH, CX10_RF_BIND_CHANNEL); else @@ -197,7 +197,7 @@ uint16_t CX10_callback() } break; case CX10_BIND2: - if( NRF24L01_ReadReg(NRF24L01_07_STATUS) & BV(NRF24L01_07_RX_DR)) + if( NRF24L01_ReadReg(NRF24L01_07_STATUS) & _BV(NRF24L01_07_RX_DR)) { // RX fifo data ready XN297_ReadPayload(packet, packet_length); NRF24L01_SetTxRxMode(TXRX_OFF); @@ -220,7 +220,7 @@ uint16_t CX10_callback() 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)); + XN297_Configure(_BV(NRF24L01_00_EN_CRC) | _BV(NRF24L01_00_CRCO) | _BV(NRF24L01_00_PWR_UP) | _BV(NRF24L01_00_PRIM_RX)); } break; case CX10_DATA: diff --git a/Multiprotocol/Convert.ino b/Multiprotocol/Convert.ino new file mode 100644 index 0000000..3e2920e --- /dev/null +++ b/Multiprotocol/Convert.ino @@ -0,0 +1,79 @@ +/* + 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 . + */ +/************************/ +/** Convert routines **/ +/************************/ +int16_t map( int16_t x, int16_t in_min, int16_t in_max, int16_t out_min, int16_t out_max) +{ +// return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; + long y ; + x -= in_min ; + y = out_max - out_min ; + y *= x ; + x = y / (in_max - in_min) ; + return x + out_min ; +} + +// Channel value is converted to 8bit values full scale +uint8_t convert_channel_8b(uint8_t num) +{ + return (uint8_t) (map(limit_channel_100(num),servo_min_100,servo_max_100,0,255)); +} + +// Channel value is converted to 8bit values to provided values scale +uint8_t convert_channel_8b_scale(uint8_t num,uint8_t min,uint8_t max) +{ + return (uint8_t) (map(limit_channel_100(num),servo_min_100,servo_max_100,min,max)); +} + +// Channel value is converted sign + magnitude 8bit values +uint8_t convert_channel_s8b(uint8_t num) +{ + uint8_t ch; + ch = convert_channel_8b(num); + return (ch < 128 ? 127-ch : ch); +} + +// Channel value is converted to 10bit values +uint16_t convert_channel_10b(uint8_t num) +{ + return (uint16_t) (map(limit_channel_100(num),servo_min_100,servo_max_100,1,1023)); +} + +// Channel value is multiplied by 1.5 +uint16_t convert_channel_frsky(uint8_t num) +{ + return Servo_data[num] + Servo_data[num]/2; +} + +// Channel value is converted for HK310 +void convert_channel_HK310(uint8_t num, uint8_t *low, uint8_t *high) +{ + uint16_t temp=0xFFFF-(4*Servo_data[num])/3; + *low=(uint8_t)(temp&0xFF); + *high=(uint8_t)(temp>>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] switch to RX mode NRF24L01_SetTxRxMode(TXRX_OFF); NRF24L01_FlushRx(); diff --git a/Multiprotocol/Hontai_nrf24l01.ino b/Multiprotocol/Hontai_nrf24l01.ino index 08ce83a..64f61a9 100644 --- a/Multiprotocol/Hontai_nrf24l01.ino +++ b/Multiprotocol/Hontai_nrf24l01.ino @@ -121,7 +121,7 @@ static void __attribute__((unused)) HONTAI_send_packet(uint8_t bind) if(sub_protocol == FORMAT_JJRCX1) NRF24L01_SetTxRxMode(TX_EN); else - XN297_Configure(BV(NRF24L01_00_EN_CRC) | BV(NRF24L01_00_CRCO) | BV(NRF24L01_00_PWR_UP)); + XN297_Configure(_BV(NRF24L01_00_EN_CRC) | _BV(NRF24L01_00_CRCO) | _BV(NRF24L01_00_PWR_UP)); NRF24L01_WriteReg(NRF24L01_05_RF_CH, bind ? HONTAI_RF_BIND_CHANNEL : hopping_frequency[hopping_frequency_no++]); hopping_frequency_no %= 3; diff --git a/Multiprotocol/KN_nrf24l01.ino b/Multiprotocol/KN_nrf24l01.ino index 9e0775b..14e9566 100644 --- a/Multiprotocol/KN_nrf24l01.ino +++ b/Multiprotocol/KN_nrf24l01.ino @@ -246,7 +246,7 @@ static void __attribute__((unused)) kn_init() NRF24L01_Initialize(); - NRF24L01_WriteReg(NRF24L01_00_CONFIG, BV(NRF24L01_00_EN_CRC) | BV(NRF24L01_00_CRCO)); + NRF24L01_WriteReg(NRF24L01_00_CONFIG, _BV(NRF24L01_00_EN_CRC) | _BV(NRF24L01_00_CRCO)); NRF24L01_WriteReg(NRF24L01_01_EN_AA, 0x00); // No Auto Acknoledgement NRF24L01_WriteReg(NRF24L01_02_EN_RXADDR, 0x01); // Enable data pipe 0 NRF24L01_WriteReg(NRF24L01_03_SETUP_AW, 0x03); // 5-byte RX/TX address @@ -259,7 +259,7 @@ static void __attribute__((unused)) kn_init() NRF24L01_Activate(0x73); NRF24L01_WriteReg(NRF24L01_1C_DYNPD, 1); // Dynamic payload for data pipe 0 // Enable: Dynamic Payload Length to enable PCF - NRF24L01_WriteReg(NRF24L01_1D_FEATURE, BV(NRF2401_1D_EN_DPL)); + NRF24L01_WriteReg(NRF24L01_1D_FEATURE, _BV(NRF2401_1D_EN_DPL)); NRF24L01_SetPower(); diff --git a/Multiprotocol/MJXQ_nrf24l01.ino b/Multiprotocol/MJXQ_nrf24l01.ino index bd6ef81..ae95060 100644 --- a/Multiprotocol/MJXQ_nrf24l01.ino +++ b/Multiprotocol/MJXQ_nrf24l01.ino @@ -142,7 +142,7 @@ static void __attribute__((unused)) MJXQ_send_packet(uint8_t bind) if (sub_protocol == H26D) NRF24L01_SetTxRxMode(TX_EN); else - XN297_Configure(BV(NRF24L01_00_EN_CRC) | BV(NRF24L01_00_CRCO) | BV(NRF24L01_00_PWR_UP)); + XN297_Configure(_BV(NRF24L01_00_EN_CRC) | _BV(NRF24L01_00_CRCO) | _BV(NRF24L01_00_PWR_UP)); NRF24L01_WriteReg(NRF24L01_05_RF_CH, hopping_frequency[hopping_frequency_no++ / 2]); hopping_frequency_no %= 2 * MJXQ_RF_NUM_CHANNELS; // channels repeated diff --git a/Multiprotocol/MT99xx_nrf24l01.ino b/Multiprotocol/MT99xx_nrf24l01.ino index 384745d..4fb2d65 100644 --- a/Multiprotocol/MT99xx_nrf24l01.ino +++ b/Multiprotocol/MT99xx_nrf24l01.ino @@ -169,7 +169,7 @@ static void __attribute__((unused)) MT99XX_init() NRF24L01_SetBitrate(NRF24L01_BR_1M); // 1Mbps NRF24L01_SetPower(); - XN297_Configure(BV(NRF24L01_00_EN_CRC) | BV(NRF24L01_00_CRCO) | BV(NRF24L01_00_PWR_UP) ); + XN297_Configure(_BV(NRF24L01_00_EN_CRC) | _BV(NRF24L01_00_CRCO) | _BV(NRF24L01_00_PWR_UP) ); } diff --git a/Multiprotocol/Multiprotocol.h b/Multiprotocol/Multiprotocol.h index 02c7293..672b922 100644 --- a/Multiprotocol/Multiprotocol.h +++ b/Multiprotocol/Multiprotocol.h @@ -15,12 +15,12 @@ // Check selected board type #ifndef XMEGA -#if not defined(ARDUINO_AVR_PRO) && not defined(ARDUINO_AVR_MINI) && not defined(ARDUINO_AVR_NANO) - #error You must select the board type "Arduino Pro or Pro Mini" or "Arduino Mini" -#endif -#if F_CPU != 16000000L || not defined(__AVR_ATmega328P__) - #error You must select the processor type "ATmega328(5V, 16MHz)" -#endif + #if not defined(ARDUINO_AVR_PRO) && not defined(ARDUINO_AVR_MINI) && not defined(ARDUINO_AVR_NANO) + #error You must select the board type "Arduino Pro or Pro Mini" or "Arduino Mini" + #endif + #if F_CPU != 16000000L || not defined(__AVR_ATmega328P__) + #error You must select the processor type "ATmega328(5V, 16MHz)" + #endif #endif //****************** @@ -156,6 +156,9 @@ struct PPM_Parameters uint8_t option; }; +// Macros +#define NOP() __asm__ __volatile__("nop") + //******************* //*** Timer *** //******************* @@ -177,207 +180,15 @@ struct PPM_Parameters #define CLR_TIMSK1_OCIE1B TIMSK1 &=~_BV(OCIE1B) #endif -//******************* -//*** Pinouts *** -//******************* - -// TX -#define SERIAL_TX_pin 1 //PD1 -#define SERIAL_TX_port PORTD -#define SERIAL_TX_ddr DDRD -#define SERIAL_TX_output SERIAL_TX_ddr |= _BV(SERIAL_TX_pin) -#define SERIAL_TX_on SERIAL_TX_port |= _BV(SERIAL_TX_pin) -#define SERIAL_TX_off SERIAL_TX_port &= ~_BV(SERIAL_TX_pin) -#ifdef DEBUG_TX - #define DEBUG_TX_on SERIAL_TX_ON - #define DEBUG_TX_off SERIAL_TX_OFF - #define DEBUG_TX_toggle SERIAL_TX_port ^= _BV(SERIAL_TX_pin) -#else - #define DEBUG_TX_on - #define DEBUG_TX_off - #define DEBUG_TX_toggle -#endif - -// Dial -#define MODE_DIAL1_pin 2 -#define MODE_DIAL1_port PORTB -#define MODE_DIAL1_ipr PINB -#define MODE_DIAL2_pin 3 -#define MODE_DIAL2_port PORTB -#define MODE_DIAL2_ipr PINB -#define MODE_DIAL3_pin 4 -#define MODE_DIAL3_port PORTB -#define MODE_DIAL3_ipr PINB -#define MODE_DIAL4_pin 0 -#define MODE_DIAL4_port PORTC -#define MODE_DIAL4_ipr PINC - -// PPM -#define PPM_pin 3 //D3 = PD3 -#define PPM_port PORTD - -// SDIO -#define SDI_pin 5 //D5 = PD5 -#define SDI_port PORTD -#define SDI_ipr PIND -#define SDI_ddr DDRD -#ifdef XMEGA - #define SDI_on SDI_port.OUTSET = _BV(SDI_pin) - #define SDI_off SDI_port.OUTCLR = _BV(SDI_pin) -#else - #define SDI_on SDI_port |= _BV(SDI_pin) - #define SDI_off SDI_port &= ~_BV(SDI_pin) - #define SDI_1 (SDI_ipr & _BV(SDI_pin)) - #define SDI_0 (SDI_ipr & _BV(SDI_pin)) == 0x00 -#endif -#define SDI_input SDI_ddr &= ~_BV(SDI_pin) -#define SDI_output SDI_ddr |= _BV(SDI_pin) - -//SDO -#define SDO_pin 6 //D6 = PD6 -#define SDO_port PORTD -#define SDO_ipr PIND -#ifdef XMEGA - #define SDO_1 (SDO_port.IN & _BV(SDO_pin)) - #define SDO_0 (SDO_port.IN & _BV(SDO_pin)) == 0x00 -#else - #define SDO_1 (SDO_ipr & _BV(SDO_pin)) - #define SDO_0 (SDO_ipr & _BV(SDO_pin)) == 0x00 -#endif - -// SCLK -#define SCLK_port PORTD -#define SCLK_ddr DDRD -#ifdef XMEGA - #define SCLK_pin 7 //PD7 - #define SCLK_on SCLK_port.OUTSET = _BV(SCLK_pin) - #define SCLK_off SCLK_port.OUTCLR = _BV(SCLK_pin) -#else - #define SCLK_pin 4 //D4 = PD4 - #define SCLK_output SCLK_ddr |= _BV(SCLK_pin) - #define SCLK_on SCLK_port |= _BV(SCLK_pin) - #define SCLK_off SCLK_port &= ~_BV(SCLK_pin) -#endif - -// A7105 -#define A7105_CSN_pin 2 //D2 = PD2 -#define A7105_CSN_port PORTD -#define A7105_CSN_ddr DDRD -#define A7105_CSN_output A7105_CSN_ddr |= _BV(A7105_CSN_pin) -#define A7105_CSN_on A7105_CSN_port |= _BV(A7105_CSN_pin) -#define A7105_CSN_off A7105_CSN_port &= ~_BV(A7105_CSN_pin) - -// CC2500 -#define CC25_CSN_pin 7 //D7 = PD7 -#define CC25_CSN_port PORTD -#define CC25_CSN_ddr DDRD -#define CC25_CSN_output CC25_CSN_ddr |= _BV(CC25_CSN_pin) -#define CC25_CSN_on CC25_CSN_port |= _BV(CC25_CSN_pin) -#define CC25_CSN_off CC25_CSN_port &= ~_BV(CC25_CSN_pin) - -// NRF24L01 -#define NRF_CSN_pin 0 //D8 = PB0 -#define NRF_CSN_port PORTB -#define NRF_CSN_ddr DDRB -#define NRF_CSN_output NRF_CSN_ddr |= _BV(NRF_CSN_pin) -#define NRF_CSN_on NRF_CSN_port |= _BV(NRF_CSN_pin) -#define NRF_CSN_off NRF_CSN_port &= ~_BV(NRF_CSN_pin) -#define NRF_CE_on -#define NRF_CE_off - -// CYRF6936 -#ifdef XMEGA - #define CYRF_CSN_pin 4 //PD4 - #define CYRF_CSN_port PORTD - #define CYRF_CSN_ddr DDRD - #define CYRF_CSN_on CYRF_CSN_port.OUTSET = _BV(CYRF_CSN_pin) - #define CYRF_CSN_off CYRF_CSN_port.OUTCLR = _BV(CYRF_CSN_pin) -#else - #define CYRF_CSN_pin 1 //D9 = PB1 - #define CYRF_CSN_port PORTB - #define CYRF_CSN_ddr DDRB - #define CYRF_CSN_output CYRF_CSN_ddr |= _BV(CYRF_CSN_pin) - #define CYRF_CSN_on CYRF_CSN_port |= _BV(CYRF_CSN_pin) - #define CYRF_CSN_off CYRF_CSN_port &= ~_BV(CYRF_CSN_pin) - - #define CYRF_RST_pin 5 //A5 = PC5 - #define CYRF_RST_port PORTC - #define CYRF_RST_ddr DDRC - #define CYRF_RST_output CYRF_RST_ddr |= _BV(CYRF_RST_pin) - #define CYRF_RST_HI CYRF_RST_port |= _BV(CYRF_RST_pin) - #define CYRF_RST_LO CYRF_RST_port &= ~_BV(CYRF_RST_pin) -#endif - -//RF Switch -#ifdef XMEGA - #define PE1_on - #define PE1_off - #define PE2_on - #define PE2_off -#else - #define PE1_pin 1 //A1 = PC1 - #define PE1_port PORTC - #define PE1_ddr DDRC - #define PE1_output PE1_ddr |= _BV(PE1_pin) - #define PE1_on PE1_port |= _BV(PE1_pin) - #define PE1_off PE1_port &= ~_BV(PE1_pin) - - #define PE2_pin 2 //A2 = PC2 - #define PE2_port PORTC - #define PE2_ddr DDRC - #define PE2_output PE2_ddr |= _BV(PE2_pin) - #define PE2_on PE2_port |= _BV(PE2_pin) - #define PE2_off PE2_port &= ~_BV(PE2_pin) -#endif - -// LED -#ifdef XMEGA - #define LED_pin 1 //PD1 - #define LED_port PORTD - #define LED_ddr DDRD - #define LED_on LED_port.OUTCLR = _BV(LED_pin) - #define LED_off LED_port.OUTSET = _BV(LED_pin) - #define LED_toggle LED_port.OUTTGL = _BV(LED_pin) - #define LED_output LED_port.DIRSET = _BV(LED_pin) - #define IS_LED_on (LED_port.OUT & _BV(LED_pin)) -#else - #define LED_pin 5 //D13 = PB5 - #define LED_port PORTB - #define LED_ddr DDRB - #define LED_on LED_port |= _BV(LED_pin) - #define LED_off LED_port &= ~_BV(LED_pin) - #define LED_toggle LED_port ^= _BV(LED_pin) - #define LED_output LED_ddr |= _BV(LED_pin) - #define IS_LED_on (LED_port & _BV(LED_pin)) -#endif - -//BIND -#ifdef XMEGA - #define BIND_pin 2 //PD2 - #define BIND_port PORTD - #define IS_BIND_BUTTON_on ( (BIND_port.IN & _BV(BIND_pin)) == 0x00 ) -#else - #define BIND_pin 5 //D13 = PB5 - #define BIND_port PORTB - #define BIND_ipr PINB - #define BIND_ddr DDRB - #define BIND_SET_INPUT BIND_ddr &= ~_BV(BIND_pin) - #define BIND_SET_OUTPUT BIND_ddr |= _BV(BIND_pin) - #define BIND_SET_PULLUP BIND_port |= _BV(BIND_pin) - #define IS_BIND_BUTTON_on ( (BIND_ipr & _BV(BIND_pin)) == 0x00 ) -#endif - -// Macros -#define NOP() __asm__ __volatile__("nop") -#define BV(bit) (1 << bit) - -//Serial flags definition +//*************** +//*** Flags *** +//*************** #define RX_FLAG_on protocol_flags |= _BV(0) #define RX_FLAG_off protocol_flags &= ~_BV(0) #define IS_RX_FLAG_on ( ( protocol_flags & _BV(0) ) !=0 ) // #define CHANGE_PROTOCOL_FLAG_on protocol_flags |= _BV(1) -#define CHANGE_PROTOCOL_FLAG_off protocol_flags &= ~_BV(1) +#define CHANGE_PROTOCOL_FLAG_off protocol_flags &= ~_BV(1) #define IS_CHANGE_PROTOCOL_FLAG_on ( ( protocol_flags & _BV(1) ) !=0 ) // #define POWER_FLAG_on protocol_flags |= _BV(2) @@ -395,45 +206,47 @@ struct PPM_Parameters #define BIND_BUTTON_FLAG_on protocol_flags |= _BV(5) #define BIND_BUTTON_FLAG_off protocol_flags &= ~_BV(5) #define IS_BIND_BUTTON_FLAG_on ( ( protocol_flags & _BV(5) ) !=0 ) - //PPM RX OK -#define PPM_FLAG_off protocol_flags &= ~_BV(6) +#define PPM_FLAG_off protocol_flags &= ~_BV(6) #define PPM_FLAG_on protocol_flags |= _BV(6) #define IS_PPM_FLAG_on ( ( protocol_flags & _BV(6) ) !=0 ) - -//Bind flag for blinking +//Bind flag #define BIND_IN_PROGRESS protocol_flags &= ~_BV(7) #define BIND_DONE protocol_flags |= _BV(7) #define IS_BIND_DONE_on ( ( protocol_flags & _BV(7) ) !=0 ) - +// #define BAD_PROTO_off protocol_flags2 &= ~_BV(0) #define BAD_PROTO_on protocol_flags2 |= _BV(0) #define IS_BAD_PROTO_on ( ( protocol_flags2 & _BV(0) ) !=0 ) - +// #define RX_DONOTUPDTAE_off protocol_flags2 &= ~_BV(1) #define RX_DONOTUPDTAE_on protocol_flags2 |= _BV(1) #define IS_RX_DONOTUPDTAE_on ( ( protocol_flags2 & _BV(1) ) !=0 ) - +// #define RX_MISSED_BUFF_off protocol_flags2 &= ~_BV(2) #define RX_MISSED_BUFF_on protocol_flags2 |= _BV(2) #define IS_RX_MISSED_BUFF_on ( ( protocol_flags2 & _BV(2) ) !=0 ) - -#define TX_MAIN_PAUSE_off protocol_flags2 &= ~_BV(3) -#define TX_MAIN_PAUSE_on protocol_flags2 |= _BV(3) -#define IS_TX_MAIN_PAUSE_on ( ( protocol_flags2 & _BV(3) ) !=0 ) - +//TX Pause +#define TX_MAIN_PAUSE_off protocol_flags2 &= ~_BV(3) +#define TX_MAIN_PAUSE_on protocol_flags2 |= _BV(3) +#define IS_TX_MAIN_PAUSE_on ( ( protocol_flags2 & _BV(3) ) !=0 ) #define TX_RX_PAUSE_off protocol_flags2 &= ~_BV(4) -#define TX_RX_PAUSE_on protocol_flags2 |= _BV(4) -#define IS_TX_RX_PAUSE_on ( ( protocol_flags2 & _BV(4) ) !=0 ) - +#define TX_RX_PAUSE_on protocol_flags2 |= _BV(4) +#define IS_TX_RX_PAUSE_on ( ( protocol_flags2 & _BV(4) ) !=0 ) #define IS_TX_PAUSE_on ( ( protocol_flags2 & (_BV(4)|_BV(3)) ) !=0 ) -#define BLINK_BIND_TIME 100 -#define BLINK_SERIAL_TIME 500 +//******************** +//*** Blink timing *** +//******************** +#define BLINK_BIND_TIME 100 +#define BLINK_SERIAL_TIME 500 #define BLINK_BAD_PROTO_TIME_LOW 1000 #define BLINK_BAD_PROTO_TIME_HIGH 50 -//AUX flags definition +//******************* +//*** AUX flags *** +//******************* +#define GET_FLAG(ch, mask) ( ch ? mask : 0) #define Servo_AUX1 Servo_AUX & _BV(0) #define Servo_AUX2 Servo_AUX & _BV(1) #define Servo_AUX3 Servo_AUX & _BV(2) @@ -443,8 +256,6 @@ struct PPM_Parameters #define Servo_AUX7 Servo_AUX & _BV(6) #define Servo_AUX8 Servo_AUX & _BV(7) -#define GET_FLAG(ch, mask) ( ch ? mask : 0) - //************************ //*** Power settings *** //************************ diff --git a/Multiprotocol/Multiprotocol.ino b/Multiprotocol/Multiprotocol.ino index c4557da..0eedd44 100644 --- a/Multiprotocol/Multiprotocol.ino +++ b/Multiprotocol/Multiprotocol.ino @@ -23,6 +23,7 @@ #include #include //#define DEBUG_TX +#include "Pins.h" #include "Multiprotocol.h" //Multiprotocol module configuration file @@ -34,6 +35,9 @@ #undef A7105_INSTALLED // Disable A7105 for OrangeTX module #undef CC2500_INSTALLED // Disable CC2500 for OrangeTX module #undef NRF24L01_INSTALLED // Disable NRF for OrangeTX module + #define TELEMETRY // Enable telemetry + #define INVERT_TELEMETRY // Enable invert telemetry + #define DSM_TELEMETRY // Enable DSM telemetry #endif //Global constants/variables @@ -213,7 +217,7 @@ void setup() #endif PE1_output; PE2_output; - //SERIAL_TX_output; + SERIAL_TX_output; // pullups MODE_DIAL1_port |= _BV(MODE_DIAL1_pin); @@ -826,68 +830,6 @@ void modules_reset() prev_power=0xFD; // unused power value } -int16_t map( int16_t x, int16_t in_min, int16_t in_max, int16_t out_min, int16_t out_max) -{ -// return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; - long y ; - x -= in_min ; - y = out_max - out_min ; - y *= x ; - x = y / (in_max - in_min) ; - return x + out_min ; -} - -// Channel value is converted to 8bit values full scale -uint8_t convert_channel_8b(uint8_t num) -{ - return (uint8_t) (map(limit_channel_100(num),servo_min_100,servo_max_100,0,255)); -} - -// Channel value is converted to 8bit values to provided values scale -uint8_t convert_channel_8b_scale(uint8_t num,uint8_t min,uint8_t max) -{ - return (uint8_t) (map(limit_channel_100(num),servo_min_100,servo_max_100,min,max)); -} - -// Channel value is converted sign + magnitude 8bit values -uint8_t convert_channel_s8b(uint8_t num) -{ - uint8_t ch; - ch = convert_channel_8b(num); - return (ch < 128 ? 127-ch : ch); -} - -// Channel value is converted to 10bit values -uint16_t convert_channel_10b(uint8_t num) -{ - return (uint16_t) (map(limit_channel_100(num),servo_min_100,servo_max_100,1,1023)); -} - -// Channel value is multiplied by 1.5 -uint16_t convert_channel_frsky(uint8_t num) -{ - return Servo_data[num] + Servo_data[num]/2; -} - -// Channel value is converted for HK310 -void convert_channel_HK310(uint8_t num, uint8_t *low, uint8_t *high) -{ - uint16_t temp=0xFFFF-(4*Servo_data[num])/3; - *low=(uint8_t)(temp&0xFF); - *high=(uint8_t)(temp>>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]> 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 + rx_tx_addr[4] = (rx_tx_addr[2]&0xF0)|(rx_tx_addr[3]&0x0F); } #ifndef XMEGA @@ -985,179 +927,6 @@ static uint32_t random_id(uint16_t adress, uint8_t create_new) return id; } -/********************/ -/** SPI routines **/ -/********************/ -#ifdef XMEGA - #define XNOP() NOP() -#else - #define XNOP() -#endif - -void SPI_Write(uint8_t command) -{ - uint8_t n=8; - - SCLK_off;//SCK start low - XNOP(); - SDI_off; - XNOP(); - do - { - if(command&0x80) - SDI_on; - else - SDI_off; - XNOP(); - SCLK_on; - XNOP(); - XNOP(); - command = command << 1; - SCLK_off; - XNOP(); - } - 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; - SCLK_on; - XNOP(); - XNOP(); - NOP(); - SCLK_off; - XNOP(); - XNOP(); - } - return result; -} - -/************************************/ -/** Arduino replacement routines **/ -/************************************/ -// replacement millis() and micros() -// These work polled, no interrupts -// micros() MUST be called at least once every 32 milliseconds -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 -#ifdef XMEGA - __asm__ __volatile__ ( - "1: sbiw %0,1" "\n\t" // 2 cycles - "nop \n" - "nop \n" - "nop \n" - "nop \n" - "brne 1b" : "=w" (us) : "0" (us) // 2 cycles - ); -#else - __asm__ __volatile__ ( - "1: sbiw %0,1" "\n\t" // 2 cycles - "brne 1b" : "=w" (us) : "0" (us) // 2 cycles - ); -#endif -} - -#ifndef XMEGA - void init() - { - // this needs to be called before setup() or some functions won't work there - sei(); - } -#endif //XMEGA - /**************************/ /**************************/ /** Interrupt routines **/ diff --git a/Multiprotocol/NRF24l01_SPI.ino b/Multiprotocol/NRF24l01_SPI.ino index 717d4d4..8627efa 100644 --- a/Multiprotocol/NRF24l01_SPI.ino +++ b/Multiprotocol/NRF24l01_SPI.ino @@ -221,11 +221,11 @@ void NRF24L01_Reset() uint8_t NRF24L01_packet_ack() { - switch (NRF24L01_ReadReg(NRF24L01_07_STATUS) & (BV(NRF24L01_07_TX_DS) | BV(NRF24L01_07_MAX_RT))) + switch (NRF24L01_ReadReg(NRF24L01_07_STATUS) & (_BV(NRF24L01_07_TX_DS) | _BV(NRF24L01_07_MAX_RT))) { - case BV(NRF24L01_07_TX_DS): + case _BV(NRF24L01_07_TX_DS): return PKT_ACKED; - case BV(NRF24L01_07_MAX_RT): + case _BV(NRF24L01_07_MAX_RT): return PKT_TIMEOUT; } return PKT_PENDING; @@ -322,8 +322,8 @@ void XN297_SetRXAddr(const uint8_t* addr, uint8_t len) void XN297_Configure(uint8_t flags) { - xn297_crc = !!(flags & BV(NRF24L01_00_EN_CRC)); - flags &= ~(BV(NRF24L01_00_EN_CRC) | BV(NRF24L01_00_CRCO)); + xn297_crc = !!(flags & _BV(NRF24L01_00_EN_CRC)); + flags &= ~(_BV(NRF24L01_00_EN_CRC) | _BV(NRF24L01_00_CRCO)); NRF24L01_WriteReg(NRF24L01_00_CONFIG, flags & 0xFF); } diff --git a/Multiprotocol/Pins.h b/Multiprotocol/Pins.h new file mode 100644 index 0000000..d18151c --- /dev/null +++ b/Multiprotocol/Pins.h @@ -0,0 +1,203 @@ +/* + 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 . + */ +//******************* +//*** Pinouts *** +//******************* + +// TX +#define SERIAL_TX_pin 1 //PD1 +#define SERIAL_TX_port PORTD +#define SERIAL_TX_ddr DDRD +#define SERIAL_TX_output SERIAL_TX_ddr |= _BV(SERIAL_TX_pin) +#define SERIAL_TX_on SERIAL_TX_port |= _BV(SERIAL_TX_pin) +#define SERIAL_TX_off SERIAL_TX_port &= ~_BV(SERIAL_TX_pin) +#ifdef DEBUG_TX + #define DEBUG_TX_on SERIAL_TX_ON + #define DEBUG_TX_off SERIAL_TX_OFF + #define DEBUG_TX_toggle SERIAL_TX_port ^= _BV(SERIAL_TX_pin) +#else + #define DEBUG_TX_on + #define DEBUG_TX_off + #define DEBUG_TX_toggle +#endif + +// Dial +#define MODE_DIAL1_pin 2 +#define MODE_DIAL1_port PORTB +#define MODE_DIAL1_ipr PINB +#define MODE_DIAL2_pin 3 +#define MODE_DIAL2_port PORTB +#define MODE_DIAL2_ipr PINB +#define MODE_DIAL3_pin 4 +#define MODE_DIAL3_port PORTB +#define MODE_DIAL3_ipr PINB +#define MODE_DIAL4_pin 0 +#define MODE_DIAL4_port PORTC +#define MODE_DIAL4_ipr PINC + +// PPM +#define PPM_pin 3 //D3 = PD3 +#define PPM_port PORTD + +// SDIO +#define SDI_pin 5 //D5 = PD5 +#define SDI_port PORTD +#define SDI_ipr PIND +#define SDI_ddr DDRD +#ifdef XMEGA + #define SDI_on SDI_port.OUTSET = _BV(SDI_pin) + #define SDI_off SDI_port.OUTCLR = _BV(SDI_pin) +#else + #define SDI_on SDI_port |= _BV(SDI_pin) + #define SDI_off SDI_port &= ~_BV(SDI_pin) + #define SDI_1 (SDI_ipr & _BV(SDI_pin)) + #define SDI_0 (SDI_ipr & _BV(SDI_pin)) == 0x00 +#endif +#define SDI_input SDI_ddr &= ~_BV(SDI_pin) +#define SDI_output SDI_ddr |= _BV(SDI_pin) + +//SDO +#define SDO_pin 6 //D6 = PD6 +#define SDO_port PORTD +#define SDO_ipr PIND +#ifdef XMEGA + #define SDO_1 (SDO_port.IN & _BV(SDO_pin)) + #define SDO_0 (SDO_port.IN & _BV(SDO_pin)) == 0x00 +#else + #define SDO_1 (SDO_ipr & _BV(SDO_pin)) + #define SDO_0 (SDO_ipr & _BV(SDO_pin)) == 0x00 +#endif + +// SCLK +#define SCLK_port PORTD +#define SCLK_ddr DDRD +#ifdef XMEGA + #define SCLK_pin 7 //PD7 + #define SCLK_on SCLK_port.OUTSET = _BV(SCLK_pin) + #define SCLK_off SCLK_port.OUTCLR = _BV(SCLK_pin) +#else + #define SCLK_pin 4 //D4 = PD4 + #define SCLK_output SCLK_ddr |= _BV(SCLK_pin) + #define SCLK_on SCLK_port |= _BV(SCLK_pin) + #define SCLK_off SCLK_port &= ~_BV(SCLK_pin) +#endif + +// A7105 +#define A7105_CSN_pin 2 //D2 = PD2 +#define A7105_CSN_port PORTD +#define A7105_CSN_ddr DDRD +#define A7105_CSN_output A7105_CSN_ddr |= _BV(A7105_CSN_pin) +#define A7105_CSN_on A7105_CSN_port |= _BV(A7105_CSN_pin) +#define A7105_CSN_off A7105_CSN_port &= ~_BV(A7105_CSN_pin) + +// CC2500 +#define CC25_CSN_pin 7 //D7 = PD7 +#define CC25_CSN_port PORTD +#define CC25_CSN_ddr DDRD +#define CC25_CSN_output CC25_CSN_ddr |= _BV(CC25_CSN_pin) +#define CC25_CSN_on CC25_CSN_port |= _BV(CC25_CSN_pin) +#define CC25_CSN_off CC25_CSN_port &= ~_BV(CC25_CSN_pin) + +// NRF24L01 +#define NRF_CSN_pin 0 //D8 = PB0 +#define NRF_CSN_port PORTB +#define NRF_CSN_ddr DDRB +#define NRF_CSN_output NRF_CSN_ddr |= _BV(NRF_CSN_pin) +#define NRF_CSN_on NRF_CSN_port |= _BV(NRF_CSN_pin) +#define NRF_CSN_off NRF_CSN_port &= ~_BV(NRF_CSN_pin) +#define NRF_CE_on +#define NRF_CE_off + +// CYRF6936 +#ifdef XMEGA + #define CYRF_CSN_pin 4 //PD4 + #define CYRF_CSN_port PORTD + #define CYRF_CSN_ddr DDRD + #define CYRF_CSN_on CYRF_CSN_port.OUTSET = _BV(CYRF_CSN_pin) + #define CYRF_CSN_off CYRF_CSN_port.OUTCLR = _BV(CYRF_CSN_pin) +#else + #define CYRF_CSN_pin 1 //D9 = PB1 + #define CYRF_CSN_port PORTB + #define CYRF_CSN_ddr DDRB + #define CYRF_CSN_output CYRF_CSN_ddr |= _BV(CYRF_CSN_pin) + #define CYRF_CSN_on CYRF_CSN_port |= _BV(CYRF_CSN_pin) + #define CYRF_CSN_off CYRF_CSN_port &= ~_BV(CYRF_CSN_pin) + + #define CYRF_RST_pin 5 //A5 = PC5 + #define CYRF_RST_port PORTC + #define CYRF_RST_ddr DDRC + #define CYRF_RST_output CYRF_RST_ddr |= _BV(CYRF_RST_pin) + #define CYRF_RST_HI CYRF_RST_port |= _BV(CYRF_RST_pin) + #define CYRF_RST_LO CYRF_RST_port &= ~_BV(CYRF_RST_pin) +#endif + +//RF Switch +#ifdef XMEGA + #define PE1_on + #define PE1_off + #define PE2_on + #define PE2_off +#else + #define PE1_pin 1 //A1 = PC1 + #define PE1_port PORTC + #define PE1_ddr DDRC + #define PE1_output PE1_ddr |= _BV(PE1_pin) + #define PE1_on PE1_port |= _BV(PE1_pin) + #define PE1_off PE1_port &= ~_BV(PE1_pin) + + #define PE2_pin 2 //A2 = PC2 + #define PE2_port PORTC + #define PE2_ddr DDRC + #define PE2_output PE2_ddr |= _BV(PE2_pin) + #define PE2_on PE2_port |= _BV(PE2_pin) + #define PE2_off PE2_port &= ~_BV(PE2_pin) +#endif + +// LED +#ifdef XMEGA + #define LED_pin 1 //PD1 + #define LED_port PORTD + #define LED_ddr DDRD + #define LED_on LED_port.OUTCLR = _BV(LED_pin) + #define LED_off LED_port.OUTSET = _BV(LED_pin) + #define LED_toggle LED_port.OUTTGL = _BV(LED_pin) + #define LED_output LED_port.DIRSET = _BV(LED_pin) + #define IS_LED_on (LED_port.OUT & _BV(LED_pin)) +#else + #define LED_pin 5 //D13 = PB5 + #define LED_port PORTB + #define LED_ddr DDRB + #define LED_on LED_port |= _BV(LED_pin) + #define LED_off LED_port &= ~_BV(LED_pin) + #define LED_toggle LED_port ^= _BV(LED_pin) + #define LED_output LED_ddr |= _BV(LED_pin) + #define IS_LED_on (LED_port & _BV(LED_pin)) +#endif + +//BIND +#ifdef XMEGA + #define BIND_pin 2 //PD2 + #define BIND_port PORTD + #define IS_BIND_BUTTON_on ( (BIND_port.IN & _BV(BIND_pin)) == 0x00 ) +#else + #define BIND_pin 5 //D13 = PB5 + #define BIND_port PORTB + #define BIND_ipr PINB + #define BIND_ddr DDRB + #define BIND_SET_INPUT BIND_ddr &= ~_BV(BIND_pin) + #define BIND_SET_OUTPUT BIND_ddr |= _BV(BIND_pin) + #define BIND_SET_PULLUP BIND_port |= _BV(BIND_pin) + #define IS_BIND_BUTTON_on ( (BIND_ipr & _BV(BIND_pin)) == 0x00 ) +#endif diff --git a/Multiprotocol/SHENQI_nrf24l01.ino b/Multiprotocol/SHENQI_nrf24l01.ino index 71fd825..2ffc2fc 100644 --- a/Multiprotocol/SHENQI_nrf24l01.ino +++ b/Multiprotocol/SHENQI_nrf24l01.ino @@ -94,7 +94,7 @@ uint16_t SHENQI_callback() SHENQI_send_packet(); else { - if( NRF24L01_ReadReg(NRF24L01_07_STATUS) & BV(NRF24L01_07_RX_DR)) + if( NRF24L01_ReadReg(NRF24L01_07_STATUS) & _BV(NRF24L01_07_RX_DR)) { if(LT8900_ReadPayload(packet, 3)) { diff --git a/Multiprotocol/SLT_nrf24l01.ino b/Multiprotocol/SLT_nrf24l01.ino index 0622b55..8d4e486 100644 --- a/Multiprotocol/SLT_nrf24l01.ino +++ b/Multiprotocol/SLT_nrf24l01.ino @@ -34,7 +34,7 @@ enum { static void __attribute__((unused)) SLT_init() { NRF24L01_Initialize(); - NRF24L01_WriteReg(NRF24L01_00_CONFIG, BV(NRF24L01_00_EN_CRC) | BV(NRF24L01_00_CRCO)); // 2-bytes CRC, radio off + NRF24L01_WriteReg(NRF24L01_00_CONFIG, _BV(NRF24L01_00_EN_CRC) | _BV(NRF24L01_00_CRCO)); // 2-bytes CRC, radio off NRF24L01_WriteReg(NRF24L01_01_EN_AA, 0x00); // No Auto Acknoledgement NRF24L01_WriteReg(NRF24L01_02_EN_RXADDR, 0x01); // Enable data pipe 0 NRF24L01_WriteReg(NRF24L01_03_SETUP_AW, 0x02); // 4-byte RX/TX address @@ -93,7 +93,7 @@ static void __attribute__((unused)) SLT_set_tx_id(void) static void __attribute__((unused)) SLT_wait_radio() { if (packet_sent) - while (!(NRF24L01_ReadReg(NRF24L01_07_STATUS) & BV(NRF24L01_07_TX_DS))) ; + while (!(NRF24L01_ReadReg(NRF24L01_07_STATUS) & _BV(NRF24L01_07_TX_DS))) ; packet_sent = 0; } @@ -101,7 +101,7 @@ static void __attribute__((unused)) SLT_send_data(uint8_t *data, uint8_t len) { SLT_wait_radio(); NRF24L01_FlushTx(); - NRF24L01_WriteReg(NRF24L01_07_STATUS, BV(NRF24L01_07_TX_DS) | BV(NRF24L01_07_RX_DR) | BV(NRF24L01_07_MAX_RT)); + NRF24L01_WriteReg(NRF24L01_07_STATUS, _BV(NRF24L01_07_TX_DS) | _BV(NRF24L01_07_RX_DR) | _BV(NRF24L01_07_MAX_RT)); NRF24L01_WritePayload(data, len); //NRF24L01_PulseCE(); packet_sent = 1; diff --git a/Multiprotocol/SPI.ino b/Multiprotocol/SPI.ino new file mode 100644 index 0000000..feedcc1 --- /dev/null +++ b/Multiprotocol/SPI.ino @@ -0,0 +1,84 @@ +/* + 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 . + */ +/********************/ +/** SPI routines **/ +/********************/ +#ifdef XMEGA + #define XNOP() NOP() +#else + #define XNOP() +#endif + +void SPI_Write(uint8_t command) +{ + uint8_t n=8; + + SCLK_off;//SCK start low + XNOP(); + SDI_off; + XNOP(); + do + { + if(command&0x80) + SDI_on; + else + SDI_off; + XNOP(); + SCLK_on; + XNOP(); + XNOP(); + command = command << 1; + SCLK_off; + XNOP(); + } + 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; + SCLK_on; + XNOP(); + XNOP(); + NOP(); + SCLK_off; + XNOP(); + XNOP(); + } + return result; +} + +uint8_t SPI_SDIO_Read(void) +{ + uint8_t result=0; + SDI_input; + for(uint8_t i=0;i<8;i++) + { + result=result<<1; + if(SDI_1) ///if SDIO =1 + result |= 0x01; + SCLK_on; + NOP(); + SCLK_off; + } + SDI_output; + return result; +} diff --git a/Multiprotocol/Symax_nrf24l01.ino b/Multiprotocol/Symax_nrf24l01.ino index 5805284..db4cb34 100644 --- a/Multiprotocol/Symax_nrf24l01.ino +++ b/Multiprotocol/Symax_nrf24l01.ino @@ -174,7 +174,7 @@ static void __attribute__((unused)) symax_init() NRF24L01_SetTxRxMode(TX_EN); // NRF24L01_ReadReg(NRF24L01_07_STATUS); - NRF24L01_WriteReg(NRF24L01_00_CONFIG, BV(NRF24L01_00_EN_CRC) | BV(NRF24L01_00_CRCO)); + NRF24L01_WriteReg(NRF24L01_00_CONFIG, _BV(NRF24L01_00_EN_CRC) | _BV(NRF24L01_00_CRCO)); NRF24L01_WriteReg(NRF24L01_01_EN_AA, 0x00); // No Auto Acknoledgement NRF24L01_WriteReg(NRF24L01_02_EN_RXADDR, 0x3F); // Enable all data pipes (even though not used?) NRF24L01_WriteReg(NRF24L01_03_SETUP_AW, 0x03); // 5-byte RX/TX address diff --git a/Multiprotocol/Telemetry.ino b/Multiprotocol/Telemetry.ino index 88fda00..432b1c1 100644 --- a/Multiprotocol/Telemetry.ino +++ b/Multiprotocol/Telemetry.ino @@ -1,8 +1,20 @@ +/* + 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 . + */ //************************** // Telemetry serial code * -// By Midelic on RCGroups * //************************** - #if defined TELEMETRY #if defined SPORT_TELEMETRY @@ -812,4 +824,3 @@ ISR(TIMER0_OVF_vect) #endif // BASH_SERIAL #endif // TELEMETRY - diff --git a/Multiprotocol/V2X2_nrf24l01.ino b/Multiprotocol/V2X2_nrf24l01.ino index 887e60e..8f88c21 100644 --- a/Multiprotocol/V2X2_nrf24l01.ino +++ b/Multiprotocol/V2X2_nrf24l01.ino @@ -80,7 +80,7 @@ static void __attribute__((unused)) v202_init() NRF24L01_Initialize(); // 2-bytes CRC, radio off - NRF24L01_WriteReg(NRF24L01_00_CONFIG, BV(NRF24L01_00_EN_CRC) | BV(NRF24L01_00_CRCO)); + NRF24L01_WriteReg(NRF24L01_00_CONFIG, _BV(NRF24L01_00_EN_CRC) | _BV(NRF24L01_00_CRCO)); NRF24L01_WriteReg(NRF24L01_01_EN_AA, 0x00); // No Auto Acknoledgement NRF24L01_WriteReg(NRF24L01_02_EN_RXADDR, 0x3F); // Enable all data pipes NRF24L01_WriteReg(NRF24L01_03_SETUP_AW, 0x03); // 5-byte RX/TX address @@ -116,7 +116,7 @@ static void __attribute__((unused)) V202_init2() // Turn radio power on NRF24L01_SetTxRxMode(TX_EN); - //Done by TX_EN??? => NRF24L01_WriteReg(NRF24L01_00_CONFIG, BV(NRF24L01_00_EN_CRC) | BV(NRF24L01_00_CRCO) | BV(NRF24L01_00_PWR_UP)); + //Done by TX_EN??? => NRF24L01_WriteReg(NRF24L01_00_CONFIG, _BV(NRF24L01_00_EN_CRC) | _BV(NRF24L01_00_CRCO) | _BV(NRF24L01_00_PWR_UP)); } static void __attribute__((unused)) V2X2_set_tx_id(void) diff --git a/Multiprotocol/YD717_nrf24l01.ino b/Multiprotocol/YD717_nrf24l01.ino index 3e1c4c5..4e6a634 100644 --- a/Multiprotocol/YD717_nrf24l01.ino +++ b/Multiprotocol/YD717_nrf24l01.ino @@ -101,7 +101,7 @@ static void __attribute__((unused)) yd717_send_packet(uint8_t bind) } // clear packet status bits and TX FIFO - NRF24L01_WriteReg(NRF24L01_07_STATUS, (BV(NRF24L01_07_TX_DS) | BV(NRF24L01_07_MAX_RT))); + NRF24L01_WriteReg(NRF24L01_07_STATUS, (_BV(NRF24L01_07_TX_DS) | _BV(NRF24L01_07_MAX_RT))); NRF24L01_FlushTx(); if( sub_protocol == YD717 ) @@ -124,7 +124,7 @@ static void __attribute__((unused)) yd717_init() // CRC, radio on NRF24L01_SetTxRxMode(TX_EN); - NRF24L01_WriteReg(NRF24L01_00_CONFIG, BV(NRF24L01_00_EN_CRC) | BV(NRF24L01_00_PWR_UP)); + NRF24L01_WriteReg(NRF24L01_00_CONFIG, _BV(NRF24L01_00_EN_CRC) | _BV(NRF24L01_00_PWR_UP)); NRF24L01_WriteReg(NRF24L01_01_EN_AA, 0x00); // Disable Acknoledgement on all data pipes NRF24L01_WriteReg(NRF24L01_02_EN_RXADDR, 0x00); // Disable all data pipes NRF24L01_WriteReg(NRF24L01_03_SETUP_AW, 0x03); // 5-byte RX/TX address