main.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2024 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "usb_device.h"
  22. /* Private includes ----------------------------------------------------------*/
  23. /* USER CODE BEGIN Includes */
  24. extern USBD_HandleTypeDef hUsbDeviceFS;
  25. /* USER CODE END Includes */
  26. /* Private typedef -----------------------------------------------------------*/
  27. /* USER CODE BEGIN PTD */
  28. /* USER CODE END PTD */
  29. /* Private define ------------------------------------------------------------*/
  30. /* USER CODE BEGIN PD */
  31. /* USER CODE END PD */
  32. /* Private macro -------------------------------------------------------------*/
  33. /* USER CODE BEGIN PM */
  34. /* USER CODE END PM */
  35. /* Private variables ---------------------------------------------------------*/
  36. /* USER CODE BEGIN PV */
  37. /* USER CODE END PV */
  38. /* Private function prototypes -----------------------------------------------*/
  39. void SystemClock_Config(void);
  40. static void MX_GPIO_Init(void);
  41. /* USER CODE BEGIN PFP */
  42. /* USER CODE END PFP */
  43. /* Private user code ---------------------------------------------------------*/
  44. /* USER CODE BEGIN 0 */
  45. void SendKeyboardReport(uint8_t modifiers, uint8_t key1, uint8_t key2, uint8_t key3, uint8_t key4, uint8_t key5, uint8_t key6)
  46. {
  47. uint8_t report[8] = {0};
  48. report[0] = modifiers; // 修饰键(如Ctrl�Shift等)
  49. report[2] = key1; // 第一个按�
  50. report[3] = key2; // 第二个按�
  51. report[4] = key3; // 第三个按�
  52. report[5] = key4; // 第四个按�
  53. report[6] = key5; // 第五个按�
  54. report[7] = key6; // 第六个按�
  55. USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS, report, sizeof(report));
  56. }
  57. /* USER CODE END 0 */
  58. /**
  59. * @brief The application entry point.
  60. * @retval int
  61. */
  62. int main(void)
  63. {
  64. /* USER CODE BEGIN 1 */
  65. uint8_t HID_Buffer[8] = {0};
  66. uint8_t HID_Buffer_clean[8] = {0};
  67. /* USER CODE END 1 */
  68. /* MCU Configuration--------------------------------------------------------*/
  69. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  70. HAL_Init();
  71. /* USER CODE BEGIN Init */
  72. /* USER CODE END Init */
  73. /* Configure the system clock */
  74. SystemClock_Config();
  75. /* USER CODE BEGIN SysInit */
  76. /* USER CODE END SysInit */
  77. /* Initialize all configured peripherals */
  78. MX_GPIO_Init();
  79. MX_USB_DEVICE_Init();
  80. /* USER CODE BEGIN 2 */
  81. /* USER CODE END 2 */
  82. /* Infinite loop */
  83. /* USER CODE BEGIN WHILE */
  84. while (1)
  85. {
  86. /* USER CODE END WHILE */
  87. /* USER CODE BEGIN 3 */
  88. }
  89. /* USER CODE END 3 */
  90. }
  91. /**
  92. * @brief System Clock Configuration
  93. * @retval None
  94. */
  95. void SystemClock_Config(void)
  96. {
  97. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  98. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  99. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  100. /** Initializes the RCC Oscillators according to the specified parameters
  101. * in the RCC_OscInitTypeDef structure.
  102. */
  103. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  104. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  105. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  106. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  107. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6;
  108. RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
  109. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  110. {
  111. Error_Handler();
  112. }
  113. /** Initializes the CPU, AHB and APB buses clocks
  114. */
  115. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  116. |RCC_CLOCKTYPE_PCLK1;
  117. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  118. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  119. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  120. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  121. {
  122. Error_Handler();
  123. }
  124. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
  125. PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL;
  126. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  127. {
  128. Error_Handler();
  129. }
  130. }
  131. /**
  132. * @brief GPIO Initialization Function
  133. * @param None
  134. * @retval None
  135. */
  136. static void MX_GPIO_Init(void)
  137. {
  138. GPIO_InitTypeDef GPIO_InitStruct = {0};
  139. /* USER CODE BEGIN MX_GPIO_Init_1 */
  140. /* USER CODE END MX_GPIO_Init_1 */
  141. /* GPIO Ports Clock Enable */
  142. __HAL_RCC_GPIOF_CLK_ENABLE();
  143. __HAL_RCC_GPIOA_CLK_ENABLE();
  144. __HAL_RCC_GPIOB_CLK_ENABLE();
  145. /*Configure GPIO pins : PA0 PA1 PA2 PA3
  146. PA4 PA5 PA6 PA7 */
  147. GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3
  148. |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
  149. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  150. GPIO_InitStruct.Pull = GPIO_NOPULL;
  151. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  152. /*Configure GPIO pin : PB1 */
  153. GPIO_InitStruct.Pin = GPIO_PIN_1;
  154. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  155. GPIO_InitStruct.Pull = GPIO_NOPULL;
  156. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  157. /* USER CODE BEGIN MX_GPIO_Init_2 */
  158. /* USER CODE END MX_GPIO_Init_2 */
  159. }
  160. /* USER CODE BEGIN 4 */
  161. /* USER CODE END 4 */
  162. /**
  163. * @brief This function is executed in case of error occurrence.
  164. * @retval None
  165. */
  166. void Error_Handler(void)
  167. {
  168. /* USER CODE BEGIN Error_Handler_Debug */
  169. /* User can add his own implementation to report the HAL error return state */
  170. __disable_irq();
  171. while (1)
  172. {
  173. }
  174. /* USER CODE END Error_Handler_Debug */
  175. }
  176. #ifdef USE_FULL_ASSERT
  177. /**
  178. * @brief Reports the name of the source file and the source line number
  179. * where the assert_param error has occurred.
  180. * @param file: pointer to the source file name
  181. * @param line: assert_param error line source number
  182. * @retval None
  183. */
  184. void assert_failed(uint8_t *file, uint32_t line)
  185. {
  186. /* USER CODE BEGIN 6 */
  187. /* User can add his own implementation to report the file name and line number,
  188. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  189. /* USER CODE END 6 */
  190. }
  191. #endif /* USE_FULL_ASSERT */