362 lines
10 KiB
C
362 lines
10 KiB
C
/* USER CODE BEGIN Header */
|
|
/**
|
|
******************************************************************************
|
|
* @file : main.c
|
|
* @brief : Main program body
|
|
******************************************************************************
|
|
* @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"
|
|
|
|
/* Private includes ----------------------------------------------------------*/
|
|
/* USER CODE BEGIN Includes */
|
|
#include "bsp_key_in_out.h"
|
|
#include "usbd_core.h"
|
|
#include "usb_hid.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 ---------------------------------------------------------*/
|
|
TIM_HandleTypeDef htim6;
|
|
|
|
UART_HandleTypeDef huart1;
|
|
|
|
/* USER CODE BEGIN PV */
|
|
|
|
/* USER CODE END PV */
|
|
|
|
/* Private function prototypes -----------------------------------------------*/
|
|
void SystemClock_Config(void);
|
|
static void MX_GPIO_Init(void);
|
|
static void MX_USART1_UART_Init(void);
|
|
static void MX_TIM6_Init(void);
|
|
/* USER CODE BEGIN PFP */
|
|
void hid_keyboard_init(uint8_t busid, uint32_t reg_base);
|
|
void hid_keyboard_test(uint8_t busid, uint8_t key);
|
|
/* USER CODE END PFP */
|
|
|
|
/* Private user code ---------------------------------------------------------*/
|
|
/* USER CODE BEGIN 0 */
|
|
void usb_dc_low_level_init(void)
|
|
{
|
|
__HAL_RCC_USB_CLK_ENABLE();
|
|
|
|
HAL_NVIC_SetPriority(USB_IRQn, 0, 0);
|
|
HAL_NVIC_EnableIRQ(USB_IRQn);
|
|
}
|
|
|
|
int fputc(int c, FILE *f)
|
|
{
|
|
uint8_t ch = c;
|
|
HAL_UART_Transmit(&huart1, &ch, 1, 10);
|
|
return c;
|
|
}
|
|
/* 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_USART1_UART_Init();
|
|
MX_TIM6_Init();
|
|
/* USER CODE BEGIN 2 */
|
|
hid_keyboard_init(0, USB_BASE);
|
|
bsp_key_in_out_init();
|
|
|
|
bsp_key_out_set_event(bsp_key1, bsp_key_event_double);
|
|
bsp_key_out_set_event(bsp_key2, bsp_key_event_double);
|
|
bsp_key_out_set_event(bsp_key3, bsp_key_event_double);
|
|
bsp_key_out_set_event(bsp_key4, bsp_key_event_double);
|
|
bsp_key_out_set_event(bsp_key5, bsp_key_event_long);
|
|
bsp_key_out_set_event(bsp_key6, bsp_key_event_long);
|
|
bsp_key_out_set_event(bsp_key7, bsp_key_event_long);
|
|
bsp_key_out_set_event(bsp_key8, bsp_key_event_long);
|
|
/* USER CODE END 2 */
|
|
|
|
/* Infinite loop */
|
|
/* USER CODE BEGIN WHILE */
|
|
while (1) {
|
|
bsp_key_number_e key;
|
|
bsp_key_event_type_e event;
|
|
bsp_key_in_get_event(&key, &event);
|
|
bsp_key_out_set_event(key, event);
|
|
|
|
if (event == bsp_key_event_press) {
|
|
if (key == bsp_key1) {
|
|
hid_keyboard_test(0, HID_KBD_USAGE_1);
|
|
} else if (key == bsp_key2) {
|
|
hid_keyboard_test(0, HID_KBD_USAGE_1 + 1);
|
|
} else if (key == bsp_key3) {
|
|
hid_keyboard_test(0, HID_KBD_USAGE_1 + 2);
|
|
} else if (key == bsp_key4) {
|
|
hid_keyboard_test(0, HID_KBD_USAGE_1 + 3);
|
|
}
|
|
} else if (event == bsp_key_event_long) {
|
|
if (key == bsp_key1) {
|
|
hid_keyboard_test(0, HID_KBD_USAGE_A);
|
|
} else if (key == bsp_key2) {
|
|
hid_keyboard_test(0, HID_KBD_USAGE_A + 1);
|
|
} else if (key == bsp_key3) {
|
|
hid_keyboard_test(0, HID_KBD_USAGE_A + 2);
|
|
} else if (key == bsp_key4) {
|
|
hid_keyboard_test(0, HID_KBD_USAGE_A + 3);
|
|
}
|
|
} else {
|
|
hid_keyboard_test(0, 0x00);
|
|
}
|
|
/* 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};
|
|
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
|
|
|
|
/** Initializes the RCC Oscillators according to the specified parameters
|
|
* in the RCC_OscInitTypeDef structure.
|
|
*/
|
|
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48;
|
|
RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
|
|
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
|
|
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_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI48;
|
|
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
|
|
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
|
|
|
|
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
|
|
PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1;
|
|
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief TIM6 Initialization Function
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
static void MX_TIM6_Init(void)
|
|
{
|
|
|
|
/* USER CODE BEGIN TIM6_Init 0 */
|
|
|
|
/* USER CODE END TIM6_Init 0 */
|
|
|
|
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
|
|
|
/* USER CODE BEGIN TIM6_Init 1 */
|
|
|
|
/* USER CODE END TIM6_Init 1 */
|
|
htim6.Instance = TIM6;
|
|
htim6.Init.Prescaler = SystemCoreClock / 1000 -1;
|
|
htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
|
|
htim6.Init.Period = BSP_KEY_IN_OUT_INTERAEL_MS - 1;
|
|
htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
|
if (HAL_TIM_Base_Init(&htim6) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
|
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
|
if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
/* USER CODE BEGIN TIM6_Init 2 */
|
|
|
|
/* USER CODE END TIM6_Init 2 */
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief USART1 Initialization Function
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
static void MX_USART1_UART_Init(void)
|
|
{
|
|
|
|
/* USER CODE BEGIN USART1_Init 0 */
|
|
|
|
/* USER CODE END USART1_Init 0 */
|
|
|
|
/* USER CODE BEGIN USART1_Init 1 */
|
|
|
|
/* USER CODE END USART1_Init 1 */
|
|
huart1.Instance = USART1;
|
|
huart1.Init.BaudRate = 115200;
|
|
huart1.Init.WordLength = UART_WORDLENGTH_8B;
|
|
huart1.Init.StopBits = UART_STOPBITS_1;
|
|
huart1.Init.Parity = UART_PARITY_NONE;
|
|
huart1.Init.Mode = UART_MODE_TX_RX;
|
|
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
|
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
|
|
huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
|
|
huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
|
|
if (HAL_UART_Init(&huart1) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
/* USER CODE BEGIN USART1_Init 2 */
|
|
|
|
/* USER CODE END USART1_Init 2 */
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief GPIO Initialization Function
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
static void MX_GPIO_Init(void)
|
|
{
|
|
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
/* USER CODE BEGIN MX_GPIO_Init_1 */
|
|
/* USER CODE END MX_GPIO_Init_1 */
|
|
|
|
/* GPIO Ports Clock Enable */
|
|
__HAL_RCC_GPIOF_CLK_ENABLE();
|
|
__HAL_RCC_GPIOA_CLK_ENABLE();
|
|
__HAL_RCC_GPIOB_CLK_ENABLE();
|
|
|
|
/*Configure GPIO pin Output Level */
|
|
HAL_GPIO_WritePin(GPIOB, KEY_OUT4_Pin|KEY_OUT3_Pin|KEY_OUT2_Pin|KEY_OUT1_Pin
|
|
|KEY_OUT5_Pin|KEY_OUT6_Pin|KEY_OUT7_Pin|KEY_OUT8_Pin, GPIO_PIN_RESET);
|
|
|
|
/*Configure GPIO pin Output Level */
|
|
HAL_GPIO_WritePin(PIR_GPIO_Port, PIR_Pin, GPIO_PIN_SET);
|
|
|
|
/*Configure GPIO pins : KEY_IN1_Pin KEY_IN2_Pin KEY_IN3_Pin KEY_IN4_Pin */
|
|
GPIO_InitStruct.Pin = KEY_IN1_Pin|KEY_IN2_Pin|KEY_IN3_Pin|KEY_IN4_Pin;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
|
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
|
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|
|
|
/*Configure GPIO pins : KEY_OUT4_Pin KEY_OUT3_Pin KEY_OUT2_Pin KEY_OUT1_Pin
|
|
KEY_OUT5_Pin KEY_OUT6_Pin KEY_OUT7_Pin KEY_OUT8_Pin */
|
|
GPIO_InitStruct.Pin = KEY_OUT4_Pin|KEY_OUT3_Pin|KEY_OUT2_Pin|KEY_OUT1_Pin
|
|
|KEY_OUT5_Pin|KEY_OUT6_Pin|KEY_OUT7_Pin|KEY_OUT8_Pin;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
|
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
|
|
|
/*Configure GPIO pin : PIR_Pin */
|
|
GPIO_InitStruct.Pin = PIR_Pin;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
|
HAL_GPIO_Init(PIR_GPIO_Port, &GPIO_InitStruct);
|
|
|
|
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
|
/* USER CODE END MX_GPIO_Init_2 */
|
|
}
|
|
|
|
/* 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 */
|