|
27 | 27 | #define USB_TIMEOUT 50
|
28 | 28 | /* USB Device Core handle declaration */
|
29 | 29 | extern USBD_HandleTypeDef hUSBD_Device_CDC;
|
30 |
| -extern __IO uint32_t device_connection_status; |
31 | 30 | 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 | 31 |
|
39 | 32 | USBSerial SerialUSB;
|
40 | 33 | void serialEventUSB() __attribute__((weak));
|
41 | 34 |
|
| 35 | +void USBSerial::begin(void) { |
| 36 | + CDC_init(); |
| 37 | +} |
| 38 | + |
42 | 39 | void USBSerial::begin(uint32_t /* baud_count */) {
|
43 | 40 | // uart config is ignored in USB-CDC
|
| 41 | + begin(); |
44 | 42 | }
|
45 | 43 |
|
46 | 44 | void USBSerial::begin(uint32_t /* baud_count */, uint8_t /* config */) {
|
47 | 45 | // uart config is ignored in USB-CDC
|
| 46 | + begin(); |
48 | 47 | }
|
49 | 48 |
|
50 |
| -void USBSerial::end(void) { |
51 |
| - |
52 |
| - USBD_LL_DeInit(&hUSBD_Device_CDC); |
| 49 | +void USBSerial::end() { |
| 50 | + CDC_deInit(); |
53 | 51 | }
|
54 | 52 |
|
55 |
| -int USBSerial::availableForWrite(void) |
| 53 | +int USBSerial::availableForWrite() |
56 | 54 | {
|
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; |
| 55 | + // Just transmit queue size, available for write |
| 56 | + return static_cast<int>(CDC_TransmitQueue_WriteSize(&TransmitQueue)); |
72 | 57 | }
|
73 | 58 |
|
74 | 59 | 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; |
| 60 | + // Just write single-byte buffer. |
| 61 | + return write(&ch, 1); |
90 | 62 | }
|
91 | 63 |
|
92 | 64 | 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 |
| - } |
| 65 | + size_t rest = size; |
| 66 | + while(rest > 0) { |
| 67 | + // Determine buffer size available for write |
| 68 | + auto portion = (size_t)CDC_TransmitQueue_WriteSize(&TransmitQueue); |
| 69 | + // Truncate it to content size (if rest is greater) |
| 70 | + if (rest < portion) { |
| 71 | + portion = rest; |
| 72 | + } |
| 73 | + if (portion > 0) { |
| 74 | + // Only if some space in the buffer exists. |
| 75 | + // TS: Only main thread calls write and writeSize methods, |
| 76 | + // it's thread-safe since IRQ does not affects |
| 77 | + // TransmitQueue write position |
| 78 | + CDC_TransmitQueue_Enqueue(&TransmitQueue, buffer, portion); |
| 79 | + rest -= portion; |
| 80 | + buffer += portion; |
| 81 | + // After storing data, start transmitting process |
| 82 | + CDC_continue_transmit(); |
| 83 | + } |
98 | 84 | }
|
99 |
| - return i; |
| 85 | + return size; |
100 | 86 | }
|
101 | 87 |
|
102 | 88 | int USBSerial::available(void) {
|
103 |
| - return ((APP_RX_DATA_SIZE + (UserRxBufPtrIn - UserRxBufPtrOut)) % APP_RX_DATA_SIZE); |
| 89 | + // Just ReceiveQueue size, available for reading |
| 90 | + return static_cast<int>(CDC_ReceiveQueue_ReadSize(&ReceiveQueue)); |
104 | 91 | }
|
105 | 92 |
|
106 | 93 | int USBSerial::read(void) {
|
107 |
| - if (UserRxBufPtrOut == UserRxBufPtrIn) { |
| 94 | + // Empty ReceiveQueue - nothing to return |
| 95 | + if (CDC_ReceiveQueue_ReadSize(&ReceiveQueue) <= 0) { |
108 | 96 | return -1;
|
109 |
| - } else { |
110 |
| - unsigned char c = UserRxBuffer[UserRxBufPtrOut]; |
111 |
| - UserRxBufPtrOut = ((UserRxBufPtrOut + 1) % APP_RX_DATA_SIZE); |
112 |
| - CDC_resume_receive(); |
113 |
| - return c; |
114 | 97 | }
|
| 98 | + // Dequeue only one char from queue |
| 99 | + // TS: it safe, because only main thread affects ReceiveQueue->read pos |
| 100 | + char ch = CDC_ReceiveQueue_Dequeue(&ReceiveQueue); |
| 101 | + // Resume receive process, if possible |
| 102 | + CDC_resume_receive(); |
| 103 | + return ch; |
115 | 104 | }
|
116 | 105 |
|
117 | 106 | int USBSerial::peek(void)
|
118 | 107 | {
|
119 |
| - if (UserRxBufPtrOut == UserRxBufPtrIn) { |
| 108 | + // Empty ReceiveQueue - nothing to return |
| 109 | + if (CDC_ReceiveQueue_ReadSize(&ReceiveQueue) <= 0) { |
120 | 110 | return -1;
|
121 |
| - } else { |
122 |
| - unsigned char c = UserRxBuffer[UserRxBufPtrOut]; |
123 |
| - return c; |
124 | 111 | }
|
| 112 | + // Peek one symbol, it can't change receive avaiablity |
| 113 | + return CDC_ReceiveQueue_Peek(&ReceiveQueue); |
125 | 114 | }
|
126 | 115 |
|
127 | 116 | void USBSerial::flush(void)
|
128 | 117 | {
|
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 |
| - } |
| 118 | + // Wait for TransmitQueue read size becomes zero |
| 119 | + // TS: safe, because it not be stopped while receive 0 |
| 120 | + while(CDC_TransmitQueue_ReadSize(&TransmitQueue) > 0) {} |
147 | 121 | }
|
148 | 122 |
|
149 | 123 | uint32_t USBSerial::baud() {
|
|
0 commit comments