Skip to content

Commit

Permalink
[stm32] usb_serial: check if connected in putchar and send
Browse files Browse the repository at this point in the history
cleanup and rename VCOM_transmit_message to VCOM_send_message
  • Loading branch information
flixr committed Dec 7, 2014
1 parent 43d3b2c commit 838721b
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 51 deletions.
5 changes: 0 additions & 5 deletions conf/firmwares/setup.makefile
Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion sw/airborne/arch/lpc21/usb_ser_hw.c
Expand Up @@ -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
Expand Down
74 changes: 35 additions & 39 deletions sw/airborne/arch/stm32/usb_ser_hw.c
Expand Up @@ -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;
}

/**
Expand All @@ -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)
Expand All @@ -449,33 +454,30 @@ 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)
{
usbd_poll(my_usbd_dev);
}

/**
* 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);
}


Expand All @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions sw/airborne/arch/stm32/usb_tunnel.c
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions sw/airborne/mcu_periph/usb_serial.h
Expand Up @@ -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);

/*
Expand All @@ -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()

Expand Down
6 changes: 3 additions & 3 deletions sw/airborne/modules/com/usb_serial_stm32_example1.c
Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -116,8 +116,8 @@ void cmd_execute(void)

// reset counter
cmd_idx = 0;
// transmit message
VCOM_transmit_message();
// send complete message
VCOM_send_message();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion sw/airborne/modules/com/usb_serial_stm32_example2.c
Expand Up @@ -81,7 +81,7 @@ void usb_serial_parse_packet(int data)
if (c == 'R') {
run = TRUE;
}
VCOM_transmit_message();
VCOM_send_message();
}

/**
Expand Down

0 comments on commit 838721b

Please sign in to comment.