WO2022178882A1 - Usb-based communication method and apparatus - Google Patents

Usb-based communication method and apparatus Download PDF

Info

Publication number
WO2022178882A1
WO2022178882A1 PCT/CN2021/078314 CN2021078314W WO2022178882A1 WO 2022178882 A1 WO2022178882 A1 WO 2022178882A1 CN 2021078314 W CN2021078314 W CN 2021078314W WO 2022178882 A1 WO2022178882 A1 WO 2022178882A1
Authority
WO
WIPO (PCT)
Prior art keywords
packet
data
establishment
usb
control transmission
Prior art date
Application number
PCT/CN2021/078314
Other languages
French (fr)
Chinese (zh)
Inventor
吴凯杰
伍东兴
Original Assignee
华为技术有限公司
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by 华为技术有限公司 filed Critical 华为技术有限公司
Priority to PCT/CN2021/078314 priority Critical patent/WO2022178882A1/en
Priority to CN202180079881.9A priority patent/CN116601618A/en
Publication of WO2022178882A1 publication Critical patent/WO2022178882A1/en

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F13/00Interconnection of, or transfer of information or other signals between, memories, input/output devices or central processing units
    • G06F13/38Information transfer, e.g. on bus

Definitions

  • the present application relates to the field of computer technology, and in particular, to a communication method and device based on Universal Serial Bus (USB).
  • USB Universal Serial Bus
  • the USB communication protocol adopts a master-slave structure to realize the 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 transmission) on the USB bus are actively initiated by the USB host, while the USB peripherals passively receive and process various commands (requests) sent by the USB host.
  • the USB communication protocol has four modes for transferring data: control transfer, bulk transfer, interrupt transfer and isochronous transfer. Among them, the control transmission is used to transmit control, status, configuration and other information between the USB host and the USB peripheral.
  • a complete control transfer includes 2 to 3 phases: setup phase, data phase and status phase, wherein the data phase is optional. The control transfer is initiated by the USB host through the setup packet. After the USB peripheral receives the setup packet sent by the USB host, it considers that a new control transfer starts.
  • the USB host often initiates a new control transfer before the last control transfer is completed, and the USB peripheral cannot recognize the difference between the setup packet and the data packet/status packet in different control transfers. The corresponding relationship, resulting in the phenomenon of wrong packet transmission.
  • Embodiments of the present application provide a USB-based communication method and device, which are used to improve the reliability of USB control transmission.
  • a USB-based communication method is provided, and the method can be applied to a USB peripheral device, and the USB peripheral device is communicatively connected to a USB host through a USB bus.
  • the method includes: the USB peripheral device receives establishment packets from the USB host at different times, and any one of the establishment packets is used to initiate a corresponding control transmission; the USB peripheral device generates a unique number for each establishment packet that has been received, and Associate each establishment packet with its corresponding number, and associate the corresponding number of each establishment packet with the data packet to be transmitted in the control transmission initiated by each establishment packet; start at the data transmission phase of any control transmission After the USB peripheral determines that the number corresponding to the data packet to be transmitted in any control transmission is consistent with the number corresponding to the establishment packet recently received by the USB peripheral, the USB peripheral transmits the pending control transmission to the USB host. transmitted packets.
  • the USB peripheral device in the embodiment of the present application generates a unique number for each setup packet received, and when preparing 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 is added to the data packet in the data packet. The number verification is performed before the packet is transmitted. Only after the corresponding number of the data packet is consistent with the number of the newly received establishment packet, the USB peripheral will start to transmit the data packet, so as to ensure the corresponding relationship between the data packet and the establishment packet and avoid wrong packets. The occurrence of transmission, thereby improving the reliability of USB control transmission.
  • the USB peripheral device needs to allocate at least one event buffer space before associating each establishment packet with its corresponding serial number.
  • associating each establishment packet with its corresponding serial number by the USB peripheral includes: storing each establishment packet and its corresponding serial number in the same event buffer space.
  • the USB peripheral stores the establishment packet in the event cache space, so that the cache space for storing the establishment packet (ie the event cache space) and the cache space for storing the data packet (ie the data cache space) are independent of each other and do not interfere with each other.
  • the packet loss phenomenon can be improved, and the reliability of USB control transmission can be further improved.
  • the USB peripheral device can store the multiple establishment packets and corresponding numbers received at different times in turn according to the time sequence of receiving the establishment packets. into the allocated multiple event buffer spaces.
  • the USB peripheral allocates multiple event buffer spaces at one time, which can reduce the frequency of allocating buffers by the USB peripheral, that is, reduce the software and hardware interaction of the USB peripheral, and improve the efficiency of USB control transmission.
  • the USB peripheral stores each establishment packet and its corresponding number in the same event buffer space, which may include: if there is free event buffer space in at least one event buffer space, then store each The establishment packets and their corresponding numbers are stored in the free event buffer space; if there is no free event buffer space in at least one event buffer space, the N establishment packets received at different times and their corresponding numbers are used to replace at least one of them respectively.
  • the history numbers stored in the event buffer space are from old to new N establishment packets and corresponding numbers in the event buffer space, where N is a positive integer.
  • the event buffer space of the USB peripheral provides an overwrite function.
  • a new establishment packet can be used to replace the old establishment packet, thus ensuring that the USB peripheral can receive and save at any time.
  • the new setup package can better avoid the occurrence of packet loss.
  • the specific way for the USB peripheral to store each establishment packet and its corresponding number in the same event buffer space includes the following: Two kinds:
  • the USB peripheral clears all event buffer spaces in at least one event buffer space;
  • the N establishment packets and corresponding serial numbers received by time are sequentially stored in the cleared N event buffer spaces; wherein, N is a positive integer. That is, the USB peripheral empties all the event buffer space at one time, and then uses the emptied event buffer space as the newly allocated event buffer space.
  • the USB peripheral device clears one event buffer in the at least one event buffer space after receiving the i-th establishment packet in the N establishment packets Space; store the i-th establishment packet and the corresponding number in the empty event buffer space, and i is a positive integer from 1 to N; wherein, N is a positive integer. That is, the USB peripheral empties a historical establishment packet after receiving a new establishment packet.
  • the one event buffer space may be any one of the at least one event buffer space.
  • the one event buffer space stores: the establishment packet with the earliest reception time among the received establishment packets, or the smallest number or the largest number among the numbers corresponding to the received establishment packets.
  • the USB peripheral associates each establishment packet with its corresponding number, and associates the corresponding number of each establishment packet with the data packet to be transmitted in the control transmission initiated by each establishment packet. Before the connection, it also parses the content of each establishment packet to obtain the size of the data packet to be transmitted in the control transmission initiated by each establishment packet (optionally, the data to be transmitted in the control transmission initiated by each establishment packet can also be obtained. content and direction of the packet); according to the size of the data packet to be transmitted, a data buffer space is allocated for the data packet to be transmitted in the control transmission initiated by each setup packet.
  • the USB peripheral device allocates data buffer space for the data packet to be transmitted in the control transmission initiated by each establishment packet according to the size, and in any Before the start of the data phase of the control transmission, a data packet to be transmitted in any one of the control transmissions can also be prepared according to the content, and the to-be-transmitted data packet is stored in the data buffer space. In this way, the reliability of USB control transmission can be improved.
  • the USB peripheral associating the number corresponding to each establishment packet with the data packet to be transmitted in the control transmission initiated by each establishment packet includes: for any establishment packet, the any establishment packet is The corresponding number and the data packet to be transmitted in the control transmission initiated by any establishment packet are stored in the data buffer space allocated for the data packet to be transmitted in the control transmission initiated by any establishment packet.
  • 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 any control transfer is consistent with the number corresponding to the latest establishment packet received by the USB peripheral, the received The data packets are stored in the data buffer space allocated for the data packets to be transmitted in the arbitrary control transmission.
  • the USB peripheral After the start of the data phase of the arbitrary control transfer, the USB peripheral receives a data packet request from the USB host, and the data packet request is used to request the USB peripheral to send a data packet; after determining that the arbitrary control transmission is to be transmitted After the number corresponding to the data packet is consistent with the number corresponding to the establishment packet newly received by the USB peripheral, read data from the data buffer space allocated for the data packet to be transmitted in any control transmission according to the data packet request. packet and send to the USB host.
  • the USB peripheral device after the USB peripheral device associates the number corresponding to each establishment packet with the data packet to be transmitted in the control transmission initiated by each establishment packet, it can also associate the corresponding number of each establishment packet with the data packet to be transmitted in the control transmission initiated by each establishment packet.
  • a status packet in a control transfer initiated by each setup packet is associated.
  • 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), and the USB peripheral determines any control transfer. Only after the number corresponding to the status packet in the transmission is consistent with the number corresponding to the establishment packet newly received by the USB peripheral device, the status packet to be transmitted in the arbitrary control transmission is sent to the USB host.
  • the USB peripheral also performs number verification before transmitting the status packet, and only after the number corresponding to the status packet is consistent with the number of the newly received establishment packet, the USB peripheral starts to transmit the status packet. This ensures that The corresponding relationship between the status packet and the establishment packet avoids the occurrence of wrong packet transmission, and further improves the reliability of USB control transmission.
  • a USB-based communication device which can be applied to a USB peripheral device, and the USB peripheral device and a USB host are communicatively connected through a USB bus; the device includes a device for implementing the first aspect or any one of the first aspects. modules/units of the method described in a possible embodiment.
  • the device may include: a USB interface module for respectively receiving establishment packets from the USB host at different times, wherein any establishment packet is used for initiating a corresponding control transmission; Each establishment packet received generates a unique number, and each establishment packet is associated with its corresponding number; the main control module is used to associate the corresponding number of each establishment packet with the control transmission initiated by each establishment packet to be transmitted.
  • the interface control module can also be used to determine the number corresponding to the data packet to be transmitted in any control transmission and the latest received by the USB peripheral after the data transmission phase of any control transmission starts. After the numbers corresponding to the established packets are consistent, the USB interface module and the USB host are controlled to transmit any data packet to be transmitted in the control transmission.
  • a third aspect provides a computer-readable storage medium storing computer program instructions that, when executed, cause the method described in the first aspect or any possible implementation manner of the first aspect to be executed. accomplish.
  • a fourth aspect provides a chip, which can be coupled with a memory, and is used for reading and executing program instructions stored in the memory, so as to achieve the above-mentioned first aspect or any possible implementation manner of the first aspect. method described.
  • a computer program product has instructions stored in the computer program product, when the computer program product is run on a computer, the computer causes the computer to execute the above-mentioned first aspect or any one of possible implementations of the first aspect. method.
  • FIG. 1 is a schematic diagram of a USB communication scenario applicable to an embodiment of the present application
  • FIGS. 2A to 2D are schematic structural diagrams of a USB peripheral device 4 to which the solution provided by the embodiment of the present application is applicable;
  • FIG. 3 is a flowchart of a method based on USB communication provided by an embodiment of the present application.
  • FIG. 5 is a flowchart of another method based on USB communication provided by an embodiment of the present application.
  • FIG. 6 is a flowchart of another method based on USB communication provided by an embodiment of the present application.
  • USB technology is usually used in data transmission of various electronic devices.
  • electronic devices can be charged by connecting to a power source through USB technology, and data transmission between one electronic device and another electronic device can also be performed through USB technology, such as video or picture. transmission sharing, etc.
  • a USB host and a USB peripheral can be connected through a USB bus to implement USB communication.
  • the USB host and the USB peripheral may be directly connected by a cable.
  • other devices may also be set between the USB host and the USB peripheral, which is not limited here.
  • a USB hub may 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 on the USB bus.
  • the USB communication protocol supports four modes of data transfer between the USB host and the USB peripheral, namely: control transfer, bulk transfer, interrupt transfer and isochronous transfer.
  • control transmission is used to transmit control, status, configuration and other information between the USB host and the USB peripheral.
  • a complete control transfer includes 2 to 3 phases: a setup phase, a data phase, and a status phase.
  • the data stage 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 begins.
  • control transmission on the USB host and the USB peripheral is divided according to the transmission direction of the data packet in the data phase, and may include two types of control OUT transmission and control IN transmission.
  • controlling OUT transmission means that the USB host sends data packets to the USB peripheral
  • controlling IN transmission means that the USB peripheral sends data packets to the USB host.
  • each control transfer requires the USB peripheral to allocate a buffer space in advance in order to receive the setup packet in the control transfer, which will cause the USB peripheral to
  • the interaction of software and hardware is cumbersome, and the efficiency of USB control transmission is low.
  • the storage locations of the setup package and the data package on the USB peripheral are in the data cache space, when the USB peripheral is allocated in the cache, the cache of the setup package and the cache of the data package are in competition, and the USB peripheral cannot Read/write the setup packet and the data packet at the same time. These reasons may cause the cache space to be allocated untimely, resulting in packet loss.
  • the USB peripheral 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, the setup packet cannot be stored, and the setup packet is discarded; If the setup packet is not stored successfully, the USB peripheral cannot allocate data buffer space in advance for the data packet to be transmitted in the data phase. Transmission failed.
  • the USB host initiates a new control transfer before the previous control transfer is completed.
  • the setup packet, data or status packet in different control transfers before and after will appear on the USB peripheral.
  • the USB peripheral cannot determine the correspondence between the setup packet and the data/status packet in different pen control transmissions, and will mistakenly reply the last data packet to the USB host, resulting in the occurrence of wrong packets.
  • embodiments of the present application provide a USB-based communication solution.
  • the USB peripheral can generate a unique number for the setup packet (or for the control transmission initiated by the setup packet), and associate the number with the setup packet. Packet association; when the USB peripheral prepares the transmission of the data packet/status packet, it needs to attach (or associate) the data packet/status packet with the number of the setup packet corresponding to the current pen control transmission (that is, with the data packet/status packet).
  • the number of the setup package corresponding to the package), and the number check is performed before transmission; only after the number attached to the data package/status package is consistent with the number of the newly received setup package, the USB peripheral starts to transmit the data/status package. , so that the occurrence of wrong packet transmission can be avoided and the reliability of USB control transmission can be improved.
  • the USB peripheral can also store the setup package in the event cache space and store the data package in the data cache space, so that the cache spaces used by the setup package and the data package are independent of each other and do not interfere with each other, which can improve the Packet loss phenomenon, improve the reliability of USB control transmission.
  • the USB peripheral device may also allocate multiple cache spaces for storing setup packets in advance, and store the setup packets from the USB host in the order in which the setup packets are received, thus reducing the allocation of cache spaces for USB peripheral devices
  • the frequency of USB peripherals is reduced, that is, the software and hardware interaction of USB peripherals is reduced, and the efficiency of USB control transmission is improved.
  • the USB peripheral can also provide a cache coverage function. After the USB peripheral receives the setup package, if the USB peripheral has not allocated a cache space for storing the setup package, but the USB peripheral has If the cache space of the historical setup package ("historical setup package" refers to the previously received setup package) is stored, the historical setup package can be replaced with the setup package. In this way, even if there is no free cache space, the USB peripherals can Receive and save new setup packets at any time to avoid packet loss.
  • the embodiment of the present application provides a schematic structural diagram of a USB peripheral 4 to which the solution can be applied, including an interface control module 41 , a main control module 42 , a cache module 43 and a USB interface module 44 .
  • the USB interface module 44 is a physical interface (ie, a USB socket) of the USB peripheral device 4, and can be inserted into a USB data cable.
  • the data from the USB host arrives at the USB peripheral 4 through the USB socket of the USB host, the USB data cable and the USB interface module 44 in turn.
  • the data sent by the USB peripheral 4 to the USB host finally passes through the USB interface module 44, the USB interface module 44 and the USB interface module 44.
  • the data cable, and the USB socket of the USB host reach the USB host.
  • the USB interface module 44 may be an interface conforming to the USB standard specification, and specifically may be a Mini USB interface, a Micro USB interface, a USB Type C interface, and the like.
  • the USB interface module 44 can be used to connect a charger to charge the electronic device, and can also be used to transmit data between the electronic device and other peripheral devices (such as a USB host). It can also be used to connect headphones to play audio through the headphones.
  • the interface control module 41 is connected to the USB interface module 44 and the USB main control module 42 , and the interface control module 41 is responsible for data transmission control on the USB interface module 44 .
  • the data packet to be sent to the USB host is read from the cache module 43 and sent through the USB interface 44 , and the data packet received by the USB interface 44 from the USB host is stored in the cache module 43 .
  • the USB interface control module 41 not only performs data reading and writing in the cache module 43 , but also adds other functions, such as generating a corresponding number for each control transmission and reporting events to the main control module 42 . , Before transmitting data packets and/or status packets, perform number check, etc.
  • the cache module 43 has a cache space, and the cache space may specifically be a computer readable and writable storage medium for storing the data received by the USB peripheral device 4 from the USB host and the data to be sent.
  • the cache space is divided into two types: event cache space and data cache space, wherein the event cache space can be used to store the setup package in the establishment stage, and the data cache space can be used to store the data package in the data stage.
  • the data cache space is a cache space for storing data, for example, the data package in the data storage stage, the status package in the status stage, and the like.
  • the event buffer space is the buffer space used to store events, such as the event of receiving the setup package, the event of the completion of data transmission, etc. It should be noted that, in the embodiment of the present application, the setup package belonging to the data is stored in the event cache space, which is different from the prior art where the setup package is stored in the data cache space, so that the interface control module 41 can access the data cache space and the event at the same time.
  • the cache space that is, the read and write of the setup package and the read and write of the data package are independent of each other, and the allocation of the cache space of the two is also independent of each other.
  • the main control module 42 is the main control center of the USB peripheral 4.
  • the main control module 42 can perform one or more of the following functions: control the data phase, start/end the status phase, allocate event buffer space, allocate data buffer space, and the like.
  • the main control module 42 can also be integrated in the interface control module 41.
  • the interface control module 41 is a USB controller, and the USB controller has a separate MCU or MPU, the main control module 41 can pass the USB controller. MCU or MPU in the implementation.
  • the main control module 42 may integrate driver software supporting the USB protocol.
  • client software (or an upper-layer application) is also integrated on the main control module 42 .
  • the driver software is a special program that can make the computer and hardware communicate with each other, which is equivalent to the interface of the hardware, and the client software controls the work of the hardware through the interface.
  • the client software is the upper-layer software of the driver software.
  • the client software cannot directly interact with other hardware of the USB peripheral 4. It needs to pass the instruction to the driver software, and then the driver software calls the hardware in the USB peripheral 4 to execute the corresponding operation.
  • the signal reported by the hardware in the USB peripheral 4 also needs to be packaged to the driver software first, and then transmitted by the driver software to the client software.
  • Various functions performed by the main control module 42 can be implemented by running driver software and client software.
  • each module can be further subdivided according to the functions performed by each module.
  • the cache module 43 includes an event cache space 431 and 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 check sub-module 413, etc., wherein the number generation sub-module 413, etc.
  • the module 411 can be used to generate a corresponding number for each control transmission, and the event reporting sub-module 412 can be used to report the event to the main control module 42 (for example, notifying the main control module 42 of the event of receiving the setup package and/or the event of the generation number) , the number check sub-module 413 can be used to perform number check in the data phase and the status phase.
  • the event reporting sub-module 412 can be used to report the event to the main control module 42 (for example, notifying the main control module 42 of the event of receiving the setup package and/or the event of the generation number)
  • the number check sub-module 413 can be used to perform number check in the data phase and the status phase.
  • the main control module 42 may specifically be, but is not limited to: a central processing unit (Central Processing Unit, CPU), a microcontroller unit (Micro Controller Unit, MCU), a microprocessor unit (Micro Processor Unit, MPU) ), digital signal processing (Digital Signal Processing/Processor, DSP), Field Programmable Gate Array (Field Programmable Gate Array, FPGA), etc.
  • the cache module 43 can be, but is not limited to: a register, a first-in first-out (First Input First Output, FIFO) memory, a static random access memory (Static Random Access Memory, SRAM), a dynamic random access memory (Dynamic Random Access Memory, DRAM) and the like.
  • the interface control module 41 may specifically be, but is not limited to, a logic circuit connected to the cache module 43 and the main control module 42 .
  • the interface control module 41 , the main control module 42 and the cache module 43 may be integrated in the same chip or in different chips, which is not limited in this application.
  • the interface control module 41, the main control module 42 and the cache module 43 are all integrated in the USB controller, wherein the specific implementation of the cache module 43 may be a register or FIFO memory or SRAM integrated in the USB controller) etc.
  • USB interface module 44 is not shown in FIG. 2C.
  • the interface control module 41 and the main control module 42 are integrated in the USB controller, and the cache module 43 is SRAM or DRAM, which is a part of the data memory outside the USB controller (the USB interface module in FIG. 2D). 44 not shown).
  • the interface control module 41 and the main control module 42 can also be provided separately, for example, the interface control module 41 is integrated in the USB controller, and the main control module 42 is integrated in the CPU.
  • USB peripheral 4 may further include more modules than those shown in FIGS. 2A to 2D , for example, may also include one or more hubs, which are not shown in this application.
  • the USB peripheral device 4 may specifically be, but not limited to, a U disk, an audio, a mouse, a keyboard, a computer, a mobile phone, a smart wearable device, a vehicle-mounted device, and the like.
  • a method based on USB communication provided by an embodiment of the present application, taking the method applied to the USB peripheral device in FIG. 2A as an example, the method includes:
  • the main control module 42 allocates at least two event buffer spaces, and notifies the interface control module 41 of the first address of the allocated event buffer spaces;
  • the interface control module 41 When the interface control module 41 stores the setup packet in the later stage, it can start from the first address and store the setup packet in sequence.
  • the USB interface module 44 receives the first setup package from the USB host, and the first setup package is used to indicate the start of the first control transmission; the interface control module 41 stores the first setup package in the first event buffer space;
  • USB interface module 44 is not shown in FIG. 3, but there is a USB interface module 44 between the actual interface control module 41 and the USB host.
  • the peripheral device finally sends data to the external device (such as a USB host) through the USB interface module 44 and receives the data sent by the external device.
  • the interface control module 41 reports an event to notify the main control module 42;
  • the specific manner in which the interface control module 41 reports the event may be a manner of generating a hardware interrupt, for example, generating a setup interrupt signal.
  • the main control module 42 After receiving the notification, the main control module 42 reads the content of the last (or the latest) setup package from the first event buffer space, for example, reads and parses the content of the first setup package, and learns the data package to be transmitted size, content and orientation;
  • the main control module 42 allocates the first data buffer space according to the size of the data packet to be transmitted, and notifies the interface control module 41 of the first address of the first data buffer space;
  • 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 stored sequentially from the first address.
  • 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 the control IN transmission, the USB interface The interface module 44 receives the 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 the data packet to the USB host through the USB interface module 44 .
  • the main control module 42 sends the status packet to the interface control module 41 ; after the USB interface module 44 receives the status packet request from the USB host, the interface control module 41 returns the status packet to the USB host through the USB interface module 44 .
  • the main control module 42 can deliver a status packet to the interface control module 41 in various ways, which is not limited in this application.
  • the status packet is directly transferred to the interface control module 41; or, for example, the status packet is stored in the first data buffer space, and then the interface control module 41 reads the status packet from the first data buffer space; or, for example, the status packet is stored in the first data buffer space. into other data buffer spaces, and then the interface control module 41 reads the status packet from the other data buffer spaces.
  • the USB interface module 44 receives the second setup package from the USB host, and the second setup package is used to indicate the start of the second control transmission; the interface control module 41 stores the second setup package in the second event buffer space;
  • the interface control module 41 reports the event to notify the main control module 42;
  • the main control module 42 After receiving the notification, the main control module 42 reads and parses the content of the last (or the latest) setup package from the second event buffer space, for example, reads and parses the content of the second setup package to obtain the content to be transmitted.
  • the main control module 42 allocates the second data buffer space according to the size of the data packet to be transmitted, and notifies the interface control module 41 of the first address of the second data buffer space;
  • 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 second data buffer space.
  • 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 buffer space; if it is the control IN transmission, the USB interface The interface module 44 receives the data packet request from the USB host, and the interface control module 41 retrieves the data packet from the second data buffer space and sends the data packet to the USB host through the USB interface module 44 .
  • the main control module 42 sends the status packet to the interface control module 41; the USB interface control module 41 returns the status packet to the USB host through the USB interface control module 41 after the USB interface module 44 receives the status packet request from the USB host.
  • FIG. 3 is an example of allocating two event buffer spaces in advance, so FIG. 3 only shows the process of two control transmissions.
  • USB peripherals can allocate more event buffer space in advance, so after the second control transfer is completed, the process similar to the above S601 ⁇ S606 or S601' ⁇ S606' can continue to be executed, which is different here. one shows.
  • the USB peripheral device in the embodiment of the present application can allocate multiple event buffer spaces at one time to store the setup packets of multiple control transmissions, which can avoid the need for the USB peripheral device to allocate a buffer in advance for each control transmission.
  • the technical defects of space reduce the interaction between software and hardware of USB peripherals, and improve the efficiency of USB control transmission.
  • USB peripherals use the event cache space to store Setup packets and the data cache space to store Data packets.
  • 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 loss caused by the untimely allocation of the data cache space.
  • the packet phenomenon further improves the reliability of USB control transmission.
  • the cache space used to store the setup package in the above-mentioned embodiment shown in FIG. 3 takes the event cache space as an example, and can also be replaced with a data cache space during specific implementation, that is, the setup package in the data cache space. Allocating multiple cache spaces can also reduce the interaction between software and hardware of USB peripherals and improve the efficiency of USB control transmission.
  • FIG. 4 another method based on USB communication provided by an embodiment of the present application, taking the method applied to the USB peripheral in the above-mentioned FIG. 2A as an example, the method includes:
  • the main control module 42 allocates one or more event buffer spaces, and notifies the interface control module 41 of the first address of the one or more event buffer spaces;
  • 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 transmission; the interface control module 41 determines whether there is a free event buffer space in the event buffer space, and if there is, the USB in S701 The setup packet received by the interface module 44 is stored in the free event buffer space; otherwise, the setup packet received by the USB interface module 44 in S701 is used to replace the setup packet (ie the historical setup packet) in the non-idle event buffer space.
  • USB interface module 44 is not shown in FIG. 4 , but there is a USB interface module between the actual interface control module 41 and the USB host. It is assumed that the USB interface module 44 finally sends data to an external device (such as a USB host) and receives data from the external device.
  • an external device such as a USB host
  • USB interface module 44 stores the setup package in the two cases of allocating one event buffer space and multiple event buffer spaces to the main control module 42 .
  • 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 transmission starts, the USB interface module 44 receives the first setup packet. to the second setup package, but since the event buffer space has already stored the first setup package in the first control transfer, the interface control module 41 replaces the first setup package with the second setup package; 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 transmission, the interface control module 41 replaces the second setup packet with the third setup packet.
  • the main control module 42 allocates multiple event buffer spaces, and when the interface control module 41 stores the setup package, it starts from the first event buffer space of the multiple event buffer spaces, and sequentially stores the setup package.
  • the main control module 42 allocating three event buffer spaces (ie, the first event buffer space, the second event buffer space, and the third event buffer space) as an example: after the first control transmission starts, the USB interface module 44 receives the first setup package, the interface control module 41 stores the first setup package in the first event buffer space; after the second control transmission starts, the USB interface module 44 receives the second setup package, and the interface control module 41 stores the second setup package in the The second event buffer space; after the third control transmission starts, 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.
  • three event buffer spaces ie, the first event buffer space, the second event buffer space, and the third event buffer space
  • the old setup package may be replaced with a new setup package to ensure that the new setup package does not lose packets.
  • the USB interface module 44 receives the fourth setup packet, and all the event buffer spaces (ie the first event buffer space, the second event buffer space, the second event buffer space The buffer space and the third event buffer space) have already stored the setup package, then the interface control module 41 can replace the old setup package with the new setup package (ie, the fourth setup package).
  • the present application does not limit the specific manner in which the interface control module 41 uses the new setup package to replace the old setup package.
  • the interface control module 41 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 event buffer space among the plurality of event buffer spaces, and stores the new setup packet and the corresponding number are stored in the emptied event buffer space.
  • the one event buffer space is stored before clearing: among the received (or stored) setup packages, the setup package with the earliest or latest time is received, or in the received (or stored) setup package. The smallest or largest number among the numbers corresponding to the setup package. It should be understood that if it is numbered in descending order, the setup package with the earliest reception time is the setup package with the smallest number. On the contrary, if it is numbered in descending order, the setup package with the earliest reception time is the one with the largest number. the setup package.
  • the fourth setup package can be used to replace the first event cache space.
  • the first setup package previously stored in .
  • USB interface module 44 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 all the event buffer space at one time, then starts a new polling, and stores the storage in sequence. New setup package and corresponding number.
  • the interface control module 41 clears all event buffer spaces (ie, the first event buffer space, the second event buffer space, and the third event buffer space), and stores the fourth setup package in the first event buffer. After that, if there is a new setup package, it will be stored in the second event buffer space in sequence.
  • the interface control module 41 reports an event to notify the main control module 42;
  • the main control module 42 After receiving the notification, the main control module 42 reads and parses the content of the last (or latest) setup package, such as the content of the first setup package, from the event buffer space, and obtains the size and content of the data package to be transmitted. and direction;
  • the main control module 42 allocates the data buffer space according to the size of the data packet to be transmitted, and notifies the interface control module 41 of the first address of the data buffer space;
  • the USB peripheral cache provides an overlay function. After the USB peripheral receives the setup package, if the USB peripheral has not allocated the event cache space for storing the setup package, the previously controlled The event buffer space allocated during the transmission process is emptied and then new setup packets are stored. In this way, even if there is no free buffer space, the USB peripheral can receive and save new setup packets at any time, thereby avoiding the occurrence of packet loss.
  • USB peripherals use the event buffer space to store Setup packets and the data buffer space to store Data packets. The event buffer space and the data buffer space are independent of each other and do not interfere with each other, so it is also possible to lose packets caused by untimely allocation of data buffer space. phenomenon, and further improve the reliability of USB control transmission.
  • the cache space used to store the setup package in the embodiment shown in the above-mentioned FIG. 4 is an example of the event cache space.
  • it can also be replaced with a data cache space, that is, a data cache used to store the setup package.
  • a data cache space that is, a data cache used to store the setup package.
  • the old setup package is replaced with a new setup package in the data cache space, and the USB peripheral device can also receive and save a new setup package at any time to avoid the effect of packet loss.
  • FIG. 5 another method based on USB communication provided by an embodiment of the present application, taking the method applied to the USB peripheral in the above-mentioned FIG. 2A as an example, the method includes:
  • the main control module 42 allocates at least one event buffer space
  • each event buffer space includes a first subspace S431A for storing the setup package and a second subspace S431B for storing the number of the setup package. It should be understood that FIG. 5 only shows one event buffer space, and there may actually be more.
  • the USB interface module 44 receives the first setup package from the USB host, and the first setup package is used to instruct the USB host and the USB peripheral to start a control transmission; the interface control module 41 generates a unique number for the first setup package, such as the first setup package. A number, and then associate the first number with the first setup package.
  • USB interface module 44 is not shown in FIG. 5 , but there is a USB interface module between the actual interface control module 41 and the USB host. It is assumed that the USB interface module 44 finally sends data to an external device (such as a USB host) and receives data from the external device.
  • an external device such as a USB host
  • the interface control module 41 associates the first number with the first setup packet, which may be the interface control module 41 stores the first number and the first setup packet in the same event buffer space.
  • the first setup package is stored in the first subspace of the first event buffer space
  • the first number is stored in the second subspace of the first event buffer space.
  • the main control module 42 allocates only one event buffer space.
  • the event buffer space can provide an overlay function, that is, the newly received setup package and its number can overwrite the historical setup package (that is, the previously received setup package) and its number.
  • the main control module 42 allocates multiple event buffer spaces, then the interface control module 41 can store the multiple setup packets received in the multiple control transmissions in sequence according to the time sequence of receiving the setup packets. into the multiple event buffer spaces.
  • the event buffer space can also provide an overlay function, that is, the interface control module 41 can use the setup newly received by the USB interface module 44.
  • the package and its number replace the historical setup package (ie, the setup package previously received by the USB interface module 44 ) and its number stored in the event buffer space.
  • the interface control module 41 reports the event to notify the main control module 42, and triggers the main control module 42 to read the serial number and the setup package;
  • the specific manner in which the interface control module 41 reports the event may be a manner of generating a hardware interrupt, for example, generating a setup interrupt signal.
  • the main control module 42 After receiving the notification, the main control module 42 reads the last (or the latest) setup package and its number from the event buffer space, parses the read setup package, and obtains the size, content and number of the data package to be transmitted. direction, the first setup package and the first number are read as an example in Figure 5;
  • the main control module 42 allocates a data buffer space of a specified size according to the size of the first data packet, and the data buffer space is used to store the first data packet to be transmitted; compare the first data packet to be transmitted with the first serial number Associated;
  • the main control module 42 may associate the first data packet to be transmitted with the first number in various manners, which are not limited in this application.
  • way 1 the main control module 42 stores the first number in the data buffer space allocated for the first data packet.
  • the data buffer space includes a data buffer for storing data A third subspace S432A of the package and a fourth subspace S432B for storing numbers, and the main control module stores the first number in the fourth subspace S432B.
  • Mode 2 The main control module 42 directly issues the first number to the interface control module 41 to indicate that the currently allocated data buffer space is allocated for the first data packet.
  • Mode 3 The main control module 42 stores the first number in the other data buffer space, and then associates the other data buffer space with the data buffer space for storing the first data packet to be transmitted.
  • the main control module 42 also needs to prepare a data packet to be sent to the USB host, and store it in the third subspace S432A of the data buffer space.
  • the USB interface Module 44 may receive new setup packets. During this period, whether the USB interface module 44 will receive a new setup packet 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-mentioned process of S802 for the new setup packet.
  • the USB interface module 44 receives the second setup packet from the USB host, and the second setup packet is used to instruct the USB host and the USB peripheral to start another control transmission; the interface control module 41 is The second setup package generates a unique number, such as the second number, and the second number and the second setup package are associated and stored in the event buffer space allocated in S801; wherein the first number is different from the second number.
  • the interface control module 41 determines that the number of the setup packet newly received by the USB interface module 44 is consistent with the number of the data packet to be transmitted, and then transmits the data to the USB host. packet; if inconsistent, the data packet is not transmitted.
  • the data transmission process may include: 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 data buffer space, otherwise the data packet is discarded.
  • the data transmission process may include: the USB interface module 44 receives a data packet request from the USB host; if the interface control module 41 determines the latest stored number in the event buffer space and the data buffer space If the numbers are the same, 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, the data packet is not returned.
  • the USB host After the USB host sends a setup packet-1 to the USB peripheral, it immediately sends a setup packet-2 to the USB peripheral. Based on the above method, the latest number in the event buffer space is the setup packet-2. The number (assuming it is the second number); at this time, if the main control module 42 prepares the transmission of the data packet-1 (that is, the number in the data buffer space is the number of the setup packet-1, such as the first number, the data buffer The data packet in the space is data packet-1), then after the USB host sends a request for data packet-2, the interface control module 41 will determine the number in the data buffer space (ie the first number) and the number in the event buffer space. The latest number (ie, the second number) is inconsistent, so the USB interface module 44 will not be controlled to send the data packet-1 to the USB host, so that the wrong packet transmission of the data packet can be avoided.
  • the main control module 42 is sending 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.
  • a number verification process can also be added to the status packet transmission process.
  • the main control module 42 can use the first number read in S804 as the first number.
  • the number of the status packet, and then the number and the status packet are sent to the interface control module 41; correspondingly, after the USB interface module 44 receives the status packet request from the USB host, the interface control module 41 can check the Whether the number of the status packet is consistent with the number of the setup packet newly received 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 Send the status packet, so as to avoid the wrong packet transmission of the status packet.
  • USB peripheral After the USB peripheral receives a new Setup package at any time, it only needs to repeat the above steps S802-S806.
  • the USB peripheral device in the embodiment of the present application generates a unique number for each setup packet, and when preparing the transmission of the data/status packet, the data/status packet is attached with the setup packet corresponding to the current pen control 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 will start to transmit the data/status packet, so as to avoid the transmission of wrong packets.
  • the event buffer space and the data buffer space are independent of each other and do not interfere with each other, so it can also improve the packet loss caused by the untimely allocation of data buffer space, and further improve the reliability of USB control transmission.
  • the USB peripheral cache can also provide an overlay function, which can ensure that the USB peripheral receives and saves a new setup packet at any time, thereby avoiding the occurrence of packet loss; in addition, the USB peripheral can also be allocated at one time. Multiple event buffer spaces can reduce the interaction between software and hardware of USB peripherals, thereby improving the efficiency of USB control transmission.
  • the cache space used to store the setup package in the embodiment shown in FIG. 5 is the event cache space as an example, and in specific implementation, it can also be replaced with a data cache space, that is, the setup is stored in the data cache space at the same time. Packets and data/status packets, and check the numbers of setup packets and data/status packets before transmitting data/status packets, which can also avoid the occurrence of wrong packet transmission, thereby improving the reliability of USB control transmission.
  • FIG. 6 another method based on USB communication provided by an embodiment of the present application, taking the method applied to the USB peripheral in the above-mentioned FIG. 2A as an example, the method includes:
  • the USB peripheral device respectively receives setup packets from the USB host at different times, wherein any setup packet is used to initiate a corresponding control transmission;
  • the USB peripheral generates a unique number for each setup package that has been received, and associates each setup package with its corresponding number; and, associates the number corresponding to each setup package with all The data packets to be transmitted in the control transmission initiated by each setup packet are associated;
  • the USB peripheral determines the number corresponding to the data packet to be transmitted in the arbitrary control transmission and the number corresponding to the setup packet newly received by the USB peripheral After the agreement is reached, the data packet to be transmitted in the arbitrary control transmission is transmitted with the USB host.
  • the embodiments of the present application further provide a computer-readable storage medium, which stores computer program instructions, and when the instructions are executed, the above-mentioned methods in FIG. 3 to FIG. 6 are implemented.
  • an embodiment of the present application also provides a chip, which can be coupled with a memory, and is used for reading and executing program instructions stored in the memory, implementing the methods in FIGS. 3 to 6 above.
  • an embodiment of the present application further provides a computer program product, where instructions are stored in the computer program product, and when the computer program product runs on a computer, the computer executes the methods in FIGS. 3 to 6 .
  • the embodiments of the present application may be provided as a method, a system, or a computer program product. Accordingly, the present application may take the form of an entirely hardware embodiment, an entirely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, the present application may take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, etc.) having computer-usable program code embodied therein.
  • computer-usable storage media including, but not limited to, disk storage, CD-ROM, optical storage, etc.
  • These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing apparatus to function in a particular manner, such that the instructions stored in the computer-readable memory result in an article of manufacture comprising instruction means, the instructions
  • the apparatus implements the functions specified in the flow or flow of the flowcharts and/or the block or blocks of the block diagrams.

