📄 usbd_otghs.lst
字号:
893
894 }
895
896 // End of bus reset
897 else if (dStatus & AT91C_OTGHS_EORST) {
898
899 TRACE_DEBUG_M("EoB ");
900 // The device enters the Default state
901 // MCK + UDPCK are already enabled
902 // Pull-Up is already connected
903 // Transceiver must be enabled
904 // Endpoint 0 must be enabled
905 SET(*(pUsb->pState), USB_STATE_DEFAULT);
906
907 OTGHS_EnableTransceiver(pUsb);
908
909 // The device leaves the Address & Configured states
910 CLEAR(*(pUsb->pState), USB_STATE_ADDRESS | USB_STATE_CONFIGURED);
911 OTGHS_ResetEndpoints(pUsb);
912 OTGHS_DisableEndpoints(pUsb);
913 OTGHS_ConfigureEndpoint(pUsb, 0);
914
915 // Flush and enable the Suspend interrupt
916 SET(pInterface->OTGHS_DEVICR, AT91C_OTGHS_WAKEUP | AT91C_OTGHS_SUSP);
917
918 // Enable the Start Of Frame (SOF) interrupt if needed
919 if (pUsb->pCallbacks->startOfFrame != 0) {
920
921 SET(pInterface->OTGHS_DEVIER, AT91C_OTGHS_SOF);
922 }
923
924 // Invoke the Reset callback
925 USB_ResetCallback(pUsb);
926
927 // Acknowledge end of bus reset interrupt
928 pInterface->OTGHS_DEVICR = AT91C_OTGHS_EORST;
929 }
930
931 // Handle upstream resume interrupt
932 else if (dStatus & AT91C_OTGHS_UPRSM) {
933
934 TRACE_DEBUG_L(" External resume interrupt\n\r");
935
936 // - Acknowledge the IT
937 pInterface->OTGHS_DEVICR = AT91C_OTGHS_UPRSM;
938 }
939
940 // Endpoint interrupts
941 else {
942 #ifndef DMA
943 // Handle endpoint interrupts
944 for (numIT = 0; numIT < NUM_IT_MAX; numIT++) {
945 if( dStatus & (1<<SHIFT_INTERUPT<<numIT) ) {
946 OTGHS_EndpointHandler(pUsb, numIT);
947 }
948 }
949 #else
950 // Handle endpoint control interrupt
951 if( dStatus & (1<<SHIFT_INTERUPT<<0) ) {
952 OTGHS_EndpointHandler(pUsb, 0);
953 }
954 // Handle DMA interrupts
955 for(numIT = 1; numIT <= NUM_IT_MAX_DMA; numIT++) {
956 if( dStatus & (1<<SHIFT_DMA<<numIT) ) {
957 OTGHS_DmaHandler(pUsb, numIT);
958 }
959 }
960 #endif
961 }
962
963 // Retrieve new interrupt status
964 dStatus = (pInterface->OTGHS_DEVISR) & (pInterface->OTGHS_DEVIMR);
965
966 // Mask unneeded interrupts
967 if (!ISSET(USB_GetState(pUsb), USB_STATE_DEFAULT)) {
968
969 dStatus &= AT91C_OTGHS_EORST | AT91C_OTGHS_SOF;
970 }
971
972 TRACE_DEBUG_H("\n\r");
973
974 if (dStatus != 0) {
975
976 TRACE_DEBUG_L(" - ");
977 }
978 }
979
980 if ( (!ISSET(USB_GetState(pUsb), USB_STATE_SUSPENDED))
981 && (ISSET(USB_GetState(pUsb), USB_STATE_POWERED))){
982
983 LED_TOGGLE(LED_USB);
984 }
985 }
986
987 //------------------------------------------------------------------------------
988 // \brief Sends data through an USB endpoint
989 //
990 // Sets up the transfer descriptor, write one or two data payloads
991 // (depending on the number of FIFO banks for the endpoint) and then
992 // starts the actual transfer. The operation is complete when all
993 // the data has been sent.
994 // \param pUsb Pointer to a S_usb instance
995 // \param bEndpoint Index of endpoint
996 // \param pData Pointer to a buffer containing the data to send
997 // \param dLength Length of the data buffer
998 // \param fCallback Optional function to invoke when the transfer finishes
999 // \param pArgument Optional argument for the callback function
1000 // \return Operation result code
1001 // \see Operation result codes
1002 // \see Callback_f
1003 // \see S_usb
1004 //------------------------------------------------------------------------------
1005 static char OTGHS_Write(const S_usb *pUsb,
1006 unsigned char bEndpoint,
1007 const void *pData,
1008 unsigned int dLength,
1009 Callback_f fCallback,
1010 void *pArgument)
1011 {
1012 S_usb_endpoint *pEndpoint = USB_GetEndpoint(pUsb, bEndpoint);
1013 AT91PS_OTGHS pInterface = OTGHS_GetDriverInterface(pUsb);
1014
1015 // Check that the endpoint is in Idle state
1016 if (pEndpoint->dState != endpointStateIdle) {
1017
1018 return USB_STATUS_LOCKED;
1019 }
1020
1021 TRACE_DEBUG_L("Write%d(%d) ", bEndpoint, dLength);
1022
1023 // Setup the transfer descriptor
1024 pEndpoint->pData = (char *) pData;
1025 pEndpoint->dBytesRemaining = dLength;
1026 pEndpoint->dBytesBuffered = 0;
1027 pEndpoint->dBytesTransferred = 0;
1028 pEndpoint->fCallback = fCallback;
1029 pEndpoint->pArgument = pArgument;
1030 pEndpoint->isDataSent = false;
1031
1032 // Send one packet
1033 pEndpoint->dState = endpointStateWrite;
1034
1035 #ifdef DMA
1036 // Test if endpoint type control
1037 if (AT91C_OTGHS_EPT_TYPE_CTL_EPT == (AT91C_OTGHS_EPT_TYPE & pInterface->OTGHS_DEVEPTCFG[bEndpoint])) {
1038 #endif
1039 // Enable endpoint IT
1040 pInterface->OTGHS_DEVIER = (1<<SHIFT_INTERUPT<<bEndpoint);
1041 pInterface->OTGHS_DEVEPTCER[bEndpoint] = AT91C_OTGHS_TXINI;
1042
1043 #ifdef DMA
1044 }
1045 else {
1046
1047 // others endoint (not control)
1048 pEndpoint->dBytesBuffered = pEndpoint->dBytesRemaining;
1049 pEndpoint->dBytesRemaining = 0;
1050
1051 pInterface->OTGHS_DEVDMA[bEndpoint].OTGHS_DEVDMAADDRESS = (unsigned int) pEndpoint->pData;
1052
1053 // Enable IT DMA
1054 pInterface->OTGHS_DEVIER = (1<<SHIFT_DMA<<bEndpoint);
1055
1056 pInterface->OTGHS_DEVDMA[bEndpoint].OTGHS_DEVDMACONTROL =
1057 (((pEndpoint->dBytesBuffered<<16)&AT91C_OTGHS_BUFF_LENGTH)
1058 | AT91C_OTGHS_END_B_EN
1059 | AT91C_OTGHS_END_BUFFIT
1060 | AT91C_OTGHS_CHANN_ENB);
1061
1062 }
1063 #endif
1064
1065 return USB_STATUS_SUCCESS;
1066 }
1067
1068 //------------------------------------------------------------------------------
1069 // \brief Reads incoming data on an USB endpoint
1070 //
1071 // This methods sets the transfer descriptor and activate the endpoint
1072 // interrupt. The actual transfer is then carried out by the endpoint
1073 // interrupt handler. The Read operation finishes either when the
1074 // buffer is full, or a short packet (inferior to endpoint maximum
1075 // packet size) is received.
1076 // \param pUsb Pointer to a S_usb instance
1077 // \param bEndpoint Index of endpoint
1078 // \param pData Pointer to a buffer to store the received data
1079 // \param dLength Length of the receive buffer
1080 // \param fCallback Optional callback function
1081 // \param pArgument Optional callback argument
1082 // \return Operation result code
1083 // \see Callback_f
1084 // \see S_usb
1085 //------------------------------------------------------------------------------
1086 static char OTGHS_Read(const S_usb *pUsb,
1087 unsigned char bEndpoint,
1088 void *pData,
1089 unsigned int dLength,
1090 Callback_f fCallback,
1091 void *pArgument)
1092 {
1093 AT91PS_OTGHS pInterface = OTGHS_GetDriverInterface(pUsb);
1094 S_usb_endpoint *pEndpoint = USB_GetEndpoint(pUsb, bEndpoint);
1095
1096 //! Return if the endpoint is not in IDLE state
1097 if (pEndpoint->dState != endpointStateIdle) {
1098
1099 return USB_STATUS_LOCKED;
1100 }
1101
1102 TRACE_DEBUG_M("Read%d(%d) ", bEndpoint, dLength);
1103
1104 // Endpoint enters Read state
1105 pEndpoint->dState = endpointStateRead;
1106
1107 //! Set the transfer descriptor
1108 pEndpoint->pData = (char *) pData;
1109 pEndpoint->dBytesRemaining = dLength;
1110 pEndpoint->dBytesBuffered = 0;
1111 pEndpoint->dBytesTransferred = 0;
1112 pEndpoint->fCallback = fCallback;
1113 pEndpoint->pArgument = pArgument;
1114
1115 #ifdef DMA
1116 // Test if endpoint type control
1117 if (AT91C_OTGHS_EPT_TYPE_CTL_EPT == (AT91C_OTGHS_EPT_TYPE & pInterface->OTGHS_DEVEPTCFG[bEndpoint])) {
1118 #endif
1119 // Control endpoint
1120 // Enable endpoint IT
1121 pInterface->OTGHS_DEVIER = (1<<SHIFT_INTERUPT<<bEndpoint);
1122 pInterface->OTGHS_DEVEPTCER[bEndpoint] = AT91C_OTGHS_RXOUT;
1123 #ifdef DMA
1124 }
1125 else {
1126
1127 // others endoint (not control)
1128 pEndpoint->dBytesBuffered =
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -