CAN In A Day_LabProcedures (1)

14 307 0
CAN In A Day_LabProcedures (1)

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

LAB PROCEDURE CAN API Lab – 2L02I Page 1 of 14 CAN In A Day RX CAN API Lab 2L02I 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 LAB PROCEDURE CAN API Lab – 2L02I Page 2 of 14 1 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 bus monitor v U A R T LCD 5V DC Not necessary. CAN only needs 0-3.5 V SW3 SW2 SW1 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. LAB PROCEDURE CAN API Lab – 2L02I Page 3 of 14 2 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. C:\Workspace\e2studio\CAN. (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. LAB PROCEDURE CAN API Lab – 2L02I Page 4 of 14 Step 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. LAB PROCEDURE CAN API Lab – 2L02I Page 5 of 14 LAB PROCEDURE CAN API Lab – 2L02I Page 6 of 14 Open 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 7FF h 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); The while(1) forever loop is there to hinder you from proceeding in case something is wrong 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. (C:\Workspace\e2studio\CAN. ) What does R_CAN_Create do? ___________________________________________ _______________________________________________________________________ _______________________________________________________________________ LAB PROCEDURE CAN API Lab – 2L02I Page 7 of 14 Step 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 in r_can_api.h . 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! LAB PROCEDURE CAN API Lab – 2L02I Page 8 of 14 Step 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 . LAB PROCEDURE CAN API Lab – 2L02I Page 9 of 14 4 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. . LAB PROCEDURE CAN API Lab – 2L02I Page 10 of 14 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.) [...]... 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); Question Which CAN mailbox is set up to receive data? (You can change it.) Set the CAN ID to a value between 1 – 0xFF: Step 6.2 The can demo... 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 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... 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 CAN API Lab – 2L02I Page 13 of 14 LAB PROCEDURE 7 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 Using the CAN interrupts is a fast way... the fastest Step 5.3 Remove the transmit back-to-back code again by changing “#if 1” to “#if 0” CAN API Lab – 2L02I Page 11 of 14 LAB PROCEDURE 6 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... Breakpoints tab in the debug view to easily delete them all Step 7.3 Put two breakpoints in can_ int_demo 1) Where transmit and 2) receive flags are detected as being set 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 The transmit interrupt routine fires when a frame has been sent onto the CAN bus The interrupt...LAB PROCEDURE 5 Transmit frames back-to-back 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 Step 5.1 When sending multiple frames as fast as possible back-to-back,... 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 Step 7.5 Go ahead and check that this works Receive interrupt Step 7.6 Similar to transmit, check that receive works Question What... 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... 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 Question Which functions are used to receive a CAN frame? _ _ 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... 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 CAN API Lab – 2L02I Page . (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);. 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. LAB PROCEDURE CAN API Lab – 2L02I Page 1 of 14 CAN In A Day RX CAN API Lab 2L02I Description: Learn how easy it is to use CAN for your networked application by using the CAN API.

Ngày đăng: 22/06/2015, 14:04

Từ khóa liên quan

Mục lục

  • 1 Board, CAN bus, and CAN monitor setup.

    • 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.

    • 2 Launch e2studio and the CAN workspace.

      • Step 2.1 Launch e2studio.

      • Step 2.2 Workspace: When the workspace launch window pops up, select the workspace directory C:\Workspace\e2studio\CAN. (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: Select Project=>Build Project to build the target binary.

      • Step 2.5 Debug launch: Select Run=>Debug Configurations./

      • 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.

      • Step 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.

      • Click Reset. Then Step (F6) to see if you are 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 (don’t press the red square - that exits debug), Reset, and Go as shown in Step 2.10.

      • Step 2.14 If you came this far successfully, e2studio and debugging work! We can instead just focus on using the CAN API.

      • 3 Init CAN. Transmit from a CAN mailbox.

        • 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.

        • Scroll down to around line 200 and findapi_status = R_CAN_Create(g_can_channel);The while(1) forever loop is there to hinder you from proceeding in case something is wrong with CAN.

Tài liệu cùng người dùng

Tài liệu liên quan