CAN Setup
1. Connect CAN Hardware
-
Using Onboard CAN (if available):
- For JetCore boards with onboard CAN, you just need to connect the CAN_H and CAN_L lines to your CAN network.
-
Using an External CAN Transceiver (e.g., MCP2515):
- Connect the CAN_H and CAN_L lines from the external transceiver to your CAN network.
- Connect the SPI pins on JetCore to the MCP2515, typically as follows:
- SCK to SCK on the transceiver.
- MOSI to MOSI on the transceiver.
- MISO to MISO on the transceiver.
- CS (Chip Select) to a free GPIO pin (e.g., GPIO 8).
-
Termination:
- Use a 120-ohm termination resistor at each end of the CAN bus to ensure signal integrity, especially for longer runs.
2. Enable CAN Interface
-
Check if CAN is already enabled:
Check if the CAN interface is already present on your system by running:ifconfig -a
- You should see a can0, can1, or similar interface if the device is recognized.
-
Enable the CAN interface (if using SPI transceiver):
- Enable the SPI interface by modifying the Jetson's device tree settings or loading the kernel modules.
For MCP2515, load the appropriate modules:
sudo modprobe can
sudo modprobe can_raw
sudo modprobe mcp251x
sudo modprobe spi_bcm2835 -
Create the CAN interface:
After loading the necessary modules, bring up the CAN interface with:sudo ip link set can0 up type can bitrate 500000
- Replace can0 with the appropriate interface name and set the bitrate to your desired CAN bus speed (e.g., 500 Kbps).
-
Verify CAN interface:
Verify the CAN interface is up and running:ifconfig can0
- You should see the CAN interface listed with the status "UP".
3. Test the CAN Interface
-
Send CAN messages:
You can usecansend
(part of the can-utils package) to send test CAN messages:cansend can0 123#deadbeef
- This sends a CAN message with ID 123 and data deadbeef.
-
Listen for CAN messages:
Use candump to listen for messages on the CAN bus:candump can0
- This will show all incoming CAN messages on the
can0
interface.
- This will show all incoming CAN messages on the
-
Use other can-utils tools for advanced CAN operations, such as:
canplayer
: To replay CAN traffic from a log file.cansend
: To send specific CAN frames.
-
Use CAN in Your Application
Python: You can use the python-can library to interact with CAN from Python:
pip install python-can
Example Python code to send a CAN message:
import can
bus = can.interface.Bus(channel='can0', bustype='socketcan')
message = can.Message(arbitration_id=0x123, data=[0xDE, 0xAD, 0xBE, 0xEF], is_extended_id=False)
bus.send(message)
- C++: Use the
socketcan
interface in C++ to communicate with the CAN bus.
-
Troubleshooting
-
CAN interface not detected:
- Check if the mcp251x module is loaded properly by running lsmod | grep mcp251x.
- Verify wiring and connections for SPI or onboard CAN.
-
No messages are being sent/received:
- Check the CAN network for proper termination resistors.
- Use candump to ensure that the messages are being received.
- Make sure the CAN bus speed (bitrate) is correctly configured.
-