A summary of USB

Source: Internet
Author: User

There are 3 main types of USB that are now applied:

1, interrupt mode under the host computer sent to the embedded device command:

The relevant code of the embedded software processing data is as follows:

Usbd_custom_hid_itftypedef Usbd_customhid_fops_fs =
{
CUSTOM_HID_REPORTDESC_FS,
CUSTOM_HID_INIT_FS,
CUSTOM_HID_DEINIT_FS,
CUSTOM_HID_OUTEVENT_FS,
};

Static int8_t Custom_hid_outevent_fs (uint8_t* event_idx);

To solve the problem of data source, the code in the library can only take two-byte commands, you can modify the code to take multiple bytes, modified as follows:

Usbd_classtypedef Usbd_custom_hid =
{
Usbd_custom_hid_init,
Usbd_custom_hid_deinit,
Usbd_custom_hid_setup,
NULL,/*ep0_txsent*/
Usbd_custom_hid_ep0_rxready,/*ep0_rxready*//* STATUS STAGE in * *
Usbd_custom_hid_datain,/*datain*/
Usbd_custom_hid_dataout,
NULL,/*sof */
Null
Null
Usbd_custom_hid_getcfgdesc,
Usbd_custom_hid_getcfgdesc,
Usbd_custom_hid_getcfgdesc,
Usbd_custom_hid_getdevicequalifierdesc,
};

The red part is a function that accepts USB data

Static uint8_t Usbd_custom_hid_dataout (Usbd_handletypedef *pdev,
uint8_t epnum)
{

Usbd_custom_hid_handletypedef *hhid = (usbd_custom_hid_handletypedef*) pdev->pclassdata;

((Usbd_custom_hid_itftypedef *) pdev->puserdata)->outevent (Hhid->report_buf[0],
HHID->REPORT_BUF[1]);
((Usbd_custom_hid_itftypedef *) pdev->puserdata)->outevent (HHID->REPORT_BUF);

Usbd_ll_preparereceive (Pdev, custom_hid_epout_addr, Hhid->report_buf,
Usbd_customhid_outreport_buf_size);

return USBD_OK;
}

The corresponding host computer source code processing functions are as follows:

UCHAR out_data[5]={0x02,0x00};
int ret;
OUT_DATA[1] = 0x06;//red on
OUT_DATA[2] = 0x01;
OUT_DATA[3] = 0x01;
ret = Usb_interrupt_write (g_udev[0],0x01, (char *) out_data,5,500);

Call a function of the red font to send this type of command.

2, load the read data to the host computer

Static uint8_t Usbd_custom_hid_setup (Usbd_handletypedef *pdev,
Usbd_setupreqtypedef *req)

Below this function is the value to read

Case Custom_hid_req_get_report://virgo
PROCESSREQFROMPC (Hhid->send2pcreport_buf, (uint8_t) (req->wvalue) &0x00ff);
Usbd_ctlsenddata (Pdev,hhid->send2pcreport_buf, (uint8_t) (req->wlength));
Break

#define Custom_hid_req_get_report 0x01

Upper Machine Code

int ret;
Char in_data[64] = {0x00};
ret = usb_control_msg (g_udev[0],0xa1,0x01,0x0301,0x0000,in_data,64,1000);

0x01, corresponding to Custom_hid_req_get_report.

0x0301 for the command

3, in the host computer to write embedded data

Usbd_classtypedef Usbd_custom_hid =
{
Usbd_custom_hid_init,
Usbd_custom_hid_deinit,
Usbd_custom_hid_setup,
NULL,/*ep0_txsent*/
Usbd_custom_hid_ep0_rxready,/*ep0_rxready*//* STATUS STAGE in * *
Usbd_custom_hid_datain,/*datain*/
Usbd_custom_hid_dataout,
NULL,/*sof */
Null
Null
Usbd_custom_hid_getcfgdesc,
Usbd_custom_hid_getcfgdesc,
Usbd_custom_hid_getcfgdesc,
Usbd_custom_hid_getdevicequalifierdesc,
};

Case Custom_hid_req_set_report:
G_req = req;
hhid->isreportavailable = 1;
Usbd_ctlpreparerx (Pdev, Hhid->report_buf, (uint8_t) (req->wlength));

#define Custom_hid_req_set_report 0x09

The corresponding host computer

ret = usb_control_msg (g_udev[0],0x21,0x09,0x0302,0x0000,out_data,20,1000);

Data Usbcontrol Type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.