19
19
#include < stdint.h>
20
20
21
21
// Include Atmel headers
22
+ #include " arduino.h"
22
23
#include " sam.h"
23
24
#include " wiring_constants.h"
24
25
#include " USBCore.h"
25
26
#include " USB_device.h"
26
27
#include " USBDesc.h"
27
28
#include " USBAPI.h"
28
- // #include "samd21_device.h"
29
29
30
30
#include " Reset.h"
31
31
32
32
33
33
#ifdef CDC_ENABLED
34
34
35
- #define CDC_SERIAL_BUFFER_SIZE 512
35
+ #define CDC_SERIAL_BUFFER_SIZE 64
36
36
37
37
/* For information purpose only since RTS is not always handled by the terminal application */
38
38
#define CDC_LINESTATE_DTR 0x01 // Data Terminal Ready
@@ -58,61 +58,44 @@ typedef struct
58
58
uint8_t lineState;
59
59
} LineInfo;
60
60
61
+ _Pragma (" pack(1)" )
61
62
static volatile LineInfo _usbLineInfo = {
62
- 57600 , // dWDTERate
63
+ 115200 , // dWDTERate
63
64
0x00 , // bCharFormat
64
65
0x00 , // bParityType
65
66
0x08 , // bDataBits
66
67
0x00 // lineState
67
68
};
68
69
69
- _Pragma (" pack(1)" )
70
70
static const CDCDescriptor _cdcInterface =
71
71
{
72
- D_IAD (0 ,2 ,CDC_COMMUNICATION_INTERFACE_CLASS,CDC_ABSTRACT_CONTROL_MODEL,1 ),
73
-
72
+ #if (defined CDC_ENABLED) && defined(HID_ENABLED)
73
+ D_IAD (0 , 2 , CDC_COMMUNICATION_INTERFACE_CLASS, CDC_ABSTRACT_CONTROL_MODEL, 0 ),
74
+ #endif
74
75
// CDC communication interface
75
76
D_INTERFACE (CDC_ACM_INTERFACE,1 ,CDC_COMMUNICATION_INTERFACE_CLASS,CDC_ABSTRACT_CONTROL_MODEL,0 ),
76
- D_CDCCS (CDC_HEADER,0x10 , 0x01 ), // Header (1.10 bcd)
77
- D_CDCCS (CDC_CALL_MANAGEMENT, 1 , 1 ), // Device handles call management (not)
77
+ D_CDCCS ( CDC_HEADER, CDC_V1_10 & 0xFF , (CDC_V1_10>> 8 ) & 0x0FF ), // Header (1.10 bcd)
78
+
78
79
D_CDCCS4 (CDC_ABSTRACT_CONTROL_MANAGEMENT,6 ), // SET_LINE_CODING, GET_LINE_CODING, SET_CONTROL_LINE_STATE supported
79
80
D_CDCCS (CDC_UNION,CDC_ACM_INTERFACE,CDC_DATA_INTERFACE), // Communication interface is master, data interface is slave 0
80
- D_ENDPOINT (USB_ENDPOINT_IN (CDC_ENDPOINT_ACM),USB_ENDPOINT_TYPE_INTERRUPT,0x10 , 0x10 ),
81
-
82
- // CDC data interface
83
- D_INTERFACE (CDC_DATA_INTERFACE,2 ,CDC_DATA_INTERFACE_CLASS,0 ,0 ),
84
- D_ENDPOINT (USB_ENDPOINT_OUT (CDC_ENDPOINT_OUT),USB_ENDPOINT_TYPE_BULK,512 ,0 ),
85
- D_ENDPOINT (USB_ENDPOINT_IN (CDC_ENDPOINT_IN ),USB_ENDPOINT_TYPE_BULK,512 ,0 )
86
- };
87
- static const CDCDescriptor _cdcOtherInterface =
88
- {
89
- D_IAD (0 ,2 ,CDC_COMMUNICATION_INTERFACE_CLASS,CDC_ABSTRACT_CONTROL_MODEL,1 ),
90
-
91
- // CDC communication interface
92
- D_INTERFACE (CDC_ACM_INTERFACE,1 ,CDC_COMMUNICATION_INTERFACE_CLASS,CDC_ABSTRACT_CONTROL_MODEL,0 ),
93
- D_CDCCS (CDC_HEADER,0x10 ,0x01 ), // Header (1.10 bcd)
94
81
D_CDCCS (CDC_CALL_MANAGEMENT,1 ,1 ), // Device handles call management (not)
95
- D_CDCCS4 (CDC_ABSTRACT_CONTROL_MANAGEMENT,6 ), // SET_LINE_CODING, GET_LINE_CODING, SET_CONTROL_LINE_STATE supported
96
- D_CDCCS (CDC_UNION,CDC_ACM_INTERFACE,CDC_DATA_INTERFACE), // Communication interface is master, data interface is slave 0
97
82
D_ENDPOINT (USB_ENDPOINT_IN (CDC_ENDPOINT_ACM),USB_ENDPOINT_TYPE_INTERRUPT,0x10 , 0x10 ),
98
83
99
84
// CDC data interface
100
85
D_INTERFACE (CDC_DATA_INTERFACE,2 ,CDC_DATA_INTERFACE_CLASS,0 ,0 ),
101
- D_ENDPOINT (USB_ENDPOINT_OUT (CDC_ENDPOINT_OUT),USB_ENDPOINT_TYPE_BULK,64 ,0 ),
102
- D_ENDPOINT (USB_ENDPOINT_IN (CDC_ENDPOINT_IN ),USB_ENDPOINT_TYPE_BULK,64 ,0 )
86
+ D_ENDPOINT (USB_ENDPOINT_OUT (CDC_ENDPOINT_OUT),USB_ENDPOINT_TYPE_BULK,EPX_SIZE ,0 ),
87
+ D_ENDPOINT (USB_ENDPOINT_IN (CDC_ENDPOINT_IN ),USB_ENDPOINT_TYPE_BULK,EPX_SIZE ,0 )
103
88
};
104
89
_Pragma (" pack()" )
105
90
106
- int WEAK CDC_GetInterface (uint8_t * interfaceNum )
91
+ const void * WEAK CDC_GetInterface (void )
107
92
{
108
- interfaceNum[0 ] += 2 ; // uses 2
109
- return USBD_SendControl (0 ,&_cdcInterface,sizeof (_cdcInterface));
93
+ return &_cdcInterface;
110
94
}
111
95
112
- int WEAK CDC_GetOtherInterface ( uint8_t * interfaceNum )
96
+ uint32_t WEAK CDC_GetInterfaceLength ( void )
113
97
{
114
- interfaceNum[0 ] += 2 ; // uses 2
115
- return USBD_SendControl (0 ,&_cdcOtherInterface,sizeof (_cdcOtherInterface));
98
+ return sizeof ( _cdcInterface );
116
99
}
117
100
118
101
bool WEAK CDC_Setup (Setup& setup)
@@ -134,7 +117,7 @@ bool WEAK CDC_Setup(Setup& setup)
134
117
if (CDC_SET_LINE_CODING == r)
135
118
{
136
119
USBD_RecvControl ((void *)&_usbLineInfo,7 );
137
- return true ;
120
+ return false ;
138
121
}
139
122
140
123
if (CDC_SET_CONTROL_LINE_STATE == r)
@@ -150,13 +133,13 @@ bool WEAK CDC_Setup(Setup& setup)
150
133
else
151
134
cancelReset ();
152
135
}
153
- return true ;
136
+ return false ;
154
137
}
155
138
}
156
139
return false ;
157
140
}
158
141
159
- int _serialPeek = -1 ;
142
+ uint32_t _serialPeek = -1 ;
160
143
void Serial_::begin (uint32_t baud_count)
161
144
{
162
145
}
@@ -171,16 +154,6 @@ void Serial_::end(void)
171
154
172
155
void Serial_::accept (void )
173
156
{
174
- // static uint32_t guard = 0;
175
-
176
- // synchronized access to guard
177
- // JCB do {
178
- // if (__LDREXW(&guard) != 0) {
179
- // __CLREX();
180
- // return; // busy
181
- // }
182
- // } while (__STREXW(1, &guard) != 0); // retry until write succeed
183
-
184
157
ring_buffer *buffer = &cdc_rx_buffer;
185
158
uint32_t i = (uint32_t )(buffer->head +1 ) % CDC_SERIAL_BUFFER_SIZE;
186
159
@@ -190,27 +163,22 @@ void Serial_::accept(void)
190
163
// and so we don't write the character or advance the head.
191
164
while (i != buffer->tail ) {
192
165
uint32_t c;
193
- if (!USBD_Available (CDC_RX)) {
194
- UDD_ReleaseRX (CDC_RX);
195
- // JCB udd_ack_fifocon(CDC_RX);
166
+ if (!USBD_Available (CDC_ENDPOINT_OUT)) {
167
+ UDD_ReleaseRX (CDC_ENDPOINT_OUT);
196
168
break ;
197
169
}
198
- c = USBD_Recv (CDC_RX);
199
- // c = UDD_Recv8(CDC_RX & 0xF);
170
+ c = USBD_Recv (CDC_ENDPOINT_OUT);
200
171
buffer->buffer [buffer->head ] = c;
201
172
buffer->head = i;
202
173
203
174
i = (i + 1 ) % CDC_SERIAL_BUFFER_SIZE;
204
175
}
205
-
206
- // release the guard
207
- // guard = 0;
208
176
}
209
177
210
178
int Serial_::available (void )
211
179
{
212
180
ring_buffer *buffer = &cdc_rx_buffer;
213
- return (unsigned int )(CDC_SERIAL_BUFFER_SIZE + buffer->head - buffer->tail ) % CDC_SERIAL_BUFFER_SIZE;
181
+ return (uint32_t )(CDC_SERIAL_BUFFER_SIZE + buffer->head - buffer->tail ) % CDC_SERIAL_BUFFER_SIZE;
214
182
}
215
183
216
184
int Serial_::peek (void )
@@ -239,16 +207,16 @@ int Serial_::read(void)
239
207
else
240
208
{
241
209
unsigned char c = buffer->buffer [buffer->tail ];
242
- buffer->tail = (unsigned int )(buffer->tail + 1 ) % CDC_SERIAL_BUFFER_SIZE;
243
- if (USBD_Available (CDC_RX ))
210
+ buffer->tail = (uint32_t )(buffer->tail + 1 ) % CDC_SERIAL_BUFFER_SIZE;
211
+ if (USBD_Available (CDC_ENDPOINT_OUT ))
244
212
accept ();
245
213
return c;
246
214
}
247
215
}
248
216
249
217
void Serial_::flush (void )
250
218
{
251
- USBD_Flush (CDC_TX );
219
+ USBD_Flush (CDC_ENDPOINT_IN );
252
220
}
253
221
254
222
size_t Serial_::write (const uint8_t *buffer, size_t size)
@@ -262,9 +230,9 @@ size_t Serial_::write(const uint8_t *buffer, size_t size)
262
230
// TODO - ZE - check behavior on different OSes and test what happens if an
263
231
// open connection isn't broken cleanly (cable is yanked out, host dies
264
232
// or locks up, or host virtual serial port hangs)
265
- if (_usbLineInfo.lineState > 0 )
233
+ // if (_usbLineInfo.lineState > 0) // Problem with Windows(R )
266
234
{
267
- int r = USBD_Send (CDC_TX , buffer, size);
235
+ uint32_t r = USBD_Send (CDC_ENDPOINT_IN , buffer, size);
268
236
269
237
if (r > 0 )
270
238
{
@@ -293,8 +261,8 @@ size_t Serial_::write(uint8_t c) {
293
261
Serial_::operator bool ()
294
262
{
295
263
// this is here to avoid spurious opening after upload
296
- // if (millis() < 500)
297
- // return false;
264
+ if (millis () < 500 )
265
+ return false ;
298
266
299
267
bool result = false ;
300
268
@@ -303,7 +271,7 @@ Serial_::operator bool()
303
271
result = true ;
304
272
}
305
273
306
- // delay(10);
274
+ delay (10 );
307
275
return result;
308
276
}
309
277
0 commit comments