@@ -64,14 +64,12 @@ typedef struct BDT {
64
64
uint32_t address; // Addr
65
65
} BDT;
66
66
67
-
68
67
// there are:
69
- // * 16 bidirectionnal endpt -> 32 physical endpt
70
- // * as there are ODD and EVEN buffer -> 32 *2 bdt
68
+ // * 4 bidirectionnal endpt -> 8 physical endpt
69
+ // * as there are ODD and EVEN buffer -> 8 *2 bdt
71
70
MBED_ALIGN (512 ) BDT bdt[NUMBER_OF_PHYSICAL_ENDPOINTS * 2]; // 512 bytes aligned!
72
71
73
- uint8_t * endpoint_buffer[(NUMBER_OF_PHYSICAL_ENDPOINTS - 2 ) * 2 ];
74
- uint8_t * endpoint_buffer_iso[2 *2 ];
72
+ uint8_t * endpoint_buffer[NUMBER_OF_PHYSICAL_ENDPOINTS * 2 ];
75
73
76
74
static uint8_t set_addr = 0 ;
77
75
static uint8_t addr = 0 ;
@@ -102,28 +100,6 @@ USBHAL::USBHAL(void) {
102
100
epCallback[5 ] = &USBHAL::EP3_IN_callback;
103
101
epCallback[6 ] = &USBHAL::EP4_OUT_callback;
104
102
epCallback[7 ] = &USBHAL::EP4_IN_callback;
105
- epCallback[8 ] = &USBHAL::EP5_OUT_callback;
106
- epCallback[9 ] = &USBHAL::EP5_IN_callback;
107
- epCallback[10 ] = &USBHAL::EP6_OUT_callback;
108
- epCallback[11 ] = &USBHAL::EP6_IN_callback;
109
- epCallback[12 ] = &USBHAL::EP7_OUT_callback;
110
- epCallback[13 ] = &USBHAL::EP7_IN_callback;
111
- epCallback[14 ] = &USBHAL::EP8_OUT_callback;
112
- epCallback[15 ] = &USBHAL::EP8_IN_callback;
113
- epCallback[16 ] = &USBHAL::EP9_OUT_callback;
114
- epCallback[17 ] = &USBHAL::EP9_IN_callback;
115
- epCallback[18 ] = &USBHAL::EP10_OUT_callback;
116
- epCallback[19 ] = &USBHAL::EP10_IN_callback;
117
- epCallback[20 ] = &USBHAL::EP11_OUT_callback;
118
- epCallback[21 ] = &USBHAL::EP11_IN_callback;
119
- epCallback[22 ] = &USBHAL::EP12_OUT_callback;
120
- epCallback[23 ] = &USBHAL::EP12_IN_callback;
121
- epCallback[24 ] = &USBHAL::EP13_OUT_callback;
122
- epCallback[25 ] = &USBHAL::EP13_IN_callback;
123
- epCallback[26 ] = &USBHAL::EP14_OUT_callback;
124
- epCallback[27 ] = &USBHAL::EP14_IN_callback;
125
- epCallback[28 ] = &USBHAL::EP15_OUT_callback;
126
- epCallback[29 ] = &USBHAL::EP15_IN_callback;
127
103
128
104
#if defined(TARGET_KL43Z) || defined(TARGET_K22F) || defined(TARGET_K64F)
129
105
// enable USBFS clock
@@ -161,6 +137,10 @@ USBHAL::USBHAL(void) {
161
137
USB0->USBCTRL &= ~(USB_USBCTRL_PDE_MASK | USB_USBCTRL_SUSP_MASK);
162
138
163
139
USB0->USBTRC0 |= 0x40 ;
140
+
141
+ /* Allocate control endpoint buffers */
142
+ endpoint_buffer[EP_BDT_IDX (0 , TX, ODD)] = (uint8_t *)malloc (MAX_PACKET_SIZE_EP0);
143
+ endpoint_buffer[EP_BDT_IDX (0 , RX, ODD)] = (uint8_t *)malloc (MAX_PACKET_SIZE_EP0);
164
144
}
165
145
166
146
USBHAL::~USBHAL (void ) { }
@@ -170,6 +150,15 @@ void USBHAL::connect(void) {
170
150
USB0->CTL |= USB_CTL_USBENSOFEN_MASK;
171
151
// Pull up enable
172
152
USB0->CONTROL |= USB_CONTROL_DPPULLUPNONOTG_MASK;
153
+
154
+ // Allocate endpoint buffers; do allocate control endpoint buffers
155
+ for (int i = 4 ; i < (NUMBER_OF_PHYSICAL_ENDPOINTS * 2 ); i++) {
156
+ if ((i == EPISO_OUT) || (i == EPISO_IN)) {
157
+ endpoint_buffer[i] = (uint8_t *)malloc (MAX_PACKET_SIZE_EPISO);
158
+ } else {
159
+ endpoint_buffer[i] = (uint8_t *)malloc (MAX_PACKET_SIZE_EPBULK);
160
+ }
161
+ }
173
162
}
174
163
175
164
void USBHAL::disconnect (void ) {
@@ -178,15 +167,11 @@ void USBHAL::disconnect(void) {
178
167
// Pull up disable
179
168
USB0->CONTROL &= ~USB_CONTROL_DPPULLUPNONOTG_MASK;
180
169
181
- // Free buffers if required:
182
- for (int i = 0 ; i< (NUMBER_OF_PHYSICAL_ENDPOINTS - 2 ) * 2 ; i++) {
170
+ // Free buffers if required; do not free the control endpoint buffers
171
+ for (int i = 4 ; i < (NUMBER_OF_PHYSICAL_ENDPOINTS * 2 ) ; i++) {
183
172
free (endpoint_buffer[i]);
184
173
endpoint_buffer[i] = NULL ;
185
174
}
186
- free (endpoint_buffer_iso[2 ]);
187
- endpoint_buffer_iso[2 ] = NULL ;
188
- free (endpoint_buffer_iso[0 ]);
189
- endpoint_buffer_iso[0 ] = NULL ;
190
175
}
191
176
192
177
void USBHAL::configureDevice (void ) {
@@ -217,25 +202,12 @@ bool USBHAL::realiseEndpoint(uint8_t endpoint, uint32_t maxPacket, uint32_t flag
217
202
218
203
if ((flags & ISOCHRONOUS) == 0 ) {
219
204
handshake_flag = USB_ENDPT_EPHSHK_MASK;
220
- if (IN_EP (endpoint)) {
221
- if (endpoint_buffer[EP_BDT_IDX (log_endpoint, TX, ODD)] == NULL )
222
- endpoint_buffer[EP_BDT_IDX (log_endpoint, TX, ODD)] = (uint8_t *) malloc (64 );
223
- buf = &endpoint_buffer[EP_BDT_IDX (log_endpoint, TX, ODD)][0 ];
224
- } else {
225
- if (endpoint_buffer[EP_BDT_IDX (log_endpoint, RX, ODD)] == NULL )
226
- endpoint_buffer[EP_BDT_IDX (log_endpoint, RX, ODD)] = (uint8_t *) malloc (64 );
227
- buf = &endpoint_buffer[EP_BDT_IDX (log_endpoint, RX, ODD)][0 ];
228
- }
205
+ }
206
+
207
+ if (IN_EP (endpoint)) {
208
+ buf = &endpoint_buffer[EP_BDT_IDX (log_endpoint, TX, ODD)][0 ];
229
209
} else {
230
- if (IN_EP (endpoint)) {
231
- if (endpoint_buffer_iso[2 ] == NULL )
232
- endpoint_buffer_iso[2 ] = (uint8_t *) malloc (1023 );
233
- buf = &endpoint_buffer_iso[2 ][0 ];
234
- } else {
235
- if (endpoint_buffer_iso[0 ] == NULL )
236
- endpoint_buffer_iso[0 ] = (uint8_t *) malloc (1023 );
237
- buf = &endpoint_buffer_iso[0 ][0 ];
238
- }
210
+ buf = &endpoint_buffer[EP_BDT_IDX (log_endpoint, RX, ODD)][0 ];
239
211
}
240
212
241
213
// IN endpt -> device to host (TX)
@@ -329,12 +301,7 @@ EP_STATUS USBHAL::endpointReadResult(uint8_t endpoint, uint8_t * buffer, uint32_
329
301
setup = 1 ;
330
302
}
331
303
332
- // non iso endpoint
333
- if (not_iso) {
334
- ep_buf = endpoint_buffer[idx];
335
- } else {
336
- ep_buf = endpoint_buffer_iso[0 ];
337
- }
304
+ ep_buf = endpoint_buffer[idx];
338
305
339
306
for (n = 0 ; n < sz; n++) {
340
307
buffer[n] = ep_buf[n];
@@ -377,13 +344,7 @@ EP_STATUS USBHAL::endpointWrite(uint8_t endpoint, uint8_t *data, uint32_t size)
377
344
idx = EP_BDT_IDX (PHY_TO_LOG (endpoint), TX, 0 );
378
345
bdt[idx].byte_count = size;
379
346
380
-
381
- // non iso endpoint
382
- if (USB0->ENDPOINT [PHY_TO_LOG (endpoint)].ENDPT & USB_ENDPT_EPHSHK_MASK) {
383
- ep_buf = endpoint_buffer[idx];
384
- } else {
385
- ep_buf = endpoint_buffer_iso[2 ];
386
- }
347
+ ep_buf = endpoint_buffer[idx];
387
348
388
349
for (n = 0 ; n < size; n++) {
389
350
ep_buf[n] = data[n];
0 commit comments