usb_rw.c
上传用户:yyyd609
上传日期:2022-07-18
资源大小:183k
文件大小:3k
源码类别:

微处理器开发

开发平台:

C/C++

  1. /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  2. PROJECT  : ARM7 USB Core
  3. COMPILER : ADS 1.0
  4. MODULE  :  USB_rw.c
  5. AUTHOR :  Stefano Ballarin - TXT
  6. -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  7. DESCRIPTION : 
  8. -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  9. MODIFICATIONS :
  10. -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
  11. #include "USB_lib.h"
  12. /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
  13. NAME: UsbWrite
  14. INPUT:
  15. EPnum - the endpoint number to be sent
  16. pEPinfo - the address of an ENDPOINT_INFO structure
  17. This structure controls the data sending
  18. OUTPUT:
  19. DESCRIPTION:
  20. Calls pEPinfo->CopyData() to copy data to the transmit buffer and send them
  21.  *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
  22. void UsbWrite(BYTE EPnum, ENDPOINT_INFO *pEPinfo)
  23. {
  24. BYTE *DataBuffer;
  25. BYTE *(*CopyRoutine)(WORD);
  26. WORD Length;
  27. Length = pEPinfo->PacketSize;    // get size of the packet 
  28. if (Length > pEPinfo->Usb_wLength) // how many data to be sent?
  29. Length = pEPinfo->Usb_wLength; // if less than packet size ...
  30.    // it is last packet
  31.    
  32. CopyRoutine = pEPinfo->CopyData;  // get copy routine address
  33. DataBuffer = (*CopyRoutine)(Length); // get address of data buffer
  34. // mSetEPTx(EPnum, DataBuffer, Length);
  35. //sb sistemare SetEPTxAddr(EPnum, DataBuffer);  // set parameters for dma transfer
  36. SetEPTxCount(EPnum, Length);
  37. pEPinfo->Usb_wLength -= Length;    // update number of data to send
  38. pEPinfo->Usb_wOffset += Length;    // update offset in data buffer
  39. SetEPTxStatus(EPnum, EP_TX_VALID);
  40. }
  41. /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
  42. NAME: UsbRead
  43. INPUT:
  44. Source - the address of endpoint receiving buffer
  45. pEPinfo - the address of an ENDPOINT_INFO structure
  46. This structure controls the data receiving
  47. OUTPUT:
  48. DESCRIPTION:
  49. Copy received data for endpoint Rx buffer to user buffer
  50. User buffer address is obtained by calling pEPinfo->CopyData()
  51. The buffer returned by CopyData(Length) must be capable of Length bytes.
  52.  *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
  53. void UsbRead0(BYTE EPnum, ENDPOINT_INFO *pEPinfo)
  54. {
  55. WORD size = pEPinfo->PacketSize; // get size of the packet
  56. BYTE *Buffer;  
  57. if (size > pEPinfo->Usb_rLength) { // how many data to be received ?
  58. size = pEPinfo->Usb_rLength;   // if less than packet size ...
  59. pEPinfo->PacketSize = size;    // it is last packet
  60. }
  61. Buffer = (*pEPinfo->CopyData)(size); // get address of data buffer
  62. // mSetEPRx(EPnum, Buffer, size);
  63. //sb sistemare SetEPRxAddr(EPnum, Buffer);  // set parameters for dma transfer
  64. SetEPRxCount(EPnum, size);
  65. SetEPRxStatus(EPnum, EP_RX_VALID);
  66. }
  67. void UsbRead(BYTE EPnum, ENDPOINT_INFO *pEPinfo)
  68. {
  69. WORD Length;
  70. if (pEPinfo->Usb_rLength == 0) // no data to transfer
  71. return;
  72. Length = pEPinfo->PacketSize - GetEPRxCount(EPnum);   //get no. of received data
  73. if (Length == 0)
  74. return;
  75. pEPinfo->Usb_rLength -= Length;   // update number of data to receive
  76. pEPinfo->Usb_rOffset += Length;   // update offset in data buffer
  77. UsbRead0(EPnum, pEPinfo);   // set parameters for dma transfer
  78. }