introduction
In the railway system, in order to ensure the safe operation of trains, it is necessary to conduct real-time detection of the rails and surrounding conditions. The current method is to install multiple detection devices along the railway to detect natural disasters such as floods, strong winds, mudslides, and parameters such as rail temperature. These devices generally use RS232, RS485 or CAN as the communication method, and are connected to various monitoring devices in the monitoring center through dedicated lines. This method greatly wastes line resources and is not easy to manage the equipment in a unified manner. Therefore, a device installed along the railway is needed, which collects the information sent by nearby detection devices and sends it directly to the monitoring center through a dedicated line. In order to communicate with multiple detection devices, multiple RS232, RS485 and CAN interfaces must be available at the same time. Based on this application need, this paper proposes a method for expanding multiple CAN bus interfaces.
1 System Structure
1.1 Chip Introduction System
The AT91RM9200 (hereinafter referred to as "9200") from Atmel is used as the MCU. This processor is based on the ARM920T core. When the main frequency is 180 MHz, the performance can reach 200 MIPS; the maximum main frequency is 209 MHz. This processor also has rich peripheral resources and is very suitable for applications in the field of industrial control [1]. The operating system used is ARMLinux, and the kernel version is 2.4.19.
At present, the mainstream CAN protocol controller generally uses I/O bus (SJA1000, etc.) or SPI interface (MCP2515, etc.) to communicate with MCU. Since this design uses PC/104 bus expansion card to expand multiple RS232 and RS485 interfaces, there are no extra I/O chip select lines available, so the 9200 SPI interface and MCP2515 are finally selected to expand the multi-channel CAN bus interface.
MCP2515 is an independent CAN controller with SPI interface launched by Microchip. It fully supports CAN V2.0B technical specification, with a communication rate of up to 1 Mbps. It contains 3 transmit buffers, 2 receive buffers, 6 29-bit acceptance filter registers and 2 29-bit acceptance mask registers [2]. Its SPI interface clock frequency can reach up to 10 MHz, which can meet the needs of expanding multiple CAN bus interfaces with one SPI host interface.
1.2 System Hardware Interface
Figure 1 is the interface principle block diagram of 9200 and MCP2515. Five MCP2515s are connected through the SPI interface of 9200. Since the number of SPI slave chip select lines of 9200 is limited, the chip select decoding method is adopted, and NPCS0 can be used as a common external interrupt line (NPCS0 and IRQ5 multiplexed pins). Since the external interrupt line resources of 9200 are limited, the interrupt line sharing method is adopted, that is, two MCP2515s share the same interrupt line, and the last MCP2515 occupies an interrupt line to meet the needs of data processing at different communication rates.
Figure 1 AT91RM9200 and MCP2515 interface block diagram
Figure 2 is the peripheral CAN bus interface block diagram of MCP2515, and the interface part of MCP2515 and 9200 is omitted in the figure. Since the equipment needs to be installed along the railway, it must have the ability to protect against lightning strikes. Therefore, a high-speed optocoupler is used to completely isolate the MCP2515 from the CAN bus transceiver (TJA1050), and the power supply of the circuits at both ends of the optocoupler must also be isolated by a power isolation module, so that the isolation effect can be truly achieved. Connecting two 30 pF capacitors between the CANH and CANL pins of TJA1050 and the ground can filter high-frequency interference on the CAN bus; two diodes can protect the bus voltage when transient interference occurs. When the optocoupler is working normally, the input current is about 10 mA, and the forward voltage drop of the internal light-emitting diode is about 1.7 V, so special attention should be paid to the selection of the resistance value of the series resistor at the input end.
Figure 2 MCP2515 CAN bus interface circuit
2 SPI master working mode
The 9200 communicates with five MCP2515s via the SPI interface. The 9200 SPI controller works in the master mode and the MCP2515 works in the slave mode. The MCP2515 supports multiple instructions (such as reset instructions, read instructions, write instructions, etc.) so that the 9200 can read/write the internal registers of the MCP2515 via the SPI interface. The working mode flow of the 9200 SPI controller as the master is shown in Figure 3 [1].
Figure 3 AT91RM9200 SPI controller master mode flow
It should be noted that after SPI is enabled, the corresponding chip select will be enabled according to the chip select configuration (fixed peripheral or variable peripheral) only when there is data in SPI_TDR (transmit data register); when there is no data in SPI_TDR, the chip select is automatically disabled. Therefore, when 9200 sends multiple bytes to MCP2515 continuously, it is necessary to ensure that the next byte is written to SPI_TDR before the previous byte is transmitted to avoid the chip select being automatically disabled; at the same time, after each byte is transmitted, the SPI_RDR (receive data register) must be read. [page]
The following uses the read instruction of MCP2515 as an example to explain the process of the driver shown in Figure 4 completing a read instruction operation (reading only one byte of data), and assumes that the 9200 SPI uses the fixed peripheral chip select configuration mode. The software implementation process of other instructions is similar to that of the read instruction.
Figure 4 SPI read instruction operation software flow
3 Driver Design
The driver is the intermediate software layer between the application and the hardware. It completely hides the details of the device's operation. The Linux operating system divides devices into three types according to the different ways of information transmission in the device: character devices, block devices, and network devices [3]. The communication between 9200 and MCP2515 is carried out in bytes through the SPI interface, so MCP2515 is a character device. Since the five MCP2515s share one SPI interface of 9200, one driver is used to manage all MCP2515s, which is conducive to unified management of all devices.
3.1 Main data structures defined in the driver
CAN bus communication is based on message frames. In the driver program, both sending and receiving data are based on message frame operations [4]. Therefore, it is necessary to design a suitable data structure to meet the needs of data operations.
3.1.1 Receiving and sending CAN message frame structure
Among them, node_num is the node number of MCP2515 (0~4), id is the identifier of the CAN message frame, dlc is the data length (0~8), data is the data buffer of the CAN message frame, ext_flag is used to identify whether the CAN message frame is an extended frame, and rtr_flag is used to identify whether the CAN message frame is a remote frame.
3.1.2 Device Configuration Structure
(1) Baud rate and message filter configuration structure
Among them, node_num is the node number of MCP2515 (0~4), baudrate is the CAN bus communication rate, filter is the message filter configuration structure, br_flag is used to identify whether the baud rate configuration is valid, and fi_flag is used to identify whether the message filter configuration is valid. The data structure types of baudrate and filter are defined as follows:
(2) Working mode configuration structure
Among them, node_num has the same meaning as above, and oper_mode indicates the working mode of the node. MCP2515 has 5 working modes, namely configuration mode, sleep mode, listening mode only, loopback mode and normal mode. Generally, the device works in normal mode.
3.1.3 Ring data receiving buffer structure
Among them, can_recv_buf is the circular data buffer for receiving CAN message frames, recv_pos and read_pos represent the positions where data is stored in and read out of the buffer respectively; wq defines a waiting queue for implementing blocking read operations.
3.2 Driver Interface
The driver interface is mainly divided into three parts: the initialization and exit function interface, which completes operations such as device installation and uninstallation; the file system interface, which is completed by the file_operations data structure; and the interface with the device, which completes operations such as reading/writing to the device.
3.2.1 Initialization and exit functions
When installing the driver, the operating system will call the initialization function to perform operations such as device registration, device initialization, and installation of interrupt handling routines. Reference [3] discusses the device registration method in detail, while this article mainly discusses the configuration method during device initialization. In this driver, device initialization is divided into two steps: one is to initialize the 9200 SPI controller, and the other is to initialize the five MCP2515s.
The exit function will be called when the device driver is uninstalled. The exit function mainly completes the device deregistration and interrupt release.
Reference [3] discusses in detail the methods of installing interrupt handling routines, deregistering devices, and releasing interrupts, which will not be described in detail here.
3.2.2 Interrupt reception service routine
After receiving the CAN message frame, MCP2515 generates an interrupt and sets the INT pin low. 9200 responds to external interrupts and calls the interrupt handling routine corresponding to the external interrupt. There are 3 interrupt handling routines: at91_mcp2515_irq_handler_0 responds to the interrupt of IRQ0, at91_mcp2515_irq_handler_1_2 responds to the interrupt of IRQ1, and at91_mcp2515_irq_handler_3_4 responds to the interrupt of IRQ2. Among them, IRQ0 is only connected to one MCP2515, while IRQ1 and IRQ2 are shared by two MCP2515s respectively. The interrupt handling process of IRQ0 and IRQ1 is shown in Figure 5 and Figure 6 respectively. The interrupt handling process of IRQ2 is the same as that of IRQ1. [page]
Figure 5 IRQ0 interrupt processing flow
Figure 6 IRQ1 interrupt processing flow
It should be noted that there is no clear interrupt operation in the processing flow of Figure 5. This is because the RX read buffer instruction is used to read the data in the MCP2515 RX buffer. After the instruction operation is completed, the MCP2515 will automatically clear the corresponding receive interrupt flag.
3.2.3 File system interface definition
The members of the file system interface struct file_operations are all function pointers, which indicate the entry point locations provided by the device driver. The file_operations defined by this driver are:
3.2.4 ioctl functions
The ioctl function is used to configure the device. We have implemented two commands in the ioctl function: IOCTRL_CONFIG_CAN_DEV is used to configure the CAN bus baud rate and message filtering of the node, and IOCTRL_SET_OPER_MODE is used to configure the working mode of the node. The configuration parameters corresponding to these two configuration commands are pointers to the corresponding data structures of the application layer. The two configuration structures have been introduced in Section 3.1.2.
When using the IOCTRL_CONFIG_CAN_DEV command to configure the baud rate and message filtering, after the configuration is completed, if the node is in the INACTIVE state, you need to enable the node's internal receive interrupt, enable the node's corresponding external interrupt, and set the node state to ACTIVE. Under normal circumstances, after executing the IOCTRL_CONFIG_CAN_DEV command on the node to be configured through the ioctl function, you must also execute the IOCTRL_SET_OPER_MODE command on the configured node to put the node in normal working mode.
3.2.5 Competition issues
This system is a single CPU system, using Linux 2.4.19 kernel, and is non-preemptive; at the same time, the driver of this design also allows only one process to open and operate the device. In this case, the competition problem involved in the driver is mainly the resource competition between the interrupt handler kernel code and the kernel code of other device operations. In all the device operations mentioned above, communication with MCP2515 must be carried out through the SPI interface of 9200. The communication between 9200 and MCP2515 starts with a command byte, and in the process of a command operation (generally multiple bytes are transmitted continuously), the chip select and clock cannot be disabled, otherwise the operation will fail. Therefore, a complete command operation of MCP2515 is a critical area. In the process of performing a command operation on MCP2515, all interrupts must be disabled to ensure the normal execution of the command operation. In the driver, local_irq_save and local_irq_restore functions are used to disable and restore interrupts. Between these two function calls is the code for executing a command operation on MCP2515.
Conclusion
This paper proposes a multi-channel CAN bus interface and driver design for unique application requirements. After testing, it can run stably and normally. Regarding the compilation and operation methods of the driver, reference [3] has a good description. The writing of the upper-level test program is also relatively simple, but attention should be paid to the definition of the data structure and the consistency of the underlying driver. This paper focuses on introducing the basic design methods and realizing basic functions. MCP2515 itself provides many functions. On the basis of realizing basic functions, functions can also be expanded according to your own application needs.
references
[1] Atmel corporation. AT91RM9200 data sheet, 2006.
[2] Microchip Technology Inc. MCP2515 data sheet, 2005.
[3] Wei Yongming, Luo Gang, Jiang Jun. LINUX Device Driver[M]. 2nd edition. Beijing: China Electric Power Press, 2004.
[4] Du Shangfeng, Cao Xiaozhong, Xu Jin. CAN bus measurement and control technology and its application [M]. Beijing: Electronic Industry Press, 2007.
Previous article:Leveraging multi-voltage architecture to achieve high performance and ultra-low power standby mode on 32-bit MCUs
Next article:Optical fingerprint recognition system module circuit design based on STM32
- Popular Resources
- Popular amplifiers
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- [RVB2601 development board trial experience] GPIO output test
- Experience in debugging Ethernet half-duplex of GD32F450
- Don't know UWB? Then take a look at these knowledge posts shared by netizens to quickly understand ultra-wideband UWB
- There should be a simpler way to convert a sine wave into a triangle wave. If you have ever used a triangle wave, please share your experience...
- TI TMS320C6678 Evaluation Module
- The output voltage of the Zener diode is too low
- Understand data center solutions in seconds
- Update the firmware of PYBCNV2
- I bought a small thing, guess what it is
- BlueNRG ADC2 reads back incorrect voltage value