|
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;
|
@@ -91,24 +90,26 @@ int USBSerial::available(void) {
|
91 | 90 | }
|
92 | 91 |
|
93 | 92 | int USBSerial::read(void) {
|
94 |
| - // Empty ReceiveQueue - nothing to return |
95 |
| - if (CDC_ReceiveQueue_ReadSize(&ReceiveQueue) <= 0) { |
96 |
| - return -1; |
97 |
| - } |
98 | 93 | // Dequeue only one char from queue
|
99 | 94 | // TS: it safe, because only main thread affects ReceiveQueue->read pos
|
100 |
| - char ch = CDC_ReceiveQueue_Dequeue(&ReceiveQueue); |
| 95 | + auto ch = CDC_ReceiveQueue_Dequeue(&ReceiveQueue); |
101 | 96 | // Resume receive process, if possible
|
102 | 97 | CDC_resume_receive();
|
103 | 98 | return ch;
|
104 | 99 | }
|
105 | 100 |
|
| 101 | +size_t USBSerial::readBytes(char *buffer, size_t length) { |
| 102 | + auto rest = static_cast<uint16_t>(length); |
| 103 | + _startMillis = millis(); |
| 104 | + do { |
| 105 | + rest -= CDC_ReceiveQueue_Read(&ReceiveQueue, reinterpret_cast<uint8_t*>(buffer), rest); |
| 106 | + if (rest == 0) return length; |
| 107 | + } while(millis() - _startMillis < _timeout); |
| 108 | + return length - rest; |
| 109 | +} |
| 110 | + |
106 | 111 | int USBSerial::peek(void)
|
107 | 112 | {
|
108 |
| - // Empty ReceiveQueue - nothing to return |
109 |
| - if (CDC_ReceiveQueue_ReadSize(&ReceiveQueue) <= 0) { |
110 |
| - return -1; |
111 |
| - } |
112 | 113 | // Peek one symbol, it can't change receive avaiablity
|
113 | 114 | return CDC_ReceiveQueue_Peek(&ReceiveQueue);
|
114 | 115 | }
|
|
0 commit comments