Communication method based on GPIO

文档序号:567674 发布日期:2021-05-18 浏览:3次 中文

阅读说明:本技术 一种基于gpio的通信方法 (Communication method based on GPIO ) 是由 罗继 胡胜发 于 2021-01-28 设计创作,主要内容包括:本发明公开了一种基于GPIO的通信方法,通过利用两个GPIO线路轮流作为信号线和数据线以实现主端设备和从端设备之间的数据传输,具体地,主端设备通过GPIO输出电平或利用从端的上下拉来通知从端设备接收数据和发送数据给从端设备,从端设备通过切换自身GPIO内部上下拉来给主端设备应答信号。本发明无需通过延时来模拟时钟或者波特率来达到数据同步,而是使用握手应答的方式进行同步从而达到稳定通信的目的,能有效节省通信时间,而且能自适应通信设备的内部环境变化,如频率改变,发生中断等。(The invention discloses a communication method based on GPIO, which uses two GPIO lines as a signal line and a data line in turn to realize data transmission between a master end device and a slave end device, specifically, the master end device informs the slave end device to receive data and send data to the slave end device through GPIO output level or pull-up and pull-down of the slave end, and the slave end device responds signals to the master end device by switching the internal pull-up and pull-down of the GPIO. The invention does not need to simulate the clock or baud rate by time delay to achieve data synchronization, but uses a handshake response mode to perform synchronization so as to achieve the aim of stable communication, can effectively save communication time, and can be adaptive to the internal environment change of communication equipment, such as frequency change, interruption and the like.)

1. A GPIO-based communication method is applied between a master end device and a slave end device, wherein a first GPIO port of the master end device is connected with a first GPIO port of the slave end device through a first GPIO line, and a second GPIO port of the master end device is connected with a second GPIO port of the slave end device through a second GPIO line;

the communication method includes the steps of:

s1, adjusting the input and output modes and the pull-up and pull-down of the first GPIO port of the master end, the first GPIO port of the slave end, the second GPIO port of the master end and the second GPIO port of the slave end to an initialization state;

s2, the master end device carries out data transmission notification on the slave end device through the first GPIO line and transmits data to the slave end device through the second GPIO line;

s3, after the slave end equipment receives data, the first GPIO line feeds back data to the master end equipment to receive response signals, and the second GPIO port of the slave end is modified to be pulled up and down to be opposite to the current level;

s4, the master end device carries out data transmission notification on the slave end device through the second GPIO line and transmits data to the slave end device through the first GPIO line;

s5, after the slave end device receives data, the slave end device feeds back data to the master end device through the second GPIO line to receive response signals, and modifies the pull-up and pull-down of the first GPIO port of the slave end to be the opposite level of the current level of the first GPIO port;

and S6, repeating the steps S2 to S5 until the data transmission is finished.

2. The GPIO-based communication method of claim 1, wherein step S1 specifically includes:

setting the input and output modes of the first GPIO port of the master terminal, the first GPIO port of the slave terminal, the second GPIO port of the master terminal and the second GPIO port of the slave terminal as input;

and setting the pull-up and pull-down of the first GPIO port of the master end, the first GPIO port of the slave end, the second GPIO port of the master end and the second GPIO port of the slave end as pull-up.

3. The GPIO-based communication method of claim 1, wherein step S2 is specifically to: the master end device configures the pull-up and pull-down of the first GPIO port and the second GPIO port of the master end to be closed, then modifies the first GPIO port of the master end to be in an output mode and outputs low level through the first GPIO port of the master end to perform data transmission starting notification on the slave end device, and then modifies the second GPIO port of the master end to be in an output mode and transmits data to the slave end device through the second GPIO line, and then modifies the first GPIO port of the master end to be in an input mode and enables the port level of the first GPIO port to be turned over to perform data preparation ready notification on the slave end device;

step S3 specifically includes: after the slave end equipment receives data, firstly modifying the pull-up and pull-down of a second GPIO port of the slave end to be opposite to the current level of the second GPIO port of the slave end, and then feeding back a data receiving response signal to the master end equipment by modifying the pull-up and pull-down of a first GPIO port of the slave end to be opposite to the current level of the first GPIO port of the slave end;

step S4 specifically includes: after the master end device modifies the first GPIO port of the master end into an output mode and transmits data to the slave end device through the first GPIO line, the second GPIO port of the master end is modified into an input mode to enable the port level of the second GPIO port to be turned over so as to inform the slave end device of data readiness;

step S5 specifically includes: after the slave end equipment receives data, the pull-up and pull-down of the first GPIO port of the slave end is modified to be opposite to the current level of the first GPIO port of the slave end, and then the pull-up and pull-down of the second GPIO port of the slave end is modified to be opposite to the current level of the second GPIO port of the slave end so as to feed back a data receiving response signal to the master end equipment.

