NetBurner 3.5.6
PDF Version
CAN to Serial

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

  1. CAN Message Reception: Listens for incoming CAN messages
  2. Message Display: Shows received data in human-readable format
  3. Interactive Transmission: Send messages via keyboard input
  4. Dual ID Support: Handles both standard and extended CAN IDs
  5. Real-Time Operation: Non-blocking message processing

Program Flow

  1. 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
  2. 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:**

  • CAN0 TX: J2[42] (PINJ2_42_CAN0_TX)
  • CAN0 RX: J2[39] (PINJ2_39_CAN0_RX)
  • CAN1 TX: J2[44] (PINJ2_44_CAN1_TX)
  • CAN1 RX: J2[41] (PINJ2_41_CAN1_RX)

    NANO54415:**

  • CAN0 TX: Pin 27 (PIN_27_CAN0_TX)
  • CAN0 RX: Pin 29 (PIN_29_CAN0_RX)
  • CAN1 TX: Pin 22 (PIN_22_CAN1_TX)
  • CAN1 RX: Pin 20 (PIN_20_CAN1_RX)

    SB800EX:**

  • CAN0 TX: PortB[2] (tertiary function)
  • CAN0 RX: PortB[1] (tertiary function)
  • CAN1 TX: PortB[0] (primary function)
  • CAN1 RX: PortC[7] (primary function)

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:**

  • ConfigureCanPins(uint32_t canModuleNumber): Sets up CAN pins for each platform

    CAN Operations:**

  • MultiCanInit(): Initialize CAN controller with baud rate
  • RegisterMultiCanRxFifo(): Set up receive message filtering
  • MultiCanSendMessage(): Transmit CAN messages
  • CanRxMessage: Class for receiving and parsing messages

    Helper Functions:**

  • NormToNbId(): Convert standard ID to NetBurner format
  • ExtToNbId(): Convert extended ID to NetBurner format
  • NbToNormId(): Convert NetBurner format to standard ID
  • NbToExtId(): Convert NetBurner format to extended ID
  • IsNBIdExt(): Check if ID is extended format

Message Processing

Reception Processing

CanRxMessage can_msg(&fifo, 1); // Wait up to 1 tick
if (can_msg.IsValid()) {
uint8_t buffer[8];
uint8_t len = can_msg.GetData(buffer, 8);
uint32_t id = can_msg.GetId();
// Process message data
// Display formatted output
}

Transmission Processing

char buffer[255];
fgets(buffer, 255, stdin); // Get user input
if (strlen(buffer) > 8) {
buffer[8] = 0; // Truncate to CAN data limit
}
// Send with extended ID
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

// Select CAN module (0 or 1)
#define CAN_MODULE (0)
//#define CAN_MODULE (1)
// Set baud rate
#define BAUD_RATE 500000

Usage Examples

Testing CAN Communication

  1. Setup Test Network:
    • Connect two NetBurner devices via CAN bus
    • Ensure proper termination (120 ohm resistors)
    • Configure both with same baud rate
  2. Interactive Testing:
    Type a message and press <enter> to transmit
    > Test Message
    Sending 12 bytes
    Sent successfully
  3. Monitor Reception:
    Received a CAN message of 12 bytes from Extended ID=4660 or 0x001234
    Data = [Test Message]

Message Filtering Examples

// Accept all messages (mask = 0)
int chan = RegisterMultiCanRxFifo(CAN_MODULE, NormToNbId(0x123), &fifo);
// Accept only specific ID (mask = 0xFFF)
int chan = RegisterMultiCanRxFifo(CAN_MODULE, NormToNbId(0x150), &fifo, 0xFFF);
// Accept range of IDs (0x400-0x4FF)
int chan = RegisterMultiCanRxFifo(CAN_MODULE, NormToNbId(0x424), &fifo, 0xFF00);

Troubleshooting

Common Issues

  1. 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
  2. No messages received
    • Verify receive filters and masks
    • Check physical bus connections
    • Confirm other nodes are transmitting
    • Monitor bus with oscilloscope/analyzer
  3. 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
  4. 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:**

// Add status monitoring
printf("CAN Status: 0x%08X\n", GetCanStatus(CAN_MODULE));

Message Statistics:**

// Track message counts
static uint32_t rxCount = 0, txCount = 0;
rxCount++; // Increment on receive
printf("RX: %ld, TX: %ld\n", rxCount, txCount);

Buffer Status:**

// Monitor FIFO status
printf("FIFO entries: %d\n", fifo.GetCount());

Advanced Features

Multiple Message Types

// Register multiple receive channels
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

// Process different message types
switch(NbToNormId(id)) {
case 0x100: ProcessSensorData(buffer, len); break;
case 0x200: ProcessControlData(buffer, len); break;
case 0x300: ProcessStatusData(buffer, len); break;
}

Error Handling

// Comprehensive error checking
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