/******************************************************************************* MASS_Reset: Mass Storage reset routine. *******************************************************************************/ void MASS_Reset() { Device_Info.Current_Configuration = 0; // Set the device as not configured pInformation->Current_Feature = MASS_ConfigDescriptor[7]; // Current Feature initialization SetBTABLE(BTABLE_ADDRESS); // Initialize Endpoint 0 SetEPType(ENDP0, EP_CONTROL); SetEPTxStatus(ENDP0, EP_TX_NAK); SetEPRxAddr(ENDP0, ENDP0_RXADDR); SetEPRxCount(ENDP0, Device_Property.MaxPacketSize); SetEPTxAddr(ENDP0, ENDP0_TXADDR); Clear_Status_Out(ENDP0); SetEPRxValid(ENDP0); //Initialize Endpoint 1 SetEPType(ENDP1, EP_BULK); SetEPTxAddr(ENDP1, ENDP1_TXADDR); SetEPTxStatus(ENDP1, EP_TX_NAK); SetEPRxStatus(ENDP1, EP_RX_DIS); // Initialize Endpoint 2 SetEPType(ENDP2, EP_BULK); SetEPRxAddr(ENDP2, ENDP2_RXADDR); SetEPRxCount(ENDP2, Device_Property.MaxPacketSize); SetEPRxStatus(ENDP2, EP_RX_VALID); SetEPTxStatus(ENDP2, EP_TX_DIS); SetEPRxCount(ENDP0, Device_Property.MaxPacketSize); SetEPRxValid(ENDP0); // Set the device to response on default address SetDeviceAddress(0); bDeviceState = ATTACHED; CBW.dSignature = BOT_CBW_SIGNATURE; Bot_State = BOT_IDLE; }
/******************************************************************************* * Function Name : Mass_Storage_In * Description : Mass Storage IN transfer. * Input : None. * Output : None. * Return : None. //设备->USB *******************************************************************************/ void Mass_Storage_In (void) { USB_STATUS_REG|=0X10;//标记轮询 //printf("Bot_State_in:%d",Bot_State); switch (Bot_State) { case BOT_CSW_Send: case BOT_ERROR: Bot_State = BOT_IDLE; SetEPRxStatus(ENDP4, EP_RX_VALID);/* enable the Endpoint to recive the next cmd*/ break; case BOT_DATA_IN: //USB从设备读数据 switch (CBW.CB[0]) { case SCSI_READ10: USB_STATUS_REG|=0X02;//标记正在读数据 SCSI_Read10_Cmd(CBW.bLUN , SCSI_LBA , SCSI_BlkLen); break; } break; case BOT_DATA_IN_LAST: Set_CSW (CSW_CMD_PASSED, SEND_CSW_ENABLE); SetEPRxStatus(ENDP4, EP_RX_VALID); break; default: break; } }
/******************************************************************************* * Function Name : Virtual_Com_Port_Reset * Description : Virtual_Com_Port Mouse reset routine * Input : None. * Output : None. * Return : None. *******************************************************************************/ void Virtual_Com_Port_Reset(void) { /* Set Virtual_Com_Port DEVICE as not configured */ pInformation->Current_Configuration = 0; /* Current Feature initialization */ pInformation->Current_Feature = Virtual_Com_Port_ConfigDescriptor[7]; /* Set Virtual_Com_Port DEVICE with the default Interface*/ pInformation->Current_Interface = 0; #ifdef STM32F10X_CL /* EP0 is already configured by USB_SIL_Init() function */ /* Init EP1 IN as Bulk endpoint */ OTG_DEV_EP_Init(EP1_IN, OTG_DEV_EP_TYPE_BULK, VIRTUAL_COM_PORT_DATA_SIZE); /* Init EP2 IN as Interrupt endpoint */ OTG_DEV_EP_Init(EP2_IN, OTG_DEV_EP_TYPE_INT, VIRTUAL_COM_PORT_INT_SIZE); /* Init EP3 OUT as Bulk endpoint */ OTG_DEV_EP_Init(EP3_OUT, OTG_DEV_EP_TYPE_BULK, VIRTUAL_COM_PORT_DATA_SIZE); #else SetBTABLE(BTABLE_ADDRESS); /* Initialize Endpoint 0 */ SetEPType(ENDP0, EP_CONTROL); SetEPTxStatus(ENDP0, EP_TX_STALL); SetEPRxAddr(ENDP0, ENDP0_RXADDR); SetEPTxAddr(ENDP0, ENDP0_TXADDR); Clear_Status_Out(ENDP0); SetEPRxCount(ENDP0, Device_Property.MaxPacketSize); SetEPRxValid(ENDP0); /* Initialize Endpoint 1 */ SetEPType(ENDP1, EP_BULK); SetEPTxAddr(ENDP1, ENDP1_TXADDR); SetEPTxStatus(ENDP1, EP_TX_NAK); SetEPRxStatus(ENDP1, EP_RX_DIS); /* Initialize Endpoint 2 */ SetEPType(ENDP2, EP_INTERRUPT); SetEPTxAddr(ENDP2, ENDP2_TXADDR); SetEPRxStatus(ENDP2, EP_RX_DIS); SetEPTxStatus(ENDP2, EP_TX_NAK); /* Initialize Endpoint 3 */ SetEPType(ENDP3, EP_BULK); SetEPRxAddr(ENDP3, ENDP3_RXADDR); SetEPRxCount(ENDP3, VIRTUAL_COM_PORT_DATA_SIZE); SetEPRxStatus(ENDP3, EP_RX_VALID); SetEPTxStatus(ENDP3, EP_TX_DIS); /* Set this device to response on default address */ SetDeviceAddress(0); #endif /* STM32F10X_CL */ bDeviceState = ATTACHED; }
vsf_err_t stm32_usbd_ep_set_OUT_epsize(uint8_t idx, uint16_t epsize) { bool ep0; int8_t index; index = stm32_usbd_get_ep(idx); if (index < 0) { return VSFERR_FAIL; } ep0 = 0 == idx; idx = (uint8_t)index; if ((EP_Cfg_Ptr - epsize) < STM32_USBD_EP_NUM * 8) { return VSFERR_NOT_ENOUGH_RESOURCES; } stm32_usbd_OUT_epsize[idx] = epsize; SetEPRxCount(idx, epsize); // fix for 16-bit aligned memory EP_Cfg_Ptr -= epsize & 1 ? epsize + 1 : epsize; SetEPRxAddr(idx, EP_Cfg_Ptr); if (ep0) { SetEPRxStatus(idx, EP_RX_VALID); } else { SetEPRxStatus(idx, EP_RX_NAK); } return VSFERR_NONE; }
/******************************************************************************* * Function Name : Mass_Storage_In * Description : Mass Storage IN transfer. * Input : None. * Output : None. * Return : None. *******************************************************************************/ void Mass_Storage_In (void) { USB_STATUS_REG|=0X10;//标记轮询 switch (Bot_State) { case BOT_CSW_Send: case BOT_ERROR: Bot_State = BOT_IDLE; SetEPRxStatus(ENDP2, EP_RX_VALID);/* enable the Endpoint to receive the next cmd*/ if (GetEPRxStatus(EP2_OUT) == EP_RX_STALL) { SetEPRxStatus(EP2_OUT, EP_RX_VALID);/* enable the Endpoint to receive the next cmd*/ } break; case BOT_DATA_IN: switch (CBW.CB[0]) { case SCSI_READ10: USB_STATUS_REG|=0X02;//标记正在读数据 SCSI_Read10_Cmd(CBW.bLUN , SCSI_LBA , SCSI_BlkLen); break; } break; case BOT_DATA_IN_LAST: Set_CSW (CSW_CMD_PASSED, SEND_CSW_ENABLE); SetEPRxStatus(ENDP2, EP_RX_VALID); break; default: break; } }
/******************************************************************************* * Function Name : Joystick_Reset. * Description : Joystick Mouse reset routine. * Input : None. * Output : None. * Return : None. *******************************************************************************/ void Joystick_Reset(void) { /* Set Joystick_DEVICE as not configured */ pInformation->Current_Configuration = 0; pInformation->Current_Interface = 0;/*the default Interface*/ /* Current Feature initialization */ pInformation->Current_Feature = Joystick_ConfigDescriptor[7]; SetBTABLE(BTABLE_ADDRESS); /* Initialize Endpoint 0 */ SetEPType(ENDP0, EP_CONTROL); SetEPTxStatus(ENDP0, EP_TX_STALL); SetEPRxAddr(ENDP0, ENDP0_RXADDR); SetEPTxAddr(ENDP0, ENDP0_TXADDR); Clear_Status_Out(ENDP0); SetEPRxCount(ENDP0, Device_Property.MaxPacketSize); SetEPRxValid(ENDP0); /* Initialize Endpoint 1 */ SetEPType(ENDP1, EP_INTERRUPT); SetEPTxAddr(ENDP1, ENDP1_TXADDR); SetEPTxCount(ENDP1, 4); SetEPRxStatus(ENDP1, EP_RX_DIS); SetEPTxStatus(ENDP1, EP_TX_NAK); /* Initialize Endpoint 2 */ SetEPType(ENDP2, EP_INTERRUPT); SetEPRxAddr(ENDP2, ENDP2_RXADDR); SetEPRxCount(ENDP2, 64); SetEPRxStatus(ENDP2, EP_RX_VALID); SetEPTxStatus(ENDP2, EP_TX_DIS); /* Set this device to response on default address */ SetDeviceAddress(0); bDeviceState = ATTACHED; }
/******************************************************************************* * Function Name : Mass_Storage_In * Description : Mass Storage IN transfer. * Input : None. * Output : None. * Return : None. *******************************************************************************/ void Mass_Storage_In (void) { switch (Bot_State) { case BOT_CSW_Send: case BOT_ERROR: Bot_State = BOT_IDLE; SetEPRxStatus(ENDP2, EP_RX_VALID);/* enable the Endpoint to recive the next cmd*/ break; case BOT_DATA_IN: switch (CBW.CB[0]) { case SCSI_READ10: SCSI_Read10_Cmd(SCSI_LBA , SCSI_BlkLen); break; } break; case BOT_DATA_IN_LAST: Set_CSW (CSW_CMD_PASSED, SEND_CSW_ENABLE); SetEPRxStatus(ENDP2, EP_RX_VALID); break; default: break; } }
// Ресет USB void SomeDev_Reset() { // Установка девайса в несконфигурированное состояние Device_Info.Current_Configuration = 0; // Инициализация CurrentFeature pInformation->Current_Feature = SomeDev_ConfigDescriptor[7]; #ifdef STM32F10X_CL // EP0 уже сконфигурирован в USB_SIL_Init() // $USBCONFIG - инициализация ендпойнтов // EP1 IN - Bulk endpoint OTG_DEV_EP_Init(EP1_IN, OTG_DEV_EP_TYPE_BULK, BULK_MAX_PACKET_SIZE); // EP2 OUT - Bulk endpoint OTG_DEV_EP_Init(EP2_OUT, OTG_DEV_EP_TYPE_BULK, BULK_MAX_PACKET_SIZE); #else // $USBCONFIG - инициализация ендпойнтов SetBTABLE(BTABLE_ADDRESS); // Инициализация Endpoint 0 SetEPType(ENDP0, EP_CONTROL); SetEPTxStatus(ENDP0, EP_TX_NAK); SetEPRxAddr(ENDP0, ENDP0_RXADDR); SetEPRxCount(ENDP0, Device_Property.MaxPacketSize); SetEPTxAddr(ENDP0, ENDP0_TXADDR); Clear_Status_Out(ENDP0); SetEPRxValid(ENDP0); // Инициализация Endpoint 1 SetEPType(ENDP1, EP_BULK); SetEPTxAddr(ENDP1, ENDP1_TXADDR); SetEPTxStatus(ENDP1, EP_TX_NAK); SetEPRxStatus(ENDP1, EP_RX_DIS); // Инициализация Endpoint 2 SetEPType(ENDP2, EP_BULK); SetEPRxAddr(ENDP2, ENDP2_RXADDR); SetEPRxCount(ENDP2, Device_Property.MaxPacketSize); SetEPRxStatus(ENDP2, EP_RX_VALID); SetEPTxStatus(ENDP2, EP_TX_DIS); SetEPRxCount(ENDP0, Device_Property.MaxPacketSize); SetEPRxValid(ENDP0); // Установка дефолтного адреса SetDeviceAddress(0); #endif /} bDeviceState = ATTACHED; }
/******************************************************************************* * Function Name : MASS_Reset * Description : Mass Storage reset routine. * Input : None. * Output : None. * Return : None. *******************************************************************************/ void MASS_Reset() { /* Set the device as not configured */ Device_Info.Current_Configuration = 0; /* Current Feature initialization */ pInformation->Current_Feature = MASS_ConfigDescriptor[7]; #ifdef STM32F10X_CL /* EP0 is already configured by USB_SIL_Init() function */ /* Init EP1 IN as Bulk endpoint */ OTG_DEV_EP_Init(EP1_IN, OTG_DEV_EP_TYPE_BULK, BULK_MAX_PACKET_SIZE); /* Init EP2 OUT as Bulk endpoint */ OTG_DEV_EP_Init(EP2_OUT, OTG_DEV_EP_TYPE_BULK, BULK_MAX_PACKET_SIZE); #else SetBTABLE(BTABLE_ADDRESS); /* Initialize Endpoint 0 */ SetEPType(ENDP0, EP_CONTROL); SetEPTxStatus(ENDP0, EP_TX_NAK); SetEPRxAddr(ENDP0, ENDP0_RXADDR); SetEPRxCount(ENDP0, Device_Property.MaxPacketSize); SetEPTxAddr(ENDP0, ENDP0_TXADDR); Clear_Status_Out(ENDP0); SetEPRxValid(ENDP0); /* Initialize Endpoint 1 */ SetEPType(ENDP1, EP_BULK); SetEPTxAddr(ENDP1, ENDP1_TXADDR); SetEPTxStatus(ENDP1, EP_TX_NAK); SetEPRxStatus(ENDP1, EP_RX_DIS); /* Initialize Endpoint 2 */ SetEPType(ENDP2, EP_BULK); SetEPRxAddr(ENDP2, ENDP2_RXADDR); SetEPRxCount(ENDP2, Device_Property.MaxPacketSize); SetEPRxStatus(ENDP2, EP_RX_VALID); SetEPTxStatus(ENDP2, EP_TX_DIS); SetEPRxCount(ENDP0, Device_Property.MaxPacketSize); SetEPRxValid(ENDP0); /* Set the device to response on default address */ SetDeviceAddress(0); #endif /* STM32F10X_CL */ bDeviceState = ATTACHED; CBW.dSignature = BOT_CBW_SIGNATURE; Bot_State = BOT_IDLE; //USB_NotConfigured_LED(); }
/******************************************************************************* * Function Name : Standard_ClearFeature. * Description : Clear or disable a specific feature. * Input : None. * Output : None. * Return : - Return USB_SUCCESS, if the request is performed. * - Return USB_UNSUPPORT, if the request is invalid. *******************************************************************************/ RESULT Standard_ClearFeature(void) { uint32_t Type_Rec = Type_Recipient; uint32_t Status; if (Type_Rec == (STANDARD_REQUEST | DEVICE_RECIPIENT)) { // Device Clear Feature ClrBit(pInformation->Current_Feature, 5); return USB_SUCCESS; } else if (Type_Rec == (STANDARD_REQUEST | ENDPOINT_RECIPIENT)) { // EndPoint Clear Feature DEVICE* pDev; uint32_t Related_Endpoint; uint32_t wIndex0; uint32_t rEP; if ((pInformation->USBwValue != ENDPOINT_STALL) || (pInformation->USBwIndex1 != 0)) { return USB_UNSUPPORT; } pDev = &Device_Table; wIndex0 = pInformation->USBwIndex0; rEP = wIndex0 & ~0x80; Related_Endpoint = ENDP0 + rEP; if (ValBit(pInformation->USBwIndex0, 7)) { // Get Status of endpoint & stall the request if the related_ENdpoint is Disabled Status = GetEPTxStatus(Related_Endpoint); } else { Status = GetEPRxStatus(Related_Endpoint); } if ((rEP >= pDev->Total_Endpoint) || (Status == 0) || (pInformation->Current_Configuration == 0)) { return USB_UNSUPPORT; } if (wIndex0 & 0x80) { // IN endpoint if (GetTxStallStatus(Related_Endpoint )) { ClearDTOG_TX(Related_Endpoint); SetEPTxStatus(Related_Endpoint, EP_TX_VALID); } } else { // OUT endpoint if (GetRxStallStatus(Related_Endpoint)) { if (Related_Endpoint == ENDP0) { // After clear the STALL, enable the default endpoint receiver SetEPRxCount(Related_Endpoint, Device_Property.MaxPacketSize); SetEPRxStatus(Related_Endpoint, EP_RX_VALID); } else { ClearDTOG_RX(Related_Endpoint); SetEPRxStatus(Related_Endpoint, EP_RX_VALID); } } } pUser_Standard_Requests->User_ClearFeature(); return USB_SUCCESS; } return USB_UNSUPPORT; }
/** * @brief called when an EP is disabled * @param pdev: device instance * @param ep_addr: endpoint address * @retval : status */ uint32_t DCD_EP_Close(USB_CORE_HANDLE *pdev , uint8_t ep_addr) { USB_EP *ep; if ((ep_addr&0x80) == 0x80) { ep = &pdev->dev.in_ep[ep_addr & 0x7F]; } else { ep = &pdev->dev.out_ep[ep_addr & 0x7F]; } if (ep->doublebuffer == 0) { if (ep->is_in) { ClearDTOG_TX(ep->num); /* Configure DISABLE status for the Endpoint*/ SetEPTxStatus(ep->num, EP_TX_DIS); } else { ClearDTOG_RX(ep->num); /* Configure DISABLE status for the Endpoint*/ SetEPRxStatus(ep->num, EP_RX_DIS); } } /*Double Buffer*/ else { if (ep->is_in==0) { /* Clear the data toggle bits for the endpoint IN/OUT*/ ClearDTOG_RX(ep->num); ClearDTOG_TX(ep->num); /* Reset value of the data toggle bits for the endpoint out*/ ToggleDTOG_TX(ep->num); SetEPRxStatus(ep->num, EP_RX_DIS); SetEPTxStatus(ep->num, EP_TX_DIS); } else { /* Clear the data toggle bits for the endpoint IN/OUT*/ ClearDTOG_RX(ep->num); ClearDTOG_TX(ep->num); ToggleDTOG_RX(ep->num); /* Configure DISABLE status for the Endpoint*/ SetEPTxStatus(ep->num, EP_TX_DIS); SetEPRxStatus(ep->num, EP_RX_DIS); } } return USB_OK; }
static void PIOS_USB_CDC_DATA_EP_OUT_Callback(void) { struct pios_usb_cdc_dev * usb_cdc_dev = (struct pios_usb_cdc_dev *)pios_usb_cdc_id; bool valid = PIOS_USB_CDC_validate(usb_cdc_dev); PIOS_Assert(valid); uint32_t DataLength; /* Get the number of received data on the selected Endpoint */ DataLength = GetEPRxCount(usb_cdc_dev->cfg->data_rx_ep); if (DataLength > sizeof(usb_cdc_dev->rx_packet_buffer)) { usb_cdc_dev->rx_oversize++; DataLength = sizeof(usb_cdc_dev->rx_packet_buffer); } /* Use the memory interface function to read from the selected endpoint */ PMAToUserBufferCopy((uint8_t *) usb_cdc_dev->rx_packet_buffer, GetEPRxAddr(usb_cdc_dev->cfg->data_rx_ep), DataLength); if (!usb_cdc_dev->rx_in_cb) { /* No Rx call back registered, disable the receiver */ SetEPRxStatus(usb_cdc_dev->cfg->data_rx_ep, EP_RX_NAK); return; } uint16_t headroom; bool need_yield = false; uint16_t rc; rc = (usb_cdc_dev->rx_in_cb)(usb_cdc_dev->rx_in_context, usb_cdc_dev->rx_packet_buffer, DataLength, &headroom, &need_yield); if (rc < DataLength) { /* Lost bytes on rx */ usb_cdc_dev->rx_dropped += (DataLength - rc); } if (headroom >= sizeof(usb_cdc_dev->rx_packet_buffer)) { /* We have room for a maximum length message */ SetEPRxStatus(usb_cdc_dev->cfg->data_rx_ep, EP_RX_VALID); } else { /* Not enough room left for a message, apply backpressure */ SetEPRxStatus(usb_cdc_dev->cfg->data_rx_ep, EP_RX_NAK); } #if defined(PIOS_INCLUDE_FREERTOS) if (need_yield) { vPortYieldFromISR(); } #endif /* PIOS_INCLUDE_FREERTOS */ }
void usbReset(void) { pInformation->Current_Configuration = 0; /* current feature is current bmAttributes */ pInformation->Current_Feature = (USB_CONFIG_ATTR_BUSPOWERED | USB_CONFIG_ATTR_SELF_POWERED); _SetBTABLE(USB_BTABLE_ADDRESS); /* setup control endpoint 0 */ _SetEPType(ENDP0, EP_CONTROL); _SetEPTxStatus(ENDP0, EP_TX_STALL); _SetEPRxAddr(ENDP0,VCOM_CTRL_RX_ADDR); _SetEPTxAddr(ENDP0,VCOM_CTRL_TX_ADDR); Clear_Status_Out(ENDP0); SetEPRxCount(ENDP0, pProperty->MaxPacketSize); SetEPRxValid(ENDP0); /* setup management endpoint 1 */ SetEPType (VCOM_NOTIFICATION_ENDP, EP_INTERRUPT); SetEPTxAddr (VCOM_NOTIFICATION_ENDP, VCOM_NOTIFICATION_ADDR); SetEPTxStatus (VCOM_NOTIFICATION_ENDP, EP_TX_NAK); SetEPRxStatus (VCOM_NOTIFICATION_ENDP, EP_RX_DIS); /* setup data endpoint OUT (rx) */ /* SetEPType (VCOM_RX_ENDP, EP_BULK); */ /* SetEPRxAddr (VCOM_RX_ENDP, VCOM_RX_ADDR); */ /* SetEPRxCount (VCOM_RX_ENDP, VCOM_RX_EPSIZE); */ /* // SetEPTxStatus (VCOM_RX_ENDP, EP_TX_DIS); */ /* SetEPRxStatus (VCOM_RX_ENDP, EP_RX_VALID); */ SetEPType (3, EP_BULK); SetEPRxAddr (3, 0x110); SetEPRxCount (3,64); // SetEPTxStatus (VCOM_RX_ENDP, EP_TX_DIS); SetEPRxStatus (3, EP_RX_VALID); /* setup data endpoint IN (tx) */ SetEPType (VCOM_TX_ENDP, EP_BULK); SetEPTxAddr (VCOM_TX_ENDP, VCOM_TX_ADDR); SetEPTxStatus (VCOM_TX_ENDP, EP_TX_NAK); SetEPRxStatus (VCOM_TX_ENDP, EP_RX_DIS); bDeviceState = ATTACHED; SetDeviceAddress(0); /* reset the rx fifo */ recvBufIn = 0; recvBufOut = 0; maxNewBytes = VCOM_RX_EPSIZE; countTx = 0; }
/** * @brief Set the EP Status * @param pdev : Selected device * Status : new Status * epnum : EP address * @retval : None */ void DCD_SetEPStatus (USB_CORE_HANDLE *pdev , uint8_t epnum , uint32_t Status) { USB_EP *ep; if ((0x80 & epnum) == 0x80) { ep = &pdev->dev.in_ep[epnum & 0x7F]; } else { ep = &pdev->dev.out_ep[epnum]; } if (ep->is_in) { SetEPTxStatus(ep->num, (uint16_t)Status); } else { SetEPRxStatus(ep->num, (uint16_t)Status); } if ((Status == EP_RX_STALL) || (Status == EP_TX_STALL)) { ep->is_stall =1; } }
/******************************************************************************* * Function Name : SCSI_Write10_Cmd * Description : SCSI Write10 Command routine. * Input : None. * Output : None. * Return : None. *******************************************************************************/ void SCSI_Write10_Cmd(u32 LBA , u32 BlockNbr) { if (Bot_State == BOT_IDLE) { if (!(SCSI_Address_Management(SCSI_WRITE10 , LBA, BlockNbr)))/*address out of range*/ { return; } if ((CBW.bmFlags & 0x80) == 0) { Bot_State = BOT_DATA_OUT; SetEPRxStatus(ENDP2, EP_RX_VALID); } else { Bot_Abort(DIR_IN); Set_Scsi_Sense_Data(ILLEGAL_REQUEST, INVALID_FIELED_IN_COMMAND); Set_CSW (CSW_CMD_FAILED, SEND_CSW_DISABLE); } return; } else if (Bot_State == BOT_DATA_OUT) { Write_Memory(LBA , BlockNbr); } }
/******************************************************************************* * Function Name : Write_Memory * Description : Handle the Write operation to the microSD card. * Input : None. * Output : None. * Return : None. *******************************************************************************/ void Write_Memory (uint8_t lun, uint32_t Memory_Offset, uint32_t Transfer_Length) { if (TransferState == TXFR_IDLE ) { Block_offset = Memory_Offset * Mass_Block_Size[lun]; Counter = Transfer_Length * Mass_Block_Size[lun]; while (Transfer_Length--) MAL_Clear(lun, Block_offset + Mass_Block_Size[lun] * Transfer_Length); TransferState = TXFR_ONGOING; } if (TransferState == TXFR_ONGOING ) { MAL_Write(lun, Block_offset, (uint32_t *)Bulk_Data_Buff, BULK_MAX_PACKET_SIZE); Block_offset += BULK_MAX_PACKET_SIZE; Counter -= BULK_MAX_PACKET_SIZE; CSW.dDataResidue -= Data_Len; SetEPRxStatus(ENDP2, EP_RX_VALID); /* enable the next transaction*/ } if (Counter == 0 || (Bot_State == BOT_CSW_Send)) { Set_CSW (CSW_CMD_PASSED, SEND_CSW_ENABLE); TransferState = TXFR_IDLE; } }
//非标准协议 void EP1_OUT_Callback(void) { u8 TMP_Buffer[2],i; u8 reclen; if (USB_TIMEOUT_CNT==USB_TIMEOUT_VALUE) { USB_Buffer_INDEX=0; } USB_TIMEOUT_CNT=1; if (USB_Buffer_INDEX<2) { PMAToUserBufferCopy(USB_BUFFER+USB_Buffer_INDEX, ENDP1_RXADDR, 0x40); reclen=USB_BUFFER[USB_Buffer_INDEX+1]; for (i=0;i<0x40;i++) { USB_BUFFER[USB_Buffer_INDEX+i]=USB_BUFFER[USB_Buffer_INDEX+i+2]; } } else { TMP_Buffer[0]=USB_BUFFER[USB_Buffer_INDEX-2]; TMP_Buffer[1]=USB_BUFFER[USB_Buffer_INDEX-1]; PMAToUserBufferCopy(USB_BUFFER+USB_Buffer_INDEX-2, ENDP1_RXADDR, 0x40); reclen=USB_BUFFER[USB_Buffer_INDEX-2+1]; USB_BUFFER[USB_Buffer_INDEX-2]=TMP_Buffer[0]; USB_BUFFER[USB_Buffer_INDEX-1]=TMP_Buffer[1]; } USB_Buffer_INDEX+=reclen; if ((USB_BUFFER[0]==USB_Buffer_INDEX-1)) USB_TIMEOUT_CNT=USB_TIMEOUT_VALUE; SetEPRxStatus(ENDP1, EP_RX_VALID); }
/******************************************************************************* * Function Name : Standard_SetEndPointFeature * Description : Set or enable a specific feature of EndPoint * Input : None. * Output : None. * Return : - Return USB_SUCCESS, if the request is performed. * - Return USB_UNSUPPORT, if the request is invalid. *******************************************************************************/ RESULT Standard_SetEndPointFeature(void) { uint32_t wIndex0 = pInformation->USBwIndex0; uint32_t rEP = wIndex0 & ~0x80; uint32_t Related_Endpoint = ENDP0 + rEP; uint32_t Status; if (ValBit(pInformation->USBwIndex0,7)) { // get Status of endpoint & stall the request if the related_ENdpoint is Disabled Status = GetEPTxStatus(Related_Endpoint); } else { Status = GetEPRxStatus(Related_Endpoint); } if (Related_Endpoint >= Device_Table.Total_Endpoint || pInformation->USBwValue != 0 || Status == 0 || pInformation->Current_Configuration == 0) { return USB_UNSUPPORT; } else { if (wIndex0 & 0x80) { // IN endpoint SetEPTxStatus(Related_Endpoint, EP_TX_STALL); } else { // OUT endpoint SetEPRxStatus(Related_Endpoint, EP_RX_STALL); } } pUser_Standard_Requests->User_SetEndPointFeature(); return USB_SUCCESS; }
vsf_err_t stm32_usbd_ep_set_OUT_dbuffer(uint8_t idx) { uint16_t epsize = stm32_usbd_ep_get_OUT_epsize(idx); int8_t index; index = stm32_usbd_ep(idx); if (index < 0) { return VSFERR_FAIL; } idx = (uint8_t)index; if ((EP_Cfg_Ptr - epsize) < STM32_USBD_EP_NUM * 8) { return VSFERR_NOT_ENOUGH_RESOURCES; } EP_Cfg_Ptr -= epsize; SetEPDoubleBuff(idx); SetEPDblBuffAddr(idx, GetEPRxAddr(idx), EP_Cfg_Ptr); SetEPDblBuffCount(idx, EP_DBUF_OUT, epsize); ClearDTOG_RX(idx); ClearDTOG_TX(idx); ToggleDTOG_TX(idx); SetEPRxStatus(idx, EP_RX_VALID); SetEPTxStatus(idx, EP_TX_DIS); stm32_usbd_OUT_dbuffer[idx] = true; return VSFERR_NONE; }
/************************************************************************************//** ** \brief USB device reset routine. ** \return none. ** ****************************************************************************************/ void Bulk_Reset(void) { /* set device as not configured */ pInformation->Current_Configuration = 0; /* current feature initialization */ pInformation->Current_Feature = Bulk_ConfigDescriptor[7]; /* set device's the default Interface*/ pInformation->Current_Interface = 0; /* set based address in USB RAM */ SetBTABLE(BTABLE_ADDRESS); /* initialize endpoint 0 CONTROL (mandatory) */ SetEPType(ENDP0, EP_CONTROL); SetEPTxStatus(ENDP0, EP_TX_STALL); SetEPRxAddr(ENDP0, ENDP0_RXADDR); SetEPTxAddr(ENDP0, ENDP0_TXADDR); Clear_Status_Out(ENDP0); SetEPRxCount(ENDP0, Device_Property.MaxPacketSize); SetEPRxValid(ENDP0); /* initialize endpoint 1 IN + OUT */ SetEPType(ENDP1, EP_BULK); SetEPTxAddr(ENDP1, ENDP1_TXADDR); SetEPRxAddr(ENDP1, ENDP1_RXADDR); SetEPTxCount(ENDP1, BULK_DATA_SIZE); SetEPRxCount(ENDP1, BULK_DATA_SIZE); SetEPRxStatus(ENDP1, EP_RX_VALID); SetEPTxStatus(ENDP1, EP_TX_NAK); /* set this device to respond on default address */ SetDeviceAddress(0); /* update device state */ bDeviceState = ATTACHED; } /*** end of Bulk_Reset ***/
/** * @brief Clear stall condition on endpoints. * @param pdev: device instance * @param epnum: endpoint address * @retval : status */ uint32_t DCD_EP_ClrStall (USB_CORE_HANDLE *pdev, uint8_t epnum) { USB_EP *ep; if ((0x80 & epnum) == 0x80) { ep = &pdev->dev.in_ep[epnum & 0x7F]; } else { ep = &pdev->dev.out_ep[epnum]; } if (ep->is_in) { ClearDTOG_TX(ep->num); SetEPTxStatus(ep->num, EP_TX_VALID); ep->is_stall = 0; } else { ClearDTOG_RX(ep->num); SetEPRxStatus(ep->num, EP_RX_VALID); ep->is_stall = 0; } return USB_OK; }
/******************************************************************************* * Function Name : SCSI_Write10_Cmd * Description : SCSI Write10 Command routine. * Input : None. * Output : None. * Return : None. *******************************************************************************/ void SCSI_Write10_Cmd(uint8_t lun , uint32_t LBA , uint32_t BlockNbr) { if (Bot_State == BOT_IDLE) { if (!(SCSI_Address_Management(CBW.bLUN, SCSI_WRITE10 , LBA, BlockNbr)))/*address out of range*/ { return; } if ((CBW.bmFlags & 0x80) == 0) { Bot_State = BOT_DATA_OUT; #ifndef STM32F10X_CL SetEPRxStatus(ENDP2, EP_RX_VALID); #endif /* STM32F10X_CL */ } else { Bot_Abort(DIR_IN); Set_Scsi_Sense_Data(CBW.bLUN, ILLEGAL_REQUEST, INVALID_FIELED_IN_COMMAND); Set_CSW (CSW_CMD_FAILED, SEND_CSW_DISABLE); } return; } else if (Bot_State == BOT_DATA_OUT) { Write_Memory(lun , LBA , BlockNbr); } }
/******************************************************************************* * Function Name : Write_Memory * Description : Handle the Write operation to the microSD card. * Input : None. * Output : None. * Return : None. *******************************************************************************/ void Write_Memory(void) { uint32_t temp = Counter + 64; i = 0; for (; Counter < temp; Counter++) { Data_Buffer[Counter] = Bulk_Data_Buff[i]; i++; } Memory_Offset += Data_Len; Transfer_Length -= Data_Len; if (!(Transfer_Length % 512)) { Counter = 0; MSD_WriteBlock(Data_Buffer, Memory_Offset - 512, 512); } CSW.dDataResidue -= Data_Len; SetEPRxStatus(ENDP2, EP_RX_VALID); /* enable the next transaction*/ if ((Transfer_Length == 0) || (Bot_State == BOT_CSW_Send)) { Counter = 0; Set_CSW (CSW_CMD_PASSED, SEND_CSW_ENABLE); } }
/******************************************************************************* * Function Name : Virtual_Com_Port_Reset * Description : Virtual_Com_Port Mouse reset routine * Input : None. * Output : None. * Return : None. *******************************************************************************/ void Virtual_Com_Port_Reset(void) { /* Set Virtual_Com_Port DEVICE as not configured */ pInformation->Current_Configuration = 0; /* Current Feature initialization */ pInformation->Current_Feature = Virtual_Com_Port_ConfigDescriptor[7]; /* Set Virtual_Com_Port DEVICE with the default Interface*/ pInformation->Current_Interface = 0; SetBTABLE(BTABLE_ADDRESS); /* Initialize Endpoint 0 */ SetEPType(ENDP0, EP_CONTROL); SetEPTxStatus(ENDP0, EP_TX_STALL); SetEPRxAddr(ENDP0, ENDP0_RXADDR); SetEPTxAddr(ENDP0, ENDP0_TXADDR); Clear_Status_Out(ENDP0); SetEPRxCount(ENDP0, Device_Property.MaxPacketSize); SetEPRxValid(ENDP0); /* Initialize Endpoint 1 */ SetEPType(ENDP1, EP_BULK); SetEPTxAddr(ENDP1, ENDP1_TXADDR); SetEPTxStatus(ENDP1, EP_TX_NAK); SetEPRxStatus(ENDP1, EP_RX_DIS); /* Initialize Endpoint 2 */ SetEPType(ENDP2, EP_INTERRUPT); SetEPTxAddr(ENDP2, ENDP2_TXADDR); SetEPRxStatus(ENDP2, EP_RX_DIS); SetEPTxStatus(ENDP2, EP_TX_NAK); /* Initialize Endpoint 3 */ SetEPType(ENDP3, EP_BULK); SetEPRxAddr(ENDP3, ENDP3_RXADDR); SetEPRxCount(ENDP3, VIRTUAL_COM_PORT_DATA_SIZE); SetEPRxStatus(ENDP3, EP_RX_VALID); SetEPTxStatus(ENDP3, EP_TX_DIS); /* Set this device to response on default address */ SetDeviceAddress(0); bDeviceState = ATTACHED; }
/******************************************************************************* * Function Name : Speaker_Reset. * Description : Speaker reset routine. * Input : None. * Output : None. * Return : None. *******************************************************************************/ void Speaker_Reset() { /* Set Speaker device as not configured state */ pInformation->Current_Configuration = 0; /* Current Feature initialization */ pInformation->Current_Feature = Speaker_ConfigDescriptor[7]; SetBTABLE(BTABLE_ADDRESS); /* Initialize Endpoint 0 */ SetEPType(ENDP0, EP_CONTROL); SetEPTxStatus(ENDP0, EP_TX_NAK); SetEPRxAddr(ENDP0, ENDP0_RXADDR); SetEPRxCount(ENDP0, Device_Property.MaxPacketSize); SetEPTxAddr(ENDP0, ENDP0_TXADDR); Clear_Status_Out(ENDP0); SetEPRxValid(ENDP0); /* Initialize Endpoint 1 */ SetEPType(ENDP1, EP_ISOCHRONOUS); SetEPDoubleBuff(ENDP4); SetEPDblBuffAddr(ENDP1, ENDP1_BUF0Addr, ENDP1_BUF1Addr); SetEPDblBuffCount(ENDP1, EP_DBUF_IN, PACKET_SIZE); ClearDTOG_RX(ENDP1); ClearDTOG_TX(ENDP1); SetEPRxStatus(ENDP1, EP_RX_DIS); SetEPTxStatus(ENDP1, EP_TX_VALID); SetEPType(ENDP2, EP_INTERRUPT); SetEPTxAddr(ENDP2, ENDP2_TXADDR); SetEPTxCount(ENDP2, 0x40); SetEPRxStatus(ENDP2, EP_RX_DIS); SetEPTxStatus(ENDP2, EP_TX_NAK); SetEPType(ENDP3, EP_INTERRUPT); SetEPTxAddr(ENDP3, ENDP3_TXADDR); SetEPTxCount(ENDP3, 0x40); SetEPRxStatus(ENDP3, EP_RX_DIS); SetEPTxStatus(ENDP3, EP_TX_NAK); SetEPRxValid(ENDP0); /* Set this device to response on default address */ SetDeviceAddress(0); bDeviceState = ATTACHED; }
/******************************************************************************* * Function Name : EP3_OUT_Callback. * Description : EP3 OUT Callback Routine. * Input : None. * Output : None. * Return : None. *******************************************************************************/ void EP3_OUT_Callback(void) { /* Read received data (bytes) */ USB_SIL_Read(EP3_OUT, Receive_Buffer_from_hid); SetEPRxStatus(ENDP3, EP_RX_VALID); }
/******************************************************************************* Function Name : USBdev_Reset. Description : USB device reset routine. Input : None. Output : None. Return : None. *******************************************************************************/ void USBdev_Reset() { // Set DEVICE as not configured pInformation->Current_Configuration = 0; // Current Feature initialization pInformation->Current_Feature = VCOM_ConfigDescriptor[7]; // Set DEVICE with the default Interface pInformation->Current_Interface = 0; // BTABLE_ADDRESS = 0 USB->BTABLE = BTABLE_ADDRESS; // Initialize Endpoint 0 SetEPType(ENDP0,EP_CONTROL); SetEPTxStatus(ENDP0,EP_TX_STALL); SetEPRxAddr(ENDP0,ENDP0_RXADDR); SetEPTxAddr(ENDP0,ENDP0_TXADDR); Clear_Status_Out(ENDP0); SetEPRxCount(ENDP0,Device_Property.MaxPacketSize); SetEPRxValid(ENDP0); // Initialize Endpoint 1 SetEPType(ENDP1,EP_BULK); SetEPTxAddr(ENDP1,ENDP1_TXADDR); SetEPTxStatus(ENDP1,EP_TX_NAK); SetEPRxStatus(ENDP1,EP_RX_DIS); // Initialize Endpoint 2 SetEPType(ENDP2,EP_INTERRUPT); SetEPTxAddr(ENDP2,ENDP2_TXADDR); SetEPRxStatus(ENDP2,EP_RX_DIS); SetEPTxStatus(ENDP2,EP_TX_NAK); // Initialize Endpoint 3 SetEPType(ENDP3,EP_BULK); SetEPRxAddr(ENDP3,ENDP3_RXADDR); SetEPRxCount(ENDP3,VCOM_INT_SIZE); SetEPRxStatus(ENDP3,EP_RX_VALID); SetEPTxStatus(ENDP3,EP_TX_DIS); // Set this device to response on default address SetDeviceAddress(0); bDeviceState = ATTACHED; }
void Device_Reset(void) { /* Set the device as not configured */ pInformation->Current_Configuration = 0; /* Current feature initialization */ pInformation->Current_Feature = ConfigDescriptor[7]; /* Set the device with the default Interface*/ pInformation->Current_Interface = 0; SetBTABLE(0x00); /* Initialize Endpoint 0 */ SetEPType(ENDP0, EP_CONTROL); SetEPTxStatus(ENDP0, EP_TX_STALL); SetEPRxAddr(ENDP0, ENDP0_RXADDR); SetEPTxAddr(ENDP0, ENDP0_TXADDR); Clear_Status_Out(ENDP0); SetEPRxCount(ENDP0, Device_Property.MaxPacketSize); SetEPRxValid(ENDP0); /* Initialize Endpoint 1 */ SetEPType(ENDP1, EP_BULK); SetEPTxAddr(ENDP1, ENDP1_TXADDR); SetEPTxStatus(ENDP1, EP_TX_NAK); SetEPRxStatus(ENDP1, EP_RX_DIS); /* Initialize Endpoint 2 */ SetEPType(ENDP2, EP_INTERRUPT); SetEPTxAddr(ENDP2, ENDP2_TXADDR); SetEPRxStatus(ENDP2, EP_RX_DIS); SetEPTxStatus(ENDP2, EP_TX_NAK); /* Initialize Endpoint 3 */ SetEPType(ENDP3, EP_BULK); SetEPRxAddr(ENDP3, ENDP3_RXADDR); SetEPRxCount(ENDP3, VCP_DATA_SIZE); SetEPRxStatus(ENDP3, EP_RX_VALID); SetEPTxStatus(ENDP3, EP_TX_DIS); /* Set this device to response on default address */ SetDeviceAddress(0); /* Device attached */ led_off(); }
/******************************************************************************* * Function Name : EP2_OUT_Callback. * Description : EP2 OUT Callback Routine. * Input : None. * Output : None. * Return : None. *******************************************************************************/ void EP2_OUT_Callback(void) { USB_R_Size = GetEPRxCount(ENDP2); PMAToUserBufferCopy((u8 *)USB_R_Buffer, ENDP2_RXADDR, USB_R_Size); SetEPRxStatus(ENDP2, EP_RX_VALID); /* enable the next transaction*/ USB_Receive_Flag = 0xff; //Mass_Storage_Out(); }
/******************************************************************************* * Function Name : Bot_Abort * Description : Stall the needed Endpoint according to the selected direction. * Input : Endpoint direction IN, OUT or both directions * Output : None. * Return : None. *******************************************************************************/ void Bot_Abort(u8 Direction) { switch (Direction) { case DIR_IN : SetEPTxStatus(ENDP1, EP_TX_STALL); break; case DIR_OUT : SetEPRxStatus(ENDP2, EP_RX_STALL); break; case BOTH_DIR : SetEPTxStatus(ENDP1, EP_TX_STALL); SetEPRxStatus(ENDP2, EP_RX_STALL); break; default: break; } }