CAN to Serial Bridge
Overview
This NetBurner application creates a bidirectional bridge between CAN bus and serial console communications on MCF5441X-based platforms. The application receives CAN messages and displays them on the serial console, while also allowing users to type messages that are transmitted over the CAN bus.
- Warning
- At least 2 CAN devices are required to avoid a hardware bus error.
Supported Platforms
- MOD5441X - CAN0: J2[42]/J2[39], CAN1: J2[44]/J2[41]
- NANO54415 - CAN0: Pin 27/29, CAN1: Pin 22/20
- SB800EX - CAN0: PortB[2]/PortB[1], CAN1: PortB[0]/PortC[7]
Hardware Requirements
- NetBurner module with CAN capability
- CAN transceiver (if not integrated on board)
- CAN bus network with other nodes
- Serial console connection for user interface
- CAN bus termination resistors (120 ohm at each end)
CAN Configuration
Default Settings
- CAN Module: CAN0 (configurable to CAN1)
- Baud Rate: 500 kbps
- Message Types: Both standard (11-bit) and extended (29-bit) IDs
- Receive Filters:
- Standard ID: 0x123
- Extended ID: 0x1234
Message Filtering
The application accepts messages matching:
- Standard ID 0x123: Demonstrates 11-bit CAN ID reception
- Extended ID 0x1234: Demonstrates 29-bit CAN ID reception
- Mask: 0 (accepts all messages - modify for selective filtering)
Application Functionality
Main Features
- CAN Message Reception: Listens for incoming CAN messages
- Message Display: Shows received data in human-readable format
- Interactive Transmission: Send messages via keyboard input
- Dual ID Support: Handles both standard and extended CAN IDs
- Real-Time Operation: Non-blocking message processing
Program Flow
- Initialization
- Initialize network stack and diagnostics
- Configure platform-specific CAN pins
- Initialize CAN controller at 500 kbps
- Register message receive FIFOs
- Set up message filters
- Main Loop
- Poll for incoming CAN messages (1 tick timeout)
- Display received messages with ID and data
- Check for serial input from user
- Transmit user-entered messages on CAN bus
Message Format Display
Received Messages:**
Received a CAN message of 8 bytes from ID=291 or 0x123
Data = [Hello!!!]
Received a CAN message of 4 bytes from Extended ID=4660 or 0x001234
Data = [Test]
User Input:**
Type a message and press <enter> to transmit
> Hello World
Sending 11 bytes
Sent successfully
Hardware Connections
Pin Assignments by Platform
MOD5441X:**
CAN Bus Wiring
- CAN_H: Connect to CAN High line
- CAN_L: Connect to CAN Low line
- GND: Common ground with other nodes
- Termination: 120 ohm resistors at bus endpoints
Key Components
Core Functions
Platform Configuration:**
Message Processing
Reception Processing
CanRxMessage can_msg(&fifo, 1);
if (can_msg.IsValid()) {
uint8_t buffer[8];
uint8_t len = can_msg.GetData(buffer, 8);
uint32_t id = can_msg.GetId();
}
Transmission Processing
char buffer[255];
fgets(buffer, 255, stdin);
if (strlen(buffer) > 8) {
buffer[8] = 0;
}
MultiCanSendMessage(CAN_MODULE, ExtToNbId(0x1234), (uint8_t*)buffer, strlen(buffer), 50);
Data Display Format
The application displays non-printable characters in hex format:
- Printable characters: Shown as-is
- Non-printable: Displayed as {XX} hex values
- Mixed data: Combination of text and hex
Example: Data = [Hello{0A}{00}World]
Building and Deployment
Configuration Options
#define CAN_MODULE (0)
#define BAUD_RATE 500000
Usage Examples
Testing CAN Communication
- Setup Test Network:
- Connect two NetBurner devices via CAN bus
- Ensure proper termination (120 ohm resistors)
- Configure both with same baud rate
- Interactive Testing:
Type a message and press <enter> to transmit
> Test Message
Sending 12 bytes
Sent successfully
- Monitor Reception:
Received a CAN message of 12 bytes from Extended ID=4660 or 0x001234
Data = [Test Message]
Message Filtering Examples
int chan = RegisterMultiCanRxFifo(CAN_MODULE, NormToNbId(0x123), &fifo);
int chan = RegisterMultiCanRxFifo(CAN_MODULE, NormToNbId(0x150), &fifo, 0xFFF);
int chan = RegisterMultiCanRxFifo(CAN_MODULE, NormToNbId(0x424), &fifo, 0xFF00);
Troubleshooting
Common Issues
- CAN initialization failed
- Check CAN transceiver power and connections
- Verify baud rate matches other network nodes
- Ensure CAN pins are properly configured
- Check for proper bus termination
- No messages received
- Verify receive filters and masks
- Check physical bus connections
- Confirm other nodes are transmitting
- Monitor bus with oscilloscope/analyzer
- Messages not sending
- Check transmit buffer availability
- Verify message length (less than or equal to 8 bytes)
- Ensure bus arbitration is working
- Check for bus-off conditions
- Garbled data display
- Verify baud rate matches network
- Check for electrical noise/interference
- Ensure proper grounding between nodes
- Verify message format consistency
Debug Techniques
Bus Monitoring:**
printf("CAN Status: 0x%08X\n", GetCanStatus(CAN_MODULE));
Message Statistics:**
static uint32_t rxCount = 0, txCount = 0;
rxCount++;
printf("RX: %ld, TX: %ld\n", rxCount, txCount);
Buffer Status:**
printf("FIFO entries: %d\n", fifo.GetCount());
Advanced Features
Multiple Message Types
int chan1 = RegisterMultiCanRxFifo(CAN_MODULE, NormToNbId(0x100), &fifo);
int chan2 = RegisterMultiCanRxFifo(CAN_MODULE, NormToNbId(0x200), &fifo);
int chan3 = RegisterMultiCanRxFifo(CAN_MODULE, ExtToNbId(0x12345), &fifo);
Enhanced Data Processing
switch(NbToNormId(id)) {
case 0x100: ProcessSensorData(buffer, len); break;
case 0x200: ProcessControlData(buffer, len); break;
case 0x300: ProcessStatusData(buffer, len); break;
}
Error Handling
CAN_RETURN_CODE result = MultiCanSendMessage(CAN_MODULE, id, data, len, timeout);
switch(result) {
case CAN_OK: printf("Sent successfully\n"); break;
case CAN_TIMEOUT: printf("Send timeout\n"); break;
case CAN_ERROR: printf("Send error\n"); break;
case CAN_BUSY: printf("Bus busy\n"); break;
}
Application Extensions
Protocol Implementation
- CANopen protocol stack
- J1939 automotive protocol
- Custom application protocols
- Multi-master arbitration
Data Logging
- Log all CAN traffic to file system
- Timestamp message reception
- Statistical analysis tools
- Playback recorded sessions
Network Integration
- Forward CAN messages over TCP/UDP
- Web interface for CAN monitoring
- MQTT bridge for IoT integration
- RESTful API for remote control
Real-Time Processing
- Interrupt-driven message handling
- Priority-based message processing
- Real-time response to critical messages
- High-frequency data acquisition
Performance Characteristics
Timing
- Message Latency: Typically <1ms for receive processing
- Throughput: Up to 500 kbps bus capacity
- CPU Loading: Low due to hardware CAN controller
- Response Time: Limited by RTOS task scheduling
Scalability
- Message Rate: Hundreds of messages per second
- Network Size: Up to 127 nodes per network
- Buffer Depth: Configurable FIFO sizes
- Concurrent Channels: Multiple receive filters per module
Related Examples
- Serial Communication: UART/RS-232 bridging
- Ethernet Bridge: CAN to TCP/UDP gateway
- Data Logging: Persistent CAN message storage
- Protocol Stacks: Higher-level CAN protocols