CAN bus demo with BUSMASTER - gneidermeier/CANFDDemo_SAMe54_START GitHub Wiki

BUSMASTER is an Open Source Software tool to Simulate, Analyze and Test data bus systems such as CAN.

BUSMASTER is compatible with a variety of available CAN bus interface adapters, including the PEAK-System Technik PCAN-USB Adapter which is being used in this tutorial series. The host environment for BUSMASTER is MS-Windows.

BUSMASTER is a useful addition to the low-cost CAN bus tool box. BUSMASTER provides some functionality not available in the free PcanView.exe such as support for J1939 networks, signal database, and node simulation.

The current version of BUSMASTER at this time is 3.2.2. Download it from BUSMASTER home page.

This guide is intended to serve only as a supplement to the BUSMASTER user documentation, which can be found in the PDF help file.

Installation of GCC for node simulation

BUSMASTER allows the user to add their own program code to simulate behavior of a specific device on the bus - this facility is known as node simulation. Vector Canalyzer users are already familiar with this capability if they have incorporated CAPL programs to automate their measurement setup. In BUSMASTER, node simulation is provided by hosting a limited C development environment based on the MinGW compiler toolchain. This capability is arguably the biggest > selling point of BUSMASTER, that is, as a very economical alternative to e.g. Vector Canalyzer or other comparable commercially available tools. Authoritative instructions for installation of MinGW using TDM-GCC Installer are found in the Help file, which should be available for reference while following this guide.

Download the TDM-GCC Installer from the SourceForge link. Be sure to follow the installation instructions exactly.

Requirement

  • Setup the eval board as per previous tutorial, running the board firmware with a periodic (e.g. 500mS) message transmitted
  • Please refer to previous tutorial Previous tutorial to be sure that the eval board, PC CAN USB adapter, and CAN bus wiring are correct and setup PCANView to confirm function of test bench
  • Disconnect or close PCANView if it is running, before starting BUSMASTER

Starting BUSMASTER

  • Device driver for CAN adapter must first be installed
  • Click the ribbon tab for CAN and expand Driver Selection - if the adapter driver is installed then the selection should be available under the drop-down menu e.g. PEAK USB
  • Click Channel Configuration on the CAN ribbon to open the Hardware Selection dialog
  • If BUSMASTER has not yet been configured but the CAN adapter driver is recognized, the PCAN-USB Driver should show up in the Available CAN hardware pane. Click the >> button to set the adapter as the Configured CAN Hardware
  • This example will use bit-rate of 500 kbps - enter 500000 in the BaudRate box (setting of timing registers will be set automatically based on the chosen bit rate)
  • Click Connect button in the Hardware Configuration section of the CAN ribbon - messages sent from eval board should show as Rx messages in BUSMASTER Message Window - Time stamp, ID, Data Bytes should update in the window

Transmit Window

BUSMASTER provides a simple message transmission facility, basically equivalent to that of PCAN-View. Multiple CAN messages can be individually configured for extended or standard message ID and data content - timing can be automated with a selected rate, or manual transmission trigger by assignment of a key-press to trigger each message.

Open the Transmit Window from Simulation Windows section of the CAN ribbon.

In the Configure Transmission Messages window, click in [Add Message] under Message Name heading. For this example, enter a CAN message ID of 0x469. This message ID is chosen as it is explicitly admitted by the filter setup in the CAN_0_example() function generated by Atmel START.

  • Change Repetition to 1000 ms and click the checkbox to enable message Repetition
  • Under Data Byte View, values of all 8 bytes of the CAN message are defaulted to 0 - click into these boxes and enter some different byte values (range 0-255)
  • Configure Transmission Messages window can be optionally left open, or closed, and click into the Message Window to bring it forward
  • Click Connect on the CAN ribbon to restart message logging
  • The previous guide demonstrated how to setup the eval board firmware with capability to parse the received CAN messages and display them on the serial terminal
  • Click Disconnect on the CAN ribbon, click Transmit Window to open Configure Transmission Messages window
  • Click the Repetition checkbox (to disable the periodic message transmission)
  • Click the checkbox on Key to enable manually triggered message generation
  • Double click the default Key ('a') to optionally change the hotkey for this message
  • Close the Configure Transmission Messages window and confirm that the test message is transmitted with each hotkey press
  • Transmission messages can be added to the configuration and or modified while BUSMASTER is connected to the bus, however, it is necessary to Disconnect before messages can be deleted

Node Simulation

The previous section of this guide demonstrated the capability to generate messages on the CAN bus with designated repetition rate, or manually triggered by a user-configured hotkey. However, the content of transmitted message is essentially > static in nature. On an actual system, such as on a vehicle or industrial equipment, the content of the CAN message would typically convey some dynamic quantity such as a sensor measurement, or a commanded to set output drive of a controlled device such as fuel injector or other kind of actuator. This is where the value of BUSMASTER node simulation capability becomes evident.

Setup

