当前位置:网站首页>Stm32cubemx learning (I) USB HID bidirectional communication

Stm32cubemx learning (I) USB HID bidirectional communication

2022-06-12 07:04:00 iqiaoqiao

brief introduction

Using punctual atoms F407 Explorer development board , The test is based on USB HID Two way data communication .

CubeMX New project ( A serial port +LED)

Set the clock source
 Insert picture description here
 Insert picture description here

Set debugger
 Insert picture description here
Set up LED
 Insert picture description here
Set the serial port
 Insert picture description here
Set up USB OTG
 Insert picture description here
 Insert picture description here
Pinout preview
 Insert picture description here
engineering management
 Insert picture description here
In the picture above , You can put Heap Size and Stack Size Bigger , bring USB More smooth data communication .

 Insert picture description here
The selection in the red box is mainly for the project to be packaged and directly used by others .

Finally, click GENERATE CODE Build project .

Test serial port and LED

stay usart.h The header file is introduced in stdio.h

/* USER CODE BEGIN Includes */
#include "stdio.h" // Import this file , For redirection printf
/* USER CODE END Includes */

stay usart.c Add redirection code in

/* USER CODE BEGIN 1 */
//  Redirect printf function 
int fputc(int ch,FILE *f)
{
    
    uint8_t temp[1]={
    ch};
    HAL_UART_Transmit(&huart1,temp,1,2);
		return 0;
}
/* USER CODE END 1 */

For the convenience of debugging , stay main.c Add the following macro definitions to

/* USER CODE BEGIN PD */
#define USER_MAIN_DEBUG

#ifdef USER_MAIN_DEBUG
#define user_main_printf(format, ...) printf( format "\r\n",##__VA_ARGS__)
#define user_main_info(format, ...) printf("main.info:" format "\r\n",##__VA_ARGS__)
#define user_main_debug(format, ...) printf("main.debug:" format "\r\n",##__VA_ARGS__)
#define user_main_error(format, ...) printf("main.error:" format "\r\n",##__VA_ARGS__)
#else
#define user_main_printf(format, ...)
#define user_main_info(format, ...)
#define user_main_debug(format, ...)
#define user_main_error(format, ...)
#endif
/* USER CODE END PD */

stay main.c Add code to

  /* USER CODE BEGIN WHILE */
  while (1)
  {
    
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
      printf("Enter main while loop!\r\n");
      user_main_debug("Main debug!\n");
      HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin,GPIO_PIN_RESET);
      HAL_Delay(500);
      HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin,GPIO_PIN_SET);
      HAL_Delay(500);
  }
  /* USER CODE END 3 */

Set to run automatically after downloading , Compile operation , Connect the serial port debugging assistant , Observe the phenomenon
 Insert picture description here
After compiling and downloading , Restart discovery LED1( A green light ) Flashing , The serial port receives character data .

If The program does not respond or the program is stuck after simulation BEAB BKPT 0xAB, resolvent :MDK Select Settings ,TARGET , Check Use MicroLIB
 Insert picture description here

Set up USB HID

utilize HID descriptor Tool Software generates send receive 64byte Message descriptor data , Add to usbd_custom_hid_if.c Of documents CUSTOM_HID_ReportDesc_FS Function


/** Usb HID report descriptor. */
__ALIGN_BEGIN static uint8_t CUSTOM_HID_ReportDesc_FS[USBD_CUSTOM_HID_REPORT_DESC_SIZE] __ALIGN_END =
{
    
  /* USER CODE BEGIN 0 */    
    0x05, 0x8c, // USAGE_PAGE (ST Page) /
    0x09, 0x01, // USAGE (Demo Kit) /
    0xa1, 0x01, // COLLECTION (Application) /
    /* 6 */
    // The Input report
    0x09,0x03, // USAGE ID - Vendor defined
    0x15,0x00, // LOGICAL_MINIMUM (0)
    0x26,0x00, 0xFF, // LOGICAL_MAXIMUM (255)
    0x75,0x08, // REPORT_SIZE (8)
    0x95,CUSTOM_HID_EPIN_SIZE, //0x95,0x16, REPORT_COUNT (20)
    0x81,0x02, // INPUT (Data,Var,Abs)
    //19
    // The Output report
    0x09,0x04, // USAGE ID - Vendor defined
    0x15,0x00, // LOGICAL_MINIMUM (0)
    0x26,0x00,0xFF, // LOGICAL_MAXIMUM (255)
    0x75,0x08, // REPORT_SIZE (8)
    0x95,CUSTOM_HID_EPOUT_SIZE, //0x95,0x16, REPORT_COUNT (20)
    0x91,0x02, // OUTPUT (Data,Var,Abs)
    //32
  /* USER CODE END 0 */
  0xC0    /* END_COLLECTION */
};

stay usbd_desc.c Revision in China USB HID Parameters