Abstract

Disclosed in the present application is a USB-based communication method and apparatus for improving the reliability of USB control transmission. A USB peripheral can receive a setup packet from a USB host at different times; the USB peripheral generates a unique serial number for each received setup packet, associates each setup packet with its corresponding serial number, and associates the serial number corresponding to each setup packet with a data packet to be transmitted in the control transmission initiated by each setup packet; and after the data transmission phase of any control transmission starts, the USB peripheral is configured to transmit the data packet to be transmitted in the control transmission with the USB host after determining that the serial number corresponding to the data packet to be transmitted in the control transmission is consistent with the serial number corresponding to the latest received setup packet of the USB peripheral. In this way, the USB peripheral can ensure the correspondence between the data packet and the setup packet, to avoid the occurrence of wrong packet transmission, thereby improving the reliability of the USB control transmission.

Description

一种基于USB的通信方法和装置A USB-based communication method and device 技术领域technical field
本申请涉及计算机技术领域,尤其涉及一种基于通用串行总线(Universal Serial Bus,USB)的通信方法和装置。The present application relates to the field of computer technology, and in particular, to a communication method and device based on Universal Serial Bus (USB).
背景技术Background technique
USB通信协议采用主从结构,实现USB主机和USB外设(或称为USB从设备、USB外围设备、USB设备等)的通信。USB总线上所有的事务(数据流传输)都是由USB主机主动发起,而USB外设被动地接收然后处理USB主机发来的各种各样的命令(要求)。USB通信协议具有4种传输数据的模式:控制传输、批量传输、中断传输和等时传输。其中,控制传输用于传输USB主机和USB外设之间的控制、状态、配置等信息。一笔完整的控制传输包含2到3个阶段:建立(setup)阶段、数据(data)阶段和状态(status)阶段,其中数据阶段是可选的。控制传输由USB主机通过setup包发起,USB外设收到USB主机发送的setup包之后,认为新的一笔控制传输开始。The USB communication protocol adopts a master-slave structure to realize the 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 transmission) on the USB bus are actively initiated by the USB host, while the USB peripherals passively receive and process various commands (requests) sent by the USB host. The USB communication protocol has four modes for transferring data: control transfer, bulk transfer, interrupt transfer and isochronous transfer. Among them, the control transmission is used to transmit control, status, configuration and other information between the USB host and the USB peripheral. A complete control transfer includes 2 to 3 phases: setup phase, data phase and status phase, wherein the data phase is optional. The control transfer is initiated by the USB host through the setup packet. After the USB peripheral receives the setup packet sent by the USB host, it considers that a new control transfer starts.
在现有技术中,USB主机常常在上一笔控制传输还未完成时,又发起一笔新的控制传输,USB外设无法识别不同笔控制传输中的setup包与data包/status包之间的对应关系,导致错包传输现象。In the prior art, the USB host often initiates a new control transfer before the last control transfer is completed, and the USB peripheral cannot recognize the difference between the setup packet and the data packet/status packet in different control transfers. The corresponding relationship, resulting in the phenomenon of wrong packet transmission.
发明内容SUMMARY OF THE INVENTION
本申请实施例提供一种基于USB的通信方法和装置,用于提高USB控制传输的可靠性。Embodiments of the present application provide a USB-based communication method and device, which are used to improve the reliability of USB control transmission.
第一方面,提供一种基于USB的通信方法,该方法可以应用于USB外设,该USB外设与USB主机通过USB总线通信连接。方法包括:USB外设在不同时间分别从USB主机接收建立包,其中任意一个建立包用于发起一笔对应的控制传输;USB外设为已接收到的每个建立包生成唯一的编号,并将每个建立包与其对应的编号相关联,以及将每个建立包对应的编号与每个建立包发起的控制传输中待传输的数据包相关联;在任意一笔控制传输的数据传输阶段开始后,USB外设在确定任意一笔控制传输中待传输的数据包对应的编号与USB外设最新接收到的建立包对应的编号一致之后,才与USB主机传输该任意一笔控制传输中待传输的数据包。In a first aspect, a USB-based communication method is provided, and the method can be applied to a USB peripheral device, and the USB peripheral device is communicatively connected to a USB host through a USB bus. The method includes: the USB peripheral device receives establishment packets from the USB host at different times, and any one of the establishment packets is used to initiate a corresponding control transmission; the USB peripheral device generates a unique number for each establishment packet that has been received, and Associate each establishment packet with its corresponding number, and associate the corresponding number of each establishment packet with the data packet to be transmitted in the control transmission initiated by each establishment packet; start at the data transmission phase of any control transmission After the USB peripheral determines that the number corresponding to the data packet to be transmitted in any control transmission is consistent with the number corresponding to the establishment packet recently received by the USB peripheral, the USB peripheral transmits the pending control transmission to the USB host. transmitted packets.
本申请实施例中的USB外设针对接收到每个setup包生成唯一的编号,在准备数据包的传输时,将数据包附带上与当前笔控制传输之对应的建立包的编号,并在数据包传输前进行编号校验,只有在数据包对应的编号与最新接收的建立包的编号一致后,USB外设才开始传输数据包,如此可以保证数据包和建立包的对应关系,避免错包传输的发生,进而提高USB控制传输的可靠性。The USB peripheral device in the embodiment of the present application generates a unique number for each setup packet received, and when preparing 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 is added to the data packet in the data packet. The number verification is performed before the packet is transmitted. Only after the corresponding number of the data packet is consistent with the number of the newly received establishment packet, the USB peripheral will start to transmit the data packet, so as to ensure the corresponding relationship between the data packet and the establishment packet and avoid wrong packets. The occurrence of transmission, thereby improving the reliability of USB control transmission.
一种可能的实施方式中,USB外设在将每个建立包与其对应的编号相关联之前,还需要分配至少一个事件缓存空间。相应的,USB外设将每个建立包与其对应的编号相关联包括:将每个建立包与其对应的编号储存到同一个事件缓存空间中。In a possible implementation manner, the USB peripheral device needs to allocate at least one event buffer space before associating each establishment packet with its corresponding serial number. Correspondingly, associating each establishment packet with its corresponding serial number by the USB peripheral includes: storing each establishment packet and its corresponding serial number in the same event buffer space.
该实施方式中,USB外设将建立包储存到事件缓存空间,使得储存建立包的缓存空间 (即事件缓存空间)和储存数据包的缓存空间(即数据缓存空间)相互独立,互不干扰,进而可以改善丢包现象,进一步提高USB控制传输的可靠性。In this embodiment, the USB peripheral stores the establishment packet in the event cache space, so that the cache space for storing the establishment packet (ie the event cache space) and the cache space for storing the data packet (ie the data cache space) are independent of each other and do not interfere with each other. In turn, the packet loss phenomenon can be improved, and the reliability of USB control transmission can be further improved.
一种可能的实施方式中,USB外设分配的事件缓存空间有多个;USB外设可以根据接收建立包的时间先后顺序,将在不同时间接收的多个建立包和对应的编号,依次存储到分配的多个事件缓存空间中。In a possible implementation, there are multiple event buffer spaces allocated by the USB peripheral device; the USB peripheral device can store the multiple establishment packets and corresponding numbers received at different times in turn according to the time sequence of receiving the establishment packets. into the allocated multiple event buffer spaces.
该实施方式中,USB外设一次性分配多个事件缓存空间,可以减少USB外设分配缓存的频繁程度,即减少USB外设的软硬件交互,提升USB控制传输的效率。In this embodiment, the USB peripheral allocates multiple event buffer spaces at one time, which can reduce the frequency of allocating buffers by the USB peripheral, that is, reduce the software and hardware interaction of the USB peripheral, and improve the efficiency of USB control transmission.
一种可能的实施方式中,USB外设将每个建立包与其对应的编号储存到同一个事件缓存空间中,可以包括:若至少一个事件缓存空间中存在空闲的事件缓存空间,则将每个建立包与其对应的编号存储在空闲的事件缓存空间中;若至少一个事件缓存空间中不存在空闲的事件缓存空间,则使用在不同时间接收的N个建立包与其对应的编号,分别替换至少一个事件缓存空间中存储的历史编号由老至新的N个事件缓存空间中的建立包和对应的编号,其中,N为正整数。In a possible implementation manner, the USB peripheral stores each establishment packet and its corresponding number in the same event buffer space, which may include: if there is free event buffer space in at least one event buffer space, then store each The establishment packets and their corresponding numbers are stored in the free event buffer space; if there is no free event buffer space in at least one event buffer space, the N establishment packets received at different times and their corresponding numbers are used to replace at least one of them respectively. The history numbers stored in the event buffer space are from old to new N establishment packets and corresponding numbers in the event buffer space, where N is a positive integer.
该实施方式中,USB外设的事件缓存空间提供覆盖功能,在没有空闲的事件缓存空间时,可以使用新的建立包替换旧的建立包,如此可以保证USB外设能够在任意时刻接收和保存新的setup包,更好地避免丢包现象的发生。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 establishment packet can be used to replace the old establishment packet, thus ensuring that the USB peripheral can receive and save at any time. The new setup package can better avoid the occurrence of packet loss.
一种可能的实施方式中,若该至少一个事件缓存空间中不存在空闲的事件缓存空间,则USB外设将每个建立包与其对应的编号储存到同一个事件缓存空间中的具体方式包括以下两种:In a possible implementation manner, if there is no free event buffer space in the at least one event buffer space, the specific way for the USB peripheral to store each establishment packet and its corresponding number in the same event buffer space includes the following: Two kinds:
方式1、在至少一个事件缓存空间中不存在空闲的事件缓存空间时,USB外设清空至少一个事件缓存空间中的所有事件缓存空间;USB外设根据接收建立包的时间先后顺序,将在不同时间接收的N个建立包和对应的编号,依次存储到清空后的N个事件缓存空间中;其中,N为正整数。也即,USB外设一次性清空所有的事件缓存空间,然后将清空后的事件缓存空间作为新分配的事件缓存空间来使用。Mode 1. When there is no free event buffer space in at least one event buffer space, the USB peripheral clears all event buffer spaces in at least one event buffer space; The N establishment packets and corresponding serial numbers received by time are sequentially stored in the cleared N event buffer spaces; wherein, N is a positive integer. That is, the USB peripheral empties all the event buffer space at one time, and then uses the emptied event buffer space as the newly allocated event buffer space.
方式2、在至少一个事件缓存空间中不存在空闲的事件缓存空间时,USB外设在接收到N个建立包中的第i个建立包后,清空该至少一个事件缓存空间中的一个事件缓存空间;将第i个建立包和对应的编号储存到清空后的该一个事件缓存空间中,i取遍1到N中的正整数;其中,N为正整数。也即,USB外设在每收到一个新的建立包后,清空一个历史建立包。Mode 2. When there is no free event buffer space in at least one event buffer space, the USB peripheral device clears one event buffer in the at least one event buffer space after receiving the i-th establishment packet in the N establishment packets Space; store the i-th establishment packet and the corresponding number in the empty event buffer space, and i is a positive integer from 1 to N; wherein, N is a positive integer. That is, the USB peripheral empties a historical establishment packet after receiving a new establishment packet.
应理解,在方式2下,该一个事件缓存空间可以是该至少一个事件缓存空间中的任意一个。可选的,该一个事件缓存空间在清空前存储有:在已接收到的建立包中接收时间最早的建立包、或者在已接收到的建立包对应的编号中最小的编号或者最大的编号。It should be understood that in mode 2, the one event buffer space may be any one of the at least one event buffer space. Optionally, before clearing, the one event buffer space stores: the establishment packet with the earliest reception time among the received establishment packets, or the smallest number or the largest number among the numbers corresponding to the received establishment packets.
应理解,上述两种方式仅为示例而非限定。It should be understood that the above two manners are only examples and not limitations.
一种可能的实施方式中,USB外设在将每个建立包与其对应的编号相关联之后,以及将每个建立包对应的编号与每个建立包发起的控制传输中待传输的数据包相关联之前,还解析每个建立包的内容,获得每个建立包发起的控制传输中待传输的数据包的大小(可选的,还可以获得每个建立包发起的控制传输中待传输的数据包的内容、方向);根据待传输的数据包的大小为每个建立包发起的控制传输中待传输的数据包分配数据缓存空间。In a possible implementation manner, after the USB peripheral associates each establishment packet with its corresponding number, and associates the corresponding number of each establishment packet with the data packet to be transmitted in the control transmission initiated by each establishment packet. Before the connection, it also parses the content of each establishment packet to obtain the size of the data packet to be transmitted in the control transmission initiated by each establishment packet (optionally, the data to be transmitted in the control transmission initiated by each establishment packet can also be obtained. content and direction of the packet); according to the size of the data packet to be transmitted, a data buffer space is allocated for the data packet to be transmitted in the control transmission initiated by each setup packet.
应理解,如果该任意一笔控制传输的数据阶段为IN传输;则USB外设在根据大小为每个建立包发起的控制传输中待传输的数据包分配数据缓存空间之后,以及在任意一笔控 制传输的数据阶段开始之前,还可以根据该内容准备好该任意一笔控制传输中待传输的数据包,并将该待传输的数据包储存到数据缓存空间中。如此,可以提高USB控制传输的可靠性。It should be understood that if the data phase of this arbitrary control transmission is IN transmission; then the USB peripheral device allocates data buffer space for the data packet to be transmitted in the control transmission initiated by each establishment packet according to the size, and in any Before the start of the data phase of the control transmission, a data packet to be transmitted in any one of the control transmissions can also be prepared according to the content, and the to-be-transmitted data packet is stored in the data buffer space. In this way, the reliability of USB control transmission can be improved.
一种可能的实施方式中,USB外设将每个建立包对应的编号与每个建立包发起的控制传输中待传输的数据包相关联包括:针对任一建立包,将该任一建立包对应的编号和该任一建立包发起的控制传输中待传输的数据包储存到为该任一建立包发起的控制传输中待传输的数据包分配的数据缓存空间中。In a possible implementation, the USB peripheral associating the number corresponding to each establishment packet with the data packet to be transmitted in the control transmission initiated by each establishment packet includes: for any establishment packet, the any establishment packet is The corresponding number and the data packet to be transmitted in the control transmission initiated by any establishment packet are stored in the data buffer space allocated for the data packet to be transmitted in the control transmission initiated by any establishment packet.
以下分别介绍OUT传输、IN传输两种场景下控制传输的数据阶段的具体实现过程。The following describes the specific implementation process of the data phase of control transmission in the two scenarios of OUT transmission and IN transmission.
情况一、任意一笔控制传输的数据阶段为OUT传输(即USB主机向USB外设发送数据包)。Case 1. The data phase of any control transfer is OUT transfer (that is, the USB host sends a data packet to the USB peripheral).
USB外设接收来自USB主机的数据包;USB外设确定该任意一笔控制传输中待传输的数据包对应的编号与USB外设最新接收到的建立包对应的编号一致之后,将接收到的数据包存储到为该任意一笔控制传输中待传输的数据包分配的数据缓存空间。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 any control transfer is consistent with the number corresponding to the latest establishment packet received by the USB peripheral, the received The data packets are stored in the data buffer space allocated for the data packets to be transmitted in the arbitrary control transmission.
情况二、任意一笔控制传输的数据阶段为IN传输(即USB外设向USB主机发送数据包);Case 2. The data phase of any control transmission is IN transmission (that is, the USB peripheral sends data packets to the USB host);
在该任意一笔控制传输的数据阶段开始后,USB外设接收来自USB主机的数据包请求,该数据包请求用于请求USB外设发送数据包;在确定该任意一笔控制传输中待传输的数据包对应的编号与USB外设最新接收到的建立包对应的编号一致之后,根据该数据包请求从为该任意一笔控制传输中待传输的数据包分配的数据缓存空间中读取数据包并发送给USB主机。After the start of the data phase of the arbitrary control transfer, the USB peripheral receives a data packet request from the USB host, and the data packet request is used to request the USB peripheral to send a data packet; after determining that the arbitrary control transmission is to be transmitted After the number corresponding to the data packet is consistent with the number corresponding to the establishment packet newly received by the USB peripheral, read data from the data buffer space allocated for the data packet to be transmitted in any control transmission according to the data packet request. packet and send to the USB host.
一种可能的实施方式中,USB外设在将每个建立包对应的编号与每个建立包发起的控制传输中待传输的数据包相关联之后,还可以将每个建立包对应的编号与每个建立包发起的控制传输中的状态包相关联。进而在任意一笔控制传输的数据阶段结束之后,USB外设接收来自USB主机的状态包请求(该状态包请求用于请求USB外设发送状态包)之后,USB外设在确定任意一笔控制传输中的状态包对应的编号与USB外设最新接收到的建立包对应的编号一致之后,才向USB主机发送该任意一笔控制传输中待传输的状态包。In a possible implementation manner, after the USB peripheral device associates the number corresponding to each establishment packet with the data packet to be transmitted in the control transmission initiated by each establishment packet, it can also associate the corresponding number of each establishment packet with the data packet to be transmitted in the control transmission initiated by each establishment packet. A status packet in a control transfer initiated by each setup packet is associated. Then, after the data phase of any control transfer ends, 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), and the USB peripheral determines any control transfer. Only after the number corresponding to the status packet in the transmission is consistent with the number corresponding to the establishment packet newly received by the USB peripheral device, the status packet to be transmitted in the arbitrary control transmission is sent to the USB host.
该实施方式中,USB外设在传输状态包前也进行编号校验,且只有在状态包对应的编号与最新接收的建立包的编号一致后,USB外设才开始传输状态包,如此可以保证状态包和建立包的对应关系,避免错包传输的发生,进一步提高了USB控制传输的可靠性。In this embodiment, the USB peripheral also performs number verification before transmitting the status packet, and only after the number corresponding to the status packet is consistent with the number of the newly received establishment packet, the USB peripheral starts to transmit the status packet. This ensures that The corresponding relationship between the status packet and the establishment packet avoids the occurrence of wrong packet transmission, and further improves the reliability of USB control transmission.
第二方面,提供一种基于USB的通信装置,该装置可以应用于USB外设,USB外设与USB主机通过USB总线通信连接;该装置包括用于执行上述第一方面或第一方面任一种可能的实施方式中所述的方法的模块/单元。In a second aspect, a USB-based communication device is provided, which can be applied to a USB peripheral device, and the USB peripheral device and a USB host are communicatively connected through a USB bus; the device includes a device for implementing the first aspect or any one of the first aspects. modules/units of the method described in a possible embodiment.
示例性的,该装置可以包括:USB接口模块,用于在不同时间分别从USB主机接收建立包,其中任意一个建立包用于发起一笔对应的控制传输;接口控制模块,用于为已接收到的每个建立包生成唯一的编号,并将每个建立包与其对应的编号相关联;主控模块,用于将每个建立包对应的编号与每个建立包发起的控制传输中待传输的数据包相关联;接口控制模块,还可以用于在任意一笔控制传输的数据传输阶段开始后,确定任意一笔控制传输中待传输的数据包对应的编号与USB外设最新接收到的建立包对应的编号一致之后,控制所述USB接口模块与USB主机传输任意一笔控制传输中待传输的数据包。Exemplarily, the device may include: a USB interface module for respectively receiving establishment packets from the USB host at different times, wherein any establishment packet is used for initiating a corresponding control transmission; Each establishment packet received generates a unique number, and each establishment packet is associated with its corresponding number; the main control module is used to associate the corresponding number of each establishment packet with the control transmission initiated by each establishment packet to be transmitted. The interface control module can also be used to determine the number corresponding to the data packet to be transmitted in any control transmission and the latest received by the USB peripheral after the data transmission phase of any control transmission starts. After the numbers corresponding to the established packets are consistent, the USB interface module and the USB host are controlled to transmit any data packet to be transmitted in the control transmission.
第三方面,提供一种计算机可读存储介质,存储有计算机程序指令,当所述指令被执 行时,使如上述第一方面或第一方面任一种可能的实施方式中所述的方法被实现。A third aspect provides a computer-readable storage medium storing computer program instructions that, when executed, cause the method described in the first aspect or any possible implementation manner of the first aspect to be executed. accomplish.
第四方面,提供一种芯片,该芯片可以与存储器耦合,该芯片用于读取并执行存储器中存储的程序指令,实现如上述第一方面或第一方面任一种可能的实施方式中所述的方法。A fourth aspect provides a chip, which can be coupled with a memory, and is used for reading and executing program instructions stored in the memory, so as to achieve the above-mentioned first aspect or any possible implementation manner of the first aspect. method described.
第五方面,提供一种计算机程序产品,该计算机程序产品中存储有指令,当其在计算机上运行时,使得计算机执行上述第一方面或第一方面任一种可能的实施方式中所述的方法。In a fifth aspect, a computer program product is provided, the computer program product has instructions stored in the computer program product, when the computer program product is run on a computer, the computer causes the computer to execute the above-mentioned first aspect or any one of possible implementations of the first aspect. method.
上述第二方面至第五方面中任一方面及其任一方面中的可能设计可以达到的技术效果,请参阅上述第一方面及其对应的可能设计可以达到的技术效果描述,这里不再重复赘述。For the technical effects that can be achieved by any one of the above-mentioned second aspect to the fifth aspect and the possible designs in any of them, please refer to the description of the technical effects that can be achieved by the above-mentioned first aspect and its corresponding possible designs, which will not be repeated here. Repeat.
附图说明Description of drawings
图1为本申请实施例适用的一种USB通信场景示意图;FIG. 1 is a schematic diagram of a USB communication scenario applicable to an embodiment of the present application;
图2A~图2D为本申请实施例提供方案可以适用的USB外设4的结构示意图;2A to 2D are schematic structural diagrams of a USB peripheral device 4 to which the solution provided by the embodiment of the present application is applicable;
图3为本申请实施例提供的一种基于USB通信的方法的流程图;3 is a flowchart of a method based on USB communication provided by an embodiment of the present application;
图4为本申请实施例提供的另一种基于USB通信的方法的流程图;4 is a flowchart of another method based on USB communication provided by an embodiment of the present application;
图5为本申请实施例提供的另一种基于USB通信的方法的流程图;5 is a flowchart of another method based on USB communication provided by an embodiment of the present application;
图6为本申请实施例提供的另一种基于USB通信的方法的流程图。FIG. 6 is a flowchart of another method based on USB communication provided by an embodiment of the present application.
具体实施方式Detailed ways
USB技术通常应用在各种电子设备的数据传输中,比如电子设备可以通过USB技术连接电源进行充电,一个电子设备和另一个电子设备之间也可以通过USB技术进行数据传输,比如进行视频或图片的传输共享等。USB technology is usually used in data transmission of various electronic devices. For example, electronic devices can be charged by connecting to a power source through USB technology, and data transmission between one electronic device and another electronic device can also be performed through USB technology, such as video or picture. transmission sharing, etc.
本申请实施例提供的技术方案可以适用于各种USB通信场景,如图1所示,USB主机和USB外设可通过USB总线连接,实现USB通信。一些可能的实施例中,USB主机和USB外设可通过线缆直接相连。另一些可能的实施例中,USB主机和USB外设之间还可以设置其它器件,这里不做限制。例如,还可以设置USB集线器,USB主机和USB外设分别通过线缆连接到USB集线器,USB主机、USB外设和USB集线器都连接在USB总线上。The technical solutions provided in the embodiments of the present application can be applied to various USB communication scenarios. As shown in FIG. 1 , a USB host and a USB peripheral can be connected through a USB bus to implement USB communication. In some possible embodiments, the USB host and the USB peripheral may be directly connected by a cable. In other possible embodiments, other devices may also be set between the USB host and the USB peripheral, which is not limited here. For example, a USB hub may 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 on the USB bus.
通常,USB通信协议支持USB主机和USB外设之间具有4种传输数据的模式,分别为:控制传输、批量传输、中断传输和等时传输。其中,控制传输用于传输USB主机和USB外设之间的控制、状态、配置等信息。一般一笔完整的控制传输包含2到3个阶段:建立(setup)阶段、数据(data)阶段和状态(status)阶段。其中,数据阶段是可选的。控制传输由USB主机通过向USB外设发送setup包来发起,USB外设收到USB主机发送的setup包之后,认为新的一笔控制传输开始。Generally, the USB communication protocol supports four modes of data transfer between the USB host and the USB peripheral, namely: control transfer, bulk transfer, interrupt transfer and isochronous transfer. Among them, the control transmission is used to transmit control, status, configuration and other information between the USB host and the USB peripheral. Generally, a complete control transfer includes 2 to 3 phases: a setup phase, a data phase, and a status phase. Among them, the data stage 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 begins.
进一步的,USB主机和USB外设上的控制传输按照数据阶段data包的传输方向来划分,可以包括控制OUT传输和控制IN传输两种。其中,控制OUT传输,是指USB主机向USB外设发送data包;控制IN传输,是指USB外设向USB主机发送data包。Further, the control transmission on the USB host and the USB peripheral is divided according to the transmission direction of the data packet in the data phase, and may include two types of control OUT transmission and control IN transmission. Among them, controlling OUT transmission means that the USB host sends data packets to the USB peripheral; controlling IN transmission means that the USB peripheral sends data packets to the USB host.
目前方案中,USB主机和USB外设之间进行控制传输时,每一笔控制传输都需要USB外设提前分配一次缓存空间,才能接收该笔控制传输中的setup包,这会导致USB外设软 硬件交互繁琐,USB控制传输的效率低。此外,由于setup包和data包在USB外设上的存储位置都是在数据缓存空间,所以USB外设在缓存分配时,setup包的缓存和data包的缓存存在竞争关系,且USB外设不能同时对setup包和data包进行读/写,这些原因都可能导致缓存空间分配不及时,进而导致丢包现象的发生。例如,USB主机向USB外设发送一个setup包后,如果USB外设尚未分配好用于存储setup包的数据缓存空间,则无法存储setup包,进而发生丢弃setup包的现象;并且,由于USB外设未成功存储setup包,又会导致USB外设不能为数据阶段中待传输的data包提前分配数据缓存空间,当数据阶段开始后,USB外设自然无法成功传输data包,最终导致本笔控制传输失败。In the current solution, when a control transfer is performed between the USB host and the USB peripheral, each control transfer requires the USB peripheral to allocate a buffer space in advance in order to receive the setup packet in the control transfer, which will cause the USB peripheral to The interaction of software and hardware is cumbersome, and the efficiency of USB control transmission is low. In addition, since the storage locations of the setup package and the data package on the USB peripheral are in the data cache space, when the USB peripheral is allocated in the cache, the cache of the setup package and the cache of the data package are in competition, and the USB peripheral cannot Read/write the setup packet and the data packet at the same time. These reasons may cause the cache space to be allocated untimely, resulting in 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, the setup packet cannot be stored, and the setup packet is discarded; If the setup packet is not stored successfully, the USB peripheral cannot allocate data buffer space in advance for the data packet to be transmitted in the data phase. Transmission failed.
另一种情况,USB主机在上一笔控制传输完成前又发起一笔新的控制传输,在这种情况下,USB外设上会出现前后不同笔控制传输中的setup包、data或status包,而USB外设无法判断先后不同笔控制传输中的setup包与data/status包之间的对应关系,则会错误地将上一笔data包回复给USB主机,导致错包现象发生。In another case, the USB host initiates a new control transfer before the previous control transfer is completed. In this case, the setup packet, data or status packet in different control transfers before and after will appear on the USB peripheral. , and the USB peripheral cannot determine the correspondence between the setup packet and the data/status packet in different pen control transmissions, and will mistakenly reply the last data packet to the USB host, resulting in the occurrence of wrong packets.
为了解决上述一个或多个问题,本申请实施例提供一种基于USB的通信方案。在本申请实施例中,USB外设每接收到一个setup包后,USB外设都可以为该setup包(或者说为该setup包发起的控制传输)生成一个唯一的编号,且将编号和setup包关联;USB外设在准备data包/status包的传输时,需要将data包/status包附带(或者说关联)上与当前笔控制传输之对应的setup包的编号(即与data包/status包对应的setup包的编号),并在传输前进行编号校验;只有在data包/status包附带的编号与最新接收的setup包的编号一致后,USB外设才开始传输该data/status包,如此可以避免错包传输的发生,提高USB控制传输的可靠性。In order to solve one or more of the above problems, embodiments of the present application provide a USB-based communication solution. In this embodiment of the present application, after each USB peripheral receives a setup packet, the USB peripheral can generate a unique number for the setup packet (or for the control transmission initiated by the setup packet), and associate the number with the setup packet. Packet association; when the USB peripheral prepares the transmission of the data packet/status packet, it needs to attach (or associate) the data packet/status packet with the number of the setup packet corresponding to the current pen control transmission (that is, with the data packet/status packet). The number of the setup package corresponding to the package), and the number check is performed before transmission; only after the number attached to the data package/status package is consistent with the number of the newly received setup package, the USB peripheral starts to transmit the data/status package. , so that the occurrence of wrong packet transmission can be avoided and the reliability of USB control transmission can be improved.
在本申请实施例中,USB外设还可以将setup包存储到事件缓存空间,将data包存储到数据缓存空间,使得setup包和data包使用的缓存空间相互独立,互不干扰,如此可以改善丢包现象,提高USB控制传输的可靠性。In the embodiment of the present application, the USB peripheral can also store the setup package in the event cache space and store the data package in the data cache space, so that the cache spaces used by the setup package and the data package are independent of each other and do not interfere with each other, which can improve the Packet loss phenomenon, improve the reliability of USB control transmission.
在本申请实施例中,USB外设还可以提前分配多个用于存储setup包的缓存空间,且按接收setup包的时间顺序依次存储来自USB主机的setup包,如此可以减少USB外设分配缓存的频繁程度,即减少USB外设的软硬件交互,提升USB控制传输的效率。In this embodiment of the present application, the USB peripheral device may also allocate multiple cache spaces for storing setup packets in advance, and store the setup packets from the USB host in the order in which the setup packets are received, thus reducing the allocation of cache spaces for USB peripheral devices The frequency of USB peripherals is reduced, that is, the software and hardware interaction of USB peripherals is reduced, and the efficiency of USB control transmission is improved.
在本申请实施例中,USB外设还可以提供缓存覆盖功能,当USB外设收到setup包后,如果USB外设尚未分配好用于存储该setup包的缓存空间,而USB外设中具有存储有历史setup包(“历史setup包”指之前接收到的setup包)的缓存空间,则可以用该setup包替换掉历史setup包,如此,即便没有空余的缓存空间,USB外设也可以在任意时刻接收和保存新的setup包,避免丢包现象的发生。In the embodiment of the present application, the USB peripheral can also provide a cache coverage function. After the USB peripheral receives the setup package, if the USB peripheral has not allocated a cache space for storing the setup package, but the USB peripheral has If the cache space of the historical setup package ("historical setup package" refers to the previously received setup package) is stored, the historical setup package can be replaced with the setup package. In this way, even if there is no free cache space, the USB peripherals can Receive and save new setup packets at any time to avoid packet loss.
下面将结合附图,对本申请实施例提供的技术方案进行详细描述。The technical solutions provided by the embodiments of the present application will be described in detail below with reference to the accompanying drawings.
参见图2A,为本申请实施例提供方案可以适用的一种USB外设4的结构示意图,包括接口控制模块41、主控模块42、缓存模块43和USB接口模块44。Referring to FIG. 2A , the embodiment of the present application provides a schematic structural diagram of a USB peripheral 4 to which the solution can be applied, including an interface control module 41 , a main control module 42 , a cache module 43 and a USB interface module 44 .
其中,USB接口模块44是USB外设4的物理接口(即USB插口),能够插入USB数据线。来自USB主机的数据依次通过USB主机的USB插口、USB数据线以及USB接口模块44到达USB外设4,相应的,USB外设4发往USB主机的数据则依次最终通过USB接口模块44、USB数据线、以及USB主机的USB插口到达USB主机。USB接口模块44可以是符合USB标准规范的接口,具体可以是Mini USB接口,Micro USB接口,USB Type C接口等。USB接口模块44可以用于连接充电器为电子设备充电,也可以用于 电子设备与外围其他设备(例如USB主机)之间传输数据。也可以用于连接耳机,通过耳机播放音频。The USB interface module 44 is a physical interface (ie, a USB socket) of the USB peripheral device 4, and can be inserted into a USB data cable. The data from the USB host arrives at the USB peripheral 4 through the USB socket of the USB host, the USB data cable and the USB interface module 44 in turn. Correspondingly, the data sent by the USB peripheral 4 to the USB host finally passes through the USB interface module 44, the USB interface module 44 and the USB interface module 44. The data cable, and the USB socket of the USB host reach the USB host. The USB interface module 44 may be an interface conforming to the USB standard specification, and specifically may be a Mini USB interface, a Micro USB interface, a USB Type C interface, and the like. The USB interface module 44 can be used to connect a charger to charge the electronic device, and can also be used to transmit data between the electronic device and other peripheral devices (such as a USB host). It can also be used to connect headphones to play audio through the headphones.
接口控制模块41与USB接口模块44、USB主控模块42相连,接口控制模块41负责USB接口模块44上的数据传输控制。例如,从缓存模块43中读取待发送给USB主机的data包并通过USB接口44进行发送、将USB接口44从USB主机接收的data包存储到缓存模块43。在本申请实施例中,USB接口控制模块41除了在缓存模块43中进行数据读写之外,还新增了其他功能,例如为每笔控制传输生成对应的编号、上报事件给主控模块42、在传输data包和/或status包之前进行编号校验等。The interface control module 41 is connected to the USB interface module 44 and the USB main control module 42 , and the interface control module 41 is responsible for data transmission control on the USB interface module 44 . For example, the data packet to be sent to the USB host is read from the cache module 43 and sent through the USB interface 44 , and the data packet received by the USB interface 44 from the USB host is stored in the cache module 43 . In the embodiment of the present application, the USB interface control module 41 not only performs data reading and writing in the cache module 43 , but also adds other functions, such as generating a corresponding number for each control transmission and reporting events to the main control module 42 . , Before transmitting data packets and/or status packets, perform number check, etc.
缓存模块43具有缓存空间,该缓存空间具体可以是计算机可读写存储介质,用于存储USB外设4从USB主机接收的数据和待发送的数据。在本申请实施例中,缓存空间分为事件缓存空间和数据缓存空间两种,其中事件缓存空间可用于存储建立阶段的setup包,数据缓存空间可用于存储数据阶段的data包。The cache module 43 has a cache space, and the cache space may specifically be a computer readable and writable storage medium for storing the data received by the USB peripheral device 4 from the USB host and the data to be sent. In the embodiment of the present application, the cache space is divided into two types: event cache space and data cache space, wherein the event cache space can be used to store the setup package in the establishment stage, and the data cache space can be used to store the data package in the data stage.
其中,数据缓存空间是用于存储数据的缓存空间,例如存储数据阶段的data包,状态阶段的status包等。事件缓存空间,是用于存储事件的缓存空间,例如接收到setup包的事件,数据传输完成的事件等。需要注意的是,本申请实施例将属于数据的setup包存储在事件缓存空间,这与现有技术将setup包存储在数据缓存空间不同,如此使得接口控制模块41可以同时访问数据缓存空间和事件缓存空间,即setup包的读写、data包的读写相互独立,且两者的缓存空间分配也相互独立。The data cache space is a cache space for storing data, for example, the data package in the data storage stage, the status package in the status stage, and the like. The event buffer space is the buffer space used to store events, such as the event of receiving the setup package, the event of the completion of data transmission, etc. It should be noted that, in the embodiment of the present application, the setup package belonging to the data is stored in the event cache space, which is different from the prior art where the setup package is stored in the data cache space, so that the interface control module 41 can access the data cache space and the event at the same time. The cache space, that is, the read and write of the setup package and the read and write of the data package are independent of each other, and the allocation of the cache space of the two is also independent of each other.
主控模块42是USB外设4的主控中心,主控模块42可以执行以下一项或多项功能:控制数据阶段、状态阶段的开始/结束、分配事件缓存空间、分配数据缓存空间等。主控模块42也可以一并集成在接口控制模块41中,例如,接口控制模块41是USB控制器,且USB控制器中具有单独的MCU或MPU,则主控模块41可以通过该USB控制器中的MCU或MPU来实现。The main control module 42 is the main control center of the USB peripheral 4. The main control module 42 can perform one or more of the following functions: control the data phase, start/end the status phase, allocate event buffer space, allocate data buffer space, and the like. The main control module 42 can also be integrated in the interface control module 41. For example, if the interface control module 41 is a USB controller, and the USB controller has a separate MCU or MPU, the main control module 41 can pass the USB controller. MCU or MPU in the implementation.
主控模块42上可以集成支持USB协议的驱动软件。可选的,主控模块42上还集成客户软件(或者说上层应用)。驱动软件是一种可以使计算机和硬件进行相互通信的特殊程序,相当于硬件的接口,客户软件通过该接口控制硬件的工作。换而言之,客户软件是驱动软件上层软件,客户软件不能直接与USB外设4其它硬件进行交互,需要将指令传递给驱动软件,然后由驱动软件调用USB外设4中的硬件执行相应的功能,相应的,USB外设4中硬件上报的信号也需要先上包给驱动软件,再由驱动软件传递到客户软件。主控模块42执行的各项功能,可以通过运行驱动软件和客户软件来实现。The main control module 42 may integrate driver software supporting the USB protocol. Optionally, client software (or an upper-layer application) is also integrated on the main control module 42 . The driver software is a special program that can make the computer and hardware communicate with each other, which is equivalent to the interface of the hardware, and the client software controls the work of the hardware through the interface. In other words, the client software is the upper-layer software of the driver software. The client software cannot directly interact with other hardware of the USB peripheral 4. It needs to pass the instruction to the driver software, and then the driver software calls the hardware in the USB peripheral 4 to execute the corresponding operation. Correspondingly, the signal reported by the hardware in the USB peripheral 4 also needs to be packaged to the driver software first, and then transmitted by the driver software to the client software. Various functions performed by the main control module 42 can be implemented by running driver software and client software.
按照各模块所执行的功能,各个模块还可以被进一步细分。例如图2B所示,缓存模块43包括事件缓存空间431和数据缓存空间432等;接口控制模块41包括编号生成子模块411、事件上报子模块412、编号校验子模块413等,其中编号生成子模块411可用于为每笔控制传输生成对应的编号,事件上报子模块412可用于上报事件给主控模块42(例如将收到setup包的事件和/或生成编号的事件通知主控模块42),编号校验子模块413可用于在数据阶段和状态阶段执行编号校验。当然,实际应用中还可以有其它划分方式,本申请不做限定。Each module can be further subdivided according to the functions performed by each module. For example, as shown in FIG. 2B, the cache module 43 includes an event cache space 431 and 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 check sub-module 413, etc., wherein the number generation sub-module 413, etc. The module 411 can be used to generate a corresponding number for each control transmission, and the event reporting sub-module 412 can be used to report the event to the main control module 42 (for example, notifying the main control module 42 of the event of receiving the setup package and/or the event of the generation number) , the number check sub-module 413 can be used to perform number check in the data phase and the status phase. Of course, there may also be other division manners in practical applications, which are not limited in this application.
在具体实现中,主控模块42具体可以是,但不限于:中央处理器(Central Processing Unit,CPU),微控制器单元(Micro Controller Unit,MCU),微处理器单元(Micro Processor Unit,MPU),数字信号处理(Digital Signal Processing/Processor,DSP),现场编程门阵列 器件(Field Programmable Gate Array,FPGA)等。缓存模块43可以是,但不限于:寄存器、先进先出(First Input First Output,FIFO)存储器、静态随机存储器(Static Random Access Memory,SRAM)、动态随机存储器(Dynamic Random Access Memory,DRAM)等。接口控制模块41具体可以是,但不限于:与缓存模块43、主控模块42连接的逻辑电路。In a specific implementation, the main control module 42 may specifically be, but is not limited to: a central processing unit (Central Processing Unit, CPU), a microcontroller unit (Micro Controller Unit, MCU), a microprocessor unit (Micro Processor Unit, MPU) ), digital signal processing (Digital Signal Processing/Processor, DSP), Field Programmable Gate Array (Field Programmable Gate Array, FPGA), etc. The cache module 43 can be, but is not limited to: a register, a first-in first-out (First Input First Output, FIFO) memory, a static random access memory (Static Random Access Memory, SRAM), a dynamic random access memory (Dynamic Random Access Memory, DRAM) and the like. The interface control module 41 may specifically be, but is not limited to, a logic circuit connected to the cache module 43 and the main control module 42 .
需要说明的是,接口控制模块41、主控模块42以及缓存模块43可以集成在同一个芯片中,也可以集成在不同的芯片中,本申请不做限定。例如,图2C所示,接口控制模块41和主控模块42和缓存模块43均集成在USB控制器中,其中缓存模块43具体实现可以是集成在USB控制器中的寄存器或FIFO存储器或SRAM)等(图2C中USB接口模块44未示出)。或者例如,图2D所示,接口控制模块41和主控模块42集成在USB控制器中,而缓存模块43是SRAM或DRAM,是USB控制器外部的数据存储器的一部分(图2D中USB接口模块44未示出)。当然,接口控制模块41和主控模块42也可以分开设置,例如接口控制模块41集成在USB控制器中,而主控模块42集成在CPU中。It should be noted that the interface control module 41 , the main control module 42 and the cache module 43 may be integrated in the same chip or in different chips, which is not limited in this application. For example, as shown in FIG. 2C, the interface control module 41, the main control module 42 and the cache module 43 are all integrated in the USB controller, wherein the specific implementation of the cache module 43 may be a register or FIFO memory or SRAM integrated in the USB controller) etc. (USB interface module 44 is not shown in FIG. 2C). Or for example, as shown in FIG. 2D, the interface control module 41 and the main control module 42 are integrated in the USB controller, and the cache module 43 is SRAM or DRAM, which is a part of the data memory outside the USB controller (the USB interface module in FIG. 2D). 44 not shown). Of course, the interface control module 41 and the main control module 42 can also be provided separately, for example, the interface control module 41 is integrated in the USB controller, and the main control module 42 is integrated in the CPU.
需要说明的是,实际应用中USB外设4还可以包括比图2A~图2D所示更多的模块,例如还可以包括一个或多个集线器,本申请不一一示出。It should be noted that, in practical applications, the USB peripheral 4 may further include more modules than those shown in FIGS. 2A to 2D , for example, may also include one or more hubs, which are not shown in this application.
在本申请实施例中,USB外设4的具体可以但不限于是U盘、音响、鼠标、键盘、电脑、手机、智能穿戴设备、车载设备等等。In the embodiment of the present application, the USB peripheral device 4 may specifically be, but not limited to, a U disk, an audio, a mouse, a keyboard, a computer, a mobile phone, a smart wearable device, a vehicle-mounted device, and the like.
参见图3,为本申请实施例提供的一种基于USB通信的方法,以方法以应用在上述图2A中的USB外设中为例,方法包括:Referring to FIG. 3 , a method based on USB communication provided by an embodiment of the present application, taking the method applied to the USB peripheral device in FIG. 2A as an example, the method includes:
S600、主控模块42分配至少两个事件缓存空间,并将分配的事件缓存空间的首地址通知给接口控制模块41;S600, the main control module 42 allocates at least two event buffer spaces, and notifies the interface control module 41 of the first address of the allocated event buffer spaces;
图3中以分配两个事件缓存空间为例,实际应用中还可以更多,例如三个、四个、五个等,本申请不做限制。In FIG. 3 , the allocation of two event buffer spaces is taken as an example. In practical applications, there may be more, such as three, four, five, etc., which are not limited in this application.
接口控制模块41后期在存储setup包时,可从该首地址开始,依次往后存储setup包。When the interface control module 41 stores the setup packet in the later stage, it can start from the first address and store the setup packet in sequence.
S601、USB接口模块44从USB主机接收第一setup包,第一setup包用于指示第一笔控制传输开始;接口控制模块41将第一setup包存储到第一事件缓存空间;S601, the USB interface module 44 receives the first setup package from the USB host, and the first setup package is used to indicate the start of the first control transmission; the interface control module 41 stores the first setup package in the first event buffer space;
需要说明的是,由于附图篇幅限制,为便于清晰地示出附图,图3中未示出USB接口模块44,但实际接口控制模块41和USB主机之间还有USB接口模块44,USB外设最终是通过USB接口模块44向外部设备(如USB主机)发送数据以及接收外部设备发送来的数据。It should be noted that, due to the space limitation of the drawings, in order to clearly show the drawings, the USB interface module 44 is not shown in FIG. 3, but there is a USB interface module 44 between the actual interface control module 41 and the USB host. The peripheral device finally sends data to the external device (such as a USB host) through the USB interface module 44 and receives the data sent by the external device.
S602、接口控制模块41上报事件通知主控模块42;S602, the interface control module 41 reports an event to notify the main control module 42;
具体的,接口控制模块41上报事件的具体方式可以是产生硬件中断的方式,例如产生setup中断信号。Specifically, the specific manner in which the interface control module 41 reports the event may be a manner of generating a hardware interrupt, for example, generating a setup interrupt signal.
S603、主控模块42收到通知后,从第一事件缓存空间读取最后(或者说最新)的一个setup包的内容,例如读取并解析第一setup包的内容,获知待传输的data包的大小、内容和方向;S603. After receiving the notification, the main control module 42 reads the content of the last (or the latest) setup package from the first event buffer space, for example, reads and parses the content of the first setup package, and learns the data package to be transmitted size, content and orientation;
S604、主控模块42根据待传输的data包的大小,分配第一数据缓存空间,并将第一数据缓存空间的首地址通知给接口控制模块41;S604, the main control module 42 allocates the first data buffer space according to the size of the data packet to be transmitted, and notifies the interface control module 41 of the first address of the first data buffer space;
应理解,如果是控制IN传输,则主控模块42还根据待传输的data包的内容准备好该data包,将该data包存入第一数据缓存空间中。储存方式可以是从首地址开始依次往后储 存。It should be understood that, if it is to control IN transmission, 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 stored sequentially from the first address.
S605、data包传输;S605, data packet transmission;
如果是控制OUT传输,则USB接口模块44接收来自USB主机的data包;接口控制模块41将USB接口模块44接收到的data包存入第一数据缓存空间中;如果是控制IN传输,则USB接口模块44接收来自USB主机的data包请求,接口控制模块41从第一数据缓存空间中取出data包并通过USB接口模块44发送给USB主机。If it is the control OUT transmission, 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 the control IN transmission, the USB interface The interface module 44 receives the 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 the data packet to the USB host through the USB interface module 44 .
S606、status包传输;S606, status packet transmission;
主控模块42向接口控制模块41下发status包;在USB接口模块44接收来自USB主机的status包请求之后,接口控制模块41通过USB接口模块44向USB主机返回status包。The main control module 42 sends the status packet to the interface control module 41 ; after the USB interface module 44 receives the status packet request from the USB host, the interface control module 41 returns the status packet to the USB host through the USB interface module 44 .
主控模块42向接口控制模块41下发status包可以有多种,本申请不做限制。例如,直接将status包传递给接口控制模块41;或者例如,将status包存入第一数据缓存空间,然后接口控制模块41从第一数据缓存空间读取status包;或者例如,将status包存入其它数据缓存空间,然后接口控制模块41从其它数据缓存空间读取status包。The main control module 42 can deliver a status packet to the interface control module 41 in various ways, which is not limited in this application. For example, the status packet is directly transferred to the interface control module 41; or, for example, the status packet is stored in the first data buffer space, and then the interface control module 41 reads the status packet from the first data buffer space; or, for example, the status packet is stored in the first data buffer space. into other data buffer spaces, and then the interface control module 41 reads the status packet from the other data buffer spaces.
至此,第一笔控制传输完成。So far, the first control transmission is completed.
S601’、USB接口模块44从USB主机接收第二setup包,第二setup包用于指示第二笔控制传输开始;接口控制模块41将第二setup包存储到第二事件缓存空间;S601', the USB interface module 44 receives the second setup package from the USB host, and the second setup package is used to indicate the start of the second control transmission; the interface control module 41 stores the second setup package in the second event buffer space;
S602’、接口控制模块41上报事件通知主控模块42;S602', the interface control module 41 reports the event to notify the main control module 42;
S603’、主控模块42收到通知后,从第二事件缓存空间读取并解析最后(或者说最新)的一个setup包的内容,例如读取并解析第二setup包的内容,获得待传输的data包的大小、内容和方向;S603'. After receiving the notification, the main control module 42 reads and parses the content of the last (or the latest) setup package from the second event buffer space, for example, reads and parses the content of the second setup package to obtain the content to be transmitted. The size, content and orientation of the data package;
S604’、主控模块42根据待传输的data包的大小,分配第二数据缓存空间,并将第二数据缓存空间的首地址通知给接口控制模块41;S604', the main control module 42 allocates the second data buffer space according to the size of the data packet to be transmitted, and notifies the interface control module 41 of the first address of the second data buffer space;
同理,如果是控制IN传输,则主控模块42还根据待传输的data包的内容准备好该data包,将该data包存入第二数据缓存空间中。Similarly, if it is to control IN transmission, 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 second data buffer space.
S605’、data包传输;S605', data packet transmission;
如果是控制OUT传输,则USB接口模块44接收来自USB主机的data包,接口控制模块41将USB接口模块44接收到的data包存入第二数据缓存空间中;如果是控制IN传输,则USB接口模块44接收来自USB主机的data包请求,接口控制模块41从第二数据缓存空间中取出data包并通过USB接口模块44发送给USB主机。If it is the control OUT transmission, 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 buffer space; if it is the control IN transmission, the USB interface The interface module 44 receives the data packet request from the USB host, and the interface control module 41 retrieves the data packet from the second data buffer space and sends the data packet to the USB host through the USB interface module 44 .
S606’、status包传输;S606', status packet transmission;
主控模块42向接口控制模块41下发status包;USB接口控制模块41在USB接口模块44接收来自USB主机的status包请求之后,通过USB接口控制模块41向USB主机返回status包。The main control module 42 sends the status packet to the interface control module 41; the USB interface control module 41 returns the status packet to the USB host through the USB interface control module 41 after the USB interface module 44 receives the status packet request from the USB host.
至此,第二笔控制传输完成。So far, the second control transmission is completed.
应理解,图3是以提前分配两个事件缓存空间为例,所以图3仅示出了两笔控制传输的过程。在实际应用中,USB外设可以提前分配更多的事件缓存空间,所以在第二笔控制传输完成后,还可以继续执行类似于上述S601~S606或S601’~S606’的流程,这里不一一示出。It should be understood that FIG. 3 is an example of allocating two event buffer spaces in advance, so FIG. 3 only shows the process of two control transmissions. In practical applications, USB peripherals can allocate more event buffer space in advance, so after the second control transfer is completed, the process similar to the above S601~S606 or S601'~S606' can continue to be executed, which is different here. one shows.
通过以上可知,本申请实施例中的USB外设可以一次性分配多个事件缓存空间,用于 存储多笔控制传输的setup包,能够避免每一笔控制传输都需要USB外设提前分配一次缓存空间的技术缺陷,减少USB外设软硬件交互,提高USB控制传输的效率。并且,USB外设用事件缓存空间存储Setup包,用数据缓存空间存储Data包,而事件缓存空间和数据缓存空间相互独立,互不干扰,所以还能够改善数据缓存空间分配不及时所导致的丢包现象,进一步提高USB控制传输的可靠性。It can be seen from the above that the USB peripheral device in the embodiment of the present application can allocate multiple event buffer spaces at one time to store the setup packets of multiple control transmissions, which can avoid the need for the USB peripheral device to allocate a buffer in advance for each control transmission. The technical defects of space reduce the interaction between software and hardware of USB peripherals, and improve the efficiency of USB control transmission. In addition, USB peripherals use the event cache space to store Setup packets and the data cache space to store Data packets. 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 loss caused by the untimely allocation of the data cache space. The packet phenomenon further improves the reliability of USB control transmission.
应理解,上述图3所示的实施例中用于存储setup包的缓存空间是以事件缓存空间为例,在具体实施时,也可以替换为数据缓存空间,即在数据缓存空间中为setup包分配多个缓存空间,同样也能起到减少USB外设软硬件交互,提高USB控制传输的效率的效果。It should be understood that the cache space used to store the setup package in the above-mentioned embodiment shown in FIG. 3 takes the event cache space as an example, and can also be replaced with a data cache space during specific implementation, that is, the setup package in the data cache space. Allocating multiple cache spaces can also reduce the interaction between software and hardware of USB peripherals and improve the efficiency of USB control transmission.
参见图4,为本申请实施例提供的另一种基于USB通信的方法,以方法以应用在上述图2A中的USB外设中为例,方法包括:Referring to FIG. 4 , another method based on USB communication provided by an embodiment of the present application, taking the method applied to the USB peripheral in the above-mentioned FIG. 2A as an example, the method includes:
S700、主控模块42分配一个或多个事件缓存空间,并将该一个或多个事件缓存空间的首地址通知给接口控制模块41;S700, the main control module 42 allocates one or more event buffer spaces, and notifies the interface control module 41 of the first address of the one or more event buffer spaces;
S701、USB接口模块44从USB主机接收setup包,该setup包用于指示一笔控制传输开始;接口控制模块41判断事件缓存空间中是否存在空闲的事件缓存空间,若存在,则将S701中USB接口模块44接收的setup包存储在空闲的事件缓存空间中;否则,使用S701中USB接口模块44接收的setup包替换非空闲的事件缓存空间中的setup包(即历史setup包)。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 transmission; the interface control module 41 determines whether there is a free event buffer space in the event buffer space, and if there is, the USB in S701 The setup packet received by the interface module 44 is stored in the free event buffer space; otherwise, the setup packet received by the USB interface module 44 in S701 is used to replace the setup packet (ie the historical setup packet) in the non-idle event buffer space.
需要说明的是,由于附图篇幅限制,为便于清晰地示出附图,图4中未示出USB接口模块44,但实际接口控制模块41和USB主机之间还有USB接口模块,USB外设最终是通过USB接口模块44向外部设备(如USB主机)发送数据以及接收外部设备发送来的数据。It should be noted that, due to the space limitation of the drawings, in order to clearly show the drawings, the USB interface module 44 is not shown in FIG. 4 , but there is a USB interface module between the actual interface control module 41 and the USB host. It is assumed that the USB interface module 44 finally sends data to an external device (such as a USB host) and receives data from the external device.
以下对主控模块42分配一个事件缓存空间和多个事件缓存空间的两种情况,分别介绍USB接口模块44储存setup包的方法。The following describes the methods for the USB interface module 44 to store the setup package in the two cases of allocating one event buffer space and multiple event buffer spaces to the main control module 42 .
情况一、主控模块42只分配一个事件缓存空间,则接口控制模块41直接将接收到第一个setup包储存到该事件缓存空间;从接收到的第二个setup包开始,在事件缓存空间中使用新接收的setup包替换上一个setup包,如此可保证最新接收的setup包不丢包。Situation 1. The main control module 42 only allocates one event buffer space, then the interface control module 41 directly stores the received first setup packet in the event buffer space; starting from the received second setup packet, in the event buffer space Replace the previous setup packet with the newly received setup packet, so as to ensure that the newly received setup packet is not lost.
例如,第一笔控制传输开始后,USB接口模块44收到第一setup包,接口控制模块41将第一setup包存入该事件缓存空间;第二笔控制传输开始后,USB接口模块44收到第二setup包,但由于事件缓存空间已经存储了第一笔控制传输中的第一setup包,所以接口控制模块41使用第二setup包替换第一setup包;第三笔控制传输开始后,USB接口模块44收到第三setup包,但由于事件缓存空间已经存储了第二笔控制传输中的第二setup包,所以接口控制模块41使用第三setup包替换第二setup包……For example, after the first control transmission 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 transmission starts, the USB interface module 44 receives the first setup packet. to the second setup package, but since the event buffer space has already stored the first setup package in the first control transfer, the interface control module 41 replaces the first setup package with the second setup package; 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 transmission, the interface control module 41 replaces the second setup packet with the third setup packet...
情况二、主控模块42分配了多个事件缓存空间,接口控制模块41在存储setup包时,从该多个事件缓存空间的第一个事件缓存空间开始,依次往后存储setup包。In case 2, the main control module 42 allocates multiple event buffer spaces, and when the interface control module 41 stores the setup package, it starts from the first event buffer space of the multiple event buffer spaces, and sequentially stores the setup package.
以主控模块42分配三个事件缓存空间(即第一事件缓存空间、第二事件缓存空间、第三事件缓存空间)为例:第一笔控制传输开始后,USB接口模块44收到第一setup包,接口控制模块41将第一setup包存入第一事件缓存空间;第二笔控制传输开始后,USB接口模块44收到第二setup包,接口控制模块41将第二setup包存入第二事件缓存空间;第三笔控制传输开始后,USB接口模块44收到第三setup包,接口控制模块41将第三setup 包存入第三事件缓存空间。Taking the main control module 42 allocating three event buffer spaces (ie, the first event buffer space, the second event buffer space, and the third event buffer space) as an example: after the first control transmission starts, the USB interface module 44 receives the first setup package, the interface control module 41 stores the first setup package in the first event buffer space; after the second control transmission starts, the USB interface module 44 receives the second setup package, and the interface control module 41 stores the second setup package in the The second event buffer space; after the third control transmission starts, 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.
进一步的,当该多个事件缓存空间的最后一个事件缓存空间用尽时,可使用新的setup包替换旧的setup包,以保证新setup包不丢包。Further, when the last event buffer space of the multiple event buffer spaces is exhausted, the old setup package may be replaced with a new setup package to ensure that the new setup package does not lose packets.
例如,沿用上述三个事件缓存空间的例子,当第四笔控制传输开始后,USB接口模块44收到第四setup包,此时所有的事件缓存空间(即第一事件缓存空间、第二事件缓存空间、第三事件缓存空间)都已经存储setup包,则接口控制模块41可以使用新的setup包(即第四setup包)替换旧的setup包。For example, following the example of the above three event buffer spaces, when the fourth control transmission starts, the USB interface module 44 receives the fourth setup packet, and all the event buffer spaces (ie the first event buffer space, the second event buffer space, the second event buffer space The buffer space and the third event buffer space) have already stored the setup package, then the interface control module 41 can replace the old setup package with the new setup package (ie, the fourth setup package).
本申请不限制接口控制模块41使用新的setup包替换旧的setup包的具体方式。以下例举其中两种可能的方式:The present application does not limit the specific manner in which the interface control module 41 uses the new setup package to replace the old setup package. The following are examples of two possible ways:
方式1、“即用即换”。Method 1, "use and replace".
USB接口模块44在收到一个新的setup包之后,如果接口控制模块41发现事件缓存空间用尽,则接口控制模块41清空该多个事件缓存空间中的一个事件缓存空间,将新的setup包和对应编号存储到清空后的该一个事件缓存空间中。可选的,该一个事件缓存空间在清空前存储有:在已接收到(或者说已储存)的setup包中接收时间最早或最晚的setup包、或者在已接收到(或者说已储存)的setup包对应的编号中最小的编号或者最大的编号。应理解,如果是按照从小到大的顺序编号,则接收时间最早的setup包是编号最小的setup包,反之,如果是按照从大到小的顺序编号,则接收时间最早的setup包是编号最大的setup包。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 event buffer space among the plurality of event buffer spaces, and stores the new setup packet and the corresponding number are stored in the emptied event buffer space. Optionally, the one event buffer space is stored before clearing: among the received (or stored) setup packages, the setup package with the earliest or latest time is received, or in the received (or stored) setup package. The smallest or largest number among the numbers corresponding to the setup package. It should be understood that if it is numbered in descending order, the setup package with the earliest reception time is the setup package with the smallest number. On the contrary, if it is numbered in descending order, the setup package with the earliest reception time is the one with the largest number. the setup package.
以第四setup包为例,且以该一个事件缓存空间在清空前存储有在已接收到的setup包中接收时间最早的setup包为例,则可以使用第四setup包替换第一事件缓存空间中之前储存的第一setup包。Taking the fourth setup package as an example, and taking the event cache space storing the setup package with the earliest reception time among the received setup packages as an example, the fourth setup package can be used to replace the first event cache space. The first setup package previously stored in .
方式2、“一次性清空”。Method 2, "one-time clearing".
USB接口模块44在收到一个新的setup包之后,如果接口控制模块41发现事件缓存空间用尽,则接口控制模块41一次性清空所有事件缓存空间,然后开始新的轮询,依次往后储存新的setup包和对应编号。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 all the event buffer space at one time, then starts a new polling, and stores the storage in sequence. New setup package and corresponding number.
以第四setup包为例,接口控制模块41清空所有的事件缓存空间(即第一事件缓存空间、第二事件缓存空间、第三事件缓存空间),将第四setup包存入第一事件缓存空间,之后如果又有新的setup包,则又按序存入第二事件缓存空间。Taking the fourth setup package as an example, the interface control module 41 clears all event buffer spaces (ie, the first event buffer space, the second event buffer space, and the third event buffer space), and stores the fourth setup package in the first event buffer. After that, if there is a new setup package, it will be stored in the second event buffer space in sequence.
S702、接口控制模块41上报事件通知主控模块42;S702, the interface control module 41 reports an event to notify the main control module 42;
S703、主控模块42收到通知后,从事件缓存空间读取并解析最后(或者说最新)一个setup包的内容,例如是第一setup包的内容,获得待传输的data包的大小、内容和方向;S703. After receiving the notification, the main control module 42 reads and parses the content of the last (or latest) setup package, such as the content of the first setup package, from the event buffer space, and obtains the size and content of the data package to be transmitted. and direction;
S704、主控模块42根据待传输的data包的大小分配数据缓存空间,并将数据缓存空间的首地址通知给接口控制模块41;S704, the main control module 42 allocates the data buffer space according to the size of the data packet to be transmitted, and notifies the interface control module 41 of the first address of the data buffer space;
S705、data包传输;S705, data packet transmission;
S706、status包传输。S706, status packet transmission.
S702~S706的具体实现过程可以参考上文S602~S606或S602’~S606’,这里不再赘述。For the specific implementation process of S702 to S706, reference may be made to the above S602 to S606 or S602' to S606', which will not be repeated here.
通过上述可知,本申请实施例中USB外设缓存提供覆盖功能,当USB外设收到setup包后,如果USB外设尚未分配好用于存储该setup包的事件缓存空间,则可以将之前控制传输过程中分配的事件缓存空间清空后来存储新的setup包,如此,即便没有空余的缓存空间,USB外设也可以在任意时刻接收和保存新的setup包,进而避免丢包现象的发生。 并且,USB外设用事件缓存空间存储Setup包,用数据缓存空间存储Data包,而事件缓存空间和数据缓存空间相互独立,互不干扰,所以还能够数据缓存空间分配不及时所导致的丢包现象,进一步提高USB控制传输的可靠性。It can be seen from the above that in the embodiment of the present application, the USB peripheral cache provides an overlay function. After the USB peripheral receives the setup package, if the USB peripheral has not allocated the event cache space for storing the setup package, the previously controlled The event buffer space allocated during the transmission process is emptied and then new setup packets are stored. In this way, even if there is no free buffer space, the USB peripheral can receive and save new setup packets at any time, thereby avoiding the occurrence of packet loss. In addition, USB peripherals use the event buffer space to store Setup packets and the data buffer space to store Data packets. The event buffer space and the data buffer space are independent of each other and do not interfere with each other, so it is also possible to lose packets caused by untimely allocation of data buffer space. phenomenon, and further improve the reliability of USB control transmission.
应理解,上述图4所示的实施例中用于存储setup包的缓存空间是以事件缓存空间为例,在具体实施时,也可以替换为数据缓存空间,即用于存储setup包的数据缓存空间用尽时,在该数据缓存空间中用新setup包替换旧setup包,也可以实现USB外设在任意时刻接收和保存新的setup包,避免丢包现象的效果。It should be understood that the cache space used to store the setup package in the embodiment shown in the above-mentioned FIG. 4 is an example of the event cache space. In specific implementation, it can also be replaced with a data cache space, that is, a data cache used to store the setup package. When the space is exhausted, the old setup package is replaced with a new setup package in the data cache space, and the USB peripheral device can also receive and save a new setup package at any time to avoid the effect of packet loss.
参见图5,为本申请实施例提供的另一种基于USB通信的方法,以方法以应用在上述图2A中的USB外设中为例,方法包括:Referring to FIG. 5 , another method based on USB communication provided by an embodiment of the present application, taking the method applied to the USB peripheral in the above-mentioned FIG. 2A as an example, the method includes:
S801、主控模块42分配至少一个事件缓存空间;S801, the main control module 42 allocates at least one event buffer space;
如图5所示,每个事件缓存空间包括一个用于存储setup包的第一子空间S431A和一个用于存储该setup包的编号的第二子空间S431B。应理解,图5仅示出了一个事件缓存空间,实际可以有更多。As shown in FIG. 5 , each event buffer space includes a first subspace S431A for storing the setup package and a second subspace S431B for storing the number of the setup package. It should be understood that FIG. 5 only shows one event buffer space, and there may actually be more.
S802、USB接口模块44接收来自USB主机的第一setup包,第一setup包用于指示USB主机和USB外设开始一笔控制传输;接口控制模块41为第一setup包生成唯一编号,如第一编号,然后将第一编号和第一setup包关联。S802, the USB interface module 44 receives the first setup package from the USB host, and the first setup package is used to instruct the USB host and the USB peripheral to start a control transmission; the interface control module 41 generates a unique number for the first setup package, such as the first setup package. A number, and then associate the first number with the first setup package.
需要说明的是,由于附图篇幅限制,为便于清晰地示出附图,图5中未示出USB接口模块44,但实际接口控制模块41和USB主机之间还有USB接口模块,USB外设最终是通过USB接口模块44向外部设备(如USB主机)发送数据以及接收外部设备发送来的数据。It should be noted that, due to the space limitation of the accompanying drawings, in order to clearly show the accompanying drawings, the USB interface module 44 is not shown in FIG. 5 , but there is a USB interface module between the actual interface control module 41 and the USB host. It is assumed that the USB interface module 44 finally sends data to an external device (such as a USB host) and receives data from the external device.
具体的,接口控制模块41将第一编号和第一setup包关联,可以是接口控制模块41将第一编号和第一setup包存储在同一个事件缓存空间中。例如,将第一setup包存储在第一事件缓存空间的第一子空间中,将第一编号存储在第一事件缓存空间的第二子空间中。Specifically, the interface control module 41 associates the first number with the first setup packet, which may be 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 package 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.
一种可能的设计中,主控模块42只分配一个事件缓存空间。这种情况下,事件缓存空间可以提供覆盖功能,即新接收的setup包及其编号可以覆盖历史setup包(即之前接收的setup包)及其编号。具体实现可以参考上述S701中情况一的相关介绍,这里不再赘述。In a possible design, the main control module 42 allocates only one event buffer space. In this case, the event buffer space can provide an overlay function, that is, the newly received setup package and its number can overwrite the historical setup package (that is, the previously received setup package) and its number. For specific implementation, reference may be made to the relevant introduction of the first case in S701, which will not be repeated here.
另一种可能的设计中,主控模块42分配多个事件缓存空间,则接口控制模块41可以根据接收setup包的时间先后顺序,将在多笔控制传输中接收的多个setup包,依次存储到所述多个事件缓存空间中。这种情况下,当提前分配的事件缓存空间用尽时,即不存在空闲的事件缓存空间时,事件缓存空间也可以提供覆盖功能,即接口控制模块41可以使用USB接口模块44新接收的setup包及其编号替换事件缓存空间中储存的历史setup包(即USB接口模块44之前接收的setup包)及其编号。具体实现可以参考上述S701中情况二的相关介绍,这里不再赘述。In another possible design, the main control module 42 allocates multiple event buffer spaces, then the interface control module 41 can store the multiple setup packets received in the multiple control transmissions in sequence according to the time sequence of receiving the setup packets. into the multiple event buffer spaces. In this case, when the event buffer space allocated in advance is used up, that is, when there is no free event buffer space, the event buffer space can also provide an overlay function, that is, the interface control module 41 can use the setup newly received by the USB interface module 44. The package and its number replace the historical setup package (ie, the setup package previously received by the USB interface module 44 ) and its number stored in the event buffer space. For specific implementation, reference may be made to the relevant introduction of the second situation in the above S701, and details are not repeated here.
S803、接口控制模块41上报事件通知主控模块42,触发主控模块42读取编号和setup包;S803, the interface control module 41 reports the event to notify the main control module 42, and triggers the main control module 42 to read the serial number and the setup package;
具体的,接口控制模块41上报事件的具体方式可以是产生硬件中断的方式,例如产生setup中断信号。Specifically, the specific manner in which the interface control module 41 reports the event may be a manner of generating a hardware interrupt, for example, generating a setup interrupt signal.
S804、主控模块42收到通知后,从事件缓存空间中读取最后(或者说最新)的一个setup包及其编号,解析读取的setup包,获得待传输的data包的大小、内容和方向,图5 中以读取到第一setup包和第一编号为例;S804. After receiving the notification, the main control module 42 reads the last (or the latest) setup package and its number from the event buffer space, parses the read setup package, and obtains the size, content and number of the data package to be transmitted. direction, the first setup package and the first number are read as an example in Figure 5;
S805、主控模块42根据第一data包的大小分配指定大小的数据缓存空间,该数据缓存空间用于存储待传输的第一data包;将待传输的第一data包与所述第一编号相关联;S805. The main control module 42 allocates a data buffer space of a specified size according to the size of the first data packet, and the data buffer space is used to store the first data packet to be transmitted; compare the first data packet to be transmitted with the first serial number Associated;
主控模块42将待传输的第一data包与所述第一编号相关联的方式可以有多种,本申请不做限制。这里例举其中几种可能的方式:方式1,主控模块42将第一编号储存到为第一data包分配的数据缓存空间中,例如图5所示,数据缓存空间包括一个用于存储data包的第三子空间S432A和一个用于存储编号的第四子空间S432B,主控模块将第一编号存储在第四子空间S432B中。方式2、主控模块42直接将第一编号下发给接口控制模块41,用于指示当前分配的数据缓存空间是为第一data包分配的。方式3、主控模块42将第一编号存入其它数据缓存空间,然后关联其它数据缓存空间与用于存储待传输的第一data包的数据缓存空间。The main control module 42 may associate the first data packet to be transmitted with the first number in various manners, which are not limited in this application. Here are several possible ways: way 1, the main control module 42 stores the first number in the data buffer space allocated for the first data packet. For example, as shown in FIG. 5, the data buffer space includes a data buffer for storing data A third subspace S432A of the package and a fourth subspace S432B for storing numbers, and the main control module stores the first number in the fourth subspace S432B. Mode 2: The main control module 42 directly issues the first number to the interface control module 41 to indicate that the currently allocated data buffer space is allocated for the first data packet. Mode 3: The main control module 42 stores the first number in the other data buffer space, and then associates the other data buffer space with the data buffer space for storing the first data packet to be transmitted.
应理解,如果是控制IN传输,则主控模块42还需要准备待发送给USB主机的data包,并储存到数据缓存空间的第三子空间S432A中。It should be understood that, if it is a control IN transmission, the main control module 42 also needs to prepare a data packet to be sent to the USB host, and store it in the third subspace S432A of the data buffer space.
还需要说明的是,在USB接口模块44接收第一setup包之后,以及由第一setup包发起的那一笔控制传输的数据阶段开始之前(即步骤S802之后、以及步骤S806之前),USB接口模块44可能接收新的setup包。在此期间USB接口模块44是否会接收到新的setup包,取决于USB主机是否发送新的setup包。如果USB接口模块44又接收到新的setup包,则接口控制模块41针对新的setup包也执行上述S802的流程。例如,步骤S802之后、以及步骤S806之前,USB接口模块44接收来自USB主机的第二setup包,第二setup包用于指示USB主机和USB外设开始另一笔控制传输;接口控制模块41为第二setup包生成唯一编号,如第二编号,将第二编号和第二setup包关联地存储在S801中分配的事件缓存空间中;其中第一编号与第二编号不同。It should also be noted that, after the USB interface module 44 receives the first setup packet, and before the start of the data phase of the control transmission initiated by the first setup packet (that is, after step S802 and before step S806), the USB interface Module 44 may receive new setup packets. During this period, whether the USB interface module 44 will receive a new setup packet 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-mentioned process of S802 for the new setup packet. For example, after step S802 and before step S806, the USB interface module 44 receives the second setup packet from the USB host, and the second setup packet is used to instruct the USB host and the USB peripheral to start another control transmission; the interface control module 41 is The second setup package generates a unique number, such as the second number, and the second number and the second setup package are associated and stored in the event buffer space allocated in S801; wherein the first number is different from the second number.
S806、data包传输;S806, data packet transmission;
当第一setup包发起的该笔控制传输的数据阶段开始后,接口控制模块41确定USB接口模块44最新接收到setup包的编号与待传输的data包的编号一致后,才与USB主机传输data包;如果不一致,则不传输data包。When the data phase of the control transmission initiated by the first setup packet starts, the interface control module 41 determines that the number of the setup packet newly received by the USB interface module 44 is consistent with the number of the data packet to be transmitted, and then transmits the data to the USB host. packet; if inconsistent, the data packet is not transmitted.
如果该笔控制传输的数据阶段为OUT传输,则数据传输过程可以包括:USB接口模块44接收来自USB主机的data包;若事件缓存空间中最新存储的编号与数据缓存空间中的编号一致,则接口控制模块41将USB接口模块44接收到的data包存储到数据缓存空间,否则丢弃该data包。If the data stage of the control transmission is OUT transmission, the data transmission process may include: 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 data buffer space, otherwise the data packet is discarded.
如果该笔控制传输的数据阶段为IN传输,则数据传输过程可以包括:USB接口模块44接收来自USB主机的data包请求;若接口控制模块41确定事件缓存空间中最新存储的编号与数据缓存空间中的编号一致,则接口控制模块41从数据缓存空间中读取data包并控制USB接口模块44将data包发送给USB主机;否则不返回data包。If the data phase of the control transmission is IN transmission, the data transmission process may include: the USB interface module 44 receives a data packet request from the USB host; if the interface control module 41 determines the latest stored number in the event buffer space and the data buffer space If the numbers are the same, 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, the data packet is not returned.
一个具体的示例:USB主机在向USB外设发送setup包-1之后,又紧接着向USB外设发送一个setup包-2,则基于上述方法,事件缓存空间中最新的编号为setup包-2的编号(假设为第二编号);此时,如果主控模块42准备的是data包-1的传输(即数据缓存空间中的编号是setup包-1的编号,如第一编号,数据缓存空间中的data包是data包-1),则USB主机发来data包-2的请求后,接口控制模块41会判断出数据缓存空间中的编号(即第一编号)和事件缓存空间中的最新编号(即第二编号)不一致,所以不会控制USB接口 模块44将data包-1发送给USB主机,如此可避免data包错包传输。A specific example: After the USB host sends a setup packet-1 to the USB peripheral, it immediately sends a setup packet-2 to the USB peripheral. Based on the above method, the latest number in the event buffer space is the setup packet-2. The number (assuming it is the second number); at this time, if the main control module 42 prepares the transmission of the data packet-1 (that is, the number in the data buffer space is the number of the setup packet-1, such as the first number, the data buffer The data packet in the space is data packet-1), then after the USB host sends a request for data packet-2, the interface control module 41 will determine the number in the data buffer space (ie the first number) and the number in the event buffer space. The latest number (ie, the second number) is inconsistent, so the USB interface module 44 will not be controlled to send the data packet-1 to the USB host, so that the wrong packet transmission of the data packet can be avoided.
S807、status包传输。S807, status packet transmission.
具体的,主控模块42在向接口控制模块41下发status包,接口控制模块41控制USB接口模块44发送status包给USB主机。Specifically, the main control module 42 is sending 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.
可选的,status包传输过程中也可以加入编号校验过程,例如:主控模块42在向接口控制模块41下发status包之前,主控模块42可以将S804中读取的第一编号作为status包的编号,然后再将此编号和status包一并下发给接口控制模块41;相应的,USB接口模块44接收到来自所述USB主机的status包请求之后,接口控制模块41可以校验status包的编号和USB接口模块44最新接收到的setup包的编号是否一致,如果一致,则接口控制模块41控制USB接口模块44发送status包给USB主机,否则接口控制模块41不控制USB接口模块44发送status包,如此可避免status包错包传输。Optionally, a number verification process can also be added to the status packet transmission process. 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 first number. The number of the status packet, and then the number and the status packet are sent to the interface control module 41; correspondingly, after the USB interface module 44 receives the status packet request from the USB host, the interface control module 41 can check the Whether the number of the status packet is consistent with the number of the setup packet newly received 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 Send the status packet, so as to avoid the wrong packet transmission of the status packet.
至此,本次控制传输完成。So far, this control transmission is completed.
USB外设在任意时刻收到一个新的Setup包后,只需重复执行上述步骤S802-S806即可。After the USB peripheral receives a new Setup package at any time, it only needs to repeat the above steps S802-S806.
通过以上可知,本申请实施例中的USB外设针对每个setup包生成唯一的编号,在准备data/status包的传输时,将data/status包附带上与当前笔控制传输之对应的setup包的编号,并在传输前进行编号校验,只有在data/status包对应的编号与最新接收的setup包的编号一致后,USB外设才开始传输data/status包,如此可以避免错包传输的发生,进而提高USB控制传输的可靠性;并且,事件缓存空间和数据缓存空间相互独立,互不干扰,所以还能够改善数据缓存空间分配不及时所导致的丢包现象,进一步提高USB控制传输的可靠性;不仅如此,USB外设缓存还可以提供覆盖功能,可以保证USB外设在任意时刻接收和保存新的setup包,进而避免丢包现象的发生;另外,USB外设还可以一次性分配多个事件缓存空间,可以减少USB外设软硬件交互,进而提高USB控制传输的效率。It can be seen from the above that the USB peripheral device in the embodiment of the present application generates a unique number for each setup packet, and when preparing the transmission of the data/status packet, the data/status packet is attached with the setup packet corresponding to the current pen control 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 will start to transmit the data/status packet, so as to avoid the transmission of wrong packets. In addition, the event buffer space and the data buffer space are independent of each other and do not interfere with each other, so it can also improve the packet loss caused by the untimely allocation of data buffer space, and further improve the reliability of USB control transmission. Reliability; not only that, the USB peripheral cache can also provide an overlay function, which can ensure that the USB peripheral receives and saves a new setup packet at any time, thereby avoiding the occurrence of packet loss; in addition, the USB peripheral can also be allocated at one time. Multiple event buffer spaces can reduce the interaction between software and hardware of USB peripherals, thereby improving the efficiency of USB control transmission.
应理解,上述图5所示的实施例中用于存储setup包的缓存空间是以事件缓存空间为例,在具体实施时,也可以替换为数据缓存空间,即在数据缓存空间中同时存储setup包和data/status包,并在传输data/status包前对setup包和data/status包进行编号校验,也可以避免错包传输的发生,进而提高USB控制传输的可靠性。It should be understood that the cache space used to store the setup package in the embodiment shown in FIG. 5 is the event cache space as an example, and in specific implementation, it can also be replaced with a data cache space, that is, the setup is stored in the data cache space at the same time. Packets and data/status packets, and check the numbers of setup packets and data/status packets before transmitting data/status packets, which can also avoid the occurrence of wrong packet transmission, thereby improving the reliability of USB control transmission.
以上各实施方式可以相互结合以实现不同的技术效果。The above embodiments can be combined with each other to achieve different technical effects.
参见图6,为本申请实施例提供的另一种基于USB通信的方法,以方法以应用在上述图2A中的USB外设中为例,方法包括:Referring to FIG. 6 , another method based on USB communication provided by an embodiment of the present application, taking the method applied to the USB peripheral in the above-mentioned FIG. 2A as an example, the method includes:
S901、USB外设在不同时间分别从所述USB主机接收setup包,其中任意一个setup包用于发起一笔对应的控制传输;S901, the USB peripheral device respectively receives setup packets from the USB host at different times, wherein any setup packet is used to initiate a corresponding control transmission;
S902、USB外设为已接收到的所述每个setup包生成唯一的编号,并将所述每个setup包与其对应的编号相关联;以及,将所述每个setup包对应的编号与所述每个setup包发起的控制传输中待传输的data包相关联;S902, the USB peripheral generates a unique number for each setup package that has been received, and associates each setup package with its corresponding number; and, associates the number corresponding to each setup package with all The data packets to be transmitted in the control transmission initiated by each setup packet are associated;
S903、USB外设在任意一笔控制传输的数据传输阶段开始后,确定所述任意一笔控制传输中待传输的data包对应的编号与所述USB外设最新接收到的setup包对应的编号一致之后,与所述USB主机传输所述任意一笔控制传输中待传输的data包。S903. After the data transmission phase of any control transmission starts, the USB peripheral determines the number corresponding to the data packet to be transmitted in the arbitrary control transmission and the number corresponding to the setup packet newly received by the USB peripheral After the agreement is reached, the data packet to be transmitted in the arbitrary control transmission is transmitted with the USB host.
上述S901~S903中各步骤的具体实现方法可以参考上文中的相关实施例,这里不再赘述。For the specific implementation method of each step in the foregoing S901 to S903, reference may be made to the relevant embodiments above, which will not be repeated here.
基于同一技术构思,本申请实施例还提供一种计算机可读存储介质,存储有计算机程序指令,当所述指令被执行时,使如上述图3~图6中的方法被实现。Based on the same technical concept, the embodiments of the present application further provide a computer-readable storage medium, which stores computer program instructions, and when the instructions are executed, the above-mentioned methods in FIG. 3 to FIG. 6 are implemented.
基于同一技术构思,本申请实施例还提供一种芯片,该芯片可以与存储器耦合,该芯片用于读取并执行存储器中存储的程序指令,实现如上述图3~图6中的方法。Based on the same technical concept, an embodiment of the present application also provides a chip, which can be coupled with a memory, and is used for reading and executing program instructions stored in the memory, implementing the methods in FIGS. 3 to 6 above.
基于同一技术构思,本申请实施例还提供一种计算机程序产品,该计算机程序产品中存储有指令,当其在计算机上运行时,使得计算机执行上述图3~图6中的方法。Based on the same technical concept, an embodiment of the present application further provides a computer program product, where instructions are stored in the computer program product, and when the computer program product runs on a computer, the computer executes the methods in FIGS. 3 to 6 .
本领域内的技术人员应明白,本申请的实施例可提供为方法、系统、或计算机程序产品。因此,本申请可采用完全硬件实施例、完全软件实施例、或结合软件和硬件方面的实施例的形式。而且,本申请可采用在一个或多个其中包含有计算机可用程序代码的计算机可用存储介质(包括但不限于磁盘存储器、CD-ROM、光学存储器等)上实施的计算机程序产品的形式。As will be appreciated by those skilled in the art, the embodiments of the present application may be provided as a method, a system, or a computer program product. Accordingly, the present application may take the form of an entirely hardware embodiment, an entirely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, the present application may take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, etc.) having computer-usable program code embodied therein.
本申请是参照根据本申请的方法、设备(系统)、和计算机程序产品的流程图和/或方框图来描述的。应理解可由计算机程序指令实现流程图和/或方框图中的每一流程和/或方框、以及流程图和/或方框图中的流程和/或方框的结合。可提供这些计算机程序指令到通用计算机、专用计算机、嵌入式处理机或其他可编程数据处理设备的处理器以产生一个机器,使得通过计算机或其他可编程数据处理设备的处理器执行的指令产生用于实现在流程图一个流程或多个流程和/或方框图一个方框或多个方框中指定的功能的装置。The present application is described with reference to flowchart illustrations and/or block diagrams of methods, apparatus (systems), and computer program products according to the present application. It will be understood that each flow and/or block in the flowchart illustrations and/or block diagrams, and combinations of flows and/or blocks in the flowchart illustrations and/or block diagrams, can be implemented by computer program instructions. These computer program instructions may be provided to the processor of a general purpose computer, special purpose computer, embedded processor or other programmable data processing device to produce a machine such that the instructions executed by the processor of the computer or other programmable data processing device produce Means for implementing the functions specified in a flow or flow of a flowchart and/or a block or blocks of a block diagram.
这些计算机程序指令也可存储在能引导计算机或其他可编程数据处理设备以特定方式工作的计算机可读存储器中,使得存储在该计算机可读存储器中的指令产生包括指令装置的制造品,该指令装置实现在流程图一个流程或多个流程和/或方框图一个方框或多个方框中指定的功能。These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing apparatus to function in a particular manner, such that the instructions stored in the computer-readable memory result in an article of manufacture comprising instruction means, the instructions The apparatus implements the functions specified in the flow or flow of the flowcharts and/or the block or blocks of the block diagrams.
这些计算机程序指令也可装载到计算机或其他可编程数据处理设备上,使得在计算机或其他可编程设备上执行一系列操作步骤以产生计算机实现的处理,从而在计算机或其他可编程设备上执行的指令提供用于实现在流程图一个流程或多个流程和/或方框图一个方框或多个方框中指定的功能的步骤。These computer program instructions can also be loaded on a computer or other programmable data processing device to cause a series of operational steps to be performed on the computer or other programmable device to produce a computer-implemented process such that The instructions provide steps for implementing the functions specified in the flow or blocks of the flowcharts and/or the block or blocks of the block diagrams.
显然,本领域的技术人员可以对本申请进行各种改动和变型而不脱离本申请的保护范围。这样,倘若本申请的这些修改和变型属于本申请权利要求及其等同技术的范围之内,则本申请也意图包含这些改动和变型在内。Obviously, those skilled in the art can make various changes and modifications to the present application without departing from the protection scope of the present application. Thus, if these modifications and variations of the present application fall within the scope of the claims of the present application and their equivalents, the present application is also intended to include these modifications and variations.