A new simulation node can be setup as an entirely new configuration, or existing simulation files (.cpp source code) can be imported e.g. from some pre-existing BUSMASTER project. Node configuration setup is a bit awkward so hopefully the following instructions will help to sidestep the potential pitfalls.

  • Disable any messages setup in the Transmission Message Window by unchecking their enable box, and close the window
  • Be sure BUSMASTER is not connected to the bus - click disconnect if necessary
  • Click Node Simulation under Simulation Windows in the CAN ribbon to open the Configure Simulated Systems window
  • In the Simulated Systems window, one or more simulation modules can be added to the hierarchy under the top level CAN Bus - click the green button with the plus sign to add a new simulation module to the test configuration
  • On Node Details dialog, enter the node name as it should appear under the hierarchy e.g. maf_sensor
  • Click Browse button on Node Details and browse into a suitable directory to store the node source code as well as generated object files etc. e.g. C:\Users\kosmo\project\busmaster\maf_sensor
  • Non-intuitive part: type the name of the desired .cpp file for the node source code, e.g. maf_sensor.cpp, then click Open on the file dialog, and click OK on the Node Details dialog
  • If done correctly, a new node editor window (e.g. titled e.g. maf_sensor) opens up with contents set to the newly created node template file
  • The new node simulation file (.cpp file) contains only some boilerplate comments and #include directives - "exclamation mark" indicated next to checkbox warns that this file has not yet been compiled - right click the node and choose build - if MinGW compiler is properly setup, the node should quickly compile and link into a .dll executable - exclamation point changes to green checkmark to indicate success status - see the Output Window for status "xxx.dll" created successfully.

Adding Timer Handler to simulation

The node simulation editor window (or called Function Editor, to stay consistent with BM documentation) is divided into three panes. To help correspondence between this guide and the BM docs, will use the following designators:

  • Pane 1: the leftmost pane where the various Handlers, Events, and Global Variables are represented
  • Pane 2: displays the content of the .cpp source file
  • Pane 3: the editor window where code can be added or modified to a selected function

To begin, add a Timer Handler, which will replicate the timed message functionality already available in the Transmit Window configuration. For this exercise, simulate periodic transmission of sensor measurement.

  • Right click Timer Handlers in Pane 1 and choose Add ... - enter Timer Handler Name (valid C code character e.g. "rel_humidity") and Timer Value of 500 (msec) - BM generates a new function name combining OnTimer with Handler Name and numerical timing value
  • For this example, the new OnTimer_ function will generate a CAN message to broadcast the humidity measurement. Should be noted that transmitting a message is not a foregone conclusion, e.g. there may be periodic logic here to do other parts of the simulation processing that does not necessarily culminate in the immediate broadcast of a CAN message.
  • Add a global timer counter: click in Global Variables, then click into Editor Pane and enter the variable definition using proper C syntax i.e. int timer_counter = 0;
  • Click into OnTimer_rel_humidity_500() and enter
timer_counter += 1;
STCAN_MSG msg1;
msg1.id = 0x469;
msg1.data[0] = 0xa5;
msg1.data[1] = timer_counter;
SendMsg(msg1);
  • After compiling the changes to the simulation module, connect to CAN bus to run it. The 0x469 message should be received by the eval board (and show up on the terminal window)

Especially note that the value in data byte[1] is incremented at each periodic update. Congratulation on successful CAN node simulation!

The editor built into BM is fairly crude, and requires knowledge of the BUSMASTER API functions - refer to API Reference and Examples sections under [ CAN | CAN Node Simulation ] chapter of BM user documentation.

Adding Message Handler to simulation

Message Handlers allow any message visible to the node to be processed programmatically.

First of all, make sure the eval board is broadcasting a periodic message (the default example has the message with extended ID 0x100000A5 being broadcast at 500msec period, which should be evident in the BM Message Window.

  • Right click Message Handlers, and in Based on, select Message ID, and then in _Message ID:0x enter 100000A5, click Apply and Close
  • Under Message Handlers click OnMsgID_100000a5() to bring the newly created function body into focus in the editor pane
  • Enter the following code:
Trace("RxMsg.id = %X  [0] %X  [1] %X\n", Rxmsg.id, Rxmsg.data[0], Rxmsg.data[1]);
  • Compile the module and Connect to the bus. If the Trace Window is not visible, click the View ribbon expand Windows and click Trace Window

*** Message Replay

Message Replay can take a log of message data captured from CAN network, and re-generate messages of interest onto the bus. This is sometimes superior to node simulation, and is a premium feature found in commercial tools such as Vektor Canalyzer. For example, data could be logged on a real vehicle or piece of equipment, and then used at the test bench to replay those signals, with correct timing, into a test setup. Data could represent a drive-cycle or other equipment function cycle. If such data is available it is potentially more productive than trying to program/simulate appropriate behavior of such equipment. There might be a module such as an ECU being tested that needs to be stimulated by a realistic time series data to represent e.g. a sensor measurement or other activity visible on the CAN bus.