mirror of
https://github.com/paparazzi/paparazzi.git
synced 2026-05-10 06:59:54 +08:00
[stm32] usb_serial: check if connected in putchar and send
cleanup and rename VCOM_transmit_message to VCOM_send_message
This commit is contained in:
@@ -80,11 +80,6 @@ ifeq ($(ARCH), lpc21)
|
||||
usb_tunnel.CFLAGS += -DUSE_USB_HIGH_PCLK
|
||||
usb_tunnel.srcs += $(SRC_ARCH)/lpcusb/usbhw_lpc.c $(SRC_ARCH)/lpcusb/usbinit.c
|
||||
usb_tunnel.srcs += $(SRC_ARCH)/lpcusb/usbcontrol.c $(SRC_ARCH)/lpcusb/usbstdreq.c
|
||||
else ifeq($(ARCH), stm32)
|
||||
else
|
||||
ifeq ($(TARGET),usb_tunnel)
|
||||
$(error usb_tunnel is only implemented for lpc21 and stm32)
|
||||
endif
|
||||
endif
|
||||
|
||||
|
||||
|
||||
@@ -567,7 +567,7 @@ static void usb_serial_send(struct usb_serial_periph* p __attribute__((unused)))
|
||||
void VCOM_event(void) {}
|
||||
|
||||
// Empty for lpc21
|
||||
void VCOM_transmit_message(void) {}
|
||||
void VCOM_send_message(void) {}
|
||||
|
||||
void VCOM_init(void) {
|
||||
// initialise stack
|
||||
|
||||
@@ -398,24 +398,29 @@ int fifo_free(fifo_t *fifo)
|
||||
|
||||
|
||||
/**
|
||||
* Writes one character to VCOM port
|
||||
* Writes one character to VCOM port fifo.
|
||||
*
|
||||
* Since we don't really have an instant feeedback from USB driver if
|
||||
* the character was written, we always return c if we are connected.
|
||||
*
|
||||
* @param [in] c character to write
|
||||
* @returns character to be written
|
||||
* Note we don't really have an instant feeedback from USB driver if
|
||||
* the character was written, so we always return c
|
||||
* @returns character to be written, -1 if not usb is not connected
|
||||
*/
|
||||
int VCOM_putchar(int c)
|
||||
{
|
||||
// check if there are at least two more bytes left in queue
|
||||
if (VCOM_check_free_space(2)) {
|
||||
// if yes, add char
|
||||
fifo_put(&txfifo, c);
|
||||
} else {
|
||||
// less than 2 bytes available, add byte and send data now
|
||||
fifo_put(&txfifo, c);
|
||||
VCOM_transmit_message();
|
||||
if (usb_connected) {
|
||||
// check if there are at least two more bytes left in queue
|
||||
if (VCOM_check_free_space(2)) {
|
||||
// if yes, add char
|
||||
fifo_put(&txfifo, c);
|
||||
} else {
|
||||
// less than 2 bytes available, add byte and send data now
|
||||
fifo_put(&txfifo, c);
|
||||
VCOM_send_message();
|
||||
}
|
||||
return c;
|
||||
}
|
||||
return c;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -440,7 +445,7 @@ bool_t VCOM_check_free_space(uint8_t len)
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if data available in VCOM buffer
|
||||
* Checks if data available in VCOM buffer.
|
||||
* @returns nonzero if char is available in the queue, zero otherwise
|
||||
*/
|
||||
int VCOM_check_available(void)
|
||||
@@ -449,8 +454,8 @@ int VCOM_check_available(void)
|
||||
}
|
||||
|
||||
/**
|
||||
* Poll usb (required by libopencm3)
|
||||
* VCOM_poll() should be called from module event function
|
||||
* Poll usb (required by libopencm3).
|
||||
* VCOM_event() should be called from main/module event function
|
||||
*/
|
||||
void VCOM_event(void)
|
||||
{
|
||||
@@ -458,24 +463,21 @@ void VCOM_event(void)
|
||||
}
|
||||
|
||||
/**
|
||||
* Send data from fifo right now (up to MAX_PACKET_SIZE)
|
||||
* Send data from fifo right now.
|
||||
* Only if usb is connected.
|
||||
*/
|
||||
void VCOM_transmit_message()
|
||||
void VCOM_send_message(void)
|
||||
{
|
||||
/*
|
||||
if (len > MAX_PACKET_SIZE) {
|
||||
len = MAX_PACKET_SIZE;
|
||||
if (usb_connected) {
|
||||
uint8_t buf[MAX_PACKET_SIZE];
|
||||
uint8_t i;
|
||||
for (i = 0; i < MAX_PACKET_SIZE; i++) {
|
||||
if (!fifo_get(&txfifo, &buf[i])) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
usbd_ep_write_packet(my_usbd_dev, 0x82, buf, i);
|
||||
}
|
||||
usbd_ep_write_packet(my_usbd_dev, 0x82, buf, len);
|
||||
*/
|
||||
uint8_t buf[MAX_PACKET_SIZE];
|
||||
uint8_t data;
|
||||
uint16_t idx = 0;
|
||||
while (fifo_get(&txfifo, &data) && (idx < MAX_PACKET_SIZE)) {
|
||||
buf[idx] = data;
|
||||
idx++;
|
||||
}
|
||||
usbd_ep_write_packet(my_usbd_dev, 0x82, buf, idx);
|
||||
}
|
||||
|
||||
|
||||
@@ -500,20 +502,14 @@ static int usb_serial_check_free_space(struct usb_serial_periph *p __attribute__
|
||||
return (int)VCOM_check_free_space(len);
|
||||
}
|
||||
|
||||
// Only transmit when USB is connected
|
||||
static void usb_serial_transmit(struct usb_serial_periph *p __attribute__((unused)), uint8_t byte)
|
||||
{
|
||||
if (usb_connected) {
|
||||
VCOM_putchar(byte);
|
||||
}
|
||||
VCOM_putchar(byte);
|
||||
}
|
||||
|
||||
// Only send message when USB is connected
|
||||
static void usb_serial_send(struct usb_serial_periph *p __attribute__((unused)))
|
||||
{
|
||||
if (usb_connected) {
|
||||
VCOM_transmit_message();
|
||||
}
|
||||
VCOM_send_message();
|
||||
}
|
||||
|
||||
void VCOM_init(void)
|
||||
|
||||
@@ -49,10 +49,16 @@ static void tunnel_event(void)
|
||||
static unsigned char inc;
|
||||
|
||||
if (uart_char_available(&USB_TUNNEL_UART) && VCOM_check_free_space(1)) {
|
||||
#if USE_LED_2
|
||||
LED_TOGGLE(2);
|
||||
#endif
|
||||
inc = uart_getch(&USB_TUNNEL_UART);
|
||||
VCOM_putchar(inc);
|
||||
}
|
||||
if (VCOM_check_available() && uart_check_free_space(&USB_TUNNEL_UART, 1)) {
|
||||
#if USE_LED_3
|
||||
LED_TOGGLE(3);
|
||||
#endif
|
||||
inc = VCOM_getchar();
|
||||
uart_transmit(&USB_TUNNEL_UART, inc);
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ bool_t VCOM_check_free_space(uint8_t len);
|
||||
int VCOM_check_available(void);
|
||||
void VCOM_set_linecoding(uint8_t mode);
|
||||
void VCOM_allow_linecoding(uint8_t mode);
|
||||
void VCOM_transmit_message(void);
|
||||
void VCOM_send_message(void);
|
||||
void VCOM_event(void);
|
||||
|
||||
/*
|
||||
@@ -56,7 +56,7 @@ void VCOM_event(void);
|
||||
#define UsbSInit() VCOM_init()
|
||||
#define UsbSCheckFreeSpace(_x) VCOM_check_free_space(_x)
|
||||
#define UsbSTransmit(_x) VCOM_putchar(_x)
|
||||
#define UsbSSendMessage() VCOM_transmit_message()
|
||||
#define UsbSSendMessage() VCOM_send_message()
|
||||
#define UsbSGetch() VCOM_getchar()
|
||||
#define UsbSChAvailable() VCOM_check_available()
|
||||
|
||||
|
||||
@@ -72,7 +72,7 @@ void usb_serial_parse_packet(int data)
|
||||
cmd_buf[cmd_idx++] = c;
|
||||
// echo char back and transmit immediately
|
||||
VCOM_putchar((uint8_t)c);
|
||||
VCOM_transmit_message();
|
||||
VCOM_send_message();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,8 +116,8 @@ void cmd_execute(void)
|
||||
|
||||
// reset counter
|
||||
cmd_idx = 0;
|
||||
// transmit message
|
||||
VCOM_transmit_message();
|
||||
// send complete message
|
||||
VCOM_send_message();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -81,7 +81,7 @@ void usb_serial_parse_packet(int data)
|
||||
if (c == 'R') {
|
||||
run = TRUE;
|
||||
}
|
||||
VCOM_transmit_message();
|
||||
VCOM_send_message();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user