Paparazzi UAS  v5.10_stable-5-g83a0da5-dirty
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 is_11bit, int16_t *channels);
74 static inline void superbitrf_receive_packet_cb(bool error, uint8_t status, uint8_t packet[]);
75 static inline void superbitrf_send_packet_cb(bool 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 superbitrf_check_free_space(struct SuperbitRF *p, long *fd __attribute__((unused)), uint16_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, long fd __attribute__((unused)), uint8_t byte)
214 {
215  p->tx_buffer[p->tx_insert_idx] = byte;
217 }
218 
219 static void superbitrf_transmit_buffer(struct SuperbitRF *p, long fd, uint8_t *data, uint16_t len)
220 {
221  int i;
222  for (i = 0; i < len; i++) {
223  superbitrf_transmit(p, fd, data[i]);
224  }
225 }
226 
227 static void superbitrf_send(struct SuperbitRF *p __attribute__((unused)), long fd __attribute__((unused))) { }
228 
229 static int null_function(struct SuperbitRF *p __attribute__((unused))) { return 0; }
230 
234 void superbitrf_init(void)
235 {
236  // Set the status to uninitialized and set the timer to 0
238  superbitrf.state = 0;
239  superbitrf.timer = 0;
242 
243  // Setup the transmit buffer
246 
249  superbitrf.protocol = 0;
250 
251  // Configure generic device
252  superbitrf.device.periph = (void *)(&superbitrf);
253  superbitrf.device.check_free_space = (check_free_space_t) superbitrf_check_free_space;
254  superbitrf.device.put_byte = (put_byte_t) superbitrf_transmit;
255  superbitrf.device.put_buffer = (put_buffer_t) superbitrf_transmit_buffer;
256  superbitrf.device.send_message = (send_message_t) superbitrf_send;
257  superbitrf.device.char_available = (char_available_t) null_function; // not needed
258  superbitrf.device.get_byte = (get_byte_t) null_function; // not needed
259 
260  // Initialize the binding pin
262 
263  // Initialize the IRQ/DRDY pin
265 
266  // Initialize the cyrf6936 chip
268 
269 #if PERIODIC_TELEMETRY
271 #endif
272 }
273 
275 {
278  superbitrf.bind_mfg_id[1] = (superbitrf.bind_mfg_id32 >> 8 & 0xFF);
279  superbitrf.bind_mfg_id[2] = (superbitrf.bind_mfg_id32 >> 16 & 0xFF);
280  superbitrf.bind_mfg_id[3] = (superbitrf.bind_mfg_id32 >> 24 & 0xFF);
281 
282  // Calculate some values based on the bind MFG id
286 }
287 
289 {
291  superbitrf.resolution = (superbitrf.protocol & 0x10) >> 4;
292 }
293 
298 {
299  uint8_t i, pn_row, data_code[16];
300  static uint8_t packet_size, tx_packet[16];
301  static bool start_transfer = true;
302 
303 #ifdef RADIO_CONTROL_LED
304  static uint32_t slowLedCpt = 0;
305 #endif
306 
307  // Check if the cyrf6936 isn't busy and the uperbitrf is initialized
309  return;
310  }
311 
312  // When the device is initialized handle the IRQ
314  // First handle the IRQ
316  // Receive the packet
319  }
320 
321  /* Check if it is a valid receive */
323  // Handle the packet received
327 
328  // Reset the packet receiving
329  superbitrf.cyrf6936.has_irq = false;
330  }
331 
332  /* Check if it has a valid send */
334  // Handle the send packet
337 
338  // Reset the packet receiving
339  superbitrf.cyrf6936.has_irq = false;
340  }
341  }
342 
343  // Check the status of the superbitrf
344  switch (superbitrf.status) {
345 
346  /* When the superbitrf isn't initialized */
347  case SUPERBITRF_UNINIT:
348  // Try to write the startup config
350  // Check if need to go to bind or transfer
352  start_transfer = false;
353  }
354 
356  }
357  break;
358 
359  /* When the superbitrf is initializing binding */
361  /* Switch the different states */
362  switch (superbitrf.state) {
363  case 0:
364  // Try to write the binding config
365  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_bind_config, 5);
366  superbitrf.state++;
367  break;
368  case 1:
369  // Set the data code and channel
370  memcpy(data_code, pn_codes[0][8], 8);
371  memcpy(data_code + 8, pn_bind, 8);
372  cyrf6936_write_chan_sop_data_crc(&superbitrf.cyrf6936, 1, pn_codes[0][0], data_code, 0x0000);
373 
375  break;
376  default:
377  // Should not happen
378  superbitrf.state = 0;
379  break;
380  }
381  break;
382 
383  /* When the superbitrf is initializing transfer */
385  // Generate the DSMX channels
387 
388  // Try to write the transfer config
389  if (cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_transfer_config, 4)) {
391  superbitrf.packet_loss = false;
394  superbitrf.state = 1;
395  }
396  break;
397 
398  /* When the superbitrf is in binding mode */
399  case SUPERBITRF_BINDING:
400 
401 #ifdef RADIO_CONTROL_LED
402  slowLedCpt++;
403  if (slowLedCpt > 100000) {
404 
405  LED_TOGGLE(RADIO_CONTROL_LED);
406  slowLedCpt = 0;
407  }
408 #endif
409 
410  /* Switch the different states */
411  switch (superbitrf.state) {
412  case 0:
413  // When there is a timeout
415  superbitrf.state++;
416  }
417  break;
418  case 1:
419  // Abort the receive
420  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_abort_receive, 2);
421 
422  superbitrf.state++;
423  break;
424  case 2:
425  // Switch channel
426  superbitrf.channel = (superbitrf.channel + 2) % 0x4F; //TODO fix define
428 
429  superbitrf.state += 2; // Already aborted
430  break;
431  case 3:
432  // Abort the receive
433  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_abort_receive, 2);
434 
435  superbitrf.state++;
436  break;
437  case 4:
438  // Start receiving
439  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
440  superbitrf.state++;
441  break;
442  default:
443  // Check if need to go to transfer
444  if (start_transfer) {
445  // Initialize the binding values
446  // set values based on mfg id
447  // if bind_mfg_id32 is loaded from persistent settings use that,
450  }
451 #ifdef RADIO_TRANSMITTER_ID
452  // otherwise load airframe file value
453  else {
454  PRINT_CONFIG_VAR(RADIO_TRANSMITTER_ID)
455  superbitrf_set_mfg_id(RADIO_TRANSMITTER_ID);
456  }
457 #endif
458 #ifdef RADIO_TRANSMITTER_CHAN
459  PRINT_CONFIG_VAR(RADIO_TRANSMITTER_CHAN)
460  if (superbitrf.num_channels == 0) {
461  superbitrf.num_channels = RADIO_TRANSMITTER_CHAN;
462  }
463 #endif
464  if (superbitrf.protocol == 0) {
466  }
467 #ifdef RADIO_TRANSMITTER_PROTOCOL
468  else {
469  PRINT_CONFIG_VAR(RADIO_TRANSMITTER_PROTOCOL)
470  superbitrf_set_protocol(RADIO_TRANSMITTER_PROTOCOL);
471  }
472 #endif
473 
474  // Start transfer
475  superbitrf.state = 0;
477  break;
478  }
479 
480  // Set the timer
482  superbitrf.state = 0;
483  break;
484  }
485  break;
486 
487  /* When the receiver is synchronizing with the transmitter */
490 
491 #ifdef RADIO_CONTROL_LED
492  slowLedCpt++;
493  if (slowLedCpt > 5000) {
494 
495  LED_TOGGLE(RADIO_CONTROL_LED);
496  slowLedCpt = 0;
497  }
498 #endif
499 
500  /* Switch the different states */
501  switch (superbitrf.state) {
502  case 0:
503  // When there is a timeout
505  superbitrf.state++;
506  }
507  break;
508  case 1:
509  // Abort the receive
510  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_abort_receive, 2);
511  superbitrf.state++;
512  break;
513  case 2:
514  // Switch channel, sop code, data code and crc
517  pn_row = (IS_DSM2(superbitrf.protocol)
520 
522  pn_codes[pn_row][superbitrf.sop_col],
523  pn_codes[pn_row][superbitrf.data_col],
525  superbitrf.state++;
526  break;
527  case 3:
528  // Create a new packet when no packet loss
529  if (!superbitrf.packet_loss) {
532  tx_packet[0] = ~superbitrf.bind_mfg_id[2];
533  tx_packet[1] = (~superbitrf.bind_mfg_id[3]) + 1 + superbitrf.packet_loss_bit;
534  } else {
535  tx_packet[0] = superbitrf.bind_mfg_id[2];
536  tx_packet[1] = (superbitrf.bind_mfg_id[3]) + 1 + superbitrf.packet_loss_bit;
537  }
538 
541  if (packet_size > 14) {
542  packet_size = 14;
543  }
544 
545  for (i = 0; i < packet_size; i++) {
547  }
548  }
549 
550  // Send a packet
551  cyrf6936_send(&superbitrf.cyrf6936, tx_packet, packet_size + 2);
552 
553  // Update the packet extraction
554  if (!superbitrf.packet_loss) {
556  }
557 
558  superbitrf.state++;
559  break;
560  case 4:
561  //TODO: check timeout? (Waiting for send)
562  break;
563  case 5:
564  superbitrf.state = 7;
565  break;
566  case 6:
567  // Wait for telemetry data
569  superbitrf.state++;
570  }
571  break;
572  case 7:
573  // When DSMX we don't need to switch
575  superbitrf.state++;
577  break;
578  }
579 
580  // Switch channel, sop code, data code and crc
581  superbitrf.channel = (superbitrf.channel + 2) % 0x4F; //TODO fix define
583  pn_row = (IS_DSM2(superbitrf.protocol)
585 
587  pn_codes[pn_row][superbitrf.sop_col],
588  pn_codes[pn_row][superbitrf.data_col],
590 
591  superbitrf.state++;
592  break;
593  case 8:
594  // Start receiving
595  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
596  superbitrf.state++;
597  break;
598  default:
599  // Set the timer
601  superbitrf.state = 0;
602  break;
603  }
604  break;
605 
606  /* Normal transfer mode */
607  case SUPERBITRF_TRANSFER:
608 
609 #ifdef RADIO_CONTROL_LED
610  LED_ON(RADIO_CONTROL_LED);
611 #endif
612 
613  /* Switch the different states */
614  switch (superbitrf.state) {
615  case 0:
616  // Fixing timer overflow
618  superbitrf.timer_overflow = false;
619  }
620 
621  // When there is a timeout
625  superbitrf.state++;
626  }
627 
628  // We really lost the communication
629  if (superbitrf.timeouts > 100) {
630  superbitrf.state = 0;
633  }
634  break;
635  case 1:
636  // Abort the receive
637  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_abort_receive, 2);
638  superbitrf.state++;
639 
640  // Set the timer
643  superbitrf.timer_overflow = true;
644  } else {
645  superbitrf.timer_overflow = false;
646  }
647 
648  // Only send on channel 2
650  superbitrf.state = 8;
651  }
652  break;
653  case 2:
654  // Wait before sending (FIXME??)
655  superbitrf.state++;
656  break;
657  case 3:
658  // Create a new packet when no packet loss
659  if (!superbitrf.packet_loss) {
662  tx_packet[0] = ~superbitrf.bind_mfg_id[2];
663  tx_packet[1] = ((~superbitrf.bind_mfg_id[3]) + 1 + superbitrf.packet_loss_bit) % 0xFF;
664  } else {
665  tx_packet[0] = superbitrf.bind_mfg_id[2];
666  tx_packet[1] = ((superbitrf.bind_mfg_id[3]) + 1 + superbitrf.packet_loss_bit) % 0xFF;
667  }
668 
671  if (packet_size > 14) {
672  packet_size = 14;
673  }
674 
675  for (i = 0; i < packet_size; i++) {
677  }
678  }
679 
680  // Send a packet
681  cyrf6936_send(&superbitrf.cyrf6936, tx_packet, packet_size + 2);
682 
683  // Update the packet extraction
684  if (!superbitrf.packet_loss) {
686  }
687 
688  superbitrf.state++;
689  break;
690  case 4:
691  //TODO: check timeout? (Waiting for send)
692  break;
693  case 5:
694  // Start receiving
695  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
696  superbitrf.state++;
697  break;
698  case 6:
699  // Fixing timer overflow
701  superbitrf.timer_overflow = false;
702  }
703 
704  // Waiting for data receive
706  superbitrf.state++;
707  }
708  break;
709  case 7:
710  // Abort the receive
711  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_abort_receive, 2);
712  superbitrf.state++;
713  break;
714  case 8:
715  // Switch channel, sop code, data code and crc
720  pn_row = (IS_DSM2(superbitrf.protocol)
722 
724  pn_codes[pn_row][superbitrf.sop_col],
725  pn_codes[pn_row][superbitrf.data_col],
727 
728  superbitrf.state++;
729  break;
730  case 9:
731  // Start receiving
732  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
733  superbitrf.state++;
734  break;
735  default:
736  // Set the timer
739  } else {
741  }
743  superbitrf.timer_overflow = true;
744  } else {
745  superbitrf.timer_overflow = false;
746  }
747 
748  superbitrf.state = 0;
749  break;
750  }
751  break;
752 
753  /* Should not come here */
754  default:
755  break;
756  }
757 }
758 
762 static inline void superbitrf_receive_packet_cb(bool error, uint8_t status, uint8_t packet[])
763 {
764  int i;
765  uint16_t sum;
766 
767  /* Switch on the status of the superbitRF */
768  switch (superbitrf.status) {
769 
770  /* When we are in binding mode */
771  case SUPERBITRF_BINDING:
772  // Check if the MFG id is exactly the same
773  if (packet[0] != packet[4] || packet[1] != packet[5] || packet[2] != packet[6] || packet[3] != packet[7]) {
774  // Start receiving without changing channel
775  superbitrf.state = 3;
776  break;
777  }
778 
779  // Calculate the first sum
780  sum = 384 - 0x10;
781  for (i = 0; i < 8; i++) {
782  sum += packet[i];
783  }
784 
785  // Check the first sum
786  if (packet[8] != sum >> 8 || packet[9] != (sum & 0xFF)) {
787  // Start receiving without changing channel
788  superbitrf.state = 3;
789  break;
790  }
791 
792  // Calculate second sum
793  for (i = 8; i < 14; i++) {
794  sum += packet[i];
795  }
796 
797  // Check the second sum
798  if (packet[14] != sum >> 8 || packet[15] != (sum & 0xFF)) {
799  // Start receiving without changing channel
800  superbitrf.state = 3;
801  break;
802  }
803 
804  // Update the mfg id, number of channels and protocol
805  uint32_t mfg_id = ((~packet[3] & 0xFF) << 24 | (~packet[2] & 0xFF) << 16 |
806  (~packet[1] & 0xFF) << 8 | (~packet[0] & 0xFF));
807  superbitrf_set_mfg_id(mfg_id);
808 
809  superbitrf.num_channels = packet[11];
810  superbitrf_set_protocol(packet[12]);
811 
812  // Store all the persistent settings.
813  // In case we have the superbit setting file loaded and persistent settings
814  // enabled in the airframe file this will store our binding information and
815  // survive a reboot.
817 
818  // Update the status of the receiver
819  superbitrf.state = 0;
821  break;
822 
823  /* When we receive a packet during syncing first channel A */
825  // Check the MFG id
826  if (error && !(status & CYRF_BAD_CRC)) {
827  // Start receiving TODO: Fix nicely
828  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
829  break;
830  }
832  (packet[0] != (~superbitrf.bind_mfg_id[2] & 0xFF) || (packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) &&
833  packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) + 1))) {
834  // Start receiving TODO: Fix nicely
835  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
836  break;
837  }
839  (packet[0] != (superbitrf.bind_mfg_id[2] & 0xFF) || (packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF) &&
840  packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF) + 1))) {
841  // Start receiving TODO: Fix nicely
842  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
843  break;
844  }
845 
846  // If the CRC is wrong invert
847  if (error && (status & CYRF_BAD_CRC)) {
849  }
850 
851  // When we receive a data packet
852  if (packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) && packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF)) {
854 
855  // Check if it is a data loss packet
856  if (packet[1] != (~superbitrf.bind_mfg_id[3] + 1 + superbitrf.packet_loss_bit) % 0xFF
857  && packet[1] != (superbitrf.bind_mfg_id[3] + 1 + superbitrf.packet_loss_bit) % 0xFF) {
858  superbitrf.packet_loss = true;
859  } else {
860  superbitrf.packet_loss = false;
861  }
862 
863  // When it is a data packet, parse the packet if not busy already
865  for (i = 2; i < superbitrf.cyrf6936.rx_count; i++) {
866  parse_pprz(&superbitrf.rx_transport, packet[i]);
867 
868  // When we have a full message
869  if (superbitrf.rx_transport.trans_rx.msg_received) {
870  DatalinkFillDlBuffer(superbitrf.rx_transport.trans_rx.payload, superbitrf.rx_transport.trans_rx.payload_len);
871  superbitrf.rx_transport.trans_rx.msg_received = false;
872  }
873  }
874  }
875  break;
876  }
877 
881 
882  superbitrf.state = 1;
884  } else {
885  superbitrf.timeouts = 0;
886  superbitrf.state = 1;
888  }
889  break;
890 
891  /* When we receive a packet during syncing second channel B */
893  // Check the MFG id
894  if (error && !(status & CYRF_BAD_CRC)) {
895  // Start receiving TODO: Fix nicely
896  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
897  break;
898  }
900  (packet[0] != (~superbitrf.bind_mfg_id[2] & 0xFF) || (packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) &&
901  packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) + 1 && packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) + 2))) {
902  // Start receiving TODO: Fix nicely
903  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
904  break;
905  }
907  (packet[0] != (superbitrf.bind_mfg_id[2] & 0xFF) || (packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF) &&
908  packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF) + 1 && packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF) + 2))) {
909  // Start receiving TODO: Fix nicely
910  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
911  break;
912  }
913 
914  // If the CRC is wrong invert
915  if (error && (status & CYRF_BAD_CRC)) {
917  }
918 
919  // When we receive a data packet
920  if (packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) && packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF)) {
922 
923  // When it is a data packet, parse the packet if not busy already
924  if (!dl_msg_available) {
925  for (i = 2; i < superbitrf.cyrf6936.rx_count; i++) {
926  parse_pprz(&superbitrf.rx_transport, packet[i]);
927 
928  // When we have a full message
929  if (superbitrf.rx_transport.trans_rx.msg_received) {
930  DatalinkFillDlBuffer(superbitrf.rx_transport.trans_rx.payload, superbitrf.rx_transport.trans_rx.payload_len);
931  superbitrf.rx_transport.trans_rx.msg_received = false;
932  }
933  }
934  }
935  break;
936  }
937 
938  // Set the channel
942  } else {
945  }
946 
947  // When the channels aren't the same go to transfer mode
948  if (superbitrf.channels[1] != superbitrf.channels[0]) {
949  superbitrf.state = 1;
951  superbitrf.timeouts = 0;
952  }
953  break;
954 
955  /* When we receive a packet during transfer */
956  case SUPERBITRF_TRANSFER:
957  // Check the MFG id
958  if (error) {
959  // Start receiving TODO: Fix nicely
960  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
961  break;
962  }
964  (packet[0] != (~superbitrf.bind_mfg_id[2] & 0xFF) || (packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) &&
965  packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) + 1 && packet[1] != (~superbitrf.bind_mfg_id[3] & 0xFF) + 2))) {
966  // Start receiving TODO: Fix nicely
967  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
968  break;
969  }
971  (packet[0] != (superbitrf.bind_mfg_id[2] & 0xFF) || (packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF) &&
972  packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF) + 1 && packet[1] != (superbitrf.bind_mfg_id[3] & 0xFF) + 2))) {
973  // Start receiving TODO: Fix nicely
974  cyrf6936_multi_write(&superbitrf.cyrf6936, cyrf_start_receive, 2);
975  break;
976  }
977 
978  // Check if it is a RC packet
979  if (packet[1] == (~superbitrf.bind_mfg_id[3] & 0xFF) || packet[1] == (superbitrf.bind_mfg_id[3] & 0xFF)) {
981 
982  // Parse the packet
985 
986  // Calculate the timing (seperately for the channel switches)
989  } else {
991  }
992 
993  // Go to next receive
994  superbitrf.state = 1;
995  superbitrf.timeouts = 0;
996  } else {
998 
999  // Check if it is a data loss packet
1000  if (packet[1] != (~superbitrf.bind_mfg_id[3] + 1 + superbitrf.packet_loss_bit)
1001  && packet[1] != (superbitrf.bind_mfg_id[3] + 1 + superbitrf.packet_loss_bit)) {
1002  superbitrf.packet_loss = true;
1003  } else {
1004  superbitrf.packet_loss = false;
1005  }
1006 
1007  superbitrf.packet_loss = false;
1008 
1009  // When it is a data packet, parse the packet if not busy already
1011  for (i = 2; i < superbitrf.cyrf6936.rx_count; i++) {
1012  parse_pprz(&superbitrf.rx_transport, packet[i]);
1013 
1014  // When we have a full message
1015  if (superbitrf.rx_transport.trans_rx.msg_received) {
1016  DatalinkFillDlBuffer(superbitrf.rx_transport.trans_rx.payload, superbitrf.rx_transport.trans_rx.payload_len);
1017  superbitrf.rx_transport.trans_rx.msg_received = false;
1018  }
1019  }
1020  }
1021 
1022  // Update the state
1023  superbitrf.state = 7;
1024  }
1025  break;
1026 
1027  /* Should not come here */
1028  default:
1029  break;
1030  }
1031 }
1032 
1033 static inline void superbitrf_send_packet_cb(bool error __attribute__((unused)))
1034 {
1035  /* Switch on the status of the superbitRF */
1036  switch (superbitrf.status) {
1037 
1038  /* When we are synchronizing */
1039  case SUPERBITRF_SYNCING_A:
1040  case SUPERBITRF_SYNCING_B:
1041  // When we successfully or unsuccessfully send a data packet
1042  if (superbitrf.state == 4) {
1043  superbitrf.state++;
1044  }
1045  break;
1046 
1047  /* When we are in transfer mode */
1048  case SUPERBITRF_TRANSFER:
1049  // When we successfully or unsuccessfully send a packet
1050  if (superbitrf.state == 4) {
1051  superbitrf.state++;
1052  }
1053  break;
1054 
1055  /* Should not come here */
1056  default:
1057  break;
1058  }
1059 }
1060 
1064 static inline void superbitrf_radio_to_channels(uint8_t *data, uint8_t nb_channels, bool is_11bit, int16_t *channels)
1065 {
1066  int i;
1067  uint8_t bit_shift = (is_11bit) ? 11 : 10;
1068  int16_t value_max = (is_11bit) ? 0x07FF : 0x03FF;
1069 
1070  for (i = 0; i < 7; i++) {
1071  const int16_t tmp = ((data[2 * i] << 8) + data[2 * i + 1]) & 0x7FFF;
1072  const uint8_t chan = (tmp >> bit_shift) & 0x0F;
1073  const int16_t val = (tmp & value_max);
1074 
1075  if (chan < nb_channels) {
1076  channels[chan] = val;
1077 
1078  // Scale the channel
1079  if (is_11bit) {
1080  channels[chan] -= 0x400;
1081  channels[chan] *= MAX_PPRZ / 0x2AC;
1082  } else {
1083  channels[chan] -= 0x200;
1084  channels[chan] *= MAX_PPRZ / 0x156;
1085  }
1086  }
1087  }
1088 }
1089 
1093 static inline void superbitrf_gen_dsmx_channels(void)
1094 {
1095  // Calculate the DSMX channels
1096  int idx = 0;
1097  uint32_t id = ~((superbitrf.bind_mfg_id[0] << 24) | (superbitrf.bind_mfg_id[1] << 16) |
1098  (superbitrf.bind_mfg_id[2] << 8) | (superbitrf.bind_mfg_id[3] << 0));
1099  uint32_t id_tmp = id;
1100 
1101  // While not all channels are set
1102  while (idx < 23) {
1103  int i;
1104  int count_3_27 = 0, count_28_51 = 0, count_52_76 = 0;
1105 
1106  id_tmp = id_tmp * 0x0019660D + 0x3C6EF35F; // Randomization
1107  uint8_t next_ch = ((id_tmp >> 8) % 0x49) + 3; // Use least-significant byte and must be larger than 3
1108  if (((next_ch ^ id) & 0x01) == 0) {
1109  continue;
1110  }
1111 
1112  // Go trough all already set channels
1113  for (i = 0; i < idx; i++) {
1114  // Channel is already used
1115  if (superbitrf.channels[i] == next_ch) {
1116  break;
1117  }
1118 
1119  // Count the channel groups
1120  if (superbitrf.channels[i] <= 27) {
1121  count_3_27++;
1122  } else if (superbitrf.channels[i] <= 51) {
1123  count_28_51++;
1124  } else {
1125  count_52_76++;
1126  }
1127  }
1128 
1129  // When channel is already used continue
1130  if (i != idx) {
1131  continue;
1132  }
1133 
1134  // Set the channel when channel groups aren't full
1135  if ((next_ch < 28 && count_3_27 < 8) // Channels 3-27: max 8
1136  || (next_ch >= 28 && next_ch < 52 && count_28_51 < 7) // Channels 28-52: max 7
1137  || (next_ch >= 52 && count_52_76 < 8)) { // Channels 52-76: max 8
1138  superbitrf.channels[idx++] = next_ch;
1139  }
1140  }
1141 }
1142 
1143 
1144 
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
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:51
#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:83
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
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:49
#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