Paparazzi UAS  v5.15_devel-230-gc96ce27
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces 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
41 PRINT_CONFIG_VAR(SUPERBITRF_SPI_DEV)
42 
43 /* Default SuperbitRF RST PORT and PIN */
44 #ifndef SUPERBITRF_RST_PORT
45 #define SUPERBITRF_RST_PORT GPIOC
46 #endif
47 PRINT_CONFIG_VAR(SUPERBITRF_RST_PORT)
48 #ifndef SUPERBITRF_RST_PIN
49 #define SUPERBITRF_RST_PIN GPIO12
50 #endif
51 PRINT_CONFIG_VAR(SUPERBITRF_RST_PIN)
52 
53 /* Default SuperbitRF DRDY(IRQ) PORT and PIN */
54 #ifndef SUPERBITRF_DRDY_PORT
55 #define SUPERBITRF_DRDY_PORT GPIOB
56 #endif
57 PRINT_CONFIG_VAR(SUPERBITRF_DRDY_PORT)
58 #ifndef SUPERBITRF_DRDY_PIN
59 #define SUPERBITRF_DRDY_PIN GPIO1
60 #endif
61 PRINT_CONFIG_VAR(SUPERBITRF_DRDY_PIN)
62 
63 /* Default forcing in DSM2 mode is false */
64 #ifndef SUPERBITRF_FORCE_DSM2
65 #define SUPERBITRF_FORCE_DSM2 TRUE
66 #endif
67 PRINT_CONFIG_VAR(SUPERBITRF_FORCE_DSM2)
68 
69 #ifndef SUPERBITRF_UPDATE_DL
70 #define SUPERBITRF_UPDATE_DL TRUE
71 #endif
72 
73 /* The superbitRF structure */
75 
76 /* The pprz transport structure */
77 struct pprz_transport pprz_srf_tp;
78 
79 /* The internal functions */
80 static inline void superbitrf_radio_to_channels(uint8_t *data, uint8_t nb_channels, bool is_11bit, int16_t *channels);
81 static inline void superbitrf_receive_packet_cb(bool error, uint8_t status, uint8_t packet[]);
82 static inline void superbitrf_send_packet_cb(bool error);
83 static inline void superbitrf_gen_dsmx_channels(void);
84 
85 /* The startup configuration for the cyrf6936 */
86 static const uint8_t cyrf_stratup_config[][2] = {
87  {CYRF_MODE_OVERRIDE, CYRF_RST}, // Reset the device
88  {CYRF_CLK_EN, CYRF_RXF}, // Enable the clock
89  {CYRF_AUTO_CAL_TIME, 0x3C}, // From manual, needed for initialization
90  {CYRF_AUTO_CAL_OFFSET, 0x14}, // From manual, needed for initialization
91  {CYRF_RX_CFG, CYRF_LNA | CYRF_FAST_TURN_EN}, // Enable low noise amplifier and fast turning
92  {CYRF_TX_OFFSET_LSB, 0x55}, // From manual, typical configuration
93  {CYRF_TX_OFFSET_MSB, 0x05}, // From manual, typical configuration
94  {CYRF_XACT_CFG, CYRF_MODE_SYNTH_RX | CYRF_FRC_END}, // Force in Synth RX mode
95  {CYRF_TX_CFG, CYRF_DATA_CODE_LENGTH | CYRF_DATA_MODE_SDR | CYRF_PA_4}, // Enable 64 chip codes, SDR mode and amplifier +4dBm
96  {CYRF_DATA64_THOLD, 0x0E}, // From manual, typical configuration
97  {CYRF_XACT_CFG, CYRF_MODE_SYNTH_RX}, // Set in Synth RX mode (again, really needed?)
98 };
99 /* The binding configuration for the cyrf6936 */
100 static const uint8_t cyrf_bind_config[][2] = {
101  {CYRF_TX_CFG, CYRF_DATA_CODE_LENGTH | CYRF_DATA_MODE_SDR | CYRF_PA_4}, // Enable 64 chip codes, SDR mode and amplifier +4dBm
102  {CYRF_FRAMING_CFG, CYRF_SOP_LEN | 0xE}, // Set SOP CODE to 64 chips and SOP Correlator Threshold to 0xE
103  {CYRF_RX_OVERRIDE, CYRF_FRC_RXDR | CYRF_DIS_RXCRC}, // Force receive data rate and disable receive CRC checker
104  {CYRF_EOP_CTRL, 0x02}, // Only enable EOP symbol count of 2
105  {CYRF_TX_OVERRIDE, CYRF_DIS_TXCRC}, // Disable transmit CRC generate
106 };
107 /* The transfer configuration for the cyrf6936 */
108 static const uint8_t cyrf_transfer_config[][2] = {
109  {CYRF_TX_CFG, CYRF_DATA_CODE_LENGTH | CYRF_DATA_MODE_8DR | CYRF_PA_4}, // Enable 64 chip codes, 8DR mode and amplifier +4dBm
110  {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
111  {CYRF_TX_OVERRIDE, 0x00}, // Reset TX overrides
112  {CYRF_RX_OVERRIDE, 0x00}, // Reset RX overrides
113 };
114 /* Abort the receive of the cyrf6936 */
117  {CYRF_RX_ABORT, 0x00}
118 };
119 /* Start the receive of the cyrf6936 */
121  {CYRF_RX_IRQ_STATUS, CYRF_RXOW_IRQ}, // Clear the IRQ
122  {CYRF_RX_CTRL, CYRF_RX_GO | CYRF_RXC_IRQEN | CYRF_RXE_IRQEN} // Start receiving and set the IRQ
123 };
124 
125 /* The PN codes used for DSM2 and DSMX channel hopping */
126 static const uint8_t pn_codes[5][9][8] = {
127  { /* Row 0 */
128  /* Col 0 */ {0x03, 0xBC, 0x6E, 0x8A, 0xEF, 0xBD, 0xFE, 0xF8},
129  /* Col 1 */ {0x88, 0x17, 0x13, 0x3B, 0x2D, 0xBF, 0x06, 0xD6},
130  /* Col 2 */ {0xF1, 0x94, 0x30, 0x21, 0xA1, 0x1C, 0x88, 0xA9},
131  /* Col 3 */ {0xD0, 0xD2, 0x8E, 0xBC, 0x82, 0x2F, 0xE3, 0xB4},
132  /* Col 4 */ {0x8C, 0xFA, 0x47, 0x9B, 0x83, 0xA5, 0x66, 0xD0},
133  /* Col 5 */ {0x07, 0xBD, 0x9F, 0x26, 0xC8, 0x31, 0x0F, 0xB8},
134  /* Col 6 */ {0xEF, 0x03, 0x95, 0x89, 0xB4, 0x71, 0x61, 0x9D},
135  /* Col 7 */ {0x40, 0xBA, 0x97, 0xD5, 0x86, 0x4F, 0xCC, 0xD1},
136  /* Col 8 */ {0xD7, 0xA1, 0x54, 0xB1, 0x5E, 0x89, 0xAE, 0x86}
137  },
138  { /* Row 1 */
139  /* Col 0 */ {0x83, 0xF7, 0xA8, 0x2D, 0x7A, 0x44, 0x64, 0xD3},
140  /* Col 1 */ {0x3F, 0x2C, 0x4E, 0xAA, 0x71, 0x48, 0x7A, 0xC9},
141  /* Col 2 */ {0x17, 0xFF, 0x9E, 0x21, 0x36, 0x90, 0xC7, 0x82},
142  /* Col 3 */ {0xBC, 0x5D, 0x9A, 0x5B, 0xEE, 0x7F, 0x42, 0xEB},
143  /* Col 4 */ {0x24, 0xF5, 0xDD, 0xF8, 0x7A, 0x77, 0x74, 0xE7},
144  /* Col 5 */ {0x3D, 0x70, 0x7C, 0x94, 0xDC, 0x84, 0xAD, 0x95},
145  /* Col 6 */ {0x1E, 0x6A, 0xF0, 0x37, 0x52, 0x7B, 0x11, 0xD4},
146  /* Col 7 */ {0x62, 0xF5, 0x2B, 0xAA, 0xFC, 0x33, 0xBF, 0xAF},
147  /* Col 8 */ {0x40, 0x56, 0x32, 0xD9, 0x0F, 0xD9, 0x5D, 0x97}
148  },
149  { /* Row 2 */
150  /* Col 0 */ {0x40, 0x56, 0x32, 0xD9, 0x0F, 0xD9, 0x5D, 0x97},
151  /* Col 1 */ {0x8E, 0x4A, 0xD0, 0xA9, 0xA7, 0xFF, 0x20, 0xCA},
152  /* Col 2 */ {0x4C, 0x97, 0x9D, 0xBF, 0xB8, 0x3D, 0xB5, 0xBE},
153  /* Col 3 */ {0x0C, 0x5D, 0x24, 0x30, 0x9F, 0xCA, 0x6D, 0xBD},
154  /* Col 4 */ {0x50, 0x14, 0x33, 0xDE, 0xF1, 0x78, 0x95, 0xAD},
155  /* Col 5 */ {0x0C, 0x3C, 0xFA, 0xF9, 0xF0, 0xF2, 0x10, 0xC9},
156  /* Col 6 */ {0xF4, 0xDA, 0x06, 0xDB, 0xBF, 0x4E, 0x6F, 0xB3},
157  /* Col 7 */ {0x9E, 0x08, 0xD1, 0xAE, 0x59, 0x5E, 0xE8, 0xF0},
158  /* Col 8 */ {0xC0, 0x90, 0x8F, 0xBB, 0x7C, 0x8E, 0x2B, 0x8E}
159  },
160  { /* Row 3 */
161  /* Col 0 */ {0xC0, 0x90, 0x8F, 0xBB, 0x7C, 0x8E, 0x2B, 0x8E},
162  /* Col 1 */ {0x80, 0x69, 0x26, 0x80, 0x08, 0xF8, 0x49, 0xE7},
163  /* Col 2 */ {0x7D, 0x2D, 0x49, 0x54, 0xD0, 0x80, 0x40, 0xC1},
164  /* Col 3 */ {0xB6, 0xF2, 0xE6, 0x1B, 0x80, 0x5A, 0x36, 0xB4},
165  /* Col 4 */ {0x42, 0xAE, 0x9C, 0x1C, 0xDA, 0x67, 0x05, 0xF6},
166  /* Col 5 */ {0x9B, 0x75, 0xF7, 0xE0, 0x14, 0x8D, 0xB5, 0x80},
167  /* Col 6 */ {0xBF, 0x54, 0x98, 0xB9, 0xB7, 0x30, 0x5A, 0x88},
168  /* Col 7 */ {0x35, 0xD1, 0xFC, 0x97, 0x23, 0xD4, 0xC9, 0x88},
169  /* Col 8 */ {0x88, 0xE1, 0xD6, 0x31, 0x26, 0x5F, 0xBD, 0x40}
170  },
171  { /* Row 4 */
172  /* Col 0 */ {0xE1, 0xD6, 0x31, 0x26, 0x5F, 0xBD, 0x40, 0x93},
173  /* Col 1 */ {0xDC, 0x68, 0x08, 0x99, 0x97, 0xAE, 0xAF, 0x8C},
174  /* Col 2 */ {0xC3, 0x0E, 0x01, 0x16, 0x0E, 0x32, 0x06, 0xBA},
175  /* Col 3 */ {0xE0, 0x83, 0x01, 0xFA, 0xAB, 0x3E, 0x8F, 0xAC},
176  /* Col 4 */ {0x5C, 0xD5, 0x9C, 0xB8, 0x46, 0x9C, 0x7D, 0x84},
177  /* Col 5 */ {0xF1, 0xC6, 0xFE, 0x5C, 0x9D, 0xA5, 0x4F, 0xB7},
178  /* Col 6 */ {0x58, 0xB5, 0xB3, 0xDD, 0x0E, 0x28, 0xF1, 0xB0},
179  /* Col 7 */ {0x5F, 0x30, 0x3B, 0x56, 0x96, 0x45, 0xF4, 0xA1},
180  /* Col 8 */ {0x03, 0xBC, 0x6E, 0x8A, 0xEF, 0xBD, 0xFE, 0xF8}
181  },
182 };
183 static const uint8_t pn_bind[] = { 0x98, 0x88, 0x1B, 0xE4, 0x30, 0x79, 0x03, 0x84 };
184 
185 #if PERIODIC_TELEMETRY
187 
188 static void send_superbit(struct transport_tx *trans, struct link_device *dev)
189 {
191  uint8_t cyrf6936_status = superbitrf.cyrf6936.status;
192  pprz_msg_send_SUPERBITRF(trans, dev, AC_ID,
193  &status,
194  &cyrf6936_status,
205  6,
207 }
208 #endif
209 
210 // Functions for the generic device API
211 static int superbitrf_check_free_space(struct SuperbitRF *p, long *fd __attribute__((unused)), uint16_t len)
212 {
213  int space = p->tx_extract_idx - p->tx_insert_idx - 1;
214  if (space < 0) {
215  space += SUPERBITRF_TX_BUFFER_SIZE;
216  }
217  return space >= len ? space : 0;
218 }
219 
220 static void superbitrf_transmit(struct SuperbitRF *p, long fd __attribute__((unused)), uint8_t byte)
221 {
222  p->tx_buffer[p->tx_insert_idx] = byte;
224 }
225 
226 static void superbitrf_transmit_buffer(struct SuperbitRF *p, long fd, uint8_t *data, uint16_t len)
227 {
228  int i;
229  for (i = 0; i < len; i++) {
230  superbitrf_transmit(p, fd, data[i]);
231  }
232 }
233 
234 static void superbitrf_send(struct SuperbitRF *p __attribute__((unused)), long fd __attribute__((unused))) { }
235 
236 static int null_function(struct SuperbitRF *p __attribute__((unused))) { return 0; }
237 
241 void superbitrf_init(void)
242 {
243  // Set the status to uninitialized and set the timer to 0
245  superbitrf.state = 0;
246  superbitrf.timer = 0;
249 
250  // Setup the transmit buffer
253 
256  superbitrf.protocol = 0;
257 
258  // Configure generic device
259  superbitrf.device.periph = (void *)(&superbitrf);
260  superbitrf.device.check_free_space = (check_free_space_t) superbitrf_check_free_space;
261  superbitrf.device.put_byte = (put_byte_t) superbitrf_transmit;
262  superbitrf.device.put_buffer = (put_buffer_t) superbitrf_transmit_buffer;
263  superbitrf.device.send_message = (send_message_t) superbitrf_send;
264  superbitrf.device.char_available = (char_available_t) null_function; // not needed
265  superbitrf.device.get_byte = (get_byte_t) null_function; // not needed
266 
267  // Initialize the binding pin
269 
270  // Initialize the IRQ/DRDY pin
272 
273  // Initialize the cyrf6936 chip
275 
276 #if PERIODIC_TELEMETRY
278 #endif
279 }
280 
285 {
286  // Init pprz transport
287  pprz_transport_init(&pprz_srf_tp);
288 }
289 
294 {
296 }
297 
299 {
302  superbitrf.bind_mfg_id[1] = (superbitrf.bind_mfg_id32 >> 8 & 0xFF);
303  superbitrf.bind_mfg_id[2] = (superbitrf.bind_mfg_id32 >> 16 & 0xFF);
304  superbitrf.bind_mfg_id[3] = (superbitrf.bind_mfg_id32 >> 24 & 0xFF);
305 
306  // Calculate some values based on the bind MFG id
310 }
311 
313 {
314  superbitrf.protocol = protocol;
315  superbitrf.resolution = (superbitrf.protocol & 0x10) >> 4;
316 }
317 
322 {
323  uint8_t i, pn_row, data_code[16];
324  static uint8_t packet_size, tx_packet[16];
325  static bool start_transfer = true;
326 
327 #ifdef RADIO_CONTROL_LED
328  static uint32_t slowLedCpt = 0;
329 #endif
330 
331  // Check if the cyrf6936 isn't busy and the uperbitrf is initialized
333  return;
334  }
335 
336  // When the device is initialized handle the IRQ
338  // First handle the IRQ
340  // Receive the packet
343  }
344 
345  /* Check if it is a valid receive */
347  // Handle the packet received
351 
352  // Reset the packet receiving
353  superbitrf.cyrf6936.has_irq = false;
354  }
355 
356  /* Check if it has a valid send */
358  // Handle the send packet
361 
362  // Reset the packet receiving
363  superbitrf.cyrf6936.has_irq = false;
364  }
365  }
366 
367  // Check the status of the superbitrf
368  switch (superbitrf.status) {
369 
370  /* When the superbitrf isn't initialized */
371  case SUPERBITRF_UNINIT:
372  // Try to write the startup config
373  if (cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_stratup_config, 11)) {
374  // Check if need to go to bind or transfer
376  start_transfer = false;
377  }
378 
380  }
381  break;
382 
383  /* When the superbitrf is initializing binding */
385  /* Switch the different states */
386  switch (superbitrf.state) {
387  case 0:
388  // Try to write the binding config
389  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_bind_config, 5);
390  superbitrf.state++;
391  break;
392  case 1:
393  // Set the data code and channel
394  memcpy(data_code, pn_codes[0][8], 8);
395  memcpy(data_code + 8, pn_bind, 8);
396  cyrf6936_write_chan_sop_data_crc(&superbitrf.cyrf6936, 1, pn_codes[0][0], data_code, 0x0000);
397 
399  break;
400  default:
401  // Should not happen
402  superbitrf.state = 0;
403  break;
404  }
405  break;
406 
407  /* When the superbitrf is initializing transfer */
409  // Generate the DSMX channels
411 
412  // Try to write the transfer config
413  if (cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_transfer_config, 4)) {
415  superbitrf.packet_loss = false;
418  superbitrf.state = 1;
419  }
420  break;
421 
422  /* When the superbitrf is in binding mode */
423  case SUPERBITRF_BINDING:
424 
425 #ifdef RADIO_CONTROL_LED
426  slowLedCpt++;
427  if (slowLedCpt > 100000) {
428 
429  LED_TOGGLE(RADIO_CONTROL_LED);
430  slowLedCpt = 0;
431  }
432 #endif
433 
434  /* Switch the different states */
435  switch (superbitrf.state) {
436  case 0:
437  // When there is a timeout
439  superbitrf.state++;
440  }
441  break;
442  case 1:
443  // Abort the receive
444  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_abort_receive, 2);
445 
446  superbitrf.state++;
447  break;
448  case 2:
449  // Switch channel
450  superbitrf.channel = (superbitrf.channel + 2) % 0x4F; //TODO fix define
452 
453  superbitrf.state += 2; // Already aborted
454  break;
455  case 3:
456  // Abort the receive
457  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_abort_receive, 2);
458 
459  superbitrf.state++;
460  break;
461  case 4:
462  // Start receiving
463  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
464  superbitrf.state++;
465  break;
466  default:
467  // Check if need to go to transfer
468  if (start_transfer) {
469  // Initialize the binding values
470  // set values based on mfg id
471  // if bind_mfg_id32 is loaded from persistent settings use that,
474  }
475 #ifdef RADIO_TRANSMITTER_ID
476  // otherwise load airframe file value
477  else {
478  PRINT_CONFIG_VAR(RADIO_TRANSMITTER_ID)
479  superbitrf_set_mfg_id(RADIO_TRANSMITTER_ID);
480  }
481 #endif
482 #ifdef RADIO_TRANSMITTER_CHAN
483  PRINT_CONFIG_VAR(RADIO_TRANSMITTER_CHAN)
484  if (superbitrf.num_channels == 0) {
485  superbitrf.num_channels = RADIO_TRANSMITTER_CHAN;
486  }
487 #endif
488  if (superbitrf.protocol == 0) {
490  }
491 #ifdef RADIO_TRANSMITTER_PROTOCOL
492  else {
493  PRINT_CONFIG_VAR(RADIO_TRANSMITTER_PROTOCOL)
494  superbitrf_set_protocol(RADIO_TRANSMITTER_PROTOCOL);
495  }
496 #endif
497 
498  // Start transfer
499  superbitrf.state = 0;
501  break;
502  }
503 
504  // Set the timer
506  superbitrf.state = 0;
507  break;
508  }
509  break;
510 
511  /* When the receiver is synchronizing with the transmitter */
514 
515 #ifdef RADIO_CONTROL_LED
516  slowLedCpt++;
517  if (slowLedCpt > 5000) {
518 
519  LED_TOGGLE(RADIO_CONTROL_LED);
520  slowLedCpt = 0;
521  }
522 #endif
523 
524  /* Switch the different states */
525  switch (superbitrf.state) {
526  case 0:
527  // When there is a timeout
529  superbitrf.state++;
530  }
531  break;
532  case 1:
533  // Abort the receive
534  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_abort_receive, 2);
535  superbitrf.state++;
536  break;
537  case 2:
538  // Switch channel, sop code, data code and crc
541  pn_row = (IS_DSM2(superbitrf.protocol)
544 
546  pn_codes[pn_row][superbitrf.sop_col],
547  pn_codes[pn_row][superbitrf.data_col],
549  superbitrf.state++;
550  break;
551  case 3:
552  // Create a new packet when no packet loss
553  if (!superbitrf.packet_loss) {
556  tx_packet[0] = ~superbitrf.bind_mfg_id[2];
557  tx_packet[1] = (~superbitrf.bind_mfg_id[3]) + 1 + superbitrf.packet_loss_bit;
558  } else {
559  tx_packet[0] = superbitrf.bind_mfg_id[2];
560  tx_packet[1] = (superbitrf.bind_mfg_id[3]) + 1 + superbitrf.packet_loss_bit;
561  }
562 
565  if (packet_size > 14) {
566  packet_size = 14;
567  }
568 
569  for (i = 0; i < packet_size; i++) {
571  }
572  }
573 
574  // Send a packet
575  cyrf6936_send(&superbitrf.cyrf6936, tx_packet, packet_size + 2);
576 
577  // Update the packet extraction
578  if (!superbitrf.packet_loss) {
580  }
581 
582  superbitrf.state++;
583  break;
584  case 4:
585  //TODO: check timeout? (Waiting for send)
586  break;
587  case 5:
588  superbitrf.state = 7;
589  break;
590  case 6:
591  // Wait for telemetry data
593  superbitrf.state++;
594  }
595  break;
596  case 7:
597  // When DSMX we don't need to switch
599  superbitrf.state++;
601  break;
602  }
603 
604  // Switch channel, sop code, data code and crc
605  superbitrf.channel = (superbitrf.channel + 2) % 0x4F; //TODO fix define
607  pn_row = (IS_DSM2(superbitrf.protocol)
609 
611  pn_codes[pn_row][superbitrf.sop_col],
612  pn_codes[pn_row][superbitrf.data_col],
614 
615  superbitrf.state++;
616  break;
617  case 8:
618  // Start receiving
619  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
620  superbitrf.state++;
621  break;
622  default:
623  // Set the timer
625  superbitrf.state = 0;
626  break;
627  }
628  break;
629 
630  /* Normal transfer mode */
631  case SUPERBITRF_TRANSFER:
632 
633 #ifdef RADIO_CONTROL_LED
634  LED_ON(RADIO_CONTROL_LED);
635 #endif
636 
637  /* Switch the different states */
638  switch (superbitrf.state) {
639  case 0:
640  // Fixing timer overflow
642  superbitrf.timer_overflow = false;
643  }
644 
645  // When there is a timeout
649  superbitrf.state++;
650  }
651 
652  // We really lost the communication
653  if (superbitrf.timeouts > 100) {
654  superbitrf.state = 0;
657  }
658  break;
659  case 1:
660  // Abort the receive
661  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_abort_receive, 2);
662  superbitrf.state++;
663 
664  // Set the timer
667  superbitrf.timer_overflow = true;
668  } else {
669  superbitrf.timer_overflow = false;
670  }
671 
672  // Only send on channel 2
674  superbitrf.state = 8;
675  }
676  break;
677  case 2:
678  // Wait before sending (FIXME??)
679  superbitrf.state++;
680  break;
681  case 3:
682  // Create a new packet when no packet loss
683  if (!superbitrf.packet_loss) {
686  tx_packet[0] = ~superbitrf.bind_mfg_id[2];
687  tx_packet[1] = ((~superbitrf.bind_mfg_id[3]) + 1 + superbitrf.packet_loss_bit) % 0xFF;
688  } else {
689  tx_packet[0] = superbitrf.bind_mfg_id[2];
690  tx_packet[1] = ((superbitrf.bind_mfg_id[3]) + 1 + superbitrf.packet_loss_bit) % 0xFF;
691  }
692 
695  if (packet_size > 14) {
696  packet_size = 14;
697  }
698 
699  for (i = 0; i < packet_size; i++) {
701  }
702  }
703 
704  // Send a packet
705  cyrf6936_send(&superbitrf.cyrf6936, tx_packet, packet_size + 2);
706 
707  // Update the packet extraction
708  if (!superbitrf.packet_loss) {
710  }
711 
712  superbitrf.state++;
713  break;
714  case 4:
715  //TODO: check timeout? (Waiting for send)
716  break;
717  case 5:
718  // Start receiving
719  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
720  superbitrf.state++;
721  break;
722  case 6:
723  // Fixing timer overflow
725  superbitrf.timer_overflow = false;
726  }
727 
728  // Waiting for data receive
730  superbitrf.state++;
731  }
732  break;
733  case 7:
734  // Abort the receive
735  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_abort_receive, 2);
736  superbitrf.state++;
737  break;
738  case 8:
739  // Switch channel, sop code, data code and crc
744  pn_row = (IS_DSM2(superbitrf.protocol)
746 
748  pn_codes[pn_row][superbitrf.sop_col],
749  pn_codes[pn_row][superbitrf.data_col],
751 
752  superbitrf.state++;
753  break;
754  case 9:
755  // Start receiving
756  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
757  superbitrf.state++;
758  break;
759  default:
760  // Set the timer
763  } else {
765  }
767  superbitrf.timer_overflow = true;
768  } else {
769  superbitrf.timer_overflow = false;
770  }
771 
772  superbitrf.state = 0;
773  break;
774  }
775  break;
776 
777  /* Should not come here */
778  default:
779  break;
780  }
781 }
782 
786 static inline void superbitrf_receive_packet_cb(bool error, uint8_t status, uint8_t packet[])
787 {
788  int i;
789  uint16_t sum;
790 
791  /* Switch on the status of the superbitRF */
792  switch (superbitrf.status) {
793 
794  /* When we are in binding mode */
795  case SUPERBITRF_BINDING:
796  // Check if the MFG id is exactly the same
797  if (packet[0] != packet[4] || packet[1] != packet[5] || packet[2] != packet[6] || packet[3] != packet[7]) {
798  // Start receiving without changing channel
799  superbitrf.state = 3;
800  break;
801  }
802 
803  // Calculate the first sum
804  sum = 384 - 0x10;
805  for (i = 0; i < 8; i++) {
806  sum += packet[i];
807  }
808 
809  // Check the first sum
810  if (packet[8] != sum >> 8 || packet[9] != (sum & 0xFF)) {
811  // Start receiving without changing channel
812  superbitrf.state = 3;
813  break;
814  }
815 
816  // Calculate second sum
817  for (i = 8; i < 14; i++) {
818  sum += packet[i];
819  }
820 
821  // Check the second sum
822  if (packet[14] != sum >> 8 || packet[15] != (sum & 0xFF)) {
823  // Start receiving without changing channel
824  superbitrf.state = 3;
825  break;
826  }
827 
828  // Update the mfg id, number of channels and protocol
829  uint32_t mfg_id = ((~packet[3] & 0xFF) << 24 | (~packet[2] & 0xFF) << 16 |
830  (~packet[1] & 0xFF) << 8 | (~packet[0] & 0xFF));
831  superbitrf_set_mfg_id(mfg_id);
832 
833  superbitrf.num_channels = packet[11];
834  superbitrf_set_protocol(packet[12]);
835 
836  // Store all the persistent settings.
837  // In case we have the superbit setting file loaded and persistent settings
838  // enabled in the airframe file this will store our binding information and
839  // survive a reboot.
841 
842  // Update the status of the receiver
843  superbitrf.state = 0;
845  break;
846 
847  /* When we receive a packet during syncing first channel A */
849  // Check the MFG id
850  if (error && !(status & CYRF_BAD_CRC)) {
851  // Start receiving TODO: Fix nicely
852  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
853  break;
854  }
856  (packet[0] != (~superbitrf.bind_mfg_id[2] & 0xFF) || (packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) &&
857  packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) + 1))) {
858  // Start receiving TODO: Fix nicely
859  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
860  break;
861  }
863  (packet[0] != (superbitrf.bind_mfg_id[2] & 0xFF) || (packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF) &&
864  packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF) + 1))) {
865  // Start receiving TODO: Fix nicely
866  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
867  break;
868  }
869 
870  // If the CRC is wrong invert
871  if (error && (status & CYRF_BAD_CRC)) {
873  }
874 
875  // When we receive a data packet
876  if (packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) && packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF)) {
878 
879  // Check if it is a data loss packet
880  if (packet[1] != (~superbitrf.bind_mfg_id[3] + 1 + superbitrf.packet_loss_bit) % 0xFF
881  && packet[1] != (superbitrf.bind_mfg_id[3] + 1 + superbitrf.packet_loss_bit) % 0xFF) {
882  superbitrf.packet_loss = true;
883  } else {
884  superbitrf.packet_loss = false;
885  }
886 
887  // When it is a data packet, parse the packet if not busy already
889  for (i = 2; i < superbitrf.cyrf6936.rx_count; i++) {
890  parse_pprz(&superbitrf.rx_transport, packet[i]);
891 
892  // When we have a full message
893  if (superbitrf.rx_transport.trans_rx.msg_received) {
894  DatalinkFillDlBuffer(superbitrf.rx_transport.trans_rx.payload, superbitrf.rx_transport.trans_rx.payload_len);
895  superbitrf.rx_transport.trans_rx.msg_received = false;
896  }
897  }
898  }
899  break;
900  }
901 
905 
906  superbitrf.state = 1;
908  } else {
909  superbitrf.timeouts = 0;
910  superbitrf.state = 1;
912  }
913  break;
914 
915  /* When we receive a packet during syncing second channel B */
917  // Check the MFG id
918  if (error && !(status & CYRF_BAD_CRC)) {
919  // Start receiving TODO: Fix nicely
920  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
921  break;
922  }
924  (packet[0] != (~superbitrf.bind_mfg_id[2] & 0xFF) || (packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) &&
925  packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) + 1 && packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) + 2))) {
926  // Start receiving TODO: Fix nicely
927  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
928  break;
929  }
931  (packet[0] != (superbitrf.bind_mfg_id[2] & 0xFF) || (packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF) &&
932  packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF) + 1 && packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF) + 2))) {
933  // Start receiving TODO: Fix nicely
934  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
935  break;
936  }
937 
938  // If the CRC is wrong invert
939  if (error && (status & CYRF_BAD_CRC)) {
941  }
942 
943  // When we receive a data packet
944  if (packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) && packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF)) {
946 
947  // When it is a data packet, parse the packet if not busy already
948  if (!dl_msg_available) {
949  for (i = 2; i < superbitrf.cyrf6936.rx_count; i++) {
950  parse_pprz(&superbitrf.rx_transport, packet[i]);
951 
952  // When we have a full message
953  if (superbitrf.rx_transport.trans_rx.msg_received) {
954  DatalinkFillDlBuffer(superbitrf.rx_transport.trans_rx.payload, superbitrf.rx_transport.trans_rx.payload_len);
955  superbitrf.rx_transport.trans_rx.msg_received = false;
956  }
957  }
958  }
959  break;
960  }
961 
962  // Set the channel
966  } else {
969  }
970 
971  // When the channels aren't the same go to transfer mode
972  if (superbitrf.channels[1] != superbitrf.channels[0]) {
973  superbitrf.state = 1;
975  superbitrf.timeouts = 0;
976  }
977  break;
978 
979  /* When we receive a packet during transfer */
980  case SUPERBITRF_TRANSFER:
981  // Check the MFG id
982  if (error) {
983  // Start receiving TODO: Fix nicely
984  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
985  break;
986  }
988  (packet[0] != (~superbitrf.bind_mfg_id[2] & 0xFF) || (packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) &&
989  packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) + 1 && packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) + 2))) {
990  // Start receiving TODO: Fix nicely
991  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
992  break;
993  }
995  (packet[0] != (superbitrf.bind_mfg_id[2] & 0xFF) || (packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF) &&
996  packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF) + 1 && packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF) + 2))) {
997  // Start receiving TODO: Fix nicely
998  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
999  break;
1000  }
1001 
1002  // Check if it is a RC packet
1003  if (packet[1] == (~superbitrf.bind_mfg_id[3] & 0xFF) || packet[1] == (superbitrf.bind_mfg_id[3] & 0xFF)) {
1004  superbitrf.rc_count++;
1005 
1006  // Parse the packet
1009 
1010  // Calculate the timing (seperately for the channel switches)
1011  if (superbitrf.crc_seed != ((superbitrf.bind_mfg_id[0] << 8) + superbitrf.bind_mfg_id[1])) {
1013  } else {
1015  }
1016 
1017  // Go to next receive
1018  superbitrf.state = 1;
1019  superbitrf.timeouts = 0;
1020  } else {
1022 
1023  // Check if it is a data loss packet
1024  if (packet[1] != (~superbitrf.bind_mfg_id[3] + 1 + superbitrf.packet_loss_bit)
1025  && packet[1] != (superbitrf.bind_mfg_id[3] + 1 + superbitrf.packet_loss_bit)) {
1026  superbitrf.packet_loss = true;
1027  } else {
1028  superbitrf.packet_loss = false;
1029  }
1030 
1031  superbitrf.packet_loss = false;
1032 
1033  // When it is a data packet, parse the packet if not busy already
1035  for (i = 2; i < superbitrf.cyrf6936.rx_count; i++) {
1036  parse_pprz(&superbitrf.rx_transport, packet[i]);
1037 
1038  // When we have a full message
1039  if (superbitrf.rx_transport.trans_rx.msg_received) {
1040  DatalinkFillDlBuffer(superbitrf.rx_transport.trans_rx.payload, superbitrf.rx_transport.trans_rx.payload_len);
1041  superbitrf.rx_transport.trans_rx.msg_received = false;
1042  }
1043  }
1044  }
1045 
1046  // Update the state
1047  superbitrf.state = 7;
1048  }
1049  break;
1050 
1051  /* Should not come here */
1052  default:
1053  break;
1054  }
1055 }
1056 
1057 static inline void superbitrf_send_packet_cb(bool error __attribute__((unused)))
1058 {
1059  /* Switch on the status of the superbitRF */
1060  switch (superbitrf.status) {
1061 
1062  /* When we are synchronizing */
1063  case SUPERBITRF_SYNCING_A:
1064  case SUPERBITRF_SYNCING_B:
1065  // When we successfully or unsuccessfully send a data packet
1066  if (superbitrf.state == 4) {
1067  superbitrf.state++;
1068  }
1069  break;
1070 
1071  /* When we are in transfer mode */
1072  case SUPERBITRF_TRANSFER:
1073  // When we successfully or unsuccessfully send a packet
1074  if (superbitrf.state == 4) {
1075  superbitrf.state++;
1076  }
1077  break;
1078 
1079  /* Should not come here */
1080  default:
1081  break;
1082  }
1083 }
1084 
1088 static inline void superbitrf_radio_to_channels(uint8_t *data, uint8_t nb_channels, bool is_11bit, int16_t *channels)
1089 {
1090  int i;
1091  uint8_t bit_shift = (is_11bit) ? 11 : 10;
1092  int16_t value_max = (is_11bit) ? 0x07FF : 0x03FF;
1093 
1094  for (i = 0; i < 7; i++) {
1095  const int16_t tmp = ((data[2 * i] << 8) + data[2 * i + 1]) & 0x7FFF;
1096  const uint8_t chan = (tmp >> bit_shift) & 0x0F;
1097  const int16_t val = (tmp & value_max);
1098 
1099  if (chan < nb_channels) {
1100  channels[chan] = val;
1101 
1102  // Scale the channel
1103  if (is_11bit) {
1104  channels[chan] -= 0x400;
1105  channels[chan] *= MAX_PPRZ / 0x2AC;
1106  } else {
1107  channels[chan] -= 0x200;
1108  channels[chan] *= MAX_PPRZ / 0x156;
1109  }
1110  }
1111  }
1112 }
1113 
1117 static inline void superbitrf_gen_dsmx_channels(void)
1118 {
1119  // Calculate the DSMX channels
1120  int idx = 0;
1121  uint32_t id = ~((superbitrf.bind_mfg_id[0] << 24) | (superbitrf.bind_mfg_id[1] << 16) |
1122  (superbitrf.bind_mfg_id[2] << 8) | (superbitrf.bind_mfg_id[3] << 0));
1123  uint32_t id_tmp = id;
1124 
1125  // While not all channels are set
1126  while (idx < 23) {
1127  int i;
1128  int count_3_27 = 0, count_28_51 = 0, count_52_76 = 0;
1129 
1130  id_tmp = id_tmp * 0x0019660D + 0x3C6EF35F; // Randomization
1131  uint8_t next_ch = ((id_tmp >> 8) % 0x49) + 3; // Use least-significant byte and must be larger than 3
1132  if (((next_ch ^ id) & 0x01) == 0) {
1133  continue;
1134  }
1135 
1136  // Go trough all already set channels
1137  for (i = 0; i < idx; i++) {
1138  // Channel is already used
1139  if (superbitrf.channels[i] == next_ch) {
1140  break;
1141  }
1142 
1143  // Count the channel groups
1144  if (superbitrf.channels[i] <= 27) {
1145  count_3_27++;
1146  } else if (superbitrf.channels[i] <= 51) {
1147  count_28_51++;
1148  } else {
1149  count_52_76++;
1150  }
1151  }
1152 
1153  // When channel is already used continue
1154  if (i != idx) {
1155  continue;
1156  }
1157 
1158  // Set the channel when channel groups aren't full
1159  if ((next_ch < 28 && count_3_27 < 8) // Channels 3-27: max 8
1160  || (next_ch >= 28 && next_ch < 52 && count_28_51 < 7) // Channels 28-52: max 7
1161  || (next_ch >= 52 && count_52_76 < 8)) { // Channels 52-76: max 8
1162  superbitrf.channels[idx++] = next_ch;
1163  }
1164  }
1165 }
1166 
1167 
1168 
unsigned short uint16_t
Definition: types.h:16
bool has_irq
When the CYRF6936 is done reading the irq.
Definition: cyrf6936.h:59
#define CYRF_RXC_IRQEN
static uint32_t idx
#define DOWNLINK_DEVICE
uint8_t rx_packet[16]
The last received packet.
Definition: cyrf6936.h:65
#define CYRF_DATA_CODE_LENGTH
The chip is idle and can be used.
Definition: cyrf6936.h:38
Periodic telemetry system header (includes downlink utility and generated code).
uint8_t rx_status
The last receive status.
Definition: cyrf6936.h:63
Some architecture independent helper functions for GPIOs.
#define CYRF_RST
Definition: cyrf6936_regs.h:81
#define CYRF_LEN_EN
bool cyrf6936_write(struct Cyrf6936 *cyrf, const uint8_t addr, const uint8_t data)
Write to one register.
Definition: cyrf6936.c:330
#define CYRF_FRC_RXDR
bool cyrf6936_multi_write(struct Cyrf6936 *cyrf, const uint8_t data[][2], const uint8_t length)
Write to multiple registers one byte.
Definition: cyrf6936.c:341
bool 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:412
#define CYRF_SOP_LEN
bool dl_msg_available
Definition: main_demo5.c:61
uint8_t rx_irq_status
The last receive interrupt status.
Definition: cyrf6936.h:62
Architecture independent SPI (Serial Peripheral Interface) API.
#define CYRF_DIS_TXCRC
enum Cyrf6936Status status
The status of the CYRF6936 chip.
Definition: cyrf6936.h:51
#define CYRF_BAD_CRC
#define CYRF_LNA
Architecture independent timing functions.
uint8_t mfg_id[6]
The manufacturer id of the CYRF6936 chip.
Definition: cyrf6936.h:60
uint8_t status
uint16_t val[TCOUPLE_NB]
unsigned long uint32_t
Definition: types.h:18
signed short int16_t
Definition: types.h:17
#define CYRF_RX_GO
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
#define CYRF_DIS_RXCRC
#define CYRF_FRC_END
Definition: cyrf6936_regs.h:94
#define SPEKTRUM_BIND_PIN
Definition: board.h:442
#define LED_TOGGLE(i)
Definition: led_hw.h:53
#define CYRF_RXE_IRQ
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:73
uint8_t rx_count
The length of the received packet.
Definition: cyrf6936.h:64
#define CYRF_RXE_IRQEN
static uint8_t gpio_get(ioportid_t port, uint16_t pin)
Get level of a gpio.
Definition: gpio_arch.h:88
unsigned char uint8_t
Definition: types.h:14
#define CYRF_RXOW_IRQ
Persistent settings interface.
#define byte
#define CYRF_SOP_EN
bool 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:373
int fd
Definition: serial.c:26
#define CYRF_FAST_TURN_EN
#define SPEKTRUM_BIND_PIN_PORT
Definition: board.h:443
uint8_t dl_buffer[MSG_SIZE]
Definition: main_demo5.c:64
arch independent LED (Light Emitting Diodes) API
#define CYRF_RXF
Definition: cyrf6936_regs.h:84
static float p[2][2]
#define LED_ON(i)
Definition: led_hw.h:51
#define CYRF_RXC_IRQ
uint8_t tx_irq_status
The last send interrupt status.
Definition: cyrf6936.h:61
bool cyrf6936_send(struct Cyrf6936 *cyrf, const uint8_t data[], const uint8_t length)
Send a packet with a certain length.
Definition: cyrf6936.c:432
#define MAX_PPRZ
Definition: paparazzi.h:8
#define CYRF_TXC_IRQ
#define CYRF_TXE_IRQ
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:68
void gpio_setup_input(ioportid_t port, uint16_t gpios)
Setup one or more pins of the given GPIO port as inputs.
Definition: gpio_arch.c:40
#define settings_StoreSettings(_v)
Definition: settings.h:40
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:47
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition: telemetry.c:46