Naze32 clone with Frysky receiver
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

rx.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * FrSky RX 2-way protocol
  3. */
  4. #include <stdint.h>
  5. #include <avr/io.h>
  6. #include <avr/interrupt.h>
  7. #include <avr/eeprom.h>
  8. #include <avr/wdt.h>
  9. #include "cc2500.h"
  10. #include "cppm.h"
  11. #include "spi.h"
  12. #include "timer.h"
  13. #include "rx.h"
  14. #ifdef DEBUG
  15. #include "serial.h"
  16. #endif
  17. #define CHANNELS 8
  18. #define PPM_MIN 1000
  19. #define PPM_MAX 2000
  20. #define HOP_DATA_LENGTH 60
  21. #define EEPROM_BASE_ADDRESS 100
  22. #define MISSING_PACKET_DELAY 9
  23. #define SEEK_CHANNEL_SKIP 13
  24. #define MAX_MISSING_PACKET 20
  25. #define FAILSAFE_MISSING_PACKET 170
  26. #define RSSI_OVER_PPM 7
  27. #define RSSI_OFFSET 71
  28. #define RSSI_MIN -103
  29. #define RSSI_MAX -96
  30. #define PPM_SCALE 0.67
  31. uint8_t ccData[27];
  32. uint8_t ccLen;
  33. uint8_t packet = 0;
  34. uint8_t channr = 0;
  35. uint8_t missingPackets = 0;
  36. uint8_t hopData[HOP_DATA_LENGTH];
  37. uint8_t listLength;
  38. uint8_t txid[2];
  39. uint8_t counter = 0;
  40. uint8_t failed = 0;
  41. uint8_t frequencyOffsetHack = 0;
  42. uint16_t c[8];
  43. int rssi;
  44. static uint16_t ppmBuffer[CHANNELS];
  45. static long map(long x, long in_min, long in_max, long out_min, long out_max);
  46. static long constrain(long x, long min, long max);
  47. static void initialize(uint8_t bind);
  48. static void binding(void);
  49. static void tuning(void);
  50. static void performBind(void);
  51. static void nextChannel(uint8_t skip);
  52. static void readBindingData(void);
  53. static void writeBindingData(void);
  54. void rxInit(void) {
  55. #ifdef DEBUG
  56. serialWriteString(0, "RX: binding\n");
  57. #endif
  58. initialize(1); // binding
  59. binding();
  60. #ifdef DEBUG
  61. serialWriteString(0, "RX: receiving\n");
  62. #endif
  63. initialize(0); // data
  64. cc2500WriteReg(CC2500_0A_CHANNR, hopData[channr]);//0A-hop
  65. cc2500WriteReg(CC2500_23_FSCAL3, 0x89); //23-89
  66. cc2500Strobe(CC2500_SRX);
  67. }
  68. static void initialize(uint8_t bind) {
  69. cc2500ResetChip();
  70. cc2500WriteReg(CC2500_02_IOCFG0, 0x01); // RX complete interrupt(GDO0)
  71. cc2500WriteReg(CC2500_17_MCSM1, 0x0C);
  72. cc2500WriteReg(CC2500_18_MCSM0, 0x18);
  73. cc2500WriteReg(CC2500_06_PKTLEN, 0x19); // Leave room for appended status bytes
  74. cc2500WriteReg(CC2500_08_PKTCTRL0, 0x05);
  75. cc2500WriteReg(CC2500_3E_PATABLE, 0xFF);
  76. cc2500WriteReg(CC2500_0B_FSCTRL1, 0x08);
  77. cc2500WriteReg(CC2500_0C_FSCTRL0, 0x00);
  78. cc2500WriteReg(CC2500_0D_FREQ2, 0x5C);
  79. cc2500WriteReg(CC2500_0E_FREQ1, 0x76);
  80. cc2500WriteReg(CC2500_0F_FREQ0, 0x27);
  81. cc2500WriteReg(CC2500_10_MDMCFG4, 0xAA);
  82. cc2500WriteReg(CC2500_11_MDMCFG3, 0x39);
  83. cc2500WriteReg(CC2500_12_MDMCFG2, 0x11);
  84. cc2500WriteReg(CC2500_13_MDMCFG1, 0x23);
  85. cc2500WriteReg(CC2500_14_MDMCFG0, 0x7A);
  86. cc2500WriteReg(CC2500_15_DEVIATN, 0x42);
  87. cc2500WriteReg(CC2500_19_FOCCFG, 0x16);
  88. cc2500WriteReg(CC2500_1A_BSCFG, 0x6C);
  89. cc2500WriteReg(CC2500_1B_AGCCTRL2, 0x03);
  90. cc2500WriteReg(CC2500_1C_AGCCTRL1, 0x40);
  91. cc2500WriteReg(CC2500_1D_AGCCTRL0, 0x91);
  92. cc2500WriteReg(CC2500_21_FREND1, 0x56);
  93. cc2500WriteReg(CC2500_22_FREND0, 0x10);
  94. cc2500WriteReg(CC2500_23_FSCAL3, 0xA9);
  95. cc2500WriteReg(CC2500_24_FSCAL2, 0x05);
  96. cc2500WriteReg(CC2500_25_FSCAL1, 0x00);
  97. cc2500WriteReg(CC2500_26_FSCAL0, 0x11);
  98. cc2500WriteReg(CC2500_29_FSTEST, 0x59);
  99. cc2500WriteReg(CC2500_2C_TEST2, 0x88);
  100. cc2500WriteReg(CC2500_2D_TEST1, 0x31);
  101. cc2500WriteReg(CC2500_2E_TEST0, 0x0B);
  102. cc2500WriteReg(CC2500_03_FIFOTHR, 0x0F);
  103. cc2500WriteReg(CC2500_09_ADDR, bind ? 0x03 : txid[0]);
  104. cc2500Strobe(CC2500_SIDLE); // Go to idle...
  105. #ifdef DEBUG
  106. uint8_t part = cc2500ReadReg(CC2500_30_PARTNUM);
  107. uint8_t version = cc2500ReadReg(CC2500_31_VERSION);
  108. serialWriteString(0, "CC2500 Part Number: 0x");
  109. serialWriteHex(0, part);
  110. serialWriteString(0, "\nCC2500 Version: 0x");
  111. serialWriteHex(0, version);
  112. serialWriteString(0, "\n");
  113. #endif
  114. // hack: Append status, filter by address, auto-flush on bad crc, PQT=0
  115. cc2500WriteReg(CC2500_07_PKTCTRL1, 0x0D);
  116. //cc2500WriteReg(CC2500_0C_FSCTRL0, 0); // Frequency offset
  117. cc2500WriteReg(CC2500_0C_FSCTRL0, bind ? 0x00 : frequencyOffsetHack);
  118. cc2500WriteReg(CC2500_0A_CHANNR, 0x00);
  119. }
  120. static void binding(void) {
  121. readBindingData();
  122. if ((txid[0] != 0xff) || (txid[1] != 0xff)) {
  123. // valid binding data found
  124. #ifdef DEBUG
  125. serialWriteString(0, "RX: found data in EEPROM!\n");
  126. #endif
  127. return;
  128. }
  129. #ifdef DEBUG
  130. serialWriteString(0, "RX: no stored data, tuning...\n");
  131. #endif
  132. // No valid txid, forcing bind
  133. tuning();
  134. //frequencyOffsetHack = 0xC8; // for test
  135. cc2500WriteReg(CC2500_0C_FSCTRL0, frequencyOffsetHack);
  136. eeprom_write_byte(EEPROM_BASE_ADDRESS + 101, frequencyOffsetHack);
  137. #ifdef DEBUG
  138. serialWriteString(0, "RX: tuned, binding...\n");
  139. #endif
  140. performBind();
  141. }
  142. static void tuning() {
  143. cc2500Strobe(CC2500_SRX); // enter in rx mode
  144. int frequencyOffsetTimer = 0;
  145. while (1) {
  146. frequencyOffsetTimer++;
  147. if (frequencyOffsetTimer > 3000) {
  148. frequencyOffsetTimer = 0;
  149. cc2500WriteReg(CC2500_0C_FSCTRL0, frequencyOffsetHack);
  150. frequencyOffsetHack += 10;
  151. if (frequencyOffsetHack > 250) {
  152. frequencyOffsetHack = 0;
  153. }
  154. //cc2500Strobe(CC2500_SRX); // enter in rx mode
  155. }
  156. if (GDO_1) {
  157. ccLen = cc2500ReadReg(CC2500_3B_RXBYTES | CC2500_READ_BURST) & 0x7F;
  158. if (ccLen) {
  159. cc2500ReadFifo(ccData, ccLen);
  160. if ((ccData[ccLen - 1] & 0x80)
  161. && (ccData[2] == 0x01)
  162. && (ccData[5] == 0x00)) {
  163. break;
  164. }
  165. }
  166. }
  167. wdt_reset();
  168. }
  169. }
  170. static void performBind(void) {
  171. cc2500Strobe(CC2500_SRX); // enter in rx mode
  172. while (1) {
  173. if (GDO_1) {
  174. ccLen = cc2500ReadReg(CC2500_3B_RXBYTES | CC2500_READ_BURST) & 0x7F;
  175. if (ccLen) {
  176. cc2500ReadFifo(ccData, ccLen);
  177. if ((ccData[ccLen - 1] & 0x80)
  178. && (ccData[2] == 0x01)
  179. && (ccData[5] == 0x00)) {
  180. txid[0] = ccData[3];
  181. txid[1] = ccData[4];
  182. for (uint8_t n = 0; n < 5; n++) {
  183. hopData[ccData[5] + n] = ccData[6 + n];
  184. }
  185. break;
  186. }
  187. }
  188. }
  189. wdt_reset();
  190. }
  191. #ifdef DEBUG
  192. serialWriteString(0, "RX: got hop data, reading list...\n");
  193. #endif
  194. listLength = 0;
  195. uint8_t eol = 0;
  196. for (uint8_t bindIdx = 0x05; bindIdx <= 120; bindIdx += 5) {
  197. while (1) {
  198. if (GDO_1) {
  199. ccLen = cc2500ReadReg(CC2500_3B_RXBYTES | CC2500_READ_BURST) & 0x7F;
  200. if (ccLen) {
  201. cc2500ReadFifo(ccData, ccLen);
  202. if ((ccData[ccLen - 1] & 0x80)
  203. && (ccData[2] == 0x01)
  204. && (ccData[3] == txid[0])
  205. && (ccData[4] == txid[1])
  206. && (ccData[5] == bindIdx)) {
  207. for (uint8_t n = 0; n < 5; n++) {
  208. //if (ccData[6 + n] == ccData[ccLen - 3]) {
  209. if (ccData[6 + n] <= 3) {
  210. eol = 1;
  211. listLength = ccData[5] + n;
  212. break;
  213. }
  214. hopData[ccData[5] + n] = ccData[6 + n];
  215. }
  216. break;
  217. }
  218. }
  219. }
  220. wdt_reset();
  221. }
  222. if (eol) {
  223. break; // End of list found, stop!
  224. }
  225. }
  226. #ifdef DEBUG
  227. serialWriteString(0, "RX: binding finished!\n");
  228. #endif
  229. writeBindingData();
  230. cc2500Strobe(CC2500_SIDLE); // Back to idle
  231. }
  232. static void nextChannel(uint8_t skip) {
  233. channr += skip;
  234. if (channr >= listLength) {
  235. channr -= listLength;
  236. }
  237. cc2500WriteReg(CC2500_0A_CHANNR, hopData[channr]);
  238. cc2500WriteReg(CC2500_23_FSCAL3, 0x89);
  239. }
  240. static void readBindingData() {
  241. for (uint8_t i = 0; i < 2; i++) {
  242. txid[i] = eeprom_read_byte(EEPROM_BASE_ADDRESS + i);
  243. }
  244. for (uint8_t i = 0; i < HOP_DATA_LENGTH; i++) {
  245. hopData[i] = eeprom_read_byte(EEPROM_BASE_ADDRESS + 10 + i);
  246. }
  247. listLength = eeprom_read_byte(EEPROM_BASE_ADDRESS + 100);
  248. frequencyOffsetHack = eeprom_read_byte(EEPROM_BASE_ADDRESS + 101);
  249. }
  250. static void writeBindingData() {
  251. for (uint8_t i = 0; i < 2; i++) {
  252. eeprom_write_byte(EEPROM_BASE_ADDRESS + i, txid[i]);
  253. }
  254. for (uint8_t i = 0; i < HOP_DATA_LENGTH; i++) {
  255. eeprom_write_byte(EEPROM_BASE_ADDRESS + 10 + i, hopData[i]);
  256. }
  257. eeprom_write_byte(EEPROM_BASE_ADDRESS + 100, listLength);
  258. }
  259. void rxReceivePacket() {
  260. time_t time = timerGet();
  261. if (missingPackets > FAILSAFE_MISSING_PACKET) {
  262. failed = 1;
  263. missingPackets = 0;
  264. }
  265. while (1) {
  266. if ((timerGet() - time) > MISSING_PACKET_DELAY) {
  267. missingPackets++;
  268. cc2500Strobe(CC2500_SIDLE);
  269. if (missingPackets > MAX_MISSING_PACKET) {
  270. nextChannel(SEEK_CHANNEL_SKIP);
  271. counter++;
  272. #ifdef DEBUG
  273. if (counter > (MAX_MISSING_PACKET << 1)) {
  274. serialWriteString(0, "RX: missing packet notification!\n");
  275. }
  276. #endif
  277. if (counter == (MAX_MISSING_PACKET << 2)) counter = 0;
  278. break;
  279. } else
  280. nextChannel(1);
  281. break;
  282. }
  283. if (GDO_1) {
  284. ccLen = cc2500ReadReg(CC2500_3B_RXBYTES | CC2500_READ_BURST) & 0x7F;
  285. if (ccLen > 20) {
  286. ccLen = 20;
  287. }
  288. if (ccLen) {
  289. cc2500ReadFifo((uint8_t *)ccData, ccLen);
  290. if (ccData[ccLen - 1] & 0x80) { // Only if correct CRC
  291. missingPackets = 0;
  292. if ((ccData[0] == 0x11) // Correct length
  293. && (ccData[1] == txid[0]) // Correct txid
  294. && (ccData[2] == txid[1])) {
  295. packet = 1;
  296. #ifdef RSSI_OVER_PPM
  297. int rssi_dec = cc2500ReadReg(CC2500_34_RSSI | CC2500_READ_BURST);
  298. if (rssi_dec < 128) {
  299. rssi = ((rssi_dec / 2) - RSSI_OFFSET) & 0x7f;
  300. } else {
  301. rssi = (((rssi_dec - 256) / 2)) - RSSI_OFFSET;
  302. }
  303. rssi = constrain(rssi, RSSI_MIN, RSSI_MAX);
  304. #endif
  305. cc2500Strobe(CC2500_SIDLE);
  306. nextChannel(1);
  307. failed = 0;
  308. break;
  309. }
  310. }
  311. }
  312. }
  313. wdt_reset();
  314. }
  315. if (packet != 0) {
  316. packet = 0;
  317. c[0] = (uint16_t)(ccData[10] & 0x0F) << 8 | ccData[6];
  318. c[1] = (uint16_t)(ccData[10] & 0xF0) << 4 | ccData[7];
  319. c[2] = (uint16_t)(ccData[11] & 0x0F) << 8 | ccData[8];
  320. c[3] = (uint16_t)(ccData[11] & 0xF0) << 4 | ccData[9];
  321. c[4] = (uint16_t)(ccData[16] & 0x0F) << 8 | ccData[12];
  322. c[5] = (uint16_t)(ccData[16] & 0xF0) << 4 | ccData[13];
  323. c[6] = (uint16_t)(ccData[17] & 0x0F) << 8 | ccData[14];
  324. c[7] = (uint16_t)(ccData[17] & 0xF0) << 4 | ccData[15];
  325. for (int i = 0; i < CHANNELS; i++) {
  326. ppmBuffer[i] = PPM_SCALE * c[i];
  327. if ((ppmBuffer[i] < 900) || (ppmBuffer[i] > 2100)) {
  328. ppmBuffer[i] = 850;
  329. }
  330. }
  331. #ifdef RSSI_OVER_PPM
  332. ppmBuffer[RSSI_OVER_PPM] = map(rssi, RSSI_MIN, RSSI_MAX, PPM_MIN, PPM_MAX);
  333. #endif
  334. #ifdef DEBUG
  335. serialWriteString(0, "RX: packet received, sending CPPM!\n");
  336. #endif
  337. cppmCopy(ppmBuffer);
  338. }
  339. cc2500Strobe(CC2500_SRX);
  340. }
  341. static long map(long x, long in_min, long in_max, long out_min, long out_max) {
  342. return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  343. }
  344. static long constrain(long x, long min, long max) {
  345. if (x < min) {
  346. x = min;
  347. }
  348. if (x > max) {
  349. x = max;
  350. }
  351. return x;
  352. }