4. The GPIO-based communication method of claim 1, wherein the step S6 further comprises:

and when the data transmission is finished, the master end equipment and the slave end equipment are both restored to the initialization state.

5. The GPIO-based communication method of claim 1, further comprising, before step S6:

and when the data receiving waiting time of the slave end equipment exceeds a preset first time threshold or the response signal waiting time of the master end equipment exceeds a preset second time threshold, stopping data transmission between the master end equipment and the slave end equipment and recovering to the initialization state.

6. The GPIO-based communication method of claim 1, wherein the pull-up and pull-down of the primary-side first GPIO port and the primary-side second GPIO port are always off when data transmission is performed.

7. The GPIO-based communication method according to claim 1, wherein the slave device uses received target unit data as data length information according to a preset upper layer transmission protocol, so that the slave device determines a time at which this data transmission is completed according to the data length information.

Technical Field

The invention relates to the technical field of electronics, in particular to a communication method based on GPIO.

Background

In order to save chip area, some chips do not integrate communication interfaces such as I2C and SPI on hardware, or the number of integrated chips is small, so that the communication interfaces are not enough in practical application, and therefore, application scenarios requiring the use of common GPIOs for inter-device communication generally exist in application.

The common GPIO is used for inter-device communication, which is usually implemented by software to simulate a mature communication protocol, such as I2C communication protocol, UART communication protocol, etc. The GPIO simulates the bottom layer logic of the protocols to achieve data synchronization by simulating a clock or a baud rate through time delay, so that the overhead of transmission time is increased, and once the time for processing each bit of data by the equipment is changed, the time is required to be spent again for joint debugging; also, it is necessary to shut down the interruption in the communication, because there is no interaction, and if the interruption occurs in the middle of reception from the terminal, an error message is lost or received, so that it is impossible to deal with a more urgent situation in the communication.

Disclosure of Invention

The invention aims to provide a communication method based on GPIO (general purpose input/output), which aims to solve the technical problem and can stably and efficiently realize communication between devices by utilizing GPIO.

In order to solve the above technical problem, an embodiment of the present invention provides a GPIO-based communication method, where the communication method is applied between a master device and a slave device, a master first GPIO port of the master device is connected to a slave first GPIO port of the slave device through a first GPIO line, and a master second GPIO port of the master device is connected to a slave second GPIO port of the slave device through a second GPIO line;

the communication method includes the steps of:

s1, adjusting the input and output modes and the pull-up and pull-down of the first GPIO port of the master end, the first GPIO port of the slave end, the second GPIO port of the master end and the second GPIO port of the slave end to an initialization state;

s2, the master end device carries out data transmission notification on the slave end device through the first GPIO line and transmits data to the slave end device through the second GPIO line;

s3, after the slave end equipment receives data, the first GPIO line feeds back data to the master end equipment to receive response signals, and the second GPIO port of the slave end is modified to be pulled up and down to be opposite to the current level;

s4, the master end device carries out data transmission notification on the slave end device through the second GPIO line and transmits data to the slave end device through the first GPIO line;

s5, after the slave end device receives data, the slave end device feeds back data to the master end device through the second GPIO line to receive response signals, and modifies the pull-up and pull-down of the first GPIO port of the slave end to be the opposite level of the current level of the first GPIO port;

and S6, repeating the steps S2 to S5 until the data transmission is finished.

Further, step S1 specifically includes:

setting the input and output modes of the first GPIO port of the master terminal, the first GPIO port of the slave terminal, the second GPIO port of the master terminal and the second GPIO port of the slave terminal as input;

and setting the pull-up and pull-down of the first GPIO port of the master end, the first GPIO port of the slave end, the second GPIO port of the master end and the second GPIO port of the slave end as pull-up.

Further, step S2 is specifically: the master end device configures the pull-up and pull-down of the first GPIO port and the second GPIO port of the master end to be closed, then modifies the first GPIO port of the master end to be in an output mode and outputs low level through the first GPIO port of the master end to perform data transmission starting notification on the slave end device, and then modifies the second GPIO port of the master end to be in an output mode and transmits data to the slave end device through the second GPIO line, and then modifies the first GPIO port of the master end to be in an input mode and enables the port level of the first GPIO port to be turned over to perform data preparation ready notification on the slave end device;

step S3 specifically includes: after the slave end equipment receives data, firstly modifying the pull-up and pull-down of a second GPIO port of the slave end to be opposite to the current level of the second GPIO port of the slave end, and then feeding back a data receiving response signal to the master end equipment by modifying the pull-up and pull-down of a first GPIO port of the slave end to be opposite to the current level of the first GPIO port of the slave end;

