Paparazzi UAS  v6.2_unstable
Paparazzi is a free software Unmanned Aircraft System.
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 
49 
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 
98 
99  while (1) {
100  VCOM_event();
101  tunnel_event();
102  }
103 
104  return 0;
105 }
uint32_t
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78
usb_serial.h
arch independent USB API
uart_getch
uint8_t uart_getch(struct uart_periph *p)
Definition: uart_arch.c:968
led_init
static void led_init(void)
Automatic initialization of actived LED Set to OFF at startup.
Definition: led.h:39
LED_OFF
#define LED_OFF(i)
Definition: led_hw.h:52
sys_time_init
void sys_time_init(void)
Definition: sys_time.c:92
uart_char_available
int uart_char_available(struct uart_periph *p)
Check UART for available chars in receive buffer.
Definition: uart_arch.c:323
VCOM_init
void VCOM_init(void)
Definition: usb_ser_hw.c:565
VCOM_check_free_space
bool VCOM_check_free_space(uint16_t len)
Checks if buffer free in VCOM buffer.
Definition: usb_ser_hw.c:450
LED_ON
#define LED_ON(i)
Definition: led_hw.h:51
VCOM_check_available
int VCOM_check_available(void)
Checks if data available in VCOM buffer.
Definition: usb_ser_hw.c:459
BLINK_MIN
#define BLINK_MIN
minimum LED blink on time in ms
Definition: usb_tunnel.c:50
VCOM_putchar
int VCOM_putchar(int c)
Writes one character to VCOM port fifo.
Definition: usb_ser_hw.c:397
std.h
uart.h
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
main
int main(void)
Definition: usb_tunnel.c:88
VCOM_event
void VCOM_event(void)
Poll usb (required by libopencm3).
Definition: usb_ser_hw.c:468
mcu.h
Arch independent mcu ( Micro Controller Unit ) utilities.
VCOM_getchar
int VCOM_getchar(void)
Reads one character from VCOM port.
Definition: usb_ser_hw.c:425
uart_check_free_space
int uart_check_free_space(struct uart_periph *p, long *fd, uint16_t len)
Definition: uart_arch.c:1078
sys_time.h
Architecture independent timing functions.
get_sys_time_msec
uint32_t get_sys_time_msec(void)
Get the time in milliseconds since startup.
Definition: sys_time_arch.c:86
uart_put_byte
void uart_put_byte(struct uart_periph *p, long fd, uint8_t data)
Uart transmit implementation.
Definition: uart_arch.c:1096
led.h
arch independent LED (Light Emitting Diodes) API
fd
int fd
Definition: serial.c:26
VCOM_allow_linecoding
void VCOM_allow_linecoding(uint8_t mode)
tunnel_event
static void tunnel_event(void)
Definition: usb_tunnel.c:52
mcu_init
void mcu_init(void)
Microcontroller peripherals initialization.
Definition: mcu.c:97