void CCID_BulkInMessage(void) { gi32UsbdMessageLength = USB_MESSAGE_HEADER_SIZE + make32(&UsbMessageBuffer[OFFSET_DWLENGTH]); pUsbMessageBuffer = UsbMessageBuffer; if (gu8IsBulkInReady) { if(gi32UsbdMessageLength >= EP2_MAX_PKT_SIZE) { gu8IsBulkInReady = 1; USBD_MemCopy((uint8_t *)(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP2)), pUsbMessageBuffer, EP2_MAX_PKT_SIZE); USBD_SET_PAYLOAD_LEN(EP2, EP2_MAX_PKT_SIZE); pUsbMessageBuffer += EP2_MAX_PKT_SIZE; gi32UsbdMessageLength -= EP2_MAX_PKT_SIZE; } else { USBD_MemCopy((uint8_t *)(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP2)), pUsbMessageBuffer, gi32UsbdMessageLength); USBD_SET_PAYLOAD_LEN(EP2, gi32UsbdMessageLength); gi32UsbdMessageLength = 0; gu8IsBulkInReady = 0; } } }
void HID_UpdateKbData(void) { int32_t i; uint8_t *buf; uint32_t key = 0xF; static uint32_t preKey; if(g_u8EP5Ready) { buf = (uint8_t *)(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP5)); /* If GPB15 = 0, just report it is key 'a' */ key = (PB->PIN & (1 << 15)) ? 0 : 1; if(key == 0) { for(i = 0; i < 8; i++) { buf[i] = 0; } if(key != preKey) { /* Trigger to note key release */ USBD_SET_PAYLOAD_LEN(EP5, 8); } } else { preKey = key; buf[2] = 0x04; /* Key A */ USBD_SET_PAYLOAD_LEN(EP5, 8); } } }
void EP2_Handler(void) { /* BULK IN transfer */ if (gu8IsBulkInReady) { if(gi32UsbdMessageLength >= EP2_MAX_PKT_SIZE) { gu8IsBulkInReady = 1; USBD_MemCopy((uint8_t *)(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP2)), pUsbMessageBuffer, EP2_MAX_PKT_SIZE); USBD_SET_PAYLOAD_LEN(EP2, EP2_MAX_PKT_SIZE); pUsbMessageBuffer += EP2_MAX_PKT_SIZE; gi32UsbdMessageLength -= EP2_MAX_PKT_SIZE; } else { USBD_MemCopy((uint8_t *)(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP2)), pUsbMessageBuffer, gi32UsbdMessageLength); USBD_SET_PAYLOAD_LEN(EP2, gi32UsbdMessageLength); gi32UsbdMessageLength = 0; gu8IsBulkInReady = 0; } } if (!gu8IsBulkOutReady) USBD_SET_PAYLOAD_LEN(EP3, EP3_MAX_PKT_SIZE); }
void MSC_ReadTrig(void) { uint32_t u32Len; if (g_u32Length) { if (g_u32BytesInStorageBuf) { /* Prepare next data packet */ g_u8Size = EP2_MAX_PKT_SIZE; if (g_u8Size > g_u32Length) g_u8Size = g_u32Length; if (USBD_GET_EP_BUF_ADDR(EP2) == g_u32BulkBuf1) USBD_MemCopy((uint8_t *)((uint32_t)USBD_BUF_BASE + g_u32BulkBuf0), (uint8_t *)g_u32Address, g_u8Size); else USBD_MemCopy((uint8_t *)((uint32_t)USBD_BUF_BASE + g_u32BulkBuf1), (uint8_t *)g_u32Address, g_u8Size); g_u32Address += g_u8Size; } else { u32Len = g_u32Length; if (u32Len > STORAGE_BUFFER_SIZE) u32Len = STORAGE_BUFFER_SIZE; MSC_ReadMedia(g_u32LbaAddress, u32Len, (uint8_t *)STORAGE_DATA_BUF); g_u32BytesInStorageBuf = u32Len; g_u32LbaAddress += u32Len; g_u32Address = STORAGE_DATA_BUF; /* Prepare next data packet */ g_u8Size = EP2_MAX_PKT_SIZE; if (g_u8Size > g_u32Length) g_u8Size = g_u32Length; if (USBD_GET_EP_BUF_ADDR(EP2) == g_u32BulkBuf1) USBD_MemCopy((uint8_t *)((uint32_t)USBD_BUF_BASE + g_u32BulkBuf0), (uint8_t *)g_u32Address, g_u8Size); else USBD_MemCopy((uint8_t *)((uint32_t)USBD_BUF_BASE + g_u32BulkBuf1), (uint8_t *)g_u32Address, g_u8Size); g_u32Address += g_u8Size; } /* DATA0/DATA1 Toggle */ if (USBD_GET_EP_BUF_ADDR(EP2) == g_u32BulkBuf1) USBD_SET_EP_BUF_ADDR(EP2, g_u32BulkBuf0); else USBD_SET_EP_BUF_ADDR(EP2, g_u32BulkBuf1); /* Trigger to send out the data packet */ USBD_SET_PAYLOAD_LEN(EP2, g_u8Size); g_u32Length -= g_u8Size; g_u32BytesInStorageBuf -= g_u8Size; } else USBD_SET_PAYLOAD_LEN(EP2, 0); }
/** * USB CDC class request handler. * This is an internal function. */ static void USB_VirtualCOM_HandleClassRequest() { USB_SetupPacket_t setupPacket; USBD_GetSetupPacket((uint8_t *) &setupPacket); if(setupPacket.bmRequestType & 0x80) { // Transfer direction: device to host switch(setupPacket.bRequest) { case USB_VCOM_REQ_GET_LINE_CODE: if(setupPacket.wIndex == USB_VCOM_INDEX) { // Copy line coding data to USB buffer USBD_MemCopy((uint8_t *) (USBD_BUF_BASE + USB_VCOM_CTRL_IN_BUF_BASE), (uint8_t *) &USB_VirtualCOM_lineCoding, USB_VirtualCOM_LineCoding_t_SIZE); } // Data stage USBD_SET_DATA1(USB_VCOM_CTRL_IN_EP); USBD_SET_PAYLOAD_LEN(USB_VCOM_CTRL_IN_EP, USB_VirtualCOM_LineCoding_t_SIZE); // Status stage USBD_PrepareCtrlOut(NULL, 0); break; default: // Setup error, stall the device USBD_SetStall(USB_VCOM_CTRL_IN_EP); USBD_SetStall(USB_VCOM_CTRL_OUT_EP); break; } } else { // Transfer direction: host to device switch(setupPacket.bRequest) { case USB_VCOM_REQ_SET_CONTROL_LINE_STATE: // Control signals are ignored // Status stage USBD_SET_DATA1(USB_VCOM_CTRL_IN_EP); USBD_SET_PAYLOAD_LEN(USB_VCOM_CTRL_IN_EP, 0); break; case USB_VCOM_REQ_SET_LINE_CODE: if(setupPacket.wIndex == USB_VCOM_INDEX) { // Prepare for line coding copy USBD_PrepareCtrlOut((uint8_t *) &USB_VirtualCOM_lineCoding, USB_VirtualCOM_LineCoding_t_SIZE); } // Status stage USBD_SET_DATA1(USB_VCOM_CTRL_IN_EP); USBD_SET_PAYLOAD_LEN(USB_VCOM_CTRL_IN_EP, 0); break; default: // Setup error, stall the device USBD_SetStall(USB_VCOM_CTRL_IN_EP); USBD_SetStall(USB_VCOM_CTRL_OUT_EP); break; } } }
void MSC_Write(void) { uint32_t lba, len; if (g_u32Length > EP3_MAX_PKT_SIZE) { if (USBD_GET_EP_BUF_ADDR(EP3) == g_u32BulkBuf0) { USBD_SET_EP_BUF_ADDR(EP3, g_u32BulkBuf1); USBD_SET_PAYLOAD_LEN(EP3, EP3_MAX_PKT_SIZE); USBD_MemCopy((uint8_t *)g_u32Address, (uint8_t *)((uint32_t)USBD_BUF_BASE + g_u32BulkBuf0), EP3_MAX_PKT_SIZE); } else { USBD_SET_EP_BUF_ADDR(EP3, g_u32BulkBuf0); USBD_SET_PAYLOAD_LEN(EP3, EP3_MAX_PKT_SIZE); USBD_MemCopy((uint8_t *)g_u32Address, (uint8_t *)((uint32_t)USBD_BUF_BASE + g_u32BulkBuf1), EP3_MAX_PKT_SIZE); } g_u32Address += EP3_MAX_PKT_SIZE; g_u32Length -= EP3_MAX_PKT_SIZE; /* Buffer full. Writer it to storage first. */ if (g_u32Address >= (STORAGE_DATA_BUF + STORAGE_BUFFER_SIZE)) { DataFlashWrite(g_u32DataFlashStartAddr, STORAGE_BUFFER_SIZE, (uint32_t)STORAGE_DATA_BUF); g_u32Address = STORAGE_DATA_BUF; g_u32DataFlashStartAddr += STORAGE_BUFFER_SIZE; } } else { if (USBD_GET_EP_BUF_ADDR(EP3) == g_u32BulkBuf0) USBD_MemCopy((uint8_t *)g_u32Address, (uint8_t *)((uint32_t)USBD_BUF_BASE + g_u32BulkBuf0), g_u32Length); else USBD_MemCopy((uint8_t *)g_u32Address, (uint8_t *)((uint32_t)USBD_BUF_BASE + g_u32BulkBuf1), g_u32Length); g_u32Address += g_u32Length; g_u32Length = 0; if ((g_sCBW.u8OPCode == UFI_WRITE_10) || (g_sCBW.u8OPCode == UFI_WRITE_12)) { lba = get_be32(&g_sCBW.au8Data[0]); len = g_sCBW.dCBWDataTransferLength; len = lba * UDC_SECTOR_SIZE + g_sCBW.dCBWDataTransferLength - g_u32DataFlashStartAddr; if (len) DataFlashWrite(g_u32DataFlashStartAddr, len, (uint32_t)STORAGE_DATA_BUF); } g_u8BulkState = BULK_IN; MSC_AckCmd(); } }
void CCID_ClassRequest(void) { uint8_t buf[8]; USBD_GetSetupPacket(buf); if (buf[0] & 0x80) /* request data transfer direction */ { // Device to host switch (buf[1]) { case CCID_GET_CLOCK_FREQUENCIES: case CCID_GET_DATA_RATES: { uint8_t pData[1] = {0}; USBD_MemCopy((uint8_t *)(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP0)), pData, sizeof(pData)); /* Data stage */ USBD_SET_DATA1(EP0); USBD_SET_PAYLOAD_LEN(EP0, sizeof(pData)); /* Status stage */ USBD_PrepareCtrlOut(0,0); break; } default: { /* Setup error, stall the device */ USBD_SetStall(0); break; } } } else { // Host to device switch (buf[1]) { case CCID_ABORT: { /* Status stage */ USBD_SET_DATA1(EP0); USBD_SET_PAYLOAD_LEN(EP0, 0); break; } default: { // Stall /* Setup error, stall the device */ USBD_SetStall(0); break; } } } }
void PTR_ClassRequest(void) { uint8_t buf[8]; USBD_GetSetupPacket(buf); if (buf[0] & 0x80) { /* request data transfer direction */ // Device to host switch (buf[1]) { case GET_PORT_STATUS: { /* Data stage */ USBD_SET_DATA1(EP0); USBD_SET_PAYLOAD_LEN(EP0, 0); /* Status stage */ USBD_PrepareCtrlOut(0,0); break; } default: { /* Setup error, stall the device */ USBD_SetStall(0); break; } } } else { // Host to device switch (buf[1]) { case SET_REPORT: { if (buf[3] == 3) { /* Request Type = Feature */ USBD_SET_DATA1(EP1); USBD_SET_PAYLOAD_LEN(EP1, 0); } break; } case SET_IDLE: { /* Status stage */ USBD_SET_DATA1(EP0); USBD_SET_PAYLOAD_LEN(EP0, 0); break; } case SET_PROTOCOL: // { // break; // } default: { // Stall /* Setup error, stall the device */ USBD_SetStall(0); break; } } } }
void EP4_Handler(void) { /* INT IN transfer */ if (gu8IsDeviceReady) { RDR_to_PC_NotifySlotChange(); USBD_MemCopy((uint8_t *)(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP4)), pu8IntInBuf, 2); USBD_SET_PAYLOAD_LEN(EP4, 2); gu8IsDeviceReady = 0; } if (!gu8IsBulkOutReady) USBD_SET_PAYLOAD_LEN(EP3, EP3_MAX_PKT_SIZE); }
/** * @brief Repeat Control IN pipe * * @param None * * @return None * * @details This function processes the remained data of Control IN transfer. * */ void USBD_CtrlIn(void) { static uint8_t u8ZeroFlag = 0; if(g_usbd_CtrlInSize) { // Process remained data if(g_usbd_CtrlInSize > g_usbd_CtrlMaxPktSize) { // Data size > MXPLD USBD_MemCopy((uint8_t *)USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP0), (uint8_t *)g_usbd_CtrlInPointer, g_usbd_CtrlMaxPktSize); USBD_SET_PAYLOAD_LEN(EP0, g_usbd_CtrlMaxPktSize); g_usbd_CtrlInPointer += g_usbd_CtrlMaxPktSize; g_usbd_CtrlInSize -= g_usbd_CtrlMaxPktSize; } else { // Data size <= MXPLD USBD_MemCopy((uint8_t *)USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP0), (uint8_t *)g_usbd_CtrlInPointer, g_usbd_CtrlInSize); USBD_SET_PAYLOAD_LEN(EP0, g_usbd_CtrlInSize); if(g_usbd_CtrlInSize == g_usbd_CtrlMaxPktSize) u8ZeroFlag = 1; g_usbd_CtrlInPointer = 0; g_usbd_CtrlInSize = 0; } } else { // In ACK for Set address if((g_usbd_SetupPacket[0] == 0) && (g_usbd_SetupPacket[1] == 5)) { if((USBD_GET_ADDR() != g_usbd_UsbAddr) && (USBD_GET_ADDR() == 0)) { USBD_SET_ADDR(g_usbd_UsbAddr); } } // For size if n x MXPLD if(u8ZeroFlag) { USBD_SET_PAYLOAD_LEN(EP0, 0); u8ZeroFlag = 0; } // No more data for IN token USBD_PrepareCtrlOut(0, 0); DBG_PRINTF("Ctrl In Done.\n"); } }
/** * @brief Repeat Control IN pipe * * @param None * * @return None * * @details This function processes the remained data of Control IN transfer. * */ void USBD_CtrlIn(void) { static uint8_t u8ZeroFlag = 0; DBG_PRINTF("Ctrl In Ack. residue %d\n", g_usbd_CtrlInSize); if(g_usbd_CtrlInSize) { // Process remained data if(g_usbd_CtrlInSize > g_usbd_CtrlMaxPktSize) { // Data size > MXPLD USBD_MemCopy((uint8_t *)USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP0), (uint8_t *)g_usbd_CtrlInPointer, g_usbd_CtrlMaxPktSize); USBD_SET_PAYLOAD_LEN(EP0, g_usbd_CtrlMaxPktSize); g_usbd_CtrlInPointer += g_usbd_CtrlMaxPktSize; g_usbd_CtrlInSize -= g_usbd_CtrlMaxPktSize; } else { // Data size <= MXPLD USBD_MemCopy((uint8_t *)USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP0), (uint8_t *)g_usbd_CtrlInPointer, g_usbd_CtrlInSize); USBD_SET_PAYLOAD_LEN(EP0, g_usbd_CtrlInSize); if(g_usbd_CtrlInSize == g_usbd_CtrlMaxPktSize) u8ZeroFlag = 1; g_usbd_CtrlInPointer = 0; g_usbd_CtrlInSize = 0; } } else // No more data for IN token { // In ACK for Set address if((g_usbd_SetupPacket[0] == REQ_STANDARD) && (g_usbd_SetupPacket[1] == USBD_SET_ADDRESS)) { if((USBD_GET_ADDR() != g_usbd_UsbAddr) && (USBD_GET_ADDR() == 0)) { USBD_SET_ADDR(g_usbd_UsbAddr); } } /* For the case of data size is integral times maximum packet size */ if(u8ZeroFlag) { USBD_SET_PAYLOAD_LEN(EP0, 0); u8ZeroFlag = 0; } DBG_PRINTF("Ctrl In done.\n"); } }
/** * @brief USBD Endpoint Config. * @param None. * @retval None. */ void PTR_Init(void) { /* Init setup packet buffer */ /* Buffer for setup packet -> [0 ~ 0x7] */ USBD->BUFSEG = SETUP_BUF_BASE; /*****************************************************/ /* EP0 ==> control IN endpoint, address 0 */ USBD_CONFIG_EP(EP0, USBD_CFG_CSTALL | USBD_CFG_EPMODE_IN | 0); /* Buffer range for EP0 */ USBD_SET_EP_BUF_ADDR(EP0, EP0_BUF_BASE); /* EP1 ==> control OUT endpoint, address 0 */ USBD_CONFIG_EP(EP1, USBD_CFG_CSTALL | USBD_CFG_EPMODE_OUT | 0); /* Buffer range for EP1 */ USBD_SET_EP_BUF_ADDR(EP1, EP1_BUF_BASE); /*****************************************************/ /* EP2 ==> Bulk IN endpoint, address 1 */ USBD_CONFIG_EP(EP2, USBD_CFG_EPMODE_IN | BULK_IN_EP_NUM); /* Buffer offset for EP2 */ USBD_SET_EP_BUF_ADDR(EP2, EP2_BUF_BASE); /* EP3 ==> Bulk Out endpoint, address 2 */ USBD_CONFIG_EP(EP3, USBD_CFG_EPMODE_OUT | BULK_OUT_EP_NUM); /* Buffer offset for EP3 */ USBD_SET_EP_BUF_ADDR(EP3, EP3_BUF_BASE); /* trigger receive OUT data */ USBD_SET_PAYLOAD_LEN(EP3, EP3_MAX_PKT_SIZE); /* EP4 ==> Interrupt IN endpoint, address 3 */ USBD_CONFIG_EP(EP4, USBD_CFG_EPMODE_IN | INT_IN_EP_NUM); /* Buffer offset for EP4 -> */ USBD_SET_EP_BUF_ADDR(EP4, EP4_BUF_BASE); /*****************************************************/ /* EP5 ==> Interrupt IN endpoint, address 4 */ USBD_CONFIG_EP(EP5, USBD_CFG_EPMODE_IN | INT_IN_EP_NUM_1); /* Buffer range for EP5 */ USBD_SET_EP_BUF_ADDR(EP5, EP5_BUF_BASE); /* EP6 ==> Interrupt OUT endpoint, address 5 */ USBD_CONFIG_EP(EP6, USBD_CFG_EPMODE_OUT | INT_OUT_EP_NUM); /* Buffer range for EP6 */ USBD_SET_EP_BUF_ADDR(EP6, EP6_BUF_BASE); /* trigger to receive OUT data */ USBD_SET_PAYLOAD_LEN(EP6, EP6_MAX_PKT_SIZE); }
/** * @brief Prepare the first Control OUT pipe * * @param[in] pu8Buf The pointer of data received from USB host. * @param[in] u32Size The OUT transfer size. * * @return None * * @details This function is used to prepare the first Control OUT transfer. * */ void USBD_PrepareCtrlOut(uint8_t *pu8Buf, uint32_t u32Size) { g_usbd_CtrlOutPointer = pu8Buf; g_usbd_CtrlOutSize = 0; g_usbd_CtrlOutSizeLimit = u32Size; USBD_SET_PAYLOAD_LEN(EP1, g_usbd_CtrlMaxPktSize); }
/** * Copies a packet to the bulk IN buffer and sets the payload length. * USB_VirtualCOM_bulkInWaiting will be set to false. * This is an internal function. * * @param buffer Source buffer (can be NULL for zero-length packets). * @param size Packet size. Must be <= USB_VCOM_BULK_IN_MAX_PKT_SIZE. */ static void USB_VirtualCOM_SendBulkInPayload(const uint8_t *buffer, uint32_t size) { USB_VirtualCOM_bulkInWaiting = 0; if(buffer != NULL && size != 0) { USBD_MemCopy((uint8_t *) (USBD_BUF_BASE + USB_VCOM_BULK_IN_BUF_BASE), (uint8_t *) buffer, size); } USBD_SET_PAYLOAD_LEN(USB_VCOM_BULK_IN_EP, size); }
int32_t HID_CmdReadPages(CMD_T *pCmd) { uint32_t u32StartPage; uint32_t u32Pages; u32StartPage = pCmd->u32Arg1; u32Pages = pCmd->u32Arg2; printf("Read command - Start page: %d Pages Numbers: %d\n", u32StartPage, u32Pages); if(u32Pages) { /* Update data to page buffer to upload */ /* TODO: We need to update the page data if got a page read command. (0xFF is used in this sample code) */ memcpy(g_u8PageBuff, g_u8TestPages, sizeof(g_u8PageBuff)); g_u32BytesInPageBuf = PAGE_SIZE; /* The signature word is used as page counter */ pCmd->u32Signature = 1; /* Trigger HID IN */ USBD_MemCopy((uint8_t *)(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP5)), (void *)g_u8PageBuff, EP5_MAX_PKT_SIZE); USBD_SET_PAYLOAD_LEN(EP5, EP5_MAX_PKT_SIZE); g_u32BytesInPageBuf -= EP5_MAX_PKT_SIZE; } return 0; }
//----- (000025B8) -------------------------------------------------------- __myevic__ uint32_t hidGetInfoCmd( CMD_T *pCmd ) { uint32_t u32StartAddr; uint32_t u32ParamLen; u32StartAddr = pCmd->u32Arg1; u32ParamLen = pCmd->u32Arg2; myprintf( "Get Info command - Start Addr: %d Param Len: %d\n", pCmd->u32Arg1, pCmd->u32Arg2 ); if ( u32ParamLen ) { dfChecksum = Checksum( (uint8_t *)DataFlash.params, FMC_FLASH_PAGE_SIZE - 4 ); if ( u32StartAddr + u32ParamLen > FMC_FLASH_PAGE_SIZE ) { u32ParamLen = FMC_FLASH_PAGE_SIZE - u32StartAddr; } MemCpy( hidData, ((uint8_t *)&DataFlash) + u32StartAddr, u32ParamLen ); hidDataIndex = u32ParamLen; pCmd->u32Signature = u32ParamLen; USBD_MemCopy( (uint8_t *)(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP2)), hidData, EP2_MAX_PKT_SIZE ); USBD_SET_PAYLOAD_LEN( EP2, EP2_MAX_PKT_SIZE ); hidDataIndex -= EP2_MAX_PKT_SIZE; } return 0; }
//========================================================================= //------------------------------------------------------------------------- __myevic__ void hidInit() { /* Init setup packet buffer */ /* Buffer range for setup packet -> [0 ~ 0x7] */ USBD->STBUFSEG = SETUP_BUF_BASE; /*****************************************************/ /* EP0 ==> control IN endpoint, address 0 */ USBD_CONFIG_EP(EP0, USBD_CFG_CSTALL | USBD_CFG_EPMODE_IN | 0); /* Buffer range for EP0 */ USBD_SET_EP_BUF_ADDR(EP0, EP0_BUF_BASE); /* EP1 ==> control OUT endpoint, address 0 */ USBD_CONFIG_EP(EP1, USBD_CFG_CSTALL | USBD_CFG_EPMODE_OUT | 0); /* Buffer range for EP1 */ USBD_SET_EP_BUF_ADDR(EP1, EP1_BUF_BASE); /*****************************************************/ /* EP2 ==> Interrupt IN endpoint, address 1 */ USBD_CONFIG_EP(EP2, USBD_CFG_EPMODE_IN | INT_IN_EP_NUM); /* Buffer range for EP2 */ USBD_SET_EP_BUF_ADDR(EP2, EP2_BUF_BASE); /* EP3 ==> Interrupt OUT endpoint, address 2 */ USBD_CONFIG_EP(EP3, USBD_CFG_EPMODE_OUT | INT_OUT_EP_NUM); /* Buffer range for EP3 */ USBD_SET_EP_BUF_ADDR(EP3, EP3_BUF_BASE); /* trigger to receive OUT data */ USBD_SET_PAYLOAD_LEN(EP3, EP3_MAX_PKT_SIZE); }
//------------------------------------------------------------------------- __myevic__ void usbdEP3Handler() { uint8_t *ptr; /* Interrupt OUT */ ptr = (uint8_t *)( USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR( EP3 ) ); hidGetOutReport( ptr, USBD_GET_PAYLOAD_LEN( EP3 ) ); USBD_SET_PAYLOAD_LEN( EP3, EP3_MAX_PKT_SIZE ); }
void EP6_Handler(void) /* Interrupt OUT handler */ { uint8_t *ptr; /* Interrupt OUT */ ptr = (uint8_t *)(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP6)); HID_GetOutReport(ptr, USBD_GET_PAYLOAD_LEN(EP6)); USBD_SET_PAYLOAD_LEN(EP6, EP6_MAX_PKT_SIZE); }
/* Receive printer command and data from host */ void PTR_Data_Receive(void) { uint8_t *pu8Buf = (uint8_t *)(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP3)); uint32_t u32Size = USBD_GET_PAYLOAD_LEN(EP3); /* trigger next OUT data */ USBD_SET_PAYLOAD_LEN(EP3, EP3_MAX_PKT_SIZE); }
/** * @brief USBD Endpoint Config. * @param None. * @retval None. */ void CCID_Init(void) { /* Init setup packet buffer */ /* Buffer for setup packet -> [0 ~ 0x7] */ USBD->BUFSEG = SETUP_BUF_BASE; /*****************************************************/ /* EP0 ==> control IN endpoint, address 0 */ USBD_CONFIG_EP(EP0, USBD_CFG_CSTALL | USBD_CFG_EPMODE_IN | 0); /* Buffer range for EP0 */ USBD_SET_EP_BUF_ADDR(EP0, EP0_BUF_BASE); /* EP1 ==> control OUT endpoint, address 0 */ USBD_CONFIG_EP(EP1, USBD_CFG_CSTALL | USBD_CFG_EPMODE_OUT | 0); /* Buffer range for EP1 */ USBD_SET_EP_BUF_ADDR(EP1, EP1_BUF_BASE); /*****************************************************/ /* EP2 ==> Bulk IN endpoint, address 2 */ USBD_CONFIG_EP(EP2, USBD_CFG_EPMODE_IN | BULK_IN_EP_NUM); /* Buffer offset for EP2 */ USBD_SET_EP_BUF_ADDR(EP2, EP2_BUF_BASE); /* EP3 ==> Bulk Out endpoint, address 2 */ USBD_CONFIG_EP(EP3, USBD_CFG_EPMODE_OUT | BULK_OUT_EP_NUM); /* Buffer offset for EP3 */ USBD_SET_EP_BUF_ADDR(EP3, EP3_BUF_BASE); /* trigger receive OUT data */ USBD_SET_PAYLOAD_LEN(EP3, EP3_MAX_PKT_SIZE); /* EP4 ==> Interrupt IN endpoint, address 3 */ USBD_CONFIG_EP(EP4, USBD_CFG_EPMODE_IN | INT_IN_EP_NUM); /* Buffer offset for EP4 -> */ USBD_SET_EP_BUF_ADDR(EP4, EP4_BUF_BASE); /* check card state */ gu8IsDeviceReady = 1; pu8IntInBuf = &UsbIntMessageBuffer[0]; pUsbMessageBuffer = &UsbMessageBuffer[0]; RDR_to_PC_NotifySlotChange(); USBD_MemCopy((uint8_t *)(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP4)), pu8IntInBuf, 2); USBD_SET_PAYLOAD_LEN(EP4, 2); }
int USB_Read(uint8_t *buf) { int ret=0; if(gi8BulkOutReady){ memcpy((void *)buf,(const void*)gpu8RxBuf,(unsigned int)gu32RxSize); ret = gu32RxSize; gu32RxSize = 0; gi8BulkOutReady = 0; USBD_SET_PAYLOAD_LEN(EP3, EP3_MAX_PKT_SIZE); } return ret; }
//------------------------------------------------------------------------- __myevic__ void hidStartInReport( uint32_t u32ParamLen ) { hidDataIndex = u32ParamLen; hidCmd.u32Signature = u32ParamLen; USBD_MemCopy( (uint8_t *)(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP2)), hidInDataPtr, EP2_MAX_PKT_SIZE ); USBD_SET_PAYLOAD_LEN( EP2, EP2_MAX_PKT_SIZE ); hidDataIndex -= EP2_MAX_PKT_SIZE; }
/** * @brief Prepare the first Control IN pipe * * @param[in] pu8Buf The pointer of data sent to USB host. * @param[in] u32Size The IN transfer size. * * @return None * * @details Prepare data for Control IN transfer. * */ void USBD_PrepareCtrlIn(uint8_t *pu8Buf, uint32_t u32Size) { DBG_PRINTF("Prepare Ctrl In %d\n", u32Size); if(u32Size > g_usbd_CtrlMaxPktSize) { // Data size > MXPLD g_usbd_CtrlInPointer = pu8Buf + g_usbd_CtrlMaxPktSize; g_usbd_CtrlInSize = u32Size - g_usbd_CtrlMaxPktSize; USBD_SET_DATA1(EP0); USBD_MemCopy((uint8_t *)USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP0), pu8Buf, g_usbd_CtrlMaxPktSize); USBD_SET_PAYLOAD_LEN(EP0, g_usbd_CtrlMaxPktSize); } else { // Data size <= MXPLD g_usbd_CtrlInPointer = 0; g_usbd_CtrlInSize = 0; USBD_SET_DATA1(EP0); USBD_MemCopy((uint8_t *)USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP0), pu8Buf, u32Size); USBD_SET_PAYLOAD_LEN(EP0, u32Size); } }
void WEAK HID_SendReport(uint8_t id, const void* data, uint32_t len) { uint8_t *p = (uint8_t *)(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP2)); const uint8_t *d = reinterpret_cast<const uint8_t *>(data); p[0] = id; for (uint32_t i=0; i<len; i++) p[i+1] = d[i]; g_u8EP2Ready = 0; /* Set transfer length and trigger IN transfer */ USBD_SET_PAYLOAD_LEN(EP2, len+1); while(!g_u8EP2Ready); }
void HID_SetInReport(void) { uint32_t u32StartPage; uint32_t u32TotalPages; uint32_t u32PageCnt; uint8_t *ptr; uint8_t u8Cmd; u8Cmd = gCmd.u8Cmd; u32StartPage = gCmd.u32Arg1; u32TotalPages= gCmd.u32Arg2; u32PageCnt = gCmd.u32Signature; /* Check if it is in data phase of read command */ if(u8Cmd == HID_CMD_READ) { /* Process the data phase of read command */ if((u32PageCnt >= u32TotalPages) && (g_u32BytesInPageBuf == 0)) { /* The data transfer is complete. */ u8Cmd = HID_CMD_NONE; printf("Read command complete!\n"); } else { if(g_u32BytesInPageBuf == 0) { /* The previous page has sent out. Read new page to page buffer */ /* TODO: We should update new page data here. (0xFF is used in this sample code) */ printf("Reading page %d\n", u32StartPage + u32PageCnt); memcpy(g_u8PageBuff, g_u8TestPages + u32PageCnt * PAGE_SIZE, sizeof(g_u8PageBuff)); g_u32BytesInPageBuf = PAGE_SIZE; /* Update the page counter */ u32PageCnt++; } /* Prepare the data for next HID IN transfer */ ptr = (uint8_t *)(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP5)); USBD_MemCopy(ptr, (void *)&g_u8PageBuff[PAGE_SIZE - g_u32BytesInPageBuf], EP5_MAX_PKT_SIZE); USBD_SET_PAYLOAD_LEN(EP5, EP5_MAX_PKT_SIZE); g_u32BytesInPageBuf -= EP5_MAX_PKT_SIZE; } } gCmd.u8Cmd = u8Cmd; gCmd.u32Signature = u32PageCnt; }
//----- (000025B8) -------------------------------------------------------- __myevic__ uint32_t hidFMCReadCmd( CMD_T *pCmd ) { uint32_t u32StartAddr; uint32_t u32ParamLen; u32StartAddr = pCmd->u32Arg1; u32ParamLen = pCmd->u32Arg2; myprintf( "FMC Read command - Start Addr: %d Param Len: %d\n", pCmd->u32Arg1, pCmd->u32Arg2 ); if ( ! u32ParamLen % EP2_MAX_PKT_SIZE ) { return -1; } if ( u32ParamLen ) { if ( u32ParamLen > FMC_FLASH_PAGE_SIZE ) { u32ParamLen = FMC_FLASH_PAGE_SIZE; } SYS_UnlockReg(); FMC_ENABLE_ISP(); for ( uint32_t i = 0 ; i < u32ParamLen ; i += 4 ) { uint32_t data; data = FMC_Read( u32StartAddr + i ); MemCpy( &hidData[i], &data, 4 ); } FMC_DISABLE_ISP(); SYS_LockReg(); hidDataIndex = u32ParamLen; pCmd->u32Signature = u32ParamLen; USBD_MemCopy( (uint8_t *)(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP2)), hidData, EP2_MAX_PKT_SIZE ); USBD_SET_PAYLOAD_LEN( EP2, EP2_MAX_PKT_SIZE ); hidDataIndex -= EP2_MAX_PKT_SIZE; } return 0; }
/** * Handler for bulk OUT transfers. * This is an internal function. */ static void USB_VirtualCOM_HandleBulkOut() { uint16_t packetSize, writeSize; // Copy data from USB packet to RX ring buffer packetSize = USBD_GET_PAYLOAD_LEN(USB_VCOM_BULK_OUT_EP); writeSize = USB_VirtualCOM_RxBuffer_Write((uint8_t *) (USBD_BUF_BASE + USB_VCOM_BULK_OUT_BUF_BASE), packetSize); if(USB_VirtualCOM_rxCallbackPtr != NULL && writeSize != 0) { // Invoke callback USB_VirtualCOM_rxCallbackPtr(); } // Ready for next bulk OUT USBD_SET_PAYLOAD_LEN(USB_VCOM_BULK_OUT_EP, USB_VCOM_BULK_OUT_MAX_PKT_SIZE); }
void EP3_Handler(void) { /* BULK OUT */ static int offset = 0; uint32_t len; len = USBD_GET_PAYLOAD_LEN(EP3); USBD_MemCopy(&UsbMessageBuffer[offset], (uint8_t *)(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP3)), len); if((len >= 0x0A && len != 0xFF) || offset != 0) { if(offset == 0) { /* Calculate number of byte to receive to finish the message */ gi32UsbdMessageLength = USB_MESSAGE_HEADER_SIZE + make32(&UsbMessageBuffer[OFFSET_DWLENGTH]); } gi32UsbdMessageLength -= (int) len; /* Prepare next reception if whole message not received */ if(gi32UsbdMessageLength > 0) { pUsbMessageBuffer = UsbMessageBuffer + len; offset += len; } if(gi32UsbdMessageLength == 0) { gu8IsBulkOutReady = 1; offset = 0; } if(gi32UsbdMessageLength < 0) { UsbMessageBuffer[OFFSET_DWLENGTH] = 0xFF; UsbMessageBuffer[OFFSET_DWLENGTH+1] = 0xFF; UsbMessageBuffer[OFFSET_DWLENGTH+2] = 0xFF; UsbMessageBuffer[OFFSET_DWLENGTH+3] = 0xFF; gu8IsBulkOutReady = 1; } } CCID_DispatchMessage(); /* trigger next out packet */ if (gi32UsbdMessageLength > 0) USBD_SET_PAYLOAD_LEN(EP3, EP3_MAX_PKT_SIZE); }
/** * @brief Repeat Control OUT pipe * * @param None * * @return None * * @details This function processes the successive Control OUT transfer. * */ void USBD_CtrlOut(void) { uint32_t u32Size; DBG_PRINTF("Ctrl Out Ack %d\n", g_usbd_CtrlOutSize); if(g_usbd_CtrlOutSize < g_usbd_CtrlOutSizeLimit) { u32Size = USBD_GET_PAYLOAD_LEN(EP1); USBD_MemCopy((uint8_t *)g_usbd_CtrlOutPointer, (uint8_t *)USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP1), u32Size); g_usbd_CtrlOutPointer += u32Size; g_usbd_CtrlOutSize += u32Size; if(g_usbd_CtrlOutSize < g_usbd_CtrlOutSizeLimit) USBD_SET_PAYLOAD_LEN(EP1, g_usbd_CtrlMaxPktSize); } }