Renesas Electronics America Inc. © 2010 Renesas Electronics America Inc. All rights reserved. ID 230L: Simplify your Networked Application with CAN and the CAN API RX CAN Lab Carl Stenquist, C&I Staff Applications Engineer 13 October 2010 Ver. 1.0
2 © 2010 Renesas Electronics America Inc. All rights reserved. Mr. Carl Stenquist Staff Applications Engineer Wrote CAN API for R8C, M16C, R32C, SH, and RX. Design CAN Development Kit. Support/manage CAN middleware. PREVIOUS EXPERIENCE CAN truck electronics, Stoneridge Electronics, Sweden. 5 years designing and supporting CAN firmware applications for Renesas MCUs. MScEE, Chalmers University of Technology, Sweden.
3 © 2010 Renesas Electronics America Inc. All rights reserved. Renesas Technology and Solution Portfolio Microcontrollers & Microprocessors #1 Market share worldwide * Analog and Power Devices #1 Market share in low-voltage MOSFET** Solutions for Innovation ASIC, ASSP & Memory Advanced and proven technologies * MCU: 31% revenue basis from Gartner "Semiconductor Applications Worldwide Annual Market Share: Database" 25 March 2010 **Power MOSFET: 17.1% on unit basis from Marketing Eye 2009 (17.1% on unit basis).
4 © 2010 Renesas Electronics America Inc. All rights reserved. 4 Renesas Technology and Solution Portfolio Microcontrollers & Microprocessors #1 Market share worldwide * Analog and Power Devices #1 Market share in low-voltage MOSFET** ASIC, ASSP & Memory Advanced and proven technologies * MCU: 31% revenue basis from Gartner "Semiconductor Applications Worldwide Annual Market Share: Database" 25 March 2010 **Power MOSFET: 17.1% on unit basis from Marketing Eye 2009 (17.1% on unit basis). Solutions for Innovation
5 © 2010 Renesas Electronics America Inc. All rights reserved. 5 Microcontroller and Microprocessor Line-up Superscalar, MMU, Multimedia Up to 1200 DMIPS, 45, 65 & 90nm process Video and audio processing on Linux Server, Industrial & Automotive Up to 500 DMIPS, 150 & 90nm process 600uA/MHz, 1.5 uA standby Medical, Automotive & Industrial Legacy Cores Next-generation migration to RX High Performance CPU, FPU, DSC Embedded Security Up to 10 DMIPS, 130nm process 350 uA/MHz, 1uA standby Capacitive touch Up to 25 DMIPS, 150nm process 190 uA/MHz, 0.3uA standby Application-specific integration Up to 25 DMIPS, 180, 90nm process 1mA/MHz, 100uA standby Crypto engine, Hardware security Up to 165 DMIPS, 90nm process 500uA/MHz, 2.5 uA standby Ethernet, CAN, USB, Motor Control, TFT Display High Performance CPU, Low Power Ultra Low Power General Purpose
6 © 2010 Renesas Electronics America Inc. All rights reserved. 6 Microcontroller and Microprocessor Line-up Superscalar, MMU, Multimedia Up to 1200 DMIPS, 45, 65 & 90nm process Video and audio processing on Linux Server, Industrial & Automotive Up to 500 DMIPS, 150 & 90nm process 600uA/MHz, 1.5 uA standby Medical, Automotive & Industrial Legacy Cores Next-generation migration to RX High Performance CPU, FPU, DSC Embedded Security Up to 10 DMIPS, 130nm process 350 uA/MHz, 1uA standby Capacitive touch Up to 25 DMIPS, 150nm process 190 uA/MHz, 0.3uA standby Application-specific integration Up to 25 DMIPS, 180, 90nm process 1mA/MHz, 100uA standby Crypto engine, Hardware security Up to 165 DMIPS, 90nm process 500uA/MHz, 2.5 uA standby Ethernet, CAN, USB, Motor Control, TFT Display High Performance CPU, Low Power Ultra Low Power General Purpose CAN MCU Solutions R8C/R32C/SH/RX
7 © 2010 Renesas Electronics America Inc. All rights reserved. CAN – Continuously Moving into New Fields Machines (knitting) Machines (knitting) Agriculture Agriculture Control Electronics Control Electronics Factory Assembly Lines Factory Assembly Lines Medical Systems Medical Systems Railway Electronics Railway Electronics Marine Electronics Marine Electronics Elevators Elevators Building Automation Building Automation Printing, Copying Printing, Copying
8 © 2010 Renesas Electronics America Inc. All rights reserved. Communication Made Simple Use CAN API, which uses the +CAN peripheral, which handles low level details = Fast implementation! Position
9 © 2010 Renesas Electronics America Inc. All rights reserved. Agenda The CAN Dataframe The CAN Mailbox The CAN API Polling vs. Interrupts Transmit Dataframes Receive Dataframes Error Handling Lab
10 © 2010 Renesas Electronics America Inc. All rights reserved. Key Takeaways Know how to… Initialize the CAN module Transmit from a CAN mailbox Receive data to a mailbox Use a CAN Monitor to view and send dataframes See how easy it is to use CAN Focus on your application!
© 2010 Renesas Electronics America Inc. All rights reserved. 11 Lowest Levels Dataframe and Mailbox
12 © 2010 Renesas Electronics America Inc. All rights reserved. ID extend 1 Rem Req 1 I F S 3+ Data (Bytes) 0-8 bytes C R C 15 ACK1ACK1 SOF1SOF1 Identifier 11 Control 6 The Dataframe Data (Bytes) 0-8 bytes Identifier 11 Control 6 “Standard” 11-bit ID Dataframe Application software only reads/writes CAN ID Data Length Code (in Control Field) Data Field
13 © 2010 Renesas Electronics America Inc. All rights reserved. RX has 32 mailboxes/peripheral RX/62N has 64 mailboxes to use on one bus. One mailbox handles one complete Dataframe (0-8 bytes) More mailboxes SW design easier More SW design flexibility Less runtime reconfiguring Frame resides in Mailbox Application bus point of interaction Application reads & writes Mailbox A.k.a. “Message Box”, “Buffer”, “Slot”
14 © 2010 Renesas Electronics America Inc. All rights reserved. Managing a Mailbox HW takes care of rest Tx mailbox: HW takes care of low level transmit details Rx mailbox: HW captures messages with the set ID Mailboxes can be reconfigured at runtime Transmit Write All app needs to do! Field ID DLC Data Receive Setup mailbox Write - - Receive Get message Read
15 © 2010 Renesas Electronics America Inc. All rights reserved. CAN Firmware & Peripheral Interaction Peripheral Firmware Node MCU Mask ID=5,Data=,RXID=5,Data=123,TX Config. A mailbox to receive ID=5 Config. A mailbox to send a frame with ID=5 Mailboxes Transceiver 123…
16 © 2010 Renesas Electronics America Inc. All rights reserved. 1.CAN ID 2.Data Length Code 3.Data Field 4.All above Which of these do you need to specify to setup a mailbox to receive? 1.CAN ID 2.Data Length Code 3.Data Field 4.All above QUESTION Which of these do you need in order to setup a mailbox to transmit?
17 © 2010 Renesas Electronics America Inc. All rights reserved. Messages are broadcast, and then received by all CAN controllers on the bus. 1.No, only to those mailboxes that have a CAN mailbox set up to receive that ID. 2.Yes, since they must do a CRC check, but the message is only copied to a CAN mailbox if there is one set up to receive that ID. QUESTION Messages are broadcast and received by all CAN controllers on the bus, they all do a CRC check, then check If any mailbox is configured to receive that ID!
© 2010 Renesas Electronics America Inc. All rights reserved. 18 Application Level The CAN API
19 © 2010 Renesas Electronics America Inc. All rights reserved. Let ’ s move up the layers. Application interfaces with CAN API only No bother with low level details. Layers (Where are we?) Optional industrial protocol
20 © 2010 Renesas Electronics America Inc. All rights reserved. Dealing with the CAN Registers… …can be tedious. Sure would be nice to have… CAN Initialization A CAN API! START Enter CAN reset/ initialization mode CAN reset mode? NO YES Enable CAN ports Set CAN control register - Loopback mode select bit - Message order select bit - Basic CAN mode select bit - Bus error interrupt enable bit Set CAN bit timing and baud rate Set mask register Go to CAN operation mode CAN operation mode? NO END YES Exit CAN sleep mode R_CAN_Create() The CAN API
21 © 2010 Renesas Electronics America Inc. All rights reserved. The CAN API Initialization, Port and Peripheral Control R_CAN_Create (ch_nr); R_CAN_SetBitrate (ch_nr); R_CAN_PortSet (ch_nr, action_type); [action_type = ENABLE, DISABLE ] R_CAN_Control (ch_nr, action_type); [action_type = ENTERSLEEP_CANMODE, EXITSLEEP_CANMODE, RESET_CANMODE, HALT_CANMODE, OPERATE_CANMODE, CANPORT_TEST_LISTEN_ONLY, CANPORT_TEST_0_EXT_LOOPBACK, CANPORT_TEST_1_INT_LOOPBACK, CANPORT_RETURN_TO_NORMAL ] Send R_CAN_TxSet (ch_nr, mbox_nr, frame_p, frame_type); R_CAN_Tx (ch_nr, mbox_nr); R_CAN_TxCheck (ch_nr, mbox_nr); R_CAN_TxStopMsg (ch_nr, mbox_nr); Receive R_CAN_RxSet (ch_nr, mbox_nr, stid, frame_type); R_CAN_RxRead (ch_nr, mbox_nr, frame_p); R_CAN_RxPoll (ch_nr, mbox_nr, frame_p); R_CAN_RxSetMask (ch_nr, sid_mask_value, mask_reg_nr); Error Check R_CAN_CheckErr (ch_nr); The CAN API
22 © 2010 Renesas Electronics America Inc. All rights reserved. Polling When data needed – Lowest priority; no interrupts When only interested in data at certain points in time - No hurry to get data Interrupts For processing data as soon as it arrives When processing messages in certain order Polling vs. Interrupts The CAN API
23 © 2010 Renesas Electronics America Inc. All rights reserved. Transmitting A Frame struct can_std_data_s { uint16 id; uint8 dlc; uint8 data[8]; }; R_CAN_TxSet (0, mbox_nr, &my_tx_frame, DATA_FRAME); my_tx_frame.id = 0x700; my_tx_frame.dlc = 2; my_tx_frame.data[0] = 0x11; my_tx_frame.data[1] = 0x22; struct can_std_frame_t my_tx_frame; The API function Waits for previous frame to finish transmit Clears message control register Disables interrupts for mailbox Sets CAN ID Sets DLC Sets transmit data Transmits Dataframe The CAN API
24 © 2010 Renesas Electronics America Inc. All rights reserved. Transmit Verification Not necessary...unless application must know when message sent CAN peripheral will deliver unless Bus Off (Just check for Error Passive) Do not define USE_CAN_POLL in config_r_can_rapi.h Successful Dataframe transmit triggers ISR CAN0_TXM0_ISR() Flag the application that data has been sent! Polling Define USE_CAN_POLL in config_r_can_rapi.h Check if data sent by calling R_CAN_TxCheck (ch_nr, mbox_nr); If returns API_OK, message was sent! Transmit interrupt
25 © 2010 Renesas Electronics America Inc. All rights reserved. A CAN mailbox configured with an ID can later be reused to handle other IDs? True, a mailbox can be reconfigured at runtime QUESTION Can a mailbox be reconfigured from transmit to receive? Again, yes! A mailbox can be reconfigured at runtime.
26 © 2010 Renesas Electronics America Inc. All rights reserved. Configure Mailbox to Receive The API Waits for any previous transmission/reception to complete Interrupt disables the mailbox Clears mailbox control register Sets Standard ID for selected mailbox Sets Dataframe/Remote frame Sets receive interrupt enabled unless USE_CAN_POLL defined R_CAN_RxSet(ch_nr, mbox_nr, stid, frame_type); The CAN API
27 © 2010 Renesas Electronics America Inc. All rights reserved. Polling for Received Data How to receive Dataframe in application At System Setup Uncomment USE_CAN_POLL in config_r_can_rapi.h: #define USE_CAN_POLL 1 When Polling R_CAN_RxPoll(ch_nr, mbox_nr, frame_p); If returns API_OK, mailbox has new message, call R_CAN_RxRead (ch_nr, mbox_nr, frame_p); Dataframe is copied to address frame_p.
28 © 2010 Renesas Electronics America Inc. All rights reserved. Using the Receive Interrupt How to receive Dataframe in application At System Setup Comment USE_CAN_POLL in config_r_can_rapi.h: //#define USE_CAN_POLL 1 When frame arrives with ID set by API the CAN Receive ISR triggers Check which mailbox caused the interrupt with R_CAN_RxPoll (ch_nr, mbox_nr, frame_p); If new frame, call R_CAN_RxRead (ch_nr, mbox_nr, frame_p); which copies the data to frame_p Set a flag to tell the main application that data has been received
29 © 2010 Renesas Electronics America Inc. All rights reserved. Messages are broadcast, and then received by all CAN controllers on the bus. 1.No, only to nodes with a mailbox set to receive that ID. 2.Yes, since they must do a CRC check. The message is only copied to a CAN mailbox if there is one set up to receive that ID. QUESTION Messages are broadcast and received by all CAN controllers on the bus, they all do a CRC check, then check If any mailbox is configured to receive that ID!
30 © 2010 Renesas Electronics America Inc. All rights reserved. Dice Pictures for you to use We will have dice at every station. In your presentation, instruct the attendee to roll the dice to a spot when they complete a section. Here are the pictures for your use.
31 © 2010 Renesas Electronics America Inc. All rights reserved. Checking Progress We are using the die to keep track of where everyone is in the lab. Make sure to update it as you change sections. When done with the lab, your die will have the 6 pointing up as shown here.
32 © 2010 Renesas Electronics America Inc. All rights reserved. CAN Lab Setup
33 © 2010 Renesas Electronics America Inc. All rights reserved. Questions?
© 2010 Renesas Electronics America Inc. All rights reserved. 34 Thank You
© 2010 Renesas Electronics America Inc. All rights reserved. 35 Appendix
36 © 2010 Renesas Electronics America Inc. All rights reserved. Your CAN Application - Keep in Mind No real addresses Messages broadcast to whomever happens to listen! Mailbox ID can be reconfigured anytime Flexible data flow Subdivide ID field for higher level functionality 11-bit ID split into “user protocol” fields Group priority (critical, info,..) Function ID (temp sensor, motor,..) Device address You CAN use!
37 © 2010 Renesas Electronics America Inc. All rights reserved. Error Handling Check node state regularly with int8_t R_CAN_CheckErr(void) Returns STATE_ERROR (Error Active): < 127 CAN transmit and receive errors occurred, OK! STATE_ERROR_PASSIVE: Over 127 errors occurred – warn user. STATE_BUSOFF:Node will not transmit until it recovers. Pause application, notify user that node is not working. Reinitialize CAN and reset mailboxes when node recovers to Error Active. The CAN API
38 © 2010 Renesas Electronics America Inc. All rights reserved. Bus Off reached Peripheral recovered: Reinitialize CAN! Minimum Application Handling of Bus Off Normal application activity Poll if peripheral is in Bus Off Application can not send or receive System goes into error mode: Poll if peripheral recovers
39 © 2010 Renesas Electronics America Inc. All rights reserved. CAN-D-Kit CAN Development Kit “CAN-D-Kit” Two R32C/118 Renesas Starter Boards Systec CAN bus monitor Debugger Interface Sample projects and evaluation software
Renesas Electronics America Inc.