A microprocessor connected fieldbus system and method
Through initialization, I/O interrupt, and thread processing procedures, the microprocessor connects to an independent fieldbus controller using general-purpose I/O ports, solving the problem of exceeding the limit on the number of fieldbuses and achieving real-time communication and efficient resource utilization.
Patent Information
- Application Number
- CN202410655280.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-24
- Publication Date
- 2025-11-28
- Estimated Expiration
- 2044-05-24
AI Technical Summary
In existing technologies, when a microprocessor connects to a fieldbus, it cannot effectively handle situations where the number of fieldbuses exceeds the sum of the number of built-in controllers and serial interfaces of the microprocessor. Furthermore, simulating serial communication by reading and writing general-purpose I/O ports in software consumes a lot of resources and affects the real-time performance of other tasks.
By using initialization, I/O interrupt, and thread processing procedures, the microprocessor's general-purpose I/O ports are connected to an independent fieldbus controller, and I/O interrupt response and adaptive thread scheduling are configured to ensure real-time performance and resource utilization efficiency.
It enables real-time communication between the microprocessor and multiple fieldbuses, avoiding impact on other tasks and ensuring real-time communication and efficient resource utilization.
Smart Images

Figure CN118746934B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of communication application, in particular to a system and method for connecting field bus by microprocessor. BACKGROUND
[0002] Field bus is widely used in industrial communication field. In order to realize the connection with field bus, the microprocessor inside the field bus communication node is usually connected with the independent field bus controller with serial interface through the built-in field bus controller of the microprocessor or the serial interface of the microprocessor. However, the above method can only cope with the case that the number of field buses needed to be connected by the field bus communication node does not exceed the sum of the number of built-in field bus controllers of the microprocessor and the number of serial interfaces of the microprocessor.
[0003] In the prior art, a method for microprocessor to simulate serial communication based on software reading and writing general IO port has appeared. Compared with the serial communication port of the microprocessor, the microprocessor simulating serial communication based on software reading and writing general IO port occupies more microprocessor resources and execution time, so when the communication data volume is large, necessary countermeasures need to be taken to ensure the real-time performance of communication and avoid the impact on other tasks in the microprocessor software. However, the existing relevant patent documents do not give the corresponding countermeasures.
[0004] For example, the patent document 1 (103442418A) proposes a coalfield fire area CC2530 node high-temperature data acquisition and synchronous hibernation control method, which uses general IO port to simulate I2C communication and SPI communication. In the claims of the patent document, the communication is periodically performed in a fixed cycle.
[0005] For another example, the patent document 2 (102654857A) proposes a communication method for simulating SPI protocol and communicating with SD card by using GPIO interface. In the claims of the patent document, the communication is sequentially performed.
[0006] In summary, the method for microprocessor to simulate serial communication based on software reading and writing general IO port can be applied to the connection between microprocessor and field bus, that is, the microprocessor is connected with the independent field bus controller with serial interface through general IO port, and then connected with field bus, which can be used as a supplementary means when the number of field buses needed to be connected by the field bus communication node is large. However, necessary countermeasures need to be taken for simulating serial communication, on the one hand to ensure the real-time performance of communication, and on the other hand to avoid the impact on other tasks in the microprocessor software. SUMMARY
[0007] The present application aims to provide a system and method for connecting field bus by microprocessor, which solves the above technical problems.
[0008] The object of the present application can be realized by the following technical solutions.
[0009] A system and method for connecting a field bus by a microprocessor, comprising an initialization procedure, an IO interrupt procedure and a thread processing procedure;
[0010] The initialization procedure comprises the following steps:
[0011] Step one: configure the parameters of an independent field bus controller based on the microprocessor general IO port A analog serial communication, so that when receiving a field bus message, the receiving interrupt IO port C of the independent field bus controller outputs a valid level, and go to step two;
[0012] Step two: configure the microprocessor general IO port B, so that when the input level is valid, an IO interrupt is generated, and go to step three;
[0013] Step three: assign an initial value to the thread scheduling period T0; create and start a message transceiving thread, and the whole initialization procedure step ends.
[0014] The IO interrupt procedure comprises the following steps:
[0015] Step four: set the message receiving flag to be valid; measure the current value Tx of the IO interrupt interval, and go to step five;
[0016] Step five: if the current value Tx of the IO interrupt interval < the minimum value Tm of the IO interrupt interval, go to step six; otherwise, the whole IO interrupt procedure step ends;
[0017] Step six: update the minimum value Tm of the IO interrupt interval, let Tm = Tx, and go to step seven;
[0018] Step seven: delete the current thread; update the thread scheduling period T0, let T0 = K·Tm, K < 1; create and start a new thread, and the whole IO interrupt procedure step ends.
[0019] The thread processing procedure comprises the following steps:
[0020] Step eight: if the message receiving flag is valid, go to step nine; otherwise, go to step eleven;
[0021] Step nine: receive a message based on the microprocessor general IO port A analog serial communication, and go to step ten;
[0022] Step ten: clear the receiving interrupt based on the microprocessor general IO port A analog serial communication, and go to step eleven;
[0023] Step eleven: if the microprocessor has a message to send, go to step twelve; otherwise, the whole thread processing procedure step ends;
[0024] Step twelve: analog serial communication is carried out based on the microprocessor general IO port A to send the message, and the whole thread processing flow step ends.
[0025] As a further scheme of the present application: in the step one, the microprocessor general IO port A is a group of ports, and the port number is related to the serial bus type; the receiving interrupt IO port C of the independent field bus controller is one port; in the step two, the microprocessor general IO port B is one port.
[0026] As a further scheme of the present application: in the step four, the IO interrupt interval current value Tx is the time interval between the last IO interrupt and the present IO interrupt.
[0027] As a further scheme of the present application: in the step one, the serial communication includes I2C and SPI, and the field bus includes CAN, LonWorks and Profibus.
[0028] A system of a microprocessor connecting field bus method, the system includes: a microprocessor, an independent field bus controller and a field bus transceiver;
[0029] The microprocessor includes a message transceiving thread processing module, an IO interrupt processing module, a general IO port A and a general IO port B.
[0030] The message transceiving thread processing module includes a thread scheduling module, a message receiving module, a message sending module and a serial communication analog module.
[0031] The IO interrupt processing module includes an IO interrupt response module and a thread scheduling period self-adaptive module.
[0032] The independent field bus controller includes a serial interface, a field bus interface and a receiving interrupt IO port C.
[0033] When the independent field bus controller receives the field bus message, the receiving interrupt IO port C outputs a valid level.
[0034] When the general IO port B detects the valid level, the IO interrupt is generated.
[0035] The IO interrupt response module sets the message receiving flag to be valid, and calculates the IO interrupt interval current value Tx.
[0036] The thread scheduling period adaptive module compares the current value Tx of the IO interrupt interval with the minimum value Tm of the IO interrupt interval, and if the current value Tx of the IO interrupt interval < the minimum value Tm of the IO interrupt interval, the following operations are performed: updating the minimum value Tm of the IO interrupt interval, letting Tm = Tx; deleting the current thread; updating the thread scheduling period T0, letting T0 = K·Tm, K < 1; creating and starting a new thread;
[0037] The thread scheduling module calls the packet receiving module and the packet sending module every thread scheduling period T0.
[0038] The packet receiving module calls the serial communication simulation module to receive a packet when the packet receiving flag is valid, and clears the receiving interrupt.
[0039] The packet sending module calls the serial communication simulation module to send a packet when the microprocessor has a packet to send.
[0040] The serial communication simulation module simulates serial communication based on the general IO port A of the microprocessor.
[0041] The beneficial effects of the present application are as follows: even if the number of fieldbus nodes that need to be connected exceeds the sum of the number of fieldbus controllers built-in the microprocessor and the number of serial interfaces of the microprocessor, the microprocessor can connect independent fieldbus controllers with serial interfaces through the general IO port, and then connect the fieldbus; when the independent fieldbus controller receives a fieldbus packet, the packet receiving flag is set to be valid in the IO interrupt process, and the thread scheduling period is adaptively adjusted according to the IO interrupt interval, and the fieldbus packet is received in the thread according to the packet receiving flag, thereby ensuring the real-time performance of the communication; the receiving and sending of the fieldbus packet are performed in the thread rather than in the interrupt, thereby avoiding the influence of the continuous reading and writing of the general IO port on other tasks in the software of the microprocessor. BRIEF DESCRIPTION OF DRAWINGS
[0042] The present application will be further described below with reference to the accompanying drawings.
[0043] Figure 1 is a structural schematic diagram of a system for connecting a fieldbus by a microprocessor according to the present application;
[0044] Figure 2 is a flowchart of an initialization process in a method for connecting a fieldbus by a microprocessor according to the present application;
[0045] Figure 3 is a flowchart of an IO interrupt process in a method for connecting a fieldbus by a microprocessor according to the present application;
[0046] Figure 4It is a flow chart of thread flow in a microprocessor connecting field bus method of the present application;
[0047] Figure 5 It is a field bus message transceiving timing diagram in a microprocessor connecting field bus method of the present application.
[0048] In the figure: 1, microprocessor; 11, message transceiving thread processing module; 111, thread scheduling module; 112, message receiving module; 113, message sending module; 114, serial communication simulation module; 12, IO interrupt processing module; 121, IO interrupt response module; 122, thread scheduling period self-adapting module; 13, general IO port A; 14, general IO port B; 2, independent field bus controller; 21, serial interface; 22, field bus interface; 23, receiving interrupt IO port C; 3, field bus transceiver. DETAILED DESCRIPTION
[0049] The technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, rather than all the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative work fall within the scope of protection of the present application.
[0050] Embodiment 1
[0051] In this embodiment, the field bus is CAN bus, the independent CAN bus controller has SPI serial interface, and the present application is a microprocessor connecting CAN bus method, including initialization flow, IO interrupt flow and thread processing flow.
[0052] Please refer to Figure 2 As shown in the figure, the initialization flow includes the following steps:
[0053] S1: based on the microprocessor general IO port A, simulate SPI communication to configure parameters of the independent CAN bus controller, so that when receiving CAN bus message, the receiving interrupt IO port C of the independent CAN bus controller outputs valid level, and turn to S2;
[0054] S2: configure the microprocessor general IO port B, so that when the input level is valid, IO interrupt is generated, and turn to S3;
[0055] S3: assign initial value to thread scheduling period T0; create and start message transceiving thread, and the whole initialization flow step is ended.
[0056] Please refer to Figure 3 As shown in the figure, the IO interrupt flow includes the following steps:
[0057] S4: set the message receiving flag to valid; measure the current value Tx of the IO interrupt interval, and go to S5;
[0058] S5: if the current value Tx of the IO interrupt interval < the minimum value Tm of the IO interrupt interval, go to S6; otherwise, the whole IO interrupt procedure ends;
[0059] S6: update the minimum value Tm of the IO interrupt interval, let Tm = Tx, and go to S7;
[0060] S7: delete the current thread; update the thread scheduling period T0, let T0 = K·Tm, K = 0.9; create and start a new thread, and the whole IO interrupt procedure ends.
[0061] Referring to Fig. 2, Figure 4 the thread processing procedure includes the following steps:
[0062] S8: if the message receiving flag is valid, go to S9; otherwise, go to S11;
[0063] S9: receive CAN messages based on the SPI serial communication simulated by the general IO port A of the microprocessor, and go to S10;
[0064] S10: clear the receiving interrupt based on the SPI serial communication simulated by the general IO port A of the microprocessor, and go to S11;
[0065] S11: if the microprocessor has CAN messages to be sent, go to S12; otherwise, the whole thread processing procedure ends;
[0066] S12: send messages based on the SPI serial communication simulated by the general IO port A of the microprocessor, and the whole thread processing procedure ends.
[0067] Referring to Fig. 2, Figure 5 by the initialization procedure, the IO interrupt procedure and the thread processing procedure, the CAN bus message receiving and sending timing is realized: when the independent CAN bus controller receives a CAN bus message, the message receiving flag is set to valid by triggering the IO interrupt; the thread is called periodically, the messages in the microprocessor are sent as needed, and the reading of the CAN bus message is executed when the message receiving flag is valid.
[0068] The method for connecting CAN bus by microprocessor in the embodiment is characterized in that the general IO port A of the microprocessor is a group of ports, the number of ports is 4, and the general IO port A is consistent with the SPI serial bus; the receiving interrupt IO port C of the independent CAN bus controller is one port; the general IO port B of the microprocessor is one port; and the current value Tx of the IO interrupt interval is the time interval between the last IO interrupt and the present IO interrupt.
[0069] Embodiment 2
[0070] Referring to Figure 1 The system for connecting CAN bus by microprocessor in the embodiment is characterized in that the system comprises a microprocessor 1, an independent CAN bus controller 2, and a CAN bus transceiver 3.
[0071] In the embodiment, when the independent CAN bus controller (2) receives a CAN bus message, a receiving interrupt is generated, and the receiving interrupt IO port C23 outputs a valid level;
[0072] When the general IO port B 14 detects the valid level, an IO interrupt is generated;
[0073] The IO interrupt response module 121 sets a message receiving flag to be valid, and calculates a current value Tx of the IO interrupt interval;
[0074] The thread scheduling period adaptive module 122 compares the current value Tx of the IO interrupt interval with a minimum value Tm of the IO interrupt interval, and if the current value Tx of the IO interrupt interval < the minimum value Tm of the IO interrupt interval, the following operations are performed: the minimum value Tm of the IO interrupt interval is updated, Tm = Tx; the current thread is deleted; the thread scheduling period T0 is updated, T0 = K·Tm, K = 0.9; and a new thread is created and started;
[0075] The thread scheduling module 111 calls the message receiving module 112 and the message sending module 113 every thread scheduling period T0.
[0076] When the message receiving flag is valid, the message receiving module 112 calls the SPI serial communication simulation module 114 to receive a CAN message, and clears the receiving interrupt.
[0077] When the microprocessor has a message to send, the message sending module 113 calls the SPI serial communication simulation module 114 to send a CAN message.
[0078] The SPI serial communication simulation module 114 simulates SPI serial communication based on the general IO port A of the microprocessor.
[0079] The core point of the present application is that the system and method for connecting field bus by using the microprocessor of the present application can connect independent field bus controllers with serial interfaces through general IO ports, and then connect field bus, so as to deal with the situation that the number of field bus required to be connected by the communication node of field bus is large; when a message is generated on the field bus, the message receiving flag is set to be valid through IO interruption, and the thread scheduling period is adaptively adjusted according to the IO interruption interval, and the field bus message is received according to the message receiving flag in the thread, so as to ensure the real-time of communication; the receiving and sending of field bus message are executed in the thread instead of interruption, so as to avoid the influence of the continuous reading and writing of general IO ports by software on other tasks in the microprocessor software.
[0080] The above has described one embodiment of the present application in detail, but the content described is only the preferred embodiment of the present application, and cannot be considered as used for limiting the implementation range of the present application. Any equivalent change and improvement made according to the application range of the present application should still belong to the patent coverage range of the present application.
Claims
1. A method of connecting a microprocessor to a fieldbus, characterized by, The initialization flow, the IO interrupt flow and the thread processing flow are included. The initialization flow includes the following steps: Step one: based on the microprocessor general IO port A analog serial communication, configure the parameters of the independent field bus controller, so that when receiving the field bus message, the receiving interrupt IO port C of the independent field bus controller outputs the effective level, and step two is entered; Step two: configure the microprocessor general IO port B, so that when the input level is effective, IO interrupt is generated, and step three is entered; Step three: the initial value of the thread scheduling period T0 is assigned; the message receiving and sending thread is created and started, and the whole initialization flow step is ended; The IO interrupt flow includes the following steps: Step four: the message receiving flag is set to be effective; the current value Tx of the IO interrupt interval is calculated, and step five is entered; Step five: if the current value Tx of the IO interrupt interval is less than the minimum value Tm of the IO interrupt interval, step six is entered; otherwise, the whole IO interrupt flow step is ended; Step six: the minimum value Tm of the IO interrupt interval is updated, and Tm=Tx is set, and step seven is entered; Step seven: the current thread is deleted; the thread scheduling period T0 is updated, and T0=K·Tm is set, K<1; the new thread is created and started, and the whole IO interrupt flow step is ended; The thread processing flow includes the following steps: Step eight: if the message receiving flag is effective, step nine is entered; otherwise, step eleven is entered; Step nine: based on the microprocessor general IO port A analog serial communication, the received message is processed, and step ten is entered; Step ten: based on the microprocessor general IO port A analog serial communication, the receiving interrupt is cleared, and step eleven is entered; Step eleven: if the microprocessor has a message to be sent, step twelve is entered; otherwise, the whole thread processing flow step is ended; Step twelve: based on the microprocessor general IO port A analog serial communication, the message is sent, and the whole thread processing flow step is ended.
2. The method of connecting a fieldbus to a microprocessor according to claim 1, wherein, In the step one, the microprocessor general IO port A is a group of ports, and the number of ports is related to the type of serial bus; the receiving interrupt IO port C of the independent field bus controller is one port; in step two, the microprocessor general IO port B is one port.
3. The method of claim 1, wherein, In the step four, the current value Tx of the IO interrupt interval is the time interval between the last IO interrupt and the present IO interrupt.
4. The method for connecting a fieldbus with a microprocessor according to claim 1, wherein, In the step one, the serial communication includes I2C and SPI, and the field bus includes CAN, LonWorks and Profibus.
5. The system for connecting a field bus to a microprocessor according to claim 1, wherein, The system includes: a microprocessor (1), an independent field bus controller (2) and a field bus transceiver (3); The microprocessor (1) includes a message receiving and sending thread processing module (11), an IO interrupt processing module (12), a general IO port A (13) and a general IO port B (14); The message receiving and sending thread processing module (11) includes a thread scheduling module (111), a message receiving module (112), a message sending module (113) and a serial communication analog module (114); The IO interrupt processing module (12) comprises an IO interrupt response module (121) and a thread scheduling period self-adapting module (122). The independent field bus controller (2) comprises a serial interface (21), a field bus interface (22) and a receiving interrupt IO port C (23).
6. A system for connecting a fieldbus to a microprocessor according to claim 5, wherein, When the independent field bus controller (2) receives a field bus message, a receiving interrupt is generated, and the receiving interrupt IO port C (23) outputs a valid level. When the general IO port B (14) detects the valid level, an IO interrupt is generated. The IO interrupt response module (121) sets a message receiving flag to be valid, and calculates a current value Tx of an IO interrupt interval. The thread scheduling period self-adapting module (122) compares the current value Tx of the IO interrupt interval with a minimum value Tm of the IO interrupt interval, and if the current value Tx of the IO interrupt interval is less than the minimum value Tm of the IO interrupt interval, the following operations are performed: the minimum value Tm of the IO interrupt interval is updated, and Tm=Tx; a current thread is deleted; a thread scheduling period T0 is updated, and T0=K·Tm, K<1; a new thread is created and started. The thread scheduling module (111) calls the message receiving module (112) and the message sending module (113) every thread scheduling period T0. When the message receiving module (112) detects that the message receiving flag is valid, the serial communication simulation module (114) is called to receive a message, and the receiving interrupt is cleared. When the message sending module (113) detects that the microprocessor has a message to be sent, the serial communication simulation module (114) is called to send the message. The serial communication simulation module (114) simulates serial communication based on the general IO port A of the microprocessor.
Citation Information
Patent Citations
Method for realizing CANopen slave station
CN101854300A
Development method of remote IO node
CN116319150A