From 0cc92b6a2462017c89a5210f592d20cfc905cf34 Mon Sep 17 00:00:00 2001 From: wangyz1997 Date: Mon, 16 Sep 2024 21:25:27 +0800 Subject: [PATCH] stm32h743_cherryusb_hs: add key support --- stm32h743_cherryusb_hs/Core/Inc/main.h | 10 +++ stm32h743_cherryusb_hs/Core/Src/main.c | 82 +++++++++++++++++-- .../Core/Src/usb_device_custom_hid.c | 29 +++---- .../MDK-ARM/stm32h743_cherryusb_hs.uvprojx | 2 +- .../stm32h743_cherryusb_hs.ioc | 51 ++++++++++-- 5 files changed, 142 insertions(+), 32 deletions(-) diff --git a/stm32h743_cherryusb_hs/Core/Inc/main.h b/stm32h743_cherryusb_hs/Core/Inc/main.h index a7ca01b..5ba265e 100644 --- a/stm32h743_cherryusb_hs/Core/Inc/main.h +++ b/stm32h743_cherryusb_hs/Core/Inc/main.h @@ -57,6 +57,16 @@ void Error_Handler(void); /* USER CODE END EFP */ /* Private defines -----------------------------------------------------------*/ +#define LED_Y2_Pin GPIO_PIN_2 +#define LED_Y2_GPIO_Port GPIOE +#define LED_Y1_Pin GPIO_PIN_6 +#define LED_Y1_GPIO_Port GPIOA +#define KEY1_Pin GPIO_PIN_5 +#define KEY1_GPIO_Port GPIOC +#define KEY2_Pin GPIO_PIN_9 +#define KEY2_GPIO_Port GPIOE +#define KEY3_Pin GPIO_PIN_11 +#define KEY3_GPIO_Port GPIOE #define LED_R_Pin GPIO_PIN_13 #define LED_R_GPIO_Port GPIOD #define LED_G_Pin GPIO_PIN_14 diff --git a/stm32h743_cherryusb_hs/Core/Src/main.c b/stm32h743_cherryusb_hs/Core/Src/main.c index 443b245..231a2f1 100644 --- a/stm32h743_cherryusb_hs/Core/Src/main.c +++ b/stm32h743_cherryusb_hs/Core/Src/main.c @@ -31,8 +31,13 @@ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ -#define LED_ON_STR "LED已打开" -#define LED_OFF_STR "LED已关闭" +#define LED_R_ON_STR "红色LED打开" +#define LED_R_OFF_STR "红色LED关闭" +#define LED_G_ON_STR "绿色LED打开" +#define LED_G_OFF_STR "绿色LED关闭" +#define KEY1_PRESSED_STR "按键1按下" +#define KEY2_PRESSED_STR "按键2按下" +#define KEY3_PRESSED_STR "按键3按下" /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ @@ -110,12 +115,43 @@ int main(void) while (1) { if (hid_led_status == 1) { - hid_custom_send((const uint8_t*)LED_OFF_STR, sizeof(LED_OFF_STR)); + HAL_GPIO_WritePin(LED_R_GPIO_Port, LED_R_Pin, GPIO_PIN_RESET); + hid_custom_send((const uint8_t*)LED_R_OFF_STR, sizeof(LED_R_OFF_STR)); } else if (hid_led_status == 2) { - hid_custom_send((const uint8_t*)LED_ON_STR, sizeof(LED_ON_STR)); + HAL_GPIO_WritePin(LED_R_GPIO_Port, LED_R_Pin, GPIO_PIN_SET); + hid_custom_send((const uint8_t*)LED_R_ON_STR, sizeof(LED_R_ON_STR)); + } else if (hid_led_status == 3) { + HAL_GPIO_WritePin(LED_G_GPIO_Port, LED_G_Pin, GPIO_PIN_RESET); + hid_custom_send((const uint8_t*)LED_G_OFF_STR, sizeof(LED_G_OFF_STR)); + } else if (hid_led_status == 4) { + HAL_GPIO_WritePin(LED_G_GPIO_Port, LED_G_Pin, GPIO_PIN_SET); + hid_custom_send((const uint8_t*)LED_G_ON_STR, sizeof(LED_G_ON_STR)); } - + hid_led_status = 0; + + if (HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin) == GPIO_PIN_RESET) { + HAL_Delay(10); + hid_custom_send((const uint8_t*)KEY1_PRESSED_STR, sizeof(KEY1_PRESSED_STR)); + while (HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin) == GPIO_PIN_RESET); + HAL_Delay(10); + } + + if (HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin) == GPIO_PIN_RESET) { + HAL_Delay(10); + hid_custom_send((const uint8_t*)KEY2_PRESSED_STR, sizeof(KEY2_PRESSED_STR)); + HAL_GPIO_TogglePin(LED_R_GPIO_Port, LED_R_Pin); + while (HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin) == GPIO_PIN_RESET); + HAL_Delay(10); + } + + if (HAL_GPIO_ReadPin(KEY3_GPIO_Port, KEY3_Pin) == GPIO_PIN_RESET) { + HAL_Delay(10); + hid_custom_send((const uint8_t*)KEY3_PRESSED_STR, sizeof(KEY3_PRESSED_STR)); + HAL_GPIO_TogglePin(LED_G_GPIO_Port, LED_G_Pin); + while (HAL_GPIO_ReadPin(KEY3_GPIO_Port, KEY3_Pin) == GPIO_PIN_RESET); + HAL_Delay(10); + } /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ @@ -234,12 +270,46 @@ static void MX_GPIO_Init(void) /* USER CODE END MX_GPIO_Init_1 */ /* GPIO Ports Clock Enable */ - __HAL_RCC_GPIOD_CLK_ENABLE(); + __HAL_RCC_GPIOE_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); + __HAL_RCC_GPIOC_CLK_ENABLE(); + __HAL_RCC_GPIOD_CLK_ENABLE(); + + /*Configure GPIO pin Output Level */ + HAL_GPIO_WritePin(LED_Y2_GPIO_Port, LED_Y2_Pin, GPIO_PIN_SET); + + /*Configure GPIO pin Output Level */ + HAL_GPIO_WritePin(LED_Y1_GPIO_Port, LED_Y1_Pin, GPIO_PIN_SET); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOD, LED_R_Pin|LED_G_Pin, GPIO_PIN_RESET); + /*Configure GPIO pin : LED_Y2_Pin */ + GPIO_InitStruct.Pin = LED_Y2_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(LED_Y2_GPIO_Port, &GPIO_InitStruct); + + /*Configure GPIO pin : LED_Y1_Pin */ + GPIO_InitStruct.Pin = LED_Y1_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(LED_Y1_GPIO_Port, &GPIO_InitStruct); + + /*Configure GPIO pin : KEY1_Pin */ + GPIO_InitStruct.Pin = KEY1_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + GPIO_InitStruct.Pull = GPIO_PULLUP; + HAL_GPIO_Init(KEY1_GPIO_Port, &GPIO_InitStruct); + + /*Configure GPIO pins : KEY2_Pin KEY3_Pin */ + GPIO_InitStruct.Pin = KEY2_Pin|KEY3_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + GPIO_InitStruct.Pull = GPIO_PULLUP; + HAL_GPIO_Init(GPIOE, &GPIO_InitStruct); + /*Configure GPIO pins : LED_R_Pin LED_G_Pin */ GPIO_InitStruct.Pin = LED_R_Pin|LED_G_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; diff --git a/stm32h743_cherryusb_hs/Core/Src/usb_device_custom_hid.c b/stm32h743_cherryusb_hs/Core/Src/usb_device_custom_hid.c index eecb6aa..e0e2a92 100644 --- a/stm32h743_cherryusb_hs/Core/Src/usb_device_custom_hid.c +++ b/stm32h743_cherryusb_hs/Core/Src/usb_device_custom_hid.c @@ -166,13 +166,6 @@ static const uint8_t hid_custom_report_desc[HID_CUSTOM_REPORT_DESC_SIZE] = { 0x06, 0x00, 0xFF, /* USAGE_PAGE (Vendor Defined Page 0) */ 0x09, 0x01, /* USAGE (Vendor Usage 1) */ 0xA1, 0x01, /* COLLECTION (Application) */ - 0x85, 0x02, /* REPORT ID (0x02) */ - 0x09, 0x01, /* USAGE (Vendor Usage 1) */ - 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ - 0x25, 0xFF, /* LOGICAL_MAXIMUM (255) */ - 0x75, 0x08, /* REPORT_SIZE (8) */ - 0x95, CUSTOM_HID_IN_EP_SIZE-1, /* REPORT_COUNT (63) */ - 0x81, 0x02, /* INPUT (Data,Var,Abs) */ 0x85, 0x01, /* REPORT ID (0x01) */ 0x09, 0x01, /* USAGE (Vendor Usage 1) */ 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ @@ -180,6 +173,13 @@ static const uint8_t hid_custom_report_desc[HID_CUSTOM_REPORT_DESC_SIZE] = { 0x75, 0x08, /* REPORT_SIZE (8) */ 0x95, CUSTOM_HID_OUT_EP_SIZE-1, /* REPORT_COUNT (63) */ 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */ + 0x85, 0x02, /* REPORT ID (0x02) */ + 0x09, 0x01, /* USAGE (Vendor Usage 1) */ + 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ + 0x25, 0xFF, /* LOGICAL_MAXIMUM (255) */ + 0x75, 0x08, /* REPORT_SIZE (8) */ + 0x95, CUSTOM_HID_IN_EP_SIZE-1, /* REPORT_COUNT (63) */ + 0x81, 0x02, /* INPUT (Data,Var,Abs) */ 0xC0 /* END_COLLECTION */ }; @@ -204,7 +204,8 @@ static void usbd_event_handler(uint8_t busid, uint8_t event) /* setup first out ep read transfer */ usbd_ep_start_read(busid, CUSTOM_HID_OUT_EP, read_buffer, CUSTOM_HID_OUT_EP_SIZE); usb_hid_status = HID_STATE_IDLE; - HAL_GPIO_WritePin(LED_G_GPIO_Port, LED_G_Pin, GPIO_PIN_SET); + HAL_GPIO_WritePin(LED_Y1_GPIO_Port, LED_Y1_Pin, GPIO_PIN_SET); + HAL_GPIO_WritePin(LED_Y2_GPIO_Port, LED_Y2_Pin, GPIO_PIN_SET); break; case USBD_EVENT_SET_REMOTE_WAKEUP: break; @@ -260,7 +261,7 @@ static void usbd_hid_custom_in_callback(uint8_t busid, uint8_t ep, uint32_t nbyt USB_LOG_RAW("%d bytes of data sent, content:\r\n", nbytes); print_buffer(send_buffer, nbytes, 8); - HAL_GPIO_TogglePin(LED_G_GPIO_Port, LED_G_Pin); + HAL_GPIO_TogglePin(LED_Y2_GPIO_Port, LED_Y2_Pin); } static void usbd_hid_custom_out_callback(uint8_t busid, uint8_t ep, uint32_t nbytes) @@ -269,13 +270,9 @@ static void usbd_hid_custom_out_callback(uint8_t busid, uint8_t ep, uint32_t nby USB_LOG_RAW("%d bytes of data received, content:\r\n", nbytes); print_buffer(read_buffer, nbytes, 8); - if (read_buffer[1] == 0x00) { - HAL_GPIO_WritePin(LED_R_GPIO_Port, LED_R_Pin, GPIO_PIN_RESET); - hid_led_status = 1; - } else { - HAL_GPIO_WritePin(LED_R_GPIO_Port, LED_R_Pin, GPIO_PIN_SET); - hid_led_status = 2; - } + HAL_GPIO_TogglePin(LED_Y1_GPIO_Port, LED_Y1_Pin); + + hid_led_status = read_buffer[1]; } static void fill_usb_descriptor_serial_number(void) diff --git a/stm32h743_cherryusb_hs/MDK-ARM/stm32h743_cherryusb_hs.uvprojx b/stm32h743_cherryusb_hs/MDK-ARM/stm32h743_cherryusb_hs.uvprojx index 1229705..32c2632 100644 --- a/stm32h743_cherryusb_hs/MDK-ARM/stm32h743_cherryusb_hs.uvprojx +++ b/stm32h743_cherryusb_hs/MDK-ARM/stm32h743_cherryusb_hs.uvprojx @@ -137,7 +137,7 @@ 4101 1 - BIN\UL2V8M.DLL + BIN\UL2CM3.DLL diff --git a/stm32h743_cherryusb_hs/stm32h743_cherryusb_hs.ioc b/stm32h743_cherryusb_hs/stm32h743_cherryusb_hs.ioc index 55d7aab..2fc91d9 100644 --- a/stm32h743_cherryusb_hs/stm32h743_cherryusb_hs.ioc +++ b/stm32h743_cherryusb_hs/stm32h743_cherryusb_hs.ioc @@ -21,15 +21,20 @@ Mcu.IP6=USART1 Mcu.IPNb=7 Mcu.Name=STM32H743VGTx Mcu.Package=LQFP100 -Mcu.Pin0=PD13 -Mcu.Pin1=PD14 -Mcu.Pin2=PA9 -Mcu.Pin3=PA10 -Mcu.Pin4=PA13 (JTMS/SWDIO) -Mcu.Pin5=PA14 (JTCK/SWCLK) -Mcu.Pin6=VP_SYS_VS_Systick -Mcu.Pin7=VP_MEMORYMAP_VS_MEMORYMAP -Mcu.PinsNb=8 +Mcu.Pin0=PE2 +Mcu.Pin1=PA6 +Mcu.Pin10=PA14 (JTCK/SWCLK) +Mcu.Pin11=VP_SYS_VS_Systick +Mcu.Pin12=VP_MEMORYMAP_VS_MEMORYMAP +Mcu.Pin2=PC5 +Mcu.Pin3=PE9 +Mcu.Pin4=PE11 +Mcu.Pin5=PD13 +Mcu.Pin6=PD14 +Mcu.Pin7=PA9 +Mcu.Pin8=PA10 +Mcu.Pin9=PA13 (JTMS/SWDIO) +Mcu.PinsNb=13 Mcu.ThirdPartyNb=0 Mcu.UserConstants= Mcu.UserName=STM32H743VGTx @@ -53,9 +58,20 @@ PA13\ (JTMS/SWDIO).Mode=Serial_Wire PA13\ (JTMS/SWDIO).Signal=DEBUG_JTMS-SWDIO PA14\ (JTCK/SWCLK).Mode=Serial_Wire PA14\ (JTCK/SWCLK).Signal=DEBUG_JTCK-SWCLK +PA6.GPIOParameters=PinState,GPIO_Label +PA6.GPIO_Label=LED_Y1 +PA6.Locked=true +PA6.PinState=GPIO_PIN_SET +PA6.Signal=GPIO_Output PA9.Locked=true PA9.Mode=Asynchronous PA9.Signal=USART1_TX +PC5.GPIOParameters=GPIO_PuPd,GPIO_Label,GPIO_Mode +PC5.GPIO_Label=KEY1 +PC5.GPIO_Mode=GPIO_MODE_INPUT +PC5.GPIO_PuPd=GPIO_PULLUP +PC5.Locked=true +PC5.Signal=GPIO_Input PD13.GPIOParameters=GPIO_Label PD13.GPIO_Label=LED_R PD13.Locked=true @@ -64,6 +80,23 @@ PD14.GPIOParameters=GPIO_Label PD14.GPIO_Label=LED_G PD14.Locked=true PD14.Signal=GPIO_Output +PE11.GPIOParameters=GPIO_PuPd,GPIO_Label,GPIO_Mode +PE11.GPIO_Label=KEY3 +PE11.GPIO_Mode=GPIO_MODE_INPUT +PE11.GPIO_PuPd=GPIO_PULLUP +PE11.Locked=true +PE11.Signal=GPIO_Input +PE2.GPIOParameters=PinState,GPIO_Label +PE2.GPIO_Label=LED_Y2 +PE2.Locked=true +PE2.PinState=GPIO_PIN_SET +PE2.Signal=GPIO_Output +PE9.GPIOParameters=GPIO_PuPd,GPIO_Label,GPIO_Mode +PE9.GPIO_Label=KEY2 +PE9.GPIO_Mode=GPIO_MODE_INPUT +PE9.GPIO_PuPd=GPIO_PULLUP +PE9.Locked=true +PE9.Signal=GPIO_Input PinOutPanel.RotationAngle=0 ProjectManager.AskForMigrate=true ProjectManager.BackupPrevious=false