** @defgroup USBD_DESC_Private_Defines USBD_DESC_Private_Defines
  * @brief Private defines.
  * @{
    
  */

#define USBD_VID 1155
#define USBD_LANGID_STRING 1033
#define USBD_MANUFACTURER_STRING "STMicroelectronics"
#define USBD_PID_FS 22352
#define USBD_PRODUCT_STRING_FS "STM32 Custom Human interface"
#define USBD_CONFIGURATION_STRING_FS "Custom HID Config"
#define USBD_INTERFACE_STRING_FS "Custom HID Interface"


#define USB_SIZ_BOS_DESC 0x0C

stay usbd_conf.h Modify the sending data length and message length
 Insert picture description here

stay usbd_customhid.h: Modify the length and time interval of receiving and sending data
 Insert picture description here
among ,CUSTOM_HID_HS_BINTERVAL and CUSTOM_HID_FS_BINTERVAL The value of can also be in CubeMX Set in .CUSTOM_HID_FS_BINTERVAL The definition of is in the file usbd_conf.h in , See previous figure .

test USB HID signal communication

stay main.c in , Add the following code :

/* USER CODE BEGIN Includes */
#include "usbd_custom_hid_if.h"
#include "usbd_customhid.h" // Including sending function header file 
extern USBD_HandleTypeDef hUsbDeviceFS; // External statement USB The handle of 
/* USER CODE END Includes */
...
/* USER CODE BEGIN PV */
unsigned char USB_Recive_Buffer[64] = {
    1,2,3,4,5,6,7,8,9,}; //USB Receive cache 
unsigned char USB_Received_Count = 0;//USB Received data count 
/* USER CODE END PV */
...
  while (1)
  {
    
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
      printf("Enter main while loop!\r\n");
      USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS, USB_Recive_Buffer, 64);
      HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin,GPIO_PIN_RESET);
      HAL_Delay(500);
      HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin,GPIO_PIN_SET);
      HAL_Delay(500);
  }
  /* USER CODE END 3 */

stay usbd_custom_hid_it.c Add the following code to :

/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
extern unsigned char USB_Recive_Buffer[64];
extern unsigned char USB_Received_Count;
/* USER CODE END PV */

...

/** * @brief Manage the CUSTOM HID class events * @param event_idx: Event index * @param state: Event state * @retval USBD_OK if all operations are OK else USBD_FAIL */
static int8_t CUSTOM_HID_OutEvent_FS(uint8_t event_idx, uint8_t state)
{
    
  /* USER CODE BEGIN 6 */
// UNUSED(event_idx);
// UNUSED(state);
    
    int i;
    /* Check the received data length */
    USB_Received_Count = USBD_GetRxCount( &hUsbDeviceFS,CUSTOM_HID_EPOUT_ADDR );  // The first parameter is USB Handle , The second parameter is the receiving end address ; To get the length of the sent data, change the second parameter to the sending end address 
    //printf("USB_Received_Count = %d \r\n",USB_Received_Count);
    
    USBD_CUSTOM_HID_HandleTypeDef   *hhid; // Define a point USBD_CUSTOM_HID_HandleTypeDef Pointer to structure 
    hhid = (USBD_CUSTOM_HID_HandleTypeDef*)hUsbDeviceFS.pClassData;// obtain USB Storage address for receiving data 
    
    for(i=0;i<USB_Received_Count;i++) 
    {
    
        USB_Recive_Buffer[i]=hhid->Report_buf[i];  // Send the received data to the user-defined cache for saving (Report_buf[i] by USB Receive buffer for )
    } 
    
  /* Start next USB packet transfer once data processing is completed */
  USBD_CUSTOM_HID_ReceivePacket(&hUsbDeviceFS);

  return (USBD_OK);
  /* USER CODE END 6 */
}

Download after compiling , The test results are as follows :
 Insert picture description here
Click on Endpoint 2/HID send out Button , give the result as follows :
 Insert picture description here
Bus Hound The capture is as follows :
 Insert picture description here

Conclusion

According to the relevant information , Use HID The mode of transmission is interrupt transmission , Interrupt interval is determined by CUSTOM_HID_FS_BINTERVAL Set up , The minimum value is 1ms, That is to say, the maximum transmission speed is 64KByte/Sec. use Bus Hound The results of software testing are similar , Here's the picture :
 Insert picture description here
Project files can be downloaded from my resources .

notes :Cmd.Phase.Ofs The first of the three data represents the command (Cmd), from 1 Start Statistics , Every time the device receives a new command, it adds 1.
The second means Phase This time (Cmd) Position in , Every time there is a data or status, add 1.
The third one indicates that the data is in this one Phase In the middle .
There is a bracket at the end , It indicates the number of times the duplicate data has been folded , stay Settings You can set the duplicate data consolidation display .

In this experiment , The rate of testing is about 1ms A bag !

原网站

版权声明
本文为[iqiaoqiao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010600142848.html