step S4 specifically includes: after the master end device modifies the first GPIO port of the master end into an output mode and transmits data to the slave end device through the first GPIO line, the second GPIO port of the master end is modified into an input mode to enable the port level of the second GPIO port to be turned over so as to inform the slave end device of data readiness;

step S5 specifically includes: after the slave end equipment receives data, the pull-up and pull-down of the first GPIO port of the slave end is modified to be opposite to the current level of the first GPIO port of the slave end, and then the pull-up and pull-down of the second GPIO port of the slave end is modified to be opposite to the current level of the second GPIO port of the slave end so as to feed back a data receiving response signal to the master end equipment.

Further, step S6 further includes:

and when the data transmission is finished, the master end equipment and the slave end equipment are both restored to the initialization state.

Further, before step S6, the method further includes:

and when the data receiving waiting time of the slave end equipment exceeds a preset first time threshold or the response signal waiting time of the master end equipment exceeds a preset second time threshold, stopping data transmission between the master end equipment and the slave end equipment and recovering to the initialization state.

Further, when data transmission is performed, the pull-up and pull-down of the first GPIO port of the master terminal and the second GPIO port of the master terminal are always in an off state.

Further, the slave device may use the received target unit data as data length information according to a preset upper layer transport protocol, so that the slave device determines the time when the data transmission is completed according to the data length information.

Compared with the prior art, the invention has the following beneficial effects:

the invention provides a communication method based on GPIO, which uses two GPIO lines as a signal line and a data line in turn to realize data transmission between a master end device and a slave end device, specifically, the master end device informs the slave end device to receive data and send data to the slave end device through GPIO output level or pull-up and pull-down of the slave end, and the slave end device responds signals to the master end device by switching the internal pull-up and pull-down of the GPIO. The invention does not need to simulate the clock or baud rate by time delay to achieve data synchronization, but uses a handshake response mode to perform synchronization so as to achieve the aim of stable communication, can effectively save communication time, and can be adaptive to the internal environment change of communication equipment, such as frequency change, interruption and the like.

Drawings

Fig. 1 is a flowchart illustrating a GPIO-based communication method according to an embodiment of the present invention;

fig. 2 is another flow chart of the GPIO-based communication method according to an embodiment of the present invention.

Detailed Description

The technical solutions in the embodiments of the present invention will be described clearly and completely with reference to the accompanying drawings in the embodiments of the present invention, and it is obvious that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.

Referring to fig. 1-2, an embodiment of the present invention provides a GPIO-based communication method, where the communication method is applied between a master device and a slave device, a first GPIO port of the master device is connected to a first GPIO port of the slave device through a first GPIO line, and a second GPIO port of the master device is connected to a second GPIO port of the slave device through a second GPIO line;

the communication method includes the steps of:

s1, setting the input and output modes of all GPIO ports of the master end device and the slave end device as input, and setting the pull-up and pull-down of all GPIO ports as pull-up; it should be noted that, during data transmission, initialization requires closing the GPIO port of the master device by pulling it up and down;

s2, the master device configuring the pull-up and pull-down of the first GPIO port and the second GPIO port of the master device to be turned off, then modifying the first GPIO port of the master device to be in an output mode and outputting a low level through the first GPIO port of the master device to notify the slave device that data transmission starts, and then modifying the second GPIO port of the master device to be in an output mode and transmitting data to the slave device through the second GPIO line, and then modifying the first GPIO port of the master device to be in an input mode to flip the port level thereof to notify the slave device that data is ready;

s3, after the slave device receives the data, first modifying the pull-up and pull-down of the second GPIO port of the slave to the opposite level of the current level thereof, and then modifying the pull-up and pull-down of the first GPIO port of the slave to the opposite level of the current level thereof to feed back a data reception response signal to the master device;

s4, after the master end device modifies the first GPIO port of the master end into an output mode and transmits data to the slave end device through the first GPIO line, the second GPIO port of the master end is modified into an input mode to enable the port level of the second GPIO port to be turned over so as to inform the slave end device of data readiness;

s5, after the slave device receives the data, first modifying the pull-up and pull-down of the first GPIO port of the slave to the opposite level of the current level thereof, and then modifying the pull-up and pull-down of the second GPIO port of the slave to the opposite level of the current level thereof to feed back a data reception response signal to the master device;

s6, repeating the steps S2 to S5 until the data transmission is finished; and when the data transmission is finished, the master end equipment and the slave end equipment are both restored to the initialization state.

In this embodiment of the present invention, the slave device uses the received first unit data as data length information, and the slave device determines the time when the data transmission is completed according to the data length information.

In the embodiment of the present invention, during the data transmission, i.e. before step S6, the method further includes:

and when the data receiving waiting time of the slave end equipment exceeds a preset first time threshold or the response signal waiting time of the master end equipment exceeds a preset second time threshold, stopping data transmission between the master end equipment and the slave end equipment and recovering to the initialization state.

