Paparazzi UAS  v5.15_devel-230-gc96ce27
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
usb_tunnel.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009 Martin Mueller
3  * 2014 Felix Ruess <felix.ruess@gmail.com>
4  *
5  * This file is part of paparazzi.
6  *
7  * paparazzi is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * paparazzi is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with paparazzi; see the file COPYING. If not, write to
19  * the Free Software Foundation, 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  */
23 
33 #include "std.h"
34 #include "mcu.h"
35 #include "mcu_periph/sys_time.h"
36 #include "led.h"
37 #include "mcu_periph/uart.h"
38 #include "mcu_periph/usb_serial.h"
39 
40 #ifndef USB_TUNNEL_UART
41 #error USB_TUNNEL_UART not defined. Add <configure name="TUNNEL_PORT" value="UARTx"/>
42 #endif
43 
44 PRINT_CONFIG_VAR(USB_TUNNEL_UART)
45 
46 PRINT_CONFIG_VAR(TUNNEL_RX_LED)
47 PRINT_CONFIG_VAR(TUNNEL_TX_LED)
48 
50 #define BLINK_MIN 100
51 
52 static inline void tunnel_event(void)
53 {
54  unsigned char inc;
55 
56 #if LED_AVAILABLE(TUNNEL_RX_LED)
57  static uint32_t rx_time = 0;
58  if (get_sys_time_msec() > rx_time + BLINK_MIN) {
59  LED_OFF(TUNNEL_RX_LED);
60  }
61 #endif
62 #if LED_AVAILABLE(TUNNEL_TX_LED)
63  static uint32_t tx_time = 0;
64  if (get_sys_time_msec() > tx_time + BLINK_MIN) {
65  LED_OFF(TUNNEL_TX_LED);
66  }
67 #endif
68 
69  if (uart_char_available(&USB_TUNNEL_UART) && VCOM_check_free_space(1)) {
70 #if LED_AVAILABLE(TUNNEL_RX_LED)
71  LED_ON(TUNNEL_RX_LED);
72  rx_time = get_sys_time_msec();
73 #endif
74  inc = uart_getch(&USB_TUNNEL_UART);
75  VCOM_putchar(inc);
76  }
77  long fd = 0;
78  if (VCOM_check_available() && uart_check_free_space(&USB_TUNNEL_UART, &fd, 1)) {
79 #if LED_AVAILABLE(TUNNEL_TX_LED)
80  LED_ON(TUNNEL_TX_LED);
81  tx_time = get_sys_time_msec();
82 #endif
83  inc = VCOM_getchar();
84  uart_put_byte(&USB_TUNNEL_UART, fd, inc);
85  }
86 }
87 
88 int main(void)
89 {
90  mcu_init();
91  sys_time_init();
92  led_init();
93 
95 
96  VCOM_init();
97 
99 
100  while (1) {
101  VCOM_event();
102  tunnel_event();
103  }
104 
105  return 0;
106 }
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
void mcu_init(void)
Microcontroller peripherals initialization.
Definition: mcu.c:87
uint8_t uart_getch(struct uart_periph *p)
Definition: uart_arch.c:867
uint32_t get_sys_time_msec(void)
Get the time in milliseconds since startup.
Definition: sys_time_arch.c:78
#define LED_OFF(i)
Definition: led_hw.h:52
void VCOM_event(void)
Definition: usb_ser_hw.c:571
#define mcu_int_enable()
Definition: mcu_arch.h:36
Architecture independent timing functions.
bool VCOM_check_free_space(uint16_t len)
Checks if buffer free in VCOM buffer.
Definition: usb_ser_hw.c:409
int VCOM_getchar(void)
Reads one character from VCOM port.
Definition: usb_ser_hw.c:386
unsigned long uint32_t
Definition: types.h:18
static void led_init(void)
Automatic initialization of actived LED Set to OFF at startup.
Definition: led.h:39
static void tunnel_event(void)
Definition: usb_tunnel.c:52
void sys_time_init(void)
Definition: sys_time.c:78
int uart_check_free_space(struct uart_periph *p, long *fd, uint16_t len)
Definition: uart_arch.c:976
void uart_put_byte(struct uart_periph *p, long fd, uint8_t data)
Uart transmit implementation.
Definition: uart_arch.c:994
Arch independent mcu ( Micro Controller Unit ) utilities.
int fd
Definition: serial.c:26
arch independent LED (Light Emitting Diodes) API
int VCOM_putchar(int c)
Writes one character to VCOM port.
Definition: usb_ser_hw.c:376
#define LED_ON(i)
Definition: led_hw.h:51
void VCOM_allow_linecoding(uint8_t mode)
int main(void)
Definition: usb_tunnel.c:88
int VCOM_check_available(void)
Checks if data available in VCOM buffer.
Definition: usb_ser_hw.c:420
int uart_char_available(struct uart_periph *p)
Check UART for available chars in receive buffer.
Definition: uart_arch.c:323
void VCOM_init(void)
Definition: usb_ser_hw.c:586
#define BLINK_MIN
minimum LED blink on time in ms
Definition: usb_tunnel.c:50
arch independent USB API