A method for identifying a USB storage device with increased mutual exclusion protection mechanism
By adding logical mutual exclusion relationships between insertion-internal use and removal-internal use semaphores in the driver, the problem of unstable signals during USB storage device connection is solved, ensuring reliable identification and normal operation of USB storage devices by the embedded computer.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- XIAN AVIATION COMPUTING TECH RES INST OF AVIATION IND CORP OF CHINA
- Filing Date
- 2023-12-28
- Publication Date
- 2026-07-24
Smart Images

Figure CN117873575B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of embedded computer system technology, specifically relating to a method for identifying USB storage devices with added mutual exclusion protection mechanisms. Background Technology
[0002] Universal Serial Bus (USB) is a serial bus interface for connecting computer systems to external devices, and it is widely used in the field of embedded computer systems. Embedded computers are often designed with a USB interface, which can connect to an external USB storage device containing a task list, allowing the loading, updating, and editing of the list to execute specific tasks.
[0003] The identification function of USB storage devices is implemented through the cooperation of driver software and application programs. Embedded computers design "insert" and "remove" semaphores in the USB storage device driver. The application program uses these two semaphores to determine whether the USB storage device is connected and to perform subsequent access operations, such as... Figure 1 As shown.
[0004] The application uses the variable "LinkStatus" to represent the connection status of the USB storage device: "LinkStatus = 0" indicates no connection, and "LinkStatus = 1" indicates a connection. Initialization sets "LinkStatus = 0". When the application starts running, it continuously checks the connection status of the USB storage device (i.e., the value of "LinkStatus"). When it detects an "insert" semaphore released by the driver, it sets "LinkStatus" to 1, indicating a connection to the USB storage device and initiating subsequent operations. Then, it starts continuously checking the "remove" semaphore. When it detects a "remove" semaphore released by the driver, it changes the connection status by setting "LinkStatus" to 0, and starts checking the "insert" semaphore again, waiting for the next loading operation. The working mechanism is as follows: Figure 2 As shown.
[0005] Due to external interference factors such as USB connector aging, unstable signals during the connection of a USB storage device can cause device initialization errors, resulting in the inability to load the inventory contents of the device and affecting the embedded computer's ability to perform tasks. Summary of the Invention
[0006] In view of this, the present invention provides a USB storage device identification method with added mutual exclusion protection mechanism. This is achieved by adding "insertion-internal use" and "removal-internal use" signals to the driver and establishing a logical mutual exclusion relationship between them. This ensures that the embedded computer's identification of the USB storage device's operating state is not in an abnormal state, thus improving reliability.
[0007] The technical solution of the present invention:
[0008] A method for identifying a USB storage device with added mutual exclusion protection mechanism includes the following steps:
[0009] S101: Add configuration for inserting and unplugging semaphores to the USB driver;
[0010] S102: Establish a mutual exclusion function to make the insertion-internal use semaphore and the extraction-internal use semaphore logically mutually exclusive;
[0011] S103: Determine whether to release the insertion or removal state of the USB storage device based on the mutual exclusion relationship.
[0012] Furthermore, the USB driver is configured with a pull-out semaphore, an insertion semaphore, a pull-out-internal-use semaphore, and an insertion-internal-use semaphore.
[0013] Before releasing the unplug signal, determine whether the USB storage device is already inserted. If the Insert-Internal Use signal can be obtained, it means that the preceding USB storage device is recognized normally, i.e., it is already inserted, and the unplug signal can be released this time. If the Insert-Internal Use signal cannot be obtained, it means that the previous connection process was abnormal, and the unplug signal will not be released this time.
[0014] Furthermore,
[0015] Before releasing the insertion semaphore, first determine whether the USB storage device is already in the unplugged state. If the unplugged-internal-use semaphore can be obtained, it means that the previous USB storage device identification is normal, that is, it is already in the unplugged state, and the insertion signal can be released this time. If the unplugged-internal-use semaphore cannot be obtained, it means that the previous connection process was abnormal, and the insertion signal will not be released this time.
[0016] Furthermore, the method for implementing the protection mechanism includes the following steps:
[0017] S201
[0018] Each time a USB device is inserted, the insertion-internal-use semaphore is assigned a valid value. If the previous device state corresponding to this insertion is identified as an inserted state, the insertion semaphore is not released to the upper-layer software. If the previous state corresponding to this insertion is identified as a removed state, the insertion semaphore is released to the upper-layer software. Each time a USB device is removed, the removal-internal-use semaphore is assigned a valid value. If the previous state corresponding to this removal is identified as a removed state, the removal semaphore is not released to the upper-layer software. If the previous state corresponding to this removal is identified as an inserted state, the removal semaphore is released to the upper-layer software. S202:
[0019] The insertion-internal use semaphore value assigned in S201 is used as the precondition for the next release of the USB storage device unplug signal; the unplug-internal use semaphore value assigned in S201 is used as the precondition for the next release of the USB storage device insertion signal; each time the insertion or unplug signal is released subsequently, the judgment is based on the previous unplug state or insertion state.
[0020] S203: Repeat steps S201-S202.
[0021] Furthermore, at any given time, only one of the Insert-Internal Use Semaphore or Pull-Out-Internal Use Semaphore is active.
[0022] The beneficial effects of this invention are:
[0023] This invention establishes a logical mutual exclusion relationship between the "insertion-internal use" and "removal-internal use" signals of the USB storage device in the driver program. This ensures that the embedded computer's recognition of the USB storage device's operating state is not in an abnormal state, thus improving reliability. Attached Figure Description
[0024] To more clearly illustrate the technical solutions of the embodiments of this disclosure, the drawings used in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this disclosure. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0025] Figure 1 The working mechanism of USB storage device identification
[0026] Figure 2 The working mechanism of application acquiring semaphores
[0027] Figure 3 This is the USB driver semaphore release protection mechanism proposed in this paper.
[0028] Figure 4 This is the application execution flowchart (with protection mechanisms).
[0029] Figure 5 This indicates an application execution error under abnormal circumstances (without protection mechanisms). Detailed Implementation
[0030] The embodiments of this disclosure will now be described in detail with reference to the accompanying drawings.
[0031] The following specific examples illustrate the implementation of this disclosure. Those skilled in the art can easily understand other advantages and effects of this disclosure from the content disclosed in this specification. Obviously, the described embodiments are only a part of the embodiments of this disclosure, and not all of them. This disclosure can also be implemented or applied through other different specific embodiments, and the details in this specification can also be modified or changed based on different viewpoints and applications without departing from the spirit of this disclosure. It should be noted that, in the absence of conflict, the following embodiments and features in the embodiments can be combined with each other. Based on the embodiments in this disclosure, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this disclosure.
[0032] It should be noted that various aspects of embodiments within the scope of the appended claims are described below. It will be apparent that the aspects described herein can be embodied in a wide variety of forms, and any particular structure and / or function described herein is merely illustrative. Based on this disclosure, those skilled in the art will understand that one aspect described herein can be implemented independently of any other aspect, and two or more of these aspects can be combined in various ways. For example, any number of aspects set forth herein can be used to implement the device and / or practice the method. Additionally, this device and / or method can be implemented using other structures and / or functionalities besides one or more of the aspects set forth herein.
[0033] It should also be noted that the illustrations provided in the following embodiments are only schematic representations of the basic concept of this disclosure. The drawings only show the components related to this disclosure and are not drawn according to the number, shape and size of the components in actual implementation. In actual implementation, the form, quantity and proportion of each component can be arbitrarily changed, and the layout of the components may also be more complex.
[0034] Furthermore, specific details are provided in the following description to facilitate a thorough understanding of the examples. However, those skilled in the art will understand that the described aspects can be practiced without these specific details.
[0035] In one embodiment of the present invention, a method for identifying a USB storage device with an added mutual exclusion protection mechanism is proposed, comprising the following steps:
[0036] S101: Add configuration for inserting and unplugging semaphores to the USB driver;
[0037] S102: Establish a mutual exclusion function to make the insertion-internal use semaphore and the extraction-internal use semaphore logically mutually exclusive;
[0038] S103: Determine whether to release the insertion or removal state of the USB storage device based on the mutual exclusion relationship.
[0039] In this embodiment, the USB driver is configured with a pull-out semaphore, an insertion semaphore, a pull-out-internal-use semaphore, and an insertion-internal-use semaphore.
[0040] Before releasing the unplug signal, determine whether the USB storage device is already inserted. If the Insert-Internal Use signal can be obtained, it means that the preceding USB storage device is recognized normally, i.e., it is already inserted, and the unplug signal can be released this time. If the Insert-Internal Use signal cannot be obtained, it means that the previous connection process was abnormal, and the unplug signal will not be released this time.
[0041] In this embodiment,
[0042] Before releasing the insertion semaphore, first determine whether the USB storage device is already in the unplugged state. If the unplugged-internal-use semaphore can be obtained, it means that the previous USB storage device identification is normal, that is, it is already in the unplugged state, and the insertion signal can be released this time. If the unplugged-internal-use semaphore cannot be obtained, it means that the previous connection process was abnormal, and the insertion signal will not be released this time.
[0043] In this embodiment, the method for implementing the protection mechanism includes the following steps:
[0044] S201: Each time a USB device is inserted, the insertion-internal usage semaphore is assigned a valid value. If the previous device state corresponding to the current USB device insertion is identified as the inserted state, the insertion semaphore is not released to the upper-layer software. If the previous state corresponding to the current USB device insertion is identified as the unplugged state, the insertion semaphore is released to the upper-layer software. Each time a USB device is unplugged, the unplug-internal usage semaphore is assigned a valid value. If the previous state corresponding to the current USB device unplugging is identified as the unplugged state, the unplugging semaphore is not released to the upper-layer software. If the previous state corresponding to the current USB device unplugging is identified as the inserted state, the unplugging semaphore is released to the upper-layer software.
[0045] S202: Use the insertion-internal use semaphore value assigned in S201 as the precondition for the next USB storage device release of the unplug signal; use the unplug-internal use semaphore value assigned in S201 as the precondition for the next USB storage device release of the insertion signal; each time the insertion or unplug signal is released subsequently, the judgment is based on the previous unplug or insertion state.
[0046] S203: Repeat steps S201-S202.
[0047] In this embodiment, at any given time, only one of the insertion-internal use semaphore or the removal-internal use semaphore is active. See also... Figure 3 ;
[0048] correspond Figure 3 The core code architecture of the corresponding driver for the process is as follows:
[0049] 1. Create four binary state semaphores: "Insert", "Remove", "Insert - Internal Use", and "Remove - Internal Use".
[0050] g_semUsbInsert = semBCreate(SEM_Q_FIFO, SEM_EMPTY); / * Insert semaphore * /
[0051] g_semUsbRemove = semBCreate(SEM_Q_FIFO, SEM_EMPTY); / * Remove semaphore * /
[0052] g_semUsbInsertToRemove = semBCreate(SEM_Q_FIFO, SEM_EMPTY); / * "Remove - for internal use" semaphore * /
[0053] g_semUsbRemoveToInsert=semBCreate(SEM_Q_FIFO,SEM_FULL);
[0054] / * Inserting semaphores for internal use * /
[0055] 2. Add the following conditional code to the USB device mounting task function:
[0056] / * Add a semaphore to control the insertion / removal sequence, i.e., insertion must precede removal, and removal must precede insertion, and so on. * /
[0057] if(OK==semTake(g_semUsbRemoveToInsert,sysClkRateGet()))
[0058] {
[0059] semGive(g_semUsbInsert);
[0060] semGive(g_semUsbInsertToRemove);
[0061] }
[0062] else
[0063] {
[0064] logMsg("\ng_semUsbRemoveToInsert not find!\n",0,0,0,0,0,0);
[0065] }
[0066] return OK;
[0067] 3. Add the following conditional code to the unmount task function of the USB device:
[0068] if(OK==semTake(g_semUsbInsertToRemove,sysClkRateGet()))
[0069] {
[0070] semGive(g_semUsbRemove);
[0071] semGive(g_semUsbRemoveToInsert);
[0072] }
[0073] else
[0074] {
[0075] logMsg("\ng_semUsbRemoveToInsert not find!\n",0,0,0,0,0,0);
[0076] }
[0077] return OK;
[0078] Figure 4The flowchart for application execution shows that, due to the driver's protection mechanism, even if the USB storage device has an unstable physical connection, the driver will not abnormally release the "insert" or "remove" signal. The application can correctly identify the insertion and removal of the USB storage device in sequence, which increases the reliability of use and is suitable for scenarios with high reliability requirements for the use of USB storage devices.
[0079] If the USB driver does not employ the identification method with protection mechanism proposed in this paper, when a USB storage device is connected, if the USB interface electrical signal becomes unstable due to abnormal conditions, the driver may release the "unplug" signal multiple times. When the USB storage device is reconnected, after the application completes the USB storage device identification process, the application will acquire the extra released "unplug" signal and interpret it as "LinkStatus = 1," thus entering the unplugging operation of the USB storage device, causing the USB storage device to be in a "not connected" state. In this state, unplugging and replugging the loading card will not restore functionality; a product restart is required.
[0080] The process of application error identification is as follows Figure 5 As shown.
[0081] The above description is merely a specific embodiment of this disclosure, but the scope of protection of this disclosure is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in this disclosure should be included within the scope of protection of this disclosure. Therefore, the scope of protection of this disclosure should be determined by the scope of the claims.
Claims
1. A method for identifying USB storage devices with added mutual exclusion protection mechanism, characterized in that, Includes the following steps: S101: Add configuration for inserting and unplugging semaphores to the USB driver; S102: Establish a mutual exclusion function to make the insertion-internal use semaphore and the extraction-internal use semaphore logically mutually exclusive; S103: Determine whether to release the insertion or removal state of the USB storage device based on the mutual exclusion relationship; The USB driver is configured with insertion-internal use semaphores and removal-internal use semaphores. Before releasing the unplug signal, it is determined whether the USB storage device is already inserted. If the Insert-Internal Use signal can be obtained, it means that the preceding USB storage device is recognized normally, that is, it is already inserted, and the unplug signal can be released this time. If the Insert-Internal Use signal cannot be obtained, it means that the previous connection process was abnormal, and the unplug signal will not be released this time. Before releasing the insertion semaphore, first determine whether the USB storage device is already in the unplugged state. If the unplugged-internal-use semaphore can be obtained, it means that the previous USB storage device is recognized normally, that is, it is already in the unplugged state, and the insertion signal can be released this time. If the unplugged-internal-use semaphore cannot be obtained, it means that the previous connection process was abnormal, and the insertion signal will not be released this time. The implementation method of the protection mechanism includes the following steps: S201: Each time a USB device is inserted, the insertion-internal usage semaphore is assigned a valid value. If the previous device state corresponding to this USB device insertion is identified as the inserted state, the insertion semaphore is not released to the upper-layer software. If the previous state corresponding to this USB device insertion is identified as the unplugged state, the insertion semaphore is released to the upper-layer software. Each time a USB device is unplugged, the unplug-internal usage semaphore is assigned a valid value. If the previous state corresponding to this USB device unplugging is identified as the unplugged state, the unplugging semaphore is not released to the upper-layer software. If the previous state corresponding to this USB device unplugging is identified as the inserted state, the unplugging semaphore is released to the upper-layer software. S202: Use the insertion-internal usage semaphore value assigned in S201 as the precondition for the next release of the USB storage device unplugging signal; use the unplugging-internal usage semaphore value assigned in S201 as the precondition for the next release of the USB storage device insertion signal; each time the insertion or unplugging semaphore is released subsequently, the judgment is based on the previous unplugging or insertion state; S203: Execute S201-S202 in a loop; At any given time, only one of the Insert-Internal Use Semaphore or Pull-Out-Internal Use Semaphore is active.