STM32 HID键盘开发笔记

CubeMX配置

SYS中要把Debug设置为Serial_Wire

alt text

键盘开关引脚要设置为下降沿触发,上拉,如图 alt text

USB配置

alt text

引脚图alt text

代码注意事项

要修改usbd_hic.c中的报告设备描述符

__ALIGN_BEGIN static uint8_t HID_MOUSE_ReportDesc[HID_MOUSE_REPORT_DESC_SIZE] __ALIGN_END =

{

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

};

修改usbd_hid.h中的代码段为:#define HID_EPIN_SIZE 0x08U#define HID_MOUSE_REPORT_DESC_SIZE 63U

alt text

中断部分要加入: #include "usb_device.h" extern USBD_HandleTypeDef hUsbDeviceFS; 如图 alt text

分别在各自的IRQHandled中加入要执行的代码,在按键按下时,执行CV和其他操作 stm32f1xx_it.c如下

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    stm32f1xx_it.c
  * @brief   Interrupt Service Routines.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 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 "stm32f1xx_it.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "usb_device.h"
extern USBD_HandleTypeDef hUsbDeviceFS;
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN TD */
uint8_t hid_buffer[8]={0};
uint8_t hid_buffer_0[8]={0};
/* USER CODE END TD */

/* 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 -----------------------------------------------*/
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/* External variables --------------------------------------------------------*/
extern PCD_HandleTypeDef hpcd_USB_FS;
/* USER CODE BEGIN EV */

/* USER CODE END EV */

/******************************************************************************/
/*           Cortex-M3 Processor Interruption and Exception Handlers          */
/******************************************************************************/
/**
  * @brief This function handles Non maskable interrupt.
  */
void NMI_Handler(void)
{
  /* USER CODE BEGIN NonMaskableInt_IRQn 0 */

  /* USER CODE END NonMaskableInt_IRQn 0 */
  /* USER CODE BEGIN NonMaskableInt_IRQn 1 */
   while (1)
  {
  }
  /* USER CODE END NonMaskableInt_IRQn 1 */
}

/**
  * @brief This function handles Hard fault interrupt.
  */
void HardFault_Handler(void)
{
  /* USER CODE BEGIN HardFault_IRQn 0 */

  /* USER CODE END HardFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_HardFault_IRQn 0 */
    /* USER CODE END W1_HardFault_IRQn 0 */
  }
}

/**
  * @brief This function handles Memory management fault.
  */
void MemManage_Handler(void)
{
  /* USER CODE BEGIN MemoryManagement_IRQn 0 */

  /* USER CODE END MemoryManagement_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */
    /* USER CODE END W1_MemoryManagement_IRQn 0 */
  }
}

/**
  * @brief This function handles Prefetch fault, memory access fault.
  */
void BusFault_Handler(void)
{
  /* USER CODE BEGIN BusFault_IRQn 0 */

  /* USER CODE END BusFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_BusFault_IRQn 0 */
    /* USER CODE END W1_BusFault_IRQn 0 */
  }
}

/**
  * @brief This function handles Undefined instruction or illegal state.
  */
void UsageFault_Handler(void)
{
  /* USER CODE BEGIN UsageFault_IRQn 0 */

  /* USER CODE END UsageFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_UsageFault_IRQn 0 */
    /* USER CODE END W1_UsageFault_IRQn 0 */
  }
}

/**
  * @brief This function handles System service call via SWI instruction.
  */
void SVC_Handler(void)
{
  /* USER CODE BEGIN SVCall_IRQn 0 */

  /* USER CODE END SVCall_IRQn 0 */
  /* USER CODE BEGIN SVCall_IRQn 1 */

  /* USER CODE END SVCall_IRQn 1 */
}

/**
  * @brief This function handles Debug monitor.
  */
void DebugMon_Handler(void)
{
  /* USER CODE BEGIN DebugMonitor_IRQn 0 */

  /* USER CODE END DebugMonitor_IRQn 0 */
  /* USER CODE BEGIN DebugMonitor_IRQn 1 */

  /* USER CODE END DebugMonitor_IRQn 1 */
}

/**
  * @brief This function handles Pendable request for system service.
  */
void PendSV_Handler(void)
{
  /* USER CODE BEGIN PendSV_IRQn 0 */

  /* USER CODE END PendSV_IRQn 0 */
  /* USER CODE BEGIN PendSV_IRQn 1 */

  /* USER CODE END PendSV_IRQn 1 */
}

/**
  * @brief This function handles System tick timer.
  */
void SysTick_Handler(void)
{
  /* USER CODE BEGIN SysTick_IRQn 0 */

  /* USER CODE END SysTick_IRQn 0 */
  HAL_IncTick();
  /* USER CODE BEGIN SysTick_IRQn 1 */

  /* USER CODE END SysTick_IRQn 1 */
}

