Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
generic_uart.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 Freek van Tienen <freek.v.tienen@gmail.com>
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 
29 #include "mcu_periph/uart.h"
30 
31 /* Default end character */
32 #ifndef GENERIC_UART_ENDCHAR
33 #define GENERIC_UART_ENDCHAR '>'
34 #endif
35 
36 /* Default max sending message */
37 #ifndef GENERIC_UART_MAX_SENDLEN
38 #define GENERIC_UART_MAX_SENDLEN 64
39 #endif
40 
41 /* Default max buffer size */
42 #ifndef GENERIC_UART_MAX_BUFSIZE
43 #define GENERIC_UART_MAX_BUFSIZE 128
44 #endif
45 
46 /* Main variables */
47 static struct link_device *gen_uart_dev = (&((GENERIC_UART_PORT).device));
48 
49 /* Event function to read UART message and forward to downlink */
50 void generic_uart_event(void) {
51  // Receive buffer
52  static uint8_t gen_msg_buf[GENERIC_UART_MAX_BUFSIZE];
53  static uint8_t gen_msg_cnt = 0;
54 
55  // Look for data on serial port and save it in the buffer
56  while (gen_msg_cnt < GENERIC_UART_MAX_BUFSIZE && gen_uart_dev->char_available(gen_uart_dev->periph)) {
57  gen_msg_buf[gen_msg_cnt++] = gen_uart_dev->get_byte(gen_uart_dev->periph);
58 
59  if(gen_msg_buf[gen_msg_cnt-1] == GENERIC_UART_ENDCHAR)
60  break;
61  }
62 
63  // Forward the message to the GCS
64  if(gen_msg_buf[gen_msg_cnt-1] == GENERIC_UART_ENDCHAR || gen_msg_cnt > GENERIC_UART_MAX_SENDLEN) {
65  DOWNLINK_SEND_PAYLOAD(DefaultChannel, DefaultDevice, gen_msg_cnt, gen_msg_buf);
66 
67  gen_msg_buf[0] = 0;
68  gen_msg_cnt = 0;
69  }
70 }
#define GENERIC_UART_MAX_BUFSIZE
Definition: generic_uart.c:43
#define GENERIC_UART_MAX_SENDLEN
Definition: generic_uart.c:38
static struct link_device * gen_uart_dev
UART device for communication.
Definition: generic_uart.c:47
#define GENERIC_UART_ENDCHAR
Definition: generic_uart.c:33
void generic_uart_event(void)
Definition: generic_uart.c:50
static int char_available(struct linkdev *d)
Periodic telemetry system header (includes downlink utility and generated code).
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98