This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

LINUXEZSDK-SITARA: How to use the CAN bus from C code in Linux?

Part Number: LINUXEZSDK-SITARA

Hi TI, 

I have a system with AM437x that has CAN bus.
I need to develop a C code to send and receive data through the CAN bus.

1-) With the Linux 'ls' command, how do I verify that the CAN interface is available on the Linux system?

2-) How can I use the CAN interface (open, send and receive data,...) from a C code.

3-) What library do I need?

4-) Is there any example available?

Thank you very much. 

Best regards. 

  • Hi,

    The best example I can suggest is to look at the CAN utils repo that is open source. Here is the link to the repo. This repo will have C code that shows how to access the CAN interfaces. I would recommend to look at cansend.c for an a detailed example. The CAN interfaces are accessed through network sockets like ethernet.

    Best Regards,

    Schuyler

  • Hi, 

    Sincerely, thank you very much!

    About the link you shared with me:
    I have checked the parameters of the cansend.c function.
    I am invoking the function cansend.c from linux with this command: sudo ./cansend.o can0 123#00
    But I get this response: 1: ./cansend.o: Syntax error: word unexpected (expecting ")")
    I have some error in the function parameters, but I don't know what I'm doing wrong.
    Question 1:
    Please, do you have any idea what I am doing wrong?

    I have also tried this code example: github.com/.../CAN-Examples
    I get this response:
    CAN Sockets Demo
    Write: Network is down
    The CAN bus is open (no devices are connected to the bus). But I am checking the bus with an oscilloscope and there is no data, I was expecting to see something on the oscilloscope.
    Question 2:
    If there are no devices connected on the BUS, and data is being sent, is it normal to observe no changes on the BUS?

    I checked this link which is for a different MPU, AM6442 can port - Processors forum - Processors - TI E2E support forums
    If I run the command: sudo ip -d -s link show can0
    I get this:
    2: can0: <NOARP,ECHO> mtu 16 qdisc noop state DOWN mode DEFAULT group default qlen 10
    link/can promiscuity 0 minmtu 0 maxmtu 0
    can state STOPPED (berr-counter tx 0 rx 0) restart-ms 0
    bitrate 1000000 sample-point 0.750
    tq 83 prop-seg 4 phase-seg1 4 phase-seg2 3 sjw 1
    c_can: tseg1 2..16 tseg2 1..8 sjw 1..4 brp 1..1024 brp-inc 1
    clock 24000000
    re-started bus-errors arbit-lost error-warn error-pass bus-off
    0 0 0 0 0 0 numtxqueues 1 gso_max_size 65536 gso_max_segs 65535
    RX: bytes packets errors dropped overrun mcast
    0 0 0 0 0 0
    TX: bytes packets errors dropped carrier collsns
    0 0 0 0 0 0

    If I run the command: sudo ip link set can0 type can bitrate 1000000 dbitrate 4000000 fd on
    I get this: RTNETLINK answers: Operation not permitted
    If I run the command: sudo ip link set can0 up
    I get this: RTNETLINK answers: Operation not permitted
    Question 3:
    Is this result normal?

    Thanks very much. I wish tou a happy day. 

    Kinds regards. 

  • Hi,

    Apologies for delay reply. I am asumming that you are using a TI file system, the sudo command is not provided in the TI filesystem and is not necessary.

    To send data on the CANBUS you will need to have a CAN device attached. This could also be a CANBUS analyzer that provide the termination necessary so electrical signals work which will allow messages are to be sent on the CANBUS. The CANBUS ip of the SOC has to think there is a device attached to send messages.

    Best Regards,

    Schuyler