A GPIO Signal Active Polling Detection Method, System, Device and Medium
The GPIO signal active polling detection method addresses latency and uncertainty issues by using a dedicated kernel thread to reserve CPU resources, ensuring real-time and accurate GPIO signal detection.
Patent Information
- Application Number
- CN202510486212.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-17
- Publication Date
- 2025-07-15
- Estimated Expiration
- 2045-04-17
AI Technical Summary
There are interrupt delay, scheduling delay and interrupt priority conflicts in existing GPIO signal detection, which makes it difficult to guarantee real-time and accuracy of detection.
The active polling detection method is adopted, and by creating kernel threads and performing GPIO signal detection on reserved CPU resources, interrupt dependence is reduced, and the CPU resource reservation mechanism isolates the CPU for active polling detection to ensure the real-time and accuracy of the detection.
It improves the real-time and accuracy of GPIO signal detection, reduces interrupt delay and scheduling delay, reduces the dependence of hardware resources, and improves system efficiency and energy efficiency.
Smart Images

Figure CN120029736B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer communication technologies, and particularly to a method, system, device, and medium for actively polling and detecting GPIO signals. Background Art
[0002] GPIO (General-Purpose Input / Output) interrupt is a hardware mechanism that allows external events to trigger an interrupt service routine in a microcontroller. It detects the state changes of GPIO signals in real time and generates an interrupt request when a change is detected. GPIO interrupt is crucial for real-time response to external events and is widely used in fields such as data acquisition, peripheral control, and fault detection.
[0003] The response process of GPIO signals is as Figure 1 shown. It only responds when a GPIO signal generates an interrupt. Before actually entering the signal processing process (thread), it is also affected by interrupt delays caused by processes such as interrupt detection, interrupt arbitration, interrupt vector acquisition, and interrupt service routine (ISR), scheduling delays caused by allocating CPU usage rights according to the priority of the signal processing process, and deprivation delays caused by reallocating CPU usage rights to a signal processing process with a higher priority, which brings a lot of additional time overhead for detecting microsecond-level GPIO signals. In addition, problems such as interrupt priority, interrupt conflict, and interrupt nesting also bring uncertain interference, making it difficult to ensure the real-time performance and accuracy of GPIO signal detection. Summary of the Invention
[0004] Based on this, it is necessary to provide a method, system, device, and medium for actively polling and detecting GPIO signals to support the real-time detection of microsecond-level GPIO signals in response to the above technical problems.
[0005] A method for actively polling and detecting GPIO signals, the method comprising:
[0006] Initializing the active polling operation;
[0007] Creating a kernel thread for the active polling operation, and after setting the affinity of the kernel thread to reserve CPU resources, starting the kernel thread;
[0008] During the running of the kernel thread, continuously retain the current GPIO port status bits and determine whether the signal detection is completed. If not, perform GPIO signal detection. When a GPIO signal is detected, enter the signal processing program, retain the current GPIO port status bits, and enter the next round of detection until all GPIO signals are detected;
[0009] After all GPIO signal detections are completed, terminate the active polling operation, stop the kernel thread, and release the resources occupied by the kernel thread.
[0010] In one embodiment, before initializing the active polling operation, it further includes:
[0011] Define a null pointer pointing to a function;
[0012] Create an active polling operation structure, in which an integer variable, a boolean variable, a thread descriptor, a null pointer pointing to a function type variable, and a void variable are created; among them, the integer variable includes the GPIO port number, the status of the active polling operation, and the signal trigger mode; the boolean variable is the termination status of the active polling operation; the thread descriptor is the kernel thread descriptor; the null pointer pointing to the function type variable is the signal processing function; the void variable is a pointer pointing to the void type;
[0013] Create a variable of the active polling operation structure type and put it into the gpio_desc structure.
[0014] In one embodiment, initializing the active polling operation includes:
[0015] Convert the GPIO port number to the corresponding gpio_desc structure pointer;
[0016] Allocate memory space for the variable of the active polling operation structure type;
[0017] Initialize the variable of the active polling operation structure type with the GPIO port number, signal trigger mode, signal processing function, and pointer pointing to the void type, set the termination status of the active polling operation in the variable of the active polling operation structure type to false, and set the status of the active polling operation to the initial state.
[0018] In one embodiment, creating the kernel thread of the active polling operation includes:
[0019] Use the kthread_create function or the application programming interface for creating threads in the kernel to create the kernel thread of the active polling operation and set the thread function.
[0020] In one embodiment, this method further includes:
[0021] In Linux, isolate the CPU resources reserved for the active polling detection of GPIO signals by adding and modifying the GRUB_CMDLINE_LINUX value;
[0022] Among them, adding and modifying the GRUB_CMDLINE_LINUX value includes:
[0023] Add the isolcpus kernel parameter, which is a startup parameter of the kernel used to isolate specified CPUs, ensuring that the isolated CPUs are not used by the scheduler for normal task scheduling and are only used to run specific tasks;
[0024] Configure the interrupt affinity parameter, unload the RCU callback processing parameter, and disable the NMI watchdog parameter; among them, the interrupt affinity parameter is used to bind interrupts to specific CPUs, specifying that certain CPUs handle specific hardware interrupts; the unload RCU callback processing parameter is used to specify that certain CPUs do not handle RCU callbacks but transfer the RCU callbacks to other CPUs for processing; the disable NMI watchdog parameter is used to stop detecting whether the system has a long-term deadlock state;
[0025] Add the disable interrupt optimization allocation parameter and the disable soft-lockup detector parameter; among them, the disable interrupt optimization allocation parameter is used to stop the automatic balancing behavior of the CPUs handling interrupts; the disable soft-lockup detector parameter is used to stop monitoring whether kernel tasks occupy the CPU for a long time.
[0026] In one embodiment, the GPIO signal detection includes:
[0027] If it is detected that the current GPIO port status bit is the trigger bit and the previous GPIO port status bit is the non-trigger bit, it is determined that a GPIO signal is detected and the signal handler is entered; otherwise, it is determined that no GPIO signal is detected, the current GPIO port status bit is directly retained, and the next round of detection is entered.
[0028] A GPIO signal active polling detection system, which is used to implement the above-mentioned GPIO signal active polling detection method. The system includes: a driver layer, a hardware abstraction layer, and a hardware layer;
[0029] The driver layer includes a GPIO device driver; among them, the GPIO device driver is used to encapsulate the kernel thread for active polling detection into the internal gpiolib library;
[0030] The hardware abstraction layer contains CPU architecture-related code, which is used to implement CPU isolation using the CPU reservation mechanism to reserve CPU resources for the active polling detection of GPIO signals;
[0031] The hardware layer includes several reserved CPUs and GPIO devices. The reserved CPUs are isolated based on the CPU architecture-related code, used to provide CPU resources for the active polling detection of GPIO signals, and directly access the GPIO devices by calling the real-time kernel thread for active polling detection in the GPIO device driver of the driver layer.
[0032] A computer device includes a memory and a processor. The memory stores a computer program. When the processor executes the computer program, the following steps are implemented:
[0033] Initialize the active polling operation;
[0034] Create a kernel thread for the active polling operation, and after setting the affinity of the kernel thread to reserved CPU resources, start the kernel thread;
[0035] During the running of the kernel thread, continuously retain the current GPIO port status bit and determine whether the signal detection is completed. If not, perform GPIO signal detection. When a GPIO signal is detected, enter the signal processing program, retain the current GPIO port status bit, and enter the next round of detection until all GPIO signal detections are completed;
[0036] After all GPIO signal detections are completed, terminate the active polling operation, stop the kernel thread, and release the resources occupied by the kernel thread.
[0037] A computer-readable storage medium stores a computer program. When the computer program is executed by a processor, the following steps are implemented:
[0038] Initialize the active polling operation;
[0039] Create a kernel thread for the active polling operation, and after setting the affinity of the kernel thread to reserved CPU resources, start the kernel thread;
[0040] During the running of the kernel thread, continuously retain the current GPIO port status bit and determine whether the signal detection is completed. If not, perform GPIO signal detection. When a GPIO signal is detected, enter the signal processing program, retain the current GPIO port status bit, and enter the next round of detection until all GPIO signal detections are completed;
[0041] After all GPIO signal detections are completed, terminate the active polling operation, stop the kernel thread, and release the resources occupied by the kernel thread.
[0042] The above-mentioned active polling detection method, system, device and medium for GPIO signals have the following beneficial effects compared with the existing GPIO signal response process:
[0043] 1. The GPIO signal detection is carried out by means of active polling. The delay of active polling detection is much smaller than that of interruption, which improves the real-time performance of GPIO signal detection. Moreover, this method allows the system to efficiently process GPIO signals without interruption, reduces the dependence on the interrupt controller, releases more hardware resources for other tasks, and improves the overall system efficiency.
[0044] 2. By reserving CPU resources to run active polling detection, while minimizing the interference of other processes and interruptions as much as possible, it also ensures that the active polling detection will not be preempted by higher-priority processes, thus reducing the scheduling delay and preemption deprivation delay.
[0045] 3. By creating a kernel thread for active polling operation, running the active polling detection at the kernel level reduces the switching between the user mode and the kernel mode, saving the additional overhead and time delay caused by context switching.
[0046] 4. It supports waking up the CPU in advance to enter the active polling detection mode, releasing the CPU resources after the detection is completed, reducing the power consumption of the CPU, and achieving energy efficiency optimization.
[0047] 5. It has scalability. The encapsulation of the kernel thread ensures the integrity of the entire active polling detection method at the kernel level, so as to be extended to detect other types of signals. Description of the Drawings
[0048] Figure 1 It is a schematic diagram of the GPIO signal response process in an embodiment;
[0049] Figure 2 It is a schematic diagram of the flow of a kind of GPIO signal active polling detection in an embodiment;
[0050] Figure 3 It is a schematic diagram of the progress of GPIO signal active polling detection in an embodiment;
[0051] Figure 4 It is a schematic diagram of CPU resource reservation in an embodiment;
[0052] Figure 5 It is a schematic diagram of the comparison between a kind of GPIO signal active polling detection system and an external hardware interrupt system in an embodiment;
[0053] Figure 6 It is a schematic diagram of the effect of GPIO signal active polling detection in an embodiment;
[0054] Figure 7 It is the internal structure diagram of a computer device in an embodiment. Detailed Embodiments
[0055] To make the objectives, technical solutions and advantages of this application more clear and understandable, the following further elaborates on this application in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely used to explain this application and are not intended to limit this application.
[0056] In one embodiment, in order to ensure and improve the real-time performance and accuracy of the system for detecting GPI signals, as Figure 2 and Figure 3 shown, a method for actively polling and detecting GPIO signals is provided. The main process includes four parts: request, start, stop, and release.
[0057] In the request part: Initialize the active polling operation.
[0058] In the start part: Create a kernel thread for the active polling operation, and after setting the affinity of the kernel thread to reserve CPU resources, start the kernel thread; during the running process of the kernel thread, continuously retain the current GPIO port status bits and determine whether the signal detection is completed. If not, perform GPIO signal detection. When a GPIO signal is detected, enter the signal processing program, retain the current GPIO port status bits, and enter the next round of detection until all GPIO signals are detected and enter the stop part.
[0059] In the stop part: Terminate the active polling operation and stop the kernel thread.
[0060] In the release part, release the resources occupied by the kernel thread.
[0061] This method adopts a polling detection mechanism, and the polling delay is lower than the interrupt delay under the same hardware conditions. For example, for Cortex-M core chips, the typical interrupt delay time is 12 - 16 clock cycles, which will be higher if affected by other interrupts. However, in the polling operation, it only takes 1 or several clock cycles from detecting the signal to the reserved CPU directly executing the signal processing program. Therefore, when processing microsecond-level GPIO signals, the improvement in real-time performance brought by polling detection is more obvious.
[0062] In one embodiment, before initializing the active polling operation, it further includes:
[0063] Define a null pointer to point to the function polling_handler_t;
[0064] Create an active polling operation structure poll_info, in which there are integer variables, boolean variables, thread descriptors, function type variables pointed to by null pointers, and void variables; among them, the integer variables include the GPIO port number gpio, the status status of the active polling operation, and the signal trigger mode trigger; the boolean variable is the termination status terminated of the active polling operation; the thread descriptor is the kernel thread descriptor polling_task; the function type variable pointed to by the null pointer poll_handler_t is the signal processing function handler; the void variable is the pointer arg pointing to the void type.
[0065] Create a variable poll of the active polling operation structure poll_info type and put it into the gpio_desc structure.
[0066] In one embodiment, initialize the active polling operation, including:
[0067] Convert the GPIO port number gpio to the corresponding gpio_desc structure pointer desc;
[0068] Allocate memory space for the variable poll of the active polling operation structure type;
[0069] Use the GPIO port number gpio, the signal trigger mode trigger, the signal processing function handler, and the pointer arg pointing to the void type to initialize the variable poll of the active polling operation structure type, set the termination status of the active polling operation in the variable poll of the active polling operation structure type to false, that is, set terminated to false, and set the status of the active polling operation to the initial state, that is, set status to GPIOF_STATUS_NONE.
[0070] In one embodiment, create a kernel thread for the active polling operation, including:
[0071] Use the kthread_create function or other methods such as the application programming interface for creating threads in the kernel to create a kernel thread for the active polling operation and set the thread function.
[0072] Furthermore, the present method also adopts a CPU resource reservation mechanism. In a system that requires real-time processing, CPU resource reservation is one of the key technologies to ensure that tasks can be completed within the specified time. CPU resource reservation means that the system reserves a certain proportion of CPU time or processing capacity for specific tasks or processes to ensure that these tasks or processes can obtain the necessary computing resources and maintain a certain level of performance and response time even under high system load. Therefore, critical tasks and processes will not be severely affected by the competition of other processes, and the frequency of context switching can be reduced, which helps to improve CPU utilization and maintain the stability and responsiveness of the entire system.
[0073] During Figure 1 the GPIO signal response process shown, there is no CPU resource reservation. At this time, after the external hardware interrupt experiences an interrupt delay, the process from the ISR wake-up signal processing process to the kernel process scheduler selecting the signal processing process woken up by the ISR and allocating CPU usage rights will incur a scheduling delay. Obviously, when the number of processes is larger and there are more processes with higher priorities, the time overhead of context switching caused by the kernel process scheduler will become larger and larger. At the same time, due to the existence of processes with higher priorities, they will deprive the signal processing process of the CPU resources it uses, resulting in a deprivation delay. When the deprivation delay exceeds the Deadline, it is very likely that the signal processing cannot end before the Deadline, which will lead to an increase in the detection delay of the next signal and even miss detecting the next signal, bringing great pressure to the subsequent signal detection.
[0074] Therefore, the present application reserves CPU resources for the active polling detection of GPIO signals by using the method of CPU resource reservation. As Figure 4 shown, when applying the CPU resource reservation mechanism in an external hardware interrupt system, at the hardware level, one or more CPU cores can be specifically reserved (which is equivalent to isolating this CPU) and only used for subsequent execution of the active polling detection task without processing any other processes. At the same time, the interrupts that interfere with the active polling detection on the reserved CPU cores are transferred to other non-reserved CPU cores. At this time, the reserved CPU does not need to select the signal processing process from numerous processes through the kernel process scheduler and allocate usage rights, and the signal processing process will not be deprived of CPU usage rights by other processes with higher priorities when it is running. When the reserved CPU detects a GPIO signal, it will immediately enter the signal processing program. After processing is completed, the reserved CPU will return and continue to detect the GPIO signal. Only when all signals have been detected will the reserved CPU stop this thread and release its usage rights so that other tasks can utilize the CPU resources.
[0075] In one embodiment, in Linux, CPU resources reserved for active polling detection of GPIO signals are isolated by adding and modifying the GRUB_CMDLINE_LINUX value.
[0076] Among them, adding and modifying the GRUB_CMDLINE_LINUX value includes:
[0077] (1) Add the isolcpus kernel parameter, which is a startup parameter of the kernel used to isolate specified CPUs, ensuring that the isolated CPUs will not be used by the scheduler for ordinary task scheduling and are only used to run specific tasks, such as tasks with high real-time requirements. For example, isolcpus=3 isolates the 4th CPU (CPU number is 3).
[0078] (2) Configure the interrupt affinity parameter, unload the RCU callback handling parameter, and turn off the NMI watchdog parameter.
[0079] Interrupt affinity parameter (irqaffinity): used to bind interrupts to specific CPUs, specifying that certain CPUs handle specific hardware interrupts. Based on this parameter, context switches and cache misses can be reduced, thereby improving performance. For example, if the interrupt affinity is set to the 1st CPU core, then irqaffinity=0.
[0080] Unload the RCU callback handling parameter (rcu_nocbs): RCU (Read-Copy-Update) is a synchronization mechanism in the kernel that allows multiple readers to access data simultaneously, while writers update the data after copying it. RCU callbacks are executed during memory reclaim. rcu_nocbs is used to specify that certain CPUs do not handle RCU callbacks but transfer the RCU callbacks to other CPUs for processing, which helps reduce the load on specific CPUs and improve system performance. For example, if the 4th CPU is isolated, then set rcu_nocbs=3.
[0081] Turn off the NMI watchdog parameter (nmi_watchdog=0): NMI (Non-Maskable Interrupt) is the highest-level hardware interrupt and cannot be disabled by software. The NMI watchdog is a mechanism used to detect whether the system has entered a long-term locked state, that is, the CPU is unable to respond for a long time. Turning off the NMI watchdog parameter is used to stop detecting whether the system has entered a long-term locked state and is usually used when it is known that the system will not lock up or when this mechanism needs to be disabled to avoid false alarms.
[0082] (3)Add parameters to optimize the distribution by disabling interrupts and to disable the soft-lockup detector parameter.
[0083] Parameter to optimize the distribution by disabling interrupts (noirqbalance): irqbalance is a daemon in the system that automatically balances interrupt handling so that all CPU cores handle interrupts as evenly as possible. noirqbalance is used to stop the automatic balancing behavior of the CPU for interrupt handling and is typically used after manually setting the interrupt affinity to maintain the stability of interrupt handling.
[0084] Parameter to disable the soft-lockup detector (nosoftlockup): The soft-lockup detector is used to monitor whether the system is unable to schedule other tasks because some kernel tasks occupy the CPU for a long time. If such a situation is detected, the system may print a stack trace and enter a locked state or trigger a panic. nosoftlockup is used to stop monitoring whether kernel tasks occupy the CPU for a long time. Disabling this detector is usually used for system debugging or to reduce the noise of system logs when it is known that the system will not have a soft-lockup.
[0085] Note that when using other non-Linux kernels, other kernel parameters may also need to be set. For example, in Xenomai:
[0086] Set xenomai.supported_cpus: Used to specify the list of CPU cores that Xenomai can use. This parameter is usually passed to the Xenomai kernel through the kernel's command line (cmdline) at system startup. In this way, users can restrict Xenomai to execute its tasks only on specific CPU cores, thereby improving the real-time performance of the system. For example, xenomai.supported_cpus=0x0f means that Xenomai supports all 4 CPUs numbered 0, 1, 2, and 3;
[0087] Disable xenomai.smi (xenomai.smi=disabled): Used to turn off the detection and warning of SMI (System Management Interrupts). SMI is a high-priority interrupt triggered by the CPU for performing system management tasks implemented in the BIOS (Basic Input / Output System), such as power management, hardware monitoring, etc. These interrupts can interrupt the current real-time task, resulting in unpredictable delays, which are unacceptable for real-time systems.
[0088] In one of the embodiments, the GPIO signal detection includes:
[0089] If it is detected that the current GPIO port status bit is the trigger bit and the previous GPIO port status bit is the non - trigger bit, it is determined that a GPIO signal is detected and the signal processing program is entered; otherwise, it is determined that no GPIO signal is detected, the current GPIO port status bit is directly retained, and the next round of detection is entered.
[0090] In one embodiment, the present application also provides a GPIO signal active polling detection system, which is used to implement the above - mentioned GPIO signal active polling detection method. The system includes: a driver layer, a hardware abstraction layer, and a hardware layer;
[0091] The driver layer includes a GPIO device driver; among them, the GPIO device driver is used to encapsulate the kernel thread of the active polling detection into the internal gpiolib library;
[0092] The hardware abstraction layer contains CPU architecture - related code, and the CPU architecture - related code is used to implement CPU isolation by using the CPU reservation mechanism to reserve CPU resources for the active polling detection of GPIO signals;
[0093] The hardware layer includes several reserved CPUs and GPIO devices. The reserved CPUs are isolated based on the CPU architecture - related code, and are used to provide CPU resources for the active polling detection of GPIO signals, and directly access the GPIO devices by calling the real - time kernel thread of the active polling detection in the GPIO device driver of the driver layer.
[0094] Furthermore, as shown in Figure 5, the GPIO signal active polling detection system is compared with the external hardware interrupt system in the same architecture. Figure 5 The solid - line part in the figure represents the GPIO signal active polling detection system that combines the kernel - thread active polling detection and CPU resource reservation. Figure 5 The dashed - line part in the figure represents the hardware interrupt system. The interrupt signal is from the peripherals (i.e., devices and GPIO devices) to the interrupt controller, which is uniformly managed by the interrupt controller, and then routed to the CPU core. When an interrupt is triggered by the peripherals, corresponding callback processing is performed through the drive of the interrupt controller. Figure 5 It can be seen that the present application can directly access the GPIO device without the drive of the interrupt controller by adding the kernel thread of the active polling detection to the gpiolib library of the GPIO device driver in the driver layer of the GPIO signal active polling detection system and combining the CPU resources reserved in the hardware layer (i.e., reserving CPU Core1).
[0095] Among them, in the GPIO signal active polling detection system, the kernel thread for active polling detection is encapsulated into the gpiolib library in the GPIO device driver. By using kernel-level threads, on the one hand, if processes (threads) in the user space are used, processes (threads) can only access limited resources and perform limited operations because the operating system has restrictions on the instructions they can execute and the memory address spaces they can access. On the contrary, in the kernel space, there are no such restrictions, and there is no need for the operating system to frequently switch between the user space and the kernel space. Additionally, there is scalability in the kernel. Besides GPIO signals, the system has other signals. By encapsulating at the kernel level, the connection of each part of this method is maintained, ensuring the integrity of the entire method so that it can be extended to other kernel drivers to detect other types of signals.
[0096] Furthermore, to more effectively illustrate the beneficial effects of the method / system proposed in this application, experimental verification was conducted on the above-mentioned GPIO signal active polling detection method / system. The detection effect of the GPIO signal active polling is as Figure 6 shown. This method / system starts in advance before the GPIO signal arrives and stops detecting after the detection is completed. During the detection process, by combining kernel-level active polling detection and CPU resource reservation, this application realizes that the reserved CPU will continuously poll until the generated GPIO signal is detected. After that, the CPU will enter the signal processing process until the signal processing is completed. Finally, the CPU will continue to detect the GPIO signal, and it can achieve a kernel real-time driver for the reserved CPU to actively poll and detect microsecond-level GPIO signals.
[0097] In one embodiment, a computer device is provided. This computer device can be a terminal, and its internal structure diagram can be as Figure 7 shown. The computer device includes a processor, a memory, a network interface, a display screen, and an input device connected through a system bus. Among them, the processor of this computer device is used to provide computing and control capabilities. The memory of this computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The network interface of this computer device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor, it realizes a GPIO signal active polling detection method. The display screen of this computer device can be a liquid crystal display screen or an electronic ink display screen. The input device of this computer device can be a touch layer covering the display screen, or a button, a trackball, or a touchpad set on the outer shell of the computer device, or an external keyboard, touchpad, or mouse, etc.
[0098] Those skilled in the art can understand, Figure 7The structure shown is only a block diagram of some structures related to the solution of this application, and does not constitute a limitation on the computer device to which the solution of this application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine some components, or have different component arrangements.
[0099] In one embodiment, a computer device is provided, including a memory and a processor. The memory stores a computer program, and when the processor executes the computer program, the following steps are implemented:
[0100] Initialize the active polling operation;
[0101] Create a kernel thread for the active polling operation, and after setting the affinity of the kernel thread to reserve CPU resources, start the kernel thread;
[0102] During the running of the kernel thread, continuously retain the current GPIO port status bits and determine whether the signal detection is completed. If not completed, perform GPIO signal detection. When a GPIO signal is detected, enter the signal processing program, and retain the current GPIO port status bits, and enter the next round of detection until all GPIO signal detections are completed;
[0103] After all GPIO signal detections are completed, terminate the active polling operation, stop the kernel thread, and release the resources occupied by the kernel thread.
[0104] In one embodiment, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the following steps are implemented:
[0105] Initialize the active polling operation;
[0106] Create a kernel thread for the active polling operation, and after setting the affinity of the kernel thread to reserve CPU resources, start the kernel thread;
[0107] During the running of the kernel thread, continuously retain the current GPIO port status bits and determine whether the signal detection is completed. If not completed, perform GPIO signal detection. When a GPIO signal is detected, enter the signal processing program, and retain the current GPIO port status bits, and enter the next round of detection until all GPIO signal detections are completed;
[0108] After all GPIO signal detections are completed, terminate the active polling operation, stop the kernel thread, and release the resources occupied by the kernel thread.
[0109] Those of ordinary skill in the art can understand that all or part of the processes in the methods of the above embodiments can be completed by instructing relevant hardware through a computer program. The computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above methods. Among them, any reference to a memory, storage, database, or other medium used in the embodiments provided in the present application can include non-volatile and / or volatile memories. Non-volatile memories can include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM), or flash memory. Volatile memories can include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in various forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDR SDRAM), enhanced SDRAM (ESDRAM), synchronous link DRAM (SLDRAM), Rambus direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and Rambus dynamic RAM (RDRAM), etc.
[0110] The technical features of the above embodiments can be combined arbitrarily. For the sake of brevity of description, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, it should be considered as the scope described in this specification.
[0111] The above-described embodiments only represent several implementation manners of the present application. Their descriptions are relatively specific and detailed, but they should not be construed as limiting the scope of the present application. It should be noted that for those of ordinary skill in the art, without departing from the concept of the present application, several modifications and improvements can still be made, and these all belong to the protection scope of the present application. Therefore, the protection scope of the present application should be subject to the appended claims.
Claims
1. A method for actively polling and detecting GPIO signals, characterized in that, The method includes: Initializing the active polling operation; Creating a kernel thread for the active polling operation, and after setting the affinity of the kernel thread to reserve CPU resources, starting the kernel thread; During the running of the kernel thread, the current GPIO port status bits are retained in real time and it is judged whether the signal detection is completed. If not, GPIO signal detection is performed. When a GPIO signal is detected, the signal handler is entered, and the current GPIO port status bits are retained, and the next round of detection is entered until all GPIO signal detections are completed; After all GPIO signal detections are completed, the active polling operation is terminated, the kernel thread is stopped, and the resources occupied by the kernel thread are released; The method further includes: In Linux, the CPU resources reserved for the active polling detection of GPIO signals are isolated by adding and modifying the GRUB_CMDLINE_LINUX value; wherein, adding and modifying the GRUB_CMDLINE_LINUX value includes: Adding the isolcpus kernel parameter, which is a startup parameter of the kernel and is used to isolate the specified CPU to ensure that the isolated CPU will not be used by the scheduler for ordinary task scheduling and is only used to run specific tasks; Configuring the interrupt affinity parameter, unloading the RCU callback processing parameter, and turning off the NMI watchdog parameter; wherein, the interrupt affinity parameter is used to bind the interrupt to a specific CPU to specify that certain CPUs handle specific hardware interrupts; the unloading RCU callback processing parameter is used to specify that certain CPUs do not handle RCU callbacks but transfer the RCU callbacks to other CPUs for processing; the turning off the NMI watchdog parameter is used to stop detecting whether the system has a long-term deadlock state; Adding the disabled interrupt optimization allocation parameter and the disabled soft-lockup detector parameter; wherein, the disabled interrupt optimization allocation parameter is used to stop the automatic balancing behavior of the CPU for interrupt processing; the disabled soft-lockup detector parameter is used to stop monitoring whether the kernel task occupies the CPU for a long time.
2. The method according to claim 1, wherein Before initializing the active polling operation, it further includes: Defining a function pointer to a null pointer; Creating an active polling operation structure, in which an integer variable, a boolean variable, a thread descriptor, a function pointer variable of the null pointer type, and a void variable are created; wherein, the integer variable includes the GPIO port number, the status of the active polling operation, and the signal trigger mode; the boolean variable is the termination status of the active polling operation; the thread descriptor is the kernel thread descriptor; the function pointer variable of the null pointer type is the signal handler; the void variable is a pointer to the void type; Creating a variable of the active polling operation structure type and putting it into the gpio_desc structure.
3. The method according to claim 2, wherein Initializing the active polling operation includes: Converting the GPIO port number to a pointer to the corresponding gpio_desc structure; Allocating memory space for the variable of the active polling operation structure type; Initialize the variable of the active polling operation structure type by using the GPIO port number, signal trigger mode, signal processing function, and a pointer to the void type. Set the termination status of the active polling operation in the active polling operation structure type variable to false, and set the status of the active polling operation to the initial status.
4. The method according to claim 1, wherein Create a kernel thread for the active polling operation, including: Use the kthread_create function or the application programming interface for creating threads in the kernel to create a kernel thread for the active polling operation and set the thread function.
5. The method according to claim 1, characterized in that, The GPIO signal detection includes: If it is detected that the current GPIO port status bit is the trigger bit and the previous GPIO port status bit is the non-trigger bit, it is determined that a GPIO signal is detected and the signal processing program is entered; otherwise, it is determined that no GPIO signal is detected, the current GPIO port status bit is directly retained, and the next round of detection is entered.
6. A GPIO signal active polling detection system, characterized in that The system is used to implement the method for active polling detection of GPIO signals described in any one of claims 1-5. The system includes: a driver layer, a hardware abstraction layer, and a hardware layer; The driver layer includes a GPIO device driver; wherein, the GPIO device driver is used to encapsulate the kernel thread of the active polling detection into the internal gpiolib library; The hardware abstraction layer contains code related to the CPU architecture. The code related to the CPU architecture is used to implement CPU isolation by using the CPU reservation mechanism to reserve CPU resources for the active polling detection of GPIO signals; The hardware layer includes several reserved CPUs and GPIO devices. The reserved CPUs are isolated based on the code related to the CPU architecture and are used to provide CPU resources for the active polling detection of GPIO signals, and directly access the GPIO devices by calling the real-time kernel thread of the active polling detection in the GPIO device driver of the driver layer.
7. A computer device, comprising a memory and a processor, the memory storing a computer program, characterized in that, When the processor executes the computer program, the steps of the method described in any one of claims 1 to 5 are implemented.
8. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by the processor, the steps of the method described in any one of claims 1 to 5 are implemented.