|
24 | 24 | #include "usbd_desc.h"
|
25 | 25 | #include "wiring.h"
|
26 | 26 |
|
27 |
| -#define USB_TIMEOUT 50 |
28 | 27 | /* USB Device Core handle declaration */
|
29 | 28 | extern USBD_HandleTypeDef hUSBD_Device_CDC;
|
30 | 29 | extern __IO uint32_t lineState;
|
@@ -90,24 +89,26 @@ int USBSerial::available(void) {
|
90 | 89 | }
|
91 | 90 |
|
92 | 91 | int USBSerial::read(void) {
|
93 |
| - // Empty ReceiveQueue - nothing to return |
94 |
| - if (CDC_ReceiveQueue_ReadSize(&ReceiveQueue) <= 0) { |
95 |
| - return -1; |
96 |
| - } |
97 | 92 | // Dequeue only one char from queue
|
98 | 93 | // TS: it safe, because only main thread affects ReceiveQueue->read pos
|
99 |
| - char ch = CDC_ReceiveQueue_Dequeue(&ReceiveQueue); |
| 94 | + auto ch = CDC_ReceiveQueue_Dequeue(&ReceiveQueue); |
100 | 95 | // Resume receive process, if possible
|
101 | 96 | CDC_resume_receive();
|
102 | 97 | return ch;
|
103 | 98 | }
|
104 | 99 |
|
| 100 | +size_t USBSerial::readBytes(char *buffer, size_t length) { |
| 101 | + auto rest = static_cast<uint16_t>(length); |
| 102 | + _startMillis = millis(); |
| 103 | + do { |
| 104 | + rest -= CDC_ReceiveQueue_Read(&ReceiveQueue, reinterpret_cast<uint8_t*>(buffer), rest); |
| 105 | + if (rest == 0) return length; |
| 106 | + } while(millis() - _startMillis < _timeout); |
| 107 | + return length - rest; |
| 108 | +} |
| 109 | + |
105 | 110 | int USBSerial::peek(void)
|
106 | 111 | {
|
107 |
| - // Empty ReceiveQueue - nothing to return |
108 |
| - if (CDC_ReceiveQueue_ReadSize(&ReceiveQueue) <= 0) { |
109 |
| - return -1; |
110 |
| - } |
111 | 112 | // Peek one symbol, it can't change receive avaiablity
|
112 | 113 | return CDC_ReceiveQueue_Peek(&ReceiveQueue);
|
113 | 114 | }
|
|
0 commit comments