make USB TX buffer less lock prone

This commit is contained in:
King Kévin 2016-01-20 22:28:22 +01:00
parent af8aedd1f6
commit 7f71c9b828
1 changed files with 14 additions and 7 deletions

View File

@ -168,7 +168,7 @@ static const struct usb_config_descriptor config = {
.bNumInterfaces = 2, // the number of interfaces in this configuration
.bConfigurationValue = 1, // the index of this configuration
.iConfiguration = 0, // a string index describing this configuration (zero means not provided)
.bmAttributes = 0x80, // self powered (0<<6), supports remote wakeup (5<<0)
.bmAttributes = 0x80, // self powered (0<<6), supports remote wakeup (0<<5)
.bMaxPower = 0x32, // the maximum amount of current that this device will draw in 2mA units
// end of header
.interface = interfaces, // pointer to an array of interfaces
@ -264,12 +264,16 @@ static void cdcacm_data_tx_cb(usbd_device *usbd_dev, uint8_t ep)
/* transmit data */
if (tx_used) { // copy received data
for (usb_length=0; usb_length<sizeof(usb_data) && tx_used; usb_length++) { // only until buffer is full
usb_data[usb_length] = tx_buffer[tx_i]; // put data in transmit data
tx_i = (tx_i+1)%sizeof(rx_buffer); // update location on buffer
tx_used--; // update used size
for (usb_length=0; usb_length<sizeof(usb_data) && usb_length<tx_used; usb_length++) { // only until buffer is full
usb_data[usb_length] = tx_buffer[(tx_i+usb_length)%sizeof(tx_buffer)]; // put data in transmit data
}
while(usbd_ep_write_packet(usb_device, 0x82, usb_data, usb_length)==0);
// this could lead to a lock down
// while(usbd_ep_write_packet(usb_device, 0x82, usb_data, usb_length)==0);
// this is less critical
uint8_t transmitted = usbd_ep_write_packet(usb_device, 0x82, usb_data, usb_length); // try to transmit data
tx_i = (tx_i+transmitted)%sizeof(rx_buffer); // update location on buffer
tx_used -= transmitted; // update used size
}
}
@ -328,9 +332,12 @@ char cdcacm_getchar(void)
/* put character on USB CDC ACM (blocking) */
void cdcacm_putchar(char c)
{
if (tx_used<sizeof(tx_buffer)) {
if (tx_used<sizeof(tx_buffer)) { // buffer not full
tx_buffer[(tx_i+tx_used)%sizeof(tx_buffer)] = c; // put character in buffer
tx_used++; // update used buffer
} else { // buffer full
tx_i = (tx_i+1)%sizeof(tx_buffer); // shift start
tx_buffer[(tx_i+tx_used)%sizeof(tx_buffer)] = c; // overwrite old data
}
usbd_ep_write_packet(usb_device, 0x82, NULL, 0); // trigger tx (not sure why cdcacm_data_tx_cb doesn't work else)
}