FPGA-Based CAN Bus Driver Design and Implementation
Aerospace products are notoriously complex, featuring numerous functional units that make domestic production cycles long and difficult. FPGAs offer a viable path forward because they allow for logic function modification through programming, providing flexible configuration and shorter development cycles compared to fixed-logic chips. This article examines the design and implementation of an FPGA-based SJA1000 CAN bus driver, specifically applied to a comprehensive electronic warfare management system.
🏗 System Architecture and Communication Flow #
In high-speed embedded system interconnects, traditional buses like RS232, RS485, and RS422 often hit performance ceilings due to transmission distance, interference, and load capacity. The CAN bus addresses these issues as a multi-master system where a controller coordinates data transmission between nodes. Because CAN messages are broadcast-based—meaning all nodes see the same data—it is highly effective for both control signals and data information.
The CAN bus supports long transmission distances (up to 10 km theoretically) and can handle up to 110 nodes on a single bus without interference. For our specific implementation, we utilize a CPU+FPGA+Driver architecture. The FPGA acts as the core of the CAN protocol IP, communicating with the CPU via a Local Bus. We integrated the Guowei SM65HVD230D transceiver to handle external CAN communication, supporting up to 5 independent CAN bus interfaces.
⚙️ CAN Frame Structure and Protocol #
The system handles two primary frame types: standard frames with 11-bit identifiers and extended frames with 29-bit identifiers. The difference lies strictly in the ID length.
Standard frames consist of 11 bytes, split into an information section (first 3 bytes) and a data section (last 8 bytes). The first byte contains the DLC (Data Length Code) in the first 4 bits, the RTR (Remote Transmission Request) in the 6th bit, and the FF (Frame Format) in the 7th bit.
Extended frames consist of 13 bytes. The first byte matches the standard frame, while the remaining information section contains the Message Identifier. The protocol is built on the OSI 7-layer model, specifically defining the data link and physical layers to ensure error-free transmission between nodes.
🛠 Driver Implementation Details #
The FPGA-based SJA1000 driver handles the core logic of transmitting and receiving CAN frames. However, the driver must also perform packet disassembly for data coming from the CPU and packet assembly for data arriving from the CAN bus. The design consists of four primary components: initialization, sending, receiving, and assembly/disassembly.
CAN Initialization #
Because the electronic warfare management system involves numerous CAN nodes, we use extended frames. Initialization involves configuring the data structures defined by these extended frames and setting up the necessary registers.
We set the frame type to data frame, the data length to 8 bytes, and initialize both the data and Message Identifier to 0. Before transmission, these values are updated based on user requirements. Register configuration includes baud rate, interrupt enablement, clock frequency division, and output controllers. For baud rate configuration, we simultaneously configure Bus Timing Register 0 and Bus Timing Register 1 to achieve the required sampling rates. The initialization process also includes creating semaphores, allocating intermediate data storage, hooking interrupts, and creating tasks to fetch CAN frames.
Sending and Receiving Logic #
Before sending a frame, the driver configures the frame and checks the transmission buffer status. It monitors the completion status until the frame is fully sent.
For receiving, the driver checks the receive buffer status before attempting to fetch data. Since the system needs to assemble multiple CAN frames into a single data unit, received frames are first stored in a buffer. Once a frame is received, the command register is set to release the receive buffer, ensuring the next frame can be captured correctly.
Packet Assembly and Disassembly #
The disassembly process breaks down user data into individual CAN frames, which are then passed to the send interface. The assembly process takes the data portions of incoming CAN frames and stitches them together to provide a complete data packet to the user.
To manage this, we implemented a triple ring buffer structure:
- Send Ring Buffer: Temporarily stores data during disassembly.
- Receive Ring Buffer: Holds incoming frames before assembly.
- User Data Ring Buffer: Stores the final assembled data for the application.
The send ring buffer must be large enough to hold the total disassembled data. We use semaphores to synchronize the disassembly process; the semaphore is acquired before disassembly and released only after the send operation completes.
To optimize performance, we create a dedicated receive task during the initialization phase. This task has a higher priority than the application task to ensure high-efficiency data ingestion. The task assembles the data and places it into the user data ring buffer, where the application can retrieve it at its own pace.
In this multi-channel setup, the 5 CAN bus channels are logically independent. Data sending is performed synchronously and sequentially across the 5 channels, while data receiving is performed simultaneously across all 5 channels.
🧪 Validation and Testing #
During the self-test phase, we completed loopback tests on all 5 CAN channels, where one channel sent data while the others acted as receivers. In the integrated test phase, the CAN driver was deployed in the electronic warfare management system. The computer board acted as the primary node, connected to CAN Channel 0 along with other modules in the rack. Four external devices were connected to the remaining four channels.
The primary node transmitted data to all 5 channels, and the data was verified via CAN boxes. After successful initialization, the system maintained stable, long-term communication between the primary node and all other nodes. Experimental results confirmed that the FPGA-based driver provides a reliable and stable communication backbone for the system.
For developers working on real-time systems where deterministic behavior is required, integrating these drivers with a real-time operating system like VxWorks can provide the necessary scheduling guarantees for high-priority communication tasks.