int main(void) { clock_prescale_set(clock_div_1); SYSTEM_init(); TIMER_init(); USB_init(); /* initialize with 65 keys */ LAYOUT_init(65); LAYOUT_set((struct layout*)LAYOUT_BEGIN); LAYOUT_set_callback(&HID_set_scancode_state); MATRIX_init(5, rows, 14, cols, (const uint8_t*)matrix, &on_key_press); HID_init(); HID_commit_state(); LED_init(); SYSTEM_subscribe(USB_SOF, ANY, MAIN_handle_sof); int sleep_tmr = TIMER_add(32, true); SYSTEM_subscribe(TIMER, sleep_tmr, MAIN_sleep_timer_handler); SYSTEM_add_task(main_task, 0); SYSTEM_add_task(RAWHID_PROTOCOL_task, 0); SYSTEM_main_loop(); }
/* assumes single configuration, single endpoint, and interface configuration 0 */ BOOL HIDKProbe( BYTE addr, DWORD flags ) { BYTE tmpbyte; BYTE rcode; BYTE confvalue; WORD total_length; USB_DESCR* data_ptr = ( USB_DESCR * )&bigbuf; char* byte_ptr = bigbuf; rcode = XferGetConfDescr( addr, 0, CONF_DESCR_LEN, 0, bigbuf ); //get configuration descriptor if( rcode ) { //error handling return( FALSE ); } if( data_ptr->descr.config.wTotalLength > 256 ) { total_length = 256; } else { total_length = data_ptr->descr.config.wTotalLength; } rcode = XferGetConfDescr( addr, 0, total_length, 0, bigbuf ); //get the whole configuration if( rcode ) { //error handling return( FALSE ); } confvalue = data_ptr->descr.config.bConfigurationValue; //save configuration value to use later while( byte_ptr < bigbuf + total_length ) { //parse configuration if( data_ptr->descr.config.bDescriptorType != USB_DESCRIPTOR_INTERFACE ) { //skip to the next descriptor byte_ptr = byte_ptr + data_ptr->descr.config.bLength; data_ptr = ( USB_DESCR* )byte_ptr; }// if( data_ptr->descr.config.bDescriptorType != USB_DESCRIPTOR_INTERFACE else { //interface descriptor if( data_ptr->descr.interface.bInterfaceClass == HID_INTF && data_ptr->descr.interface.bInterfaceSubClass == BOOT_INTF_SUBCLASS && data_ptr->descr.interface.bInterfaceProtocol == HID_PROTOCOL_KEYBOARD ) { devtable[ addr ].devclass = HID_K; //fill device class tmpbyte = devtable[ addr ].epinfo->MaxPktSize; //save max.packet size HID_init(); //initialize data structures devtable[ addr ].epinfo = hid_ep; //switch endpoint information structure devtable[ addr ].epinfo[ 0 ].MaxPktSize = tmpbyte; //fill in max.packet size hid_device.interface = data_ptr->descr.interface.bInterfaceNumber; //fill in interface number to be used in HID requests hid_device.addr = addr; //fill in address byte_ptr = byte_ptr + data_ptr->descr.config.bLength; //skip to the next descriptor data_ptr = ( USB_DESCR* )byte_ptr; while( byte_ptr < bigbuf + total_length ) { if( data_ptr->descr.config.bDescriptorType != USB_DESCRIPTOR_ENDPOINT ) { //skip to endpoint descriptor byte_ptr = byte_ptr + data_ptr->descr.config.bLength; data_ptr = ( USB_DESCR* )byte_ptr; } else { /* fill endpoint information structure */ devtable[ addr ].epinfo[ 1 ].epAddr = data_ptr->descr.endpoint.bEndpointAddress; devtable[ addr ].epinfo[ 1 ].Attr = data_ptr->descr.endpoint.bmAttributes; devtable[ addr ].epinfo[ 1 ].MaxPktSize = data_ptr->descr.endpoint.wMaxPacketSize; devtable[ addr ].epinfo[ 1 ].Interval = data_ptr->descr.endpoint.bInterval; /* configure device */ rcode = XferSetConf( addr, 0, confvalue ); //set configuration if( rcode ) { //error handling return( FALSE ); } rcode = XferSetProto( addr, 0, hid_device.interface, BOOT_PROTOCOL ); if( rcode ) { //error handling return( FALSE ); } else { return( TRUE ); } } }//while( byte_ptr.... }//if (Class matches else { //if class don't match; stop processing after first interface. Not really correct return( FALSE ); } }//else if( data_ptr-> }// while( byte_ptr < &buf + total_length return( FALSE ); }