/******************************************************************************/
/* STM32F1xx Peripheral Interrupt Handlers                                    */
/* Add here the Interrupt Handlers for the used peripherals.                  */
/* For the available peripheral interrupt handler names,                      */
/* please refer to the startup file (startup_stm32f1xx.s).                    */
/******************************************************************************/

/**
  * @brief This function handles USB low priority or CAN RX0 interrupts.
  */
void USB_LP_CAN1_RX0_IRQHandler(void)
{
  /* USER CODE BEGIN USB_LP_CAN1_RX0_IRQn 0 */

  /* USER CODE END USB_LP_CAN1_RX0_IRQn 0 */
  HAL_PCD_IRQHandler(&hpcd_USB_FS);
  /* USER CODE BEGIN USB_LP_CAN1_RX0_IRQn 1 */

  /* USER CODE END USB_LP_CAN1_RX0_IRQn 1 */
}

/**
  * @brief This function handles EXTI line[9:5] interrupts.
  */
void EXTI9_5_IRQHandler(void)
{
  /* USER CODE BEGIN EXTI9_5_IRQn 0 */
  HAL_Delay(10);
  if(HAL_GPIO_ReadPin(CC_GPIO_Port,CC_Pin)==GPIO_PIN_RESET){
    HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_RESET);
    
    hid_buffer[0]=0x01;
    hid_buffer[2]=0x06;//C
    USBD_HID_SendReport(&hUsbDeviceFS,hid_buffer,sizeof(hid_buffer));
    HAL_Delay(20);
    USBD_HID_SendReport(&hUsbDeviceFS,hid_buffer_0,sizeof(hid_buffer_0));

    // HAL_GPIO_TogglePin(LED1_GPIO_Port,LED1_Pin);
    // HAL_Delay(100);
    // HAL_GPIO_TogglePin(LED1_GPIO_Port,LED1_Pin);
  }

  if(HAL_GPIO_ReadPin(CV_GPIO_Port,CV_Pin)==GPIO_PIN_RESET){
    HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_SET);
    
    hid_buffer[0]=0x01;
    hid_buffer[2]=0x19;//V
    USBD_HID_SendReport(&hUsbDeviceFS,hid_buffer,sizeof(hid_buffer));
    HAL_Delay(20);
    USBD_HID_SendReport(&hUsbDeviceFS,hid_buffer_0,sizeof(hid_buffer_0));


    // HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin,GPIO_PIN_RESET);
    // HAL_Delay(2000);
    // HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin,GPIO_PIN_SET);
    // HAL_GPIO_TogglePin(LED2_GPIO_Port,LED2_Pin);
    // HAL_Delay(100);
    // HAL_GPIO_TogglePin(LED2_GPIO_Port,LED2_Pin);
  }
    
  

  /* USER CODE END EXTI9_5_IRQn 0 */
  HAL_GPIO_EXTI_IRQHandler(CC_Pin);
  HAL_GPIO_EXTI_IRQHandler(CV_Pin);
  /* USER CODE BEGIN EXTI9_5_IRQn 1 */

  /* USER CODE END EXTI9_5_IRQn 1 */
}

/**
  * @brief This function handles EXTI line[15:10] interrupts.
  */
void EXTI15_10_IRQHandler(void)
{
  /* USER CODE BEGIN EXTI15_10_IRQn 0 */
  HAL_Delay(10);
  if(HAL_GPIO_ReadPin(ENTER_GPIO_Port,ENTER_Pin)==GPIO_PIN_RESET){
    HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_SET);
    
    hid_buffer[0]=0x00;
    hid_buffer[2]=0x28;
    USBD_HID_SendReport(&hUsbDeviceFS,hid_buffer,sizeof(hid_buffer));
    HAL_Delay(20);
    USBD_HID_SendReport(&hUsbDeviceFS,hid_buffer_0,sizeof(hid_buffer_0));

    // HAL_GPIO_TogglePin(LED3_GPIO_Port,LED3_Pin);
  }

  /* USER CODE END EXTI15_10_IRQn 0 */
  HAL_GPIO_EXTI_IRQHandler(ENTER_Pin);
  /* USER CODE BEGIN EXTI15_10_IRQn 1 */

  /* USER CODE END EXTI15_10_IRQn 1 */
}

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */

硬件实物图

alt text alt text alt text


本文章使用limfx的vscode插件快速发布

参考视频

https://www.bilibili.com/video/BV1Fj411V7aq?t=629.3

https://www.bilibili.com/video/BV1dA4m1c7VE?t=17.3&p=12

https://www.bilibili.com/video/BV1CL4y157Wg?t=265.2&p=20