|
27 | 27 | #if TUSB_OPT_DEVICE_ENABLED && CFG_TUD_CDC |
28 | 28 |
|
29 | 29 | #include "Arduino.h" |
| 30 | + |
30 | 31 | #include "Adafruit_TinyUSB_API.h" |
31 | 32 |
|
32 | | -#include "Adafruit_USBD_Device.h" |
33 | 33 | #include "Adafruit_USBD_CDC.h" |
| 34 | +#include "Adafruit_USBD_Device.h" |
34 | 35 |
|
35 | 36 | // TODO Multiple instances supports |
36 | 37 | // static uint8_t _itf_count; |
37 | 38 | // static Adafruit_USBD_CDC* _itf_arr[] |
38 | 39 |
|
39 | | -#define EPOUT 0x00 |
40 | | -#define EPIN 0x80 |
| 40 | +#define EPOUT 0x00 |
| 41 | +#define EPIN 0x80 |
41 | 42 |
|
42 | 43 | Adafruit_USBD_CDC Serial; |
43 | 44 |
|
44 | | -Adafruit_USBD_CDC::Adafruit_USBD_CDC(void) |
45 | | -{ |
| 45 | +Adafruit_USBD_CDC::Adafruit_USBD_CDC(void) { |
46 | 46 | _begun = false; |
47 | 47 | _itf = 0; |
48 | 48 | } |
49 | 49 |
|
50 | | -uint16_t Adafruit_USBD_CDC::getInterfaceDescriptor(uint8_t itfnum, uint8_t* buf, uint16_t bufsize) |
51 | | -{ |
| 50 | +uint16_t Adafruit_USBD_CDC::getInterfaceDescriptor(uint8_t itfnum, uint8_t *buf, |
| 51 | + uint16_t bufsize) { |
52 | 52 | // CDC is mostly always existed for DFU |
53 | 53 | // usb core will automatically update endpoint number |
54 | | - uint8_t desc[] = { TUD_CDC_DESCRIPTOR(itfnum, 0, EPIN, 8, EPOUT, EPIN, 64) }; |
| 54 | + uint8_t desc[] = {TUD_CDC_DESCRIPTOR(itfnum, 0, EPIN, 8, EPOUT, EPIN, 64)}; |
55 | 55 | uint16_t const len = sizeof(desc); |
56 | 56 |
|
57 | | - if ( bufsize < len ) return 0; |
| 57 | + if (bufsize < len) { |
| 58 | + return 0; |
| 59 | + } |
58 | 60 |
|
59 | 61 | memcpy(buf, desc, len); |
60 | 62 | return len; |
61 | 63 | } |
62 | 64 |
|
63 | 65 | // Baud and config is ignore in CDC |
64 | | -void Adafruit_USBD_CDC::begin (uint32_t baud) |
65 | | -{ |
66 | | - (void) baud; |
| 66 | +void Adafruit_USBD_CDC::begin(uint32_t baud) { |
| 67 | + (void)baud; |
| 68 | + |
| 69 | + if (_begun) { |
| 70 | + return; |
| 71 | + } |
67 | 72 |
|
68 | | - if (_begun) return; |
69 | 73 | _begun = true; |
70 | 74 |
|
71 | 75 | Serial.setStringDescriptor("TinyUSB Serial"); |
72 | 76 | USBDevice.addInterface(Serial); |
73 | 77 | } |
74 | 78 |
|
75 | | -void Adafruit_USBD_CDC::begin (uint32_t baud, uint8_t config) |
76 | | -{ |
77 | | - (void) config; |
| 79 | +void Adafruit_USBD_CDC::begin(uint32_t baud, uint8_t config) { |
| 80 | + (void)config; |
78 | 81 | this->begin(baud); |
79 | 82 | } |
80 | 83 |
|
81 | | -void Adafruit_USBD_CDC::end(void) |
82 | | -{ |
| 84 | +void Adafruit_USBD_CDC::end(void) { |
| 85 | + // Resset configuration descriptor without Serial as CDC |
83 | 86 | USBDevice.clearConfiguration(); |
84 | 87 | } |
85 | 88 |
|
86 | | -uint32_t Adafruit_USBD_CDC::baud(void) |
87 | | -{ |
| 89 | +uint32_t Adafruit_USBD_CDC::baud(void) { |
88 | 90 | cdc_line_coding_t coding; |
89 | 91 | tud_cdc_get_line_coding(&coding); |
90 | 92 |
|
91 | 93 | return coding.bit_rate; |
92 | 94 | } |
93 | 95 |
|
94 | | -uint8_t Adafruit_USBD_CDC::stopbits(void) |
95 | | -{ |
| 96 | +uint8_t Adafruit_USBD_CDC::stopbits(void) { |
96 | 97 | cdc_line_coding_t coding; |
97 | 98 | tud_cdc_get_line_coding(&coding); |
98 | 99 |
|
99 | 100 | return coding.stop_bits; |
100 | 101 | } |
101 | 102 |
|
102 | | -uint8_t Adafruit_USBD_CDC::paritytype(void) |
103 | | -{ |
| 103 | +uint8_t Adafruit_USBD_CDC::paritytype(void) { |
104 | 104 | cdc_line_coding_t coding; |
105 | 105 | tud_cdc_get_line_coding(&coding); |
106 | 106 |
|
107 | 107 | return coding.parity; |
108 | 108 | } |
109 | 109 |
|
110 | | -uint8_t Adafruit_USBD_CDC::numbits(void) |
111 | | -{ |
| 110 | +uint8_t Adafruit_USBD_CDC::numbits(void) { |
112 | 111 | cdc_line_coding_t coding; |
113 | 112 | tud_cdc_get_line_coding(&coding); |
114 | 113 |
|
115 | 114 | return coding.data_bits; |
116 | 115 | } |
117 | 116 |
|
118 | | -int Adafruit_USBD_CDC::dtr(void) |
119 | | -{ |
120 | | - return tud_cdc_connected(); |
121 | | -} |
| 117 | +int Adafruit_USBD_CDC::dtr(void) { return tud_cdc_connected(); } |
122 | 118 |
|
123 | | -Adafruit_USBD_CDC::operator bool() |
124 | | -{ |
| 119 | +Adafruit_USBD_CDC::operator bool() { |
125 | 120 | bool ret = tud_cdc_connected(); |
126 | 121 |
|
127 | 122 | // Add an yield to run usb background in case sketch block wait as follows |
128 | 123 | // while( !Serial ) {} |
129 | | - if ( !ret ) yield(); |
130 | | - |
| 124 | + if (!ret) { |
| 125 | + yield(); |
| 126 | + } |
131 | 127 | return ret; |
132 | 128 | } |
133 | 129 |
|
134 | | -int Adafruit_USBD_CDC::available(void) |
135 | | -{ |
| 130 | +int Adafruit_USBD_CDC::available(void) { |
136 | 131 | uint32_t count = tud_cdc_available(); |
137 | 132 |
|
138 | 133 | // Add an yield to run usb background in case sketch block wait as follows |
139 | 134 | // while( !Serial.available() ) {} |
140 | | - if (!count) yield(); |
| 135 | + if (!count) { |
| 136 | + yield(); |
| 137 | + } |
141 | 138 |
|
142 | 139 | return count; |
143 | 140 | } |
144 | 141 |
|
145 | | -int Adafruit_USBD_CDC::peek(void) |
146 | | -{ |
| 142 | +int Adafruit_USBD_CDC::peek(void) { |
147 | 143 | uint8_t ch; |
148 | | - return tud_cdc_peek(&ch) ? (int) ch : -1; |
| 144 | + return tud_cdc_peek(&ch) ? (int)ch : -1; |
149 | 145 | } |
150 | 146 |
|
151 | | -int Adafruit_USBD_CDC::read(void) |
152 | | -{ |
153 | | - return (int) tud_cdc_read_char(); |
154 | | -} |
| 147 | +int Adafruit_USBD_CDC::read(void) { return (int)tud_cdc_read_char(); } |
155 | 148 |
|
156 | | -void Adafruit_USBD_CDC::flush(void) |
157 | | -{ |
158 | | - tud_cdc_write_flush(); |
159 | | -} |
| 149 | +void Adafruit_USBD_CDC::flush(void) { tud_cdc_write_flush(); } |
160 | 150 |
|
161 | | -size_t Adafruit_USBD_CDC::write(uint8_t ch) |
162 | | -{ |
163 | | - return write(&ch, 1); |
164 | | -} |
| 151 | +size_t Adafruit_USBD_CDC::write(uint8_t ch) { return write(&ch, 1); } |
165 | 152 |
|
166 | | -size_t Adafruit_USBD_CDC::write(const uint8_t *buffer, size_t size) |
167 | | -{ |
| 153 | +size_t Adafruit_USBD_CDC::write(const uint8_t *buffer, size_t size) { |
168 | 154 | size_t remain = size; |
169 | | - while ( remain && tud_cdc_connected() ) |
170 | | - { |
| 155 | + while (remain && tud_cdc_connected()) { |
171 | 156 | size_t wrcount = tud_cdc_write(buffer, remain); |
172 | 157 | remain -= wrcount; |
173 | 158 | buffer += wrcount; |
174 | 159 |
|
175 | 160 | // Write FIFO is full, run usb background to flush |
176 | | - if ( remain ) yield(); |
| 161 | + if (remain) { |
| 162 | + yield(); |
| 163 | + } |
177 | 164 | } |
178 | 165 |
|
179 | 166 | return size - remain; |
180 | 167 | } |
181 | 168 |
|
182 | | -int Adafruit_USBD_CDC::availableForWrite(void) |
183 | | -{ |
| 169 | +int Adafruit_USBD_CDC::availableForWrite(void) { |
184 | 170 | return tud_cdc_write_available(); |
185 | 171 | } |
186 | 172 |
|
187 | | -extern "C" |
188 | | -{ |
| 173 | +extern "C" { |
189 | 174 |
|
190 | 175 | // Invoked when cdc when line state changed e.g connected/disconnected |
191 | 176 | // Use to reset to DFU when disconnect with 1200 bps |
192 | | -void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) |
193 | | -{ |
194 | | - (void) rts; |
| 177 | +void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) { |
| 178 | + (void)rts; |
195 | 179 |
|
196 | 180 | // DTR = false is counted as disconnected |
197 | | - if ( !dtr ) |
198 | | - { |
| 181 | + if (!dtr) { |
199 | 182 | // touch1200 only with first CDC instance (Serial) |
200 | | - if ( itf == 0 ) |
201 | | - { |
| 183 | + if (itf == 0) { |
202 | 184 | cdc_line_coding_t coding; |
203 | 185 | tud_cdc_get_line_coding(&coding); |
204 | 186 |
|
205 | | - if ( coding.bit_rate == 1200 ) TinyUSB_Port_EnterDFU(); |
| 187 | + if (coding.bit_rate == 1200) { |
| 188 | + TinyUSB_Port_EnterDFU(); |
| 189 | + } |
206 | 190 | } |
207 | 191 | } |
208 | 192 | } |
209 | | - |
210 | 193 | } |
211 | 194 |
|
212 | 195 | #endif // TUSB_OPT_DEVICE_ENABLED |
0 commit comments