Realization of CAN bus communication based on ARM9 under Linux environment

Realization of CAN bus communication based on ARM9 under Linux environment

1 Introduction
The CAN (Controller Area Network) bus was first proposed by the German BOSCH company to realize the communication of the microcontroller in the automotive environment, exchange information between the ECUs of the on-board electronic control devices, and form an automotive electronic control network. Because of its low cost, good real-time performance, high fault tolerance, and flexible design, it has been widely used in various industrial fields and is recognized as one of the most promising off-the-shelf buses. At the same time, with the maturity and improvement of ARM (Advanced RISC Machines) chips and embedded Linux operating systems, the development of CAN communication is more convenient and more widely used. This article will introduce a CAN bus communication method based on EP9315 in the Linux environment from both hardware and software.
2. Hardware introduction and interface realization This design chooses EP9315 processor launched by Cirrus Logic and SJA1000 independent CAN controller launched by PHILIPS. The following is a brief introduction to the two chips, and then a detailed description of the CAN interface hardware circuit design.
2.1 Introduction to EP9315 and SJA1000 chips
EP9315 is the flagship product of the EP93XX series of microprocessors. Embedded advanced ARM920T microprocessor core running at 200MHz (recommended to run at 184MHz under industrial conditions), and memory management unit MMU supporting Linux, Windows CE and other operating systems, 16KB instruction cache and 16KB data cache can be existing The program and data provide zero latency, or in a latched manner to ensure no delay access to key instructions and data. EP9315 integrates MaverickCrunch math coprocessor and MaverickKey hardware programmable ID. The former significantly improves the floating point, shaping operation and signal processing capabilities of ARM920T. At the same time, it also has a wealth of integrated peripheral interfaces, including 1/10 / 100Mbps Ethernet Network MAC, 3-channel USB2.0 full-speed main port, SPI, IS and AC'97 serial interfaces, PCMCIA interface, Raster / LCD interface, image accelerator, touch screen interface with 12-bit A / D converter, keyboard interface, UART Interface, rich GPIO, support 4 groups of 32-bit SDRAM seamless connection, etc.
SJA1000 is a high-performance CAN controller, supports BasicCAN and PeliCANl two working modes, provides INTEL and Motorola two addressing modes, time-division multiplexing of address lines and data lines, SJA1000 is based on register addressing, can be read and written Register to operate it.
2.2 Hardware interface circuit design
EP9315 has 16 enhanced GPIOs. You can configure the PADR and PADDR registers to enable EGPIO2 to control the ALE address latch signal of SJ1000. EGPIO3 receives the SJA1000 interrupt signal and can configure the interrupt type. Because the data line of SJA1000 is multiplexed with the address line in time-sharing, when sending the address, the RD, WR, and CS signals must be invalid. When sending or reading data, the RD, WR, and CS signals are only valid, so the CPLD (EPM7032 chip ) Or some logic gates to implement this logic, and chip select two banks to be used for address operation (nCS5_PHYBASE 0x50000000) and data operation (nCS3_PHYBASE 0x30000000). When reading SJA1000, first select SJA1000 through CS from EP9315, validate ALE through EGPIO2, latch the address, and select the bank 5 of FLASH from CS5 through CPLD, store the address into the mapped BANK5 area, and then make ALE 0 Cancel the address latch, and finally set the RD signal of EP9315 low, make the read signal of SJA1000 valid after 74LV32, and complete the read operation; during the write operation, similarly gate SJA1000, enable ALE, send the address and lock it in BANK5 Then cancel ALE, set the WR signal of EP9315 low, enable the WR signal of SJA1000 through 74LV32, and then send the mapped BANK3, write the data to SJA1000, and complete the write operation.
3. Software analysis and implementation This article uses Linux system, the kernel version is 2.4.21-rmk1, build ARM-LINUX cross compilation environment and use dynamic module loading method to drive development. The advantage of modularity is that it can keep the size of the kernel image to a minimum and has the greatest flexibility, which is convenient for testing new kernel code without recompiling and booting the kernel. The development of Shell user applications mainly uses GDB remote debugging technology. The debugging environment is composed of the host GDB and the target machine debugging stub, and the two are connected through a serial port or TCP. The overall flow chart of the driver and application is shown in Figure 2.


Figure 2 The overall flow of drivers and applications


3.1 Implementation of CAN driver under Linux
The CAN device is a character device, which is a device that performs I / O operations one by one in byte units. When a read and write request is issued to it, the actual hardware I / O occurs immediately. The caching of character devices is optional, and random access is not supported. Applications can open, read, write, and close character devices through standard system calls like accessing byte streams (similar files).
The driver is mainly composed of the following 4 key links:
â‘´ Module initialization module_init (Mysja1000_init)
The initialization function of the module is responsible for registering any facilities provided by the module. The use of module_init is mandatory, this macro will add a special section in the object code of the module to explain the location of the kernel initialization function. Without this definition, the initialization function will never be called. When the module is normally initialized, the registration of CAN devices and device interrupts is completed in the initialization function, the configuration of the relevant registers in the SJA1000 chip, the initial setting of the EGPIO port in EP9315, and the function void * __ioremap (unsigned long phys_addr , unsigned long size, unsigned long flags) Map the IO address space to the virtual address space of the kernel, so that you can read and write IO memory resources like reading and writing RAM.