Claims (24)

  1. 一种基于通用串行总线USB的通信方法,其特征在于,所述方法应用于USB外设,所述USB外设与USB主机通过USB总线通信连接;A communication method based on a universal serial bus (USB), characterized in that the method is applied to a USB peripheral device, and the USB peripheral device and a USB host are communicatively connected through a USB bus;
    所述方法包括:The method includes:
    在不同时间分别从所述USB主机接收建立包,其中任意一个建立包用于发起一笔对应的控制传输;Receive establishment packets from the USB host at different times, wherein any establishment packet is used to initiate a corresponding control transmission;
    为已接收到的所述每个建立包生成唯一的编号,并将所述每个建立包与其对应的编号相关联;以及,将所述每个建立包对应的编号与所述每个建立包发起的控制传输中待传输的数据包相关联;Generate a unique number for each of the received set-up packages, and associate each of the set-up packages with its corresponding number; The data packets to be transmitted in the initiated control transmission are associated;
    在任意一笔控制传输的数据传输阶段开始后,确定所述任意一笔控制传输中待传输的数据包对应的编号与所述USB外设最新接收到的建立包对应的编号一致之后,与所述USB主机传输所述任意一笔控制传输中待传输的数据包。After the start of the data transmission phase of any control transmission, after it is determined that the number corresponding to the data packet to be transmitted in the arbitrary control transmission is consistent with the number corresponding to the establishment packet newly received by the USB peripheral, The USB host transmits the data packets to be transmitted in the arbitrary control transmission.
  2. 如权利要求1所述的方法,其特征在于,在将所述每个建立包与其对应的编号相关联之前,所述方法还包括:The method according to claim 1, wherein before associating each establishment packet with its corresponding serial number, the method further comprises:
    分配至少一个事件缓存空间;Allocate at least one event buffer space;
    将所述每个建立包与其对应的编号相关联,包括:Associate each establishment package with its corresponding number, including:
    将所述每个建立包与其对应的编号储存到同一个事件缓存空间中。Each of the establishment packets and their corresponding numbers are stored in the same event buffer space.
  3. 如权利要求2所述的方法,其特征在于,分配的所述事件缓存空间有多个;The method of claim 2, wherein there are multiple allocated event buffer spaces;
    将所述每个建立包与其对应的编号储存到同一个事件缓存空间中,包括:Store each of the establishment packets and their corresponding numbers in the same event buffer space, including:
    根据接收建立包的时间先后顺序,将在不同时间接收的多个建立包和对应的编号,依次存储到分配的多个事件缓存空间中。According to the time sequence of receiving the establishment packets, the multiple establishment packets received at different times and the corresponding numbers are sequentially stored in the allocated multiple event buffer spaces.
  4. 如权利要求2所述的方法,其特征在于,将所述每个建立包与其对应的编号储存到同一个事件缓存空间中,包括:The method according to claim 2, characterized in that, storing each establishment packet and its corresponding serial number in the same event buffer space, comprising:
    若所述至少一个事件缓存空间中存在空闲的事件缓存空间,则将所述每个建立包与其对应的编号存储在所述空闲的事件缓存空间中;If there is a free event buffer space in the at least one event buffer space, then store the number corresponding to each establishment packet in the free event buffer space;
    若所述至少一个事件缓存空间中不存在空闲的事件缓存空间,则使用在不同时间接收的N个建立包与其对应的编号,分别替换所述至少一个事件缓存空间中存储的历史编号由老至新的N个事件缓存空间中的建立包和对应的编号,其中,所述N为正整数。If there is no free event buffer space in the at least one event buffer space, the N set-up packets received at different times and their corresponding numbers are used to respectively replace the historical numbers stored in the at least one event buffer space from old to old. The establishment packets and corresponding numbers in the new N event buffer spaces, where N is a positive integer.
  5. 如权利要求2所述的方法,其特征在于,若所述至少一个事件缓存空间中不存在空闲的事件缓存空间,则将所述每个建立包与其对应的编号储存到同一个事件缓存空间中,包括:The method according to claim 2, wherein if there is no free event buffer space in the at least one event buffer space, storing each establishment packet and its corresponding number in the same event buffer space ,include:
    清空所述至少一个事件缓存空间中的所有事件缓存空间;根据接收建立包的时间先后顺序,将在不同时间接收的N个建立包和对应的编号,依次存储到清空后的N个事件缓存空间中;或者,Empty all event cache spaces in the at least one event cache space; according to the chronological order of receiving the establishment packets, store N establishment packets and corresponding numbers received at different times in the cleared N event cache spaces in turn in; or,
    在接收到N个建立包中的第i个建立包后,清空所述至少一个事件缓存空间中的一个事件缓存空间;将所述第i个建立包和对应的编号储存到清空后的所述一个事件缓存空间中,所述i取遍1到N中的正整数;After receiving the i-th establishment packet in the N establishment packets, clear one event buffer space in the at least one event buffer space; store the i-th establishment packet and the corresponding number in the cleared In an event buffer space, the i is a positive integer from 1 to N;
    其中,N为正整数。Among them, N is a positive integer.
  6. 如权利要求5所述的方法,其特征在于,所述一个事件缓存空间在清空前存储有: 在已接收到的建立包中接收时间最早的建立包、或者在已接收到的建立包对应的编号中最小的编号或者最大的编号。The method according to claim 5, characterized in that, before being cleared, the one event buffer space stores: the establishment packet with the earliest reception time among the received establishment packets, or the establishment packet corresponding to the received establishment packet The smallest number or the largest number among the numbers.
  7. 如权利要求1-6任一项所述的方法,其特征在于,在将所述每个建立包与其对应的编号相关联之后,以及将所述每个建立包对应的编号与所述每个建立包发起的控制传输中待传输的数据包相关联之前,所述方法还包括:The method according to any one of claims 1-6, characterized in that after associating each establishment packet with its corresponding number, and after associating the corresponding number of each establishment packet with each Before establishing the association of the data packets to be transmitted in the packet-initiated control transmission, the method further includes:
    解析所述每个建立包的内容,获得所述每个建立包发起的控制传输中待传输的数据包的大小;Parse the content of each establishment packet, and obtain the size of the data packet to be transmitted in the control transmission initiated by each establishment packet;
    根据所述大小为所述每个建立包发起的控制传输中待传输的数据包分配数据缓存空间;Allocate data buffer space for data packets to be transmitted in the control transmission initiated by each setup packet according to the size;
    将所述每个建立包对应的编号与所述每个建立包发起的控制传输中待传输的数据包相关联,包括:Associate the number corresponding to each establishment packet with the data packet to be transmitted in the control transmission initiated by each establishment packet, including:
    针对任一建立包,将所述任一建立包对应的编号和所述任一建立包发起的控制传输中待传输的数据包储存到为所述任一建立包发起的控制传输中待传输的数据包分配的数据缓存空间中。For any establishment packet, the number corresponding to the establishment packet and the data packet to be transmitted in the control transmission initiated by the establishment packet are stored in the control transmission initiated for the establishment packet. In the data buffer space allocated by the packet.
  8. 如权利要求7所述的方法,其特征在于,所述任意一笔控制传输的数据阶段为所述USB主机向所述USB外设发送数据包;The method according to claim 7, wherein, the data stage of any control transmission is that the USB host sends a data packet to the USB peripheral;
    在任意一笔控制传输的数据阶段开始后,确定所述任意一笔控制传输中待传输的数据包对应的编号与所述USB外设最新接收到的建立包对应的编号一致之后,与所述USB主机传输所述任意一笔控制传输中待传输的数据包,包括:After the start of the data phase of any control transmission, after it is determined that the number corresponding to the data packet to be transmitted in the arbitrary control transmission is consistent with the number corresponding to the establishment packet newly received by the USB peripheral The USB host transmits the data packets to be transmitted in any of the control transmissions, including:
    接收来自所述USB主机的数据包;receiving data packets from the USB host;
    确定所述任意一笔控制传输中待传输的数据包对应的编号与所述USB外设最新接收到的建立包对应的编号一致之后,将接收到的所述数据包存储到为所述任意一笔控制传输中待传输的数据包分配的所述数据缓存空间。After determining that the number corresponding to the data packet to be transmitted in the arbitrary control transmission is consistent with the number corresponding to the establishment packet newly received by the USB peripheral device, the received data packet is stored as the arbitrary one. The data buffer space allocated by the data packets to be transmitted in the pen control transmission.
  9. 如权利要求7所述的方法,其特征在于,所述任意一笔控制传输的数据阶段为所述USB外设向所述USB主机发送数据包;The method according to claim 7, wherein the data phase of the arbitrary control transmission is that the USB peripheral device sends a data packet to the USB host;
    在解析所述每个建立包的内容之后,还包括:After parsing the content of each establishment package, it also includes:
    获得所述每个建立包发起的控制传输中待传输的数据包的内容;Obtain the content of the data packet to be transmitted in the control transmission initiated by each establishment packet;
    在根据所述大小为所述每个建立包发起的控制传输中待传输的数据包分配数据缓存空间之后,以及在所述任意一笔控制传输的数据阶段开始之前,所述方法还包括:After allocating data buffer space for the data packets to be transmitted in the control transmission initiated by each setup packet according to the size, and before the start of the data phase of any control transmission, the method further includes:
    根据所述内容将所述每个建立包发起的控制传输中待传输的数据包储存到为所述每个建立包发起的控制传输中待传输的数据包分配的数据缓存空间中;According to the content, the data packets to be transmitted in the control transmission initiated by each establishment packet are stored in the data buffer space allocated for the data packets to be transmitted in the control transmission initiated by each establishment packet;
    在任意一笔控制传输的数据阶段开始后,确定所述任意一笔控制传输中待传输的数据包对应的编号与所述USB外设最新接收到的建立包对应的编号一致之后,与所述USB主机传输所述任意一笔控制传输中待传输的数据包,包括:After the start of the data phase of any control transmission, after it is determined that the number corresponding to the data packet to be transmitted in the arbitrary control transmission is consistent with the number corresponding to the establishment packet newly received by the USB peripheral The USB host transmits the data packets to be transmitted in any of the control transmissions, including:
    接收来自所述USB主机的数据包请求,所述数据包请求用于请求所述USB外设发送数据包;receiving 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;
    确定所述任意一笔控制传输中待传输的数据包对应的编号与所述USB外设最新接收到的建立包对应的编号一致之后,根据所述数据包请求从为所述任意一笔控制传输中待传输的数据包分配的所述数据缓存空间中读取数据包并发送给所述USB主机。After determining that the number corresponding to the data packet to be transmitted in the arbitrary control transmission is consistent with the number corresponding to the establishment packet newly received by the USB peripheral device, according to the data packet request from the control transmission for the arbitrary control transmission The data packet is read from the data buffer space allocated by the data packet to be transmitted and sent to the USB host.
  10. 如权利要求1-9任一项所述的方法,其特征在于,在将所述每个建立包对应的编号 与所述每个建立包发起的控制传输中待传输的数据包相关联之后,所述方法还包括:The method according to any one of claims 1-9, characterized in that after associating the number corresponding to each establishment packet with the data packet to be transmitted in the control transmission initiated by each establishment packet, The method also includes:
    将所述每个建立包对应的编号与所述每个建立包发起的控制传输中的状态包相关联;Associate the number corresponding to each establishment packet with the status packet in the control transmission initiated by each establishment packet;
    在所述任意一笔控制传输的数据阶段结束之后,所述方法还包括:After the data phase of the arbitrary control transmission ends, the method further includes:
    接收来自所述USB主机的状态包请求,所述状态包请求用于请求所述USB外设发送状态包;确定所述任意一笔控制传输中的状态包对应的编号与所述USB外设最新接收到的建立包对应的编号一致之后,向所述USB主机发送所述任意一笔控制传输中待传输的状态包。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; determine that the number corresponding to the status packet in any of the control transmissions is up-to-date with the USB peripheral After the numbers corresponding to the received setup packets are consistent, the status packet to be transmitted in the arbitrary control transmission is sent to the USB host.
  11. 一种基于USB的通信装置,其特征在于,所述装置应用于USB外设,所述USB外设与USB主机通过USB总线通信连接;A USB-based communication device, characterized in that the device is applied to a USB peripheral device, and the USB peripheral device and a USB host are communicatively connected through a USB bus;
    USB接口模块,用于在不同时间分别从所述USB主机接收建立包,其中任意一个建立包用于发起一笔对应的控制传输;The USB interface module is used to respectively receive establishment packets from the USB host at different times, wherein any establishment packet is used to initiate a corresponding control transmission;
    接口控制模块,用于为已接收到的所述每个建立包生成唯一的编号,并将所述每个建立包与其对应的编号相关联;an interface control module, for generating a unique number for each of the received setup packets, and associating each of the setup packets with its corresponding number;
    主控模块,用于将所述每个建立包对应的编号与所述每个建立包发起的控制传输中待传输的数据包相关联;a main control module, for associating the corresponding number of each establishment packet with the data packet to be transmitted in the control transmission initiated by each establishment packet;
    所述接口控制模块,还用于在任意一笔控制传输的数据传输阶段开始后,确定所述任意一笔控制传输中待传输的数据包对应的编号与所述USB外设最新接收到的建立包对应的编号一致之后,控制所述USB接口模块与所述USB主机传输所述任意一笔控制传输中待传输的数据包。The interface control module is further configured to, after the start of the data transmission phase of any control transmission, determine the number corresponding to the data packet to be transmitted in the arbitrary control transmission and the newly received establishment of the USB peripheral. After the numbers corresponding to the packets are consistent, the USB interface module and the USB host are controlled to transmit the data packets to be transmitted in the arbitrary control transmission.
  12. 如权利要求11所述的装置,其特征在于,所述主控模块还用于:The device according to claim 11, wherein the main control module is further used for:
    在所述接口控制模块将所述每个建立包与其对应的编号相关联之前,分配至少一个事件缓存空间;Allocate at least one event buffer space before the interface control module associates each establishment packet with its corresponding serial number;
    所述接口控制模块在将所述每个建立包与其对应的编号相关联时,具体用于:When the interface control module associates each establishment packet with its corresponding serial number, it is specifically used for:
    将所述每个建立包与其对应的编号储存到同一个事件缓存空间中。Each of the establishment packets and their corresponding numbers are stored in the same event buffer space.
  13. 如权利要求12所述的装置,其特征在于,分配的所述事件缓存空间有多个;The apparatus of claim 12, wherein there are multiple allocated event buffer spaces;
    所述接口控制模块在将所述每个建立包与其对应的编号储存到同一个事件缓存空间中时,具体用于:When the interface control module stores each establishment packet and its corresponding serial number in the same event buffer space, it is specifically used for:
    根据接收建立包的时间先后顺序,将在不同时间接收的多个建立包和对应的编号,依次存储到分配的多个事件缓存空间中。According to the time sequence of receiving the establishment packets, the multiple establishment packets received at different times and the corresponding numbers are sequentially stored in the allocated multiple event buffer spaces.
  14. 如权利要求12所述的装置,其特征在于,所述接口控制模块在将所述每个建立包与其对应的编号储存到同一个事件缓存空间中时,具体用于:The device according to claim 12, wherein, when the interface control module stores each establishment packet and its corresponding serial number in the same event buffer space, it is specifically used for:
    若所述至少一个事件缓存空间中存在空闲的事件缓存空间,则将所述每个建立包与其对应的编号存储在所述空闲的事件缓存空间中;If there is a free event buffer space in the at least one event buffer space, then store the number corresponding to each establishment packet in the free event buffer space;
    若所述至少一个事件缓存空间中不存在空闲的事件缓存空间,则使用在不同时间接收的N个建立包与其对应的编号,分别替换所述至少一个事件缓存空间中存储的历史编号由老至新的N个事件缓存空间中的建立包和对应的编号,其中,所述N为正整数。If there is no free event buffer space in the at least one event buffer space, the N set-up packets received at different times and their corresponding numbers are used to respectively replace the historical numbers stored in the at least one event buffer space from old to old. The establishment packets and corresponding numbers in the new N event buffer spaces, where N is a positive integer.
  15. 如权利要求13所述的装置,其特征在于,若所述至少一个事件缓存空间中不存在空闲的事件缓存空间,则所述接口控制模块在将所述每个建立包与其对应的编号储存到同一个事件缓存空间中时,具体用于:The apparatus according to claim 13, wherein if there is no free event buffer space in the at least one event buffer space, the interface control module stores the corresponding number of each establishment packet in the When in the same event buffer space, it is specifically used for:
    清空所述至少一个事件缓存空间中的所有事件缓存空间;根据接收建立包的时间先后 顺序,将在不同时间接收的N个建立包和对应的编号,依次存储到清空后的N个事件缓存空间中;或者,Empty all event cache spaces in the at least one event cache space; according to the chronological order of receiving the establishment packets, store N establishment packets and corresponding numbers received at different times in the cleared N event cache spaces in turn in; or,
    在所述USB接口模块接收到N个建立包中的第i个建立包后,清空所述至少一个事件缓存空间中的一个事件缓存空间;将所述第i个建立包和对应的编号储存到清空后的所述一个事件缓存空间中,所述i取遍1到N中的正整数;After the USB interface module receives the i-th establishment packet in the N establishment packets, clear one event buffer space in the at least one event buffer space; store the i-th establishment packet and the corresponding number in the In the one event buffer space after being emptied, the i is a positive integer from 1 to N;
    其中,N为正整数。Among them, N is a positive integer.
  16. 如权利要求15所述的装置,其特征在于,所述一个事件缓存空间在清空前存储有:在所述USB接口模块已接收到的建立包中接收时间最早的建立包、或者在所述USB接口模块已接收到的建立包对应的编号中最小的编号或者最大的编号。The device according to claim 15, wherein before being cleared, the one event buffer space stores: the establishment packet with the earliest reception time among the establishment packets received by the USB interface module, or the establishment packet with the earliest reception time in the USB interface module. The smallest number or the largest number among the numbers corresponding to the setup packets received by the interface module.
  17. 如权利要求11-16任一项所述的装置,其特征在于,所述主控模块还用于:The device according to any one of claims 11-16, wherein the main control module is further configured to:
    在所述接口控制模块将所述每个建立包与其对应的编号相关联之后,以及在所述主控模块将所述每个建立包对应的编号与所述每个建立包发起的控制传输中待传输的数据包相关联之前,解析所述每个建立包的内容,获得所述每个建立包发起的控制传输中待传输的数据包的大小;根据所述大小为所述每个建立包发起的控制传输中待传输的数据包分配数据缓存空间;After the interface control module associates each setup packet with its corresponding number, and in the control transmission initiated by the main control module between the number corresponding to each setup packet and each setup packet Before the data packets to be transmitted are associated, the content of each establishment packet is parsed, and the size of the data packet to be transmitted in the control transmission initiated by each establishment packet is obtained; Allocate data buffer space for data packets to be transmitted in the initiated control transmission;
    所述主控模块在将所述每个建立包对应的编号与所述每个建立包发起的控制传输中待传输的数据包相关联时,具体用于:When the main control module associates the number corresponding to each establishment packet with the data packet to be transmitted in the control transmission initiated by each establishment packet, it is specifically used for:
    针对任一建立包,将所述任一建立包对应的编号和所述任一建立包发起的控制传输中待传输的数据包储存到为所述任一建立包发起的控制传输中待传输的数据包分配的数据缓存空间中。For any establishment packet, the number corresponding to the establishment packet and the data packet to be transmitted in the control transmission initiated by the establishment packet are stored in the control transmission initiated for the establishment packet. In the data buffer space allocated by the packet.
  18. 如权利要求17所述的装置,其特征在于,所述任意一笔控制传输的数据阶段为所述USB主机向所述USB外设发送数据包;The device according to claim 17, wherein, the data phase of the arbitrary control transmission is that the USB host sends a data packet to the USB peripheral;
    所述USB接口模块还用于:在所述任意一笔控制传输的数据阶段开始后,接收来自所述USB主机的数据包;The USB interface module is further configured to: receive a data packet from the USB host after the start of the data phase of any control transmission;
    所述接口控制模块在控制所述USB接口模块与所述USB主机传输所述任意一笔控制传输中待传输的数据包时,具体用于:确定所述任意一笔控制传输中待传输的数据包对应的编号与所述USB外设最新接收到的建立包对应的编号一致之后,将接收到的所述数据包存储到为所述任意一笔控制传输中待传输的数据包分配的所述数据缓存空间。When controlling the USB interface module and the USB host to transmit the data packets to be transmitted in the arbitrary control transmission, the interface control module is specifically configured to: determine the data to be transmitted in the arbitrary control transmission After the number corresponding to the packet is consistent with the number corresponding to the establishment packet newly received by the USB peripheral, the received data packet is stored in the data packet allocated for the data packet to be transmitted in the arbitrary control transmission. Data cache space.
  19. 如权利要求17所述的装置,其特征在于,所述任意一笔控制传输的数据阶段为所述USB外设向所述USB主机发送数据包;The device of claim 17, wherein the data phase of the arbitrary control transmission is that the USB peripheral device sends a data packet to the USB host;
    所述主控模块还用于:在解析所述每个建立包的内容之后,获得所述每个建立包发起的控制传输中待传输的数据包的内容;在根据所述大小为所述每个建立包发起的控制传输中待传输的数据包分配数据缓存空间之后,以及在所述任意一笔控制传输的数据阶段开始之前,根据所述内容将所述每个建立包发起的控制传输中待传输的数据包储存到为所述每个建立包发起的控制传输中待传输的数据包分配的所述数据缓存空间中;The main control module is further configured to: after parsing the content of each establishment packet, obtain the content of the data packet to be transmitted in the control transmission initiated by each establishment packet; After allocating data buffer space for the data packets to be transmitted in the control transmission initiated by each setup packet, and before the start of the data phase of any one control transmission, the control transmission initiated by each setup packet is stored in the control transmission initiated by each setup packet according to the content. The data packets to be transmitted are stored in the data buffer space allocated for the data packets to be transmitted in the control transmission initiated by each setup packet;
    所述USB接口模块还用于:在所述任意一笔控制传输的数据阶段开始后,接收来自所述USB主机的数据包请求,所述数据包请求用于请求所述USB外设发送数据包;The USB interface module is further configured to receive a data packet request from the USB host after the start of the data phase of any control transmission, where the data packet request is used to request the USB peripheral to send a data packet ;
    所述接口控制模块控制所述USB接口模块与所述USB主机传输所述任意一笔控制传输中待传输的数据包时,具体用于:确定所述任意一笔控制传输中待传输的数据包对应的编号与所述USB外设最新接收到的建立包对应的编号一致之后,根据所述数据包请求从为 所述任意一笔控制传输中待传输的数据包分配的所述数据缓存空间中读取数据包,并将读取到的数据包通过所述USB接口模块发送给所述USB主机。When the interface control module controls the USB interface module and the USB host to transmit the data packets to be transmitted in the arbitrary control transmission, it is specifically configured to: determine the data packets to be transmitted in the arbitrary control transmission After the corresponding number is consistent with the number corresponding to the establishment packet newly received by the USB peripheral device, according to the data packet request, from the data buffer space allocated for the data packet to be transmitted in the arbitrary control transmission. The data packet is read, and the read data packet is sent to the USB host through the USB interface module.
  20. 如权利要求11-19任一项所述的装置,其特征在于,所述主控模块还用于:The device according to any one of claims 11-19, 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 transmission initiated by each setup packet, the number corresponding to each setup packet is initiated with the is associated with the status packet in the control transfer;
    所述USB接口模块还用于:在所述任意一笔控制传输的数据阶段结束之后,接收来自所述USB主机的状态包请求,所述状态包请求用于请求所述USB外设发送状态包;The USB interface module is further configured to receive a status packet request from the USB host after the data phase of any control transmission ends, where the status packet request is used to request the USB peripheral to send a status packet ;
    所述接口控制模块还用于:确定所述任意一笔控制传输中的状态包对应的编号与所述USB外设最新接收到的建立包对应的编号一致之后,控制所述USB接口模块向所述USB主机发送所述任意一笔控制传输中待传输的状态包。The interface control module is further configured to: after determining that the number corresponding to the status packet in the arbitrary control transmission is consistent with the number corresponding to the establishment packet newly received by the USB peripheral device, control the USB interface module to send the information to the relevant device. The USB host sends the status packet to be transmitted in the arbitrary control transmission.
  21. 如权利要求12所述的装置,其特征在于,所述接口控制模块还用于:在将所述每个建立包与其对应的编号储存到同一个事件缓存空间中之后,向所述主控模块上报中断信号,所述中断信号用于指示所述主控模块从所述事件缓存空间中读取所述每个建立包与其对应的编号;The device according to claim 12, wherein the interface control module is further configured to: after storing each establishment packet and its corresponding serial number in the same event buffer space, send a message to the main control module reporting an interrupt signal, the interrupt signal is used to instruct the main control module to read the corresponding number of each establishment packet from the event buffer space;
    所述主控模块还用于:在将所述每个建立包对应的编号与所述每个建立包发起的控制传输中待传输的数据包相关联之前,从所述接口控制模块接收所述中断信号;基于所述中断信号从所述事件缓存空间中读取所述每个建立包与其对应的编号。The main control module is further configured to: receive the interface control module from the interface control module before associating the number corresponding to each establishment packet with the data packet to be transmitted in the control transmission initiated by the each establishment packet. interrupt signal; read the corresponding number of each establishment packet from the event buffer space based on the interrupt signal.
  22. 一种计算机可读存储介质,其特征在于,包含计算机程序指令,当所述指令被执行时,使如权利要求1-10中任一项所述的方法被实现。A computer-readable storage medium comprising computer program instructions which, when executed, cause the method of any one of claims 1-10 to be implemented.
  23. 一种芯片,其特征在于,所述芯片与存储器耦合,用于读取并执行所述存储器中存储的程序指令,实现如权利要求1-10中任一项所述的方法。A chip, characterized in that the chip is coupled with a memory, and is used for reading and executing program instructions stored in the memory, so as to implement the method according to any one of claims 1-10.
  24. 一种计算机程序产品,其特征在于,所述计算机程序产品中存储有指令,当其在计算机上运行时,使得计算机执行如权利要求1-10中任一项所述的方法。A computer program product, characterized in that the computer program product stores instructions that, when executed on a computer, cause the computer to execute the method according to any one of claims 1-10.
