Naze32 clone with Frysky receiver
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

frsky_arduino_rx_complete.ino 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. /*
  2. * Frsky RX 2-way
  3. * By Midelic
  4. * on RCGroups
  5. * an adaptation from Kyrre Aalerud(Kreature)
  6. * 2012 Frsky rx demo code
  7. * http://www.rcgroups.com/forums/showthread.php?t=1667453
  8. * Thanks also to Phracturedblue and his deviation firmware
  9. **********************************
  10. */
  11. #include <avr/interrupt.h>
  12. #include <EEPROM.h>
  13. #include "iface_cc2500.h"
  14. //#define DEBUG
  15. //#define DEBUG0
  16. //#define DEBUG1
  17. //#define DEBUG2
  18. //#define DEBUG3
  19. //#define DEBUG4
  20. //#define DEBUG5
  21. #define FAILSAFE
  22. #define SPIBB
  23. //#define SPIHW
  24. #if defined SPIHW
  25. #include <SPI.h>
  26. #endif
  27. #define chanel_number 8 //set the number of chanels
  28. #define SEEK_CHANSKIP 13
  29. #define MAX_MISSING_PKT 20
  30. //*****************************************
  31. #define PPM_FrLen 22500
  32. #define PPM_PulseLen 300
  33. #define default_servo_value 1500
  34. #define onState 1 //set polarity of the pulses: 1 is positive, 0 is negative
  35. #define sigPin 10
  36. #if defined(SPIBB)
  37. #define MO_pin 5 //D5
  38. #define MI_pin 6//D6
  39. #define SCLK_pin 4 //D4
  40. #define CS 2 //D2
  41. #define GDO_pin 3//D3 GDO0 pin
  42. //
  43. #define SCK_on PORTD |= 0x10//D4
  44. #define SCK_off PORTD &= 0xEF//D4
  45. #define MO_on PORTD |= 0x20 //D5
  46. #define MO_off PORTD &= 0xDF //D5
  47. //
  48. #define MI_1 (PIND & 0x40) == 0x40 //D6 input
  49. #define MI_0 (PIND & 0x40) == 0x00 //D6
  50. //
  51. #define CS_on PORTD |= 0x04 //D2
  52. #define CS_off PORTD &= 0xFB //D2
  53. //
  54. #define GDO_1 (PIND & 0x08) == 0x08 //D3 input
  55. #define GDO_0 (PIND & 0x08) == 0x00 //D3
  56. //
  57. #endif
  58. #define bind_pin A0//C0 bind plug also servo8
  59. //
  60. #define Servo1_OUT 7 //Servo1(D7)
  61. #define Servo2_OUT 8 //Servo2(B0)
  62. #define Servo3_OUT 9 //Servo3(B1)
  63. #define Servo4_OUT 10 //Servo4(B2)//PPM pin
  64. #define Servo5_OUT 11 //Servo5(B3)
  65. #define Servo6_OUT 12 //Servo6(B4)
  66. #define Servo7_OUT 13 //Servo7(B5)
  67. #define Servo8_OUT A0 //Servo8(C0)
  68. //
  69. #define Servo1_OUT_HIGH PORTD |= _BV(7) //Servo1(D7)
  70. #define Servo2_OUT_HIGH PORTB |= _BV(0) //Servo2(B0)
  71. #define Servo3_OUT_HIGH PORTB |= _BV(1) //Servo3(B1)
  72. #define Servo4_OUT_HIGH PORTB |= _BV(2) //Servo4(B2)
  73. #define Servo5_OUT_HIGH PORTB |= _BV(3) //Servo5(B3)
  74. #define Servo6_OUT_HIGH PORTB |= _BV(4) //Servo6(B4)
  75. #define Servo7_OUT_HIGH PORTB |= _BV(5)//Servo7(B5)
  76. #define Servo8_OUT_HIGH PORTC |= _BV(0) //Servo8(C0)
  77. //
  78. #define Servo_Ports_LOW PORTD &= 0x7F ; PORTB &= 0xc0; PORTC &=0xFE //all servos low
  79. //
  80. #define LED_pin A3
  81. #define LED_ON PORTC |= _BV(3)
  82. #define LED_OFF PORTC &= ~_BV(3)
  83. #define NOP() __asm__ __volatile__("nop")
  84. //*******************************************
  85. // Globals:
  86. static uint8_t ccData[27];
  87. static uint8_t ccLen;
  88. static boolean packet = false;
  89. //static uint16_t time;
  90. static uint8_t channr;
  91. static uint8_t missingPackets = 0;
  92. uint8_t calData[60];
  93. uint8_t hopData[60];
  94. uint8_t listLength;
  95. uint8_t txid[2];
  96. static uint8_t counter = 0;
  97. volatile uint16_t Servo_data[10] = {1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500};
  98. volatile byte scale;
  99. static byte jumper1 = 0;
  100. static byte jumper2 = 0;
  101. volatile int ppm[chanel_number];
  102. static uint16_t total_servo_time = 0;
  103. static byte cur_chan_numb = 0;
  104. boolean debug = false;
  105. int count = 0;
  106. uint16_t c[8];
  107. //
  108. void setup()
  109. {
  110. #if defined(SPIBB)
  111. pinMode(MO_pin, OUTPUT);//SI
  112. pinMode(MI_pin, INPUT);//SO
  113. pinMode(SCLK_pin, OUTPUT);//SCLK
  114. pinMode(CS, OUTPUT);//CS output
  115. pinMode(GDO_pin, INPUT); //GDO0 pin
  116. SCK_off;//start sck low
  117. MO_off;//low
  118. #endif
  119. //
  120. pinMode(LED_pin, OUTPUT);
  121. CS_on;
  122. //
  123. #if defined(SPIHW)
  124. pinMode(CS, OUTPUT);
  125. pinMode(GDO_pin, INPUT);
  126. //
  127. SPI.setClockDivider(SPI_CLOCK_DIV2);
  128. SPI.setBitOrder( MSBFIRST);
  129. SPI.begin();
  130. #endif
  131. //
  132. pinMode(Servo1_OUT, OUTPUT); //Servo1
  133. pinMode(Servo2_OUT, OUTPUT); //Servo2
  134. pinMode(Servo3_OUT, OUTPUT); //Servo3
  135. pinMode(Servo4_OUT, OUTPUT); //Servo4
  136. //
  137. pinMode(Servo6_OUT, OUTPUT); //Servo6
  138. pinMode(Servo7_OUT, OUTPUT); //Servo7
  139. pinMode(Servo8_OUT, OUTPUT); //Servo8
  140. //Servo8_OUT_HIGH;//bindpin pullup
  141. //
  142. #if defined DEBUG
  143. Serial.begin(115200);
  144. int8_t i;
  145. Serial.print("PartNum ");
  146. i = cc2500_readReg(CC2500_30_PARTNUM + CC2500_READ_BURST);
  147. Serial.println(i);
  148. delay(10);
  149. Serial.print("Version ");
  150. i = cc2500_readReg(CC2500_31_VERSION + CC2500_READ_BURST);
  151. Serial.println(i);
  152. #endif
  153. //
  154. #if F_CPU == 16000000
  155. scale = 2;
  156. #elif F_CPU == 8000000
  157. scale = 1;
  158. #else
  159. #error // 8 or 16MHz only !
  160. #endif
  161. //
  162. initialize(1);//binding
  163. binding();
  164. pinMode(Servo8_OUT, OUTPUT); //Servo8.bind pin is set to output again.
  165. initialize(0);//data
  166. //
  167. jumper1 = PPM_jumper(); // Check the possible jumper positions for changing the receiver mode.
  168. //
  169. if (jumper1 == 1) {
  170. //initiallize default ppm values
  171. for (int i = 0; i < chanel_number; i++) {
  172. ppm[i] = default_servo_value;
  173. }
  174. pinMode(sigPin, OUTPUT);
  175. digitalWrite(sigPin, !onState); //set the PPM signal pin to the default state (off)
  176. }
  177. config_timer();
  178. //
  179. channr = 0;
  180. cc2500_writeReg(CC2500_0A_CHANNR, hopData[channr]);//0A-hop
  181. cc2500_writeReg(CC2500_23_FSCAL3, 0x89); //23-89
  182. cc2500_strobe(CC2500_SRX);
  183. }
  184. void loop()
  185. {
  186. unsigned long time = micros();
  187. #if defined(FAILSAFE)
  188. if (missingPackets > 170) {
  189. //**************************************
  190. //noInterrupts();//
  191. //digitalWrite(sigPin, LOW);
  192. //Servo_Ports_LOW;
  193. //**********************************************
  194. missingPackets = 0;
  195. int i;
  196. for (i = 0; i < 8; i++) {
  197. Servo_data[i] = 1500;
  198. ppm[i] = 1500;
  199. if (i == 2) {
  200. Servo_data[2] = 1000; //THROTLE ON CHN3 here it can be changed Throttle on other channel
  201. ppm[2] = 1000;
  202. }
  203. }
  204. }
  205. #endif
  206. while (1) {
  207. if ((micros() - time) > 9000) {
  208. missingPackets++;
  209. cc2500_strobe(CC2500_SIDLE);
  210. if (missingPackets > MAX_MISSING_PKT) {
  211. nextChannel(SEEK_CHANSKIP);
  212. LED_OFF;
  213. counter++;
  214. if (counter > (MAX_MISSING_PKT << 1))
  215. LED_ON;
  216. if (counter == (MAX_MISSING_PKT << 2)) counter = 0;
  217. break;
  218. } else
  219. nextChannel(1);
  220. break;
  221. }
  222. if (GDO_1) {
  223. ccLen = cc2500_readReg(CC2500_3B_RXBYTES | CC2500_READ_BURST) & 0x7F;
  224. if (ccLen > 20)
  225. ccLen = 20;//
  226. if (ccLen) {
  227. cc2500_readFifo((uint8_t *)ccData, ccLen);
  228. if (ccData[ccLen - 1] & 0x80) { // Only if correct CRC
  229. missingPackets = 0;
  230. if (ccData[0] == 0x11) { // Correct length
  231. if ((ccData[1] == txid[0]) && (ccData[2] == txid[1])) { // Only if correct txid
  232. packet = true;
  233. //sei(); ///////////////////////////////////////////////////////////////////////////////////////
  234. //int rssi = cc2500_readReg(CC2500_34_RSSI | CC2500_READ_BURST);//check RSSI
  235. cc2500_strobe(CC2500_SIDLE);
  236. nextChannel(1);
  237. LED_ON;
  238. break;
  239. }
  240. }
  241. }
  242. }
  243. }
  244. }
  245. if (packet == true) {
  246. packet = false;
  247. debug = true;
  248. //cli();
  249. c[0] = (uint16_t)(ccData[10] & 0x0F) << 8 | ccData[6];
  250. c[1] = (uint16_t)(ccData[10] & 0xF0) << 4 | ccData[7];
  251. c[2] = (uint16_t)(ccData[11] & 0x0F) << 8 | ccData[8];
  252. c[3] = (uint16_t)(ccData[11] & 0xF0) << 4 | ccData[9];
  253. c[4] = (uint16_t)(ccData[16] & 0x0F) << 8 | ccData[12];
  254. c[5] = (uint16_t)(ccData[16] & 0xF0) << 4 | ccData[13];
  255. c[6] = (uint16_t)(ccData[17] & 0x0F) << 8 | ccData[14];
  256. c[7] = (uint16_t)(ccData[17] & 0xF0) << 4 | ccData[15];
  257. //sei();
  258. for (int i = 0; i < 8; i++) {
  259. Servo_data[i] = 0.67 * c[i];
  260. if (Servo_data[i] < 900) { //added new
  261. Servo_data[i] = 1500; //added new
  262. Servo_data[2] = 1000;
  263. }
  264. ppm[i] = Servo_data[i];
  265. }
  266. #if defined(DEBUG5)
  267. //Serial.println(rssi);
  268. #endif
  269. #if defined(DEBUG0)
  270. for (int i = 0; i < 8; i++) {
  271. Serial.print(" ");
  272. Serial.print(Servo_data[i]);
  273. Serial.print(" ");
  274. }
  275. Serial.println(" ");
  276. #endif
  277. }
  278. //
  279. cc2500_strobe(CC2500_SRX);
  280. if (debug == true) {
  281. debug = false;
  282. #if defined(DEBUG2)
  283. Serial.println(ccData[3], HEX);
  284. #endif
  285. }
  286. }
  287. void initialize(int bind)
  288. {
  289. cc2500_resetChip();
  290. cc2500_writeReg(CC2500_02_IOCFG0, 0x01); // reg 0x02: RX complete interrupt(GDO0)
  291. cc2500_writeReg(CC2500_17_MCSM1, 0x0C); // reg 0x17:
  292. cc2500_writeReg(CC2500_18_MCSM0, 0x18); // reg 0x18:
  293. cc2500_writeReg(CC2500_06_PKTLEN, 0x19); // Leave room for appended status bytes
  294. cc2500_writeReg(CC2500_08_PKTCTRL0, 0x05); // reg 0x08:
  295. cc2500_writeReg(CC2500_3E_PATABLE, 0xFF); //
  296. cc2500_writeReg(CC2500_0B_FSCTRL1, 0x08); // reg 0x0B:
  297. cc2500_writeReg(CC2500_0C_FSCTRL0, 0x00); // reg 0x0C
  298. cc2500_writeReg(CC2500_0D_FREQ2, 0x5C); // reg 0x0D
  299. cc2500_writeReg(CC2500_0E_FREQ1, 0x76); // reg 0x0E
  300. cc2500_writeReg(CC2500_0F_FREQ0, 0x27); // reg 0x0F
  301. cc2500_writeReg(CC2500_10_MDMCFG4, 0xAA); // reg 0x10
  302. cc2500_writeReg(CC2500_11_MDMCFG3, 0x39); // reg 0x11
  303. cc2500_writeReg(CC2500_12_MDMCFG2, 0x11); // reg 0x12
  304. cc2500_writeReg(CC2500_13_MDMCFG1, 0x23); // reg 0x13
  305. cc2500_writeReg(CC2500_14_MDMCFG0, 0x7A); // reg 0x14
  306. cc2500_writeReg(CC2500_15_DEVIATN, 0x42); // reg 0x15
  307. cc2500_writeReg(CC2500_19_FOCCFG, 0x16); // reg 0x16
  308. cc2500_writeReg(CC2500_1A_BSCFG, 0x6C); // reg 0x1A
  309. cc2500_writeReg(CC2500_1B_AGCCTRL2, 0x03); // reg 0x1B
  310. cc2500_writeReg(CC2500_1C_AGCCTRL1, 0x40); // reg 0x1C
  311. cc2500_writeReg(CC2500_1D_AGCCTRL0, 0x91); // reg 0x1D
  312. cc2500_writeReg(CC2500_21_FREND1, 0x56); // reg 0x21:
  313. cc2500_writeReg(CC2500_22_FREND0, 0x10); // reg 0x22:
  314. cc2500_writeReg(CC2500_23_FSCAL3, 0xA9); // reg 0x23:
  315. cc2500_writeReg(CC2500_24_FSCAL2, 0x05); // reg 0x24:
  316. cc2500_writeReg(CC2500_25_FSCAL1, 0x00); // reg 0x25
  317. cc2500_writeReg(CC2500_26_FSCAL0, 0x11); // reg 0x26
  318. cc2500_writeReg(CC2500_29_FSTEST, 0x59); // reg 0x29
  319. cc2500_writeReg(CC2500_2C_TEST2, 0x88); // reg 0x2C
  320. cc2500_writeReg(CC2500_2D_TEST1, 0x31); // reg 0x2D
  321. cc2500_writeReg(CC2500_2E_TEST0, 0x0B); // reg 0x2E
  322. cc2500_writeReg(CC2500_03_FIFOTHR, 0x0F); // reg 0x03:
  323. cc2500_writeReg(CC2500_09_ADDR, bind ? 0x03 : txid[0]);
  324. cc2500_strobe(CC2500_SIDLE); // Go to idle...
  325. cc2500_writeReg(CC2500_07_PKTCTRL1, 0x0D); // reg 0x07 hack: Append status, filter by address, auto-flush on bad crc, PQT=0
  326. //cc2500_writeReg(CC2500_0C_FSCTRL0, 0); // Frequency offset...
  327. cc2500_writeReg(CC2500_0C_FSCTRL0, bind ? 0x00 : count); // Frequency offset hack
  328. cc2500_writeReg(CC2500_0A_CHANNR, 0x00);
  329. }
  330. // Receives complete bind setup
  331. void getBind(void)
  332. {
  333. cc2500_strobe(CC2500_SRX);//enter in rx mode
  334. listLength = 0;
  335. boolean eol = false;
  336. // len|bind |tx id|idx|h0|h1|h2|h3|h4|00|00|00|00|00|00|01
  337. // Start by getting bind packet 0 and the txid
  338. // 0 1 2 txid0(3) txid1()4 5 6 7 8 9 10 11 12 13 14 15 16 17
  339. //ccdata //11 03 01 d7 2d 00 00 1e 3c 5b 78 00 00 00 00 00 00 01
  340. //11 03 01 19 3e 00 02 8e 2f bb 5c 00 00 00 00 00 00 01
  341. while (1) {
  342. if (GDO_1) {
  343. ccLen = cc2500_readReg(CC2500_3B_RXBYTES | CC2500_READ_BURST) & 0x7F;
  344. if (ccLen) {
  345. cc2500_readFifo((uint8_t *)ccData, ccLen);
  346. if (ccData[ccLen - 1] & 0x80) {
  347. if (ccData[2] == 0x01) {
  348. if (ccData[5] == 0x00) {
  349. txid[0] = ccData[3];
  350. txid[1] = ccData[4];
  351. for (uint8_t n = 0; n < 5; n++) {
  352. hopData[ccData[5] + n] = ccData[6 + n];
  353. }
  354. break;
  355. }
  356. }
  357. }
  358. }
  359. }
  360. }
  361. #if defined(DEBUG)
  362. Serial.print(txid[0], HEX);
  363. Serial.println(txid[1], HEX);
  364. #endif
  365. for (uint8_t bindIdx = 0x05; bindIdx <= 120; bindIdx += 5) {
  366. while (1) {
  367. if (GDO_1) {
  368. ccLen = cc2500_readReg(CC2500_3B_RXBYTES | CC2500_READ_BURST) & 0x7F;
  369. if (ccLen) {
  370. cc2500_readFifo((uint8_t *)ccData, ccLen);
  371. if (ccData[ccLen - 1] & 0x80) {
  372. if (ccData[2] == 0x01) {
  373. if ((ccData[3] == txid[0]) && (ccData[4] == txid[1])) {
  374. if (ccData[5] == bindIdx) {
  375. for (uint8_t n = 0; n < 5; n++) {
  376. if (ccData[6 + n] == ccData[ccLen - 3]) {
  377. eol = true;
  378. listLength = ccData[5] + n;
  379. break;
  380. }
  381. hopData[ccData[5] + n] = ccData[6 + n];
  382. }
  383. break;
  384. }
  385. }
  386. }
  387. }
  388. }
  389. }
  390. }
  391. #if defined(DEBUG)
  392. Serial.println(bindIdx / 5);
  393. #endif
  394. if (eol) break; // End of list found, stop!
  395. }
  396. #if defined(DEBUG)
  397. for (uint8_t jumpIdx = 0; jumpIdx < (listLength); jumpIdx++) {
  398. Serial.print(" ");
  399. Serial.print(hopData[jumpIdx], HEX);
  400. Serial.print(" ");
  401. }
  402. Serial.println(" ");
  403. #endif
  404. Store_bind();
  405. cc2500_strobe(CC2500_SIDLE); // Back to idle
  406. }
  407. ISR(TIMER1_COMPA_vect)
  408. {
  409. TCNT1 = 0;
  410. if (jumper1 == 0) {
  411. pinMode(Servo5_OUT, OUTPUT);
  412. Servo_Ports_LOW;
  413. //code for servo.
  414. cur_chan_numb++;//next servo
  415. if (cur_chan_numb < chanel_number) {
  416. total_servo_time += Servo_data[cur_chan_numb] * scale;
  417. OCR1A = Servo_data[cur_chan_numb] * scale;
  418. } else {
  419. OCR1A = PPM_FrLen * scale - total_servo_time;
  420. cur_chan_numb = 0xff;
  421. total_servo_time = 0;
  422. }
  423. switch (cur_chan_numb) {
  424. case 0:
  425. Servo1_OUT_HIGH;
  426. break;
  427. case 1:
  428. Servo2_OUT_HIGH;
  429. break;
  430. case 2:
  431. Servo3_OUT_HIGH;
  432. break;
  433. case 3:
  434. Servo4_OUT_HIGH;
  435. break;
  436. case 4:
  437. Servo5_OUT_HIGH;
  438. break;
  439. case 5:
  440. Servo6_OUT_HIGH;
  441. break;
  442. case 6:
  443. Servo7_OUT_HIGH;
  444. break;
  445. case 7:
  446. Servo8_OUT_HIGH;
  447. break;
  448. }
  449. } else {
  450. static boolean state = true;
  451. pinMode(sigPin, OUTPUT);
  452. digitalWrite(sigPin, !onState);
  453. if (state) {
  454. digitalWrite(sigPin, onState);
  455. OCR1A = PPM_PulseLen * scale;
  456. state = false;
  457. } else {
  458. static byte cur_chan_numb;
  459. static unsigned int calc_rest;
  460. // digitalWrite(sigPin, !onState);//PPM on servo4 pin10
  461. state = true;
  462. if (cur_chan_numb >= chanel_number) {
  463. cur_chan_numb = 0;
  464. calc_rest = calc_rest + PPM_PulseLen;//
  465. OCR1A = (PPM_FrLen - calc_rest) * scale;
  466. calc_rest = 0;
  467. } else {
  468. OCR1A = (ppm[cur_chan_numb] - PPM_PulseLen) * scale;
  469. calc_rest = calc_rest + ppm[cur_chan_numb];
  470. cur_chan_numb++;
  471. }
  472. }
  473. }
  474. }
  475. void config_timer()
  476. {
  477. OCR1A = 50 * scale;
  478. cli();
  479. TCCR1A = 0; //
  480. TCCR1B = 0;
  481. TCCR1B |= (1 << WGM12);
  482. TCCR1B |= (1 << CS11);
  483. TIMSK1 |= (1 << OCIE1A);
  484. sei();
  485. }
  486. void nextChannel(uint8_t skip)
  487. {
  488. channr += skip;//
  489. if (channr >= listLength) channr -= listLength;
  490. cc2500_writeReg(CC2500_0A_CHANNR, hopData[channr]);
  491. cc2500_writeReg(CC2500_23_FSCAL3, 0x89);
  492. }
  493. void binding()
  494. {
  495. jumper2 = bind_jumper();
  496. while (1) {
  497. if (jumper2 == 0) { //bind complete or no bind
  498. uint8_t i;
  499. uint8_t adr = 100;
  500. for (i = 0; i < 2; i++) {
  501. txid[i] = EEPROM.read(adr + i);
  502. }
  503. for (i = 0; i < sizeof(hopData); i++) {
  504. hopData[i] = EEPROM.read(adr + 10 + i);
  505. }
  506. listLength = EEPROM.read(adr + 100);
  507. count = EEPROM.read(adr + 101);
  508. break;
  509. } else {
  510. LED_ON;
  511. tunning();
  512. //count=0xC8;//for test
  513. cc2500_writeReg(CC2500_0C_FSCTRL0, count);
  514. int adr = 100;
  515. EEPROM.write(adr + 101, count);
  516. getBind();
  517. while (1) {
  518. LED_ON;
  519. delay(500);
  520. LED_OFF;
  521. delay(500);
  522. }
  523. }
  524. }
  525. }
  526. void tunning()
  527. {
  528. cc2500_strobe(CC2500_SRX);//enter in rx mode
  529. int count1 = 0;
  530. while (1) {
  531. count1++;
  532. if (count >= 250) {
  533. count = 0;
  534. }
  535. if (count1 > 3000) {
  536. count1 = 0;
  537. cc2500_writeReg(CC2500_0C_FSCTRL0, count); // Frequency offset hack
  538. count = count + 10;
  539. //cc2500_strobe(CC2500_SRX);//enter in rx mode
  540. }
  541. if (GDO_1) {
  542. ccLen = cc2500_readReg(CC2500_3B_RXBYTES | CC2500_READ_BURST) & 0x7F;
  543. if (ccLen) {
  544. cc2500_readFifo((uint8_t *)ccData, ccLen);
  545. if (ccData[ccLen - 1] & 0x80) {
  546. if (ccData[2] == 0x01) {
  547. if (ccData[5] == 0x00) {
  548. break;
  549. }
  550. }
  551. }
  552. }
  553. }
  554. }
  555. #if defined(DEBUG1)
  556. Serial.println(count, HEX);
  557. #endif
  558. }
  559. void Store_bind()
  560. {
  561. uint8_t i;
  562. int adr = 100;
  563. for (i = 0; i < 2; i++) {
  564. EEPROM.write(adr + i, txid[i]);
  565. }
  566. for (i = 0; i < sizeof(hopData); i++) {
  567. EEPROM.write(adr + 10 + i, hopData[i]);
  568. }
  569. EEPROM.write(adr + 100, listLength);
  570. }
  571. unsigned char PPM_jumper(void)
  572. {
  573. // PPM Selection (jumper between Ch1 and ch3)
  574. pinMode(Servo3_OUT, INPUT); //CH3 input
  575. digitalWrite(Servo3_OUT, HIGH); // pull up
  576. digitalWrite(Servo1_OUT, HIGH); // CH1 is HIGH
  577. delayMicroseconds(1);
  578. if ( digitalRead(Servo3_OUT) == HIGH) {
  579. digitalWrite(Servo1_OUT, LOW); // CH1 is LOW
  580. delayMicroseconds(1);
  581. if (digitalRead(Servo3_OUT) == LOW) { // OK jumper plugged
  582. pinMode(Servo3_OUT, OUTPUT);
  583. return 1;
  584. }
  585. }
  586. pinMode(Servo3_OUT, OUTPUT);
  587. return 0; // servo PWM by default
  588. }
  589. //bind jumper
  590. unsigned char bind_jumper(void)
  591. {
  592. pinMode(bind_pin, INPUT_PULLUP);//pull up
  593. if ( digitalRead(bind_pin) == LOW) {
  594. delayMicroseconds(1);
  595. return 1;
  596. }
  597. return 0;
  598. }