From 838721bb3e22fdf7add855cb7e84d4955b5a7ba2 Mon Sep 17 00:00:00 2001 From: Felix Ruess Date: Sat, 6 Dec 2014 18:20:24 +0100 Subject: [PATCH] [stm32] usb_serial: check if connected in putchar and send cleanup and rename VCOM_transmit_message to VCOM_send_message --- conf/firmwares/setup.makefile | 5 -- sw/airborne/arch/lpc21/usb_ser_hw.c | 2 +- sw/airborne/arch/stm32/usb_ser_hw.c | 74 +++++++++---------- sw/airborne/arch/stm32/usb_tunnel.c | 6 ++ sw/airborne/mcu_periph/usb_serial.h | 4 +- .../modules/com/usb_serial_stm32_example1.c | 6 +- .../modules/com/usb_serial_stm32_example2.c | 2 +- 7 files changed, 48 insertions(+), 51 deletions(-) diff --git a/conf/firmwares/setup.makefile b/conf/firmwares/setup.makefile index 5ae6900fb37..027bc77855f 100644 --- a/conf/firmwares/setup.makefile +++ b/conf/firmwares/setup.makefile @@ -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 diff --git a/sw/airborne/arch/lpc21/usb_ser_hw.c b/sw/airborne/arch/lpc21/usb_ser_hw.c index 7c9da9d1d17..4df537726f5 100644 --- a/sw/airborne/arch/lpc21/usb_ser_hw.c +++ b/sw/airborne/arch/lpc21/usb_ser_hw.c @@ -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 diff --git a/sw/airborne/arch/stm32/usb_ser_hw.c b/sw/airborne/arch/stm32/usb_ser_hw.c index 38995c8f3da..cefe51d70ae 100644 --- a/sw/airborne/arch/stm32/usb_ser_hw.c +++ b/sw/airborne/arch/stm32/usb_ser_hw.c @@ -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; - } - 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++; + 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, 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) diff --git a/sw/airborne/arch/stm32/usb_tunnel.c b/sw/airborne/arch/stm32/usb_tunnel.c index d94c25340ea..391932b1ad8 100644 --- a/sw/airborne/arch/stm32/usb_tunnel.c +++ b/sw/airborne/arch/stm32/usb_tunnel.c @@ -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); } diff --git a/sw/airborne/mcu_periph/usb_serial.h b/sw/airborne/mcu_periph/usb_serial.h index ec2ed7acdc8..ad54a885df5 100644 --- a/sw/airborne/mcu_periph/usb_serial.h +++ b/sw/airborne/mcu_periph/usb_serial.h @@ -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() diff --git a/sw/airborne/modules/com/usb_serial_stm32_example1.c b/sw/airborne/modules/com/usb_serial_stm32_example1.c index 35a55dce59b..080bca1ba3c 100644 --- a/sw/airborne/modules/com/usb_serial_stm32_example1.c +++ b/sw/airborne/modules/com/usb_serial_stm32_example1.c @@ -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(); } /** diff --git a/sw/airborne/modules/com/usb_serial_stm32_example2.c b/sw/airborne/modules/com/usb_serial_stm32_example2.c index af125312f90..c44b02fc9b0 100644 --- a/sw/airborne/modules/com/usb_serial_stm32_example2.c +++ b/sw/airborne/modules/com/usb_serial_stm32_example2.c @@ -81,7 +81,7 @@ void usb_serial_parse_packet(int data) if (c == 'R') { run = TRUE; } - VCOM_transmit_message(); + VCOM_send_message(); } /**