Renesas RX600 Series Application Note
Renesas RX600 Series Application Note

Renesas RX600 Series Application Note

Can application programming interface
Hide thumbs Also See for RX600 Series:

Advertisement

Quick Links

APPLICATION NOTE
RX600 Series
R01AN0339EU0203
Rev. 2.03
CAN Application Programming Interface
Mar 23, 2013
Introduction
This application note introduces the Renesas CAN Application Programming Interface and explains how to use it to
send, receive, and monitor data on the CAN bus. It also explains briefly some features of the CAN peripheral.
Bundled with this application note comes the CAN API driver source code files config_r_can_rapi.h, r_can_api.h, and
r_can_api.c. Note that there are alternative ways to write the driver functions. For example you may want to write your
own driver functions to tailor a specific behavior.
The RX CAN peripheral has 32 CAN mailboxes to read from and write to in order to communicate over CAN. These
mailboxes are message 'buffers' and will hold the CAN data frame until it is overwritten by another incoming frame, or
rewritten by the application. Each mailbox can be configured dynamically to transmit or receive. Usually, most are
configured to receive and a few to transmit.
A CAN API is also available from Renesas for the CAN equipped MCUs in the families R32C, M32C, M16C, R8C, for
SH RCAN-ET MCUs SH7286, SH7137, and for the SH7216. Most, if not all, CAN MCUs are available on Renesas
Starter Kit boards. Demonstration source code for the API is available for all these devices and runs the same demo
across all of these devices so that they can be connected and run together.
The term 'mailbox', or in some literature 'message box' or 'message buffer' refers to the physical location where
messages are stored inside the MCU's CAN peripheral. In this document we will use the term 'mailbox'.
Target Device
RX600 Series of MCUs with CAN.
R01AN0339EU0203 Rev. 2.03
Page 1 of 29
Mar 23, 2013

Advertisement

Table of Contents
loading