⑵Subroutines serving I / O requests This part of the program is also called the upper half of the driver. This part of the program is called because of the result of the system call. When this part of the program is executed, the system still thinks it belongs to the same process as the calling process, but it has changed from user mode to core mode. Because this part of the program is the interface between the driver and the application, it must be implemented through a key data structure file_operaTIons under Linux. The members of the file_operaTIons structure are almost all function pointers, which are essentially a function jump table. For example, the can_open entry point is used to open the CAN device for I / O operations; the can_read entry point is used to receive CAN data frames; the can_write entry point is used to send CAN data frames; the can_ioctl entry point provides a way to perform CAN device-specific operations The method of reading and writing the SJA1000 register through ioctl enables users to easily configure the SJA1000 status according to their needs.
(3) This part of the interrupt service subroutine is also called the lower half of the driver. The Linux system is responsible for receiving hardware interrupts, and then the system calls the interrupt service subroutine, instead of directly calling this part of the program from the ARM interrupt vector table. The device interrupt has been registered with the request_irq () function during module initialization, so when the IRQ (interrupt request) is generated, the ISR (interrupt service routine) runs. In the interrupt service program, we must first read the value of the interrupt register IR of SJA1000, and identify the source of the interrupt. For example, when the receive interrupt bit RI is 1, it means that it is a receive interrupt, and then call the receive function to receive data. When the CPU reads this read-only memory, all bits except the RI bit are reset.
â‘·Buffer operation
SJA1000 is equipped with TXB (13 bytes), TXB (13 bytes) and RXFIFO (64 bytes). TXB is the interface between CPU and BSP (bit stream processor), and stores the complete message sent to the CAN network. The receive buffer is the interface between the receive filter and the CPU and is used to receive and store messages on the CAN bus. RXB is a window of the RXFIFO and can be accessed by the CPU. In order to improve the efficiency of sending and receiving, a software buffer is opened in the driver, the size can be set according to the need, and a large amount of data will be processed after passing through the software buffer. When operating the software buffer, the producer / consumer model is used to set pointers for data storage and readout, respectively, and the memcopy function is used to complete the operation on the frame data. For example, when reading data, first read the RXB data with the starting address of 16 in the SJA1000 into an array in the operation mode. After some necessary judgment, the data in this array is transferred to the designated software buffer by the memcopy function. One frame of data is transferred between the hardware buffer and the software buffer at one time. The frame is divided into a standard frame and an extended frame.
3.2 Implementation of Shell application Shell application is mainly to provide users with a series of convenient and intuitive interfaces, so that users do not have to understand the details of hardware work, do not have to study the specific implementation of the driver, but only according to their own needs, through some simple and clear commands To complete the required task. This article will implement the following command functions in the Shell application:
Init initializes the register, rsja reads the register value, wsja writes the register, gfrm receives the data frame, sfrm sends the data frame, conf configures the filter, help help, exit exit.
In the main function main, open the CAN device through the int open (const char * filename, int mode) function, get a file handle fd, pass this handle to the shell function, so that you can operate the CAN device in each command function For example, you can use ioctl (fd, SJA_WRITE, ®_data) in the function int write_sja1000 (int fd, unsigned char addr, unsigned char value) to write to the SJA1000 register.
In the shell application, you can set a character array char cmd [5], through fgets (cmd, 5, stdin) to receive the command typed by the user, and then through the strcmp function to identify the user's command type and perform related operations. In order to avoid garbage in the input buffer and cause errors in command recognition, the input buffer needs to be cleared in time, and when the fflush function in gcc is invalid, you can pass while ((c = getchar ())! = 'N' && c! = EOF) {} to clear the input buffer.
After writing all the drivers and application programs, use the Makfile file to formulate the compilation rules, and then compile according to the rules. After successful debugging, you can download the binary code to the board through the HyperTerminal to run, and end all work.
4. Conclusion This article provides an interface scheme between EP9315 and CAN controller SJA1000, analyzes the principle and development process of the driver and application program under Linux operating system in detail, realizes CAN device communication, and proves it in actual application and testing. The correctness and reliability of the design. Because of the many advantages of embedded Linux and the real-time, easy-to-use, reliability and other advantages of CAN, they will have a wider application prospect in various fields of industrial control and life.
The author of this article innovates: using EP9315 and SJA1000 to achieve CAN bus communication, complete all the work connected by hardware and driven by the bottom layer so that the upper layer application software, using software to control CAN communication conveniently and stably.

Air Coil

Air Core Inductor Coil,Ferrite Core Coil,Winding Air Core Inductors,Copper Induction Coil

IHUA INDUSTRIES CO.,LTD. , https://www.ihua-magnetics.com