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