[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:
Felix Ruess
2014-12-06 18:20:24 +01:00
parent 43d3b2c1e0
commit 838721bb3e
7 changed files with 48 additions and 51 deletions
-5
View File
@@ -80,11 +80,6 @@ ifeq ($(ARCH), lpc21)
usb_tunnel.CFLAGS += -DUSE_USB_HIGH_PCLK 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/usbhw_lpc.c $(SRC_ARCH)/lpcusb/usbinit.c
usb_tunnel.srcs += $(SRC_ARCH)/lpcusb/usbcontrol.c $(SRC_ARCH)/lpcusb/usbstdreq.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 endif
+1 -1
View File
@@ -567,7 +567,7 @@ static void usb_serial_send(struct usb_serial_periph* p __attribute__((unused)))
void VCOM_event(void) {} void VCOM_event(void) {}
// Empty for lpc21 // Empty for lpc21
void VCOM_transmit_message(void) {} void VCOM_send_message(void) {}
void VCOM_init(void) { void VCOM_init(void) {
// initialise stack // initialise stack
+35 -39
View File
@@ -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 * @param [in] c character to write
* @returns character to be written * @returns character to be written, -1 if not usb is not connected
* Note we don't really have an instant feeedback from USB driver if
* the character was written, so we always return c
*/ */
int VCOM_putchar(int c) int VCOM_putchar(int c)
{ {
// check if there are at least two more bytes left in queue if (usb_connected) {
if (VCOM_check_free_space(2)) { // check if there are at least two more bytes left in queue
// if yes, add char if (VCOM_check_free_space(2)) {
fifo_put(&txfifo, c); // if yes, add char
} else { fifo_put(&txfifo, c);
// less than 2 bytes available, add byte and send data now } else {
fifo_put(&txfifo, c); // less than 2 bytes available, add byte and send data now
VCOM_transmit_message(); 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 * @returns nonzero if char is available in the queue, zero otherwise
*/ */
int VCOM_check_available(void) int VCOM_check_available(void)
@@ -449,8 +454,8 @@ int VCOM_check_available(void)
} }
/** /**
* Poll usb (required by libopencm3) * Poll usb (required by libopencm3).
* VCOM_poll() should be called from module event function * VCOM_event() should be called from main/module event function
*/ */
void VCOM_event(void) 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 (usb_connected) {
if (len > MAX_PACKET_SIZE) { uint8_t buf[MAX_PACKET_SIZE];
len = 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); 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) 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))) static void usb_serial_send(struct usb_serial_periph *p __attribute__((unused)))
{ {
if (usb_connected) { VCOM_send_message();
VCOM_transmit_message();
}
} }
void VCOM_init(void) void VCOM_init(void)
+6
View File
@@ -49,10 +49,16 @@ static void tunnel_event(void)
static unsigned char inc; static unsigned char inc;
if (uart_char_available(&USB_TUNNEL_UART) && VCOM_check_free_space(1)) { 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); inc = uart_getch(&USB_TUNNEL_UART);
VCOM_putchar(inc); VCOM_putchar(inc);
} }
if (VCOM_check_available() && uart_check_free_space(&USB_TUNNEL_UART, 1)) { if (VCOM_check_available() && uart_check_free_space(&USB_TUNNEL_UART, 1)) {
#if USE_LED_3
LED_TOGGLE(3);
#endif
inc = VCOM_getchar(); inc = VCOM_getchar();
uart_transmit(&USB_TUNNEL_UART, inc); uart_transmit(&USB_TUNNEL_UART, inc);
} }
+2 -2
View File
@@ -46,7 +46,7 @@ bool_t VCOM_check_free_space(uint8_t len);
int VCOM_check_available(void); int VCOM_check_available(void);
void VCOM_set_linecoding(uint8_t mode); void VCOM_set_linecoding(uint8_t mode);
void VCOM_allow_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); void VCOM_event(void);
/* /*
@@ -56,7 +56,7 @@ void VCOM_event(void);
#define UsbSInit() VCOM_init() #define UsbSInit() VCOM_init()
#define UsbSCheckFreeSpace(_x) VCOM_check_free_space(_x) #define UsbSCheckFreeSpace(_x) VCOM_check_free_space(_x)
#define UsbSTransmit(_x) VCOM_putchar(_x) #define UsbSTransmit(_x) VCOM_putchar(_x)
#define UsbSSendMessage() VCOM_transmit_message() #define UsbSSendMessage() VCOM_send_message()
#define UsbSGetch() VCOM_getchar() #define UsbSGetch() VCOM_getchar()
#define UsbSChAvailable() VCOM_check_available() #define UsbSChAvailable() VCOM_check_available()
@@ -72,7 +72,7 @@ void usb_serial_parse_packet(int data)
cmd_buf[cmd_idx++] = c; cmd_buf[cmd_idx++] = c;
// echo char back and transmit immediately // echo char back and transmit immediately
VCOM_putchar((uint8_t)c); VCOM_putchar((uint8_t)c);
VCOM_transmit_message(); VCOM_send_message();
} }
} }
@@ -116,8 +116,8 @@ void cmd_execute(void)
// reset counter // reset counter
cmd_idx = 0; cmd_idx = 0;
// transmit message // send complete message
VCOM_transmit_message(); VCOM_send_message();
} }
/** /**
@@ -81,7 +81,7 @@ void usb_serial_parse_packet(int data)
if (c == 'R') { if (c == 'R') {
run = TRUE; run = TRUE;
} }
VCOM_transmit_message(); VCOM_send_message();
} }
/** /**