| 
									
										
										
										
											2016-04-06 12:57:42 +02:00
										 |  |  | #define ARDUINO_AVR_PRO		1 | 
					
						
							|  |  |  | //#define __AVR_ATmega328P__	1 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-16 19:51:42 +02:00
										 |  |  | #define ORANGE_TX	1 | 
					
						
							| 
									
										
										
										
											2016-04-06 12:57:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-12 21:44:30 +02:00
										 |  |  | // For BLUE module use: | 
					
						
							|  |  |  | //#define DSM_BLUE | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-06 12:57:42 +02:00
										 |  |  | #include <stdlib.h> | 
					
						
							|  |  |  | #include <string.h> | 
					
						
							|  |  |  | #include <avr/interrupt.h> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void protocol_init(void) ; | 
					
						
							| 
									
										
										
										
											2016-12-04 18:58:29 +01:00
										 |  |  | static void update_channels_aux(void) ; | 
					
						
							| 
									
										
										
										
											2016-04-06 12:57:42 +02:00
										 |  |  | static uint32_t random_id(uint16_t adress, uint8_t create_new) ; | 
					
						
							|  |  |  | static void update_serial_data(void) ; | 
					
						
							|  |  |  | static void Mprotocol_serial_init(void) ; | 
					
						
							|  |  |  | static void update_led_status(void) ; | 
					
						
							|  |  |  | static void set_rx_tx_addr(uint32_t id) ; | 
					
						
							|  |  |  | uint16_t limit_channel_100(uint8_t ch) ; | 
					
						
							| 
									
										
										
										
											2016-08-29 09:51:27 +02:00
										 |  |  | void initTXSerial( uint8_t speed); | 
					
						
							|  |  |  | void Serial_write(uint8_t data); | 
					
						
							| 
									
										
										
										
											2016-04-06 12:57:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | extern void NRF24L01_Reset(void ) ; | 
					
						
							|  |  |  | extern void A7105_Reset(void ) ; | 
					
						
							|  |  |  | extern void CC2500_Reset(void ) ; | 
					
						
							|  |  |  | extern uint8_t CYRF_Reset(void ) ; | 
					
						
							|  |  |  | extern void CYRF_SetTxRxMode(uint8_t mode) ; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-12 21:44:30 +02:00
										 |  |  | extern void frskyUpdate(void) ; | 
					
						
							|  |  |  | extern uint16_t initDsm2(void) ; | 
					
						
							|  |  |  | extern uint16_t ReadDsm2(void) ; | 
					
						
							| 
									
										
										
										
											2016-04-20 17:34:28 +02:00
										 |  |  | extern uint16_t DevoInit(void) ; | 
					
						
							| 
									
										
										
										
											2016-04-20 17:53:59 +02:00
										 |  |  | extern uint16_t devo_callback(void) ; | 
					
						
							| 
									
										
										
										
											2016-09-21 14:45:17 +02:00
										 |  |  | extern uint16_t initJ6Pro(void) ; | 
					
						
							|  |  |  | extern uint16_t ReadJ6Pro(void) ; | 
					
						
							| 
									
										
										
										
											2017-02-06 10:39:58 +01:00
										 |  |  | extern uint16_t WK_setup(void) ; | 
					
						
							|  |  |  | extern uint16_t WK_cb(void) ; | 
					
						
							| 
									
										
										
										
											2016-04-06 12:57:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | extern void randomSeed(unsigned int seed) ; | 
					
						
							|  |  |  | extern long random(long howbig) ; | 
					
						
							|  |  |  | extern long map(long x, long in_min, long in_max, long out_min, long out_max) ; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | extern uint32_t millis(void) ; | 
					
						
							|  |  |  | extern uint32_t micros(void) ; | 
					
						
							|  |  |  | extern void delayMicroseconds(uint16_t x) ; | 
					
						
							| 
									
										
										
										
											2016-09-12 21:44:30 +02:00
										 |  |  | extern void delayMilliseconds(unsigned long ms) ; | 
					
						
							| 
									
										
										
										
											2016-04-06 12:57:42 +02:00
										 |  |  | extern void init(void) ; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 18:34:20 +02:00
										 |  |  | extern void modules_reset() ; | 
					
						
							| 
									
										
										
										
											2017-02-06 10:39:58 +01:00
										 |  |  | extern uint8_t Update_All() ; | 
					
						
							| 
									
										
										
										
											2016-09-09 18:34:20 +02:00
										 |  |  | extern void tx_pause() ; | 
					
						
							|  |  |  | extern void tx_resume() ; | 
					
						
							|  |  |  | extern void TelemetryUpdate() ; | 
					
						
							|  |  |  | extern uint16_t initDsm() ; | 
					
						
							|  |  |  | extern uint16_t ReadDsm() ; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-06 12:57:42 +02:00
										 |  |  | #define yield() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define clockCyclesPerMicrosecond() ( F_CPU / 1000000L ) | 
					
						
							|  |  |  | #define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // the prescaler is set so that timer0 ticks every 64 clock cycles, and the | 
					
						
							|  |  |  | // the overflow handler is called every 256 ticks. | 
					
						
							|  |  |  | #define MICROSECONDS_PER_TIMER0_OVERFLOW (clockCyclesToMicroseconds(64 * 256)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // the whole number of milliseconds per timer0 overflow | 
					
						
							|  |  |  | #define MILLIS_INC (MICROSECONDS_PER_TIMER0_OVERFLOW / 1000) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // the fractional number of milliseconds per timer0 overflow. we shift right | 
					
						
							|  |  |  | // by three to fit these numbers into a byte. (for the clock speeds we care | 
					
						
							|  |  |  | // about - 8 and 16 MHz - this doesn't lose precision.) | 
					
						
							|  |  |  | #define FRACT_INC ((MICROSECONDS_PER_TIMER0_OVERFLOW % 1000) >> 3) | 
					
						
							|  |  |  | #define FRACT_MAX (1000 >> 3) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef cbi | 
					
						
							|  |  |  | #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) | 
					
						
							|  |  |  | #endif | 
					
						
							|  |  |  | #ifndef sbi | 
					
						
							|  |  |  | #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) | 
					
						
							|  |  |  | #endif | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void init() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	// this needs to be called before setup() or some functions won't | 
					
						
							|  |  |  | 	// work there | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Enable external oscillator (16MHz) | 
					
						
							|  |  |  | 	OSC.XOSCCTRL = OSC_FRQRANGE_12TO16_gc | OSC_XOSCSEL_XTAL_256CLK_gc ; | 
					
						
							|  |  |  | 	OSC.CTRL |= OSC_XOSCEN_bm ; | 
					
						
							|  |  |  | 	while( ( OSC.STATUS & OSC_XOSCRDY_bm ) == 0 ) | 
					
						
							|  |  |  | 		/* wait */ ; | 
					
						
							|  |  |  | 	// Enable PLL (*2 = 32MHz) | 
					
						
							|  |  |  | 	OSC.PLLCTRL = OSC_PLLSRC_XOSC_gc | 2 ; | 
					
						
							|  |  |  | 	OSC.CTRL |= OSC_PLLEN_bm ; | 
					
						
							|  |  |  | 	while( ( OSC.STATUS & OSC_PLLRDY_bm ) == 0 ) | 
					
						
							|  |  |  | 		/* wait */ ; | 
					
						
							|  |  |  | 	// Switch to PLL clock | 
					
						
							|  |  |  | 	CPU_CCP = 0xD8 ; | 
					
						
							|  |  |  | 	CLK.CTRL = CLK_SCLKSEL_RC2M_gc ; | 
					
						
							|  |  |  | 	CPU_CCP = 0xD8 ; | 
					
						
							|  |  |  | 	CLK.CTRL = CLK_SCLKSEL_PLL_gc ; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	PMIC.CTRL = 7 ;		// Enable all interrupt levels | 
					
						
							|  |  |  | 	sei(); | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | #if defined(ADCSRA) | 
					
						
							|  |  |  | 	// set a2d prescale factor to 128 | 
					
						
							|  |  |  | 	// 16 MHz / 128 = 125 KHz, inside the desired 50-200 KHz range. | 
					
						
							|  |  |  | 	// XXX: this will not work properly for other clock speeds, and | 
					
						
							|  |  |  | 	// this code should use F_CPU to determine the prescale factor. | 
					
						
							|  |  |  | 	sbi(ADCSRA, ADPS2); | 
					
						
							|  |  |  | 	sbi(ADCSRA, ADPS1); | 
					
						
							|  |  |  | 	sbi(ADCSRA, ADPS0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// enable a2d conversions | 
					
						
							|  |  |  | 	sbi(ADCSRA, ADEN); | 
					
						
							|  |  |  | #endif | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// the bootloader connects pins 0 and 1 to the USART; disconnect them | 
					
						
							|  |  |  | 	// here so they can be used as normal digital i/o; they will be | 
					
						
							|  |  |  | 	// reconnected in Serial.begin() | 
					
						
							|  |  |  | #if defined(UCSRB) | 
					
						
							|  |  |  | 	UCSRB = 0; | 
					
						
							|  |  |  | #elif defined(UCSR0B) | 
					
						
							|  |  |  | 	UCSR0B = 0; | 
					
						
							|  |  |  | #endif | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Dip Switch inputs | 
					
						
							|  |  |  | 	PORTA.DIRCLR = 0xFF ; | 
					
						
							|  |  |  | 	PORTA.PIN0CTRL = 0x18 ; | 
					
						
							|  |  |  | 	PORTA.PIN1CTRL = 0x18 ; | 
					
						
							|  |  |  | 	PORTA.PIN2CTRL = 0x18 ; | 
					
						
							|  |  |  | 	PORTA.PIN3CTRL = 0x18 ; | 
					
						
							|  |  |  | 	PORTA.PIN4CTRL = 0x18 ; | 
					
						
							|  |  |  | 	PORTA.PIN5CTRL = 0x18 ; | 
					
						
							|  |  |  | 	PORTA.PIN6CTRL = 0x18 ; | 
					
						
							|  |  |  | 	PORTA.PIN7CTRL = 0x18 ; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "Multiprotocol.ino" | 
					
						
							| 
									
										
										
										
											2016-09-21 14:45:17 +02:00
										 |  |  | #include "SPI.ino" | 
					
						
							| 
									
										
										
										
											2017-02-06 10:31:55 +01:00
										 |  |  | #include "Common.ino" | 
					
						
							| 
									
										
										
										
											2016-09-21 14:45:17 +02:00
										 |  |  | #include "Arduino.ino" | 
					
						
							| 
									
										
										
										
											2016-04-06 12:57:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "cyrf6936_SPI.ino" | 
					
						
							| 
									
										
										
										
											2016-09-09 18:34:20 +02:00
										 |  |  | #include "DSM_cyrf6936.ino" | 
					
						
							| 
									
										
										
										
											2016-04-19 14:29:47 +02:00
										 |  |  | #include "Devo_cyrf6936.ino" | 
					
						
							| 
									
										
										
										
											2016-09-21 14:45:17 +02:00
										 |  |  | #include "J6Pro_cyrf6936.ino" | 
					
						
							| 
									
										
										
										
											2017-02-06 10:39:58 +01:00
										 |  |  | #include "WK2x01_cyrf6936.ino" | 
					
						
							| 
									
										
										
										
											2016-04-06 12:57:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "Telemetry.ino" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int main(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	init() ; | 
					
						
							|  |  |  | 	setup() ; | 
					
						
							|  |  |  | 	for(;;) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		loop() ; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 |