CAN bus demo on SAMe54 Xplained Pro - gneidermeier/CANFDDemo_SAMe54_START GitHub Wiki
This guide covers a basic demonstration of the SAM E54 Xplained Pro eval board.
Our purpose for evaluating the SAMe54 is specifically to garner expertise with CAN-bus communication. CAN is a "multi-master serial bus standard for connecting Electronic Control Units [ECUs]" - please refer to the cited article or other resource if not familiar with CAN-bus.
In this guide, the Atmel START web based graphical configuration tool is used to generate the provided CAN FD Demo example reference project. The example will be modified to demonstrate communication between the eval board and a PC over the CAN-bus.
Requirements:
- SAM E54 Xplained Pro eval board
- Micro USB cable (not supplied with the board)
- Computer with Atmel Studio installed
- Terminal emulator program
- USB-CAN interface: using the PEAK-System Technik PCAN-USB Adapter
- CAN-bus monitoring software: if using the PEAK PCAN-USB Adapter, please download PcanView.exe (Windows standalone executable, no install required)
- Optional: BUSMASTER an Open Source Software tool to Simulate, Analyze and Test data bus systems such as CAN - BUSMASTER is compatible with the PEAK PCAN-USB Adapter.
Setting up the SAMe54 Xplained eval board
- Connect SAMe54 Xplained board to PC with micro-USB cable connected to DEBUG USB connector on the board
- Open a serial communication terminal emulator program and configure COM port to 115200-8-N-1-N
Installing the USB-CAN adapter
- Follow manufacturer instructions to install device driver software for the PC
- Downloads for PEAK PCAN-USB Adapter can be found on the Produkt CD web page
- Warning from manufacturer Do not use a USB extension cable to connect the PCANUSB adapter to the computer
- Also warning Install the driver before you connect the adapter to the computer
- see Testing SAMe54 CAN-bus receiver
Generating and importing example project into Atmel Studio
- Open Atmel START web based graphical configuration tool and click BROWSE EXAMPLES
- Filter by keyword e.g. "CAN FD", category Communication and/or Board e.g. "SAM E54 Xplained Pro"
- Select CAN FD Demo and click DOWNLOAD SELECTED EXAMPLE
- In Atmel Studio, open [File | Import | Atmel Start Project] and select the downloaded project bundle CAN FD Demo.atzip
- Build (F7) and Debug (F5) ... board seemingly does nothing - welcome to a non-working CAN-bus project!
Modify clock configuration
The CAN FD Demo is intended to drive the CAN peripheral from a 40 Mhz clock source - the clock frequency into the CAN module is the basis for determining the exact timing values with which to set up the CAN bit timing registers. Unfortunately Atmel Studio seems to have issues with clock configurator and that specifically impact the PLL peripheral.
Although the project settings in Atmel START will not properly configure the clocks for the CAN-bus demo, Atmel START can still be used to reconfigure the project, if the limitations of START are known and thus avoided!
After importing the example project into Atmel Studio, the START configuration tool is available in the Studio IDE.
- Open [ Project | Re-Configure Atmel Start Project ]
- Click CLOCKS tab to open CLOCK CONFIGURATOR window
- Click checkbox in External Crystal Oscillator to enable XOSC1
- Click checkbox in Generic clock generator 1 to enable GCLK1
- Click gear icon in 'Generic clock generator 1' to open CLOCK SETTINGS dialog - open the Generic clock generator 1 source dropdown menu and select External Crystal Oscillator (XOSC1)
- Still in CLOCK SETTINGS, set Generic clock generator 1 division to 12. Click Close - indicated output frequency of GCLK1 should be 1 MHz
- Click gear icon in DPLL0 component - in CLOCK SETTINGS set Reference Clock Source to Generic clock generator 1
- In CLOCK SETTINGS, enter 0 in Loop Divider Ratio Fractional Part and enter 119 in Loop Divider Ratio Integer Part
- Set DPLL0 Reference Clock Selection to GCLK clock reference. Click Close - DPLL0 oscillator should indicate output frequency of 120 MHz
- Click gear icon of Generic clock generator 2 component. Generic clock generator 2 source should be set to Digital Phase Locked Loop (DPLL0)
- In GCLK2 Configuration, uncheck Divide Selection, and set Generic clock generator 2 division to 3.
- Click close - GCLK2 should show 40 MHz clock sourced to CAN (see image below).
- Click GENERATE PROJECT - Project Summary dialog advises that a few files would be overwritten (this is OK right now but be aware that of the possibility of overwriting locally modified files). Click OK.
Connecting CAN-bus phy
A minimal CAN-bus topology consists of a pair of signal wires (CAN-high and CAN-low), with each end of the physical bus terminated by a 120 ohm resistor. The SAMe54 xPlained board features a ATA6561 CAN transceiver with connection pads at PB12/PB13 (see labels CANL and CANH on the SAMe54 board). See image below: CAN-L is connected by white wire, CAN-H connected by red wire, and 120 ohm termination resistor soldered to the connector pins.
The PC is interfaced to the CAN-bus by the PEAK USB-CAN adapter, with CAN-L soldered to pin-2 of female d-sub, CAN-H soldered to pin-7 of female d-sub, and 120 ohm termination resistor soldered across the pins - see images below.
Monitoring CAN-bus on PC
Please ensure that the CAN-USB adapter, device drivers, and CAN-bus monitoring software supported by your particular adapter hardware are installed. This tutorial assumes the use of PEAK USB-CAN adapter and PCAN-View monitoring software.
- Launch PCAN-View, be sure PCAN-USB: Device indicated under Available PCAN hardware.
- Leave default Bit rate 500k. Click OK.
CAN Demo should be running on SAMe54 board. Option Menu should be showing in terminal window. With terminal window in focus, striking the a key sends a standard CAN message with CAN-ID 469h. Each time the message with CAN-ID 469h is sent, PCAN-View should update the display with the received message ID, data, and update the Cycle Time and Count.
Installing a timer to automate message transmission
The CAN FD Demo example provides a simple user interface to interactively generate CAN messages from the SAMe54 eval board. We can easily add an instance of the SAMe54 timer peripheral and extend the example to automate the periodic message transmission.
- Choose "Project | Re-Configure Atmel Start Project"
- On DASHBOARD tab click Add software component - OR - on CLOCK CONFIGURATOR tab, click + icon next to COMPONENTS
- In ADD SOFTWARE COMPONENTS dialog, expand Drivers, select Timer, click Add component(s)
- On DASHBOARD tab, click gear icon in TIMER_0 component that was just added
- Under COMPONENT SETTINGS open the Instance drop-down menu and Select TC0
- Under CLOCKS open the TC drop-down menu and select Generic clock generator 0
- Still on DASHBOARD tab, under BASIC CONFIGURATION section set Prescaler drop-down menu to Divide by 1024 and timer tick in uS should be 1000 (default)
- Click GENERATE PROJECT
Add code to bring up the timer driver - in main()
, prior to entering infinite while() loop, add the invocation of TIMER_0_example()
i.e.:
TIMER_0_example();
while (1) {
TIMER_0_example()
is found to be implemented in the generated file driver_examples.c - the timer task callback functions can also be found there. Add a printf to see the triggering of the timer callback on the terminal emulator:
static void TIMER_0_task1_cb(const struct timer_task *const timer_task)
{
printf(" hello TIMER_0_task1_cb\r\n");
}
Periodic transmission of CAN message on timer0 (task2) callback can be demonstrated by calling CAN_0_example()
(also implemented in the generated file driver_examples.c)
static void TIMER_0_task2_cb(const struct timer_task *const timer_task)
{
CAN_0_example();
}
The START example code configures the CAN device for FD/BR (flexible data rate and flexible bit rate) which is a relatively new feature in CAN specification but not germane to this example (not certain what CAN bus tools are supporting FD/BR at this point). To disable FD/BR, paste the following code prior to the infinite while() in main()
:
/* Disable the FDOE and BRSE from register configuration */
hri_can_set_CCCR_INIT_bit(CAN_0.dev.hw);
while (hri_can_get_CCCR_INIT_bit(CAN_0.dev.hw) == 0)
;
hri_can_set_CCCR_CCE_bit(CAN_0.dev.hw);
hri_can_clear_CCCR_FDOE_bit(CAN_0.dev.hw);
hri_can_clear_CCCR_BRSE_bit(CAN_0.dev.hw);
TIMER_0_example();
Try running the program now and confirm receipt of periodic CAN messages in PCANView (Cycle Time and Count continuously refreshed).
The CAN peripheral module actually has settings to enable/disable variable data rate and bit-rate features. Open the Atmel START DASHBOARD again, select CAN_0, and under BASIC CONFIGURATION, uncheck FD Operation Enable and Bit Rate Switch Enable and GENERATE PROJECT.
Adding GPIO pin function (LED)
SAMe54 Xplained board provides one yellow user LED which can be driven by GPIO pin PC18. This pin is mapped to pin-C of the onboard 'PDEC' connector. The LED can be toggled on the timer callback, and in addition to seeing the blinking LED, the signal can be measured directly by connecting a scope to pin-C of the PDEC connector. Insert the code to setup the GPIO pin function prior to the infinite while() in main()
:
// GPIO on PC18
#define LED0 GPIO(GPIO_PORTC, 18)
gpio_set_pin_level(LED0, true);
// Set pin direction to output
gpio_set_pin_direction(LED0, GPIO_DIRECTION_OUT);
gpio_set_pin_function(LED0, GPIO_PIN_FUNCTION_OFF);
TIMER_0_example();
Now add the line to toggle the pin in the timer task callback:
static void TIMER_0_task1_cb(const struct timer_task *const timer_task)
{
gpio_toggle_pin_level(GPIO(GPIO_PORTC, 18));
}
Testing SAMe54 CAN-bus receiver
In PCAN-View, open the New Transmit Message dialog box. Enter the CAN message ID and up to 8 bytes of data. Set the cycle time to e.g. 1000mS. See image below.
Modify CAN_0_rx_callback()
as shown below:
void CAN_0_rx_callback(struct can_async_descriptor *const descr)
{
...
can_async_read(descr, &msg);
printf("%04X: %02X %02X %02X %02X %02X %02X %02X %02X\r\n",
msg.id, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
return;
}
Verify on terminal emulator that the SAMe54 is receiving the message sent from PCAN-View.
Next: CAN Bus timing, minimal example on SAMe54 Xplained Pro