|
24 | 24 | #include "usbd_desc.h"
|
25 | 25 | #include "wiring.h"
|
26 | 26 |
|
27 |
| -#define USB_TIMEOUT 50 |
28 |
| -/* USB Device Core handle declaration */ |
29 |
| -extern USBD_HandleTypeDef hUSBD_Device_CDC; |
30 |
| -extern __IO uint32_t device_connection_status; |
31 | 27 | extern __IO uint32_t lineState;
|
32 |
| -extern __IO uint8_t UserTxBuffer[APP_TX_DATA_SIZE]; |
33 |
| -extern __IO uint8_t UserRxBuffer[APP_RX_DATA_SIZE]; |
34 |
| -extern __IO uint32_t UserTxBufPtrIn; |
35 |
| -extern __IO uint32_t UserTxBufPtrOut; |
36 |
| -extern __IO uint32_t UserRxBufPtrIn; |
37 |
| -extern __IO uint32_t UserRxBufPtrOut; |
38 | 28 |
|
39 | 29 | USBSerial SerialUSB;
|
40 | 30 | void serialEventUSB() __attribute__((weak));
|
41 | 31 |
|
| 32 | +void USBSerial::begin(void) { |
| 33 | + CDC_init(); |
| 34 | +} |
| 35 | + |
42 | 36 | void USBSerial::begin(uint32_t /* baud_count */) {
|
43 | 37 | // uart config is ignored in USB-CDC
|
| 38 | + begin(); |
44 | 39 | }
|
45 | 40 |
|
46 | 41 | void USBSerial::begin(uint32_t /* baud_count */, uint8_t /* config */) {
|
47 | 42 | // uart config is ignored in USB-CDC
|
| 43 | + begin(); |
48 | 44 | }
|
49 | 45 |
|
50 |
| -void USBSerial::end(void) { |
51 |
| - |
52 |
| - USBD_LL_DeInit(&hUSBD_Device_CDC); |
| 46 | +void USBSerial::end() { |
| 47 | + CDC_deInit(); |
53 | 48 | }
|
54 | 49 |
|
55 |
| -int USBSerial::availableForWrite(void) |
| 50 | +int USBSerial::availableForWrite() |
56 | 51 | {
|
57 |
| - int ret_val; |
58 |
| - |
59 |
| - /* UserTxBufPtrOut can be modified by TIM ISR, so in order to be sure that the */ |
60 |
| - /* value that we read is correct, we need to disable TIM Interrupt. */ |
61 |
| - CDC_disable_TIM_Interrupt(); |
62 |
| - |
63 |
| - if (UserTxBufPtrIn >= UserTxBufPtrOut) { |
64 |
| - ret_val = (APP_TX_DATA_SIZE - 1 - UserTxBufPtrIn + UserTxBufPtrOut); |
65 |
| - } else { |
66 |
| - ret_val = (UserTxBufPtrOut - UserTxBufPtrIn - 1); |
67 |
| - } |
68 |
| - |
69 |
| - CDC_enable_TIM_Interrupt(); |
70 |
| - |
71 |
| - return ret_val; |
| 52 | + // Just transmit queue size, available for write |
| 53 | + return static_cast<int>(CDC_TransmitQueue_WriteSize(&TransmitQueue)); |
72 | 54 | }
|
73 | 55 |
|
74 | 56 | size_t USBSerial::write(uint8_t ch) {
|
75 |
| - |
76 |
| - /* UserTxBufPtrOut can be modified by TIM ISR, so in order to be sure that the */ |
77 |
| - /* value that we read is correct, we need to disable TIM Interrupt. */ |
78 |
| - CDC_disable_TIM_Interrupt(); |
79 |
| - |
80 |
| - if (((UserTxBufPtrIn + 1) % APP_TX_DATA_SIZE) == UserTxBufPtrOut) { |
81 |
| - // Buffer full!!! Force a flush to not loose data and go on |
82 |
| - CDC_flush(); |
83 |
| - } |
84 |
| - UserTxBuffer[UserTxBufPtrIn] = ch; |
85 |
| - UserTxBufPtrIn = ((UserTxBufPtrIn + 1) % APP_TX_DATA_SIZE); |
86 |
| - |
87 |
| - CDC_enable_TIM_Interrupt(); |
88 |
| - |
89 |
| - return 1; |
| 57 | + // Just write single-byte buffer. |
| 58 | + return write(&ch, 1); |
90 | 59 | }
|
91 | 60 |
|
92 | 61 | size_t USBSerial::write(const uint8_t *buffer, size_t size){
|
93 |
| - size_t i = 0; |
94 |
| - for (i=0; i < size; i++) { |
95 |
| - if (write(buffer[i]) != 1) { |
96 |
| - break; |
97 |
| - } |
| 62 | + size_t rest = size; |
| 63 | + while(rest > 0) { |
| 64 | + // Determine buffer size available for write |
| 65 | + auto portion = (size_t)CDC_TransmitQueue_WriteSize(&TransmitQueue); |
| 66 | + // Truncate it to content size (if rest is greater) |
| 67 | + if (rest < portion) { |
| 68 | + portion = rest; |
| 69 | + } |
| 70 | + if (portion > 0) { |
| 71 | + // Only if some space in the buffer exists. |
| 72 | + // TS: Only main thread calls write and writeSize methods, |
| 73 | + // it's thread-safe since IRQ does not affects |
| 74 | + // TransmitQueue write position |
| 75 | + CDC_TransmitQueue_Enqueue(&TransmitQueue, buffer, portion); |
| 76 | + rest -= portion; |
| 77 | + buffer += portion; |
| 78 | + // After storing data, start transmitting process |
| 79 | + CDC_continue_transmit(); |
| 80 | + } |
98 | 81 | }
|
99 |
| - return i; |
| 82 | + return size; |
100 | 83 | }
|
101 | 84 |
|
102 | 85 | int USBSerial::available(void) {
|
103 |
| - return ((APP_RX_DATA_SIZE + (UserRxBufPtrIn - UserRxBufPtrOut)) % APP_RX_DATA_SIZE); |
| 86 | + // Just ReceiveQueue size, available for reading |
| 87 | + return static_cast<int>(CDC_ReceiveQueue_ReadSize(&ReceiveQueue)); |
104 | 88 | }
|
105 | 89 |
|
106 | 90 | int USBSerial::read(void) {
|
107 |
| - if (UserRxBufPtrOut == UserRxBufPtrIn) { |
108 |
| - return -1; |
109 |
| - } else { |
110 |
| - unsigned char c = UserRxBuffer[UserRxBufPtrOut]; |
111 |
| - UserRxBufPtrOut = ((UserRxBufPtrOut + 1) % APP_RX_DATA_SIZE); |
| 91 | + // Dequeue only one char from queue |
| 92 | + // TS: it safe, because only main thread affects ReceiveQueue->read pos |
| 93 | + auto ch = CDC_ReceiveQueue_Dequeue(&ReceiveQueue); |
| 94 | + // Resume receive process, if possible |
| 95 | + CDC_resume_receive(); |
| 96 | + return ch; |
| 97 | +} |
| 98 | + |
| 99 | +size_t USBSerial::readBytes(char *buffer, size_t length) { |
| 100 | + uint16_t read; |
| 101 | + auto rest = static_cast<uint16_t>(length); |
| 102 | + _startMillis = millis(); |
| 103 | + do { |
| 104 | + read = CDC_ReceiveQueue_Read(&ReceiveQueue, reinterpret_cast<uint8_t*>(buffer), rest); |
112 | 105 | CDC_resume_receive();
|
113 |
| - return c; |
114 |
| - } |
| 106 | + rest -= read; |
| 107 | + buffer += read; |
| 108 | + if (rest == 0) return length; |
| 109 | + } while(millis() - _startMillis < _timeout); |
| 110 | + return length - rest; |
| 111 | +} |
| 112 | + |
| 113 | +size_t USBSerial::readBytesUntil(char terminator, char *buffer, size_t length) { |
| 114 | + uint16_t read; |
| 115 | + auto rest = static_cast<uint16_t>(length); |
| 116 | + _startMillis = millis(); |
| 117 | + do { |
| 118 | + bool found = CDC_ReceiveQueue_ReadUntil(&ReceiveQueue, static_cast<uint8_t>(terminator), |
| 119 | + reinterpret_cast<uint8_t*>(buffer), rest, &read); |
| 120 | + CDC_resume_receive(); |
| 121 | + rest -= read; |
| 122 | + buffer += read; |
| 123 | + if (found) { |
| 124 | + return length - rest; |
| 125 | + } |
| 126 | + if (rest == 0) { |
| 127 | + return length; |
| 128 | + } |
| 129 | + } while(millis() - _startMillis < _timeout); |
| 130 | + return length - rest; |
115 | 131 | }
|
116 | 132 |
|
117 | 133 | int USBSerial::peek(void)
|
118 | 134 | {
|
119 |
| - if (UserRxBufPtrOut == UserRxBufPtrIn) { |
120 |
| - return -1; |
121 |
| - } else { |
122 |
| - unsigned char c = UserRxBuffer[UserRxBufPtrOut]; |
123 |
| - return c; |
124 |
| - } |
| 135 | + // Peek one symbol, it can't change receive avaiablity |
| 136 | + return CDC_ReceiveQueue_Peek(&ReceiveQueue); |
125 | 137 | }
|
126 | 138 |
|
127 | 139 | void USBSerial::flush(void)
|
128 | 140 | {
|
129 |
| - /* UserTxBufPtrOut can be modified by TIM ISR, so in order to be sure that the */ |
130 |
| - /* value that we read is correct, we need to disable TIM Interrupt. */ |
131 |
| - CDC_disable_TIM_Interrupt(); |
132 |
| - CDC_flush(); |
133 |
| - CDC_enable_TIM_Interrupt(); |
134 |
| -} |
135 |
| - |
136 |
| -uint8_t USBSerial::pending(void) { |
137 |
| - return 0; |
138 |
| -} |
139 |
| - |
140 |
| -uint8_t USBSerial::isConnected(void) { |
141 |
| - |
142 |
| - if (device_connection_status == 1) { |
143 |
| - return 1; |
144 |
| - } else { |
145 |
| - return 0; |
146 |
| - } |
| 141 | + // Wait for TransmitQueue read size becomes zero |
| 142 | + // TS: safe, because it not be stopped while receive 0 |
| 143 | + while(CDC_TransmitQueue_ReadSize(&TransmitQueue) > 0) {} |
147 | 144 | }
|
148 | 145 |
|
149 | 146 | uint32_t USBSerial::baud() {
|
|
0 commit comments