Overview: Connect the boards, the CAN bus cable, the debugger, and the Systec CAN Monitor.. Procedural Steps Step 1.1 Connect the black A-Mini B USB cable to the PC and the RX board’s
Trang 1CAN In A Day
Description: Learn how easy it is to use CAN for your networked application by using the CAN API
Lab Sections
1 Board, CAN bus, and CAN monitor setup 2
2 Launch e2studio and the CAN workspace 3
3 Init CAN Transmit from a CAN mailbox 6
4 Run the CAN monitor 9
5 Transmit frames back-to-back 11
6 Receive to a CAN mailbox 12
7 Using CAN interrupts 14
Lab Objectives
Learn how to
1 Initialize CAN quickly by using the API
2 Transmit data from a CAN mailbox
3 Receive data of a certain CAN ID to a
mailbox, then ‘take care of it’ in the
application
4 Monitor CAN bus traffic with a simple low
cost ($80) CAN bus monitor
Skill Level
Hands-on debug session at the C-programming
level Make simple changes in the code and
watch CAN data in the debug watch window
Time to Complete Lab
90 minutes including CAN presentation
Lab Materials
Please verify you have the following materials
at your lab station
SW
• e2studio v1.01 or above
• Segger Driver for USB debug
• RX Toolchain v 1.2.0.0 or above
• Systec PCANView CAN Monitor software installed
HW
• YRDK63N board
• USB cable: Standard-A to Mini-B for RDK Segger port (Black USB cable.)
• USB cable: Standard-A to Standard-B for CAN monitor (White USB cable.)
• Systec CAN monitor
• Red/White CAN monitor cable
|with DB9 connector
Trang 21 Board, CAN bus, and CAN monitor setup
Overview:
Connect the boards, the CAN bus cable, the debugger, and the Systec CAN Monitor
Procedural Steps
Step 1.1 Connect the black A-Mini B USB cable to the PC and the RX board’s USB port (Segger J-Link
port) which is next to the 5V power inlet to the board
Step 1.2 The 5V power inlet will not be used; we will let the PC power the board through the USB’s +5V
Step 1.3 Connect the white A-B USB cable to the Systec monitor and the PC
Step 1.4 Connect the grey DB-9 CAN bus connector to the other side of the CAN sniffer
YRDK63N
A to mini-B USB cable
To PC for J-Link debug
J8
SYSTEC CAN
LCD
5V DC Not necessary.
CAN only needs 0-3.5 V
Ring of LEDs
v
A to B USB cable
To PC for Sys Tec CAN monitor
CAN Hi = thick red wire -> pin 1
J-Link
Debug
Skip to next section if the board and the Systec CAN monitor are already connected
Trang 32 Launch e2studio and the CAN workspace
Overview:
Using e2studio, connect to the board via the USB debug link
Procedural Steps
Step 2.1 Launch
Step 2.2 Workspace: When the workspace launch window pops up, select the workspace
directory
e2studio
(This folder only contains just one project; in folder CAN_API_Demo_YRDK63N
Step 2.3 Project: Make sure the project is active by clicking on the name
‘CAN_API_Demo_YRDK63N‘ in the project explorer pane to the left
.)
Step 2.4 Build:
Step 2.5 Debug launch: Select Run=>Debug Configurations
Select Project=>Build Project to build the target binary
Step 2.6 Run and debug the target with Renesas GDB Hardware Launch Copy the debug
configurations from the next figure If the option CAN_API_Demo_YRDK63N is not available,
double-click Renesas GDB Hardware Debugging
Trang 4Step 2.7 If asked to select debug hardware, select Segger JLink
As target device, select R5F63NB
Step 2.8 If E2studio asks for your permission to go to the Debug Perspective, do so
Step 2.9
Step 2.10 Click Reset Then Step (F6) to see if you are
The binary should automatically download to the target
connected successfully
Step 2.11 It should now look like this The green
back-ground shows what source code the
Program Counter is closest to
Step 2.12 Put a breakpoint in main.c, function main()
By stepping through main(), verify that the
board is running OK
Step 2.13 If you for example missed seeing the
LEDS blink, just press Pause (d
press the red square - that exits
on’t debug),
2.10
Reset, and Go as shown in Step
Step 2.14 If you came this far successfully,
e2studio and debugging work!
We can instead just focus on using the
CAN API
Trang 6Open up r_can_api.h so that you see the complete CAN API
To start, we need to enable the CAN peripheral with R_CAN_Create, which sets up the CAN
peripheral registers for Standard CAN, sets the bitrate (determined by config_r_can_rapi.h), clears the
CAN mailboxes etc
We also call R_CAN_PortSet to set up the MCU’s CAN Tx, Rx, and transceiver ports
These two functions will call any other necessary functions so that we can start communicating
3 Init CAN Transmit from a CAN mailbox
Overview:
Using the API we will set up the CAN peripheral to transmit ID 7FFh and transmit from a mailbox
Procedural Steps
Step 3.1 In e2studio, press Pause (Step 2.10.)
Step 3.2 Go to (step into) the main application function can_api_demo() defined in file can_api_demo.c
Initialize the CAN peripheral
Step 3.3 Scroll down to around line 200 and find
api_status = R_CAN_Create(g_can_channel);
with CAN
Step 3.4 Keep stepping in can_api_demo() Further down, bus mode is selected We will use Normal
CAN bus communication mode Note the possibility to use Internal loopback mode!
Create a CAN dataframe
Question
Open up the CAN API application note r01an0339eu_rx.pdf, and keep the file open for
later
What does R_CAN_Create do? _
_
_
Trang 7Step 3.5 In can_api_demo(), go down a few more lines to init_can_app() Step into it with F5 (or put a
breakpoint in init_can_app and press Restart)
Step 3.6 Keep going until you get to where the global structure g_tx_dataframe is assigned data
The transmit mailbox variable we are using is global for the sake of the demo; to be visible
from a large scope It is defined at the top of the file Its type, can_frame_t, is defined
The structure contains three elements; the CAN ID, the data length code (number of
payload bytes), and the actual payload data Uncomment the code where g_tx_dataframe is
assigned values – change them if you like Note the CAN ID and data values below
A pointer to this g_tx_dataframe structure will be passed later on to the CAN API transmit
function
Recompile and download later when we are done changing the code Keep on stepping!
Step 3.7 We will transmit the CAN frame from the function sw1_function in switches.c You can
conveniently locate a function by using the Project Explorer tab as shown below
We have defined the dataframe variable in RAM, and filled it with content
Now we need to call the transmit API to have it sent over the CAN bus!
Question
In Normal CAN mode, no transmitted CAN messages are received within the same node (MCU) even if a mailbox is set to pick up that same CAN ID Loopback mode simplifies
testing of a CAN application in this respect
How many boards are needed to test both send and receive of the same IDs using
Internal
Loopback mode? _
Hint: CAN API Appl note section 10.1
Question
Note the CAN ID for the demo test frames that will be transmitted from the board later
(using SW1):
Nr bytes in the CAN dataframe:
Payload of the CAN dataframe:
The CAN API R_CAN_Control is called to set CAN in HALT mode before we
configure mailboxes This is a good practice so that frames are not coming and going as
we are manipulating the mailboxes!
Trang 8Step 3.8 Uncomment the call to R_CAN_TxSet It takes four
arguments:
- Channel nr: we only have ch 0.
- Mailbox nr you can pick any number 0-31
- Pointer to frame
- Dataframe or Remote frame
to be sent
Step 3.9 Before we run, back in main(), uncomment the call to
check_can_errors() right after read_switches() so that we can detect
bus problems by viewing the board display
Step 3.10 Compile (Ctrl+B), download (Step 2.4), and Go
Step 3.11 Verify that when you
(F8)
press SW1
the sw1_function executes, and that the frame shows up in the
on the RX board that
PCANview CAN monitor by following Section 4
Trang 94 Run the CAN monitor
Overview:
We will use the CAN Monitor to view CAN data
Procedural Steps
Step 4.1 Launch the PCANView from the desktop or the Start Menu->Programs-> USB_CANmodul
Utilities->Tools->PCANView (USBCAN)
Step 4.2 First dialog screen:
Device-Nr = any
Baud rate = 500 kbaud
CAN Channel is 0
Step 4.3 Click OK
Step 4.4 On the next screen verify the Message Filter is set for
Standard ID and to filter messages from 000-7FF
Step 4.5 Click OK
Trang 10
The columns of the Systec PCAN window are
Message CAN-ID
Length Number of bytes in data field
Data CAN dataframe payload
Period Time [ms] since last message
Count Nr of frames with this CAN ID
Clear the window with the Escape-key
Step 4.6 You should now see the monitor window Each message line shows the latest frame for a
particular CAN message ID
This example screenshot shows the sniffer received 66 CAN frames of CAN-ID
0x700 (You will not see this in the lab.)
Trang 115 Transmit frames back-to-back
Step 5.1 When sending multiple frames as fast as possible back-to-back, we want to make sure one is
transmitted before trying to send the next one, to not mess it up
Step 5.2 In switches.c, uncomment the back to back code by changing “#if 0” to “#if 1”
Study what the code below it does
/***************************************************************
This is how to send multiple messages back to back
****************************************************************/
#if 0 //Set to 1 to uncomment for lab!
Step 5.3 Remove the transmit back-to-back code again by changing “#if 1” to “#if 0”
Let’s learn how to send frames sequentially as fast as possible using the same mailbox We’ll
use a smaller cousin of R_CAN_TxSet :
R_CAN_Tx(0, CANBOX_TX);
This API needs the last two arguments of Step 4.8 Therefore it can only be used if the previous API has first been used to for that mailbox
Question
1 When you have compiled, downloaded and run, are the full number of CAN frames sent as expected? _
2 Change the CAN ID, the number of messages, recompile and try again!
Using multiple mailboxes we would be able to send sequential messages a bit faster Using transmit FIFO mode would be the fastest
Question
Looking at the API suite, what function could be used if we want to check the status of an ongoing transmission? _
Trang 126 Receive to a CAN mailbox
Overview:
Set up a mailbox to receive a certain CAN ID Use the CAN bus monitor to send a message with this ID,
check that it is received into the mailbox, and copy the data from the CAN peripheral to user application data
Procedural Steps
Step 6.1 Back in the init_can_app function, let’s add some code to receive CAN data Note we are
using STD_ID_MODE Uncomment the lines
g_rx_dataframe.id = g_rx_id_default;
api_status |= R_CAN_RxSet(g_can_channel, CANBOX_RX, g_rx_dataframe.id, DATA_FRAME);
Step 6.2 The can demo main loop continuously calls can_poll_demo() in can_api_demo.c Locate the
function with e.g the Project Explorer Inside this function a receiving mechanism is set up using the API
Step 6.3 In both API calls there may be errors in the arguments if the lab creator was being difficult
Better check that the arguments are correct, then compile and download
Step 6.4 Set a breakpoint just inside the second if statement in the can_poll_demo receive section,
then press Go (F8)
Step 6.5 Set up the monitor to transmit a CAN data-frame to the board with your receive ID In
PCANView select Transmit->New Enter the above ID Enter your choice for Length and Data Enter 0 for Period
Step 6.6 Press OK
Question
Which CAN mailbox is set up to receive data? (You can change it.)
Set the CAN ID to a value between 1 – 0xFF:
Question
Which functions are used to receive a CAN frame?
_ _
Trang 13Step 6.7 Now highlight the line in the Transmit section and use the space bar to send the message
from the PC (the sniffer is told to send it.)
Step 6.8 When the breakpoint triggers go to the watch tab (it’s the ‘Expressions’ tab in e2studio)
Step 6.9 Highlight “rx_dataframe” then drag the data structure name into Expressions tab
Step 6.10 Step over the function R_CAN_Read (F6) and study the content of g_rx_dataframe in the
Expressions tab by clicking on the ‘+’ in front of it You can see the hex value of the data on the right side of the window when you highlight the data
Question
Do the data length and data payload values match what you sent? _
What happens if you rapidly fire multiple frames from the Systec CAN monitor? (Watch the board
display.)
To avoid overruns, we need to more quickly retrieve the CAN frames from the receive
mailbox We could do this by using the FIFO, or as a first resort, use the CAN receive interrupt
Trang 147 sing CAN interrupts
Overview:
Use the CAN interrupts to much more quickly act on bus activity, instead of polling for successful CAN transmits and receives of CAN data
Procedural Steps
Step 7.1 Activate CAN interrupts by changing USE_CAN POLL in config_r_can_rapi.h, then build and
download the project
Step 7.2 Remove all breakpoints so we don’t have breakpoints in non-used code
Step 7.3 Put two breakpoints in can_int_demo 1) Where transmit and 2) receive flags are detected as
being set
Open the Breakpoints tab in the debug view to easily delete them all
Step 7.4 The interrupt routines are at the bottom of can_api_demo.c Put a breakpoint in each routine
to make sure they work
Transmit interrupt
Step 7.5 Go ahead and check that this works
Receive interrupt
Step 7.6 Similar to transmit, check that receive works
/END lab
Using the CAN interrupts is a fast way to empty the mailboxes of data so that the data is not
lost (overrun) The receive interrupt can copy the data over to a user data variable so the mailboxes can be reused faster than when mailbox poling is used
Question
What does the receive interrupt routine do?
_
The interrupt routine was written to be as short as possible, but perhaps it is a bit too short - How
can code be rearranged between can_int_demo and the receive interrupt routine to vastly lessen the
chance of having overruns? (Hint; see the top green box above.)
_
_
The transmit interrupt routine fires when a frame has been sent onto the CAN bus The
interrupt double checks that data was sent successfully from our demo mailbox If so, a flag is set to notify the application
The application just calls can_int_demo continuously from the main program loop can_int_demo
checks the flag If the receive flag is set the data frame content is displayed