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