USB firmware Development Summary (2)

Source: Internet
Author: User

3.2Firmware onUSBProgramming ideas in the Device setup phase

Generally, the interface chip of the USB device causes some interruptions to notify programmers of specific events. For example, the ep0 (default control endpoint) setup package arrives, and the ep0 in or out transaction occurs. Control transmission is divided into three phases: establishment, data, and status. Therefore, for a transmission control, the device firmware must properly control its execution process and cannot be reversed. After receiving the information of the ep0 setup package, the firmware analyzes the specific content of the request. Here it is assumed that it is the read Descriptor and then sends the specific content of the descriptor to the host in the data phase, after sending the message, it enters the status stage. After the status phase ends, a control transmission is completed. It should be noted that, even in various stages, the host must wait for the transaction request to be sent before responding to the specific operation. That is, assume that the firmware analyzes the ep0 setup package to obtain the host's request to read a descriptor, and then the firmware should enter the data stage, but only the process logic enters, the specific operation must wait until the in token of the control of the host arrives before the real data transmission in the data phase starts, and then enters the status phase. Generally, you only need to set a register to notify the chip to start the state phase, without interfering with its details. The host initially identifies the device through the control pipeline. After a series of control transmission (the host recognizes the request of the device) is completed, the host can recognize the USB device, it will be displayed in the Device Manager (but it may not be completely normal to use the device, because there may be some protocols not completed, for example, the mass storage device still needs to correctly respond to the SCSI command, part 1 of this document will be detailed ). The following is an example of how the firmware processing controls transmission. Of course, this is not limited to the actual application.

 

The idea of this example is to use global variables to record the occurrence of the interruption in response to the interruption caused by USB, and then perform specific processing in the main loop.

 

/* Pseudo code of the USB Service Program */

Void usb_service (void)

{

/* Process the three phases of transmission control */

Switch (Ep. ep0.stage)

{

Case c_stage_ep0_setup:/* In the creation phase */

If (! Usb_setup ()/* if the request is supported */

Ep. ep0.stage = c_stage_ep0_data;/* data transfer stage */

Break;

Case c_stage_ep0_data:/* in the data phase */

If (Ep. ep0.status = c_status_ep0_in_nack)/* receives the in token */

{

Usb_writeep0fifo ();/* send data to the host through the control endpoint */

Ep. ep0.status = c_status_reset;/* the process is completed, so the status is reset */

Ep. ep0.stage = c_stage_ep0_status;/* Transfer status */

Enable ep0_in_nack interrupt again;/* the interrupt will be disabled in ISR */

}

Break;

Case c_stage_ep0_status:

Enable the ep0_status register;

Break;

Default:

Break;

}

}

 

/* USB interrupt service program pseudocode (Part )*/

Void usb_isr (void)

{

If (Setup package arrival)

{

Clear interruption;

Ep. ep0.stage = c_stage_ep0_setup;

Ep. ep0.status = c_status_ep0_setup_arrival;

}

Else if (ep0 in token arrives but the chip automatically replies to the Nak)

{

Clear interruption;

Disable the interrupt;

Ep. ep0.status = c_status_ep0_in_nack;

}

}

 

/* Pseudocode of the setup stage handler for USB control transmission */

Int usb_setup (void)

{

Obtain the request type and related data through the value of each register;

 

If (the request type is read descriptor)

{

Switch (descriptor value)

{

Case device descriptor value:

Point the pointer to the device descriptor buffer to send data globally;

Break;

......

Default: break;

}

}

Else if (other requests to be processed by the device)

{

Processing;

}

Else/* unsupported requests */

{

Send stall signals;

Return 1;/* Return Error */

}

 

Return 0;/* return correct */

}

 

/* Send data through the endpoint 0 */

Void usb_writeep0fifo (void)

{

Obtains a global pointer to send data;

Use a pointer to read the descriptor data and fill it with the FIFO of the endpoint 0;

The notification chip ep0 in data packet is ready;

}

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.