最近找到一个stm32f411开发板,买了很久之前测试完就没使用了现在来做个HID键盘用用
下面这个是当时购买淘宝店铺下面的gitee链接有兴趣的可以看看
WeActStudio.MiniSTM32F4x1: WeAct Studio STM32F401CEU6/STM32F411CEU6 核心板资料 Github 镜像仓库 (gitee.com)
关于stm32f411ceu6是有内置上拉电阻的,外部电路可以直连USB,stm32f072c8t6也是一样
但是stm32f103c8t6一定要外部上拉,可能是只有新款的才有?不管了,stm32f411先用起来
一、打开stm32cubemx选择stm32f411ceu6,配置RCC和SYS
二、打开USB_OTG_FS选择Device_Only
三、把USB_DEVICE配置成HID设备,填入一些参数我在图片下面说明这些参数是啥意思,在Device Descriptor上面的VID和PID我们就不去修改啦
0x05:表示我们设置的HID设备通信时间间隔单位为ms
63:这个是我们配置的设备描述符,要配合另一个生产设备描述符的软件一起讲这个放到下面去
64:这个就是发送的缓冲区大小,表示一次可以发送最多发送64个字节
四、我们来配置时钟,一般图省事我直接配置成48M,要是你有其他的时钟大小也是可以试试的
五、给工程取个名字选择一个保存的路径,选择MDKV5,在Code Generator单独生产.c和.h的文件上面打勾就行
上面这些步骤完成点击generate code就生成了我们的MDK工程了,下一步我们找到设备描述符用的软件进入第二部分!!!
六、这里我使用的是软件自带的设备描述符(软件我之后会放在网盘中)
在截图里简单描述一下大概的意思,附上一个讲解的很详细的网站!应该是目前我看的讲解的最详细的了
USB HID报告描述符教程 - 知乎 (zhihu.com)
(可能有不对的地方,具体的按照上面的知乎链接的内容)
上面的内容是软件自带的文件的keyboard文件,点击file-open找到和软件相同目录的地方就可以看到
七、选择文件的另存为把这个HID文件转成txt格式方便我们复制到程序中
0x05, 0x01, // USAGE_PAGE (Generic Desktop) //63 0x09, 0x06, // USAGE (Keyboard) 0xa1, 0x01, // COLLECTION (Application) 0x05, 0x07, // USAGE_PAGE (Keyboard) 0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl) 0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI) 0x15, 0x00, // LOGICAL_MINIMUM (0) 0x25, 0x01, // LOGICAL_MAXIMUM (1) 0x75, 0x01, // REPORT_SIZE (1) 0x95, 0x08, // REPORT_COUNT (8) 0x81, 0x02, // INPUT (Data,Var,Abs) 0x95, 0x01, // REPORT_COUNT (1) 0x75, 0x08, // REPORT_SIZE (8) 0x81, 0x03, // INPUT (Cnst,Var,Abs) 0x95, 0x05, // REPORT_COUNT (5) 0x75, 0x01, // REPORT_SIZE (1) 0x05, 0x08, // USAGE_PAGE (LEDs) 0x19, 0x01, // USAGE_MINIMUM (Num Lock) 0x29, 0x05, // USAGE_MAXIMUM (Kana) 0x91, 0x02, // OUTPUT (Data,Var,Abs) 0x95, 0x01, // REPORT_COUNT (1) 0x75, 0x03, // REPORT_SIZE (3) 0x91, 0x03, // OUTPUT (Cnst,Var,Abs) 0x95, 0x06, // REPORT_COUNT (6) 0x75, 0x08, // REPORT_SIZE (8) 0x15, 0x00, // LOGICAL_MINIMUM (0) 0x25, 0x65, // LOGICAL_MAXIMUM (101) 0x05, 0x07, // USAGE_PAGE (Keyboard) 0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated)) 0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application) 0x81, 0x00, // INPUT (Data,Ary,Abs) 0xC0 /* END_COLLECTION */
八、打开生成的mdk工程,找到usbd_custom_hid_if.c把设备描述符的复制到
CUSTOM_HID_ReportDesc_FS[USBD_CUSTOM_HID_REPORT_DESC_SIZE]
注意保护区外已经有了一个0xC0所以我们生产的设备描述符可以去掉最后的C0
九、我们把头文件导入到main.c中,写一个数组来测试一下HID键盘
/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * * Copyright (c) 2023 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "usb_device.h" #include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ //#include "usbd_customhid.h" #include "usbd_custom_hid_if.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ uint8_t buffer[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};//keyboard extern USBD_HandleTypeDef hUsbDeviceFS;//外部// /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_USB_DEVICE_Init(); /* USER CODE BEGIN 2 */ /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0) == GPIO_PIN_RESET) { buffer[2] = 0x04; //a USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS,buffer,8); HAL_Delay(15); buffer[2] = 0x00; USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS,buffer,8); HAL_Delay(15); } /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Configure the main internal regulator output voltage */ __HAL_RCC_PWR_CLK_ENABLE(); __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLM = 25; RCC_OscInitStruct.PLL.PLLN = 192; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; RCC_OscInitStruct.PLL.PLLQ = 4; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB buses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV2; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) { Error_Handler(); } } /* USER CODE BEGIN 4 */ /* USER CODE END 4 */ /** * @brief This function is executed in case of error occurrence. * @retval None */ void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ /* User can add his own implementation to report the HAL error return state */ __disable_irq(); while (1) { } /* USER CODE END Error_Handler_Debug */ } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */
效果就是按下PA0的按键,USB会输出一个键盘的a
(突然发现配置的设备描述符是发送9个字节为一个完整数据,再mian中只发送了8个字节的数组居然也能正常使用,可能是最后一个字节刚好是0没有用上?这里没有做修改)
给大家附上一个我觉得描述的很详细每个字节8个位具体功能的链接可以参考一下,但是根据不同的设备描述符配置有调整
自己整理的USB-HID鼠标、键盘通讯格式,欢迎指教 (amobbs.com 阿莫电子论坛 - 东莞阿莫电子网站)
完结!
下一部分在添加鼠标的HID操作
这个工程的例子已经上传到github
Pepperrrrrr/stm32f4_HID: stm32f411ceu6_stm32cubemx_HIDkeyboard (github.com)
大佬们给个小星星吧
标签:BEGIN,CODE,END,stm32f411CEU6,stm32cubemx,HID,USER,RCC From: https://www.cnblogs.com/hjf-log/p/17663638.html