Paparazzi UAS  v5.8.2_stable-0-g6260b7c
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 /* 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(struct transport_tx *trans, struct link_device *dev)
182 {
184  uint8_t cyrf6936_status = superbitrf.cyrf6936.status;
185  pprz_msg_send_SUPERBITRF(trans, dev, AC_ID,
186  &status,
187  &cyrf6936_status,
198  6,
200 }
201 #endif
202 
203 // Functions for the generic device API
204 static bool_t superbitrf_check_free_space(struct SuperbitRF *p, uint8_t len)
205 {
206  int16_t space = p->tx_extract_idx - p->tx_insert_idx;
207  if (space <= 0) {
208  space += SUPERBITRF_TX_BUFFER_SIZE;
209  }
210  return (uint16_t)(space - 1) >= len;
211 }
212 
213 static void superbitrf_transmit(struct SuperbitRF *p, uint8_t byte)
214 {
215  p->tx_buffer[p->tx_insert_idx] = byte;
217 }
218 
219 static void superbitrf_send(struct SuperbitRF *p __attribute__((unused))) { }
220 
221 static int null_function(struct SuperbitRF *p __attribute__((unused))) { return 0; }
222 
226 void superbitrf_init(void)
227 {
228  // Set the status to uninitialized and set the timer to 0
230  superbitrf.state = 0;
231  superbitrf.timer = 0;
234 
235  // Setup the transmit buffer
238 
241  superbitrf.protocol = 0;
242 
243  // Configure generic device
244  superbitrf.device.periph = (void *)(&superbitrf);
250 
251  // Initialize the binding pin
253 
254  // Initialize the IRQ/DRDY pin
256 
257  // Initialize the cyrf6936 chip
259 
260 #if PERIODIC_TELEMETRY
261  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_SUPERBITRF, send_superbit);
262 #endif
263 }
264 
266 {
269  superbitrf.bind_mfg_id[1] = (superbitrf.bind_mfg_id32 >> 8 & 0xFF);
270  superbitrf.bind_mfg_id[2] = (superbitrf.bind_mfg_id32 >> 16 & 0xFF);
271  superbitrf.bind_mfg_id[3] = (superbitrf.bind_mfg_id32 >> 24 & 0xFF);
272 
273  // Calculate some values based on the bind MFG id
277 }
278 
280 {
282  superbitrf.resolution = (superbitrf.protocol & 0x10) >> 4;
283 }
284 
289 {
290  uint8_t i, pn_row, data_code[16];
291  static uint8_t packet_size, tx_packet[16];
292  static bool_t start_transfer = TRUE;
293 
294 #ifdef RADIO_CONTROL_LED
295  static uint32_t slowLedCpt = 0;
296 #endif
297 
298  // Check if the cyrf6936 isn't busy and the uperbitrf is initialized
300  return;
301  }
302 
303  // When the device is initialized handle the IRQ
305  // First handle the IRQ
307  // Receive the packet
310  }
311 
312  /* Check if it is a valid receive */
314  // Handle the packet received
318 
319  // Reset the packet receiving
321  }
322 
323  /* Check if it has a valid send */
325  // Handle the send packet
328 
329  // Reset the packet receiving
331  }
332  }
333 
334  // Check the status of the superbitrf
335  switch (superbitrf.status) {
336 
337  /* When the superbitrf isn't initialized */
338  case SUPERBITRF_UNINIT:
339  // Try to write the startup config
341  // Check if need to go to bind or transfer
343  start_transfer = FALSE;
344  }
345 
347  }
348  break;
349 
350  /* When the superbitrf is initializing binding */
352  /* Switch the different states */
353  switch (superbitrf.state) {
354  case 0:
355  // Try to write the binding config
356  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_bind_config, 5);
357  superbitrf.state++;
358  break;
359  case 1:
360  // Set the data code and channel
361  memcpy(data_code, pn_codes[0][8], 8);
362  memcpy(data_code + 8, pn_bind, 8);
363  cyrf6936_write_chan_sop_data_crc(&superbitrf.cyrf6936, 1, pn_codes[0][0], data_code, 0x0000);
364 
366  break;
367  default:
368  // Should not happen
369  superbitrf.state = 0;
370  break;
371  }
372  break;
373 
374  /* When the superbitrf is initializing transfer */
376  // Generate the DSMX channels
378 
379  // Try to write the transfer config
380  if (cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_transfer_config, 4)) {
385  superbitrf.state = 1;
386  }
387  break;
388 
389  /* When the superbitrf is in binding mode */
390  case SUPERBITRF_BINDING:
391 
392 #ifdef RADIO_CONTROL_LED
393  slowLedCpt++;
394  if (slowLedCpt > 100000) {
395 
396  LED_TOGGLE(RADIO_CONTROL_LED);
397  slowLedCpt = 0;
398  }
399 #endif
400 
401  /* Switch the different states */
402  switch (superbitrf.state) {
403  case 0:
404  // When there is a timeout
406  superbitrf.state++;
407  }
408  break;
409  case 1:
410  // Abort the receive
411  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_abort_receive, 2);
412 
413  superbitrf.state++;
414  break;
415  case 2:
416  // Switch channel
417  superbitrf.channel = (superbitrf.channel + 2) % 0x4F; //TODO fix define
419 
420  superbitrf.state += 2; // Already aborted
421  break;
422  case 3:
423  // Abort the receive
424  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_abort_receive, 2);
425 
426  superbitrf.state++;
427  break;
428  case 4:
429  // Start receiving
430  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
431  superbitrf.state++;
432  break;
433  default:
434  // Check if need to go to transfer
435  if (start_transfer) {
436  // Initialize the binding values
437  // set values based on mfg id
438  // if bind_mfg_id32 is loaded from persistent settings use that,
441  }
442 #ifdef RADIO_TRANSMITTER_ID
443  // otherwise load airframe file value
444  else {
445  PRINT_CONFIG_VAR(RADIO_TRANSMITTER_ID)
446  superbitrf_set_mfg_id(RADIO_TRANSMITTER_ID);
447  }
448 #endif
449 #ifdef RADIO_TRANSMITTER_CHAN
450  PRINT_CONFIG_VAR(RADIO_TRANSMITTER_CHAN)
451  if (superbitrf.num_channels == 0) {
452  superbitrf.num_channels = RADIO_TRANSMITTER_CHAN;
453  }
454 #endif
455  if (superbitrf.protocol == 0) {
457  }
458 #ifdef RADIO_TRANSMITTER_PROTOCOL
459  else {
460  PRINT_CONFIG_VAR(RADIO_TRANSMITTER_PROTOCOL)
461  superbitrf_set_protocol(RADIO_TRANSMITTER_PROTOCOL);
462  }
463 #endif
464 
465  // Start transfer
466  superbitrf.state = 0;
468  break;
469  }
470 
471  // Set the timer
473  superbitrf.state = 0;
474  break;
475  }
476  break;
477 
478  /* When the receiver is synchronizing with the transmitter */
481 
482 #ifdef RADIO_CONTROL_LED
483  slowLedCpt++;
484  if (slowLedCpt > 5000) {
485 
486  LED_TOGGLE(RADIO_CONTROL_LED);
487  slowLedCpt = 0;
488  }
489 #endif
490 
491  /* Switch the different states */
492  switch (superbitrf.state) {
493  case 0:
494  // When there is a timeout
496  superbitrf.state++;
497  }
498  break;
499  case 1:
500  // Abort the receive
501  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_abort_receive, 2);
502  superbitrf.state++;
503  break;
504  case 2:
505  // Switch channel, sop code, data code and crc
508  pn_row = (IS_DSM2(superbitrf.protocol)
511 
513  pn_codes[pn_row][superbitrf.sop_col],
514  pn_codes[pn_row][superbitrf.data_col],
516  superbitrf.state++;
517  break;
518  case 3:
519  // Create a new packet when no packet loss
520  if (!superbitrf.packet_loss) {
523  tx_packet[0] = ~superbitrf.bind_mfg_id[2];
524  tx_packet[1] = (~superbitrf.bind_mfg_id[3]) + 1 + superbitrf.packet_loss_bit;
525  } else {
526  tx_packet[0] = superbitrf.bind_mfg_id[2];
527  tx_packet[1] = (superbitrf.bind_mfg_id[3]) + 1 + superbitrf.packet_loss_bit;
528  }
529 
532  if (packet_size > 14) {
533  packet_size = 14;
534  }
535 
536  for (i = 0; i < packet_size; i++) {
538  }
539  }
540 
541  // Send a packet
542  cyrf6936_send(&superbitrf.cyrf6936, tx_packet, packet_size + 2);
543 
544  // Update the packet extraction
545  if (!superbitrf.packet_loss) {
547  }
548 
549  superbitrf.state++;
550  break;
551  case 4:
552  //TODO: check timeout? (Waiting for send)
553  break;
554  case 5:
555  superbitrf.state = 7;
556  break;
557  // Start receiving
558  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
560  superbitrf.state++;
561  break;
562  case 6:
563  // Wait for telemetry data
565  superbitrf.state++;
566  }
567  break;
568  case 7:
569  // When DSMX we don't need to switch
571  superbitrf.state++;
573  break;
574  }
575 
576  // Switch channel, sop code, data code and crc
577  superbitrf.channel = (superbitrf.channel + 2) % 0x4F; //TODO fix define
579  pn_row = (IS_DSM2(superbitrf.protocol)
581 
583  pn_codes[pn_row][superbitrf.sop_col],
584  pn_codes[pn_row][superbitrf.data_col],
586 
587  superbitrf.state++;
588  break;
589  case 8:
590  // Start receiving
591  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
592  superbitrf.state++;
593  break;
594  default:
595  // Set the timer
597  superbitrf.state = 0;
598  break;
599  }
600  break;
601 
602  /* Normal transfer mode */
603  case SUPERBITRF_TRANSFER:
604 
605 #ifdef RADIO_CONTROL_LED
606  LED_ON(RADIO_CONTROL_LED);
607 #endif
608 
609  /* Switch the different states */
610  switch (superbitrf.state) {
611  case 0:
612  // Fixing timer overflow
615  }
616 
617  // When there is a timeout
621  superbitrf.state++;
622  }
623 
624  // We really lost the communication
625  if (superbitrf.timeouts > 100) {
626  superbitrf.state = 0;
629  }
630  break;
631  case 1:
632  // Abort the receive
633  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_abort_receive, 2);
634  superbitrf.state++;
635 
636  // Set the timer
640  } else {
642  }
643 
644  // Only send on channel 2
646  superbitrf.state = 8;
647  }
648  break;
649  case 2:
650  // Wait before sending (FIXME??)
651  superbitrf.state++;
652  break;
653  case 3:
654  // Create a new packet when no packet loss
655  if (!superbitrf.packet_loss) {
658  tx_packet[0] = ~superbitrf.bind_mfg_id[2];
659  tx_packet[1] = ((~superbitrf.bind_mfg_id[3]) + 1 + superbitrf.packet_loss_bit) % 0xFF;
660  } else {
661  tx_packet[0] = superbitrf.bind_mfg_id[2];
662  tx_packet[1] = ((superbitrf.bind_mfg_id[3]) + 1 + superbitrf.packet_loss_bit) % 0xFF;
663  }
664 
667  if (packet_size > 14) {
668  packet_size = 14;
669  }
670 
671  for (i = 0; i < packet_size; i++) {
673  }
674  }
675 
676  // Send a packet
677  cyrf6936_send(&superbitrf.cyrf6936, tx_packet, packet_size + 2);
678 
679  // Update the packet extraction
680  if (!superbitrf.packet_loss) {
682  }
683 
684  superbitrf.state++;
685  break;
686  case 4:
687  //TODO: check timeout? (Waiting for send)
688  break;
689  case 5:
690  // Start receiving
691  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
692  superbitrf.state++;
693  break;
694  case 6:
695  // Fixing timer overflow
698  }
699 
700  // Waiting for data receive
702  superbitrf.state++;
703  }
704  break;
705  case 7:
706  // Abort the receive
707  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_abort_receive, 2);
708  superbitrf.state++;
709  break;
710  case 8:
711  // Switch channel, sop code, data code and crc
716  pn_row = (IS_DSM2(superbitrf.protocol)
718 
720  pn_codes[pn_row][superbitrf.sop_col],
721  pn_codes[pn_row][superbitrf.data_col],
723 
724  superbitrf.state++;
725  break;
726  case 9:
727  // Start receiving
728  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
729  superbitrf.state++;
730  break;
731  default:
732  // Set the timer
735  } else {
737  }
740  } else {
742  }
743 
744  superbitrf.state = 0;
745  break;
746  }
747  break;
748 
749  /* Should not come here */
750  default:
751  break;
752  }
753 }
754 
758 static inline void superbitrf_receive_packet_cb(bool_t error, uint8_t status, uint8_t packet[])
759 {
760  int i;
761  uint16_t sum;
762 
763  /* Switch on the status of the superbitRF */
764  switch (superbitrf.status) {
765 
766  /* When we are in binding mode */
767  case SUPERBITRF_BINDING:
768  // Check if the MFG id is exactly the same
769  if (packet[0] != packet[4] || packet[1] != packet[5] || packet[2] != packet[6] || packet[3] != packet[7]) {
770  // Start receiving without changing channel
771  superbitrf.state = 3;
772  break;
773  }
774 
775  // Calculate the first sum
776  sum = 384 - 0x10;
777  for (i = 0; i < 8; i++) {
778  sum += packet[i];
779  }
780 
781  // Check the first sum
782  if (packet[8] != sum >> 8 || packet[9] != (sum & 0xFF)) {
783  // Start receiving without changing channel
784  superbitrf.state = 3;
785  break;
786  }
787 
788  // Calculate second sum
789  for (i = 8; i < 14; i++) {
790  sum += packet[i];
791  }
792 
793  // Check the second sum
794  if (packet[14] != sum >> 8 || packet[15] != (sum & 0xFF)) {
795  // Start receiving without changing channel
796  superbitrf.state = 3;
797  break;
798  }
799 
800  // Update the mfg id, number of channels and protocol
801  uint32_t mfg_id = ((~packet[3] & 0xFF) << 24 | (~packet[2] & 0xFF) << 16 |
802  (~packet[1] & 0xFF) << 8 | (~packet[0] & 0xFF));
803  superbitrf_set_mfg_id(mfg_id);
804 
805  superbitrf.num_channels = packet[11];
806  superbitrf_set_protocol(packet[12]);
807 
808  // Store all the persistent settings.
809  // In case we have the superbit setting file loaded and persistent settings
810  // enabled in the airframe file this will store our binding information and
811  // survive a reboot.
813 
814  // Update the status of the receiver
815  superbitrf.state = 0;
817  break;
818 
819  /* When we receive a packet during syncing first channel A */
821  // Check the MFG id
822  if (error && !(status & CYRF_BAD_CRC)) {
823  // Start receiving TODO: Fix nicely
824  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
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))) {
830  // Start receiving TODO: Fix nicely
831  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
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))) {
837  // Start receiving TODO: Fix nicely
838  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
839  break;
840  }
841 
842  // If the CRC is wrong invert
843  if (error && (status & CYRF_BAD_CRC)) {
845  }
846 
847  // When we receive a data packet
848  if (packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) && packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF)) {
850 
851  // Check if it is a data loss packet
852  if (packet[1] != (~superbitrf.bind_mfg_id[3] + 1 + superbitrf.packet_loss_bit) % 0xFF
853  && packet[1] != (superbitrf.bind_mfg_id[3] + 1 + superbitrf.packet_loss_bit) % 0xFF) {
855  } else {
857  }
858 
859  // When it is a data packet, parse the packet if not busy already
861  for (i = 2; i < superbitrf.cyrf6936.rx_count; i++) {
862  parse_pprz(&superbitrf.rx_transport, packet[i]);
863 
864  // When we have a full message
868  }
869  }
870  }
871  break;
872  }
873 
877 
878  superbitrf.state = 1;
880  } else {
881  superbitrf.timeouts = 0;
882  superbitrf.state = 1;
884  }
885  break;
886 
887  /* When we receive a packet during syncing second channel B */
889  // Check the MFG id
890  if (error && !(status & CYRF_BAD_CRC)) {
891  // Start receiving TODO: Fix nicely
892  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
893  break;
894  }
896  (packet[0] != (~superbitrf.bind_mfg_id[2] & 0xFF) || (packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) &&
897  packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) + 1 && packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) + 2))) {
898  // Start receiving TODO: Fix nicely
899  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
900  break;
901  }
903  (packet[0] != (superbitrf.bind_mfg_id[2] & 0xFF) || (packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF) &&
904  packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF) + 1 && packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF) + 2))) {
905  // Start receiving TODO: Fix nicely
906  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
907  break;
908  }
909 
910  // If the CRC is wrong invert
911  if (error && (status & CYRF_BAD_CRC)) {
913  }
914 
915  // When we receive a data packet
916  if (packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) && packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF)) {
918 
919  // When it is a data packet, parse the packet if not busy already
920  if (!dl_msg_available) {
921  for (i = 2; i < superbitrf.cyrf6936.rx_count; i++) {
922  parse_pprz(&superbitrf.rx_transport, packet[i]);
923 
924  // When we have a full message
928  }
929  }
930  }
931  break;
932  }
933 
934  // Set the channel
938  } else {
941  }
942 
943  // When the channels aren't the same go to transfer mode
944  if (superbitrf.channels[1] != superbitrf.channels[0]) {
945  superbitrf.state = 1;
947  superbitrf.timeouts = 0;
948  }
949  break;
950 
951  /* When we receive a packet during transfer */
952  case SUPERBITRF_TRANSFER:
953  // Check the MFG id
954  if (error) {
955  // Start receiving TODO: Fix nicely
956  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
957  break;
958  }
960  (packet[0] != (~superbitrf.bind_mfg_id[2] & 0xFF) || (packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) &&
961  packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) + 1 && packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) + 2))) {
962  // Start receiving TODO: Fix nicely
963  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
964  break;
965  }
967  (packet[0] != (superbitrf.bind_mfg_id[2] & 0xFF) || (packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF) &&
968  packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF) + 1 && packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF) + 2))) {
969  // Start receiving TODO: Fix nicely
970  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
971  break;
972  }
973 
974  // Check if it is a RC packet
975  if (packet[1] == (~superbitrf.bind_mfg_id[3] & 0xFF) || packet[1] == (superbitrf.bind_mfg_id[3] & 0xFF)) {
977 
978  // Parse the packet
981 
982  // Calculate the timing (seperately for the channel switches)
985  } else {
987  }
988 
989  // Go to next receive
990  superbitrf.state = 1;
991  superbitrf.timeouts = 0;
992  } else {
994 
995  // Check if it is a data loss packet
996  if (packet[1] != (~superbitrf.bind_mfg_id[3] + 1 + superbitrf.packet_loss_bit)
997  && packet[1] != (superbitrf.bind_mfg_id[3] + 1 + superbitrf.packet_loss_bit)) {
999  } else {
1001  }
1002 
1004 
1005  // When it is a data packet, parse the packet if not busy already
1007  for (i = 2; i < superbitrf.cyrf6936.rx_count; i++) {
1008  parse_pprz(&superbitrf.rx_transport, packet[i]);
1009 
1010  // When we have a full message
1014  }
1015  }
1016  }
1017 
1018  // Update the state
1019  superbitrf.state = 7;
1020  }
1021  break;
1022 
1023  /* Should not come here */
1024  default:
1025  break;
1026  }
1027 }
1028 
1029 static inline void superbitrf_send_packet_cb(bool_t error __attribute__((unused)))
1030 {
1031  /* Switch on the status of the superbitRF */
1032  switch (superbitrf.status) {
1033 
1034  /* When we are synchronizing */
1035  case SUPERBITRF_SYNCING_A:
1036  case SUPERBITRF_SYNCING_B:
1037  // When we successfully or unsuccessfully send a data packet
1038  if (superbitrf.state == 4) {
1039  superbitrf.state++;
1040  }
1041  break;
1042 
1043  /* When we are in transfer mode */
1044  case SUPERBITRF_TRANSFER:
1045  // When we successfully or unsuccessfully send a packet
1046  if (superbitrf.state == 4) {
1047  superbitrf.state++;
1048  }
1049  break;
1050 
1051  /* Should not come here */
1052  default:
1053  break;
1054  }
1055 }
1056 
1060 static inline void superbitrf_radio_to_channels(uint8_t *data, uint8_t nb_channels, bool_t is_11bit, int16_t *channels)
1061 {
1062  int i;
1063  uint8_t bit_shift = (is_11bit) ? 11 : 10;
1064  int16_t value_max = (is_11bit) ? 0x07FF : 0x03FF;
1065 
1066  for (i = 0; i < 7; i++) {
1067  const int16_t tmp = ((data[2 * i] << 8) + data[2 * i + 1]) & 0x7FFF;
1068  const uint8_t chan = (tmp >> bit_shift) & 0x0F;
1069  const int16_t val = (tmp & value_max);
1070 
1071  if (chan < nb_channels) {
1072  channels[chan] = val;
1073 
1074  // Scale the channel
1075  if (is_11bit) {
1076  channels[chan] -= 0x400;
1077  channels[chan] *= MAX_PPRZ / 0x2AC;
1078  } else {
1079  channels[chan] -= 0x200;
1080  channels[chan] *= MAX_PPRZ / 0x156;
1081  }
1082  }
1083  }
1084 }
1085 
1089 static inline void superbitrf_gen_dsmx_channels(void)
1090 {
1091  // Calculate the DSMX channels
1092  int idx = 0;
1093  uint32_t id = ~((superbitrf.bind_mfg_id[0] << 24) | (superbitrf.bind_mfg_id[1] << 16) |
1094  (superbitrf.bind_mfg_id[2] << 8) | (superbitrf.bind_mfg_id[3] << 0));
1095  uint32_t id_tmp = id;
1096 
1097  // While not all channels are set
1098  while (idx < 23) {
1099  int i;
1100  int count_3_27 = 0, count_28_51 = 0, count_52_76 = 0;
1101 
1102  id_tmp = id_tmp * 0x0019660D + 0x3C6EF35F; // Randomization
1103  uint8_t next_ch = ((id_tmp >> 8) % 0x49) + 3; // Use least-significant byte and must be larger than 3
1104  if (((next_ch ^ id) & 0x01) == 0) {
1105  continue;
1106  }
1107 
1108  // Go trough all already set channels
1109  for (i = 0; i < idx; i++) {
1110  // Channel is already used
1111  if (superbitrf.channels[i] == next_ch) {
1112  break;
1113  }
1114 
1115  // Count the channel groups
1116  if (superbitrf.channels[i] <= 27) {
1117  count_3_27++;
1118  } else if (superbitrf.channels[i] <= 51) {
1119  count_28_51++;
1120  } else {
1121  count_52_76++;
1122  }
1123  }
1124 
1125  // When channel is already used continue
1126  if (i != idx) {
1127  continue;
1128  }
1129 
1130  // Set the channel when channel groups aren't full
1131  if ((next_ch < 28 && count_3_27 < 8) // Channels 3-27: max 8
1132  || (next_ch >= 28 && next_ch < 52 && count_28_51 < 7) // Channels 28-52: max 7
1133  || (next_ch >= 52 && count_52_76 < 8)) { // Channels 52-76: max 8
1134  superbitrf.channels[idx++] = next_ch;
1135  }
1136  }
1137 }
1138 
1139 
1140 
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:432
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:341
unsigned short uint16_t
Definition: types.h:16
status
Definition: anemotaxis.c:10
#define CYRF_RXC_IRQEN
volatile bool_t msg_received
message received flag
Definition: transport.h:42
Generic transmission transport header.
Definition: transport.h:89
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
static void parse_pprz(struct pprz_transport *t, uint8_t c)
#define SPEKTRUM_BIND_PIN
Definition: apogee_1.0.h:417
#define CYRF_FRC_RXDR
#define CYRF_SOP_LEN
#define FALSE
Definition: std.h:5
static uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.h:39
bool_t dl_msg_available
Definition: main_demo5.c:61
uint8_t rx_irq_status
The last receive interrupt status.
Definition: cyrf6936.h:62
#define LED_TOGGLE(i)
Definition: led_hw.h:44
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:330
bool_t has_irq
When the CYRF6936 is done reading the irq.
Definition: cyrf6936.h:59
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:373
#define CYRF_DIS_TXCRC
enum Cyrf6936Status status
The status of the CYRF6936 chip.
Definition: cyrf6936.h:51
#define TRUE
Definition: std.h:4
#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
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 CYRF_RXE_IRQ
#define SPEKTRUM_BIND_PIN_PORT
Definition: apogee_1.0.h:418
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:69
uint8_t rx_count
The length of the received packet.
Definition: cyrf6936.h:64
#define LED_ON(i)
Definition: led_hw.h:42
#define CYRF_RXE_IRQEN
unsigned char uint8_t
Definition: types.h:14
#define CYRF_RXOW_IRQ
Persistent settings interface.
#define CYRF_SOP_EN
#define CYRF_FAST_TURN_EN
struct transport_rx trans_rx
uint16_t gpio_get(uint32_t gpioport, uint16_t gpios)
Read a gpio value.
Definition: gpio_ardrone.c:120
void gpio_setup_input(uint32_t port, uint16_t gpios)
Setup one or more pins of the given GPIO port as inputs.
Definition: gpio_ardrone.c:86
arch independent LED (Light Emitting Diodes) API
#define CYRF_RXF
Definition: cyrf6936_regs.h:84
static float p[2][2]
#define CYRF_RXC_IRQ
uint8_t tx_irq_status
The last send interrupt status.
Definition: cyrf6936.h:61
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:412
#define MAX_PPRZ
Definition: paparazzi.h:8
static void pprz_parse_payload(struct pprz_transport *t)
#define CYRF_TXC_IRQ
#define CYRF_TXE_IRQ
#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