Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
hott_common.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 Alexandre Bustico, Gautier Hattenberger
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, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
28 #include BOARD_CONFIG
29 #include <string.h>
30 
31 /*
32  * HOTT protocol and state machine status
33  */
34 #define HOTT_START_BYTE 0xa8
35 
36 #define HOTT_STATUS_UNINIT 0
37 #define HOTT_STATUS_GOT_START 1
38 #define HOTT_STATUS_GOT_HEADER1 2
39 #define HOTT_STATUS_DATA 3
40 
41 
42 void hott_common_init(struct SHott *hott_p, struct uart_periph *dev)
43 {
44  hott_p->frame_available = false;
45  hott_p->status = HOTT_STATUS_UNINIT;
46  // Set UART parameters (115200, 8 bits, 1 stops, UPARITY_NO)
49 }
50 
51 #define HOTT_CRC_POLYNOME 0x1021
52 /*******************************************************************************
53 * Function Name : CRC16
54 * Description : crc calculation, adds a 8 bit unsigned to 16 bit crc
55 *******************************************************************************/
57 {
58  uint8_t i;
59  crc = crc ^ (int16_t)value<<8;
60  for(i=0; i<8; i++) {
61  if (crc & 0x8000)
62  crc = (crc << 1) ^ HOTT_CRC_POLYNOME;
63  else
64  crc = (crc << 1);
65  }
66  return crc;
67 }
68 
70 static void decode_hott_buffer(const uint8_t *src, uint16_t *dst, uint8_t channels, bool *available, uint16_t *dstppm __attribute__((unused)))
71 {
72  // decode hott data
73  uint8_t i;
74  uint16_t sumd_crc_rx, sumd_crc=0;
75  sumd_crc = hott_CRC16(sumd_crc, src[0]);
76  sumd_crc = hott_CRC16(sumd_crc, src[1]);
77  sumd_crc = hott_CRC16(sumd_crc, src[2]);
78  for(i=0;i<channels;i++) {
79  sumd_crc = hott_CRC16(sumd_crc, src[i*2+3]);
80  sumd_crc = hott_CRC16(sumd_crc, src[i*2+4]);
81  dst[i] = (uint16_t)src[i*2 + 3]<<8 | (uint16_t)src[i*2+4];
82  }
83  sumd_crc_rx = (uint16_t)src[channels*2 +3]<<8 | (uint16_t)src[channels*2+4];
84  // convert hott to ppm
85 #if PERIODIC_TELEMETRY
86  for (int channel = 0; channel < HOTT_NB_CHANNEL; channel++) {
87  dstppm[channel] = USEC_OF_RC_PPM_TICKS(dst[channel]);
88  }
89 #endif
90  // test crc
91  *available = (sumd_crc == sumd_crc_rx);
92 }
93 
94 
95 // Decoding event function
96 // Reading from UART
97 void hott_common_decode_event(struct SHott *hott_p, struct uart_periph *dev)
98 {
99  uint8_t rbyte;
100  if (uart_char_available(dev)) {
101  do {
102  rbyte = uart_getch(dev);
103  switch (hott_p->status) {
104  case HOTT_STATUS_UNINIT:
105  // Wait for the start byte
106  hott_p->idx = 0;
107  if (rbyte == HOTT_START_BYTE) {
108  hott_p->status++;
109  hott_p->expected_channels = 0;
110  hott_p->buffer[hott_p->idx] = rbyte; // store header for crc
111  hott_p->idx++;
112  }
113  break;
115  if (rbyte ==0x01 || rbyte ==0x81) { // hott status
116  /*
117  * SUMD_Header Byte 1 Status
118  * 0x01: valid and live SUMD data frame
119  * 0x81: valid SUMD data frame with transmitter in fail safe condition.
120  * Note:
121  * The SUMD_Data section contains
122  * valid channel data. The channel data are
123  * set by transmitter fail safe values. A FBL
124  * system may replace the transmitter fail safe
125  * data by FBL stored values
126  *
127  * other values: Values different to 0x01 or 0x81 indicate an invalid SUMD data frame and
128  * should not be processed by SUMD algorithms
129  * */
130  hott_p->buffer[hott_p->idx] = rbyte; // store byte for CRC
131  hott_p->idx++;
132  hott_p->status++;
133  } else {
134  hott_p->status=0; // reset
135  }
136  break;
138  /*Indicates the number of channels transmitted in the SUMD_Data section
139  */
140  if (rbyte >=2 && rbyte <= HOTT_NB_CHANNEL) {
141  hott_p->buffer[hott_p->idx] = rbyte; // stored for crc calculation
142  hott_p->idx++;
143  hott_p->expected_channels = rbyte;
144  hott_p->status++;
145  }else {
146  hott_p->status=0; // reset
147  }
148  break;
149  case HOTT_STATUS_DATA:
150  // Store buffer
151  hott_p->buffer[hott_p->idx] = rbyte;
152  hott_p->idx++;
153  // Decode if last byte is (one of) the correct end byte
154  if(hott_p->idx >= (hott_p->expected_channels*2 +2+3)){ // 3 bytes header, 2 bytes crc
155  decode_hott_buffer(hott_p->buffer, hott_p->pulses , hott_p->expected_channels, &hott_p->frame_available, hott_p->ppm);
156  hott_p->status = HOTT_STATUS_UNINIT;
157  }
158  break;
159  default:
160  break;
161  }
162  } while (uart_char_available(dev));
163  }
164 }
#define B115200
Definition: uart_arch.h:48
#define USEC_OF_RC_PPM_TICKS(_v)
Definition: ppm_arch.h:46
#define HOTT_CRC_POLYNOME
Definition: hott_common.c:51
#define HOTT_STATUS_GOT_HEADER1
Definition: hott_common.c:38
static uint16_t hott_CRC16(uint16_t crc, uint8_t value)
Definition: hott_common.c:56
void hott_common_init(struct SHott *hott_p, struct uart_periph *dev)
Init function.
Definition: hott_common.c:42
static void decode_hott_buffer(const uint8_t *src, uint16_t *dst, uint8_t channels, bool *available, uint16_t *dstppm)
Decode the raw buffer.
Definition: hott_common.c:70
#define HOTT_STATUS_UNINIT
Definition: hott_common.c:36
#define HOTT_START_BYTE
Definition: hott_common.c:34
#define HOTT_STATUS_DATA
Definition: hott_common.c:39
#define HOTT_STATUS_GOT_START
Definition: hott_common.c:37
void hott_common_decode_event(struct SHott *hott_p, struct uart_periph *dev)
Decoding event function.
Definition: hott_common.c:97
bool frame_available
new frame available
Definition: hott_common.h:73
uint8_t idx
input index
Definition: hott_common.h:76
uint8_t buffer[HOTT_BUF_LENGTH]
input buffer
Definition: hott_common.h:74
uint16_t ppm[HOTT_NB_CHANNEL]
decoded and converted values
Definition: hott_common.h:72
uint16_t pulses[HOTT_NB_CHANNEL]
decoded values
Definition: hott_common.h:71
uint8_t status
decoder state machine status
Definition: hott_common.h:77
#define HOTT_NB_CHANNEL
Generated code holding the description of a given transmitter.
Definition: hott_common.h:60
uint8_t expected_channels
expected number of channels send in header
Definition: hott_common.h:75
HOTT structure.
Definition: hott_common.h:70
int uart_char_available(struct uart_periph *p)
Check UART for available chars in receive buffer.
Definition: uart_arch.c:357
void uart_periph_set_bits_stop_parity(struct uart_periph *periph, uint8_t bits, uint8_t stop, uint8_t parity)
Definition: uart_arch.c:296
uint8_t uart_getch(struct uart_periph *p)
Definition: uart_arch.c:348
void uart_periph_set_baudrate(struct uart_periph *periph, uint32_t baud)
Definition: uart_arch.c:280
Generic interface for radio control modules.
#define UBITS_8
Definition: serial_port.c:50
#define UPARITY_NO
Definition: serial_port.c:55
#define USTOP_1
Definition: serial_port.c:52
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:74
UART peripheral.
Definition: uart.h:72
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
Definition: vl53l1_types.h:88
short int16_t
Typedef defining 16 bit short type.
Definition: vl53l1_types.h:93
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98