📄 usbd_udphs.lst
字号:
192 #endif
193 }
194
195 //------------------------------------------------------------------------------
196 /// Disable UDPHS clock
197 //------------------------------------------------------------------------------
198 static inline void UDPHS_DisableUsbClock( void )
199 {
200 #if !defined (PMC_BY_HARD)
201 AT91C_BASE_PMC->PMC_PCDR = (1 << AT91C_ID_UDPHS);
202 // 480MHZ
203 AT91C_BASE_CKGR->CKGR_UCKR &= ~AT91C_CKGR_UPLLEN;
204 #endif
205 }
206
207 //------------------------------------------------------------------------------
208 /// Handles a completed transfer on the given endpoint, invoking the
209 /// configured callback if any.
210 /// \param bEndpoint Number of the endpoint for which the transfer has completed.
211 /// \param bStatus Status code returned by the transfer operation
212 //------------------------------------------------------------------------------
213 static void UDPHS_EndOfTransfer( unsigned char bEndpoint, char bStatus )
214 {
215 Endpoint *pEndpoint = &(endpoints[bEndpoint]);
216 Transfer *pTransfer = &(pEndpoint->transfer);
217
218 // Check that endpoint was sending or receiving data
219 if( (pEndpoint->state == UDP_ENDPOINT_RECEIVING)
220 || (pEndpoint->state == UDP_ENDPOINT_SENDING) ) {
221
222 TRACE_DEBUG_WP("Eo");
223
224 // Endpoint returns in Idle state
225 pEndpoint->state = UDP_ENDPOINT_IDLE;
226
227 // Invoke callback is present
228 if (pTransfer->fCallback != 0) {
229
230 ((TransferCallback) pTransfer->fCallback)
231 (pTransfer->pArgument,
232 bStatus,
233 pTransfer->transferred,
234 pTransfer->remaining + pTransfer->buffered);
235 }
236 else {
237 TRACE_DEBUG_WP("No callBack\n\r");
238 }
239 }
240 }
241
242 //------------------------------------------------------------------------------
243 /// Clears the correct RX flag in endpoint status register
244 /// \param bEndpoint Index of endpoint
245 //------------------------------------------------------------------------------
246 static void UDPHS_ClearRxFlag( unsigned char bEndpoint )
247 {
248 AT91C_BASE_UDPHS->UDPHS_EPT[bEndpoint].UDPHS_EPTCLRSTA = AT91C_UDPHS_RX_BK_RDY;
249 }
250
251 //------------------------------------------------------------------------------
252 /// Transfers a data payload from the current tranfer buffer to the endpoint
253 /// FIFO
254 /// \param bEndpoint Number of the endpoint which is sending data.
255 //------------------------------------------------------------------------------
256 static void UDPHS_WritePayload( unsigned char bEndpoint )
257 {
258 Endpoint *pEndpoint = &(endpoints[bEndpoint]);
259 Transfer *pTransfer = &(pEndpoint->transfer);
260 char *pFifo;
261 signed int size;
262 unsigned int dCtr;
263
264 pFifo = (char*)((unsigned int *)AT91C_BASE_UDPHS_EPTFIFO + (16384 * bEndpoint));
265
266 // Get the number of bytes to send
267 size = pEndpoint->size;
268 if (size > pTransfer->remaining) {
269
270 size = pTransfer->remaining;
271 }
272
273 // Update transfer descriptor information
274 pTransfer->buffered += size;
275 pTransfer->remaining -= size;
276
277 // Write packet in the FIFO buffer
278 dCtr = 0;
279 while (size > 0) {
280
281 pFifo[dCtr] = *(pTransfer->pData);
282 pTransfer->pData++;
283 size--;
284 dCtr++;
285 }
286 }
287
288 //------------------------------------------------------------------------------
289 /// Transfers a data payload from an endpoint FIFO to the current transfer buffer
290 /// \param bEndpoint Endpoint number.
291 /// \param wPacketSize Size of received data packet
292 //------------------------------------------------------------------------------
293 static void UDPHS_ReadPayload( unsigned char bEndpoint, int wPacketSize )
294 {
295 Endpoint *pEndpoint = &(endpoints[bEndpoint]);
296 Transfer *pTransfer = &(pEndpoint->transfer);
297 char *pFifo;
298 unsigned char dBytes=0;
299
300 pFifo = (char*)((unsigned int *)AT91C_BASE_UDPHS_EPTFIFO + (16384 * bEndpoint));
301
302 // Check that the requested size is not bigger than the remaining transfer
303 if (wPacketSize > pTransfer->remaining) {
304
305 pTransfer->buffered += wPacketSize - pTransfer->remaining;
306 wPacketSize = pTransfer->remaining;
307 }
308
309 // Update transfer descriptor information
310 pTransfer->remaining -= wPacketSize;
311 pTransfer->transferred += wPacketSize;
312
313 // Retrieve packet
314 while (wPacketSize > 0) {
315
316 *(pTransfer->pData) = pFifo[dBytes];
317 pTransfer->pData++;
318 wPacketSize--;
319 dBytes++;
320 }
321 }
322
323
324 //------------------------------------------------------------------------------
325 /// Received SETUP packet from endpoint 0 FIFO
326 /// \param pRequest Generic USB SETUP request sent over Control endpoints
327 //------------------------------------------------------------------------------
328 static void UDPHS_ReadRequest( USBGenericRequest *pRequest )
329 {
330 unsigned int *pData = (unsigned int *)pRequest;
331 unsigned int fifo;
332
333 fifo = (AT91C_BASE_UDPHS_EPTFIFO->UDPHS_READEPT0[0]);
334 *pData = fifo;
335 fifo = (AT91C_BASE_UDPHS_EPTFIFO->UDPHS_READEPT0[0]);
336 pData++;
337 *pData = fifo;
338 //TRACE_ERROR("SETUP: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n\r", pData[0],pData[1],pData[2],pData[3],pData[4],pData[5],pData[6],pData[7]);
339 }
340
341 //------------------------------------------------------------------------------
342 /// Reset all endpoint transfer descriptors
343 //------------------------------------------------------------------------------
344 static void UDPHS_ResetEndpoints( void )
345 {
346 Endpoint *pEndpoint;
347 Transfer *pTransfer;
348 unsigned char bEndpoint;
349
350 // Reset the transfer descriptor of every endpoint
351 for( bEndpoint = 0; bEndpoint < BOARD_USB_NUMENDPOINTS; bEndpoint++ ) {
352
353 pEndpoint = &(endpoints[bEndpoint]);
354 pTransfer = &(pEndpoint->transfer);
355
356 // Reset endpoint transfer descriptor
357 pTransfer->pData = 0;
358 pTransfer->transferred = -1;
359 pTransfer->buffered = -1;
360 pTransfer->remaining = -1;
361 pTransfer->fCallback = 0;
362 pTransfer->pArgument = 0;
363
364 // Reset endpoint state
365 pEndpoint->bank = 0;
366 pEndpoint->state = UDP_ENDPOINT_DISABLED;
367 }
368 }
369
370
371 //------------------------------------------------------------------------------
372 /// Disable all endpoints (except control endpoint 0), aborting current
373 /// transfers if necessary
374 //------------------------------------------------------------------------------
375 static void UDPHS_DisableEndpoints( void )
376 {
377 unsigned char bEndpoint;
378
379 // Disable each endpoint, terminating any pending transfer
380 // Control endpoint 0 is not disabled
381 for( bEndpoint = 1; bEndpoint < BOARD_USB_NUMENDPOINTS; bEndpoint++ ) {
382
383 UDPHS_EndOfTransfer( bEndpoint, USBD_STATUS_ABORTED );
384 endpoints[bEndpoint].state = UDP_ENDPOINT_DISABLED;
385 }
386 }
387
388 //------------------------------------------------------------------------------
389 /// Endpoint interrupt handler.
390 /// Handle IN/OUT transfers, received SETUP packets and STALLing
391 /// \param bEndpoint Index of endpoint
392 //------------------------------------------------------------------------------
393 static void UDPHS_EndpointHandler( unsigned char bEndpoint )
394 {
395 Endpoint *pEndpoint = &(endpoints[bEndpoint]);
396 Transfer *pTransfer = &(pEndpoint->transfer);
397 unsigned int status = AT91C_BASE_UDPHS->UDPHS_EPT[bEndpoint].UDPHS_EPTSTA;
398 unsigned short wPacketSize;
399 USBGenericRequest request;
400
401 TRACE_DEBUG_WP("E%d ", bEndpoint);
402 TRACE_DEBUG_WP("st:0x%X ", status);
403
404 // Handle interrupts
405 // IN packet sent
406 if( (AT91C_UDPHS_TX_PK_RDY == (AT91C_BASE_UDPHS->UDPHS_EPT[bEndpoint].UDPHS_EPTCTL & AT91C_UDPHS_TX_PK_RDY))
407 && (0 == (status & AT91C_UDPHS_TX_PK_RDY )) ) {
408
409 TRACE_DEBUG_WP("Wr ");
410
411 // Check that endpoint was in Sending state
412 if( pEndpoint->state == UDP_ENDPOINT_SENDING ) {
413
414 if (pTransfer->buffered > 0) {
415 pTransfer->transferred += pTransfer->buffered;
416 pTransfer->buffered = 0;
417 }
418
419 if( ((pTransfer->buffered)==0)
420 &&((pTransfer->transferred)==0)
421 &&((pTransfer->remaining)==0)
422 &&(sendZLP == 0)) {
423 sendZLP = 1;
424 }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -