Paparazzi UAS  v5.2.2_stable-0-gd6b9f29
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
superbitrf.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 Freek van Tienen <freek.v.tienen@gmail.com>
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
27 #include "subsystems/datalink/superbitrf.h"
28 
29 #include <string.h>
30 #include "paparazzi.h"
31 #include "led.h"
32 #include "mcu_periph/spi.h"
33 #include "mcu_periph/sys_time.h"
34 #include "mcu_periph/gpio.h"
35 #include "subsystems/settings.h"
36 
37 /* Default SuperbitRF SPI DEV */
38 #ifndef SUPERBITRF_SPI_DEV
39 #define SUPERBITRF_SPI_DEV spi1
40 #endif
42 
43 /* Default SuperbitRF RST PORT and PIN */
44 #ifndef SUPERBITRF_RST_PORT
45 #define SUPERBITRF_RST_PORT GPIOC
46 #endif
48 #ifndef SUPERBITRF_RST_PIN
49 #define SUPERBITRF_RST_PIN GPIO12
50 #endif
52 
53 /* Default SuperbitRF DRDY(IRQ) PORT and PIN */
54 #ifndef SUPERBITRF_DRDY_PORT
55 #define SUPERBITRF_DRDY_PORT GPIOB
56 #endif
58 #ifndef SUPERBITRF_DRDY_PIN
59 #define SUPERBITRF_DRDY_PIN GPIO1
60 #endif
62 
63 /* Default forcing in DSM2 mode is false */
64 #ifndef SUPERBITRF_FORCE_DSM2
65 #define SUPERBITRF_FORCE_DSM2 TRUE
66 #endif
68 
69 /* The superbitRF structure */
71 
72 /* The internal functions */
73 static inline void superbitrf_radio_to_channels(uint8_t* data, uint8_t nb_channels, bool_t is_11bit, int16_t* channels);
74 static inline void superbitrf_receive_packet_cb(bool_t error, uint8_t status, uint8_t packet[]);
75 static inline void superbitrf_send_packet_cb(bool_t error);
76 static inline void superbitrf_gen_dsmx_channels(void);
77 
78 /* The startup configuration for the cyrf6936 */
79 static const uint8_t cyrf_stratup_config[][2] = {
80  {CYRF_MODE_OVERRIDE, CYRF_RST}, // Reset the device
81  {CYRF_CLK_EN, CYRF_RXF}, // Enable the clock
82  {CYRF_AUTO_CAL_TIME, 0x3C}, // From manual, needed for initialization
83  {CYRF_AUTO_CAL_OFFSET, 0x14}, // From manual, needed for initialization
84  {CYRF_RX_CFG, CYRF_LNA | CYRF_FAST_TURN_EN}, // Enable low noise amplifier and fast turning
85  {CYRF_TX_OFFSET_LSB, 0x55}, // From manual, typical configuration
86  {CYRF_TX_OFFSET_MSB, 0x05}, // From manual, typical configuration
87  {CYRF_XACT_CFG, CYRF_MODE_SYNTH_RX | CYRF_FRC_END}, // Force in Synth RX mode
88  {CYRF_TX_CFG, CYRF_DATA_CODE_LENGTH | CYRF_DATA_MODE_SDR | CYRF_PA_4}, // Enable 64 chip codes, SDR mode and amplifier +4dBm
89  {CYRF_DATA64_THOLD, 0x0E}, // From manual, typical configuration
90  {CYRF_XACT_CFG, CYRF_MODE_SYNTH_RX}, // Set in Synth RX mode (again, really needed?)
91 };
92 /* The binding configuration for the cyrf6936 */
93 static const uint8_t cyrf_bind_config[][2] = {
94  {CYRF_TX_CFG, CYRF_DATA_CODE_LENGTH | CYRF_DATA_MODE_SDR | CYRF_PA_4}, // Enable 64 chip codes, SDR mode and amplifier +4dBm
95  {CYRF_FRAMING_CFG, CYRF_SOP_LEN | 0xE}, // Set SOP CODE to 64 chips and SOP Correlator Threshold to 0xE
96  {CYRF_RX_OVERRIDE, CYRF_FRC_RXDR | CYRF_DIS_RXCRC}, // Force receive data rate and disable receive CRC checker
97  {CYRF_EOP_CTRL, 0x02}, // Only enable EOP symbol count of 2
98  {CYRF_TX_OVERRIDE, CYRF_DIS_TXCRC}, // Disable transmit CRC generate
99 };
100 /* The transfer configuration for the cyrf6936 */
101 static const uint8_t cyrf_transfer_config[][2] = {
102  {CYRF_TX_CFG, CYRF_DATA_CODE_LENGTH | CYRF_DATA_MODE_8DR | CYRF_PA_4}, // Enable 64 chip codes, 8DR mode and amplifier +4dBm
103  {CYRF_FRAMING_CFG, CYRF_SOP_EN | CYRF_SOP_LEN | CYRF_LEN_EN | 0xE}, // Set SOP CODE enable, SOP CODE to 64 chips, Packet length enable, and SOP Correlator Threshold to 0xE
104  {CYRF_TX_OVERRIDE, 0x00}, // Reset TX overrides
105  {CYRF_RX_OVERRIDE, 0x00}, // Reset RX overrides
106 };
107 /* Abort the receive of the cyrf6936 */
110  {CYRF_RX_ABORT, 0x00}
111 };
112 /* Start the receive of the cyrf6936 */
114  {CYRF_RX_IRQ_STATUS, CYRF_RXOW_IRQ}, // Clear the IRQ
115  {CYRF_RX_CTRL, CYRF_RX_GO | CYRF_RXC_IRQEN | CYRF_RXE_IRQEN} // Start receiving and set the IRQ
116 };
117 
118 /* The PN codes used for DSM2 and DSMX channel hopping */
119 static const uint8_t pn_codes[5][9][8] = {
120 { /* Row 0 */
121  /* Col 0 */ {0x03, 0xBC, 0x6E, 0x8A, 0xEF, 0xBD, 0xFE, 0xF8},
122  /* Col 1 */ {0x88, 0x17, 0x13, 0x3B, 0x2D, 0xBF, 0x06, 0xD6},
123  /* Col 2 */ {0xF1, 0x94, 0x30, 0x21, 0xA1, 0x1C, 0x88, 0xA9},
124  /* Col 3 */ {0xD0, 0xD2, 0x8E, 0xBC, 0x82, 0x2F, 0xE3, 0xB4},
125  /* Col 4 */ {0x8C, 0xFA, 0x47, 0x9B, 0x83, 0xA5, 0x66, 0xD0},
126  /* Col 5 */ {0x07, 0xBD, 0x9F, 0x26, 0xC8, 0x31, 0x0F, 0xB8},
127  /* Col 6 */ {0xEF, 0x03, 0x95, 0x89, 0xB4, 0x71, 0x61, 0x9D},
128  /* Col 7 */ {0x40, 0xBA, 0x97, 0xD5, 0x86, 0x4F, 0xCC, 0xD1},
129  /* Col 8 */ {0xD7, 0xA1, 0x54, 0xB1, 0x5E, 0x89, 0xAE, 0x86}
130 },
131 { /* Row 1 */
132  /* Col 0 */ {0x83, 0xF7, 0xA8, 0x2D, 0x7A, 0x44, 0x64, 0xD3},
133  /* Col 1 */ {0x3F, 0x2C, 0x4E, 0xAA, 0x71, 0x48, 0x7A, 0xC9},
134  /* Col 2 */ {0x17, 0xFF, 0x9E, 0x21, 0x36, 0x90, 0xC7, 0x82},
135  /* Col 3 */ {0xBC, 0x5D, 0x9A, 0x5B, 0xEE, 0x7F, 0x42, 0xEB},
136  /* Col 4 */ {0x24, 0xF5, 0xDD, 0xF8, 0x7A, 0x77, 0x74, 0xE7},
137  /* Col 5 */ {0x3D, 0x70, 0x7C, 0x94, 0xDC, 0x84, 0xAD, 0x95},
138  /* Col 6 */ {0x1E, 0x6A, 0xF0, 0x37, 0x52, 0x7B, 0x11, 0xD4},
139  /* Col 7 */ {0x62, 0xF5, 0x2B, 0xAA, 0xFC, 0x33, 0xBF, 0xAF},
140  /* Col 8 */ {0x40, 0x56, 0x32, 0xD9, 0x0F, 0xD9, 0x5D, 0x97}
141 },
142 { /* Row 2 */
143  /* Col 0 */ {0x40, 0x56, 0x32, 0xD9, 0x0F, 0xD9, 0x5D, 0x97},
144  /* Col 1 */ {0x8E, 0x4A, 0xD0, 0xA9, 0xA7, 0xFF, 0x20, 0xCA},
145  /* Col 2 */ {0x4C, 0x97, 0x9D, 0xBF, 0xB8, 0x3D, 0xB5, 0xBE},
146  /* Col 3 */ {0x0C, 0x5D, 0x24, 0x30, 0x9F, 0xCA, 0x6D, 0xBD},
147  /* Col 4 */ {0x50, 0x14, 0x33, 0xDE, 0xF1, 0x78, 0x95, 0xAD},
148  /* Col 5 */ {0x0C, 0x3C, 0xFA, 0xF9, 0xF0, 0xF2, 0x10, 0xC9},
149  /* Col 6 */ {0xF4, 0xDA, 0x06, 0xDB, 0xBF, 0x4E, 0x6F, 0xB3},
150  /* Col 7 */ {0x9E, 0x08, 0xD1, 0xAE, 0x59, 0x5E, 0xE8, 0xF0},
151  /* Col 8 */ {0xC0, 0x90, 0x8F, 0xBB, 0x7C, 0x8E, 0x2B, 0x8E}
152 },
153 { /* Row 3 */
154  /* Col 0 */ {0xC0, 0x90, 0x8F, 0xBB, 0x7C, 0x8E, 0x2B, 0x8E},
155  /* Col 1 */ {0x80, 0x69, 0x26, 0x80, 0x08, 0xF8, 0x49, 0xE7},
156  /* Col 2 */ {0x7D, 0x2D, 0x49, 0x54, 0xD0, 0x80, 0x40, 0xC1},
157  /* Col 3 */ {0xB6, 0xF2, 0xE6, 0x1B, 0x80, 0x5A, 0x36, 0xB4},
158  /* Col 4 */ {0x42, 0xAE, 0x9C, 0x1C, 0xDA, 0x67, 0x05, 0xF6},
159  /* Col 5 */ {0x9B, 0x75, 0xF7, 0xE0, 0x14, 0x8D, 0xB5, 0x80},
160  /* Col 6 */ {0xBF, 0x54, 0x98, 0xB9, 0xB7, 0x30, 0x5A, 0x88},
161  /* Col 7 */ {0x35, 0xD1, 0xFC, 0x97, 0x23, 0xD4, 0xC9, 0x88},
162  /* Col 8 */ {0x88, 0xE1, 0xD6, 0x31, 0x26, 0x5F, 0xBD, 0x40}
163 },
164 { /* Row 4 */
165  /* Col 0 */ {0xE1, 0xD6, 0x31, 0x26, 0x5F, 0xBD, 0x40, 0x93},
166  /* Col 1 */ {0xDC, 0x68, 0x08, 0x99, 0x97, 0xAE, 0xAF, 0x8C},
167  /* Col 2 */ {0xC3, 0x0E, 0x01, 0x16, 0x0E, 0x32, 0x06, 0xBA},
168  /* Col 3 */ {0xE0, 0x83, 0x01, 0xFA, 0xAB, 0x3E, 0x8F, 0xAC},
169  /* Col 4 */ {0x5C, 0xD5, 0x9C, 0xB8, 0x46, 0x9C, 0x7D, 0x84},
170  /* Col 5 */ {0xF1, 0xC6, 0xFE, 0x5C, 0x9D, 0xA5, 0x4F, 0xB7},
171  /* Col 6 */ {0x58, 0xB5, 0xB3, 0xDD, 0x0E, 0x28, 0xF1, 0xB0},
172  /* Col 7 */ {0x5F, 0x30, 0x3B, 0x56, 0x96, 0x45, 0xF4, 0xA1},
173  /* Col 8 */ {0x03, 0xBC, 0x6E, 0x8A, 0xEF, 0xBD, 0xFE, 0xF8}
174 },
175 };
176 static const uint8_t pn_bind[] = { 0x98, 0x88, 0x1B, 0xE4, 0x30, 0x79, 0x03, 0x84 };
177 
178 #if PERIODIC_TELEMETRY
180 
181 static void send_superbit(void) {
182  DOWNLINK_SEND_SUPERBITRF(DefaultChannel, DefaultDevice,
195  6,
197 }
198 #endif
199 
203 void superbitrf_init(void) {
204  // Set the status to uninitialized and set the timer to 0
206  superbitrf.state = 0;
207  superbitrf.timer = 0;
210 
211  // Setup the transmit buffer
214 
217  superbitrf.protocol = 0;
218 
219  // Initialize the binding pin
221 
222  // Initialize the IRQ/DRDY pin
224 
225  // Initialize the cyrf6936 chip
227 
228 #if PERIODIC_TELEMETRY
229  register_periodic_telemetry(DefaultPeriodic, "SUPERBITRF", send_superbit);
230 #endif
231 }
232 
239 
240  // Calculate some values based on the bind MFG id
244 }
245 
249 }
250 
254 void superbitrf_event(void) {
255  uint8_t i, pn_row, data_code[16];
256  static uint8_t packet_size, tx_packet[16];
257  static bool_t start_transfer = TRUE;
258 
259 #ifdef RADIO_CONTROL_LED
260  static uint32_t slowLedCpt = 0;
261 #endif
262 
263  // Check if the cyrf6936 isn't busy and the uperbitrf is initialized
265  return;
266 
267  // When the device is initialized handle the IRQ
269  // First handle the IRQ
271  // Receive the packet
274  }
275 
276  /* Check if it is a valid receive */
278  // Handle the packet received
281 
282  // Reset the packet receiving
284  }
285 
286  /* Check if it has a valid send */
288  // Handle the send packet
291 
292  // Reset the packet receiving
294  }
295  }
296 
297  // Check the status of the superbitrf
298  switch(superbitrf.status) {
299 
300  /* When the superbitrf isn't initialized */
301  case SUPERBITRF_UNINIT:
302  // Try to write the startup config
304  // Check if need to go to bind or transfer
306  start_transfer = FALSE;
307 
309  }
310  break;
311 
312  /* When the superbitrf is initializing binding */
314  /* Switch the different states */
315  switch (superbitrf.state) {
316  case 0:
317  // Try to write the binding config
319  superbitrf.state++;
320  break;
321  case 1:
322  // Set the data code and channel
323  memcpy(data_code, pn_codes[0][8], 8);
324  memcpy(data_code + 8, pn_bind, 8);
325  cyrf6936_write_chan_sop_data_crc(&superbitrf.cyrf6936, 1, pn_codes[0][0], data_code, 0x0000);
326 
328  break;
329  default:
330  // Should not happen
331  superbitrf.state = 0;
332  break;
333  }
334  break;
335 
336  /* When the superbitrf is initializing transfer */
338  // Generate the DSMX channels
340 
341  // Try to write the transfer config
347  superbitrf.state = 1;
348  }
349  break;
350 
351  /* When the superbitrf is in binding mode */
352  case SUPERBITRF_BINDING:
353 
354 #ifdef RADIO_CONTROL_LED
355  slowLedCpt++;
356  if(slowLedCpt>100000){
357 
358  LED_TOGGLE(RADIO_CONTROL_LED);
359  slowLedCpt = 0;
360  }
361 #endif
362 
363  /* Switch the different states */
364  switch (superbitrf.state) {
365  case 0:
366  // When there is a timeout
368  superbitrf.state++;
369  break;
370  case 1:
371  // Abort the receive
373 
374  superbitrf.state++;
375  break;
376  case 2:
377  // Switch channel
378  superbitrf.channel = (superbitrf.channel + 2) % 0x4F; //TODO fix define
380 
381  superbitrf.state += 2; // Already aborted
382  break;
383  case 3:
384  // Abort the receive
386 
387  superbitrf.state++;
388  break;
389  case 4:
390  // Start receiving
392  superbitrf.state++;
393  break;
394  default:
395  // Check if need to go to transfer
396  if(start_transfer) {
397  // Initialize the binding values
398  // set values based on mfg id
399  // if bind_mfg_id32 is loaded from persistent settings use that,
402  }
403  #ifdef RADIO_TRANSMITTER_ID
404  // otherwise load airframe file value
405  else {
406  PRINT_CONFIG_VAR(RADIO_TRANSMITTER_ID);
407  superbitrf_set_mfg_id(RADIO_TRANSMITTER_ID);
408  }
409  #endif
410  #ifdef RADIO_TRANSMITTER_CHAN
411  PRINT_CONFIG_VAR(RADIO_TRANSMITTER_CHAN);
412  if (superbitrf.num_channels == 0) {
413  superbitrf.num_channels = RADIO_TRANSMITTER_CHAN;
414  }
415  #endif
416  if (superbitrf.protocol == 0) {
418  }
419  #ifdef RADIO_TRANSMITTER_PROTOCOL
420  else {
421  PRINT_CONFIG_VAR(RADIO_TRANSMITTER_PROTOCOL);
422  superbitrf_set_protocol(RADIO_TRANSMITTER_PROTOCOL);
423  }
424  #endif
425 
426  // Start transfer
427  superbitrf.state = 0;
429  break;
430  }
431 
432  // Set the timer
434  superbitrf.state = 0;
435  break;
436  }
437  break;
438 
439  /* When the receiver is synchronizing with the transmitter */
442 
443 #ifdef RADIO_CONTROL_LED
444  slowLedCpt++;
445  if(slowLedCpt>5000){
446 
447  LED_TOGGLE(RADIO_CONTROL_LED);
448  slowLedCpt = 0;
449  }
450 #endif
451 
452  /* Switch the different states */
453  switch (superbitrf.state) {
454  case 0:
455  // When there is a timeout
457  superbitrf.state++;
458  break;
459  case 1:
460  // Abort the receive
462  superbitrf.state++;
463  break;
464  case 2:
465  // Switch channel, sop code, data code and crc
468 
470  pn_codes[pn_row][superbitrf.sop_col],
471  pn_codes[pn_row][superbitrf.data_col],
473  superbitrf.state++;
474  break;
475  case 3:
476  // Create a new packet when no packet loss
477  if(!superbitrf.packet_loss) {
480  tx_packet[0] = ~superbitrf.bind_mfg_id[2];
481  tx_packet[1] = (~superbitrf.bind_mfg_id[3])+1+superbitrf.packet_loss_bit;
482  } else {
483  tx_packet[0] = superbitrf.bind_mfg_id[2];
484  tx_packet[1] = (superbitrf.bind_mfg_id[3])+1+superbitrf.packet_loss_bit;
485  }
486 
487  packet_size = (superbitrf.tx_insert_idx-superbitrf.tx_extract_idx+128 %128);
488  if(packet_size > 14)
489  packet_size = 14;
490 
491  for(i = 0; i < packet_size; i++)
492  tx_packet[i+2] = superbitrf.tx_buffer[(superbitrf.tx_extract_idx+i) %128];
493  }
494 
495  // Send a packet
496  cyrf6936_send(&superbitrf.cyrf6936, tx_packet, packet_size+2);
497 
498  // Update the packet extraction
500  superbitrf.tx_extract_idx = (superbitrf.tx_extract_idx+packet_size) %128;
501 
502  superbitrf.state++;
503  break;
504  case 4:
505  //TODO: check timeout? (Waiting for send)
506  break;
507  case 5:
508  superbitrf.state = 7;
509  break;
510  // Start receiving
513  superbitrf.state++;
514  break;
515  case 6:
516  // Wait for telemetry data
518  superbitrf.state++;
519  break;
520  case 7:
521  // When DSMX we don't need to switch
523  superbitrf.state++;
525  break;
526  }
527 
528  // Switch channel, sop code, data code and crc
529  superbitrf.channel = (superbitrf.channel + 2) % 0x4F; //TODO fix define
532 
534  pn_codes[pn_row][superbitrf.sop_col],
535  pn_codes[pn_row][superbitrf.data_col],
537 
538  superbitrf.state++;
539  break;
540  case 8:
541  // Start receiving
543  superbitrf.state++;
544  break;
545  default:
546  // Set the timer
548  superbitrf.state = 0;
549  break;
550  }
551  break;
552 
553  /* Normal transfer mode */
554  case SUPERBITRF_TRANSFER:
555 
556 #ifdef RADIO_CONTROL_LED
557  LED_ON(RADIO_CONTROL_LED);
558 #endif
559 
560  /* Switch the different states */
561  switch (superbitrf.state) {
562  case 0:
563  // Fixing timer overflow
566 
567  // When there is a timeout
571  superbitrf.state++;
572  }
573 
574  // We really lost the communication
575  if(superbitrf.timeouts > 100) {
576  superbitrf.state = 0;
579  }
580  break;
581  case 1:
582  // Abort the receive
584  superbitrf.state++;
585 
586  // Set the timer
590  else
592 
593  // Only send on channel 2
595  superbitrf.state = 8;
596  break;
597  case 2:
598  // Wait before sending (FIXME??)
599  superbitrf.state++;
600  break;
601  case 3:
602  // Create a new packet when no packet loss
603  if(!superbitrf.packet_loss) {
606  tx_packet[0] = ~superbitrf.bind_mfg_id[2];
607  tx_packet[1] = ((~superbitrf.bind_mfg_id[3])+1+superbitrf.packet_loss_bit) % 0xFF;
608  } else {
609  tx_packet[0] = superbitrf.bind_mfg_id[2];
610  tx_packet[1] = ((superbitrf.bind_mfg_id[3])+1+superbitrf.packet_loss_bit) % 0xFF;
611  }
612 
613  packet_size = (superbitrf.tx_insert_idx-superbitrf.tx_extract_idx+128 %128);
614  if(packet_size > 14)
615  packet_size = 14;
616 
617  for(i = 0; i < packet_size; i++)
618  tx_packet[i+2] = superbitrf.tx_buffer[(superbitrf.tx_extract_idx+i) %128];
619  }
620 
621  // Send a packet
622  cyrf6936_send(&superbitrf.cyrf6936, tx_packet, packet_size+2);
623 
624  // Update the packet extraction
626  superbitrf.tx_extract_idx = (superbitrf.tx_extract_idx+packet_size) %128;
627 
628  superbitrf.state++;
629  break;
630  case 4:
631  //TODO: check timeout? (Waiting for send)
632  break;
633  case 5:
634  // Start receiving
636  superbitrf.state++;
637  break;
638  case 6:
639  // Fixing timer overflow
642 
643  // Waiting for data receive
645  superbitrf.state++;
646  break;
647  case 7:
648  // Abort the receive
650  superbitrf.state++;
651  break;
652  case 8:
653  // Switch channel, sop code, data code and crc
658 
660  pn_codes[pn_row][superbitrf.sop_col],
661  pn_codes[pn_row][superbitrf.data_col],
663 
664  superbitrf.state++;
665  break;
666  case 9:
667  // Start receiving
669  superbitrf.state++;
670  break;
671  default:
672  // Set the timer
675  else
679  else
681 
682  superbitrf.state = 0;
683  break;
684  }
685  break;
686 
687  /* Should not come here */
688  default:
689  break;
690  }
691 }
692 
696 static inline void superbitrf_receive_packet_cb(bool_t error, uint8_t status, uint8_t packet[]) {
697  int i;
698  uint16_t sum;
699 
700  /* Switch on the status of the superbitRF */
701  switch (superbitrf.status) {
702 
703  /* When we are in binding mode */
704  case SUPERBITRF_BINDING:
705  // Check if the MFG id is exactly the same
706  if (packet[0] != packet[4] || packet[1] != packet[5] || packet[2] != packet[6] || packet[3] != packet[7]) {
707  // Start receiving without changing channel
708  superbitrf.state = 3;
709  break;
710  }
711 
712  // Calculate the first sum
713  sum = 384 - 0x10;
714  for (i = 0; i < 8; i++)
715  sum += packet[i];
716 
717  // Check the first sum
718  if (packet[8] != sum >> 8 || packet[9] != (sum & 0xFF)) {
719  // Start receiving without changing channel
720  superbitrf.state = 3;
721  break;
722  }
723 
724  // Calculate second sum
725  for (i = 8; i < 14; i++)
726  sum += packet[i];
727 
728  // Check the second sum
729  if (packet[14] != sum >> 8 || packet[15] != (sum & 0xFF)) {
730  // Start receiving without changing channel
731  superbitrf.state = 3;
732  break;
733  }
734 
735  // Update the mfg id, number of channels and protocol
736  uint32_t mfg_id = ((~packet[3] &0xFF) << 24 | (~packet[2] &0xFF) << 16 |
737  (~packet[1] &0xFF) << 8 | (~packet[0] &0xFF));
738  superbitrf_set_mfg_id(mfg_id);
739 
740  superbitrf.num_channels = packet[11];
741  superbitrf_set_protocol(packet[12]);
742 
743  // Store all the persistent settings.
744  // In case we have the superbit setting file loaded and persistent settings
745  // enabled in the airframe file this will store our binding information and
746  // survive a reboot.
748 
749  // Update the status of the receiver
750  superbitrf.state = 0;
752  break;
753 
754  /* When we receive a packet during syncing first channel A */
756  // Check the MFG id
757  if(error && !(status & CYRF_BAD_CRC)) {
758  // Start receiving TODO: Fix nicely
760  break;
761  }
763  (packet[0] != (~superbitrf.bind_mfg_id[2]&0xFF) || (packet[1] != (~superbitrf.bind_mfg_id[3]&0xFF) &&
764  packet[1] != (~superbitrf.bind_mfg_id[3]&0xFF)+1))) {
765  // Start receiving TODO: Fix nicely
767  break;
768  }
770  (packet[0] != (superbitrf.bind_mfg_id[2]&0xFF) || (packet[1] != (superbitrf.bind_mfg_id[3]&0xFF) &&
771  packet[1] != (superbitrf.bind_mfg_id[3]&0xFF)+1))) {
772  // Start receiving TODO: Fix nicely
774  break;
775  }
776 
777  // If the CRC is wrong invert
778  if (error && (status & CYRF_BAD_CRC))
780 
781  // When we receive a data packet
782  if(packet[1] != (~superbitrf.bind_mfg_id[3]&0xFF) && packet[1] != (superbitrf.bind_mfg_id[3]&0xFF)) {
784 
785  // Check if it is a data loss packet
786  if(packet[1] != (~superbitrf.bind_mfg_id[3] + 1 + superbitrf.packet_loss_bit)%0xFF && packet[1] != (superbitrf.bind_mfg_id[3] + 1 + superbitrf.packet_loss_bit)%0xFF)
788  else
790 
791  // When it is a data packet, parse the packet if not busy already
793  for(i = 2; i < superbitrf.cyrf6936.rx_count; i++) {
794  parse_pprz(&superbitrf.rx_transport, packet[i]);
795 
796  // When we have a full message
800  }
801  }
802  }
803  break;
804  }
805 
809 
810  superbitrf.state = 1;
812  } else {
813  superbitrf.timeouts = 0;
814  superbitrf.state = 1;
816  }
817  break;
818 
819  /* When we receive a packet during syncing second channel B */
821  // Check the MFG id
822  if(error && !(status & CYRF_BAD_CRC)) {
823  // Start receiving TODO: Fix nicely
825  break;
826  }
828  (packet[0] != (~superbitrf.bind_mfg_id[2]&0xFF) || (packet[1] != (~superbitrf.bind_mfg_id[3]&0xFF) &&
829  packet[1] != (~superbitrf.bind_mfg_id[3]&0xFF)+1 && packet[1] != (~superbitrf.bind_mfg_id[3]&0xFF)+2))) {
830  // Start receiving TODO: Fix nicely
832  break;
833  }
835  (packet[0] != (superbitrf.bind_mfg_id[2]&0xFF) || (packet[1] != (superbitrf.bind_mfg_id[3]&0xFF) &&
836  packet[1] != (superbitrf.bind_mfg_id[3]&0xFF)+1 && packet[1] != (superbitrf.bind_mfg_id[3]&0xFF)+2))) {
837  // Start receiving TODO: Fix nicely
839  break;
840  }
841 
842  // If the CRC is wrong invert
843  if (error && (status & CYRF_BAD_CRC))
845 
846  // When we receive a data packet
847  if(packet[1] != (~superbitrf.bind_mfg_id[3]&0xFF) && packet[1] != (superbitrf.bind_mfg_id[3]&0xFF)) {
849 
850  // When it is a data packet, parse the packet if not busy already
851  if(!dl_msg_available) {
852  for(i = 2; i < superbitrf.cyrf6936.rx_count; i++) {
853  parse_pprz(&superbitrf.rx_transport, packet[i]);
854 
855  // When we have a full message
859  }
860  }
861  }
862  break;
863  }
864 
865  // Set the channel
869  }
870  else {
873  }
874 
875  // When the channels aren't the same go to transfer mode
876  if(superbitrf.channels[1] != superbitrf.channels[0]) {
877  superbitrf.state = 1;
879  superbitrf.timeouts = 0;
880  }
881  break;
882 
883  /* When we receive a packet during transfer */
884  case SUPERBITRF_TRANSFER:
885  // Check the MFG id
886  if(error) {
887  // Start receiving TODO: Fix nicely
889  break;
890  }
892  (packet[0] != (~superbitrf.bind_mfg_id[2]&0xFF) || (packet[1] != (~superbitrf.bind_mfg_id[3]&0xFF) &&
893  packet[1] != (~superbitrf.bind_mfg_id[3]&0xFF)+1 && packet[1] != (~superbitrf.bind_mfg_id[3]&0xFF)+2))) {
894  // Start receiving TODO: Fix nicely
896  break;
897  }
899  (packet[0] != (superbitrf.bind_mfg_id[2]&0xFF) || (packet[1] != (superbitrf.bind_mfg_id[3]&0xFF) &&
900  packet[1] != (superbitrf.bind_mfg_id[3]&0xFF)+1 && packet[1] != (superbitrf.bind_mfg_id[3]&0xFF)+2))) {
901  // Start receiving TODO: Fix nicely
903  break;
904  }
905 
906  // Check if it is a RC packet
907  if(packet[1] == (~superbitrf.bind_mfg_id[3]&0xFF) || packet[1] == (superbitrf.bind_mfg_id[3]&0xFF)) {
909 
910  // Parse the packet
913 
914  // Calculate the timing (seperately for the channel switches)
917  else
919 
920  // Go to next receive
921  superbitrf.state = 1;
922  superbitrf.timeouts = 0;
923  } else {
925 
926  // Check if it is a data loss packet
927  if(packet[1] != (~superbitrf.bind_mfg_id[3] + 1 + superbitrf.packet_loss_bit) && packet[1] != (superbitrf.bind_mfg_id[3] + 1 + superbitrf.packet_loss_bit))
929  else
931 
933 
934  // When it is a data packet, parse the packet if not busy already
936  for(i = 2; i < superbitrf.cyrf6936.rx_count; i++) {
937  parse_pprz(&superbitrf.rx_transport, packet[i]);
938 
939  // When we have a full message
943  }
944  }
945  }
946 
947  // Update the state
948  superbitrf.state = 7;
949  }
950  break;
951 
952  /* Should not come here */
953  default:
954  break;
955  }
956 }
957 
958 static inline void superbitrf_send_packet_cb(bool_t error __attribute__((unused))) {
959  /* Switch on the status of the superbitRF */
960  switch (superbitrf.status) {
961 
962  /* When we are synchronizing */
965  // When we successfully or unsuccessfully send a data packet
966  if(superbitrf.state == 4)
967  superbitrf.state++;
968  break;
969 
970  /* When we are in transfer mode */
971  case SUPERBITRF_TRANSFER:
972  // When we successfully or unsuccessfully send a packet
973  if(superbitrf.state == 4)
974  superbitrf.state++;
975  break;
976 
977  /* Should not come here */
978  default:
979  break;
980  }
981 }
982 
986 static inline void superbitrf_radio_to_channels(uint8_t* data, uint8_t nb_channels, bool_t is_11bit, int16_t* channels) {
987  int i;
988  uint8_t bit_shift = (is_11bit)? 11:10;
989  int16_t value_max = (is_11bit)? 0x07FF: 0x03FF;
990 
991  for (i=0; i<7; i++) {
992  const int16_t tmp = ((data[2*i]<<8) + data[2*i+1]) & 0x7FFF;
993  const uint8_t chan = (tmp >> bit_shift) & 0x0F;
994  const int16_t val = (tmp&value_max);
995 
996  if(chan < nb_channels) {
997  channels[chan] = val;
998 
999  // Scale the channel
1000  if(is_11bit) {
1001  channels[chan] -= 0x400;
1002  channels[chan] *= MAX_PPRZ/0x2AC;
1003  } else {
1004  channels[chan] -= 0x200;
1005  channels[chan] *= MAX_PPRZ/0x156;
1006  }
1007  }
1008  }
1009 }
1010 
1014 static inline void superbitrf_gen_dsmx_channels(void) {
1015  // Calculate the DSMX channels
1016  int idx = 0;
1017  uint32_t id = ~((superbitrf.bind_mfg_id[0] << 24) | (superbitrf.bind_mfg_id[1] << 16) |
1018  (superbitrf.bind_mfg_id[2] << 8) | (superbitrf.bind_mfg_id[3] << 0));
1019  uint32_t id_tmp = id;
1020 
1021  // While not all channels are set
1022  while(idx < 23) {
1023  int i;
1024  int count_3_27 = 0, count_28_51 = 0, count_52_76 = 0;
1025 
1026  id_tmp = id_tmp * 0x0019660D + 0x3C6EF35F; // Randomization
1027  uint8_t next_ch = ((id_tmp >> 8) % 0x49) + 3; // Use least-significant byte and must be larger than 3
1028  if (((next_ch ^ id) & 0x01 ) == 0)
1029  continue;
1030 
1031  // Go trough all already set channels
1032  for (i = 0; i < idx; i++) {
1033  // Channel is already used
1034  if(superbitrf.channels[i] == next_ch)
1035  break;
1036 
1037  // Count the channel groups
1038  if(superbitrf.channels[i] <= 27)
1039  count_3_27++;
1040  else if (superbitrf.channels[i] <= 51)
1041  count_28_51++;
1042  else
1043  count_52_76++;
1044  }
1045 
1046  // When channel is already used continue
1047  if (i != idx)
1048  continue;
1049 
1050  // Set the channel when channel groups aren't full
1051  if ((next_ch < 28 && count_3_27 < 8) // Channels 3-27: max 8
1052  || (next_ch >= 28 && next_ch < 52 && count_28_51 < 7) // Channels 28-52: max 7
1053  || (next_ch >= 52 && count_52_76 < 8)) { // Channels 52-76: max 8
1054  superbitrf.channels[idx++] = next_ch;
1055  }
1056  }
1057 }
1058 
1059 
1060 
bool_t cyrf6936_send(struct Cyrf6936 *cyrf, const uint8_t data[], const uint8_t length)
Send a packet with a certain length.
Definition: cyrf6936.c:400
bool_t cyrf6936_multi_write(struct Cyrf6936 *cyrf, const uint8_t data[][2], const uint8_t length)
Write to multiple registers one byte.
Definition: cyrf6936.c:319
unsigned short uint16_t
Definition: types.h:16
status
Definition: anemotaxis.c:10
#define CYRF_RXC_IRQEN
uint16_t crc_seed
The CRC seed that is calculated with the bind MFG id.
Definition: superbitrf.h:99
uint8_t channel_idx
The current channel index.
Definition: superbitrf.h:86
#define CYRF_DATA_CODE_LENGTH
bool_t timer_overflow
When the timer overflows.
Definition: superbitrf.h:78
The chip is idle and can be used.
Definition: cyrf6936.h:38
Periodic telemetry system header (includes downlink utility and generated code).
enum dsm_resolution resolution
The resolution that the transmitter has.
Definition: superbitrf.h:98
Some architecture independent helper functions for GPIOs.
#define CYRF_RST
Definition: cyrf6936_regs.h:81
#define CYRF_LEN_EN
static void parse_pprz(struct pprz_transport *t, uint8_t c)
uint8_t sop_col
The sop code column number calculated with the bind MFG id.
Definition: superbitrf.h:100
#define LED_ON(i)
Definition: led_hw.h:28
#define CYRF_FRC_RXDR
enum Cyrf6936Status status
The status of the CYRF6936 chip.
Definition: cyrf6936.h:51
uint8_t rx_packet[16]
The last received packet.
Definition: cyrf6936.h:65
#define CYRF_SOP_LEN
struct transport trans
bool_t register_periodic_telemetry(struct pprz_telemetry *_pt, const char *_msg, telemetry_cb _cb)
Register a telemetry callback function.
Definition: telemetry.c:38
uint8_t bind_mfg_id[4]
The MFG id where the receiver is bound to.
Definition: superbitrf.h:44
bool_t has_irq
When the CYRF6936 is done reading the irq.
Definition: cyrf6936.h:59
uint8_t rx_irq_status
The last receive interrupt status.
Definition: cyrf6936.h:62
struct Cyrf6936 cyrf6936
The cyrf chip used.
Definition: superbitrf.h:74
#define FALSE
Definition: imu_chimu.h:141
bool_t dl_msg_available
Definition: main_demo5.c:56
uint8_t tx_buffer[128]
The transmit buffer.
Definition: superbitrf.h:110
Architecture independent SPI (Serial Peripheral Interface) API.
bool_t cyrf6936_write(struct Cyrf6936 *cyrf, const uint8_t addr, const uint8_t data)
Write to one register.
Definition: cyrf6936.c:309
bool_t cyrf6936_write_chan_sop_data_crc(struct Cyrf6936 *cyrf, const uint8_t chan, const uint8_t sop_code[], const uint8_t data_code[], const uint16_t crc_seed)
Set the channel, SOP code, DATA code and the CRC seed.
Definition: cyrf6936.c:348
#define CYRF_DIS_TXCRC
uint8_t state
The states each status can be in.
Definition: superbitrf.h:76
#define SPEKTRUM_BIND_PIN_PORT
Definition: apogee_0.99.h:263
uint32_t timer
The timer in microseconds.
Definition: superbitrf.h:77
int16_t rc_values[14]
The rc values from the packet.
Definition: superbitrf.h:106
#define CYRF_BAD_CRC
bool_t rc_frame_available
When a RC frame is available.
Definition: superbitrf.h:103
uint32_t uplink_count
How many valid uplink packages are received.
Definition: superbitrf.h:91
#define CYRF_LNA
Architecture independent timing functions.
enum SuperbitRFStatus status
The status of the superbitRF.
Definition: superbitrf.h:75
uint32_t rc_count
How many valid RC packages are received.
Definition: superbitrf.h:92
uint16_t val[TCOUPLE_NB]
unsigned long uint32_t
Definition: types.h:18
uint8_t tx_irq_status
The last send interrupt status.
Definition: cyrf6936.h:61
uint8_t tx_insert_idx
The transmit buffer insert index.
Definition: superbitrf.h:111
signed short int16_t
Definition: types.h:17
#define CYRF_RX_GO
#define CYRF_DIS_RXCRC
uint32_t irq_count
How many interrupts are made.
Definition: superbitrf.h:88
uint32_t tx_packet_count
How many packets are send(also the invalid)
Definition: superbitrf.h:90
#define CYRF_FRC_END
Definition: cyrf6936_regs.h:94
uint8_t tx_extract_idx
The transmit buffer extract index.
Definition: superbitrf.h:112
uint32_t transfer_timeouts
The amount of timeouts during transfer.
Definition: superbitrf.h:80
struct pprz_transport rx_transport
The receive transport.
Definition: superbitrf.h:108
#define LED_TOGGLE(i)
Definition: led_hw.h:30
#define CYRF_RXE_IRQ
uint8_t data_col
The data code column number calculated with the bind MFG id.
Definition: superbitrf.h:101
#define TRUE
Definition: imu_chimu.h:144
uint8_t channels[23]
The channels used for DSM2/DSMX.
Definition: superbitrf.h:85
#define CYRF_RXE_IRQEN
uint8_t packet_loss_bit
The packet loss indicating bit.
Definition: superbitrf.h:82
unsigned char uint8_t
Definition: types.h:14
#define CYRF_RXOW_IRQ
Persistent settings interface.
uint8_t rx_count
The length of the received packet.
Definition: cyrf6936.h:64
bool_t packet_loss
When we have packet loss last packet.
Definition: superbitrf.h:83
#define CYRF_SOP_EN
volatile bool_t msg_received
Definition: transport.h:45
#define CYRF_FAST_TURN_EN
uint8_t timeouts
The amount of timeouts.
Definition: superbitrf.h:79
uint8_t rx_status
The last receive status.
Definition: cyrf6936.h:63
uint8_t num_channels
The number of channels the transmitter has.
Definition: superbitrf.h:46
arch independent LED (Light Emitting Diodes) API
#define CYRF_RXF
Definition: cyrf6936_regs.h:84
uint8_t channel
The current channel number.
Definition: superbitrf.h:87
#define CYRF_RXC_IRQ
bool_t cyrf6936_read_rx_irq_status_packet(struct Cyrf6936 *cyrf)
Read the RX IRQ status register, the rx status register and the rx packet.
Definition: cyrf6936.c:382
uint32_t bind_mfg_id32
The MFG id where the receiver is bound to in uint32.
Definition: superbitrf.h:45
uint32_t rx_packet_count
How many packets are received(also the invalid)
Definition: superbitrf.h:89
#define MAX_PPRZ
Definition: paparazzi.h:8
uint8_t mfg_id[6]
The manufacturer id of the CYRF6936 chip.
Definition: cyrf6936.h:60
static void pprz_parse_payload(struct pprz_transport *t)
#define CYRF_TXC_IRQ
#define CYRF_TXE_IRQ
uint32_t resync_count
The amount of resyncs needed during transfer.
Definition: superbitrf.h:81
#define SPEKTRUM_BIND_PIN
Definition: apogee_0.99.h:262
static void gpio_setup_input(uint32_t port, uint32_t gpios)
Setup one or more pins of the given GPIO port as inputs.
Definition: gpio_arch.h:88
static uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.h:67
uint32_t timing1
Time between last receive in microseconds.
Definition: superbitrf.h:104
#define settings_StoreSettings(_v)
Definition: settings.h:38
enum dsm_protocol protocol
The protocol the transmitter uses.
Definition: superbitrf.h:47
void cyrf6936_init(struct Cyrf6936 *cyrf, struct spi_periph *spi_p, const uint8_t slave_idx, const uint32_t rst_port, const uint16_t rst_pin)
Initializing the cyrf chip.
Definition: cyrf6936.c:46
uint32_t timing2
Time between second last receive in microseconds.
Definition: superbitrf.h:105
uint16_t gpio_get(uint32_t gpioport, uint16_t gpios)
Read a gpio value.