This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to transmit BULK via USB HID?

How to realize the USB HID interface and PC data transmission?

I want to connect my computer via USB hid protocol for communication

This is my code, but can only send data, can not receive, send data will enter "APP_USBD_HID_USER_EVT_IN_REPORT_DONE" interrupt, but PC to nrf52840 send data, will not enter "APP_USBD_HID_USER_EVT_OUT_REPORT_READY" interrupt, but can pass nrf_usbd_epout_size_get(nrf_drv_usb_epout3);Gets the size of the data sent to the PC.

# define APP_USBD_HID_BULK_REPORT_DSC () {\
X06 0, 0 xa0, 0 XFF, / * usage page (FFA0h, vendor defined) * / \
0x09, 0x01, /* usage (vendor defined) */ \
0xA1, 0x01, /* collection (Application) */ \
0x09, 0x02, /* usage (vendor defined) */ \
0xA1, 0x00, /* set (Physical) */ \
0 x06, xa1 0, 0 XFF, / * usage page (vendor defined) * / \
/* input report */ \
0x09, 0x03, /* usage (vendor defined) */ \
0x09, 0x04, /* usage (vendor defined) */ \
0x15, 0x80, /* logical minimum value (0x80 or -128) */ \
0x25, 0x7F, /* logical maximum (0x7F or 127) */ \
0x35, 0x00, /* physical minimum value (0) */ \
0x45, 0xFF, /* physical maximum (255) */ \
0 x75, 0 x08, / * Report length Report size (8) * / \
0x95, 0x40, /* reports the value (64 fields) */ \
0 x81, 0 x02, / * input (data, variable, absolute) * / \
/* output report */ \
0x09, 0x05, /* usage (vendor defined) */ \
0x09, 0x06, /* usage (vendor defined) */ \
0x15, 0x80, /* logical minimum value (0x80 or -128) */ \
0x25, 0x7F, /* logical maximum (0x7F or 127) */ \
0x35, 0x00, /* physical minimum value (0) */ \
0x45, 0xFF, /* physical maximum (255) */ \
0x75, 0x08, /* report length (8 bits) */ \
0x95, 0x40, /* reports the value (64 fields) */ \
0 x91, 0 x02, / * output (data, variable, absolute) * / \
0xC0, /* end of set (Physical) */ \
0xC0, /* end of collection (Application) */ \
}

#define ENDPOINT_LIST2()                                      \
(                                                             \
        NRF_DRV_USBD_EPIN3,                                   \
		NRF_DRV_USBD_EPOUT3                                   \
)

APP_USBD_HID_GENERIC_SUBCLASS_REPORT_DESC(custom_bulk_desc,APP_USBD_HID_BULK_REPORT_DSC());

static const app_usbd_hid_subclass_desc_t * reps2[] = {&custom_bulk_desc};
APP_USBD_HID_GENERIC_GLOBAL_DEF(m_app_hid_bulk,
                                APP_USBD_INTERFACE_BULK,
                                hid_bulk_ev_handler,
                                ENDPOINT_LIST2(),
                                reps2,
                                0x40,
                                0x40,
                                APP_USBD_HID_SUBCLASS_BOOT,
                                APP_USBD_HID_PROTO_GENERIC);


static void hid_bulk_ev_handler(app_usbd_class_inst_t const * p_inst,
                                app_usbd_hid_user_event_t event)
{
    switch (event)
    {
        case APP_USBD_HID_USER_EVT_OUT_REPORT_READY:
        {
            NRF_LOG_INFO("hid_bulk_ev_handler : APP_USBD_HID_USER_EVT_OUT_REPORT_READY");
            break;
        }
        case APP_USBD_HID_USER_EVT_IN_REPORT_DONE:
        {
			NRF_LOG_INFO("hid_bulk_ev_handler : APP_USBD_HID_USER_EVT_IN_REPORT_DONE");
            break;
        }
        case APP_USBD_HID_USER_EVT_SET_BOOT_PROTO:
        {
            UNUSED_RETURN_VALUE(hid_generic_clear_buffer(p_inst));
            NRF_LOG_INFO("SET_BOOT_PROTO");
            break;
        }
        case APP_USBD_HID_USER_EVT_SET_REPORT_PROTO:
        {
            UNUSED_RETURN_VALUE(hid_generic_clear_buffer(p_inst));
            NRF_LOG_INFO("SET_REPORT_PROTO");
            break;
        }
        default:
            break;
    }
}
int main(void)
{
...
	app_usbd_class_inst_t const * class_inst_generic2;
	class_inst_generic2 = app_usbd_hid_generic_class_inst_get(&m_app_hid_bulk);
	ret = app_usbd_class_append(class_inst_generic2);
	APP_ERROR_CHECK(ret);
...
}

Parents Reply
  • I changed the code in app_usbd_hid_generic.c, and every time the PC sends a message, it goes into this function and gets the endpoint number, but the data is from the old data from the last time

    static ret_code_t hid_generic_ep_transfer_out(app_usbd_class_inst_t const * p_inst)
    {
    
        app_usbd_hid_generic_t const * p_generic = hid_generic_get(p_inst);
        nrf_drv_usbd_ep_t              ep_addr   = app_usbd_hid_epout_addr_get(p_inst);
    
        /*Request setup data*/
        app_usbd_hid_report_buffer_t const * p_rep_buff;
    
        p_rep_buff = app_usbd_hid_rep_buff_out_get(&p_generic->specific.inst.hid_inst);
        NRF_DRV_USBD_TRANSFER_OUT(transfer, p_rep_buff->p_buff, p_rep_buff->size);
    
    	NRF_LOG_INFO("hid_generic_ep_transfer_out,ep_addr = %d",ep_addr);
    	NRF_LOG_INFO("p_rep_buff->p_buff[0] = %d,[1] = %d,[2] = %d,[3] = %d,[4] = %d,[5] = %d",p_rep_buff->p_buff[0], p_rep_buff->p_buff[1], 
    																					p_rep_buff->p_buff[2], p_rep_buff->p_buff[3], 
    																					p_rep_buff->p_buff[4], p_rep_buff->p_buff[5]);
        return app_usbd_ep_transfer(ep_addr, &transfer);
    }

Children
No Data
Related