Paparazzi UAS  v5.15_devel-111-g8fb4629
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 *************************************
54 * Function Name : CRC16
55 * Description : crc calculation, adds a 8 bit unsigned to 16 bit crc
56 *******************************************************************************/
58 {
59  uint8_t i;
60  crc = crc ^ (int16_t)value<<8;
61  for(i=0; i<8; i++) {
62  if (crc & 0x8000)
63  crc = (crc << 1) ^ HOTT_CRC_POLYNOME;
64  else
65  crc = (crc << 1);
66  }
67  return crc;
68 }
69 
71 static void decode_hott_buffer(const uint8_t *src, uint16_t *dst, uint8_t channels, bool *available, uint16_t *dstppm __attribute__((unused)))
72 {
73  // decode hott data
74  uint8_t i;
75  uint16_t sumd_crc_rx, sumd_crc=0;
76  sumd_crc = hott_CRC16(sumd_crc, src[0]);
77  sumd_crc = hott_CRC16(sumd_crc, src[1]);
78  sumd_crc = hott_CRC16(sumd_crc, src[2]);
79  for(i=0;i<channels;i++) {
80  sumd_crc = hott_CRC16(sumd_crc, src[i*2+3]);
81  sumd_crc = hott_CRC16(sumd_crc, src[i*2+4]);
82  dst[i] = (uint16_t)src[i*2 + 3]<<8 | (uint16_t)src[i*2+4];
83  }
84  sumd_crc_rx = (uint16_t)src[channels*2 +3]<<8 | (uint16_t)src[channels*2+4];
85  // convert hott to ppm
86 #if PERIODIC_TELEMETRY
87  for (int channel = 0; channel < HOTT_NB_CHANNEL; channel++) {
88  dstppm[channel] = USEC_OF_RC_PPM_TICKS(dst[channel]);
89  }
90 #endif
91  // test crc
92  *available = (sumd_crc == sumd_crc_rx);
93 }
94 
95 
96 // Decoding event function
97 // Reading from UART
98 void hott_common_decode_event(struct SHott *hott_p, struct uart_periph *dev)
99 {
100  uint8_t rbyte;
101  if (uart_char_available(dev)) {
102  do {
103  rbyte = uart_getch(dev);
104  switch (hott_p->status) {
105  case HOTT_STATUS_UNINIT:
106  // Wait for the start byte
107  hott_p->idx = 0;
108  if (rbyte == HOTT_START_BYTE) {
109  hott_p->status++;
110  hott_p->expected_channels = 0;
111  hott_p->buffer[hott_p->idx] = rbyte; // store header for crc
112  hott_p->idx++;
113  }
114  break;
116  if (rbyte ==0x01 || rbyte ==0x81) { // hott status
117  /*
118  * SUMD_Header Byte 1 Status
119  * 0x01: valid and live SUMD data frame
120  * 0x81: valid SUMD data frame with transmitter in fail safe condition.
121  * Note:
122  * The SUMD_Data section contains
123  * valid channel data. The channel data are
124  * set by transmitter fail safe values. A FBL
125  * system may replace the transmitter fail safe
126  * data by FBL stored values
127  *
128  * other values: Values different to 0x01 or 0x81 indicate an invalid SUMD data frame and
129  * should not be processed by SUMD algorithms
130  * */
131  hott_p->buffer[hott_p->idx] = rbyte; // store byte for CRC
132  hott_p->idx++;
133  hott_p->status++;
134  } else
135  {
136  hott_p->status=0; // reset
137  }
138  break;
140  /*Indicates the number of channels transmitted in the SUMD_Data section
141  */
142  if (rbyte >=2 && rbyte <= HOTT_NB_CHANNEL) {
143  hott_p->buffer[hott_p->idx] = rbyte; // stored for crc calculation
144  hott_p->idx++;
145  hott_p->expected_channels = rbyte;
146  hott_p->status++;
147  }else {
148  hott_p->status=0; // reset
149  }
150  break;
151  case HOTT_STATUS_DATA:
152  // Store buffer
153  hott_p->buffer[hott_p->idx] = rbyte;
154  hott_p->idx++;
155  // Decode if last byte is (one of) the correct end byte
156  if(hott_p->idx >= (hott_p->expected_channels*2 +2+3)){ // 3 bytes header, 2 bytes crc
157  decode_hott_buffer(hott_p->buffer, hott_p->pulses , hott_p->expected_channels, &hott_p->frame_available, hott_p->ppm);
158  hott_p->status = HOTT_STATUS_UNINIT;
159  }
160  break;
161  default:
162  break;
163  }
164  } while (uart_char_available(dev));
165  }
166 }
unsigned short uint16_t
Definition: types.h:16
uint16_t ppm[HOTT_NB_CHANNEL]
decoded and converted values
Definition: hott_common.h:79
#define USTOP_1
Definition: serial_port.c:52
void uart_periph_set_baudrate(struct uart_periph *p, uint32_t baud)
Set baudrate.
Definition: uart_arch.c:861
#define HOTT_STATUS_GOT_START
Definition: hott_common.c:37
uint8_t buffer[HOTT_BUF_LENGTH]
input buffer
Definition: hott_common.h:81
uint8_t uart_getch(struct uart_periph *p)
Definition: uart_arch.c:845
#define B115200
Definition: uart_arch.h:48
uint16_t uart_char_available(struct uart_periph *p)
Check UART for available chars in receive buffer.
Definition: uart_arch.c:323
bool frame_available
new frame available
Definition: hott_common.h:80
HOTT structure.
Definition: hott_common.h:77
#define HOTT_CRC_POLYNOME
Definition: hott_common.c:51
#define HOTT_NB_CHANNEL
Generated code holding the description of a given transmitter.
Definition: hott_common.h:60
#define UBITS_8
Definition: serial_port.c:50
uint8_t status
decoder state machine status
Definition: hott_common.h:84
#define UPARITY_NO
Definition: serial_port.c:55
uint8_t idx
input index
Definition: hott_common.h:83
#define USEC_OF_RC_PPM_TICKS(_v)
Definition: ppm_arch.h:46
uint8_t expected_channels
expected number of channels send in header
Definition: hott_common.h:82
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:71
UART peripheral.
Definition: uart.h:70
signed short int16_t
Definition: types.h:17
uint16_t pulses[HOTT_NB_CHANNEL]
decoded values
Definition: hott_common.h:78
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:73
#define HOTT_STATUS_UNINIT
Definition: hott_common.c:36
Common hott structs and defines.
unsigned char uint8_t
Definition: types.h:14
void uart_periph_set_bits_stop_parity(struct uart_periph *p, uint8_t bits, uint8_t stop, uint8_t parity)
Set parity and stop bits.
Definition: uart_arch.c:890
void hott_common_decode_event(struct SHott *hott_p, struct uart_periph *dev)
Decoding event function.
Definition: hott_common.c:98
void hott_common_init(struct SHott *hott_p, struct uart_periph *dev)
Init function.
Definition: hott_common.c:42
static uint8_t channel
Definition: ADS8344.c:80
#define HOTT_STATUS_DATA
Definition: hott_common.c:39
#define HOTT_START_BYTE
Definition: hott_common.c:34
#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:57