Summary of Contents for Renesas RX600 Series

  • Page 1 A CAN API is also available from Renesas for the CAN equipped MCUs in the families R32C, M32C, M16C, R8C, for SH RCAN-ET MCUs SH7286, SH7137, and for the SH7216. Most, if not all, CAN MCUs are available on Renesas Starter Kit boards.
  • Page 2: Table Of Contents

    RX600 Series CAN Application Programming Interface Contents CAN Basics ............................3 The CAN Peripheral .......................... 3 Communication Layers ........................4 The Mailbox ............................4 Using Extended ID ..........................4 Adding the CAN API to Your Project ....................5 The CAN Config File ......................... 5 The CAN API .............................
  • Page 3: Can Basics

    RX600 Series CAN Application Programming Interface CAN Basics CAN was designed to provide reliable, error-free network communication for applications in which safety and real-time operation cannot be compromised. Its main attributes can be summarized as follows: • High reliability and noise immunity •...
  • Page 4: Communication Layers

    Figure 1. CAN physical and source code layers. In this document we will not discuss any higher level protocols such as CANopen or DeviceNet. (For some Renesas CAN MCUs there is a CANopen solution. Contact your sales representative.) The Mailbox When a CAN message is to be sent, it must first be written to a mailbox by the application firmware.
  • Page 5: Adding The Can Api To Your Project

    For example, there is the option of running in CAN polled mode or CAN interrupt mode. is the place where this is selected. config_r_can_rapi.h Do not change anything in the file, which contains the Renesas CAN API functions for your Renesas r_can_api.c MCU. Interrupts vs. Polling USE_CAN_POLL Include the macro USE_CAN_POLL if you want to poll CAN mailboxes for messages received and sent.
  • Page 6: The Can Api

    RX600 Series CAN Application Programming Interface CAN Bitrate Settings See API R_CAN_SetBitrate below and the file config_r_can_rapi.h. The CAN API The API is a set of functions that allow you to use CAN without having to commit attention to all the details of setting up the CAN peripheral, to be able to easily have your application communicate with other nodes on the network.
  • Page 7 RX600 Series CAN Application Programming Interface R_CAN_SW_WAKEUP_ERR The CAN peripheral did not wake up from Sleep mode. R_CAN_SW_SLEEP_ERR The CAN peripheral did enter Sleep mode. R_CAN_SW_HALT_ERR The CAN peripheral did not enter Halt mode. R_CAN_SW_RST_ERR The CAN peripheral did not enter Reset mode.
  • Page 8 RX600 Series CAN Application Programming Interface R_CAN_Create Initializes the CAN peripheral, sets bitrate, masks, mailbox defaults and configures CAN interrupts This function will by default invoke the rest of the initialization functions. It also sets the CAN interrupt levels. It will also call all other relevant set-up functions such as •...
  • Page 9 RX600 Series CAN Application Programming Interface R_CAN_PortSet Configures the MCU and transceiver port pins This function is responsible for configuring the MCU and transceiver port pins. Transceiver port pins such as Enable will vary depending on design, and this function must then be modified.
  • Page 10 RX600 Series CAN Application Programming Interface R_CAN_Control Set CAN operating modes Controls transition to CAN operating modes determined by the CAN Control register. For example, the Halt mode should be used to later configure a receive mailbox. Format uint32_t R_CAN_Control(...
  • Page 11 RX600 Series CAN Application Programming Interface R_CAN_SetBitrate Sets the CAN bitrate (communication speed) The baud rate and bit timing must always be set during the configuration process. It can be changed later on if reset mode is entered. Format void R_CAN_SetBitrate(const uint32_t ch_nr);...
  • Page 12 RX600 Series CAN Application Programming Interface R_CAN_TxSet and R_CAN_TxSetXid Set up a mailbox to transmit R_CAN_TxSet will write to a mailbox the specified ID, data length and data frame payload, then set the mailbox to transmit mode and send a frame onto the bus by calling R_CAN_Tx().
  • Page 13 RX600 Series CAN Application Programming Interface R_CAN_Tx Starts actual message transmission onto the CAN bus This API will wait until the mailbox finishes handling a prior frame, then set the mailbox to transmit mode. Format uint32_t R_CAN_Tx( const uint32_t ch_nr,...
  • Page 14 RX600 Series CAN Application Programming Interface R_CAN_TxCheck Check for successful data frame transmission. Use to check a mailbox for a successful data frame transmission. Format uint32_t R_CAN_TxCheck( const uint32_t ch_nr, const uint32_t mbox_nr Arguments ch_nr 0,1,2,3 Which CAN bus to use. 1-4 channels may be available.
  • Page 15 RX600 Series CAN Application Programming Interface R_CAN_TxStopMsg Stop a mailbox that has been asked to transmit a frame Format uint32_t R_CAN_TxStopMsg( const uint32_t ch_nr, const uint32_t mbox_nr Arguments ch_nr 0,1,2,3 Which CAN bus to use. 1-4 channels may be available.
  • Page 16 RX600 Series CAN Application Programming Interface R_CAN_RxSet and R_CAN_RxSetXid Set up a mailbox to receive R_CAN_RxSet The API sets up a given mailbox to receive data frames with the given CAN ID. Incoming data frames with the same ID will be stored in the mailbox.
  • Page 17 RX600 Series CAN Application Programming Interface R_CAN_RxPoll Checks if a mailbox has received a message Format uint32_t R_CAN_RxPoll( const uint32_t ch_nr, const uint32_t mbox_nr Arguments ch_nr 0,1,2,3 Which CAN bus to use. 1-4 channels may be available. mbox_nr 0-32 Which CAN mailbox to check.
  • Page 18 RX600 Series CAN Application Programming Interface R_CAN_RxRead Read the CAN data frame content from a mailbox The API checks if a given mailbox has received a message. If so, a copy of the mailbox’s dataframe will be written to the given structure.
  • Page 19 RX600 Series CAN Application Programming Interface R_CAN_RxSetMask Sets the CAN ID Acceptance Masks To accept only one ID, set mask to all ones. To accept all messages, set mask to all zeros. To accept a range of messages, set the corresponding ID bits to zero.
  • Page 20 RX600 Series CAN Application Programming Interface Fast filtering of messages with Acceptance Filter Support If you have used a mask to receive a broad range of message IDs, you must filter for the actual desired messages with firmware. To increase the speed of this search one may use the Acceptance Filter Support instead.
  • Page 21 RX600 Series CAN Application Programming Interface R_CAN_CheckErr Check for bus errors The API checks the CAN status, or Error State, of the CAN peripheral. Format uint32_t R_CAN_CheckErr(const uint32_t ch_nr); Parameters ch_nr 0,1,2,3 Which CAN bus to use. 1-4 channels may be available.
  • Page 22 RX600 Series CAN Application Programming Interface Error Active Normal TEC or REC < 128 11 consecutive recessive bits detected 128 times TEC or REC > 127 Bus Off Error Passive No comm- No Errors sent TEC > 255 unication Figure 4. CAN Error States.
  • Page 23 RX600 Series CAN Application Programming Interface Figure 5. Handling recovery from Bus Off for the application. (The MCU detects recovery of the bus on its own.) A node will automatically resume the normal Error Active state again after seeing 11 consecutive recessive bits on the bus 128 times.
  • Page 24 RX600 Series CAN Application Programming Interface /* Tell user if CAN bus status changed. All Status bits are read only. */ if (error_bus_status[ch_nr] != error_bus_status_prev[ch_nr]) switch (error_bus_status[ch_nr]) /* Error Active. */ case R_CAN_STATUS_ERROR_ACTIVE: /* Only report if there was a previous error. */ if (error_bus_status_prev[ch_nr] >...
  • Page 25: Can Interrupt Checklist

    RX600 Series CAN Application Programming Interface The CAN error interrupt can be used to check the error state of the node, although polling with the API regularly is usually sufficient since low level error handling is done by the peripheral.
  • Page 26 RX600 Series CAN Application Programming Interface Figure 6. CAN Internal Loopback mode. This lets you test the functionality of a node without having a CAN bus connected. Internal Loopback can be convenient when testing as this mode allows the CAN controller to run without sending CAN errors due to no Acks received when the node is alone on the bus, it acknowledges transmitted frames itself.
  • Page 27 RX600 Series CAN Application Programming Interface Figure 8. A node in Listen Only mode will not acknowledge messages or send Error frames etc Listen Only is useful for bringing up a new node that has been added to an existing CAN bus. The mode can be used for a recently connected node’s application to ensure that frames have properly been received before going live.
  • Page 28: Time Stamp

    RX600 Series CAN Application Programming Interface 11. Time Stamp The timestamp function captures the value of the on-chip time stamp to a mailbox when a message is received. By examining the time stamp you can for example determine the sequence of messages if they are spread out over multiple receive mailboxes to determine the order of the messages.
  • Page 29: Website And Support

    RX600 Series CAN Application Programming Interface Website and Support Renesas Electronics Website http://www.renesas.com/ Inquiries http://www.renesas.com/inquiry All trademarks and registered trademarks are the property of their respective owners. R01AN0339EU0203 Rev. 2.03 Page 29 of 29 Mar 23, 2013...
  • Page 30: Revision Record

    Changed CAPI_CFG_CAN_ISR to CAPI_CFG_CANx_ISR. Added API return code "R_CAN_NOT_OK". Changed “config_r_can_rap.h” to “config_r_can_rapi.h”. 2.00 Jan 10, 2011 Changed Properties to RX600 Series Added Extended CAN. RX63N workspace added. Macros to be discontinued going forward (not needed): - Changed text under USE_CAN_API_SEARCH - Removed CAPI_CFG_CANx_ISR 2.01...
  • Page 31: General Precautions In The Handling Of Mpu/Mcu Products

    General Precautions in the Handling of MPU/MCU Products The following usage notes are applicable to all MPU/MCU products from Renesas. For detailed usage notes on the products covered by this document, refer to the relevant sections of the document as well as any technical updates that have been issued for the products.
  • Page 32 10. It is the responsibility of the buyer or distributor of Renesas Electronics products, who distributes, disposes of, or otherwise places the product with a third party, to notify such third party in advance of the contents and conditions set forth in this document, Renesas Electronics assumes no responsibility for any losses incurred by you or third parties as a result of unauthorized use of Renesas Electronics products.

Table of Contents