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
sbus_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 "mcu_periph/gpio.h"
30 #include <string.h>
31 
32 /*
33  * SBUS protocol and state machine status
34  */
35 #define SBUS_START_BYTE 0x0f
36 #define SBUS_END_BYTE 0x00
37 #define SBUS_BIT_PER_CHANNEL 11
38 #define SBUS_BIT_PER_BYTE 8
39 #define SBUS_FLAGS_BYTE 22
40 #define SBUS_FRAME_LOST_BIT 2
41 
42 #define SBUS_STATUS_UNINIT 0
43 #define SBUS_STATUS_GOT_START 1
44 
52 #ifndef RC_SET_POLARITY
53 #define RC_SET_POLARITY gpio_set
54 #endif
55 
56 
57 void sbus_common_init(struct Sbus *sbus_p, struct uart_periph *dev)
58 {
59  sbus_p->frame_available = FALSE;
60  sbus_p->status = SBUS_STATUS_UNINIT;
61 
62  // Set UART parameters (100K, 8 bits, 2 stops, even parity)
65 
66  // Set polarity
67 #ifdef RC_POLARITY_GPIO_PORT
70 #endif
71 
72 }
73 
74 
76 static void decode_sbus_buffer(const uint8_t *src, uint16_t *dst, bool_t *available,
77  uint16_t *dstppm)
78 {
79  // reset counters
80  uint8_t byteInRawBuf = 0;
81  uint8_t bitInRawBuf = 0;
82  uint8_t channel = 0;
83  uint8_t bitInChannel = 0;
84 
85  // clear bits
86  memset(dst, 0, SBUS_NB_CHANNEL * sizeof(uint16_t));
87 
88  // decode sbus data
89  for (uint8_t i = 0; i < (SBUS_NB_CHANNEL * SBUS_BIT_PER_CHANNEL); i++) {
90  if (src[byteInRawBuf] & (1 << bitInRawBuf)) {
91  dst[channel] |= (1 << bitInChannel);
92  }
93 
94  bitInRawBuf++;
95  bitInChannel++;
96 
97  if (bitInRawBuf == SBUS_BIT_PER_BYTE) {
98  bitInRawBuf = 0;
99  byteInRawBuf++;
100  }
101  if (bitInChannel == SBUS_BIT_PER_CHANNEL) {
102  bitInChannel = 0;
103 #if PERIODIC_TELEMETRY
104  dstppm[channel] = USEC_OF_RC_PPM_TICKS(dst[channel]);
105 #endif
106  channel++;
107  }
108  }
109  // test frame lost flag
110  *available = !bit_is_set(src[SBUS_FLAGS_BYTE], SBUS_FRAME_LOST_BIT);
111 }
112 
113 // Decoding event function
114 // Reading from UART
115 void sbus_common_decode_event(struct Sbus *sbus_p, struct uart_periph *dev)
116 {
117  uint8_t rbyte;
118  if (uart_char_available(dev)) {
119  do {
120  rbyte = uart_getch(dev);
121  switch (sbus_p->status) {
122  case SBUS_STATUS_UNINIT:
123  // Wait for the start byte
124  if (rbyte == SBUS_START_BYTE) {
125  sbus_p->status++;
126  sbus_p->idx = 0;
127  }
128  break;
130  // Store buffer
131  sbus_p->buffer[sbus_p->idx] = rbyte;
132  sbus_p->idx++;
133  if (sbus_p->idx == SBUS_BUF_LENGTH) {
134  // Decode if last byte is the correct end byte
135  if (rbyte == SBUS_END_BYTE) {
136  decode_sbus_buffer(sbus_p->buffer, sbus_p->pulses, &sbus_p->frame_available, sbus_p->ppm);
137  }
138  sbus_p->status = SBUS_STATUS_UNINIT;
139  }
140  break;
141  default:
142  break;
143  }
144  } while (uart_char_available(dev));
145  }
146 }
unsigned short uint16_t
Definition: types.h:16
void sbus_common_decode_event(struct Sbus *sbus_p, struct uart_periph *dev)
Decoding event function.
Definition: sbus_common.c:115
#define SBUS_BIT_PER_BYTE
Definition: sbus_common.c:38
#define RC_POLARITY_GPIO_PIN
Definition: apogee_1.0.h:100
#define RC_SET_POLARITY
Set polarity using RC_POLARITY_GPIO.
Definition: sbus_common.c:53
uint16_t pulses[SBUS_NB_CHANNEL]
decoded values
Definition: sbus_common.h:79
Some architecture independent helper functions for GPIOs.
uint16_t uart_char_available(struct uart_periph *p)
Check UART for available chars in receive buffer.
Definition: uart_arch.c:296
#define SBUS_STATUS_UNINIT
Definition: sbus_common.c:42
uint8_t status
decoder state machine status
Definition: sbus_common.h:84
uint8_t idx
input index
Definition: sbus_common.h:83
#define FALSE
Definition: std.h:5
void uart_periph_set_baudrate(struct uart_periph *periph, uint32_t baud)
Definition: uart_arch.c:216
#define UPARITY_EVEN
Definition: uart.h:55
#define SBUS_END_BYTE
Definition: sbus_common.c:36
#define RC_POLARITY_GPIO_PORT
Definition: apogee_1.0.h:99
UART peripheral.
Definition: uart.h:60
#define SBUS_BUF_LENGTH
Generated code holding the description of a given transmitter.
Definition: sbus_common.h:61
#define B100000
Definition: uart_arch.h:44
void sbus_common_init(struct Sbus *sbus_p, struct uart_periph *dev)
Init function.
Definition: sbus_common.c:57
void uart_periph_set_bits_stop_parity(struct uart_periph *p, uint8_t bits, uint8_t stop, uint8_t parity)
Definition: uart_arch.c:77
#define USEC_OF_RC_PPM_TICKS(_v)
Definition: ppm_arch.h:45
Common sbus structs and defines.
uint16_t ppm[SBUS_NB_CHANNEL]
decoded and converted values
Definition: sbus_common.h:80
uint8_t uart_getch(struct uart_periph *p)
Definition: uart_arch.c:287
static void decode_sbus_buffer(const uint8_t *src, uint16_t *dst, bool_t *available, uint16_t *dstppm)
Decode the raw buffer.
Definition: sbus_common.c:76
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:69
#define UBITS_8
Definition: uart.h:48
#define SBUS_STATUS_GOT_START
Definition: sbus_common.c:43
#define SBUS_FLAGS_BYTE
Definition: sbus_common.c:39
unsigned char uint8_t
Definition: types.h:14
uint8_t buffer[SBUS_BUF_LENGTH]
input buffer
Definition: sbus_common.h:82
#define SBUS_START_BYTE
Definition: sbus_common.c:35
#define USTOP_2
Definition: uart.h:51
static uint8_t channel
Definition: ADS8344.c:80
#define SBUS_NB_CHANNEL
Definition: sbus_common.h:62
#define SBUS_FRAME_LOST_BIT
Definition: sbus_common.c:40
void gpio_setup_output(uint32_t port, uint16_t gpios)
Setup one or more pins of the given GPIO port as outputs.
Definition: gpio_ardrone.c:102
SBUS structure.
Definition: sbus_common.h:78
#define SBUS_BIT_PER_CHANNEL
Definition: sbus_common.c:37
bool_t frame_available
new frame available
Definition: sbus_common.h:81