This example demonstrates how to transmit CAN messages over the CAN bus using the XMC™ MCU. This example can be used with the CAN receiver example running on another board.
Provide feedback on this code example.
- ModusToolbox™ software v3.0
- SEGGER J-Link software
- Programming language: C
- Associated parts: All XMC™ MCU parts
- GNU Arm® embedded compiler v10.3.1 (
GCC_ARM
) - Default value ofTOOLCHAIN
- Arm® compiler v6.16 (
ARM
) - IAR C/C++ compiler v9.30.1 (
IAR
)
- XMC1400 boot kit (
KIT_XMC14_BOOT_001
) - Default value ofTARGET
- XMC4200 Platform2Go kit (
KIT_XMC_PLT2GO_XMC4200
) - XMC4300 relax EtherCAT kit (
KIT_XMC43_RELAX_ECAT_V1
) - XMC4400 Platform2Go kit (
KIT_XMC_PLT2GO_XMC4400
) - XMC4500 relax kit (
KIT_XMC45_RELAX_V1
) - XMC4700 relax kit (
KIT_XMC47_RELAX_V1
) - XMC4800 relax EtherCAT kit (
KIT_XMC48_RELAX_ECAT_V1
)
This example uses the board's default configuration. See the kit user guide to ensure that the board is configured correctly.
Two XMC MCU boards are required to test this code example: one for the XMC™ MCU: CAN receiver application and the other for the XMC™ MCU: CAN transmitter application. See Supported kits.
Connect the CANH and CANL of one board to the CANH and CANL of the other board respectively to establish CAN communication between the transmitter and the receiver node. Also connect the ground pin of both boards together.
Table 1. CAN board connections
Development board | Signal name | Pin header in board |
---|---|---|
KIT_XMC14_BOOT_001 | CANH | J102.H |
CANL | J102.L | |
KIT_XMC47_RELAX_V1 | CANH | X2.33 |
CANL | X2.35 |
Notes:
-
The KIT_XMC_PLT2GO_XMC4200 and KIT_XMC43_RELAX_ECAT_V1 kits provide the CAN interface via a D-Sub DE-9 connector. Therefore, you might need a DBP connector or DB9 to RS232/ RS485 convertor board to connect these boards.
-
The KIT_XMC45_RELAX_V1 kit does not have an external CAN transceiver. Therefore, you need to connect an external CAN transceiver to the CAN Tx and Rx pins.
-
The CAN bus lines (signals CANH and CANL) of the on-board transceiver in the KIT_XMC47_RELAX_V1 and KIT_XMC_PLT2GO_XMC4400 kits are not terminated by a 120 Ω resistor and must be terminated externally. Add a 120 Ω termination resistor between the CANH and CANL pins for proper operation of CAN.
Figure 1. Hardware setup
This example requires no additional software or tools.
Create the project and open it using one of the following:
In Eclipse IDE for ModusToolbox™ software
-
Click the New Application link in the Quick Panel (or, use File > New > ModusToolbox™ Application). This launches the Project Creator tool.
-
Pick a kit supported by the code example from the list shown in the Project Creator - Choose Board Support Package (BSP) dialog.
When you select a supported kit, the example is reconfigured automatically to work with the kit. To work with a different supported kit later, use the Library Manager to choose the BSP for the supported kit. You can use the Library Manager to select or update the BSP and firmware libraries used in this application. To access the Library Manager, click the link from the Quick Panel.
You can also just start the application creation process again and select a different kit.
If you want to use the application for a kit not listed here, you may need to update the source files. If the kit does not have the required resources, the application may not work.
-
In the Project Creator - Select Application dialog, choose the example by enabling the checkbox.
-
(Optional) Change the suggested New Application Name.
-
The Application(s) Root Path defaults to the Eclipse workspace which is usually the desired location for the application. If you want to store the application in a different location, you can change the Application(s) Root Path value. Applications that share libraries should be in the same root path.
-
Click Create to complete the application creation process.
For more details, see the Eclipse IDE for ModusToolbox™ software user guide (locally available at {ModusToolbox™ software install directory}/docs_{version}/mt_ide_user_guide.pdf).
In command-line interface (CLI)
ModusToolbox™ software provides the Project Creator as both a GUI tool and the command line tool, "project-creator-cli". The CLI tool can be used to create applications from a CLI terminal or from within batch files or shell scripts. This tool is available in the {ModusToolbox™ software install directory}/tools_{version}/project-creator/ directory.
Use a CLI terminal to invoke the "project-creator-cli" tool. On Windows, use the command line "modus-shell" program provided in the ModusToolbox™ software installation instead of a standard Windows command-line application. This shell provides access to all ModusToolbox™ software tools. You can access it by typing modus-shell
in the search box in the Windows menu. In Linux and macOS, you can use any terminal application.
The "project-creator-cli" tool has the following arguments:
Argument | Description | Required/optional |
---|---|---|
--board-id |
Defined in the <id> field of the BSP manifest |
Required |
--app-id |
Defined in the <id> field of the CE manifest |
Required |
--target-dir |
Specify the directory in which the application is to be created if you prefer not to use the default current working directory | Optional |
--user-app-name |
Specify the name of the application if you prefer to have a name other than the example's default name | Optional |
The following example clones the "CAN transmitter" application with the desired name "CANTransmitter" configured for the KIT_XMC14_BOOT_001 BSP into the specified working directory, C:/mtb_projects:
project-creator-cli --board-id KIT_XMC14_BOOT_001 --app-id mtb-example-xmc-can-transmitter --user-app-name CANTransmitter --target-dir "C:/mtb_projects"
Note: The project-creator-cli tool uses the git clone
and make getlibs
commands to fetch the repository and import the required libraries. For details, see the "Project creator tools" section of the ModusToolbox™ software user guide (locally available at {ModusToolbox™ software install directory}/docs_{version}/mtb_user_guide.pdf).
To work with a different supported kit later, use the Library Manager to choose the BSP for the supported kit. You can invoke the Library Manager GUI tool from the terminal using make library-manager
command or use the Library Manager CLI tool "library-manager-cli" to change the BSP.
The "library-manager-cli" tool has the following arguments:
Argument | Description | Required/optional |
---|---|---|
--add-bsp-name |
Name of the BSP that should be added to the application | Required |
--set-active-bsp |
Name of the BSP that should be as active BSP for the application | Required |
--add-bsp-version |
Specify the version of the BSP that should be added to the application if you do not wish to use the latest from manifest | Optional |
--add-bsp-location |
Specify the location of the BSP (local/shared) if you prefer to add the BSP in a shared path | Optional |
Following example adds the KIT_XMC47_RELAX_V1 BSP to the already created application and makes it the active BSP for the app:
library-manager-cli --project "C:/mtb_projects/CANTransmitter" --add-bsp-name KIT_XMC47_RELAX_V1 --add-bsp-version "latest-v4.X" --add-bsp-location "local"
library-manager-cli --project "C:/mtb_projects/CANTransmitter" --set-active-bsp APP_KIT_XMC47_RELAX_V1
In third-party IDEs
Use one of the following options:
-
Use the standalone Project Creator tool:
-
Launch Project Creator from the Windows Start menu or from {ModusToolbox™ software install directory}/tools_{version}/project-creator/project-creator.exe.
-
In the initial Choose Board Support Package screen, select the BSP, and click Next.
-
In the Select Application screen, select the appropriate IDE from the Target IDE drop-down menu.
-
Click Create and follow the instructions printed in the bottom pane to import or open the exported project in the respective IDE.
-
-
Use command-line interface (CLI):
-
Follow the instructions from the In command-line interface (CLI) section to create the application.
-
Export the application to a supported IDE using the
make <ide>
command. -
Follow the instructions displayed in the terminal to create or import the application as an IDE project.
-
For a list of supported IDEs and more details, see the "Exporting to IDEs" section of the ModusToolbox™ software user guide (locally available at {ModusToolbox™ software install directory}/docs_{version}/mtb_user_guide.pdf).
-
Connect the board to your PC using a micro-USB cable through the debug USB connector.
-
Program the board using Eclipse IDE for ModusToolbox™ software:
-
Select the application project in the Project Explorer.
-
In the Quick Panel, scroll down, and click <Application Name> Program (JLink).
-
-
Connect the second board running the CAN receiver example as described in the Hardware setup section.
-
Confirm that the user LED toggles on the transmitter board, which confirms that the CAN frame is transmitted periodically.
-
Press the user button on the transmitter board and confirm that the user LED2 on the receiver board is updated accordingly.
Note: KIT_XMC14_BOOT_001 does not have an on-board user switch. Connect an external switch to pin P4.4 to test the example.
You can debug the example to step through the code. In the IDE, use the <Application Name> Debug (JLink) configuration in the Quick Panel. For more details, see the "Program and debug" section in the Eclipse IDE for ModusToolbox™ software user guide.
In this code example, the CAN Node 1 is linked with message object 0 (MO0), which is configured to transmit a single byte over the CAN bus. The status of the user button is sent over the CAN bus for every 100 ms. Successful transmission of the message is indicated by toggling the user LED.
Figure 2. Firmware flow
The project uses a custom design.modus file since the following settings were modified in the default design.modus file.
Figure 3. CAN RX pin settings
Figure 4. CAN TX pin settings
Figure 5. User button pin settings
Resources | Links |
---|---|
Code examples | Using ModusToolbox™ software on GitHub |
Device documentation | XMC1000 family datasheets XMC1000 family technical reference manuals XMC4000 family datasheets XMC4000 family technical reference manuals |
Development kits | XMC™ MCU eval boards |
Libraries on GitHub | mtb-xmclib-cat3 – XMC™ MCU peripheral library (XMCLib) |
Tools | Eclipse IDE for ModusToolbox™ software – ModusToolbox™ software is a collection of easy-to-use software and tools enabling rapid development with Infineon MCUs, covering applications from embedded sense and control to wireless and cloud-connected systems using AIROC™ Wi-Fi and Bluetooth® connectivity devices. |
Infineon provides a wealth of data at www.infineon.com to help you select the right device, and quickly and effectively integrate it into your design.
For XMC™ MCU devices, see 32-bit XMC™ Industrial microcontroller based on Arm® Cortex®-M.
Document title: CE232628 – XMC™ MCU: CAN transmitter
Version | Description of change |
---|---|
1.0.0 | New code example |
1.1.0 | Added support for new kits |
2.0.0 | Updated to support ModusToolbox™ software v3.0. This CE is not backward compatible with previous version of ModusToolbox™ software. |
2.1.0 | Added support for CAN personality |
All other trademarks or registered trademarks referenced herein are the property of their respective owners.
© 2022 Infineon Technologies AG
All Rights Reserved.
The information given in this document shall in no event be regarded as a guarantee of conditions or characteristics. With respect to any examples or hints given herein, any typical values stated herein and/or any information regarding the application of the device, Infineon Technologies hereby disclaims any and all warranties and liabilities of any kind, including without limitation, warranties of non-infringement of intellectual property rights of any third party.
For further information on technology, delivery terms and conditions and prices, please contact the nearest Infineon Technologies Office (www.infineon.com).
Due to technical requirements, components may contain dangerous substances. For information on the types in question, please contact the nearest Infineon Technologies Office.
Infineon Technologies components may be used in life-support devices or systems only with the express written approval of Infineon Technologies, if a failure of such components can reasonably be expected to cause the failure of that life-support device or system or to affect the safety or effectiveness of that device or system. Life support devices or systems are intended to be implanted in the human body or to support and/or maintain and sustain and/or protect human life. If they fail, it is reasonable to assume that the health of the user or other persons may be endangered.