PCT/CN2021/078314 2021-02-27 2021-02-27 Usb-based communication method and apparatus WO2022178882A1 (en)

Priority Applications (2)

Application Number Priority Date Filing Date Title
PCT/CN2021/078314 WO2022178882A1 (en) 2021-02-27 2021-02-27 Usb-based communication method and apparatus
CN202180079881.9A CN116601618A (en) 2021-02-27 2021-02-27 USB-based communication method and device

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
PCT/CN2021/078314 WO2022178882A1 (en) 2021-02-27 2021-02-27 Usb-based communication method and apparatus

Publications (1)

Publication Number Publication Date
WO2022178882A1 true WO2022178882A1 (en) 2022-09-01

Family

ID=83047653

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2021/078314 WO2022178882A1 (en) 2021-02-27 2021-02-27 Usb-based communication method and apparatus

Country Status (2)

Country Link
CN (1) CN116601618A (en)
WO (1) WO2022178882A1 (en)

Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20040177197A1 (en) * 2001-04-27 2004-09-09 Mcleod John Alexander Method and apparatus for extending the range of the universal serial bus protocol
CN101122878A (en) * 2006-08-09 2008-02-13 英业达股份有限公司 USB interface standard inspection system and its method
CN102043751A (en) * 2010-06-29 2011-05-04 北京飞天诚信科技有限公司 Method for identifying host operation system by using USB equipment
CN104123258A (en) * 2014-07-23 2014-10-29 龙迅半导体科技(合肥)有限公司 USB data transmission interface and system
CN110476156A (en) * 2017-04-03 2019-11-19 华为技术有限公司 Universal serial bus networks interchanger
CN111213293A (en) * 2017-10-12 2020-05-29 深圳传音通讯有限公司 Control method and control device for signal switching of intelligent equipment

