Paparazzi UAS  v5.12_stable-4-g9b43e9b
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 pprz transport structure */
73 struct pprz_transport pprz_srf_tp;
74 
75 /* The internal functions */
76 static inline void superbitrf_radio_to_channels(uint8_t *data, uint8_t nb_channels, bool is_11bit, int16_t *channels);
77 static inline void superbitrf_receive_packet_cb(bool error, uint8_t status, uint8_t packet[]);
78 static inline void superbitrf_send_packet_cb(bool error);
79 static inline void superbitrf_gen_dsmx_channels(void);
80 
81 /* The startup configuration for the cyrf6936 */
82 static const uint8_t cyrf_stratup_config[][2] = {
83  {CYRF_MODE_OVERRIDE, CYRF_RST}, // Reset the device
84  {CYRF_CLK_EN, CYRF_RXF}, // Enable the clock
85  {CYRF_AUTO_CAL_TIME, 0x3C}, // From manual, needed for initialization
86  {CYRF_AUTO_CAL_OFFSET, 0x14}, // From manual, needed for initialization
87  {CYRF_RX_CFG, CYRF_LNA | CYRF_FAST_TURN_EN}, // Enable low noise amplifier and fast turning
88  {CYRF_TX_OFFSET_LSB, 0x55}, // From manual, typical configuration
89  {CYRF_TX_OFFSET_MSB, 0x05}, // From manual, typical configuration
90  {CYRF_XACT_CFG, CYRF_MODE_SYNTH_RX | CYRF_FRC_END}, // Force in Synth RX mode
91  {CYRF_TX_CFG, CYRF_DATA_CODE_LENGTH | CYRF_DATA_MODE_SDR | CYRF_PA_4}, // Enable 64 chip codes, SDR mode and amplifier +4dBm
92  {CYRF_DATA64_THOLD, 0x0E}, // From manual, typical configuration
93  {CYRF_XACT_CFG, CYRF_MODE_SYNTH_RX}, // Set in Synth RX mode (again, really needed?)
94 };
95 /* The binding configuration for the cyrf6936 */
96 static const uint8_t cyrf_bind_config[][2] = {
97  {CYRF_TX_CFG, CYRF_DATA_CODE_LENGTH | CYRF_DATA_MODE_SDR | CYRF_PA_4}, // Enable 64 chip codes, SDR mode and amplifier +4dBm
98  {CYRF_FRAMING_CFG, CYRF_SOP_LEN | 0xE}, // Set SOP CODE to 64 chips and SOP Correlator Threshold to 0xE
99  {CYRF_RX_OVERRIDE, CYRF_FRC_RXDR | CYRF_DIS_RXCRC}, // Force receive data rate and disable receive CRC checker
100  {CYRF_EOP_CTRL, 0x02}, // Only enable EOP symbol count of 2
101  {CYRF_TX_OVERRIDE, CYRF_DIS_TXCRC}, // Disable transmit CRC generate
102 };
103 /* The transfer configuration for the cyrf6936 */
104 static const uint8_t cyrf_transfer_config[][2] = {
105  {CYRF_TX_CFG, CYRF_DATA_CODE_LENGTH | CYRF_DATA_MODE_8DR | CYRF_PA_4}, // Enable 64 chip codes, 8DR mode and amplifier +4dBm
106  {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
107  {CYRF_TX_OVERRIDE, 0x00}, // Reset TX overrides
108  {CYRF_RX_OVERRIDE, 0x00}, // Reset RX overrides
109 };
110 /* Abort the receive of the cyrf6936 */
113  {CYRF_RX_ABORT, 0x00}
114 };
115 /* Start the receive of the cyrf6936 */
117  {CYRF_RX_IRQ_STATUS, CYRF_RXOW_IRQ}, // Clear the IRQ
118  {CYRF_RX_CTRL, CYRF_RX_GO | CYRF_RXC_IRQEN | CYRF_RXE_IRQEN} // Start receiving and set the IRQ
119 };
120 
121 /* The PN codes used for DSM2 and DSMX channel hopping */
122 static const uint8_t pn_codes[5][9][8] = {
123  { /* Row 0 */
124  /* Col 0 */ {0x03, 0xBC, 0x6E, 0x8A, 0xEF, 0xBD, 0xFE, 0xF8},
125  /* Col 1 */ {0x88, 0x17, 0x13, 0x3B, 0x2D, 0xBF, 0x06, 0xD6},
126  /* Col 2 */ {0xF1, 0x94, 0x30, 0x21, 0xA1, 0x1C, 0x88, 0xA9},
127  /* Col 3 */ {0xD0, 0xD2, 0x8E, 0xBC, 0x82, 0x2F, 0xE3, 0xB4},
128  /* Col 4 */ {0x8C, 0xFA, 0x47, 0x9B, 0x83, 0xA5, 0x66, 0xD0},
129  /* Col 5 */ {0x07, 0xBD, 0x9F, 0x26, 0xC8, 0x31, 0x0F, 0xB8},
130  /* Col 6 */ {0xEF, 0x03, 0x95, 0x89, 0xB4, 0x71, 0x61, 0x9D},
131  /* Col 7 */ {0x40, 0xBA, 0x97, 0xD5, 0x86, 0x4F, 0xCC, 0xD1},
132  /* Col 8 */ {0xD7, 0xA1, 0x54, 0xB1, 0x5E, 0x89, 0xAE, 0x86}
133  },
134  { /* Row 1 */
135  /* Col 0 */ {0x83, 0xF7, 0xA8, 0x2D, 0x7A, 0x44, 0x64, 0xD3},
136  /* Col 1 */ {0x3F, 0x2C, 0x4E, 0xAA, 0x71, 0x48, 0x7A, 0xC9},
137  /* Col 2 */ {0x17, 0xFF, 0x9E, 0x21, 0x36, 0x90, 0xC7, 0x82},
138  /* Col 3 */ {0xBC, 0x5D, 0x9A, 0x5B, 0xEE, 0x7F, 0x42, 0xEB},
139  /* Col 4 */ {0x24, 0xF5, 0xDD, 0xF8, 0x7A, 0x77, 0x74, 0xE7},
140  /* Col 5 */ {0x3D, 0x70, 0x7C, 0x94, 0xDC, 0x84, 0xAD, 0x95},
141  /* Col 6 */ {0x1E, 0x6A, 0xF0, 0x37, 0x52, 0x7B, 0x11, 0xD4},
142  /* Col 7 */ {0x62, 0xF5, 0x2B, 0xAA, 0xFC, 0x33, 0xBF, 0xAF},
143  /* Col 8 */ {0x40, 0x56, 0x32, 0xD9, 0x0F, 0xD9, 0x5D, 0x97}
144  },
145  { /* Row 2 */
146  /* Col 0 */ {0x40, 0x56, 0x32, 0xD9, 0x0F, 0xD9, 0x5D, 0x97},
147  /* Col 1 */ {0x8E, 0x4A, 0xD0, 0xA9, 0xA7, 0xFF, 0x20, 0xCA},
148  /* Col 2 */ {0x4C, 0x97, 0x9D, 0xBF, 0xB8, 0x3D, 0xB5, 0xBE},
149  /* Col 3 */ {0x0C, 0x5D, 0x24, 0x30, 0x9F, 0xCA, 0x6D, 0xBD},
150  /* Col 4 */ {0x50, 0x14, 0x33, 0xDE, 0xF1, 0x78, 0x95, 0xAD},
151  /* Col 5 */ {0x0C, 0x3C, 0xFA, 0xF9, 0xF0, 0xF2, 0x10, 0xC9},
152  /* Col 6 */ {0xF4, 0xDA, 0x06, 0xDB, 0xBF, 0x4E, 0x6F, 0xB3},
153  /* Col 7 */ {0x9E, 0x08, 0xD1, 0xAE, 0x59, 0x5E, 0xE8, 0xF0},
154  /* Col 8 */ {0xC0, 0x90, 0x8F, 0xBB, 0x7C, 0x8E, 0x2B, 0x8E}
155  },
156  { /* Row 3 */
157  /* Col 0 */ {0xC0, 0x90, 0x8F, 0xBB, 0x7C, 0x8E, 0x2B, 0x8E},
158  /* Col 1 */ {0x80, 0x69, 0x26, 0x80, 0x08, 0xF8, 0x49, 0xE7},
159  /* Col 2 */ {0x7D, 0x2D, 0x49, 0x54, 0xD0, 0x80, 0x40, 0xC1},
160  /* Col 3 */ {0xB6, 0xF2, 0xE6, 0x1B, 0x80, 0x5A, 0x36, 0xB4},
161  /* Col 4 */ {0x42, 0xAE, 0x9C, 0x1C, 0xDA, 0x67, 0x05, 0xF6},
162  /* Col 5 */ {0x9B, 0x75, 0xF7, 0xE0, 0x14, 0x8D, 0xB5, 0x80},
163  /* Col 6 */ {0xBF, 0x54, 0x98, 0xB9, 0xB7, 0x30, 0x5A, 0x88},
164  /* Col 7 */ {0x35, 0xD1, 0xFC, 0x97, 0x23, 0xD4, 0xC9, 0x88},
165  /* Col 8 */ {0x88, 0xE1, 0xD6, 0x31, 0x26, 0x5F, 0xBD, 0x40}
166  },
167  { /* Row 4 */
168  /* Col 0 */ {0xE1, 0xD6, 0x31, 0x26, 0x5F, 0xBD, 0x40, 0x93},
169  /* Col 1 */ {0xDC, 0x68, 0x08, 0x99, 0x97, 0xAE, 0xAF, 0x8C},
170  /* Col 2 */ {0xC3, 0x0E, 0x01, 0x16, 0x0E, 0x32, 0x06, 0xBA},
171  /* Col 3 */ {0xE0, 0x83, 0x01, 0xFA, 0xAB, 0x3E, 0x8F, 0xAC},
172  /* Col 4 */ {0x5C, 0xD5, 0x9C, 0xB8, 0x46, 0x9C, 0x7D, 0x84},
173  /* Col 5 */ {0xF1, 0xC6, 0xFE, 0x5C, 0x9D, 0xA5, 0x4F, 0xB7},
174  /* Col 6 */ {0x58, 0xB5, 0xB3, 0xDD, 0x0E, 0x28, 0xF1, 0xB0},
175  /* Col 7 */ {0x5F, 0x30, 0x3B, 0x56, 0x96, 0x45, 0xF4, 0xA1},
176  /* Col 8 */ {0x03, 0xBC, 0x6E, 0x8A, 0xEF, 0xBD, 0xFE, 0xF8}
177  },
178 };
179 static const uint8_t pn_bind[] = { 0x98, 0x88, 0x1B, 0xE4, 0x30, 0x79, 0x03, 0x84 };
180 
181 #if PERIODIC_TELEMETRY
183 
184 static void send_superbit(struct transport_tx *trans, struct link_device *dev)
185 {
187  uint8_t cyrf6936_status = superbitrf.cyrf6936.status;
188  pprz_msg_send_SUPERBITRF(trans, dev, AC_ID,
189  &status,
190  &cyrf6936_status,
201  6,
203 }
204 #endif
205 
206 // Functions for the generic device API
207 static bool superbitrf_check_free_space(struct SuperbitRF *p, long *fd __attribute__((unused)), uint16_t len)
208 {
209  int16_t space = p->tx_extract_idx - p->tx_insert_idx;
210  if (space <= 0) {
211  space += SUPERBITRF_TX_BUFFER_SIZE;
212  }
213  return (uint16_t)(space - 1) >= len;
214 }
215 
216 static void superbitrf_transmit(struct SuperbitRF *p, long fd __attribute__((unused)), uint8_t byte)
217 {
218  p->tx_buffer[p->tx_insert_idx] = byte;
220 }
221 
222 static void superbitrf_transmit_buffer(struct SuperbitRF *p, long fd, uint8_t *data, uint16_t len)
223 {
224  int i;
225  for (i = 0; i < len; i++) {
226  superbitrf_transmit(p, fd, data[i]);
227  }
228 }
229 
230 static void superbitrf_send(struct SuperbitRF *p __attribute__((unused)), long fd __attribute__((unused))) { }
231 
232 static int null_function(struct SuperbitRF *p __attribute__((unused))) { return 0; }
233 
237 void superbitrf_init(void)
238 {
239  // Set the status to uninitialized and set the timer to 0
241  superbitrf.state = 0;
242  superbitrf.timer = 0;
245 
246  // Setup the transmit buffer
249 
252  superbitrf.protocol = 0;
253 
254  // Configure generic device
255  superbitrf.device.periph = (void *)(&superbitrf);
256  superbitrf.device.check_free_space = (check_free_space_t) superbitrf_check_free_space;
257  superbitrf.device.put_byte = (put_byte_t) superbitrf_transmit;
258  superbitrf.device.put_buffer = (put_buffer_t) superbitrf_transmit_buffer;
259  superbitrf.device.send_message = (send_message_t) superbitrf_send;
260  superbitrf.device.char_available = (char_available_t) null_function; // not needed
261  superbitrf.device.get_byte = (get_byte_t) null_function; // not needed
262 
263  // Initialize the binding pin
265 
266  // Initialize the IRQ/DRDY pin
268 
269  // Initialize the cyrf6936 chip
271 
272 #if PERIODIC_TELEMETRY
274 #endif
275 }
276 
281 {
282  // Init pprz transport
283  pprz_transport_init(&pprz_srf_tp);
284 }
285 
290 {
292 }
293 
295 {
298  superbitrf.bind_mfg_id[1] = (superbitrf.bind_mfg_id32 >> 8 & 0xFF);
299  superbitrf.bind_mfg_id[2] = (superbitrf.bind_mfg_id32 >> 16 & 0xFF);
300  superbitrf.bind_mfg_id[3] = (superbitrf.bind_mfg_id32 >> 24 & 0xFF);
301 
302  // Calculate some values based on the bind MFG id
306 }
307 
309 {
311  superbitrf.resolution = (superbitrf.protocol & 0x10) >> 4;
312 }
313 
318 {
319  uint8_t i, pn_row, data_code[16];
320  static uint8_t packet_size, tx_packet[16];
321  static bool start_transfer = true;
322 
323 #ifdef RADIO_CONTROL_LED
324  static uint32_t slowLedCpt = 0;
325 #endif
326 
327  // Check if the cyrf6936 isn't busy and the uperbitrf is initialized
329  return;
330  }
331 
332  // When the device is initialized handle the IRQ
334  // First handle the IRQ
336  // Receive the packet
339  }
340 
341  /* Check if it is a valid receive */
343  // Handle the packet received
347 
348  // Reset the packet receiving
349  superbitrf.cyrf6936.has_irq = false;
350  }
351 
352  /* Check if it has a valid send */
354  // Handle the send packet
357 
358  // Reset the packet receiving
359  superbitrf.cyrf6936.has_irq = false;
360  }
361  }
362 
363  // Check the status of the superbitrf
364  switch (superbitrf.status) {
365 
366  /* When the superbitrf isn't initialized */
367  case SUPERBITRF_UNINIT:
368  // Try to write the startup config
370  // Check if need to go to bind or transfer
372  start_transfer = false;
373  }
374 
376  }
377  break;
378 
379  /* When the superbitrf is initializing binding */
381  /* Switch the different states */
382  switch (superbitrf.state) {
383  case 0:
384  // Try to write the binding config
385  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_bind_config, 5);
386  superbitrf.state++;
387  break;
388  case 1:
389  // Set the data code and channel
390  memcpy(data_code, pn_codes[0][8], 8);
391  memcpy(data_code + 8, pn_bind, 8);
392  cyrf6936_write_chan_sop_data_crc(&superbitrf.cyrf6936, 1, pn_codes[0][0], data_code, 0x0000);
393 
395  break;
396  default:
397  // Should not happen
398  superbitrf.state = 0;
399  break;
400  }
401  break;
402 
403  /* When the superbitrf is initializing transfer */
405  // Generate the DSMX channels
407 
408  // Try to write the transfer config
409  if (cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_transfer_config, 4)) {
411  superbitrf.packet_loss = false;
414  superbitrf.state = 1;
415  }
416  break;
417 
418  /* When the superbitrf is in binding mode */
419  case SUPERBITRF_BINDING:
420 
421 #ifdef RADIO_CONTROL_LED
422  slowLedCpt++;
423  if (slowLedCpt > 100000) {
424 
425  LED_TOGGLE(RADIO_CONTROL_LED);
426  slowLedCpt = 0;
427  }
428 #endif
429 
430  /* Switch the different states */
431  switch (superbitrf.state) {
432  case 0:
433  // When there is a timeout
435  superbitrf.state++;
436  }
437  break;
438  case 1:
439  // Abort the receive
440  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_abort_receive, 2);
441 
442  superbitrf.state++;
443  break;
444  case 2:
445  // Switch channel
446  superbitrf.channel = (superbitrf.channel + 2) % 0x4F; //TODO fix define
448 
449  superbitrf.state += 2; // Already aborted
450  break;
451  case 3:
452  // Abort the receive
453  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_abort_receive, 2);
454 
455  superbitrf.state++;
456  break;
457  case 4:
458  // Start receiving
459  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
460  superbitrf.state++;
461  break;
462  default:
463  // Check if need to go to transfer
464  if (start_transfer) {
465  // Initialize the binding values
466  // set values based on mfg id
467  // if bind_mfg_id32 is loaded from persistent settings use that,
470  }
471 #ifdef RADIO_TRANSMITTER_ID
472  // otherwise load airframe file value
473  else {
474  PRINT_CONFIG_VAR(RADIO_TRANSMITTER_ID)
475  superbitrf_set_mfg_id(RADIO_TRANSMITTER_ID);
476  }
477 #endif
478 #ifdef RADIO_TRANSMITTER_CHAN
479  PRINT_CONFIG_VAR(RADIO_TRANSMITTER_CHAN)
480  if (superbitrf.num_channels == 0) {
481  superbitrf.num_channels = RADIO_TRANSMITTER_CHAN;
482  }
483 #endif
484  if (superbitrf.protocol == 0) {
486  }
487 #ifdef RADIO_TRANSMITTER_PROTOCOL
488  else {
489  PRINT_CONFIG_VAR(RADIO_TRANSMITTER_PROTOCOL)
490  superbitrf_set_protocol(RADIO_TRANSMITTER_PROTOCOL);
491  }
492 #endif
493 
494  // Start transfer
495  superbitrf.state = 0;
497  break;
498  }
499 
500  // Set the timer
502  superbitrf.state = 0;
503  break;
504  }
505  break;
506 
507  /* When the receiver is synchronizing with the transmitter */
510 
511 #ifdef RADIO_CONTROL_LED
512  slowLedCpt++;
513  if (slowLedCpt > 5000) {
514 
515  LED_TOGGLE(RADIO_CONTROL_LED);
516  slowLedCpt = 0;
517  }
518 #endif
519 
520  /* Switch the different states */
521  switch (superbitrf.state) {
522  case 0:
523  // When there is a timeout
525  superbitrf.state++;
526  }
527  break;
528  case 1:
529  // Abort the receive
530  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_abort_receive, 2);
531  superbitrf.state++;
532  break;
533  case 2:
534  // Switch channel, sop code, data code and crc
537  pn_row = (IS_DSM2(superbitrf.protocol)
540 
542  pn_codes[pn_row][superbitrf.sop_col],
543  pn_codes[pn_row][superbitrf.data_col],
545  superbitrf.state++;
546  break;
547  case 3:
548  // Create a new packet when no packet loss
549  if (!superbitrf.packet_loss) {
552  tx_packet[0] = ~superbitrf.bind_mfg_id[2];
553  tx_packet[1] = (~superbitrf.bind_mfg_id[3]) + 1 + superbitrf.packet_loss_bit;
554  } else {
555  tx_packet[0] = superbitrf.bind_mfg_id[2];
556  tx_packet[1] = (superbitrf.bind_mfg_id[3]) + 1 + superbitrf.packet_loss_bit;
557  }
558 
561  if (packet_size > 14) {
562  packet_size = 14;
563  }
564 
565  for (i = 0; i < packet_size; i++) {
567  }
568  }
569 
570  // Send a packet
571  cyrf6936_send(&superbitrf.cyrf6936, tx_packet, packet_size + 2);
572 
573  // Update the packet extraction
574  if (!superbitrf.packet_loss) {
576  }
577 
578  superbitrf.state++;
579  break;
580  case 4:
581  //TODO: check timeout? (Waiting for send)
582  break;
583  case 5:
584  superbitrf.state = 7;
585  break;
586  case 6:
587  // Wait for telemetry data
589  superbitrf.state++;
590  }
591  break;
592  case 7:
593  // When DSMX we don't need to switch
595  superbitrf.state++;
597  break;
598  }
599 
600  // Switch channel, sop code, data code and crc
601  superbitrf.channel = (superbitrf.channel + 2) % 0x4F; //TODO fix define
603  pn_row = (IS_DSM2(superbitrf.protocol)
605 
607  pn_codes[pn_row][superbitrf.sop_col],
608  pn_codes[pn_row][superbitrf.data_col],
610 
611  superbitrf.state++;
612  break;
613  case 8:
614  // Start receiving
615  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
616  superbitrf.state++;
617  break;
618  default:
619  // Set the timer
621  superbitrf.state = 0;
622  break;
623  }
624  break;
625 
626  /* Normal transfer mode */
627  case SUPERBITRF_TRANSFER:
628 
629 #ifdef RADIO_CONTROL_LED
630  LED_ON(RADIO_CONTROL_LED);
631 #endif
632 
633  /* Switch the different states */
634  switch (superbitrf.state) {
635  case 0:
636  // Fixing timer overflow
638  superbitrf.timer_overflow = false;
639  }
640 
641  // When there is a timeout
645  superbitrf.state++;
646  }
647 
648  // We really lost the communication
649  if (superbitrf.timeouts > 100) {
650  superbitrf.state = 0;
653  }
654  break;
655  case 1:
656  // Abort the receive
657  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_abort_receive, 2);
658  superbitrf.state++;
659 
660  // Set the timer
663  superbitrf.timer_overflow = true;
664  } else {
665  superbitrf.timer_overflow = false;
666  }
667 
668  // Only send on channel 2
670  superbitrf.state = 8;
671  }
672  break;
673  case 2:
674  // Wait before sending (FIXME??)
675  superbitrf.state++;
676  break;
677  case 3:
678  // Create a new packet when no packet loss
679  if (!superbitrf.packet_loss) {
682  tx_packet[0] = ~superbitrf.bind_mfg_id[2];
683  tx_packet[1] = ((~superbitrf.bind_mfg_id[3]) + 1 + superbitrf.packet_loss_bit) % 0xFF;
684  } else {
685  tx_packet[0] = superbitrf.bind_mfg_id[2];
686  tx_packet[1] = ((superbitrf.bind_mfg_id[3]) + 1 + superbitrf.packet_loss_bit) % 0xFF;
687  }
688 
691  if (packet_size > 14) {
692  packet_size = 14;
693  }
694 
695  for (i = 0; i < packet_size; i++) {
697  }
698  }
699 
700  // Send a packet
701  cyrf6936_send(&superbitrf.cyrf6936, tx_packet, packet_size + 2);
702 
703  // Update the packet extraction
704  if (!superbitrf.packet_loss) {
706  }
707 
708  superbitrf.state++;
709  break;
710  case 4:
711  //TODO: check timeout? (Waiting for send)
712  break;
713  case 5:
714  // Start receiving
715  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
716  superbitrf.state++;
717  break;
718  case 6:
719  // Fixing timer overflow
721  superbitrf.timer_overflow = false;
722  }
723 
724  // Waiting for data receive
726  superbitrf.state++;
727  }
728  break;
729  case 7:
730  // Abort the receive
731  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_abort_receive, 2);
732  superbitrf.state++;
733  break;
734  case 8:
735  // Switch channel, sop code, data code and crc
740  pn_row = (IS_DSM2(superbitrf.protocol)
742 
744  pn_codes[pn_row][superbitrf.sop_col],
745  pn_codes[pn_row][superbitrf.data_col],
747 
748  superbitrf.state++;
749  break;
750  case 9:
751  // Start receiving
752  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
753  superbitrf.state++;
754  break;
755  default:
756  // Set the timer
759  } else {
761  }
763  superbitrf.timer_overflow = true;
764  } else {
765  superbitrf.timer_overflow = false;
766  }
767 
768  superbitrf.state = 0;
769  break;
770  }
771  break;
772 
773  /* Should not come here */
774  default:
775  break;
776  }
777 }
778 
782 static inline void superbitrf_receive_packet_cb(bool error, uint8_t status, uint8_t packet[])
783 {
784  int i;
785  uint16_t sum;
786 
787  /* Switch on the status of the superbitRF */
788  switch (superbitrf.status) {
789 
790  /* When we are in binding mode */
791  case SUPERBITRF_BINDING:
792  // Check if the MFG id is exactly the same
793  if (packet[0] != packet[4] || packet[1] != packet[5] || packet[2] != packet[6] || packet[3] != packet[7]) {
794  // Start receiving without changing channel
795  superbitrf.state = 3;
796  break;
797  }
798 
799  // Calculate the first sum
800  sum = 384 - 0x10;
801  for (i = 0; i < 8; i++) {
802  sum += packet[i];
803  }
804 
805  // Check the first sum
806  if (packet[8] != sum >> 8 || packet[9] != (sum & 0xFF)) {
807  // Start receiving without changing channel
808  superbitrf.state = 3;
809  break;
810  }
811 
812  // Calculate second sum
813  for (i = 8; i < 14; i++) {
814  sum += packet[i];
815  }
816 
817  // Check the second sum
818  if (packet[14] != sum >> 8 || packet[15] != (sum & 0xFF)) {
819  // Start receiving without changing channel
820  superbitrf.state = 3;
821  break;
822  }
823 
824  // Update the mfg id, number of channels and protocol
825  uint32_t mfg_id = ((~packet[3] & 0xFF) << 24 | (~packet[2] & 0xFF) << 16 |
826  (~packet[1] & 0xFF) << 8 | (~packet[0] & 0xFF));
827  superbitrf_set_mfg_id(mfg_id);
828 
829  superbitrf.num_channels = packet[11];
830  superbitrf_set_protocol(packet[12]);
831 
832  // Store all the persistent settings.
833  // In case we have the superbit setting file loaded and persistent settings
834  // enabled in the airframe file this will store our binding information and
835  // survive a reboot.
837 
838  // Update the status of the receiver
839  superbitrf.state = 0;
841  break;
842 
843  /* When we receive a packet during syncing first channel A */
845  // Check the MFG id
846  if (error && !(status & CYRF_BAD_CRC)) {
847  // Start receiving TODO: Fix nicely
848  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
849  break;
850  }
852  (packet[0] != (~superbitrf.bind_mfg_id[2] & 0xFF) || (packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) &&
853  packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) + 1))) {
854  // Start receiving TODO: Fix nicely
855  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
856  break;
857  }
859  (packet[0] != (superbitrf.bind_mfg_id[2] & 0xFF) || (packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF) &&
860  packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF) + 1))) {
861  // Start receiving TODO: Fix nicely
862  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
863  break;
864  }
865 
866  // If the CRC is wrong invert
867  if (error && (status & CYRF_BAD_CRC)) {
869  }
870 
871  // When we receive a data packet
872  if (packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) && packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF)) {
874 
875  // Check if it is a data loss packet
876  if (packet[1] != (~superbitrf.bind_mfg_id[3] + 1 + superbitrf.packet_loss_bit) % 0xFF
877  && packet[1] != (superbitrf.bind_mfg_id[3] + 1 + superbitrf.packet_loss_bit) % 0xFF) {
878  superbitrf.packet_loss = true;
879  } else {
880  superbitrf.packet_loss = false;
881  }
882 
883  // When it is a data packet, parse the packet if not busy already
885  for (i = 2; i < superbitrf.cyrf6936.rx_count; i++) {
886  parse_pprz(&superbitrf.rx_transport, packet[i]);
887 
888  // When we have a full message
889  if (superbitrf.rx_transport.trans_rx.msg_received) {
890  DatalinkFillDlBuffer(superbitrf.rx_transport.trans_rx.payload, superbitrf.rx_transport.trans_rx.payload_len);
891  superbitrf.rx_transport.trans_rx.msg_received = false;
892  }
893  }
894  }
895  break;
896  }
897 
901 
902  superbitrf.state = 1;
904  } else {
905  superbitrf.timeouts = 0;
906  superbitrf.state = 1;
908  }
909  break;
910 
911  /* When we receive a packet during syncing second channel B */
913  // Check the MFG id
914  if (error && !(status & CYRF_BAD_CRC)) {
915  // Start receiving TODO: Fix nicely
916  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
917  break;
918  }
920  (packet[0] != (~superbitrf.bind_mfg_id[2] & 0xFF) || (packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) &&
921  packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) + 1 && packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) + 2))) {
922  // Start receiving TODO: Fix nicely
923  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
924  break;
925  }
927  (packet[0] != (superbitrf.bind_mfg_id[2] & 0xFF) || (packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF) &&
928  packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF) + 1 && packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF) + 2))) {
929  // Start receiving TODO: Fix nicely
930  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
931  break;
932  }
933 
934  // If the CRC is wrong invert
935  if (error && (status & CYRF_BAD_CRC)) {
937  }
938 
939  // When we receive a data packet
940  if (packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) && packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF)) {
942 
943  // When it is a data packet, parse the packet if not busy already
944  if (!dl_msg_available) {
945  for (i = 2; i < superbitrf.cyrf6936.rx_count; i++) {
946  parse_pprz(&superbitrf.rx_transport, packet[i]);
947 
948  // When we have a full message
949  if (superbitrf.rx_transport.trans_rx.msg_received) {
950  DatalinkFillDlBuffer(superbitrf.rx_transport.trans_rx.payload, superbitrf.rx_transport.trans_rx.payload_len);
951  superbitrf.rx_transport.trans_rx.msg_received = false;
952  }
953  }
954  }
955  break;
956  }
957 
958  // Set the channel
962  } else {
965  }
966 
967  // When the channels aren't the same go to transfer mode
968  if (superbitrf.channels[1] != superbitrf.channels[0]) {
969  superbitrf.state = 1;
971  superbitrf.timeouts = 0;
972  }
973  break;
974 
975  /* When we receive a packet during transfer */
976  case SUPERBITRF_TRANSFER:
977  // Check the MFG id
978  if (error) {
979  // Start receiving TODO: Fix nicely
980  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
981  break;
982  }
984  (packet[0] != (~superbitrf.bind_mfg_id[2] & 0xFF) || (packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) &&
985  packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) + 1 && packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) + 2))) {
986  // Start receiving TODO: Fix nicely
987  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
988  break;
989  }
991  (packet[0] != (superbitrf.bind_mfg_id[2] & 0xFF) || (packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF) &&
992  packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF) + 1 && packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF) + 2))) {
993  // Start receiving TODO: Fix nicely
994  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
995  break;
996  }
997 
998  // Check if it is a RC packet
999  if (packet[1] == (~superbitrf.bind_mfg_id[3] & 0xFF) || packet[1] == (superbitrf.bind_mfg_id[3] & 0xFF)) {
1000  superbitrf.rc_count++;
1001 
1002  // Parse the packet
1005 
1006  // Calculate the timing (seperately for the channel switches)
1007  if (superbitrf.crc_seed != ((superbitrf.bind_mfg_id[0] << 8) + superbitrf.bind_mfg_id[1])) {
1009  } else {
1011  }
1012 
1013  // Go to next receive
1014  superbitrf.state = 1;
1015  superbitrf.timeouts = 0;
1016  } else {
1018 
1019  // Check if it is a data loss packet
1020  if (packet[1] != (~superbitrf.bind_mfg_id[3] + 1 + superbitrf.packet_loss_bit)
1021  && packet[1] != (superbitrf.bind_mfg_id[3] + 1 + superbitrf.packet_loss_bit)) {
1022  superbitrf.packet_loss = true;
1023  } else {
1024  superbitrf.packet_loss = false;
1025  }
1026 
1027  superbitrf.packet_loss = false;
1028 
1029  // When it is a data packet, parse the packet if not busy already
1031  for (i = 2; i < superbitrf.cyrf6936.rx_count; i++) {
1032  parse_pprz(&superbitrf.rx_transport, packet[i]);
1033 
1034  // When we have a full message
1035  if (superbitrf.rx_transport.trans_rx.msg_received) {
1036  DatalinkFillDlBuffer(superbitrf.rx_transport.trans_rx.payload, superbitrf.rx_transport.trans_rx.payload_len);
1037  superbitrf.rx_transport.trans_rx.msg_received = false;
1038  }
1039  }
1040  }
1041 
1042  // Update the state
1043  superbitrf.state = 7;
1044  }
1045  break;
1046 
1047  /* Should not come here */
1048  default:
1049  break;
1050  }
1051 }
1052 
1053 static inline void superbitrf_send_packet_cb(bool error __attribute__((unused)))
1054 {
1055  /* Switch on the status of the superbitRF */
1056  switch (superbitrf.status) {
1057 
1058  /* When we are synchronizing */
1059  case SUPERBITRF_SYNCING_A:
1060  case SUPERBITRF_SYNCING_B:
1061  // When we successfully or unsuccessfully send a data packet
1062  if (superbitrf.state == 4) {
1063  superbitrf.state++;
1064  }
1065  break;
1066 
1067  /* When we are in transfer mode */
1068  case SUPERBITRF_TRANSFER:
1069  // When we successfully or unsuccessfully send a packet
1070  if (superbitrf.state == 4) {
1071  superbitrf.state++;
1072  }
1073  break;
1074 
1075  /* Should not come here */
1076  default:
1077  break;
1078  }
1079 }
1080 
1084 static inline void superbitrf_radio_to_channels(uint8_t *data, uint8_t nb_channels, bool is_11bit, int16_t *channels)
1085 {
1086  int i;
1087  uint8_t bit_shift = (is_11bit) ? 11 : 10;
1088  int16_t value_max = (is_11bit) ? 0x07FF : 0x03FF;
1089 
1090  for (i = 0; i < 7; i++) {
1091  const int16_t tmp = ((data[2 * i] << 8) + data[2 * i + 1]) & 0x7FFF;
1092  const uint8_t chan = (tmp >> bit_shift) & 0x0F;
1093  const int16_t val = (tmp & value_max);
1094 
1095  if (chan < nb_channels) {
1096  channels[chan] = val;
1097 
1098  // Scale the channel
1099  if (is_11bit) {
1100  channels[chan] -= 0x400;
1101  channels[chan] *= MAX_PPRZ / 0x2AC;
1102  } else {
1103  channels[chan] -= 0x200;
1104  channels[chan] *= MAX_PPRZ / 0x156;
1105  }
1106  }
1107  }
1108 }
1109 
1113 static inline void superbitrf_gen_dsmx_channels(void)
1114 {
1115  // Calculate the DSMX channels
1116  int idx = 0;
1117  uint32_t id = ~((superbitrf.bind_mfg_id[0] << 24) | (superbitrf.bind_mfg_id[1] << 16) |
1118  (superbitrf.bind_mfg_id[2] << 8) | (superbitrf.bind_mfg_id[3] << 0));
1119  uint32_t id_tmp = id;
1120 
1121  // While not all channels are set
1122  while (idx < 23) {
1123  int i;
1124  int count_3_27 = 0, count_28_51 = 0, count_52_76 = 0;
1125 
1126  id_tmp = id_tmp * 0x0019660D + 0x3C6EF35F; // Randomization
1127  uint8_t next_ch = ((id_tmp >> 8) % 0x49) + 3; // Use least-significant byte and must be larger than 3
1128  if (((next_ch ^ id) & 0x01) == 0) {
1129  continue;
1130  }
1131 
1132  // Go trough all already set channels
1133  for (i = 0; i < idx; i++) {
1134  // Channel is already used
1135  if (superbitrf.channels[i] == next_ch) {
1136  break;
1137  }
1138 
1139  // Count the channel groups
1140  if (superbitrf.channels[i] <= 27) {
1141  count_3_27++;
1142  } else if (superbitrf.channels[i] <= 51) {
1143  count_28_51++;
1144  } else {
1145  count_52_76++;
1146  }
1147  }
1148 
1149  // When channel is already used continue
1150  if (i != idx) {
1151  continue;
1152  }
1153 
1154  // Set the channel when channel groups aren't full
1155  if ((next_ch < 28 && count_3_27 < 8) // Channels 3-27: max 8
1156  || (next_ch >= 28 && next_ch < 52 && count_28_51 < 7) // Channels 28-52: max 7
1157  || (next_ch >= 52 && count_52_76 < 8)) { // Channels 52-76: max 8
1158  superbitrf.channels[idx++] = next_ch;
1159  }
1160  }
1161 }
1162 
1163 
1164 
unsigned short uint16_t
Definition: types.h:16
status
Definition: anemotaxis.c:10
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
#define SPEKTRUM_BIND_PIN
Definition: apogee_1.0.h:417
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
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 LED_TOGGLE(i)
Definition: led_hw.h:52
#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: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
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:50
#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