Abstract: Based on the characteristics and basic principles of infrared communication protocol, infrared wireless communication technology is analyzed, and the basic design points of infrared data communication in embedded systems are discussed in conjunction with practical routines.
Infrared and Bluetooth protocols are two popular short-range wireless communication protocols. But at present, the major manufacturers of Bluetooth protocol have not yet had a unified standard specification, coupled with the disadvantage of relatively expensive hardware, so infrared communication on the market is still widely used in small mobile devices such as mobile phones and notebook computers, and is actually used in embedded systems. Has a high practical significance.
1 Infrared protocol background
Infrared is an electromagnetic wave with a wavelength between 750nm and 1mm. Its frequency is higher than that of microwave and lower than that of visible light. It is a light that is invisible to human eyes. At present, radio waves and microwaves have been widely used in long-distance wireless communication, but due to the short wavelength of infrared rays and poor diffraction ability to obstacles, they are more suitable for direct line data transmission where short-distance wireless communication is required. . In order to enable various devices to communicate through an infrared interface, the Infrared Data Protocol (Infrared Data AssociaTIon, referred to as IRDA) has released a unified software and hardware specification for infrared, which is the infrared data communication standard.
2 Basic structure of infrared protocol
Infrared data communication standards include basic protocols and protocols in specific application areas. Similar to the TCP-IP protocol, it is a layered structure whose structure forms a stack, as shown in Figure 1.
There are three basic protocols: ①Physical layer protocol (IrPHY), which sets out the goals and requirements of infrared communication hardware design, including infrared optical characteristics, data encoding, and including formats of frames at various baud rates. In order to achieve compatibility, the hardware platform and hardware interface design must conform to the specifications established by the infrared protocol. ② The connection establishment protocol (IrLAP) layer formulates the process specification of the underlying connection establishment, and describes the process and requirements for establishing a basic reliable connection. ③ The connection management protocol (IrLMP) layer has developed a specification to reuse multiple services and applications on the basis of IrLAP connections per unit. The protocols above the IrLMP protocol all belong to the specifications and protocols of specific application fields. ④ The streaming protocol (TIngTP) performs flow control when transmitting data. Establish a mechanism for splitting, reorganizing, and retransmitting data. ⑤ Object Exchange Protocol (IrOBEX) has established the data format for the transmission of files and other data objects. ⑥Simulation of serial port layer protocol (IrCOMM) allows existing applications that use serial communication to communicate using infrared like serial ports. ⑦ Local Area Network Access Protocol (IrLAN) allows the wake-up of mobile devices such as laptop computers via infrared local area networks, and the actual remote remote control and other functions.
The entire infrared protocol stack is relatively large and complex. In an embedded system, due to the limitations of microprocessor speed and memory capacity, it is impossible and unnecessary to implement the entire infrared protocol stack. A typical example is the splitting and reassembly of data in the TInyTP protocol. It uses a credit card (creditcard) mechanism, which greatly increases the complexity of the code design, and the actual infrared communication generally does not have a large amount of data transmission, especially in embedded systems can be considered to put the data Enter a single data packet for transmission, and use the timeout and retransmission mechanism to ensure the reliability of the transmission. Therefore, the protocol stack can be simplified, and according to actual needs, the protocols and functions required by them can be selectively implemented.
3 Basic transmission principle of infrared protocol data
Due to hardware interface limitations, the rate of infrared communication in embedded systems is basically between 9600 bps and 115.2 kbps. Here is the infrared data encoding and wireless transmission through the asynchronous communication transceiver (UART) on the hardware circuit board. At 115.2kbps, infrared uses RZI coding and modulation scheme, and the pulse period is 3/16 bit period. Data check uses CRC16. The basic idea is to pack and check the data to be sent according to the CRC16 algorithm (CRC algorithm can refer to the relevant information), unpack the CRC and compare it with the constant OXF0B8 when receiving, if it matches, the data check is correct. The basic unit of infrared data transmission is frame. Frame is a combination of some specific domains, in which the underlying byte packet format of infrared protocol is shown in Figure 2.
The meaning of each field is as follows: STA is the start mark, that is, the address field of 0x7E and ADDR is 8 bits; DATA is the data field; FSC is the CRC check code of 16; STO marks the end of the frame and must be received when receiving two consecutive frames When there are at least three more ones, it indicates that the frame has an error, and the device will discard the frame. In the actual transmission of infrared data, in order to consider delay control, generally add multiple STA fields in the data frame header, usually using 11 consecutive 0x7E to achieve the purpose of delay. During reception, when multiple STA domains are received, they are treated as one, and the extra STA domains are ignored. The state machine process of infrared data transmission is shown in Figure 3.
Here are some explanations for Figure 2: (1) Address Discovery process is first performed during data transmission. In this process, a broadcast frame is sent, waiting for the response of the other device, and the address of the other device can be obtained after receiving the response frame. (2) After obtaining the address of the other party, proceed with the Connct process, during which the transmission parameters such as baud rate, data packet size, rotation time slice, etc. will be negotiated with the other party's device, and then the connection will be established. (3) After establishing the connection, enter the InformaTIon Transfer process to verify and transfer the data. The time slice data frame receiving and sending control is performed according to a certain algorithm. (4) After the data transmission is completed, enter the Disconnect process to disconnect. (5) During the Address Discovery process, it may be found that the address of the other party's device conflicts with the address of the local device. At this time, enter the Address Conflict Resolution process, and then return after resolving the device conflict.
Figure 3 is a standard infrared data transmission state machine process, but in some embedded design solutions, for the purpose of power saving, you can not enter the Address Discovery process, which is to simplify the Address Discovery process and shift to the Sniff process. After probing for a certain period of time, if it does not receive the response frame of the other device, it will automatically enter the sleep state, and if it receives the response frame of the other device, it will enter the normal connection process. At the same time, there is a window size parameter in the process of negotiating the transmission parameters with the other party during the connection process. It specifies the number of frames that the receiver can buffer and then confirms the reception. The value is 1-7. When the storage space of the embedded system is limited, the default value 1 can be used for simple confirmation of the data, that is, immediately after receiving a data frame. This not only saves resources but also makes the code size smaller and runs faster.
4 Mid-infrared protocol implementation design of embedded system
The author uses Sitronix's ST2204 circuit board as the hardware platform, and the processing chip core is 65C02. The ST2204 circuit board uses an integrated 8-bit processor, the addressability reaches 44M bytes, and provides a low-voltage detection function. Because the 2204 integrates these functions, it is very suitable for the implementation of embedded design of handheld mobile devices that save power and support long battery life. Assembly language is used in firmware design and software design. The assembly on 65C02 adopts the memory mapping method, and widely uses zero-page addressing, so it is very convenient and efficient to use. The entire design implementation can be divided into hardware design and software design. The hardware design includes circuit design and firmware program (Firmware) writing; the software design includes CRC data coding verification, data transmission and receiving, and the status of the primary station and the secondary station (Slave).
In terms of hardware design, according to the needs of the equipment and the performance of the hardware board chip, you can design a corresponding circuit to experiment on the simulation board. The firmware program and writing can be divided into blocks, such as the initialization (Initilize) module, interrupt processing (Interrupt) module, clock (Timer) event processing module, etc. The initialization module can be initialized according to the various register values ​​provided by the hardware board's specification. The interrupt processing module can be written according to the entry address provided by the interrupt vector table. The baud rate required by the equipment is closely related. Clock processing can be based on the different clock accuracy of the basic clock device provided by the hardware board to meet different needs. In the actual design of infrared transmission, the timer is mainly used in three directions: the first is the processing of the secondary station overtime and not responding after the broadcast frame is sent by the master station during the sniff probe process; the second is the timeout retransmission control; the last is the data transmission process Control of rotating time slices. The accuracy required in the third aspect is relatively high, and the standard established by the infrared protocol is between 25 ms and 85 ms. Therefore, it is necessary to put timeout processing in interrupt processing. Use semaphores and program counters for time control when programming. The basic idea is to use a program counter to accumulate time. When the respective event time arrives, three semaphores are set to mark the event processing. When the event processing is completed, the respective semaphores are reset and transferred to re-timing.
In terms of software design, it is necessary to frame wrap (Frame Wrapper) the transmitted data, add CRC16 check, and use assembly to implement the CRC algorithm is slightly more complicated than C. A main technique is to put the data address to be verified and the index address of the CRC data table into a zero-page memory address, and use general-purpose registers to indirectly address it. In this way, the pointer effect in the C language is realized, and the CRC table can be queried more conveniently. In the application of data transmission and reception, it is divided into two angles: primary station and slave station. The master station is responsible for initiating, establishing connections, and scheduling time slice rotation. The secondary station is mainly responsible for answering and responding to commands. Under certain conditions, the angles of the main and auxiliary stations can be interchanged, and both the main and auxiliary stations can send and receive data.
The most important interrupt function for sending and receiving data is also the core of the bottom layer. On the receiving side, first perform hardware initialization, set the UART receiving initialization state and set the interrupt permission flag (for specific settings, please refer to the description of the selected circuit board), etc. When the infrared data arrives, it will trigger a UART interrupt. After the system processes the current event, it will call the receive interrupt to process the received data according to the entry address provided by the interrupt vector table. During the reception process, the UART will search for matching start bits and end flags. After receiving, return to the system call program. In practical applications, after receiving the data, the frame type can be judged according to the loading and receiving frame control field, and combined with the corresponding state machine where the receiving station is located for flow processing. The following is the source code of the IR interrupt program:
/ *******************************************
* UART Receiver Interrupt Service Routine
******************************************** /
ISR_URX:
pha
phx; push stack, save general register value
cld; clear the decimal flag
ldx # 00001100B; allow to receive, and set to receive the next byte
stx ldx mBagLen cpx #IrDAb_BAG_LEN; the length of a packet bcs ferr_over; overflow no lda sta mReadBuff, x; write data lda sta mIrdaByteFlag; save the status flag inc mBagLen; counter ++ bra? exit ? err_over; smb0 ? exit; plx; pop the stack and restore the general register value pla rti This interrupt handler is triggered when the hardware receives a byte. It first pushes the general register value on the stack and saves it, then controls the status register, checks some status flags, and then receives the data; saves the data in a buffer, and detects and controls the status such as overflow. Finally restore the value of the general register and return to the interrupt adjustment function. Infrared sender programs can be written according to similar principles. One thing to note when writing a data transceiver interrupt program is that the amount of program code is closely related to the processor's main frequency and the selected infrared baud rate. If you do not pay attention, it is easy to cause the phenomenon of "lost interruption", which should be avoided. Another point to note: UART works in half-duplex mode, and it is not possible to send and receive data at the same time in some applications that require high real-time system and time accuracy. However, due to its short transmission and reception time (up to 500ms), it can be simulated as simultaneous transmission and reception in some common applications. After the program is written, it is compiled / connected and positioned, and it can be simulated and debugged by loading it in the host development system in the form of hexadecimal with a debugger. However, the analog mode cannot fully simulate all the characteristics of the hardware. It is mainly used to debug software logic and state machine processes. For real-time hardware features such as debugging UART data transmission and reception, it needs to be verified on the target system. Silicone Insulated Wire,Silicone Cable,Silicone Rubber Cable,Silicone Rubber Wire Dong guan Sum Wai Electronic Co,. Ltd. , http://www.sw-cables.com