main.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET);
  88. HAL_Delay(500);
  89. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
  90. HAL_Delay(500);
  91. /* USER CODE BEGIN 3 */
  92. }
  93. /* USER CODE END 3 */
  94. }
  95. /**
  96. * @brief System Clock Configuration
  97. * @retval None
  98. */
  99. void SystemClock_Config(void)
  100. {
  101. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  102. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  103. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  104. /** Initializes the RCC Oscillators according to the specified parameters
  105. * in the RCC_OscInitTypeDef structure.
  106. */
  107. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  108. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  109. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  110. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  111. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6;
  112. RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
  113. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  114. {
  115. Error_Handler();
  116. }
  117. /** Initializes the CPU, AHB and APB buses clocks
  118. */
  119. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  120. |RCC_CLOCKTYPE_PCLK1;
  121. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  122. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  123. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  124. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  125. {
  126. Error_Handler();
  127. }
  128. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
  129. PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL;
  130. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  131. {
  132. Error_Handler();
  133. }
  134. }
  135. /**
  136. * @brief GPIO Initialization Function
  137. * @param None
  138. * @retval None
  139. */
  140. static void MX_GPIO_Init(void)
  141. {
  142. GPIO_InitTypeDef GPIO_InitStruct = {0};
  143. /* USER CODE BEGIN MX_GPIO_Init_1 */
  144. /* USER CODE END MX_GPIO_Init_1 */
  145. /* GPIO Ports Clock Enable */
  146. __HAL_RCC_GPIOF_CLK_ENABLE();
  147. __HAL_RCC_GPIOA_CLK_ENABLE();
  148. __HAL_RCC_GPIOB_CLK_ENABLE();
  149. /*Configure GPIO pins : PA0 PA1 PA2 PA3
  150. PA4 PA5 PA6 PA7 */
  151. GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_2|GPIO_PIN_3
  152. |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
  153. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  154. GPIO_InitStruct.Pull = GPIO_NOPULL;
  155. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  156. GPIO_InitStruct.Pin = GPIO_PIN_1;
  157. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  158. GPIO_InitStruct.Pull = GPIO_NOPULL;
  159. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  160. /*Configure GPIO pin : PB1 */
  161. GPIO_InitStruct.Pin = GPIO_PIN_1;
  162. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  163. GPIO_InitStruct.Pull = GPIO_NOPULL;
  164. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  165. /* USER CODE BEGIN MX_GPIO_Init_2 */
  166. /* USER CODE END MX_GPIO_Init_2 */
  167. }
  168. /* USER CODE BEGIN 4 */
  169. /* USER CODE END 4 */
  170. /**
  171. * @brief This function is executed in case of error occurrence.
  172. * @retval None
  173. */
  174. void Error_Handler(void)
  175. {
  176. /* USER CODE BEGIN Error_Handler_Debug */
  177. /* User can add his own implementation to report the HAL error return state */
  178. __disable_irq();
  179. while (1)
  180. {
  181. }
  182. /* USER CODE END Error_Handler_Debug */
  183. }
  184. #ifdef USE_FULL_ASSERT
  185. /**
  186. * @brief Reports the name of the source file and the source line number
  187. * where the assert_param error has occurred.
  188. * @param file: pointer to the source file name
  189. * @param line: assert_param error line source number
  190. * @retval None
  191. */
  192. void assert_failed(uint8_t *file, uint32_t line)
  193. {
  194. /* USER CODE BEGIN 6 */
  195. /* User can add his own implementation to report the file name and line number,
  196. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  197. /* USER CODE END 6 */
  198. }
  199. #endif /* USE_FULL_ASSERT */