Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
usb_serial_stm32_example1.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Michal Podhradsky
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  */
22 
31 #include <string.h>
32 
33 void send_command(void);
34 void cmd_execute(void);
35 
36 char cmd_buf[64];
38 bool cmd_avail;
40 
44 void init_usb_serial(void)
45 {
46  VCOM_init();
47  cmd_idx = 0;
48  cmd_avail = false;
49 }
50 
51 
59 {
60  if (data == -1) { return; }
61  char c = (char)data;
62 
63  if (c == '\r' || c == '\n') {
64  // command complete
65  cmd_avail = true;
66  // add termination characters and the prompt into buffer
67  VCOM_putchar('\r');
68  VCOM_putchar('\n');
70  } else {
71  // buffer command
72  cmd_buf[cmd_idx++] = c;
73  // echo char back and transmit immediately
76  }
77 }
78 
82 static inline void ReadUsbBuffer(void)
83 {
84  while (VCOM_check_available() && !cmd_avail) {
86  }
87 }
88 
93 void cmd_execute(void)
94 {
95  // copy command into tx buffer for user feedback
96  for (int i = 0; i < cmd_idx; i++) {
98  }
99 
100  if (strncmp("help", cmd_buf, cmd_idx) == 0) {
101  uint8_t response[] = " - user asked for help";
102  for (uint16_t i = 0; i < sizeof(response); i++) {
103  VCOM_putchar(response[i]);
104  }
105  } else {
106  uint8_t response[] = " Command not recognized";
107  for (uint16_t i = 0; i < sizeof(response); i++) {
108  VCOM_putchar(response[i]);
109  }
110  }
111 
112  // add termination characters and the prompt into buffer
113  VCOM_putchar('\r');
114  VCOM_putchar('\n');
116 
117  // reset counter
118  cmd_idx = 0;
119  // send complete message
121 }
122 
127 {
128  VCOM_event();
129  if (VCOM_check_available()) {
130  ReadUsbBuffer();
131  }
132  if (cmd_avail) {
133  cmd_execute();
134  cmd_avail = false;
135  }
136 }
VCOM_check_available
int VCOM_check_available(void)
Checks if data available in VCOM buffer.
Definition: usb_ser_hw.c:420
c
VIC slots used for the LPC2148 define name e g gps UART1_VIC_SLOT e g modem SPI1_VIC_SLOT SPI1 in mcu_periph spi_arch c or spi_slave_hs_arch c(and some others not using the SPI peripheral yet..) I2C0_VIC_SLOT 8 mcu_periph/i2c_arch.c I2C1_VIC_SLOT 9 mcu_periph/i2c_arch.c USB_VIC_SLOT 10 usb
prompt
uint8_t prompt
Definition: usb_serial_stm32_example1.c:39
uint16_t
unsigned short uint16_t
Definition: types.h:16
cmd_buf
char cmd_buf[64]
Definition: usb_serial_stm32_example1.c:36
VCOM_event
void VCOM_event(void)
Definition: usb_ser_hw.c:571
VCOM_send_message
void VCOM_send_message(void)
Definition: usb_ser_hw.c:574
cmd_avail
bool cmd_avail
Definition: usb_serial_stm32_example1.c:38
send_command
void send_command(void)
VCOM_getchar
int VCOM_getchar(void)
Reads one character from VCOM port.
Definition: usb_ser_hw.c:386
uint8_t
unsigned char uint8_t
Definition: types.h:14
cmd_idx
uint8_t cmd_idx
Definition: usb_serial_stm32_example1.c:37
event_usb_serial
void event_usb_serial(void)
Call VCOM_poll() from module event function.
Definition: usb_serial_stm32_example1.c:126
ReadUsbBuffer
static void ReadUsbBuffer(void)
Helper function.
Definition: usb_serial_stm32_example1.c:82
init_usb_serial
void init_usb_serial(void)
Init module, call VCOM_init() from here.
Definition: usb_serial_stm32_example1.c:44
VCOM_putchar
int VCOM_putchar(int c)
Writes one character to VCOM port.
Definition: usb_ser_hw.c:376
usb_serial_parse_packet
void usb_serial_parse_packet(int data)
Parse data from buffer Note that the function receives int, not char Because we want to be able to ca...
Definition: usb_serial_stm32_example1.c:58
usb_serial_stm32.h
VCOM_init
void VCOM_init(void)
Definition: usb_ser_hw.c:586
cmd_execute
void cmd_execute(void)
Execute command from user use strncmp.
Definition: usb_serial_stm32_example1.c:93