我在学STM32的USB。用的自定义设备。
部分程序如下:
void CustomHID_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 = CustomHID_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);
SetEPRxAddr(ENDP1, ENDP1_RXADDR);
SetEPTxCount(ENDP1, 2);
SetEPRxCount(ENDP1, 2);
SetEPRxStatus(ENDP1, EP_RX_VALID);
SetEPTxStatus(ENDP1, EP_TX_NAK);
bDeviceState = ATTACHED;
/* Set this device to response on default address */
SetDeviceAddress(0);
}
。。。。。。。。。。。。。
/******************** Descriptor of Custom HID endpoints ******************/
/* 27 */
0x07, /* bLength: Endpoint Descriptor size */
USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType: */
0x81, /* bEndpointAddress: Endpoint Address (IN) */
0x03, /* bmAttributes: Interrupt endpoint */
0x02, /* wMaxPacketSize: 2 Bytes max */
0x00,
0x01, /* bInterval: Polling Interval (32 ms) */
/* 34 */
0x07, /* bLength: Endpoint Descriptor size */
USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType: */
/* Endpoint descriptor type */
0x01, /* bEndpointAddress: */
/* Endpoint Address (OUT) */
0x03, /* bmAttributes: Interrupt endpoint */
0x02, /* wMaxPacketSize: 2 Bytes max */
0x00,
0x01, /* bInterval: Polling Interval (20 ms) */
/* 41 */
}
。。。。。。。。。。。。。。。。。。
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
u8 Receive_Buffer[2];
u8 temp;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name : EP1_OUT_Callback.
* Description : EP1 OUT Callback Routine.
* Input : None.
* Output : None.
* Return : None.
*******************************************************************************/
void EP1_OUT_Callback(void)
{
BitAction Led_State;
PMAToUserBufferCopy(Receive_Buffer, ENDP1_RXADDR, 2);
temp=Receive_Buffer[1];
SetEPRxStatus(ENDP1, EP_RX_VALID);
if(temp!=0x55)
GPIO_SetBits(GPIOC, GPIO_Pin_12 );
else
GPIO_ResetBits(GPIOC, GPIO_Pin_12 );
}
。。。。。。。。。。。。。。。。。。。
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
#define nReportCnt 2
/* Private variables ---------------------------------------------------------*/
unsigned long int TimeCount=0;
u8 Transi_Buffer[nReportCnt];
u8 USB_Receive标志寄存器;
u8 flag=0;
u8 i=0;
/* Extern variables ----------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
void Delay(vu32 nCount);
/* Private functions ---------------------------------------------------------*/
void USB_SendString(void)
{
Transi_Buffer[0] = 0x07;
for(i=1;i<nReportCnt;i++)
//i++;
Transi_Buffer=i;
UserToPMABufferCopy(Transi_Buffer, ENDP1_TXADDR, nReportCnt);
SetEPTxCount(ENDP1, nReportCnt);
SetEPTxValid(ENDP1);
}
。。。。。。。。。。。。。。
USB_Init();
//USB_SendString();
while (1)
{
if ( TimeCount >= 3000000 ) //收到后上位机的数据后,将1S发送一次数据给PC
{
USB_SendString();
TimeCount = 0x00;
}
TimeCount++;
}
用bus_hound监视截图如下:
|