Chào mọi người, hiện tại e đang làm một project về Keyboard HID trên con Stm32f103c8t6.
Hiện tại thì khi e cắm kit với pc thì pc đã nhận kit là một keyboard
Nhưng khi bấm nút (giả sử kí tự A) thì lại ko được. em dùng Mikroc
còn đây là thư viện
Hiện tại thì khi e cắm kit với pc thì pc đã nhận kit là một keyboard
Nhưng khi bấm nút (giả sử kí tự A) thì lại ko được. em dùng Mikroc
Code:
#include "USB_DSC.c" char readbuff[64]; char writebuff[64]; unsigned long int InterruptCount = 0; void USB0Interrupt() iv IVT_INT_USB_LP_CAN_RX0 { //GPIOB_ODR.B2 = 1; USB_Interrupt_Proc(); InterruptCount++; //GPIOB_ODR.B2 = 0; } void usb_init1() { GPIO_Digital_Output(&GPIOA_BASE, _GPIO_PINMASK_8); // USB Control GPIOA_ODR.B8 = 1; // activate USB HID_Enable(&readbuff,&writebuff); } void readKeyboard() { char modifier=0; char keycode[6]; //Some Keyboard Buttons keycode[0]=0; if (InterruptCount > 10) { if (GPIOA_IDR.B0==1) keycode[0]=0x04; //A or a if (GPIOB_IDR.B4==1) keycode[0]=0x16; //S or s if (GPIOB_IDR.B5==1) keycode[0]=0x07; //D or d if (GPIOB_IDR.B6==1) keycode[0]=0x09; //F or f if (GPIOB_IDR.B7==1) keycode[0]=0x0A; //G or g if (GPIOB_IDR.B8==1) keycode[0]=0x0B; //H or h if (GPIOB_IDR.B9==1) keycode[0]=0x0D; //J or j if (GPIOB_IDR.B10==1)keycode[0]=0x0E; //K or k keycode[1]=0; keycode[2]=0; keycode[3]=0; keycode[4]=0; keycode[5]=0; writebuff[0]=0x01; writebuff[1]=0b00000000; writebuff[2]=0x00; writebuff[3]=keycode; writebuff[4]=0; writebuff[5]=0; writebuff[6]=0; while(!HID_Write(&writebuff, 7)); writebuff[1] = 0; // No modifiers now writebuff[3] = 0; // No key pressed now while(!HID_Write(&writebuff, 7)); // Copy to USB buffer and try to send } } void main() { GPIO_digital_input(&GPIOB_BASE, _GPIO_PINMASK_ALL ); GPIO_digital_output(&GPIOB_BASE, _GPIO_PINMASK_2); GPIO_digital_input(&GPIOA_BASE, _GPIO_PINMASK_0 ); usb_init1(); UART1_init(115200); delay_ms(100); while(1) { readKeyboard(); delay_ms(100); } }
Code:
const unsigned int USB_VENDOR_ID = 0x1234; const unsigned int USB_PRODUCT_ID = 0x0001; const char USB_SELF_POWER = 0x80; // Self powered 0xC0, 0x80 bus powered const char USB_MAX_POWER = 50; // Bus power required in units of 2 mA const char HID_INPUT_REPORT_BYTES = 64; const char HID_OUTPUT_REPORT_BYTES = 64; const char USB_TRANSFER_TYPE = 0x03; //0x03 Interrupt const char EP_IN_INTERVAL = 1; const char EP_OUT_INTERVAL = 1; const char USB_INTERRUPT = 1; const char USB_HID_EP = 1; const char USB_HID_RPT_SIZE = 63; /* Device Descriptor */ const struct { char bLength; // bLength - Descriptor size in bytes (12h) char bDescriptorType; // bDescriptorType - The constant DEVICE (01h) unsigned int bcdUSB; // bcdUSB - USB specification release number (BCD) char bDeviceClass; // bDeviceClass - Class Code char bDeviceSubClass; // bDeviceSubClass - Subclass code char bDeviceProtocol; // bDeviceProtocol - Protocol code char bMaxPacketSize0; // bMaxPacketSize0 - Maximum packet size for endpoint 0 unsigned int idVendor; // idVendor - Vendor ID unsigned int idProduct; // idProduct - Product ID unsigned int bcdDevice; // bcdDevice - Device release number (BCD) char iManufacturer; // iManufacturer - Index of string descriptor for the manufacturer char iProduct; // iProduct - Index of string descriptor for the product. char iSerialNumber; // iSerialNumber - Index of string descriptor for the serial number. char bNumConfigurations; // bNumConfigurations - Number of possible configurations } device_dsc = { 0x12, // bLength 0x01, // bDescriptorType 0x0200, // bcdUSB 0x00, // bDeviceClass 0x00, // bDeviceSubClass 0x00, // bDeviceProtocol 8, // bMaxPacketSize0 USB_VENDOR_ID, // idVendor USB_PRODUCT_ID, // idProduct 0x0001, // bcdDevice 0x01, // iManufacturer 0x02, // iProduct 0x00, // iSerialNumber 0x01 // bNumConfigurations }; /* Configuration 1 Descriptor */ const char configDescriptor1[]= { // Configuration Descriptor 0x09, // bLength - Descriptor size in bytes 0x02, // bDescriptorType - The constant CONFIGURATION (02h) 0x29,0x00, // wTotalLength - The number of bytes in the configuration descriptor and all of its subordinate descriptors 1, // bNumInterfaces - Number of interfaces in the configuration 1, // bConfigurationValue - Identifier for Set Configuration and Get Configuration requests 0, // iConfiguration - Index of string descriptor for the configuration USB_SELF_POWER, // bmAttributes - Self/bus power and remote wakeup settings USB_MAX_POWER, // bMaxPower - Bus power required in units of 2 mA // Interface Descriptor 0x09, // bLength - Descriptor size in bytes (09h) 0x04, // bDescriptorType - The constant Interface (04h) 0, // bInterfaceNumber - Number identifying this interface 0, // bAlternateSetting - A number that identifies a descriptor with alternate settings for this bInterfaceNumber. 2, // bNumEndpoint - Number of endpoints supported not counting endpoint zero 0x03, // bInterfaceClass - Class code 0, // bInterfaceSubclass - Subclass code 0, // bInterfaceProtocol - Protocol code 0, // iInterface - Interface string index // HID Class-Specific Descriptor 0x09, // bLength - Descriptor size in bytes. 0x21, // bDescriptorType - This descriptor's type: 21h to indicate the HID class. 0x01,0x01, // bcdHID - HID specification release number (BCD). 0x00, // bCountryCode - Numeric expression identifying the country for localized hardware (BCD) or 00h. 1, // bNumDescriptors - Number of subordinate report and physical descriptors. 0x22, // bDescriptorType - The type of a class-specific descriptor that follows USB_HID_RPT_SIZE,0x00, // wDescriptorLength - Total length of the descriptor identified above. // Endpoint Descriptor 0x07, // bLength - Descriptor size in bytes (07h) 0x05, // bDescriptorType - The constant Endpoint (05h) USB_HID_EP | 0x80, // bEndpointAddress - Endpoint number and direction USB_TRANSFER_TYPE, // bmAttributes - Transfer type and supplementary information 0x40,0x00, // wMaxPacketSize - Maximum packet size supported EP_IN_INTERVAL, // bInterval - Service interval or NAK rate // Endpoint Descriptor 0x07, // bLength - Descriptor size in bytes (07h) 0x05, // bDescriptorType - The constant Endpoint (05h) USB_HID_EP, // bEndpointAddress - Endpoint number and direction USB_TRANSFER_TYPE, // bmAttributes - Transfer type and supplementary information 0x40,0x00, // wMaxPacketSize - Maximum packet size supported EP_OUT_INTERVAL // bInterval - Service interval or NAK rate }; const struct { char report[]; }hid_rpt_desc = { 0x05, 0x01, // USAGE_PAGE (Generic Desktop) 0x09, 0x06, // USAGE (Keyboard) 0xa1, 0x01, // COLLECTION (Application) 0x05, 0x07, // USAGE_PAGE (Keyboard) 0x19, 0xe0, // USAGE_MINIMUM 224(Keyboard LeftControl) 0x29, 0xe7, // USAGE_MAXIMUM 231(Keyboard Right GUI) (left and right: alt, shift, ctrl and win) 0x15, 0x00, // LOGICAL_MINIMUM (0) 0x25, 0x01, // LOGICAL_MAXIMUM (1) 0x75, 0x01, // REPORT_SIZE (1) 0x95, 0x08, // R EPORT_COUNT (8) 0x81, 0x02, // INPUT (Data,Var,Abs) 0x95, 0x01, // REPORT_COUNT (1) 0x75, 0x08, // REPORT_SIZE (8) 0x81, 0x03, // INPUT (Cnst,Var,Abs) 0x95, 0x05, // REPORT_COUNT (5) 0x75, 0x01, // REPORT_SIZE (1) 0x05, 0x08, // USAGE_PAGE (LEDs) 0x19, 0x01, // USAGE_MINIMUM (Num Lock) 0x29, 0x05, // USAGE_MAXIMUM (Kana) 0x91, 0x02, // OUTPUT (Data,Var,Abs) 0x95, 0x01, // REPORT_COUNT (1) 0x75, 0x03, // REPORT_SIZE (3) 0x91, 0x03, // OUTPUT (Cnst,Var,Abs) 0x95, 0x06, // REPORT_COUNT (6) 0x75, 0x08, // REPORT_SIZE (8) 0x15, 0x00, // LOGICAL_MINIMUM (0) 0x25, 0x65, // LOGICAL_MAXIMUM (101) 0x05, 0x07, // USAGE_PAGE (Keyboard) 0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated)) 0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application) 0x81, 0x00, // INPUT (Data,Ary,Abs) 0xc0 // END_COLLECTION }; //Language code string descriptor const struct { char bLength; char bDscType; unsigned int string[1]; } strd1 = { 4, 0x03, {0x0409} }; //Manufacturer string descriptor const struct{ char bLength; char bDscType; unsigned int string[10]; }strd2={ 22, //sizeof this descriptor string 0x03, {'T','U',' ','G','a','b','r','o','v','o'} }; //Product string descriptor const struct{ char bLength; char bDscType; unsigned int string[15]; }strd3={ 32, //sizeof this descriptor string 0x03, {'U','S','B',' ','M','a','n','i','p','u','l','a','t','o','r'} }; //Array of configuration descriptors const char* USB_config_dsc_ptr[1]; //Array of string descriptors const char* USB_string_dsc_ptr[3]; void USB_Init_Desc(){ USB_config_dsc_ptr[0] = &configDescriptor1; USB_string_dsc_ptr[0] = (const char*)&strd1; USB_string_dsc_ptr[1] = (const char*)&strd2; USB_string_dsc_ptr[2] = (const char*)&strd3; }