1. STM32CubeMX 설정
18 편에서 했던 HID 마우스 설정이랑 같습니다.
이렇게 하면 기본적으로 마우스 설정으로 되서
usbd_hid.c
usbd_hid.h
위에 2 파일에서 값을 바꿔줘야 합니다.
2. usbd_hid.c
Middlewares 에 있는 usbd_hid,c 에서
변경 전 |
변경 후 |
__ALIGN_BEGIN static uint8_t HID_MOUSE_ReportDesc[HID_MOUSE_REPORT_DESC_SIZE] __ALIGN_END = { 0x05, 0x01, 0x09, 0x02, 0xA1, 0x01, 0x09, 0x01,
0xA1, 0x00, 0x05, 0x09, 0x19, 0x01, 0x29, 0x03,
0x15, 0x00, 0x25, 0x01, 0x95, 0x03, 0x75, 0x01,
0x81, 0x02, 0x95, 0x01, 0x75, 0x05, 0x81, 0x01,
0x05, 0x01, 0x09, 0x30, 0x09, 0x31, 0x09, 0x38,
0x15, 0x81, 0x25, 0x7F, 0x75, 0x08, 0x95, 0x03,
0x81, 0x06, 0xC0, 0x09, 0x3c, 0x05, 0xff, 0x09,
0x01, 0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 0x95,
0x02, 0xb1, 0x22, 0x75, 0x06, 0x95, 0x01, 0xb1,
0x01, 0xc0 }; |
__ALIGN_BEGIN static uint8_t HID_MOUSE_ReportDesc[HID_MOUSE_REPORT_DESC_SIZE] __ALIGN_END = { // 78 bytes 0x05, 0x01, // Usage Page (Generic Desktop Ctrls) 0x09, 0x06, // Usage (Keyboard) 0xA1, 0x01, // Collection (Application) 0x85, 0x01, // Report ID (1) 0x05, 0x07, // Usage Page (Kbrd/Keypad) 0x75, 0x01, // Report Size (1) 0x95, 0x08, // Report Count (8) 0x19, 0xE0, // Usage Minimum (0xE0) 0x29, 0xE7, // Usage Maximum (0xE7) 0x15, 0x00, // Logical Minimum (0) 0x25, 0x01, // Logical Maximum (1) 0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) 0x95, 0x03, // Report Count (3) 0x75, 0x08, // Report Size (8) 0x15, 0x00, // Logical Minimum (0) 0x25, 0x64, // Logical Maximum (100) 0x05, 0x07, // Usage Page (Kbrd/Keypad) 0x19, 0x00, // Usage Minimum (0x00) 0x29, 0x65, // Usage Maximum (0x65) 0x81, 0x00, // Input (Data,Array,Abs,No Wrap,Linear,Preferred State,No Null Position) 0xC0, // End Collection 0x05, 0x0C, // Usage Page (Consumer) 0x09, 0x01, // Usage (Consumer Control) 0xA1, 0x01, // Collection (Application) 0x85, 0x02, // Report ID (2) 0x05, 0x0C, // Usage Page (Consumer) 0x15, 0x00, // Logical Minimum (0) 0x25, 0x01, // Logical Maximum (1) 0x75, 0x01, // Report Size (1) 0x95, 0x08, // Report Count (8) 0x09, 0xB5, // Usage (Scan Next Track) 0x09, 0xB6, // Usage (Scan Previous Track) 0x09, 0xB7, // Usage (Stop) 0x09, 0xB8, // Usage (Eject) 0x09, 0xCD, // Usage (Play/Pause) 0x09, 0xE2, // Usage (Mute) 0x09, 0xE9, // Usage (Volume Increment) 0x09, 0xEA, // Usage (Volume Decrement) 0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) 0xC0, // End Collection }; |
변경 전 |
변경 후 |
__ALIGN_BEGIN static uint8_t USBD_HID_CfgDesc[USB_HID_CONFIG_DESC_SIZ] __ALIGN_END = { 0x09, /* bLength: Configuration Descriptor size */ USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ USB_HID_CONFIG_DESC_SIZ, /* wTotalLength: Bytes returned */ 0x00, 0x01, /*bNumInterfaces: 1 interface*/ 0x01, /*bConfigurationValue: Configuration value*/ 0x00, /*iConfiguration: Index of string descriptor describing the configuration*/ 0xE0, /*bmAttributes: bus powered and Support Remote Wake-up */ 0x32, /*MaxPower 100 mA: this current is used for detecting Vbus*/
/************** Descriptor of Joystick Mouse interface ****************/ /* 09 */ 0x09, /*bLength: Interface Descriptor size*/ USB_DESC_TYPE_INTERFACE,/*bDescriptorType: Interface descriptor type*/ 0x00, /*bInterfaceNumber: Number of Interface*/ 0x00, /*bAlternateSetting: Alternate setting*/ 0x01, /*bNumEndpoints*/ 0x03, /*bInterfaceClass: HID*/ 0x01, /*bInterfaceSubClass : 1=BOOT, 0=no boot*/ 0x02, /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/ 0, /*iInterface: Index of string descriptor*/ /******************** Descriptor of Joystick Mouse HID ********************/ /* 18 */ 0x09, /*bLength: HID Descriptor size*/ HID_DESCRIPTOR_TYPE, /*bDescriptorType: HID*/ 0x11, /*bcdHID: HID Class Spec release number*/ 0x01, 0x00, /*bCountryCode: Hardware target country*/ 0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/ 0x22, /*bDescriptorType*/ HID_MOUSE_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/ 0x00, /******************** Descriptor of Mouse endpoint ********************/ /* 27 */ 0x07, /*bLength: Endpoint Descriptor size*/ USB_DESC_TYPE_ENDPOINT, /*bDescriptorType:*/
HID_EPIN_ADDR, /*bEndpointAddress: Endpoint Address (IN)*/ 0x03, /*bmAttributes: Interrupt endpoint*/ HID_EPIN_SIZE, /*wMaxPacketSize: 4 Byte max */ 0x00, HID_FS_BINTERVAL, /*bInterval: Polling Interval (10 ms)*/ /* 34 */ } ; |
__ALIGN_BEGIN static uint8_t USBD_HID_CfgDesc[USB_HID_CONFIG_DESC_SIZ] __ALIGN_END = { 0x09, /* bLength: Configuration Descriptor size */ USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ USB_HID_CONFIG_DESC_SIZ, /* wTotalLength: Bytes returned */ 0x00, 0x01, /*bNumInterfaces: 1 interface*/ 0x01, /*bConfigurationValue: Configuration value*/ 0x00, /*iConfiguration: Index of string descriptor describing the configuration*/ 0xE0, /*bmAttributes: bus powered and Support Remote Wake-up */ 0x32, /*MaxPower 100 mA: this current is used for detecting Vbus*/
/************** Descriptor of Joystick Mouse interface ****************/ /* 09 */ 0x09, /*bLength: Interface Descriptor size*/ USB_DESC_TYPE_INTERFACE,/*bDescriptorType: Interface descriptor type*/ 0x00, /*bInterfaceNumber: Number of Interface*/ 0x00, /*bAlternateSetting: Alternate setting*/ 0x01, /*bNumEndpoints*/ 0x03, /*bInterfaceClass: HID*/ 0x00, /*bInterfaceSubClass : 1=BOOT, 0=no boot*/ 0x01, /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/ 0, /*iInterface: Index of string descriptor*/ /******************** Descriptor of Joystick Mouse HID ********************/ /* 18 */ 0x09, /*bLength: HID Descriptor size*/ HID_DESCRIPTOR_TYPE, /*bDescriptorType: HID*/ 0x11, /*bcdHID: HID Class Spec release number*/ 0x01, 0x00, /*bCountryCode: Hardware target country*/ 0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/ 0x22, /*bDescriptorType*/ HID_MOUSE_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/ 0x00, /******************** Descriptor of Mouse endpoint ********************/ /* 27 */ 0x07, /*bLength: Endpoint Descriptor size*/ USB_DESC_TYPE_ENDPOINT, /*bDescriptorType:*/
HID_EPIN_ADDR, /*bEndpointAddress: Endpoint Address (IN)*/ 0x03, /*bmAttributes: Interrupt endpoint*/ HID_EPIN_SIZE, /*wMaxPacketSize: 4 Byte max */ 0x00, HID_FS_BINTERVAL, /*bInterval: Polling Interval (10 ms)*/ /* 34 */ } ; |
3. usbd_hid.h
변경 전 |
변경 후 |
#define HID_EPIN_ADDR 0x81 #define HID_EPIN_SIZE 0x04 #define USB_HID_CONFIG_DESC_SIZ 34 #define USB_HID_DESC_SIZ 9 #define HID_MOUSE_REPORT_DESC_SIZE 74 |
#define HID_EPIN_ADDR 0x81 #define HID_EPIN_SIZE 0x05 #define USB_HID_CONFIG_DESC_SIZ 34 #define USB_HID_DESC_SIZ 9 #define HID_MOUSE_REPORT_DESC_SIZE 78 |
HID_EPIN_SIZE 이걸 0x05로 변경
HID_MOUSE_REPORT_DESC_SIZE 78로 변경
4. main.c
/* Includes ------------------------------------------------------------------*/ #include "main.h" #include "stm32f1xx_hal.h" #include "usb_device.h" /* USER CODE BEGIN Includes */ // USB keyboard codes #define USB_HID_MODIFIER_LEFT_CTRL 0x01 #define USB_HID_MODIFIER_LEFT_SHIFT 0x02 #define USB_HID_MODIFIER_LEFT_ALT 0x04 #define USB_HID_MODIFIER_LEFT_GUI 0x08 // (Win/Apple/Meta) #define USB_HID_MODIFIER_RIGHT_CTRL 0x10 #define USB_HID_MODIFIER_RIGHT_SHIFT 0x20 #define USB_HID_MODIFIER_RIGHT_ALT 0x40 #define USB_HID_MODIFIER_RIGHT_GUI 0x80 #define USB_HID_KEY_A 0x04 #define USB_HID_KEY_B 0x05 #define USB_HID_KEY_C 0x06 #define USB_HID_KEY_D 0x07 #define USB_HID_KEY_E 0x08 #define USB_HID_KEY_F 0x09 #define USB_HID_KEY_G 0x0A #define USB_HID_KEY_H 0x0B #define USB_HID_KEY_I 0x0C #define USB_HID_KEY_J 0x0D #define USB_HID_KEY_K 0x0E #define USB_HID_KEY_L 0x0F #define USB_HID_KEY_M 0x10 #define USB_HID_KEY_N 0x11 #define USB_HID_KEY_O 0x12 #define USB_HID_KEY_P 0x13 #define USB_HID_KEY_Q 0x14 #define USB_HID_KEY_R 0x15 #define USB_HID_KEY_S 0x16 #define USB_HID_KEY_T 0x17 #define USB_HID_KEY_U 0x18 #define USB_HID_KEY_V 0x19 #define USB_HID_KEY_W 0x1A #define USB_HID_KEY_X 0x1B #define USB_HID_KEY_Y 0x1C #define USB_HID_KEY_Z 0x1D /* USER CODE END Includes */ /* Private variables ---------------------------------------------------------*/ UART_HandleTypeDef huart1; /* USER CODE BEGIN PV */ /* Private variables ---------------------------------------------------------*/ /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_USART1_UART_Init(void); /* USER CODE BEGIN PFP */ /* Private function prototypes -----------------------------------------------*/ static void GetPointerData(uint8_t *pbuf); /* USER CODE END PFP */ /* USER CODE BEGIN 0 */ int fputc(int ch, FILE *f) { uint8_t temp[1]={ch}; HAL_UART_Transmit(&huart1, temp, 1, 2); return(ch); } /* USER CODE END 0 */ int main(void) { /* USER CODE BEGIN 1 */ struct keyboardHID_t { uint8_t id; uint8_t modifiers; uint8_t key1; uint8_t key2; uint8_t key3; }; struct keyboardHID_t keyboardHID; keyboardHID.id = 1; keyboardHID.modifiers = 0; keyboardHID.key1 = 0; keyboardHID.key2 = 0; keyboardHID.key3 = 0; // HID Media /* 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_USB_DEVICE_Init(); MX_USART1_UART_Init(); /* USER CODE BEGIN 2 */ HAL_StatusTypeDef R_stat; uint8_t UsartData[1]; // GetPointerData(HID_Buffer);
printf("rs232 test\r\n"); /* USER CODE END 2 */
/* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) {
/* USER CODE END WHILE */ R_stat = HAL_UART_Receive(&huart1,UsartData,1,5000); if(R_stat == HAL_OK){ printf("%c\r\n",UsartData[0]); if(UsartData[0]=='t'){ keyboardHID.modifiers = USB_HID_MODIFIER_LEFT_SHIFT; keyboardHID.key1 = USB_HID_KEY_H; USBD_HID_SendReport(&hUsbDeviceFS, &keyboardHID, sizeof(struct keyboardHID_t)); HAL_Delay(30);
keyboardHID.modifiers = 0; keyboardHID.key1 = USB_HID_KEY_E; USBD_HID_SendReport(&hUsbDeviceFS, &keyboardHID, sizeof(struct keyboardHID_t)); HAL_Delay(30);
keyboardHID.modifiers = 0; keyboardHID.key1 = USB_HID_KEY_L; USBD_HID_SendReport(&hUsbDeviceFS, &keyboardHID, sizeof(struct keyboardHID_t)); HAL_Delay(30);
keyboardHID.modifiers = 0; keyboardHID.key1 = 0; USBD_HID_SendReport(&hUsbDeviceFS, &keyboardHID, sizeof(struct keyboardHID_t)); HAL_Delay(30);
keyboardHID.modifiers = 0; keyboardHID.key1 = USB_HID_KEY_L; USBD_HID_SendReport(&hUsbDeviceFS, &keyboardHID, sizeof(struct keyboardHID_t)); HAL_Delay(30);
keyboardHID.modifiers = 0; keyboardHID.key1 = USB_HID_KEY_O; USBD_HID_SendReport(&hUsbDeviceFS, &keyboardHID, sizeof(struct keyboardHID_t)); HAL_Delay(30);
keyboardHID.modifiers = 0; keyboardHID.key1 = USB_HID_KEY_H; keyboardHID.key2 = USB_HID_KEY_N; keyboardHID.key3 = USB_HID_KEY_Y; USBD_HID_SendReport(&hUsbDeviceFS, &keyboardHID, sizeof(struct keyboardHID_t)); HAL_Delay(30);
keyboardHID.modifiers = 0; keyboardHID.key1 = 0; keyboardHID.key2 = 0; keyboardHID.key3 = 0; USBD_HID_SendReport(&hUsbDeviceFS, &keyboardHID, sizeof(struct keyboardHID_t)); HAL_Delay(30); } } /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ } |
5. 결과
Rs 통신으로 "t"를 전송하면
Hellohny라고 써진다~
'공부 > STM32F1' 카테고리의 다른 글
22.[stm32f103][hal] TFT LCD (구동칩ssd1289) 문자 쓰기 (0) | 2017.11.30 |
---|---|
20.[stm32f103][hal] TFT LCD (구동칩ssd1289) background (0) | 2017.11.29 |
18.[stm32f103][hal]USB HID 마우스 (1) | 2017.11.22 |
16.[stm32f103][hal]lcd -1602(1) (7) | 2017.11.02 |
15.[stm32f103][hal] 타이머 (6) _ Timer PWM One Pulse Mode (0) | 2017.09.28 |