stsw-stm32121库中:
把数据copy到对应端点的发送缓冲区后,使能发送状态编码STAT_TX=VALID,这时候usb的该端点就可以发送数据了
----->即先调用usb_sil.c中的USB_SIL_Write(),然后调用SetEPRxValid(uint8_t bEpNum)函数。
或者STM32Cube中库文件stm32f1xx_ii_usb.c:中函数USB_WritePMA(),然后调用PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_VALID);函数
/** * @brief Copy a buffer from user memory area to packet memory area (PMA) * @param USBx : pointer to USB register. * @param pbUsrBuf : pointer to user memory area. * @param wPMABufAddr : address into PMA. * @param wNBytes : number of bytes to be copied. * @retval None */ void USB_WritePMA(USB_TypeDef *USBx, uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes) { uint32_t nbytes = (wNBytes + 1) >> 1; /* nbytes = (wNBytes + 1) / 2 */ uint32_t index = 0, temp1 = 0, temp2 = 0; uint16_t *pdwVal = NULL; pdwVal = (uint16_t *)(wPMABufAddr * 2 + (uint32_t)USBx + 0x400); for (index = nbytes; index != 0; index--) { temp1 = (uint16_t) * pbUsrBuf; pbUsrBuf++; temp2 = temp1 | (uint16_t) * pbUsrBuf << 8; *pdwVal++ = temp2; pdwVal++; pbUsrBuf++; } }
/** * @brief Copy a buffer from packet memory area (PMA) to user memory area * @param USBx : pointer to USB register. * @param pbUsrBuf : pointer to user memory area. * @param wPMABufAddr : address into PMA. * @param wNBytes : number of bytes to be copied. * @retval None */ void USB_ReadPMA(USB_TypeDef *USBx, uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes) { uint32_t nbytes = (wNBytes + 1) >> 1;/* /2*/ uint32_t index = 0; uint32_t *pdwVal = NULL; pdwVal = (uint32_t *)(wPMABufAddr * 2 + (uint32_t)USBx + 0x400); for (index = nbytes; index != 0; index--) { *(uint16_t*)pbUsrBuf++ = *pdwVal++; pbUsrBuf++; } }
stm32cube中定义的端点属性结构体
typedef struct { uint8_t num; /*!< Endpoint number This parameter must be a number between Min_Data = 1 and Max_Data = 15 */ uint8_t is_in; /*!< Endpoint direction This parameter must be a number between Min_Data = 0 and Max_Data = 1 */ uint8_t is_stall; /*!< Endpoint stall condition This parameter must be a number between Min_Data = 0 and Max_Data = 1 */ uint8_t type; /*!< Endpoint type This parameter can be any value of @ref USB_EP_Type */ uint16_t pmaadress; /*!< PMA Address This parameter can be any value between Min_addr = 0 and Max_addr = 1K */ uint16_t pmaaddr0; /*!< PMA Address0 This parameter can be any value between Min_addr = 0 and Max_addr = 1K */ uint16_t pmaaddr1; /*!< PMA Address1 This parameter can be any value between Min_addr = 0 and Max_addr = 1K */ uint8_t doublebuffer; /*!< Double buffer enable This parameter can be 0 or 1 */ uint16_t tx_fifo_num; /*!< This parameter is not required by USB Device FS peripheral, it is used only by USB OTG FS peripheral This parameter is added to ensure compatibility across USB peripherals */ uint32_t maxpacket; /*!< Endpoint Max packet size This parameter must be a number between Min_Data = 0 and Max_Data = 64KB */ uint8_t *xfer_buff; /*!< Pointer to transfer buffer */ uint32_t xfer_len; /*!< Current transfer length */ uint32_t xfer_count; /*!< Partial transfer length in case of multi packet transfer */ } USB_EPTypeDef;
F103中usb的最大数据分组为1023个字节(每个端点的PMA最大是1023)