Paparazzi UAS  v5.2.2_stable-0-gd6b9f29
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures 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, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
29 #include BOARD_CONFIG
30 #include "mcu_periph/gpio.h"
31 #include <string.h>
32 
33 /*
34  * SBUS protocol and state machine status
35  */
36 #define SBUS_START_BYTE 0x0f
37 #define SBUS_END_BYTE 0x00
38 #define SBUS_BIT_PER_CHANNEL 11
39 #define SBUS_BIT_PER_BYTE 8
40 #define SBUS_FLAGS_BYTE 22
41 #define SBUS_FRAME_LOST_BIT 2
42 
43 #define SBUS_STATUS_UNINIT 0
44 #define SBUS_STATUS_GOT_START 1
45 
53 #ifndef RC_SET_POLARITY
54 #define RC_SET_POLARITY gpio_set
55 #endif
56 
57 
58 void sbus_common_init(struct _sbus* sbus_p, struct uart_periph* dev) {
59 
60  sbus_p->frame_available = FALSE;
61  sbus_p->status = SBUS_STATUS_UNINIT;
62 
63  // Set UART parameters (100K, 8 bits, 2 stops, even parity)
66 
67  // Set polarity
68 #ifdef RC_POLARITY_GPIO_PORT
71 #endif
72 
73 }
74 
75 
77 static void decode_sbus_buffer (const uint8_t *src, uint16_t *dst, bool_t *available, 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  bitInRawBuf++;
94  bitInChannel++;
95 
96  if (bitInRawBuf == SBUS_BIT_PER_BYTE) {
97  bitInRawBuf = 0;
98  byteInRawBuf++;
99  }
100  if (bitInChannel == SBUS_BIT_PER_CHANNEL) {
101  bitInChannel = 0;
102 #if PERIODIC_TELEMETRY
103  dstppm[channel] = USEC_OF_RC_PPM_TICKS(dst[channel]);
104 #endif
105  channel++;
106  }
107  }
108  // test frame lost flag
109  *available = !bit_is_set(src[SBUS_FLAGS_BYTE], SBUS_FRAME_LOST_BIT);
110 }
111 
112 // Decoding event function
113 // Reading from UART
114 void sbus_common_decode_event(struct _sbus* sbus_p, struct uart_periph* dev) {
115  uint8_t rbyte;
116  if (uart_char_available(dev)) {
117  do {
118  rbyte = uart_getch(dev);
119  switch (sbus_p->status) {
120  case SBUS_STATUS_UNINIT:
121  // Wait for the start byte
122  if (rbyte == SBUS_START_BYTE) {
123  sbus_p->status++;
124  sbus_p->idx = 0;
125  }
126  break;
128  // Store buffer
129  sbus_p->buffer[sbus_p->idx] = rbyte;
130  sbus_p->idx++;
131  if (sbus_p->idx == SBUS_BUF_LENGTH) {
132  // Decode if last byte is the correct end byte
133  if (rbyte == SBUS_END_BYTE) {
134  decode_sbus_buffer(sbus_p->buffer, sbus_p->pulses, &sbus_p->frame_available, sbus_p->ppm);
135  }
136  sbus_p->status = SBUS_STATUS_UNINIT;
137  }
138  break;
139  default:
140  break;
141  }
142  } while (uart_char_available(dev));
143  }
144 }
unsigned short uint16_t
Definition: types.h:16
uint8_t uart_getch(struct uart_periph *p)
Definition: uart.c:204
#define SBUS_BIT_PER_BYTE
Definition: sbus_common.c:39
#define RC_POLARITY_GPIO_PIN
Definition: apogee_1.0.h:100
static uint16_t uart_char_available(struct uart_periph *p)
Check UART for available chars in receive buffer.
Definition: uart.h:87
SBUS structure.
Definition: sbus_common.h:65
#define RC_SET_POLARITY
Set polarity using RC_POLARITY_GPIO.
Definition: sbus_common.c:54
Some architecture independent helper functions for GPIOs.
uint8_t status
decoder state machine status
Definition: sbus_common.h:71
#define SBUS_STATUS_UNINIT
Definition: sbus_common.c:43
#define FALSE
Definition: imu_chimu.h:141
#define UPARITY_EVEN
Definition: uart.h:50
#define SBUS_END_BYTE
Definition: sbus_common.c:37
UART peripheral.
Definition: uart.h:55
uint16_t ppm[SBUS_NB_CHANNEL]
decoded and converted values
Definition: sbus_common.h:67
uint8_t buffer[SBUS_BUF_LENGTH]
input buffer
Definition: sbus_common.h:69
uint16_t pulses[SBUS_NB_CHANNEL]
decoded values
Definition: sbus_common.h:66
#define RC_POLARITY_GPIO_PORT
Definition: apogee_1.0.h:99
#define SBUS_BUF_LENGTH
Generated code holding the description of a given transmitter.
Definition: sbus_common.h:58
#define B100000
Definition: uart_arch.h:44
void sbus_common_decode_event(struct _sbus *sbus_p, struct uart_periph *dev)
Decoding event function.
Definition: sbus_common.c:114
void uart_periph_set_bits_stop_parity(struct uart_periph *p, uint8_t bits, uint8_t stop, uint8_t parity)
Definition: uart_arch.c:71
#define USEC_OF_RC_PPM_TICKS(_v)
Definition: ppm_arch.h:39
Futaba SBUS decoder.
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:77
bool_t frame_available
new frame available
Definition: sbus_common.h:68
#define UBITS_8
Definition: uart.h:43
#define SBUS_STATUS_GOT_START
Definition: sbus_common.c:44
#define SBUS_FLAGS_BYTE
Definition: sbus_common.c:40
unsigned char uint8_t
Definition: types.h:14
static void gpio_setup_output(uint32_t port, uint32_t gpios)
Setup one or more pins of the given GPIO port as outputs.
Definition: gpio_arch.h:76
#define SBUS_START_BYTE
Definition: sbus_common.c:36
#define USTOP_2
Definition: uart.h:46
static uint8_t channel
Definition: ADS8344.c:80
#define SBUS_NB_CHANNEL
Definition: sbus_common.h:59
#define SBUS_FRAME_LOST_BIT
Definition: sbus_common.c:41
#define SBUS_BIT_PER_CHANNEL
Definition: sbus_common.c:38
uint8_t idx
input index
Definition: sbus_common.h:70
void sbus_common_init(struct _sbus *sbus_p, struct uart_periph *dev)
Init function.
Definition: sbus_common.c:58
void uart_periph_set_baudrate(struct uart_periph *p, uint32_t baud)
Definition: uart_arch.c:65