Patent Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20040177197A1 (en) * 2001-04-27 2004-09-09 Mcleod John Alexander Method and apparatus for extending the range of the universal serial bus protocol
CN101122878A (en) * 2006-08-09 2008-02-13 英业达股份有限公司 USB interface standard inspection system and its method
CN102043751A (en) * 2010-06-29 2011-05-04 北京飞天诚信科技有限公司 Method for identifying host operation system by using USB equipment
CN104123258A (en) * 2014-07-23 2014-10-29 龙迅半导体科技(合肥)有限公司 USB data transmission interface and system
CN110476156A (en) * 2017-04-03 2019-11-19 华为技术有限公司 Universal serial bus networks interchanger
CN111213293A (en) * 2017-10-12 2020-05-29 深圳传音通讯有限公司 Control method and control device for signal switching of intelligent equipment

Also Published As

Publication number Publication date
CN116601618A (en) 2023-08-15

Similar Documents

Publication Publication Date Title
CN102075401B (en) Method, equipment and system for transmitting message on peripheral component interface express (PCIE) bus
CN108132897B (en) SRIO controller based on ZYNQ platform soft core
CN108107827B (en) SRIO control method based on ZYNQ platform soft core
WO2023103301A1 (en) Distributed heterogeneous acceleration platform communication method and system, and device and medium
JP2003316731A (en) Data transfer control device, electronic equipment, and data transfer control method
KR20130107618A (en) Method and apparatus for data transfer of remote usb device between server and client
WO2022040847A1 (en) Communication data processing method and apparatus
US7006533B2 (en) Method and apparatus for hublink read return streaming
TW201717039A (en) Method and system for USB 2.0 bandwidth reservation
WO2022178882A1 (en) Usb-based communication method and apparatus
JP2003316732A (en) Data transfer control device, electronic equipment, and data transfer control method
US20090182927A1 (en) Direct Memory Move of Multiple Buffers Between Logical Partitions
CN112241277A (en) IC firmware updating method
US20200319926A1 (en) System on chip comprising a plurality of master resources
CN104850517A (en) Method and apparatus for transmitting packet data using DMA
CN110765044B (en) Data packet transmission device and system
WO2012058875A1 (en) Method and system for serial communication
EP3631640B1 (en) Communication between field programmable gate arrays
US11233514B2 (en) Semiconductor device including subsystem interfaces and communications method thereof
US20240069754A1 (en) Computing system and associated method
CN111200594B (en) Method and device for receiving data, data receiving equipment and storage medium
WO2024012015A1 (en) Storage system, main control chip, data storage method and data reading method
CN117041186B (en) Data transmission method, chip system, computing device and storage medium
CN111026694B (en) Data receiving method, data receiving apparatus, image forming system, and electronic apparatus
CN116594951B (en) FPGA-based data transmission system and method

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 21927317

Country of ref document: EP

Kind code of ref document: A1

WWE Wipo information: entry into national phase

Ref document number: 202180079881.9

Country of ref document: CN

NENP Non-entry into the national phase

Ref country code: DE

122 Ep: pct application non-entry in european phase

Ref document number: 21927317

Country of ref document: EP

Kind code of ref document: A1