1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-25 17:25:20 +03:00

minor cleanup and a little extra safety

This commit is contained in:
blckmn 2016-07-17 07:56:55 +10:00
parent 187c41cfd1
commit e7eb534401

View file

@ -77,13 +77,10 @@ static uint8_t usbVcpRead(serialPort_t *instance)
uint8_t buf[1]; uint8_t buf[1];
uint32_t rxed = 0; while (true) {
if (CDC_Receive_DATA(buf, 1))
while (rxed < 1) { return buf[0];
rxed += CDC_Receive_DATA((uint8_t*)buf + rxed, 1 - rxed);
} }
return buf[0];
} }
static void usbVcpWriteBuf(serialPort_t *instance, void *data, int count) static void usbVcpWriteBuf(serialPort_t *instance, void *data, int count)
@ -95,8 +92,10 @@ static void usbVcpWriteBuf(serialPort_t *instance, void *data, int count)
} }
uint32_t start = millis(); uint32_t start = millis();
for (uint8_t *p = data; count > 0; ) { uint8_t *p = data;
uint32_t txed = CDC_Send_DATA(p, count); uint32_t txed = 0;
while (count > 0) {
txed = CDC_Send_DATA(p, count);
count -= txed; count -= txed;
p += txed; p += txed;
@ -119,14 +118,19 @@ static bool usbVcpFlush(vcpPort_t *port)
return false; return false;
} }
uint8_t txed = 0;
uint32_t start = millis(); uint32_t start = millis();
uint8_t *p = port->txBuf;
uint32_t txed = 0;
while (count > 0) {
txed = CDC_Send_DATA(p, count);
count -= txed;
p += txed;
do { if (millis() - start > USB_TIMEOUT) {
txed += CDC_Send_DATA(&port->txBuf[txed], count-txed); break;
} while (txed < count && (millis() - start < USB_TIMEOUT)); }
}
return txed == count; return count == 0;
} }
static void usbVcpWrite(serialPort_t *instance, uint8_t c) static void usbVcpWrite(serialPort_t *instance, uint8_t c)