STM32 USB transmission (dual buffering)

Source: Internet
Author: User
Tags ack

Author: Yasin Lee

Transferred from: http://blog.csdn.net/coder_jack/article/details/7444798


The following content is downloaded from the network


The day before yesterday to test their own written USB driver found from the host to the STM32 out transfer (host to device) rate is only the highest 33kb/s, it is dizzy dead. After research found that the driver set the pipe maxtransfersize parameters, the original set 64 can 33kb/s, after reference to other USB device driver value, set to 65535, and then test the speed of USB out, reached the 500kb/s, Finally solved the bottleneck of the driver. But calculate the USB 2.0 full speed communication rate is 12mb/s, eliminate the CRC, tokens, SOF and so on how to spend more than the maximum 500kb/s ah. To see the internet, basically should be able to reach more than 600kb/s~700kb/s, I now the speed should still have a lot of promotion is. Look at the program and find
void Ep3_out_callback (void)//ep3 out callback function that is called when EP3 receives data
{
Count_out = Geteprxcount (ENDP3);//Get the received data length
Pmatouserbuffercopy (Buffer_out, endp3_rxaddr, count_out);//copy data from the buffer of USB EP3 Rx to the user-specified array
Seteprxvalid (ENDP3); After the copy is completed, the EP3 sends the ACK host to send the next packet.
}
Try to pmatouserbuffercopy this sentence out (so that STM32 do not process the received data) and then test the speed, surprised to find that the speed has reached the 997kb/s. In the evening, think carefully, the data must be used, the process of the data copy of the time consumption is always unavoidable; Because the USB device typically receives data bulk data, the nak-> copies the buffer data to the user area (user process) Send an ACK to notify the host to complete the receive can send the next host to send the next, follow the steps above USB receive step by step, as long as the STM32 does not complete data processing, the state has been a nak, the host will continue to send the packet, wasting bandwidth, So it will cause me to 500kb/s the maximum speed of the above-mentioned situation difficult to increase. Unwilling to heart AH ~ ~
Last night, we carefully studied the USB section of STM32 's Technical Reference manual, which mentions that bulk can be processed using a double-buffering mechanism (ping-pong), which solves the above situation. The principle of double buffering mechanism is to allocate 2 block receive buffer, STM32 user processing and USB interface can alternately occupy 2 buffers, when the USB endpoint receives data to write one of the buffers, the user's application can process another buffer, so that the buffer in turn to exchange the occupants, As long as the user handler completes processing within the time fragment received by the USB endpoint, it will not affect the USB communication speed at all.
Program Partial modification
First, the Ep3_out setting changes,
ZYP: Modify the EP3 to bulk double buffering mode-------------------------
Seteptype (ENDP3, Ep_bulk);
Setepdoublebuff (ENDP3);
Setepdblbuffaddr (ENDP3, endp3_buf0addr, endp3_buf1addr);
Setepdblbuffcount (ENDP3, Ep_dbuf_out, virtual_com_port_data_size);
Cleardtog_rx (ENDP3);
Cleardtog_tx (ENDP3);
Toggledtog_tx (ENDP3);
Seteprxstatus (ENDP3, ep_rx_valid);
Seteptxstatus (ENDP3, Ep_tx_dis);
//------------------------------------------------------
Second, the modification of Ep3_out callback function
void Ep3_out_callback (void)
{
ZYP: The following is a handler function modified to EP3 double-buffered out
if (Getendpoint (ENDP3) & EP_DTOG_TX)//first determine which buffer the data received is placed in
{
Freeuserbuffer (ENDP3, ep_dbuf_out); First release the user's possession of the buffer, so that the next receiving process of USB can be done immediately, while the user in parallel to the following processing
Count_out = Getepdblbuf0count (ENDP3);//Read the number of bytes received
Pmatouserbuffercopy (Buffer_out, endp3_buf0addr, count_out);
}
Else
{
Freeuserbuffer (ENDP3, ep_dbuf_out);
Count_out = Getepdblbuf1count (ENDP3);
Pmatouserbuffercopy (Buffer_out, endp3_buf1addr, count_out);
}
}

Above the Freeuserbuffer (ENDP3, ep_dbuf_out); The upper and lower position of this sentence is the key, if placed behind the function, there will still be a host waiting for STM32 processing data, the speed is still 500kb/s.
Put this sentence in front of the copy function, you really put the double buffering ping-pong mechanism used up. Roughly forget the next pmatouserbuffercopy (Buffer_out, endp3_buf1addr, count_out); When Count_out is the maximum value of 64 STM32 execution takes 302 cycles, In the case of 72MHZ, approximately 4.2 microseconds of execution time, while USB transmissions transmit 64 bytes of data at 12mb/s wire speed at least 40 microseconds, so as long as the pmatouserbuffercopy time does not exceed 40 microseconds, there is no case of buffer contention.

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.