2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
25 #include "build_config.h"
29 #include "hw_config.h"
31 #include "drivers/system.h"
34 #include "serial_usb_vcp.h"
37 #define USB_TIMEOUT 50
39 static vcpPort_t vcpPort
;
41 void usbVcpSetBaudRate(serialPort_t
*instance
, uint32_t baudRate
)
49 void usbVcpSetMode(serialPort_t
*instance
, portMode_t mode
)
57 bool isUsbVcpTransmitBufferEmpty(serialPort_t
*instance
)
63 uint8_t usbVcpAvailable(serialPort_t
*instance
)
67 return receiveLength
& 0xFF; // FIXME use uint32_t return type everywhere
70 uint8_t usbVcpRead(serialPort_t
*instance
)
79 rxed
+= CDC_Receive_DATA((uint8_t*)buf
+ rxed
, 1 - rxed
);
85 void usbVcpWrite(serialPort_t
*instance
, uint8_t c
)
90 uint32_t start
= millis();
92 if (!(usbIsConnected() && usbIsConfigured())) {
97 txed
= CDC_Send_DATA((uint8_t*)&c
, 1);
98 } while (txed
< 1 && (millis() - start
< USB_TIMEOUT
));
102 const struct serialPortVTable usbVTable
[] = { { usbVcpWrite
, usbVcpAvailable
, usbVcpRead
, usbVcpSetBaudRate
, isUsbVcpTransmitBufferEmpty
, usbVcpSetMode
} };
104 serialPort_t
*usbVcpOpen(void)
110 USB_Interrupts_Config();
114 s
->port
.vTable
= usbVTable
;
116 return (serialPort_t
*)s
;