/******************************************************************************* * Function Name : Read_Memory * Description : Handle the Read operation from the microSD card. * Input : None. * Output : None. * Return : None. *******************************************************************************/ void Read_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]; TransferState = TXFR_ONGOING; } if (TransferState == TXFR_ONGOING ) { MAL_Read(lun, Block_offset, (uint32_t *)Bulk_Data_Buff, BULK_MAX_PACKET_SIZE); USB_SIL_Write(EP1_IN, Bulk_Data_Buff, BULK_MAX_PACKET_SIZE); Block_offset += BULK_MAX_PACKET_SIZE; Counter -= BULK_MAX_PACKET_SIZE; SetEPTxCount(ENDP1, BULK_MAX_PACKET_SIZE); SetEPTxStatus(ENDP1, EP_TX_VALID); CSW.dDataResidue -= BULK_MAX_PACKET_SIZE; } if (Counter == 0) { Block_offset = 0; Bot_State = BOT_DATA_IN_LAST; TransferState = TXFR_IDLE; } }
/******************************************************************************* * Function Name : Read_Memory * Description : Handle the Read operation from the microSD card. * Input : None. * Output : None. * Return : None. *******************************************************************************/ void Read_Memory(uint8_t lun, uint32_t Memory_Offset, uint32_t Transfer_Length) { static uint32_t Offset, Length; if (TransferState == TXFR_IDLE ) { Offset = Memory_Offset * Mass_Block_Size[lun]; Length = Transfer_Length * Mass_Block_Size[lun]; TransferState = TXFR_ONGOING; } if (TransferState == TXFR_ONGOING ) { if (!Block_Read_count) { MAL_Read(lun , Offset , Data_Buffer, Mass_Block_Size[lun]); USB_SIL_Write(EP1_IN, (uint8_t *)Data_Buffer, BULK_MAX_PACKET_SIZE); Block_Read_count = Mass_Block_Size[lun] - BULK_MAX_PACKET_SIZE; Block_offset = BULK_MAX_PACKET_SIZE; } else { USB_SIL_Write(EP1_IN, (uint8_t *)Data_Buffer + Block_offset, BULK_MAX_PACKET_SIZE); Block_Read_count -= BULK_MAX_PACKET_SIZE; Block_offset += BULK_MAX_PACKET_SIZE; } SetEPTxCount(ENDP1, BULK_MAX_PACKET_SIZE); #ifndef USE_STM3210C_EVAL SetEPTxStatus(ENDP1, EP_TX_VALID); #endif Offset += BULK_MAX_PACKET_SIZE; Length -= BULK_MAX_PACKET_SIZE; CSW.dDataResidue -= BULK_MAX_PACKET_SIZE; Led_RW_ON(); } if (Length == 0) { Block_Read_count = 0; Block_offset = 0; Offset = 0; Bot_State = BOT_DATA_IN_LAST; TransferState = TXFR_IDLE; Led_RW_OFF(); } }
/******************************************************************************* * Function Name : Read_Memory * Description : Handle the Read operation from the microSD card. * Input : None. * Output : None. * Return : None. *******************************************************************************/ void Read_Memory(u8 lun, u32 Memory_Offset, u32 Transfer_Length) { static u32 Offset, Length; if (TransferState == TXFR_IDLE ) { Block_Read_count = 0; //20120830 joeÔö¼ÓµÄ Offset = Memory_Offset * Mass_Block_Size[lun]; Length = Transfer_Length * Mass_Block_Size[lun]; TransferState = TXFR_ONGOING; } if (TransferState == TXFR_ONGOING ) { if (!Block_Read_count) { MAL_Read(lun , Offset , Data_Buffer, Mass_Block_Size[lun]); USB_SIL_Write(EP1_IN, (u8 *)Data_Buffer, BULK_MAX_PACKET_SIZE); Block_Read_count = Mass_Block_Size[lun] - BULK_MAX_PACKET_SIZE; Block_offset = BULK_MAX_PACKET_SIZE; } else { USB_SIL_Write(EP1_IN, (u8 *)Data_Buffer + Block_offset, BULK_MAX_PACKET_SIZE); Block_Read_count -= BULK_MAX_PACKET_SIZE; Block_offset += BULK_MAX_PACKET_SIZE; } SetEPTxCount(ENDP1, BULK_MAX_PACKET_SIZE); SetEPTxStatus(ENDP1, EP_TX_VALID); Offset += BULK_MAX_PACKET_SIZE; Length -= BULK_MAX_PACKET_SIZE; CSW.dDataResidue -= BULK_MAX_PACKET_SIZE; //Led_RW_ON(); } if (Length == 0) { Block_Read_count = 0; Block_offset = 0; Offset = 0; Bot_State = BOT_DATA_IN_LAST; TransferState = TXFR_IDLE; //Led_RW_OFF(); } }
/******************************************************************************* * Function Name : Read_Memory * Description : Handle the Read operation from the microSD card. * Input : None. * Output : None. * Return : None. *******************************************************************************/ void Read_Memory(uint8_t lun, uint32_t Memory_Offset, uint32_t Transfer_Length) { static uint64_t Offset, Length;//要支持大于4G的SD卡,必须使用long long(64位)数据类型 u8 STA;//新增一个定义 if (TransferState == TXFR_IDLE ) { Offset = (long long)Memory_Offset * Mass_Block_Size[lun]; Length = (long long)Transfer_Length * Mass_Block_Size[lun]; TransferState = TXFR_ONGOING; } if (TransferState == TXFR_ONGOING ) { if (!Block_Read_count) { STA=MAL_Read(lun ,Offset,Data_Buffer,Mass_Block_Size[lun]); if(STA)USB_STATUS_REG|=0X08;//SD卡读错误! USB_SIL_Write(EP1_IN, (uint8_t *)Data_Buffer, BULK_MAX_PACKET_SIZE); Block_Read_count = Mass_Block_Size[lun] - BULK_MAX_PACKET_SIZE; Block_offset = BULK_MAX_PACKET_SIZE; }else { USB_SIL_Write(EP1_IN, (uint8_t *)Data_Buffer + Block_offset, BULK_MAX_PACKET_SIZE); Block_Read_count -= BULK_MAX_PACKET_SIZE; Block_offset += BULK_MAX_PACKET_SIZE; } SetEPTxCount(ENDP1, BULK_MAX_PACKET_SIZE); SetEPTxStatus(ENDP1, EP_TX_VALID); Offset += BULK_MAX_PACKET_SIZE; Length -= BULK_MAX_PACKET_SIZE; CSW.dDataResidue -= BULK_MAX_PACKET_SIZE; Led_RW_ON(); } if (Length == 0) { Block_Read_count = 0; Block_offset = 0; Offset = 0; Bot_State = BOT_DATA_IN_LAST; TransferState = TXFR_IDLE; Led_RW_OFF(); } }
/******************************************************************************* * Function Name : Read_Memory * Description : Handle the Read operation from the microSD card. * Input : None. * Output : None. * Return : None. *******************************************************************************/ void Read_Memory(u8 lun, u32 Memory_Offset, u32 Transfer_Length) { static u32 Offset, Length; u8 STA; if (TransferState == TXFR_IDLE ) { Offset = Memory_Offset * Mass_Block_Size[lun]; Length = Transfer_Length * Mass_Block_Size[lun]; TransferState = TXFR_ONGOING; } if (TransferState == TXFR_ONGOING ) { if (!Block_Read_count) { STA=MAL_Read(lun , Offset , Data_Buffer, Mass_Block_Size[lun]); if(STA)USB_STATUS_REG|=0X08;//SD卡读错误! UserToPMABufferCopy((u8 *)Data_Buffer, ENDP3_TXADDR, BULK_MAX_PACKET_SIZE); Block_Read_count = Mass_Block_Size[lun] - BULK_MAX_PACKET_SIZE; Block_offset = BULK_MAX_PACKET_SIZE; } else { UserToPMABufferCopy((u8 *)Data_Buffer + Block_offset, ENDP3_TXADDR, BULK_MAX_PACKET_SIZE); Block_Read_count -= BULK_MAX_PACKET_SIZE; Block_offset += BULK_MAX_PACKET_SIZE; } SetEPTxCount(ENDP3, BULK_MAX_PACKET_SIZE); SetEPTxStatus(ENDP3, EP_TX_VALID); Offset += BULK_MAX_PACKET_SIZE; Length -= BULK_MAX_PACKET_SIZE; CSW.dDataResidue -= BULK_MAX_PACKET_SIZE; //Led_RW_ON();//提示正在读写 } if (Length == 0) { Block_Read_count = 0; Block_offset = 0; Offset = 0; Bot_State = BOT_DATA_IN_LAST; TransferState = TXFR_IDLE; //Led_RW_OFF();//读写完成了 } }
/** * @brief DFU_Req_UPLOAD * Handles the DFU UPLOAD request. * @param pdev: instance * @param req: pointer to the request structure * @retval status */ static void DFU_Req_UPLOAD(void *pdev, USB_SETUP_REQ *req) { const uint8_t *Phy_Addr = NULL; uint32_t Addr = 0; /* Data setup request */ if (req->wLength > 0) { if ((DeviceState == STATE_dfuIDLE) || (DeviceState == STATE_dfuUPLOAD_IDLE)) { /* Update the global langth and block number */ wBlockNum = req->wValue; wlength = req->wLength; /* DFU Get Command */ if (wBlockNum == 0) { /* Update the state machine */ DeviceState = (wlength > 3)? STATE_dfuIDLE:STATE_dfuUPLOAD_IDLE; DeviceStatus[4] = DeviceState; DeviceStatus[1] = 0; DeviceStatus[2] = 0; DeviceStatus[3] = 0; /* Store the values of all supported commands */ MAL_Buffer[0] = CMD_GETCOMMANDS; MAL_Buffer[1] = CMD_SETADDRESSPOINTER; MAL_Buffer[2] = CMD_ERASE; /* Send the status data over EP0 */ USBD_CtlSendData (pdev, (uint8_t *)(&(MAL_Buffer[0])), 3); } else if (wBlockNum > 1) { DeviceState = STATE_dfuUPLOAD_IDLE ; DeviceStatus[4] = DeviceState; DeviceStatus[1] = 0; DeviceStatus[2] = 0; DeviceStatus[3] = 0; Addr = ((wBlockNum - 2) * XFERSIZE) + Pointer; /* Change is Accelerated*/ /* Return the physical address where data are stored */ Phy_Addr = MAL_Read(Addr, wlength); /* Send the status data over EP0 */ USBD_CtlSendData (pdev, Phy_Addr, wlength); } else /* unsupported wBlockNum */ { DeviceState = STATUS_ERRSTALLEDPKT; DeviceStatus[4] = DeviceState; DeviceStatus[1] = 0; DeviceStatus[2] = 0; DeviceStatus[3] = 0; /* Call the error management function (command will be nacked */ USBD_CtlError (pdev, req); } } /* Unsupported state */ else { wlength = 0; wBlockNum = 0; /* Call the error management function (command will be nacked */ USBD_CtlError (pdev, req); } } /* No Data setup request */ else { DeviceState = STATE_dfuIDLE; DeviceStatus[1] = 0; DeviceStatus[2] = 0; DeviceStatus[3] = 0; DeviceStatus[4] = DeviceState; } }
/******************************************************************************* * Function Name : UPLOAD * Description : Upload routine. * Input : Length. * Output : None. * Return : Pointer to data. *******************************************************************************/ uint8_t *UPLOAD(uint16_t Length) { DEVICE_INFO *pInfo = &Device_Info; uint8_t B1, B0; uint16_t offset, returned; uint8_t *Phy_Addr = NULL; uint32_t Addr = 0; B0 = pInfo->USBwValues.bw.bb0; B1 = pInfo->USBwValues.bw.bb1; wBlockNum = (uint16_t)B1; wBlockNum = wBlockNum * 0x100; wBlockNum += (uint16_t)B0; /* wBlockNum value updated*/ B0 = pInfo->USBwLengths.bw.bb0; B1 = pInfo->USBwLengths.bw.bb1; wlength = (uint16_t)B0; wlength = wlength * 0x100; wlength += (uint16_t)B1; /* wlength value updated*/ offset = pInformation->Ctrl_Info.Usb_wOffset; if (wBlockNum == 0) /* Get Command */ { if (wlength > 3) { DeviceState = STATE_dfuIDLE ; } else { DeviceState = STATE_dfuUPLOAD_IDLE; } DeviceStatus[4] = DeviceState; DeviceStatus[1] = 0; DeviceStatus[2] = 0; DeviceStatus[3] = 0; MAL_Buffer[0] = CMD_GETCOMMANDS; MAL_Buffer[1] = CMD_SETADDRESSPOINTER; MAL_Buffer[2] = CMD_ERASE; if (Length == 0) { pInformation->Ctrl_Info.Usb_wLength = 3 ; return NULL; } return(&MAL_Buffer[0]); } else if (wBlockNum > 1) { DeviceState = STATE_dfuUPLOAD_IDLE ; DeviceStatus[4] = DeviceState; DeviceStatus[1] = 0; DeviceStatus[2] = 0; DeviceStatus[3] = 0; Addr = ((wBlockNum - 2) * wTransferSize) + Pointer; /* Change is Accelerated*/ Phy_Addr = MAL_Read(Addr, wlength); returned = wlength - offset; if (Length == 0) { pInformation->Ctrl_Info.Usb_wLength = returned ; return NULL; } return(Phy_Addr + offset); } else /* unsupported wBlockNum */ { DeviceState = STATUS_ERRSTALLEDPKT; DeviceStatus[4] = DeviceState; DeviceStatus[1] = 0; DeviceStatus[2] = 0; DeviceStatus[3] = 0; return NULL; } }