Guest User

main.c

a guest
Mar 30th, 2025
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.53 KB | Source Code | 0 0
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file           : main.c
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * Copyright (c) 2025 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.  
  23. /* Private includes ----------------------------------------------------------*/
  24. /* USER CODE BEGIN Includes */
  25. #include "usbd_hid.h"
  26. extern USBD_HandleTypeDef hUsbDeviceFS;
  27. /* USER CODE END Includes */
  28.  
  29. /* Private typedef -----------------------------------------------------------*/
  30. /* USER CODE BEGIN PTD */
  31. typedef struct
  32. {
  33.     uint8_t MODIFIER; // always 0
  34.     uint8_t RESERVED;
  35.     uint8_t KEYCODE1;
  36.     uint8_t KEYCODE2;
  37.     uint8_t KEYCODE3;
  38.     uint8_t KEYCODE4;
  39.     uint8_t KEYCODE5;
  40.     uint8_t KEYCODE6;
  41. }subKeyBoard;
  42. /* USER CODE END PTD */
  43.  
  44. /* Private define ------------------------------------------------------------*/
  45. /* USER CODE BEGIN PD */
  46. #define NUM_ROWS 3
  47. #define NUM_COLS 3
  48.  
  49. #define PINSET GPIOB
  50.  
  51. #define ROW0_PIN GPIO_PIN_5
  52. #define ROW1_PIN GPIO_PIN_4
  53. #define ROW2_PIN GPIO_PIN_3
  54.  
  55. #define COL0_PIN GPIO_PIN_2
  56. #define COL1_PIN GPIO_PIN_1
  57. #define COL2_PIN GPIO_PIN_0
  58. /* USER CODE END PD */
  59.  
  60. /* Private macro -------------------------------------------------------------*/
  61. /* USER CODE BEGIN PM */
  62.  
  63. /* USER CODE END PM */
  64.  
  65. /* Private variables ---------------------------------------------------------*/
  66.  
  67. /* USER CODE BEGIN PV */
  68. subKeyBoard kblord = {0,0,0,0,0,0,0,0};
  69. const uint8_t keymap[3][3] = {
  70.     {0x1E, 0x1F, 0x20},
  71.     {0x21, 0x22, 0x23},
  72.     {0x24, 0x25, 0x26}
  73. };
  74. /* USER CODE END PV */
  75.  
  76. /* Private function prototypes -----------------------------------------------*/
  77. void SystemClock_Config(void);
  78. static void MX_GPIO_Init(void);
  79. /* USER CODE BEGIN PFP */
  80.  
  81. /* USER CODE END PFP */
  82.  
  83. /* Private user code ---------------------------------------------------------*/
  84. /* USER CODE BEGIN 0 */
  85.  
  86. /* USER CODE END 0 */
  87.  
  88. /**
  89.   * @brief  The application entry point.
  90.   * @retval int
  91.   */
  92. int main(void)
  93. {
  94.  
  95.   /* USER CODE BEGIN 1 */
  96.  
  97.   /* USER CODE END 1 */
  98.  
  99.   /* MCU Configuration--------------------------------------------------------*/
  100.  
  101.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  102.   HAL_Init();
  103.  
  104.   /* USER CODE BEGIN Init */
  105.  
  106.   /* USER CODE END Init */
  107.  
  108.   /* Configure the system clock */
  109.   SystemClock_Config();
  110.  
  111.   /* USER CODE BEGIN SysInit */
  112.  
  113.   /* USER CODE END SysInit */
  114.  
  115.   /* Initialize all configured peripherals */
  116.   MX_GPIO_Init();
  117.   MX_USB_DEVICE_Init();
  118.   /* USER CODE BEGIN 2 */
  119.  
  120.   /* USER CODE END 2 */
  121.  
  122.   /* Infinite loop */
  123.   /* USER CODE BEGIN WHILE */
  124.   while (1)
  125.   {
  126.     /* USER CODE END WHILE */
  127.  
  128.     /* USER CODE BEGIN 3 */
  129.       uint8_t pressed_key = Keypad_Scan();
  130.  
  131.       // If a key is pressed, send the report
  132.       if (pressed_key != 0x00) {
  133.           SendKPReport(pressed_key);  // Send the key press report
  134.       }
  135.  
  136.       // Small delay to debounce and avoid excessive USB traffic
  137.       HAL_Delay(10);
  138.   }
  139.   /* USER CODE END 3 */
  140. }
  141.  
  142. /**
  143.   * @brief System Clock Configuration
  144.   * @retval None
  145.   */
  146. void SystemClock_Config(void)
  147. {
  148.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  149.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  150.   RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  151.  
  152.   /** Initializes the RCC Oscillators according to the specified parameters
  153.   * in the RCC_OscInitTypeDef structure.
  154.   */
  155.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48;
  156.   RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
  157.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  158.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  159.   {
  160.     Error_Handler();
  161.   }
  162.  
  163.   /** Initializes the CPU, AHB and APB buses clocks
  164.   */
  165.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  166.                               |RCC_CLOCKTYPE_PCLK1;
  167.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI48;
  168.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  169.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  170.  
  171.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  172.   {
  173.     Error_Handler();
  174.   }
  175.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
  176.   PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_HSI48;
  177.  
  178.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  179.   {
  180.     Error_Handler();
  181.   }
  182. }
  183.  
  184. /**
  185.   * @brief GPIO Initialization Function
  186.   * @param None
  187.   * @retval None
  188.   */
  189. static void MX_GPIO_Init(void)
  190. {
  191.   GPIO_InitTypeDef GPIO_InitStruct = {0};
  192.   /* USER CODE BEGIN MX_GPIO_Init_1 */
  193.  
  194.   /* USER CODE END MX_GPIO_Init_1 */
  195.  
  196.   /* GPIO Ports Clock Enable */
  197.   __HAL_RCC_GPIOB_CLK_ENABLE();
  198.   __HAL_RCC_GPIOA_CLK_ENABLE();
  199.  
  200.   /*Configure GPIO pin Output Level */
  201.   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5, GPIO_PIN_RESET);
  202.  
  203.   /*Configure GPIO pins : PB0 PB1 PB2 */
  204.   GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2;
  205.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  206.   GPIO_InitStruct.Pull = GPIO_NOPULL;                       /* Initially NOPULL */
  207.   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  208.  
  209.   /*Configure GPIO pins : PB3 PB4 PB5 */
  210.   GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5;
  211.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  212.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  213.   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  214.  
  215.   /* USER CODE BEGIN MX_GPIO_Init_2 */
  216.  
  217.   /* USER CODE END MX_GPIO_Init_2 */
  218. }
  219.  
  220. /* USER CODE BEGIN 4 */
  221.  
  222. //uint8_t Scan_KP(void) {
  223. //  uint8_t key = 0;
  224. //
  225. //  for (uint8_t row=0; row < NUM_ROWS; row++) {
  226. //      switch (row) {
  227. //      case 0:
  228. //          HAL_GPIO_WritePin(GPIOA, ROW0_PIN, GPIO_PIN_SET);  // Set ROW1 high
  229. //          HAL_GPIO_WritePin(GPIOA, ROW1_PIN, GPIO_PIN_RESET);  // Set ROW2 low
  230. //          HAL_GPIO_WritePin(GPIOA, ROW2_PIN, GPIO_PIN_RESET);  // Set ROW3 low
  231. //          break;
  232. //      case 1:
  233. //          HAL_GPIO_WritePin(GPIOA, ROW0_PIN, GPIO_PIN_RESET);  // Set ROW1 low
  234. //          HAL_GPIO_WritePin(GPIOA, ROW1_PIN, GPIO_PIN_SET);  // Set ROW2 high
  235. //          HAL_GPIO_WritePin(GPIOA, ROW2_PIN, GPIO_PIN_RESET);  // Set ROW3 low
  236. //          break;
  237. //       case 2:
  238. //          HAL_GPIO_WritePin(GPIOA, ROW0_PIN, GPIO_PIN_RESET);  // Set ROW1 low
  239. //          HAL_GPIO_WritePin(GPIOA, ROW1_PIN, GPIO_PIN_RESET);  // Set ROW2 low
  240. //          HAL_GPIO_WritePin(GPIOA, ROW2_PIN, GPIO_PIN_SET);  // Set ROW3 high
  241. //          break;
  242. //  }
  243. //
  244. //      uint8_t col = 0;
  245. //      if (HAL_GPIO_ReadPin(GPIOA, COL0_PIN) == GPIO_PIN_SET) {
  246. //          col = 1;
  247. //      }
  248. //      if (HAL_GPIO_ReadPin(GPIOA, COL1_PIN) == GPIO_PIN_SET) {
  249. //          col = 2;
  250. //      }
  251. //      if (HAL_GPIO_ReadPin(GPIOA, COL2_PIN) == GPIO_PIN_SET) {
  252. //          col = 3;
  253. //      }
  254. //
  255. //      if (col != 0) {
  256. //          // A key is pressed
  257. //          key = (row + 1) * 3 - (3 - col); //Map the key based on row and column
  258. //          return key;
  259. //      }
  260. //  }
  261. //  return (uint8_t)0;
  262. //}
  263. //
  264.  
  265. uint8_t Keypad_Scan(void) {
  266.     // Loop through rows
  267.     for (int row = 0; row < 3; row++) {
  268.         // Activate the current row (set it low)
  269.         HAL_GPIO_WritePin(PINSET, ROW0_PIN << row, GPIO_PIN_RESET);
  270.  
  271.         // Check columns
  272.         for (int col = 0; col < 3; col++) {
  273.             if (HAL_GPIO_ReadPin(PINSET, COL0_PIN << col) == GPIO_PIN_RESET) {
  274.                 // Key is pressed, return the corresponding keycode from the keymap
  275.                 return keymap[row][col];
  276.             }
  277.         }
  278.  
  279.         // Deactivate the row (set it high)
  280.         HAL_GPIO_WritePin(PINSET, ROW0_PIN << row, GPIO_PIN_SET);
  281.     }
  282.  
  283.     // No key pressed
  284.     return 0x0;  // Return 0x00 if no key is pressed
  285. }
  286.  
  287. void SendKPReport(uint8_t key) {
  288.     if (key != 0x00) {  // If a key is pressed (not 0x00)
  289.         kblord.MODIFIER = 0x01;  // No modifier keys
  290.         kblord.KEYCODE1 = key;  // The pressed keycode
  291.         kblord.KEYCODE2 = 0x00;  // No second key (only 1 key in the report for now)
  292.         kblord.KEYCODE3 = 0x00;  // No third key
  293.         kblord.KEYCODE4 = 0x00;  // No fourth key
  294.         kblord.KEYCODE5 = 0x00;  // No fifth key
  295.         kblord.KEYCODE6 = 0x00;  // No sixth key
  296.  
  297.         // Send the key press report to USB HID
  298.         USBD_HID_SendReport(&hUsbDeviceFS, &kblord, sizeof(kblord));
  299.  
  300.         // Reset the keycode to 0 after sending the report
  301.         kblord.KEYCODE1 = 0x00;
  302.     }
  303. }
  304. /* USER CODE END 4 */
  305.  
  306. /**
  307.   * @brief  This function is executed in case of error occurrence.
  308.   * @retval None
  309.   */
  310. void Error_Handler(void) {
  311.   /* USER CODE BEGIN Error_Handler_Debug */
  312.   /* User can add his own implementation to report the HAL error return state */
  313.   __disable_irq();
  314.   while (1)
  315.   {
  316.   }
  317.   /* USER CODE END Error_Handler_Debug */
  318. }
  319.  
  320.  
  321. #ifdef  USE_FULL_ASSERT
  322. /**
  323.   * @brief  Reports the name of the source file and the source line number
  324.   *         where the assert_param error has occurred.
  325.   * @param  file: pointer to the source file name
  326.   * @param  line: assert_param error line source number
  327.   * @retval None
  328.   */
  329. void assert_failed(uint8_t *file, uint32_t line)
  330. {
  331.   /* USER CODE BEGIN 6 */
  332.   /* User can add his own implementation to report the file name and line number,
  333.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  334.   /* USER CODE END 6 */
  335. }
  336. #endif /* USE_FULL_ASSERT */
  337.  
Tags: C99 STM32
Advertisement
Add Comment
Please, Sign In to add comment