Based on the above scheme, in order to better understand the GPIO-based communication method provided by the embodiments of the present invention, the following detailed description is made:

the invention is characterized in that the invention is a bottom layer design of communication, the invention transmits data bit by bit through a handshaking mode, after the system receives the data, the system can negotiate and stipulate a specific upper layer communication protocol, for example, the first byte is the equipment number, the second is the information of the transmission data length, etc.;

the invention needs to use two GPIO lines, and the GPIO ports of the two end devices have input and output functions and internal pull-up and pull-down switching functions, and the two GPIOs are called G1 and G2 hereinafter.

Roles of G1 and G2 are not fixed to the signal line and the data line, but are rotated.

The slave end responds to a signal for the master end by switching the internal pull-up and pull-down of the GPIO, the master end informs the slave end and sends data by the GPIO output level, the master end informs the slave end by utilizing the pull-up and pull-down of the slave end, once one device does the master end, the pull-up and pull-down of the device needs to be closed, and the pull-up and pull-down of the slave end are remained, so that once the master end releases the output of the GPIO, the GPIO level can follow the pull-up and pull-down of the slave end.

Specifically, the communication steps between the master device and the slave device are as follows:

1) initializing, setting two GPIOs of a master part and a slave part as input, pulling up, connecting external G1 with G1, and connecting G2 with G2;

2) starting communication, the master end closes the pull-up and pull-down of G1/2, and pulls down G1 to inform the slave end to start data transmission;

3) the master is ready for data to be output on G2, G1 is released, and G1 goes high (1);

4) when the slave terminal detects that G1 changes to high level, data (such as 1) with the level of G2 being one bit is collected;

5) the slave terminal modifies the pull-up/down of G2 to be the opposite level (0) of the current level of G2, then modifies G1 to be the pull-down (0), informs the master terminal that the receiving is finished, and waits for the opposite level signal of G2 to be the data acquisition signal of the next bit;

6) when the main terminal detects that G1 changes to low level, the main terminal modifies G1 to output the data of the next bit and then releases G2(1- > 0);

7) if the slave end detects that the level of G2 changes, data (such as 0) of G1 as the next bit is collected;

8) the slave terminal modifies the pull-up/down of G1 to be the opposite level (1) of the current level of G1, then modifies G2 to be the pull-up (1), informs the master terminal that the receiving is finished, and waits for the opposite level signal of G1 to be the data acquisition signal of the next bit;

9) when the main terminal detects that G2 changes to high level, the main terminal modifies G2 to output the data of the next bit and then releases G1(0- > 1);

10) and repeating the steps 4-9 until the transmission is finished.

It should be noted that the above method or flow embodiment is described as a series of acts or combinations for simplicity, but those skilled in the art should understand that the present invention is not limited by the described acts or sequences, as some steps may be performed in other sequences or simultaneously according to the present invention. Further, those skilled in the art will appreciate that the embodiments described in the specification are exemplary embodiments and that no single embodiment is necessarily required by the inventive embodiments.

It should be noted that the features of the embodiment of the present invention include:

1. in the implementation, an upper layer communication protocol needs to be specified, where the first unit of data received from the slave is simply specified as the data length of the current transmission, and the unit is: word;

2. g1 and G2 are connected to two devices needing communication on external hardware, and the external devices do not need to be pulled up or down;

3. the master device prepares data and then signals the slave to inform the slave that the slave is ready to receive the data;

4. the slave end receives signals and starts to receive data, and the master end responds to the signals when receiving one bit;

5. according to the handshake mode, when the slave end equipment receives a word, the length of the data transmission needs to be analyzed, and then the data of the master end equipment is continuously received;

6. and when the transmitted and received data reach the length of the data transmission, the transmission is finished, and the slave terminal analyzes and processes the data. The master/slave end restores the default state and waits for the next transmission initiation;

7. or when the signal/response is overtime (the time is determined according to the requirement), the master device and the slave device stop sending or receiving data, recover the default state and wait for the next transmission.

Compared with the prior art, the invention has the following beneficial effects:

by implementing the communication method of the invention, data synchronization is achieved without simulating a clock or a baud rate by time delay, but synchronization is performed by using a handshake response mode so as to achieve the purpose of stable communication, thereby effectively saving communication time and being capable of self-adapting to the internal environment change of communication equipment, such as frequency change, interruption and the like.

While the foregoing is directed to the preferred embodiment of the present invention, it will be understood by those skilled in the art that various changes and modifications may be made without departing from the spirit and scope of the invention.

10页详细技术资料下载
上一篇:一种医用注射器针头装配设备
下一篇:一种Type-C接口保护电路、方法、装置、存储介质和电子设备

网友询问留言

已有0条留言

还没有人留言评论。精彩留言会获得点赞!

精彩留言,会给你点赞!