Удалось-таки заставить работать HID пример Keil. Теперь же трудности возникли при переделке его в HID-клавиатуру. В соответствии с
этим изменил следющие функции в файле проекта usbuser.c:
Код
void USB_Configure_Event (void) {
if (USB_Configuration) { /* Check if USB is configured */
memset (KeyReport, 0, sizeof(KeyReport));
send_string ("USB configuration\r\n");//Функция выдает строку "USB Configuration" на терминал
USB_WriteEP(0x81, KeyReport, sizeof(KeyReport));
}
}
Код
void USB_EndPoint3 (U32 event) {
GetInReport();
USB_WriteEP(0x81, KeyReport, sizeof(KeyReport));
}
В usbcfg.h отметил галочкой Endpoint3
В demo.c:
Код
U8 KeyReport[8];
void GetInReport (void) {
U8 i=0;
memset (KeyReport, 0, sizeof(KeyReport));
send_string ("USB Endpoint1\r\n");
if ((FIO0PIN & 0x20000000) == 0) { // Check if PBINT is pressed
KeyReport[2] = 0x01;
} else {
if ((FIO0PIN & 0x40000000) == 0) { // Check if PBINT is pressed
KeyReport[2] = 0x02;
} else {
if ((FIO0PIN & 0x00000002) == 0) { // Check if PBINT is pressed
KeyReport[2] = 0x03;
} else {
if ((FIO2PIN & 0x00000400) == 0) { // Check if PBINT is pressed
KeyReport[2] = 0x04;
} else {
KeyReport[2] = 0x00;
}
}
}
}
//вывод массива сообщения от "клавиатуры" на терминал
for (i=0; i<8; i++)
{
U0THR=(hex2ASCII(KeyReport[i]/16))%256;
while ( !(U0LSR & 0x20) ); // отправки символа
U0THR=(hex2ASCII(KeyReport[i]%16))%256;
while ( !(U0LSR & 0x20) ); // отправки символа
}
send_string("\r\n");
}
Файл usbdesc.c
Код
#include "type.h"
#include "usb.h"
#include "usbcfg.h"
#include "usbdesc.h"
#include "hid.h"
/*------------------------------------------------------------------------------
HID Report Descriptor
*------------------------------------------------------------------------------*/
/* Bit Input Output
0 INT0 LED0
1 --- LED1
2 --- LED2
3 --- LED3
4 --- LED4
5 --- LED5
6 --- LED6
7 --- LED7
*/
/* HID Report Descriptor */
const BYTE HID_ReportDescriptor[] = {
HID_UsagePage(HID_USAGE_PAGE_GENERIC),
HID_Usage(HID_USAGE_GENERIC_KEYBOARD),
HID_Collection(HID_Application),
HID_UsagePage(HID_USAGE_PAGE_KEYBOARD),
HID_UsageMin(224),
HID_UsageMax(231),
HID_LogicalMin(0),
HID_LogicalMax(1),
HID_ReportSize(1),
HID_ReportCount(8),
HID_Input(HID_Data | HID_Variable | HID_Absolute),
HID_ReportCount(1),
HID_ReportSize(8),
HID_Input(HID_Constant),
HID_ReportCount(5),
HID_ReportSize(1),
HID_UsagePage(HID_USAGE_PAGE_LED),
HID_UsageMin(1),
HID_UsageMax(5),
HID_Output(HID_Data | HID_Variable | HID_Absolute),
HID_ReportCount(1),
HID_ReportSize(3),
HID_Output(HID_Constant),
HID_ReportCount(6),
HID_ReportSize(8),
HID_LogicalMin(0),
HID_LogicalMax(101),
HID_UsagePage(HID_USAGE_PAGE_KEYBOARD),
HID_UsageMin(0),
HID_UsageMax(101),
HID_Output(HID_Data | HID_Array),
HID_EndCollection,
};
const U16 HID_ReportDescSize = sizeof(HID_ReportDescriptor);
/* USB Standard Device Descriptor */
const U8 USB_DeviceDescriptor[] = {
USB_DEVICE_DESC_SIZE, /* bLength */
USB_DEVICE_DESCRIPTOR_TYPE, /* bDescriptorType */
WBVAL(0x0200), /* 2.00 */ /* bcdUSB */
0x00, /* bDeviceClass */
0x00, /* bDeviceSubClass */
0x00, /* bDeviceProtocol */
USB_MAX_PACKET0, /* bMaxPacketSize0 */
WBVAL(0x04B4), /* idVendor */
WBVAL(0x2050), /* idProduct */
WBVAL(0x0001), /* 1.00 */ /* bcdDevice */
0x01, /* iManufacturer */
0x02, /* iProduct */
0x03, /* iSerialNumber */
0x01 /* bNumConfigurations: one possible configuration*/
};
/* USB Configuration Descriptor */
/* All Descriptors (Configuration, Interface, Endpoint, Class, Vendor) */
const U8 USB_ConfigDescriptor[] = {
/* Configuration 1 */
USB_CONFIGUARTION_DESC_SIZE, /* bLength */
USB_CONFIGURATION_DESCRIPTOR_TYPE, /* bDescriptorType */
WBVAL( /* wTotalLength */
USB_CONFIGUARTION_DESC_SIZE +
USB_INTERFACE_DESC_SIZE +
HID_DESC_SIZE +
USB_ENDPOINT_DESC_SIZE
),
0x01, /* bNumInterfaces */
0x01, /* bConfigurationValue: 0x01 is used to select this configuration */
0x00, /* iConfiguration: no string to describe this configuration */
USB_CONFIG_BUS_POWERED /*|*/ /* bmAttributes */
/*USB_CONFIG_REMOTE_WAKEUP*/,
USB_CONFIG_POWER_MA(100), /* bMaxPower, device power consumption is 100 mA */
/* Interface 0, Alternate Setting 0, HID Class */
USB_INTERFACE_DESC_SIZE, /* bLength */
USB_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType */
0x00, /* bInterfaceNumber */
0x00, /* bAlternateSetting */
0x01, /* bNumEndpoints */
USB_DEVICE_CLASS_HUMAN_INTERFACE, /* bInterfaceClass */
HID_SUBCLASS_BOOT, /* bInterfaceSubClass */
HID_PROTOCOL_KEYBOARD, /* bInterfaceProtocol */
0x04, /* iInterface */
/* HID Class Descriptor */
/* HID_DESC_OFFSET = 0x0012 */
HID_DESC_SIZE, /* bLength */
HID_HID_DESCRIPTOR_TYPE, /* bDescriptorType */
WBVAL(0x0100), /* 1.00 */ /* bcdHID */
0x00, /* bCountryCode */
0x01, /* bNumDescriptors */
HID_REPORT_DESCRIPTOR_TYPE, /* bDescriptorType */
WBVAL(HID_REPORT_DESC_SIZE), /* wDescriptorLength */
/* Endpoint, HID Interrupt In */
USB_ENDPOINT_DESC_SIZE, /* bLength */
USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType */
USB_ENDPOINT_IN(1), /* bEndpointAddress */
USB_ENDPOINT_TYPE_INTERRUPT, /* bmAttributes */
WBVAL(0x0004), /* wMaxPacketSize */
0x02, /* 32ms */ /* bInterval */
/* Terminator */
0 /* bLength */
};
/* USB String Descriptor (optional) */
const U8 USB_StringDescriptor[] = {
/* Index 0x00: LANGID Codes */
0x04, /* bLength */
USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */
WBVAL(0x0409), /* US English */ /* wLANGID */
/* Index 0x01: Manufacturer */
(13*2 + 2), /* bLength (13 Char + Type + lenght) */
USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */
'K',0,
'e',0,
'i',0,
'l',0,
' ',0,
'S',0,
'o',0,
'f',0,
't',0,
'w',0,
'a',0,
'r',0,
'e',0,
/* Index 0x02: Product */
(14*2 + 2), /* bLength (16 Char + Type + lenght) */
USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */
'S',0,
'K',0,
'-',0,
'L',0,
'P',0,
'C',0,
'2',0,
'3',0,
'7',0,
'8',0,
' ',0,
'H',0,
'I',0,
'D',0,
/* Index 0x03: Serial Number */
(12*2 + 2), /* bLength (12 Char + Type + lenght) */
USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */
'0',0, /* allowed characters are */
'0',0, /* 0x0030 - 0x0039 ('0'..'9') */
'0',0, /* 0x0041 - 0x0046 ('A'..'F') */
'1',0, /* length >= 26 */
'A',0,
'0',0,
'0',0,
'0',0,
'0',0,
'0',0,
'0',0,
'0',0,
/* Index 0x04: Interface 0, Alternate Setting 0 */
( 8*2 + 2), /* bLength (3 Char + Type + lenght) */
USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */
'K',0,
'e',0,
'y',0,
'b',0,
'o',0,
'a',0,
'r',0,
'd',0,
};
При запуске платы на терминал два раза выводится строка "USB Configuration", а дальше тишина. На нажатие кнопок нет никакой реакции. При повторной переделке исходного проекта USBHID заметил, что подобное поведение начинается после изменения строчки
Код
USB_WriteEP(0x81, &InReport, sizeof(InReport));
на
Код
USB_WriteEP(0x81, InReport, sizeof(InReport));
До этого изменения вызов функции GetInReport() нормально проходит. В чем может быть проблема?