A USB-based communication method and device
By generating a unique number for each setup packet and associated with the control transmission, the USB peripheral performs number verification before transmitting the data packet, solving the problem of wrong packet transmission in USB communication and improving the reliability of USB control transmission.
Patent Information
- Application Number
- CN202180079881.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-02-27
- Publication Date
- 2025-06-17
- Estimated Expiration
- 2041-02-27
AI Technical Summary
In the existing USB communication protocol, the USB host initiates a new transmission when the previous control transmission is not completed, resulting in the USB peripheral being unable to recognize the correspondence between the setup packet, data packet and status packet, and error packet transmission occurs.
The USB peripheral generates a unique number for each received setup packet and associates it with the corresponding control transmission. During the data transmission phase, the USB peripheral only transmits the data packet after confirming that the corresponding number of the data packet is consistent with the latest received setup packet number.
Through number association and verification, we ensure the correct correspondence between the data packet and the setup packet, avoid incorrect packet transmission, and improve the reliability of USB control transmission.
Smart Images

Figure CN116601618B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technologies, and in particular, to a communication method and apparatus based on the Universal Serial Bus (USB). Background Art
[0002] The USB communication protocol adopts a master-slave structure to implement communication between a USB host and a USB peripheral (or referred to as a USB slave device, a USB peripheral device, a USB device, etc.). All transactions (data stream transmissions) on the USB bus are initiated actively by the USB host, while the USB peripheral passively receives and then processes various commands (requests) sent by the USB host. The USB communication protocol has four data transfer modes: control transfer, bulk transfer, interrupt transfer, and isochronous transfer. Among them, control transfer is used to transfer control, status, configuration, and other information between the USB host and the USB peripheral. A complete control transfer includes two to three phases: a setup phase, a data phase, and a status phase, where the data phase is optional. The control transfer is initiated by the USB host through a setup packet. After receiving the setup packet sent by the USB host, the USB peripheral considers that a new control transfer starts.
[0003] In the prior art, the USB host often initiates a new control transfer before the previous control transfer is completed. The USB peripheral cannot recognize the correspondence between the setup packet and the data packet / status packet in different control transfers, resulting in mispacket transmission. Summary of the Invention
[0004] Embodiments of this application provide a communication method and apparatus based on the USB, which are used to improve the reliability of USB control transfer.
[0005] In a first aspect, a communication method based on the USB is provided. This method can be applied to a USB peripheral, and the USB peripheral is communicatively connected to a USB host through a USB bus. The method includes: the USB peripheral receives setup packets from the USB host at different times, where any one of the setup packets is used to initiate a corresponding control transfer; the USB peripheral generates a unique number for each received setup packet, associates each setup packet with its corresponding number, and associates the number corresponding to each setup packet with the data packet to be transmitted in the control transfer initiated by each setup packet; after the data transfer phase of any control transfer starts, the USB peripheral transmits the data packet to be transmitted in any control transfer with the USB host only after determining that the number corresponding to the data packet to be transmitted in any control transfer is consistent with the number corresponding to the latest received setup packet by the USB peripheral.
[0006] In the embodiments of the present application, the USB peripheral generates a unique number for each received setup packet. When preparing for the transmission of the data packet, the data packet is attached with the number of the setup packet corresponding to the current pen control transmission, and a number verification is performed before the data packet is transmitted. Only after the number corresponding to the data packet is consistent with the number of the latest received setup packet, the USB peripheral starts to transmit the data packet. In this way, the corresponding relationship between the data packet and the setup packet can be ensured, the occurrence of mis-packet transmission can be avoided, and the reliability of the USB control transmission can be improved accordingly.
[0007] In a possible implementation manner, before associating each setup packet with its corresponding number, the USB peripheral also needs to allocate at least one event buffer space. Correspondingly, the USB peripheral associating each setup packet with its corresponding number includes: storing each setup packet and its corresponding number in the same event buffer space.
[0008] In this implementation manner, the USB peripheral stores the setup packet in the event buffer space, so that the buffer space for storing the setup packet (i.e., the event buffer space) and the buffer space for storing the data packet (i.e., the data buffer space) are independent of each other and do not interfere with each other. Accordingly, the packet loss phenomenon can be improved, and the reliability of the USB control transmission can be further enhanced.
[0009] In a possible implementation manner, the USB peripheral allocates multiple event buffer spaces; the USB peripheral can, according to the chronological order of receiving the setup packets, sequentially store multiple setup packets received at different times and their corresponding numbers in the multiple allocated event buffer spaces.
[0010] In this implementation manner, the USB peripheral allocates multiple event buffer spaces at one time, which can reduce the frequency of the USB peripheral allocating buffers, that is, reduce the software and hardware interaction of the USB peripheral, and improve the efficiency of the USB control transmission.
[0011] In a possible implementation manner, the USB peripheral storing each setup packet and its corresponding number in the same event buffer space may include: if there is an idle event buffer space in at least one event buffer space, storing each setup packet and its corresponding number in the idle event buffer space; if there is no idle event buffer space in at least one event buffer space, using N setup packets received at different times and their corresponding numbers to respectively replace the setup packets and the corresponding numbers in the N event buffer spaces with the oldest historical numbers stored in at least one event buffer space, where N is a positive integer.
[0012] In this embodiment, the event buffer space of the USB peripheral provides an overwrite function. When there is no free event buffer space, a new setup packet can be used to replace the old setup packet, so as to ensure that the USB peripheral can receive and save new setup packets at any time, and better avoid packet loss.
[0013] In a possible embodiment, if there is no free event buffer space in the at least one event buffer space, the specific ways for the USB peripheral to store each setup packet and its corresponding number in the same event buffer space include the following two:
[0014] Method 1: When there is no free event buffer space in the at least one event buffer space, the USB peripheral clears all the event buffer spaces in the at least one event buffer space; the USB peripheral stores the N setup packets received at different times and their corresponding numbers in the N cleared event buffer spaces in the order of the time of receiving the setup packets; where N is a positive integer. That is, the USB peripheral clears all the event buffer spaces at one time, and then uses the cleared event buffer spaces as newly allocated event buffer spaces.
[0015] Method 2: When there is no free event buffer space in the at least one event buffer space, after the USB peripheral receives the i-th setup packet among the N setup packets, it clears one event buffer space in the at least one event buffer space; stores the i-th setup packet and its corresponding number in the cleared one event buffer space, where i takes all positive integers from 1 to N; where N is a positive integer. That is, after the USB peripheral receives each new setup packet, it clears one historical setup packet.
[0016] It should be understood that, in Method 2, the one event buffer space can be any one of the at least one event buffer space. Optionally, before being cleared, the one event buffer space stores: the setup packet with the earliest reception time among the received setup packets, or the smallest number or the largest number among the numbers corresponding to the received setup packets.
[0017] It should be understood that the above two methods are only examples and not limitations.
[0018] In a possible implementation, after associating each setup packet with its corresponding number, and before associating the number corresponding to each setup packet with the data packet to be transmitted in the control transfer initiated by each setup packet, the USB peripheral also parses the content of each setup packet to obtain the size of the data packet to be transmitted in the control transfer initiated by each setup packet (optionally, the content and direction of the data packet to be transmitted in the control transfer initiated by each setup packet can also be obtained); and allocates data buffer space for the data packets to be transmitted in the control transfer initiated by each setup packet according to the size of the data packets to be transmitted.
[0019] It should be understood that if the data stage of any control transfer is an IN transfer; then after the USB peripheral allocates data buffer space for the data packets to be transmitted in the control transfer initiated by each setup packet according to the size, and before the start of the data stage of any control transfer, the USB peripheral can also prepare the data packet to be transmitted in the any control transfer according to the content and store the data packet to be transmitted into the data buffer space. In this way, the reliability of the USB control transfer can be improved.
[0020] In a possible implementation, the USB peripheral associating the number corresponding to each setup packet with the data packet to be transmitted in the control transfer initiated by each setup packet includes: for any setup packet, storing the number corresponding to the any setup packet and the data packet to be transmitted in the control transfer initiated by the any setup packet into the data buffer space allocated for the data packet to be transmitted in the control transfer initiated by the any setup packet.
[0021] The following separately introduces the specific implementation processes of the data stage of the control transfer in two scenarios of OUT transfer and IN transfer.
[0022] Case 1: The data stage of any control transfer is an OUT transfer (i.e., the USB host sends a data packet to the USB peripheral).
[0023] The USB peripheral receives the data packet from the USB host; after the USB peripheral determines that the number corresponding to the data packet to be transmitted in the any control transfer is consistent with the number corresponding to the latest setup packet received by the USB peripheral, it stores the received data packet into the data buffer space allocated for the data packet to be transmitted in the any control transfer.
[0024] Case 2: The data stage of any control transfer is an IN transfer (i.e., the USB peripheral sends a data packet to the USB host);
[0025] After the start of the data stage of any control transfer, the USB peripheral receives a data packet request from the USB host, which is used to request the USB peripheral to send a data packet; after determining that the number corresponding to the data packet to be transferred in any control transfer is consistent with the number corresponding to the setup packet that the USB peripheral has most recently received, the USB peripheral reads the data packet from the data buffer space allocated for the data packet to be transferred in any control transfer according to the data packet request and sends it to the USB host.
[0026] In a possible implementation, after the USB peripheral associates the number corresponding to each setup packet with the data packet to be transferred in the control transfer initiated by each setup packet, the USB peripheral can also associate the number corresponding to each setup packet with the status packet in the control transfer initiated by each setup packet. Further, after the end of the data stage of any control transfer, after the USB peripheral receives a status packet request from the USB host (the status packet request is used to request the USB peripheral to send a status packet), the USB peripheral sends the status packet to be transferred in any control transfer to the USB host only after determining that the number corresponding to the status packet in any control transfer is consistent with the number corresponding to the setup packet that the USB peripheral has most recently received.
[0027] In this implementation, the USB peripheral also performs number verification before transmitting the status packet, and only starts transmitting the status packet after the number corresponding to the status packet is consistent with the number of the most recently received setup packet. In this way, the corresponding relationship between the status packet and the setup packet can be ensured, and the occurrence of mispacket transmission can be avoided, further improving the reliability of USB control transfer.
[0028] In a second aspect, a USB-based communication device is provided. The device can be applied to a USB peripheral, and the USB peripheral and the USB host are communicatively connected through a USB bus; the device includes a module / unit for executing the method described in the first aspect or any possible implementation of the first aspect.
[0029] Exemplarily, the device may include: a USB interface module for receiving setup packets from the USB host at different times, where any one of the setup packets is used to initiate a corresponding control transfer; an interface control module for generating a unique number for each received setup packet and associating each setup packet with its corresponding number; a main control module for associating the number corresponding to each setup packet with the data packet to be transferred in the control transfer initiated by each setup packet; the interface control module may also be used to control the USB interface module to transfer the data packet to be transferred in any control transfer with the USB host after determining that the number corresponding to the data packet to be transferred in any control transfer is consistent with the number corresponding to the setup packet that the USB peripheral has most recently received after the start of the data transfer stage of any control transfer.
[0030] In a third aspect, there is provided a computer-readable storage medium storing computer program instructions which, when executed, cause the method described in the first aspect or any possible implementation manner of the first aspect to be implemented.
[0031] In a fourth aspect, there is provided a chip which can be coupled to a memory. The chip is configured to read and execute program instructions stored in the memory to implement the method described in the first aspect or any possible implementation manner of the first aspect.
[0032] In a fifth aspect, there is provided a computer program product storing instructions which, when run on a computer, cause the computer to execute the method described in the first aspect or any possible implementation manner of the first aspect.
[0033] For the technical effects that can be achieved by any one of the second to fifth aspects and any possible design in any one of these aspects, please refer to the description of the technical effects that can be achieved by the first aspect and its corresponding possible designs, which will not be repeated here. Description of the Drawings
[0034] Figure 1 FIG. is a schematic diagram of a USB communication scenario applicable to the embodiments of the present application;
[0035] Figures 2A to 2D FIG. is a schematic diagram of the structure of a USB peripheral 4 to which the solution provided by the embodiments of the present application can be applied;
[0036] Figure 3 FIG. is a flowchart of a method based on USB communication provided by the embodiments of the present application;
[0037] Figure 4 FIG. is a flowchart of another method based on USB communication provided by the embodiments of the present application;
[0038] Figure 5 FIG. is a flowchart of another method based on USB communication provided by the embodiments of the present application;
[0039] Figure 6 FIG. is a flowchart of another method based on USB communication provided by the embodiments of the present application. Detailed Embodiments
[0040] USB technology is usually applied to data transmission in various electronic devices. For example, an electronic device can be charged by connecting to a power source through USB technology, and data can also be transmitted between one electronic device and another through USB technology, such as for video or picture transmission and sharing.
[0041] The technical solutions provided by the embodiments of this application can be applied to various USB communication scenarios, such as Figure 1 As shown, a USB host and a USB peripheral can be connected through a USB bus to achieve USB communication. In some possible embodiments, the USB host and the USB peripheral can be directly connected by a cable. In some other possible embodiments, other devices can also be provided between the USB host and the USB peripheral, which is not limited here. For example, a USB hub can also be provided. The USB host and the USB peripheral are respectively connected to the USB hub through cables, and the USB host, the USB peripheral, and the USB hub are all connected to the USB bus.
[0042] Generally, the USB communication protocol supports 4 data transfer modes between a USB host and a USB peripheral, namely: control transfer, bulk transfer, interrupt transfer, and isochronous transfer. Among them, control transfer is used to transfer control, status, configuration, and other information between the USB host and the USB peripheral. Generally, a complete control transfer includes 2 to 3 phases: setup phase, data phase, and status phase. Among them, the data phase is optional. The control transfer is initiated by the USB host by sending a setup packet to the USB peripheral. After the USB peripheral receives the setup packet sent by the USB host, it considers that a new control transfer starts.
[0043] Furthermore, the control transfers on the USB host and the USB peripheral can be divided according to the transfer direction of the data packets in the data phase, and can include two types: control OUT transfer and control IN transfer. Among them, control OUT transfer means that the USB host sends a data packet to the USB peripheral; control IN transfer means that the USB peripheral sends a data packet to the USB host.
[0044] In the current solution, when a control transfer is performed between a USB host and a USB peripheral, each control transfer requires the USB peripheral to allocate a cache space in advance in order to receive the setup packet in that control transfer. This leads to cumbersome software-hardware interaction of the USB peripheral and low efficiency of USB control transfer. In addition, since the storage locations of the setup packet and the data packet on the USB peripheral are both in the data cache space, there is a competition relationship between the cache of the setup packet and the cache of the data packet during cache allocation by the USB peripheral, and the USB peripheral cannot read / write the setup packet and the data packet simultaneously. All these reasons may cause the cache space to be allocated untimely, which in turn leads to packet loss. For example, after the USB host sends a setup packet to the USB peripheral, if the USB peripheral has not allocated the data cache space for storing the setup packet, it cannot store the setup packet, and thus the setup packet is discarded. Moreover, since the USB peripheral fails to successfully store the setup packet, it will also cause the USB peripheral to be unable to allocate the data cache space for the data packet to be transmitted in the data stage in advance. When the data stage starts, the USB peripheral naturally cannot successfully transmit the data packet, ultimately resulting in the failure of this control transfer.
[0045] In another case, the USB host initiates a new control transfer before the previous control transfer is completed. In this situation, there will be setup packets, data packets, or status packets from different control transfers on the USB peripheral, and the USB peripheral cannot determine the correspondence between the setup packets and the data / status packets in different control transfers. Then, it will wrongly reply the previous data packet to the USB host, resulting in the occurrence of mispackets.
[0046] To solve one or more of the above problems, the embodiments of the present application provide a communication solution based on USB. In the embodiments of the present application, after each setup packet is received by the USB peripheral, the USB peripheral can generate a unique number for the setup packet (or for the control transfer initiated by the setup packet), and associate the number with the setup packet. When the USB peripheral prepares the transmission of the data packet / status packet, it needs to attach (or associate) the number of the setup packet corresponding to the current control transfer (i.e., the number of the setup packet corresponding to the data packet / status packet) to the data packet / status packet, and perform number verification before transmission. Only after the number attached to the data packet / status packet is consistent with the number of the latest received setup packet, will the USB peripheral start to transmit the data / status packet. In this way, mispacket transmission can be avoided, and the reliability of USB control transfer can be improved.
[0047] In the embodiment of the present application, the USB peripheral can also store the setup packet in the event buffer space and store the data packet in the data buffer space, so that the buffer spaces used by the setup packet and the data packet are independent of each other and do not interfere with each other. In this way, the packet loss phenomenon can be improved and the reliability of the USB control transfer can be enhanced.
[0048] In the embodiment of the present application, the USB peripheral can also allocate in advance multiple buffer spaces for storing setup packets, and store the setup packets from the USB host in sequence according to the time order of receiving the setup packets. In this way, the frequency of the USB peripheral allocating buffers can be reduced, that is, the software and hardware interaction of the USB peripheral can be reduced, and the efficiency of the USB control transfer can be improved.
[0049] In the embodiment of the present application, the USB peripheral can also provide a buffer overwrite function. When the USB peripheral receives a setup packet, if the USB peripheral has not yet allocated a buffer space for storing the setup packet, and there is a buffer space in the USB peripheral that stores a historical setup packet (the "historical setup packet" refers to the setup packet received previously), then the setup packet can be used to replace the historical setup packet. In this way, even if there is no free buffer space, the USB peripheral can receive and save a new setup packet at any time, avoiding the occurrence of the packet loss phenomenon.
[0050] Next, the technical solutions provided by the embodiments of the present application will be described in detail with reference to the accompanying drawings.
[0051] See Figure 2A , which is a schematic structural diagram of a USB peripheral 4 applicable to the solution provided by the embodiment of the present application, including an interface control module 41, a main control module 42, a buffer module 43, and a USB interface module 44.
[0052] Among them, the USB interface module 44 is the physical interface of the USB peripheral 4 (i.e., the USB socket), and can insert a USB data cable. The data from the USB host reaches the USB peripheral 4 in sequence through the USB socket of the USB host, the USB data cable, and the USB interface module 44. Correspondingly, the data sent by the USB peripheral 4 to the USB host finally reaches the USB host in sequence through the USB interface module 44, the USB data cable, and the USB socket of the USB host. The USB interface module 44 can be an interface that conforms to the USB standard specification, specifically, it can be a Mini USB interface, a Micro USB interface, a USB Type C interface, etc. The USB interface module 44 can be used to connect a charger to charge the electronic device, and can also be used for data transmission between the electronic device and other peripheral devices (such as a USB host). It can also be used to connect a headset to play audio through the headset.
[0053] The interface control module 41 is connected to the USB interface module 44 and the USB host control module 42. The interface control module 41 is responsible for the data transmission control on the USB interface module 44. For example, it reads the data packet (data packet) to be sent to the USB host from the buffer module 43 and sends it through the USB interface 44, and stores the data packet received by the USB interface 44 from the USB host into the buffer module 43. In the embodiment of the present application, in addition to reading and writing data in the buffer module 43, the USB interface control module 41 also adds other functions, such as generating a corresponding number for each control transfer, reporting events to the host control module 42, and performing number verification before transmitting the data packet and / or status packet, etc.
[0054] The buffer module 43 has a buffer space, which can specifically be a computer-readable and writable storage medium, and is used to store the data received by the USB peripheral 4 from the USB host and the data to be sent. In the embodiment of the present application, the buffer space is divided into two types: an event buffer space and a data buffer space. Among them, the event buffer space can be used to store the setup packet in the establishment stage, and the data buffer space can be used to store the data packet in the data stage.
[0055] Among them, the data buffer space is a buffer space for storing data, such as storing the data packet in the data stage, the status packet in the status stage, etc. The event buffer space is a buffer space for storing events, such as the event of receiving the setup packet, the event of data transmission completion, etc. It should be noted that in the embodiment of the present application, the setup packet belonging to the data is stored in the event buffer space, which is different from the prior art that stores the setup packet in the data buffer space. In this way, the interface control module 41 can access the data buffer space and the event buffer space at the same time, that is, the reading and writing of the setup packet and the reading and writing of the data packet are independent of each other, and the allocation of their buffer spaces is also independent of each other.
[0056] The host control module 42 is the main control center of the USB peripheral 4. The host control module 42 can perform one or more of the following functions: controlling the start / end of the data stage and the status stage, allocating the event buffer space, allocating the data buffer space, etc. The host control module 42 can also be integrated into the interface control module 41. For example, if the interface control module 41 is a USB controller and there is a separate MCU or MPU in the USB controller, the host control module 41 can be implemented through the MCU or MPU in the USB controller.
[0057] The main control module 42 can integrate driver software that supports the USB protocol. Optionally, customer software (or upper-layer application) is also integrated on the main control module 42. The driver software is a special program that enables a computer and hardware to communicate with each other. It is equivalent to the interface of the hardware. The customer software controls the operation of the hardware through this interface. In other words, the customer software is the upper-layer software of the driver software. The customer software cannot directly interact with the USB peripheral 4 or other hardware. It needs to pass the instructions to the driver software, and then the driver software calls the hardware in the USB peripheral 4 to execute the corresponding functions. Correspondingly, the signals reported by the hardware in the USB peripheral 4 also need to be packaged and sent to the driver software first, and then passed to the customer software by the driver software. The various functions executed by the main control module 42 can be achieved by running the driver software and the customer software.
[0058] According to the functions performed by each module, each module can be further subdivided. For example Figure 2B As shown, the cache module 43 includes an event cache space 431, a data cache space 432, etc.; the interface control module 41 includes a number generation sub-module 411, an event reporting sub-module 412, a number verification sub-module 413, etc. The number generation sub-module 411 can be used to generate a corresponding number for each control transfer. The event reporting sub-module 412 can be used to report events to the main control module 42 (for example, notify the main control module 42 of the event of receiving a setup packet and / or the event of generating a number). The number verification sub-module 413 can be used to perform number verification in the data stage and the status stage. Of course, in practical applications, there can be other partitioning methods, which are not limited in this application.
[0059] In a specific implementation, the main control module 42 can specifically be, but is not limited to: a Central Processing Unit (CPU), a Micro Controller Unit (MCU), a Micro Processor Unit (MPU), a Digital Signal Processing / Processor (DSP), a Field Programmable Gate Array (FPGA), etc. The cache module 43 can be, but is not limited to: registers, First Input First Output (FIFO) memories, Static Random Access Memories (SRAM), Dynamic Random Access Memories (DRAM), etc. The interface control module 41 can specifically be, but is not limited to: a logic circuit connected to the cache module 43 and the main control module 42.
[0060] It should be noted that the interface control module 41, the main control module 42, and the cache module 43 can be integrated in the same chip or in different chips, and the present application does not make any limitations. For example, Figure 2C as shown, the interface control module 41, the main control module 42, and the cache module 43 are all integrated in the USB controller, and the specific implementation of the cache module 43 can be a register, a FIFO memory, or an SRAM integrated in the USB controller, etc. ( Figure 2C the USB interface module 44 is not shown in the figure). Or for another example, Figure 2D as shown, the interface control module 41 and the main control module 42 are integrated in the USB controller, and the cache module 43 is an SRAM or a DRAM and is a part of the data memory external to the USB controller ( Figure 2D the USB interface module 44 is not shown in the figure). Of course, the interface control module 41 and the main control module 42 can also be separately arranged. For example, the interface control module 41 is integrated in the USB controller, and the main control module 42 is integrated in the CPU.
[0061] It should be noted that in actual applications, the USB peripheral 4 may also include more modules than Figures 2A to 2D shown in the figure. For example, it may also include one or more hubs, which are not shown one by one in the present application.
[0062] In the embodiments of the present application, the USB peripheral 4 may specifically but not limitedly be a USB flash drive, a speaker, a mouse, a keyboard, a computer, a mobile phone, a smart wearable device, a vehicle-mounted device, etc.
[0063] Refer to Figure 3 , which is a method based on USB communication provided by the embodiments of the present application. Taking the method as an example applied to the USB peripheral in the above Figure 2A , the method includes:
[0064] S600. The main control module 42 allocates at least two event cache spaces and notifies the interface control module 41 of the starting address of the allocated event cache spaces;
[0065] Figure 3 Taking the allocation of two event cache spaces as an example, in actual applications, there may be more, such as three, four, five, etc., and the present application does not make any limitations.
[0066] When the interface control module 41 stores the setup packet later, it can start from this starting address and store the setup packet sequentially backward.
[0067] S601. The USB interface module 44 receives a first setup packet from the USB host, and the first setup packet is used to indicate the start of the first control transfer; the interface control module 41 stores the first setup packet in the first event cache space;
[0068] It should be noted that due to the limited space of the attached drawings, for the convenience of clearly showing the attached drawings, Figure 3 the USB interface module 44 is not shown in the figure. However, there is actually a USB interface module 44 between the interface control module 41 and the USB host. The USB peripheral finally sends data to an external device (such as a USB host) and receives data sent by the external device through the USB interface module 44.
[0069] S602. The interface control module 41 reports an event to notify the main control module 42;
[0070] Specifically, the specific way for the interface control module 41 to report an event can be by generating a hardware interrupt, for example, generating a setup interrupt signal.
[0071] S603. After receiving the notification, the main control module 42 reads the content of the last (or the latest) setup packet from the first event buffer space, for example, reads and parses the content of the first setup packet to obtain the size, content, and direction of the data packet to be transmitted;
[0072] S604. The main control module 42 allocates a first data buffer space according to the size of the data packet to be transmitted, and notifies the interface control module 41 of the starting address of the first data buffer space;
[0073] It should be understood that if it is a control IN transfer, the main control module 42 also prepares the data packet according to the content of the data packet to be transmitted, and stores the data packet in the first data buffer space. The storage method can be to store it sequentially from the starting address backwards.
[0074] S605. Data packet transmission;
[0075] If it is a control OUT transfer, the USB interface module 44 receives the data packet from the USB host; the interface control module 41 stores the data packet received by the USB interface module 44 in the first data buffer space; if it is a control IN transfer, the USB interface module 44 receives a data packet request from the USB host, and the interface control module 41 takes out the data packet from the first data buffer space and sends it to the USB host through the USB interface module 44.
[0076] S606. Status packet transmission;
[0077] The main control module 42 issues a status packet to the interface control module 41; after the USB interface module 44 receives a status packet request from the USB host, the interface control module 41 returns a status packet to the USB host through the USB interface module 44.
[0078] There are various ways for the main control module 42 to send a status packet to the interface control module 41, which is not restricted in this application. For example, directly passing the status packet to the interface control module 41; or, for example, storing the status packet in the first data cache space, and then the interface control module 41 reads the status packet from the first data cache space; or, for example, storing the status packet in other data cache spaces, and then the interface control module 41 reads the status packet from other data cache spaces.
[0079] Thus, the first control transfer is completed.
[0080] S601’: The USB interface module 44 receives a second setup packet from the USB host, and the second setup packet is used to indicate the start of the second control transfer; the interface control module 41 stores the second setup packet in the second event cache space;
[0081] S602’: The interface control module 41 reports an event to notify the main control module 42;
[0082] S603’: After receiving the notification, the main control module 42 reads and parses the content of the last (or the latest) setup packet from the second event cache space. For example, reads and parses the content of the second setup packet to obtain the size, content, and direction of the data packet to be transferred;
[0083] S604’: The main control module 42 allocates a second data cache space according to the size of the data packet to be transferred, and notifies the interface control module 41 of the starting address of the second data cache space;
[0084] Similarly, if it is a control IN transfer, the main control module 42 also prepares the data packet according to the content of the data packet to be transferred, and stores the data packet in the second data cache space.
[0085] S605’: Data packet transfer;
[0086] If it is a control OUT transfer, the USB interface module 44 receives the data packet from the USB host, and the interface control module 41 stores the data packet received by the USB interface module 44 in the second data cache space; if it is a control IN transfer, the USB interface module 44 receives a data packet request from the USB host, and the interface control module 41 takes out the data packet from the second data cache space and sends it to the USB host through the USB interface module 44.
[0087] S606’: Status packet transfer;
[0088] The main control module 42 sends a status packet to the interface control module 41; after the USB interface control module 41 receives a status packet request from the USB host in the USB interface module 44, the USB interface control module 41 returns a status packet to the USB host.
[0089] So far, the second control transfer is completed.
[0090] It should be understood that Figure 3 taking the example of allocating two event buffer spaces in advance, so Figure 3 only the process of two control transfers is shown. In practical applications, the USB peripheral can allocate more event buffer spaces in advance. Therefore, after the second control transfer is completed, the processes similar to S601 - S606 or S601' - S606' above can continue to be executed, which are not shown one by one here.
[0091] From the above, it can be seen that the USB peripheral in the embodiment of the present application can allocate multiple event buffer spaces at one time to store the setup packets of multiple control transfers, which can avoid the technical defect that each control transfer requires the USB peripheral to allocate a buffer space in advance, reduce the software - hardware interaction of the USB peripheral, and improve the efficiency of USB control transfer. Moreover, the USB peripheral uses the event buffer space to store the Setup packet and the data buffer space to store the Data packet, and the event buffer space and the data buffer space are independent of each other and do not interfere with each other. Therefore, it can also improve the packet loss phenomenon caused by untimely allocation of the data buffer space and further improve the reliability of USB control transfer.
[0092] It should be understood that in the above Figure 3 shown embodiment, the buffer space for storing the setup packet takes the event buffer space as an example. In specific implementation, it can also be replaced with the data buffer space, that is, multiple buffer spaces are allocated for the setup packet in the data buffer space, which can also achieve the effect of reducing the software - hardware interaction of the USB peripheral and improving the efficiency of USB control transfer.
[0093] Refer to Figure 4 , another method based on USB communication provided by the embodiment of the present application, taking the method as an example applied to the USB peripheral in the above Figure 2A , the method includes:
[0094] S700. The main control module 42 allocates one or more event buffer spaces and notifies the interface control module 41 of the starting address of the one or more event buffer spaces;
[0095] S701, the USB interface module 44 receives a setup packet from the USB host, and the setup packet is used to indicate the start of a control transfer; the interface control module 41 determines whether there is free event cache space in the event cache space. If so, the setup packet received by the USB interface module 44 in S701 is stored in the free event cache space; otherwise, the setup packet received by the USB interface module 44 in S701 is used to replace the setup packet in the non-free event cache space (i.e., the historical setup packet).
[0096] It should be noted that due to the length limitation of the drawings, in order to clearly show the drawings, Figure 4 The USB interface module 44 is not shown in the figure, but there is actually a USB interface module between the interface control module 41 and the USB host. The USB peripheral device ultimately sends data to an external device (such as a USB host) and receives data sent by the external device through the USB interface module 44.
[0097] The following describes the method for storing the setup package in the USB interface module 44 for two cases where the main control module 42 allocates one event buffer space and multiple event buffer spaces.
[0098] In case 1, the main control module 42 only allocates one event buffer space, and the interface control module 41 directly stores the first received setup packet in the event buffer space; starting from the second received setup packet, the newly received setup packet is used to replace the previous setup packet in the event buffer space, so as to ensure that the latest received setup packet is not lost.
[0099] For example, after the first control transfer starts, the USB interface module 44 receives the first setup packet, and the interface control module 41 stores the first setup packet in the event buffer space; after the second control transfer starts, the USB interface module 44 receives the second setup packet, but since the event buffer space has already stored the first setup packet in the first control transfer, the interface control module 41 uses the second setup packet to replace the first setup packet; after the third control transfer starts, the USB interface module 44 receives the third setup packet, but since the event buffer space has already stored the second setup packet in the second control transfer, the interface control module 41 uses the third setup packet to replace the second setup packet…
[0100] Case 2: the main control module 42 allocates multiple event buffer spaces. When storing the setup package, the interface control module 41 starts from the first event buffer space of the multiple event buffer spaces and stores the setup packages in sequence.
[0101] Taking the main control module 42 as an example to allocate three event buffer spaces (i.e., the first event buffer space, the second event buffer space, and the third event buffer space): After the start of the first control transfer, the USB interface module 44 receives the first setup packet, and the interface control module 41 stores the first setup packet in the first event buffer space; after the start of the second control transfer, the USB interface module 44 receives the second setup packet, and the interface control module 41 stores the second setup packet in the second event buffer space; after the start of the third control transfer, the USB interface module 44 receives the third setup packet, and the interface control module 41 stores the third setup packet in the third event buffer space.
[0102] Furthermore, when the last event buffer space of the multiple event buffer spaces is exhausted, a new setup packet can be used to replace the old setup packet to ensure that the new setup packet is not lost.
[0103] For example, continuing with the example of the above three event buffer spaces, after the start of the fourth control transfer, the USB interface module 44 receives the fourth setup packet. At this time, all event buffer spaces (i.e., the first event buffer space, the second event buffer space, and the third event buffer space) have stored setup packets, so the interface control module 41 can use the new setup packet (i.e., the fourth setup packet) to replace the old setup packet.
[0104] This application does not limit the specific manner in which the interface control module 41 uses a new setup packet to replace the old setup packet. The following are two possible ways:
[0105] Way 1: "Replace immediately when used".
[0106] After the USB interface module 44 receives a new setup packet, if the interface control module 41 finds that the event buffer space is exhausted, the interface control module 41 clears one of the multiple event buffer spaces and stores the new setup packet and the corresponding number in the cleared event buffer space. Optionally, the setup packet stored in the one event buffer space before clearing is: the setup packet with the earliest or latest reception time among the received (or stored) setup packets, or the smallest or largest number among the numbers corresponding to the received (or stored) setup packets. It should be understood that if the numbers are numbered in ascending order, the setup packet with the earliest reception time is the setup packet with the smallest number, and vice versa, if the numbers are numbered in descending order, the setup packet with the earliest reception time is the setup packet with the largest number.
[0107] Taking the fourth setup packet as an example, and taking the case where the earliest received setup packet among the received setup packets is stored in an event buffer space before it is emptied, the fourth setup packet can be used to replace the previously stored first setup packet in the first event buffer space.
[0108] Method 2: "Empty all at once".
[0109] After the USB interface module 44 receives a new setup packet, if the interface control module 41 finds that the event buffer space is exhausted, the interface control module 41 empties all event buffer spaces at once, and then starts a new poll, and stores new setup packets and corresponding numbers sequentially backward.
[0110] Taking the fourth setup packet as an example, the interface control module 41 empties all event buffer spaces (i.e., the first event buffer space, the second event buffer space, and the third event buffer space), stores the fourth setup packet in the first event buffer space, and then if there are new setup packets, they are stored in the second event buffer space in order.
[0111] S702: The interface control module 41 reports an event to notify the main control module 42;
[0112] S703: After receiving the notification, the main control module 42 reads and parses the content of the last (or the latest) setup packet from the event buffer space. For example, it is the content of the first setup packet, and obtains the size, content, and direction of the data packet to be transmitted;
[0113] S704: The main control module 42 allocates data buffer space according to the size of the data packet to be transmitted, and notifies the interface control module 41 of the starting address of the data buffer space;
[0114] S705: Transmit the data packet;
[0115] S706: Transmit the status packet.
[0116] The specific implementation process of S702 - S706 can refer to S602 - S606 or S602' - S606' above, and will not be elaborated here.
[0117] As described above, in the embodiments of the present application, the USB peripheral cache provides an overwrite function. When the USB peripheral receives a setup packet, if the USB peripheral has not yet allocated an event cache space for storing the setup packet, it can clear the event cache space allocated during the previous control transfer to store the new setup packet. In this way, even if there is no free cache space, the USB peripheral can receive and save new setup packets at any time, thereby avoiding packet loss. Moreover, the USB peripheral uses the event cache space to store the Setup packet and the data cache space to store the Data packet, and the event cache space and the data cache space are independent of each other and do not interfere with each other. Therefore, it can also avoid packet loss caused by untimely allocation of the data cache space, further improving the reliability of USB control transfer.
[0118] It should be understood that in the above Figure 4 illustrated embodiment, the cache space for storing the setup packet takes the event cache space as an example. In specific implementation, it can also be replaced with the data cache space, that is, when the data cache space for storing the setup packet is exhausted, the old setup packet is replaced with the new setup packet in the data cache space, and the effect of the USB peripheral receiving and saving new setup packets at any time and avoiding packet loss can also be achieved.
[0119] See Figure 5 , another method based on USB communication provided by the embodiments of the present application, taking the method as an example applied to the USB peripheral in the above Figure 2A includes:
[0120] S801. The main control module 42 allocates at least one event cache space;
[0121] As Figure 5 shown, each event cache space includes a first subspace S431A for storing the setup packet and a second subspace S431B for storing the number of the setup packet. It should be understood that Figure 5 only one event cache space is shown, and there can actually be more.
[0122] S802. The USB interface module 44 receives a first setup packet from the USB host. The first setup packet is used to indicate that the USB host and the USB peripheral start a control transfer. The interface control module 41 generates a unique number for the first setup packet, such as the first number, and then associates the first number with the first setup packet.
[0123] It should be noted that due to the limited space of the attached drawings, for the convenience of clearly showing the attached drawings, Figure 5The USB interface module 44 is not shown, but there is a USB interface module between the actual interface control module 41 and the USB host. The USB peripheral finally sends data to an external device (such as a USB host) and receives data sent by the external device through the USB interface module 44.
[0124] Specifically, the interface control module 41 associates the first number with the first setup packet. It can be that the interface control module 41 stores the first number and the first setup packet in the same event buffer space. For example, the first setup packet is stored in the first subspace of the first event buffer space, and the first number is stored in the second subspace of the first event buffer space.
[0125] In a possible design, the main control module 42 only allocates one event buffer space. In this case, the event buffer space can provide an overwrite function, that is, the newly received setup packet and its number can overwrite the historical setup packet (that is, the previously received setup packet) and its number. The specific implementation can refer to the relevant introduction of Case 1 in S701 above, and will not be elaborated here.
[0126] In another possible design, the main control module 42 allocates multiple event buffer spaces. Then, the interface control module 41 can store multiple setup packets received in multiple control transfers into the multiple event buffer spaces in the order of the time when the setup packets are received. In this case, when the pre-allocated event buffer spaces are exhausted, that is, when there is no free event buffer space, the event buffer space can also provide an overwrite function, that is, the interface control module 41 can use the newly received setup packet and its number of the USB interface module 44 to replace the historical setup packet (that is, the setup packet previously received by the USB interface module 44) and its number stored in the event buffer space. The specific implementation can refer to the relevant introduction of Case 2 in S701 above, and will not be elaborated here.
[0127] S803. The interface control module 41 reports an event to notify the main control module 42, triggering the main control module 42 to read the number and the setup packet;
[0128] Specifically, the specific way for the interface control module 41 to report an event can be by generating a hardware interrupt, such as generating a setup interrupt signal.
[0129] S804. After receiving the notification, the main control module 42 reads the last (or the latest) setup packet and its number from the event buffer space, and parses the read setup packet to obtain the size, content, and direction of the data packet to be transmitted. Figure 5 Taking the reading of the first setup packet and the first number as an example;
[0130] S805. The main control module 42 allocates a data cache space of a specified size according to the size of the first data packet. This data cache space is used to store the first data packet to be transmitted; and associates the first data packet to be transmitted with the first number.
[0131] There are various ways for the main control module 42 to associate the first data packet to be transmitted with the first number, and this application does not make any restrictions. Here are several possible ways as examples: Way 1, the main control module 42 stores the first number into the data cache space allocated for the first data packet. For example, Figure 5 as shown, the data cache space includes a third subspace S432A for storing data packets and a fourth subspace S432B for storing numbers. The main control module stores the first number in the fourth subspace S432B. Way 2, the main control module 42 directly sends the first number to the interface control module 41 to indicate that the currently allocated data cache space is for the first data packet. Way 3, the main control module 42 stores the first number into other data cache spaces, and then associates the other data cache spaces with the data cache space for storing the first data packet to be transmitted.
[0132] It should be understood that if it is to control IN transmission, the main control module 42 also needs to prepare the data packet to be sent to the USB host and store it in the third subspace S432A of the data cache space.
[0133] It should also be noted that after the USB interface module 44 receives the first setup packet and before the data stage of the control transfer initiated by the first setup packet starts (i.e., after step S802 and before step S806), the USB interface module 44 may receive a new setup packet. Whether the USB interface module 44 will receive a new setup packet during this period depends on whether the USB host sends a new setup packet. If the USB interface module 44 receives a new setup packet again, the interface control module 41 also executes the above S802 process for the new setup packet. For example, after step S802 and before step S806, the USB interface module 44 receives a second setup packet from the USB host. The second setup packet is used to indicate that the USB host and the USB peripheral start another control transfer; the interface control module 41 generates a unique number for the second setup packet, such as the second number, and stores the second number and the second setup packet in the event cache space allocated in S801 in an associated manner; where the first number is different from the second number.
[0134] S806. Data packet transmission.
[0135] After the data stage of the control transfer initiated by the first setup packet starts, the interface control module 41 transfers the data packet to the USB host only when it determines that the number of the setup packet most recently received by the USB interface module 44 is the same as the number of the data packet to be transferred; if they are not the same, the data packet is not transferred.
[0136] If the data stage of this control transfer is an OUT transfer, the data transfer process may include: the USB interface module 44 receives a data packet from the USB host; if the number most recently stored in the event buffer space is the same as the number in the data buffer space, the interface control module 41 stores the data packet received by the USB interface module 44 in the data buffer space; otherwise, the data packet is discarded.
[0137] If the data stage of this control transfer is an IN transfer, the data transfer process may include: the USB interface module 44 receives a data packet request from the USB host; if the interface control module 41 determines that the number most recently stored in the event buffer space is the same as the number in the data buffer space, the interface control module 41 reads the data packet from the data buffer space and controls the USB interface module 44 to send the data packet to the USB host; otherwise, no data packet is returned.
[0138] A specific example: after the USB host sends setup packet -1 to the USB peripheral and then immediately sends a setup packet -2 to the USB peripheral, based on the above method, the number most recently in the event buffer space is the number of setup packet -2 (assumed to be the second number); at this time, if the main control module 42 is preparing to transfer data packet -1 (i.e., the number in the data buffer space is the number of setup packet -1, such as the first number, and the data packet in the data buffer space is data packet -1), when the USB host sends a request for data packet -2, the interface control module 41 will determine that the number in the data buffer space (i.e., the first number) is not the same as the number most recently in the event buffer space (i.e., the second number), so it will not control the USB interface module 44 to send data packet -1 to the USB host, thus avoiding the mis - transfer of data packets.
[0139] S807, status packet transfer.
[0140] Specifically, the main control module 42 issues a status packet to the interface control module 41, and the interface control module 41 controls the USB interface module 44 to send the status packet to the USB host.
[0141] Optionally, a number verification process can also be added during the transmission of the status packet. For example, before the main control module 42 sends the status packet to the interface control module 41, the main control module 42 can use the first number read in S804 as the number of the status packet, and then send this number and the status packet to the interface control module 41 together; correspondingly, after the USB interface module 44 receives the status packet request from the USB host, the interface control module 41 can verify whether the number of the status packet is consistent with the number of the latest received setup packet by the USB interface module 44. If they are consistent, the interface control module 41 controls the USB interface module 44 to send the status packet to the USB host. Otherwise, the interface control module 41 does not control the USB interface module 44 to send the status packet, thus avoiding the mistransmission of the status packet.
[0142] So far, this control transfer is completed.
[0143] After receiving a new Setup packet at any time, the USB peripheral only needs to repeat the above steps S802 - S806.
[0144] From the above, it can be seen that the USB peripheral in the embodiment of the present application generates a unique number for each setup packet. When preparing the transmission of the data / status packet, the data / status packet is attached with the number of the setup packet corresponding to the current pen control transfer, and the number verification is performed before the transmission. Only after the number corresponding to the data / status packet is consistent with the number of the latest received setup packet, the USB peripheral starts to transmit the data / status packet. In this way, the occurrence of mispackets can be avoided, thereby improving the reliability of the USB control transfer; moreover, the event cache space and the data cache space are independent of each other and do not interfere with each other, so it can also improve the packet loss phenomenon caused by the untimely allocation of the data cache space, further improving the reliability of the USB control transfer; not only that, the USB peripheral cache can also provide a coverage function, which can ensure that the USB peripheral receives and saves new setup packets at any time, thereby avoiding the occurrence of packet loss; in addition, the USB peripheral can also allocate multiple event cache spaces at one time, which can reduce the interaction between the software and hardware of the USB peripheral, thereby improving the efficiency of the USB control transfer.
[0145] It should be understood that the above Figure 5In the illustrated embodiment, the cache space for storing the setup package takes the event cache space as an example. In specific implementation, it can also be replaced with the data cache space, that is, both the setup package and the data / status package are stored in the data cache space, and number verification is performed on the setup package and the data / status package before transmitting the data / status package, which can also avoid the occurrence of mispackets during transmission, thereby improving the reliability of USB control transmission.
[0146] The above embodiments can be combined with each other to achieve different technical effects.
[0147] See Figure 6 , another method based on USB communication provided by the embodiments of the present application. Taking the method as an example of being applied to the USB peripherals in the above Figure 2A , the method includes:
[0148] S901. The USB peripheral receives the setup package from the USB host at different times, and any one of the setup packages is used to initiate a corresponding control transmission;
[0149] S902. The USB peripheral generates a unique number for each received setup package, and associates each setup package with its corresponding number; and associates the number corresponding to each setup package with the data package to be transmitted in the control transmission initiated by each setup package;
[0150] S903. After the start of the data transmission stage of any control transmission, the USB peripheral determines that the number corresponding to the data package to be transmitted in the any control transmission is consistent with the number corresponding to the latest received setup package by the USB peripheral, and then transmits the data package to be transmitted in the any control transmission to the USB host.
[0151] For the specific implementation methods of the steps in S901 to S903 above, reference can be made to the relevant embodiments in the above text, and details are not described here again.
[0152] Based on the same technical concept, the embodiments of the present application also provide a computer-readable storage medium storing computer program instructions, which when executed, implement the method as described in the above Figures 3 to 6 .
[0153] Based on the same technical concept, the embodiments of the present application also provide a chip, which can be coupled with a memory. The chip is used to read and execute the program instructions stored in the memory to implement the method as described in the above Figures 3 to 6 .
[0154] Based on the same inventive concept, an embodiment of the present application also provides a computer program product. Instructions are stored in the computer program product. When it runs on a computer, it causes the computer to execute the above-mentioned Figures 3 to 6 method.
[0155] Those skilled in the art should understand that the embodiments of the present application can be provided as methods, systems, or computer program products. Therefore, the present application can take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware aspects. Moreover, the present application can take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0156] The present application is described with reference to the flowcharts and / or block diagrams of methods, apparatuses (systems), and computer program products according to the present application. It should be understood that each flow and / or block in the flowchart and / or block diagram can be implemented by computer program instructions, and the combination of flows and / or blocks in the flowchart and / or block diagram can also be implemented. These computer program instructions can be provided to the processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing devices to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing devices generate a device for implementing the functions specified in Figure 1 one or more flows and / or blocks Figure 1 one or more blocks.
[0157] These computer program instructions can also be stored in a computer-readable memory that can direct a computer or other programmable data processing device to work in a specific manner, so that the instructions stored in the computer-readable memory generate a manufactured article including an instruction device, and the instruction device implements the functions specified in Figure 1 one or more flows and / or blocks Figure 1 one or more blocks.
[0158] These computer program instructions can also be loaded onto a computer or other programmable data processing device, so that a series of operation steps are executed on the computer or other programmable device to generate a computer-implemented process. Therefore, the instructions executed on the computer or other programmable device provide steps for implementing the functions specified in Figure 1 one or more flows and / or blocks Figure 1 one or more blocks.
[0159] Obviously, those skilled in the art can make various changes and modifications to this application without departing from the scope of protection of this application. Thus, if these modifications and variations of this application fall within the scope of the claims of this application and their equivalent technologies, this application is also intended to include these modifications and variations.
Claims
1. A communication method based on Universal Serial Bus (USB), characterized in that, The method is applied to a USB peripheral, and the USB peripheral is communicatively connected to a USB host through a USB bus; The method includes: Receiving setup packets from the USB host at different times, where any one of the setup packets is used to initiate a corresponding control transfer; Generating a unique number for each of the received setup packets, and associating each of the setup packets with its corresponding number; and, associating the number corresponding to each of the setup packets with the data packets to be transmitted in the control transfer initiated by each of the setup packets; After the data transfer stage of any control transfer starts, after determining that the number corresponding to the data packet to be transmitted in the any control transfer is consistent with the number corresponding to the latest received setup packet by the USB peripheral, transmitting the data packets to be transmitted in the any control transfer with the USB host.
2. The method according to claim 1, characterized in that, Before associating each of the setup packets with its corresponding number, the method further includes: Allocating at least one event buffer space; Associating each of the setup packets with its corresponding number includes: Storing each of the setup packets and its corresponding number in the same event buffer space.
3. The method according to claim 2, characterized in that, There are multiple allocated event buffer spaces; Storing each of the setup packets and its corresponding number in the same event buffer space includes: Sequentially storing multiple setup packets received at different times and their corresponding numbers into the multiple allocated event buffer spaces according to the chronological order of receiving the setup packets.
4. The method according to claim 2, characterized in that, Storing each of the setup packets and its corresponding number in the same event buffer space includes: If there is an idle event buffer space in the at least one event buffer space, storing each of the setup packets and its corresponding number in the idle event buffer space; If there is no idle event buffer space in the at least one event buffer space, using N setup packets received at different times and their corresponding numbers to respectively replace the setup packets and corresponding numbers in the N event buffer spaces with the oldest to newest historical numbers stored in the at least one event buffer space, where N is a positive integer.
5. The method according to claim 2, characterized in that, If there is no idle event buffer space in the at least one event buffer space, storing each of the setup packets and its corresponding number in the same event buffer space includes: Clearing all the event buffer spaces in the at least one event buffer space; sequentially storing N setup packets received at different times and their corresponding numbers into the N cleared event buffer spaces according to the chronological order of receiving the setup packets; or, After receiving the i-th setup packet among the N setup packets, clearing one event buffer space in the at least one event buffer space; storing the i-th setup packet and its corresponding number in the cleared one event buffer space, where i takes all positive integers from 1 to N; where N is a positive integer.
6. The method according to claim 5, characterized in that, Before being cleared, the one event buffer space stores: the setup packet with the earliest receiving time among the received setup packets, or the smallest number or the largest number among the numbers corresponding to the received setup packets.
7. The method according to any one of claims 1 - 6, characterized in that, After associating each of the established packets with its corresponding number, and before associating the number corresponding to each of the established packets with the data packets to be transmitted in the control transfer initiated by each of the established packets, the method further includes: Parsing the content of each of the established packets to obtain the size of the data packets to be transmitted in the control transfer initiated by each of the established packets; Allocating data buffer space for the data packets to be transmitted in the control transfer initiated by each of the established packets according to the size; Associating the number corresponding to each of the established packets with the data packets to be transmitted in the control transfer initiated by each of the established packets, including: For any established packet, storing the number corresponding to the any established packet and the data packets to be transmitted in the control transfer initiated by the any established packet into the data buffer space allocated for the data packets to be transmitted in the control transfer initiated by the any established packet.
8. The method according to claim 7, characterized in that, The data stage of any control transfer is for the USB host to send data packets to the USB peripheral; After the data stage of any control transfer starts, after determining that the number corresponding to the data packets to be transmitted in the any control transfer is consistent with the number corresponding to the latest established packet received by the USB peripheral, transmitting the data packets to be transmitted in the any control transfer with the USB host, including: Receiving data packets from the USB host; After determining that the number corresponding to the data packets to be transmitted in the any control transfer is consistent with the number corresponding to the latest established packet received by the USB peripheral, storing the received data packets into the data buffer space allocated for the data packets to be transmitted in the any control transfer.
9. The method according to claim 7, characterized in that, The data stage of any control transfer is for the USB peripheral to send data packets to the USB host; After parsing the content of each of the established packets, it further includes: Obtaining the content of the data packets to be transmitted in the control transfer initiated by each of the established packets; After allocating data buffer space for the data packets to be transmitted in the control transfer initiated by each of the established packets according to the size, and before the data stage of any control transfer starts, the method further includes: Storing the data packets to be transmitted in the control transfer initiated by each of the established packets into the data buffer space allocated for the data packets to be transmitted in the control transfer initiated by each of the established packets according to the content; After the data stage of any control transfer starts, after determining that the number corresponding to the data packets to be transmitted in the any control transfer is consistent with the number corresponding to the latest established packet received by the USB peripheral, transmitting the data packets to be transmitted in the any control transfer with the USB host, including: Receiving a data packet request from the USB host, where the data packet request is used to request the USB peripheral to send data packets; After determining that the number corresponding to the data packet to be transmitted in any control transfer is consistent with the number corresponding to the setup packet most recently received by the USB peripheral, read the data packet from the data buffer space allocated for the data packet to be transmitted in any control transfer according to the data packet request and send it to the USB host.
10. The method according to any one of claims 1-6, 8-9, characterized in that, After associating the number corresponding to each setup packet with the data packet to be transmitted in the control transfer initiated by each setup packet, the method further includes: Associating the number corresponding to each setup packet with the status packet in the control transfer initiated by each setup packet; After the data stage of any control transfer ends, the method further includes: Receiving a status packet request from the USB host, where the status packet request is used to request the USB peripheral to send a status packet; after determining that the number corresponding to the status packet in any control transfer is consistent with the number corresponding to the setup packet most recently received by the USB peripheral, send the status packet to be transmitted in any control transfer to the USB host.
11. The method according to claim 7, characterized in that, After associating the number corresponding to each setup packet with the data packet to be transmitted in the control transfer initiated by each setup packet, the method further includes: Associating the number corresponding to each setup packet with the status packet in the control transfer initiated by each setup packet; After the data stage of any control transfer ends, the method further includes: Receiving a status packet request from the USB host, where the status packet request is used to request the USB peripheral to send a status packet; after determining that the number corresponding to the status packet in any control transfer is consistent with the number corresponding to the setup packet most recently received by the USB peripheral, send the status packet to be transmitted in any control transfer to the USB host.
12. A USB-based communication device, characterized in that, The device is applied to a USB peripheral, and the USB peripheral is communicatively connected to a USB host through a USB bus; A USB interface module, configured to receive setup packets from the USB host at different times, where any one of the setup packets is used to initiate a corresponding control transfer; An interface control module, configured to generate a unique number for each received setup packet and associate each setup packet with its corresponding number; A main control module, configured to associate the number corresponding to each setup packet with the data packet to be transmitted in the control transfer initiated by each setup packet; The interface control module is further configured to, after the data transmission stage of any control transfer starts, after determining that the number corresponding to the data packet to be transmitted in any control transfer is consistent with the number corresponding to the setup packet most recently received by the USB peripheral, control the USB interface module to transmit the data packet to be transmitted in any control transfer with the USB host.
13. The device according to claim 12, characterized in that, The main control module is further configured to: Allocate at least one event buffer space before the interface control module associates each setup packet with its corresponding number; When the interface control module associates each setup packet with its corresponding number, it is specifically configured to: Store each of the established packets and its corresponding number in the same event cache space.
14. The device according to claim 13, characterized in that, There are multiple allocated event cache spaces; When the interface control module stores each of the established packets and its corresponding number in the same event cache space, it is specifically used for: According to the time sequence of receiving the established packets, store multiple established packets received at different times and their corresponding numbers in the multiple allocated event cache spaces in sequence.
15. The device according to claim 13, characterized in that, When the interface control module stores each of the established packets and its corresponding number in the same event cache space, it is specifically used for: If there is an idle event cache space in the at least one event cache space, store each of the established packets and its corresponding number in the idle event cache space; If there is no idle event cache space in the at least one event cache space, use N established packets received at different times and their corresponding numbers to respectively replace the established packets and corresponding numbers in the N event cache spaces with the oldest to newest historical numbers stored in the at least one event cache space, where N is a positive integer.
16. The device according to claim 14, characterized in that, If there is no idle event cache space in the at least one event cache space, when the interface control module stores each of the established packets and its corresponding number in the same event cache space, it is specifically used for: Empty all the event cache spaces in the at least one event cache space; according to the time sequence of receiving the established packets, store N established packets received at different times and their corresponding numbers in the N emptied event cache spaces in sequence; Or, After the USB interface module receives the i-th established packet among the N established packets, empty one event cache space in the at least one event cache space; Store the i-th established packet and its corresponding number in the emptied one event cache space, where i takes all positive integers from 1 to N; Where N is a positive integer.
17. The device according to claim 16, wherein, Before emptying, the one event cache space stores: the established packet with the earliest receiving time among the established packets received by the USB interface module, or the smallest number or the largest number among the numbers corresponding to the established packets received by the USB interface module.
18. The device according to any one of claims 12 - 17, wherein, The main control module is further used for: After the interface control module associates each of the established packets with its corresponding number, and before the main control module associates the number corresponding to each established packet with the data packet to be transmitted in the control transfer initiated by each established packet, parse the content of each established packet to obtain the size of the data packet to be transmitted in the control transfer initiated by each established packet; allocate data cache space for the data packet to be transmitted in the control transfer initiated by each established packet according to the size; When the main control module associates the number corresponding to each established packet with the data packet to be transmitted in the control transfer initiated by each established packet, it is specifically used for: For any established packet, store the number corresponding to the any established packet and the data packet to be transmitted in the control transfer initiated by the any established packet in the data buffer space allocated for the data packet to be transmitted in the control transfer initiated by the any established packet.
19. The device according to claim 18, wherein, The data stage of any control transfer is that the USB host sends a data packet to the USB peripheral; The USB interface module is further configured to: after the start of the data stage of any control transfer, receive a data packet from the USB host; When the interface control module controls the USB interface module to transmit the data packet to be transmitted in any control transfer with the USB host, it is specifically configured to: after determining that the number corresponding to the data packet to be transmitted in any control transfer is consistent with the number corresponding to the latest established packet received by the USB peripheral, store the received data packet in the data buffer space allocated for the data packet to be transmitted in any control transfer.
20. The device according to claim 18, wherein, The data stage of any control transfer is that the USB peripheral sends a data packet to the USB host; The main control module is further configured to: after parsing the content of each established packet, obtain the content of the data packet to be transmitted in the control transfer initiated by each established packet; after allocating data buffer space for the data packet to be transmitted in the control transfer initiated by each established packet, and before the start of the data stage of any control transfer, store the data packet to be transmitted in the control transfer initiated by each established packet in the data buffer space allocated for the data packet to be transmitted in the control transfer initiated by each established packet according to the content; The USB interface module is further configured to: after the start of the data stage of any control transfer, receive a data packet request from the USB host, where the data packet request is used to request the USB peripheral to send a data packet; When the interface control module controls the USB interface module to transmit the data packet to be transmitted in any control transfer with the USB host, it is specifically configured to: after determining that the number corresponding to the data packet to be transmitted in any control transfer is consistent with the number corresponding to the latest established packet received by the USB peripheral, read the data packet from the data buffer space allocated for the data packet to be transmitted in any control transfer according to the data packet request, and send the read data packet to the USB host through the USB interface module.
21. The device according to any one of claims 12 - 17, 19 - 20, wherein, The main control module is further configured to: After associating the number corresponding to each established packet with the data packet to be transmitted in the control transfer initiated by each established packet, associate the number corresponding to each established packet with the status packet in the control transfer initiated by each established packet; The USB interface module is further configured to: after the end of the data stage of any control transfer, receive a status packet request from the USB host, where the status packet request is used to request the USB peripheral to send a status packet; The interface control module is further configured to: after determining that the number corresponding to the status packet in any one control transfer is consistent with the number corresponding to the setup packet newly received by the USB peripheral, control the USB interface module to send the status packet to be transmitted in any one control transfer to the USB host.
22. The device according to claim 18, wherein, The main control module is further configured to: after associating the number corresponding to each setup packet with the data packet to be transmitted in the control transfer initiated by each setup packet, associate the number corresponding to each setup packet with the status packet in the control transfer initiated by each setup packet; The USB interface module is further configured to: after the data stage of any one control transfer ends, receive a status packet request from the USB host, where the status packet request is used to request the USB peripheral to send a status packet; The interface control module is further configured to: after determining that the number corresponding to the status packet in any one control transfer is consistent with the number corresponding to the setup packet newly received by the USB peripheral, control the USB interface module to send the status packet to be transmitted in any one control transfer to the USB host.
23. The device according to claim 13, wherein, The interface control module is further configured to: after storing each setup packet and its corresponding number in the same event buffer space, report an interrupt signal to the main control module, where the interrupt signal is used to instruct the main control module to read each setup packet and its corresponding number from the event buffer space; The main control module is further configured to: before associating the number corresponding to each setup packet with the data packet to be transmitted in the control transfer initiated by each setup packet, receive the interrupt signal from the interface control module; read each setup packet and its corresponding number from the event buffer space based on the interrupt signal.
24. A computer-readable storage medium, wherein, comprising computer program instructions which, when executed, implement the method according to any one of claims 1-11.
25. A chip, wherein, The chip is coupled to the memory and is configured to read and execute the program instructions stored in the memory to implement the method according to any one of claims 1-11.
26. A computer program product, wherein, The computer program product stores instructions which, when run on a computer, cause the computer to execute the method according to any one of claims 1-11.
Citation Information
Patent Citations
Multifunction peripheral and device control system
CN101277362A
Method for identifying host operation system by using USB equipment
CN102043751A