CAN trough SPI ?

Rotule
2014-02-13
2022-07-12
1 2 3 > >> (Page 1 of 3)
  • Rotule - 2014-02-13

    Is there(or will there ever be?) any mean to use CAN with an expansion board like this one : http://www.industrialberry.com/can-controller/

    with the raspberry pi and CodeSys?

    I think this would be a pretty awesome feature

    Also is there any mean to programm custom SPI libraries(i'm thinking of making an ADC board and use analog inputs in codesys logic)

    THanks for this great product!

     
  • maauto - 2014-02-19

    I suggest to the developers to seriously think about making CAN/CANOpen supported in some way or another too.
    Both for educational purposes (for the "Teaching" part in "Raspberry Pi Teaching Platform") and for purposes of actually using this in automotive and other fields, where CAN slaves are either already present as a standard or cheaply available.

    ProfiBus/Profinet solutions on the other hand tend to be rather expensive (compared to CANOpen counterparts) due to licensing etc. At least in my personal experience.

     
  • Rotule - 2014-02-19

    And its also easy to build your own messages to communicate on a higher layer protocol (thinking of J1939 and CANopen(if just sharing PDO) )

    I played a little bit with codesys 3, i use Codesys 2.5 at work everyday, i think the J1939 Library seems pretty awesome, it gives you the latitude to configure custom messages wich is good, but could we get to lower layer with the pi and just send and recieve raw CAN messages?

     
  • Rotule - 2014-08-03

    While working woth codesys 3.5 on the new ECU I'm using at work, I understood that if the CAN driver is integrated at the system level you can use it directly in codesys with the low-level coding.

     
  • eschwellinger

    eschwellinger - 2014-08-03

    Hi,
    if anyone manage to build his own Linux kernel with socketcan support,
    this will work with the 3S runtime. The needed parts in the runtime are in.
    And if you have socketcan running, even J1939 will work out of the box, everything needed is on board in CODESYS IDE

    BR
    Edwin

     
  • eschwellinger

    eschwellinger - 2014-08-29

    Hi,
    Thanks for reporting this,
    could you give me the messages from the plc log?
    Regards
    Edwin

     
  • jzhvymetal - 2014-08-29

    Edwin,

    As requested here are the attached log files. To give you more info I am still using demo version codesyscontrol_V3.5.4.0.deb. I also noticed I was not loading all the modules required for SocketCan. Please notice I updated the steps up above along with the Dmesg output screenshot. Also I do not load the codesyscontrol service until all modules are loaded. Let me know if you need any further information.

     
  • eschwellinger

    eschwellinger - 2014-08-29

    Hi,
    please use the 3.5.5.0 version of the runtime, then it might work, to be honest I did test it but I will do it.
    Best regards
    Edwin

     
  • jzhvymetal - 2014-08-30

    Same problem using CODESYS_3.5SP5Patch1_Release and Raspberry Pi CoDeSys Control Version: 2.0.0.0 aka codesyscontrol_arm_raspberry_V3.5.5.0.deb. See Attached PLClogs. FYI I know cansocket is working because I can use candump and monitor traffic off a PLC with CanOpen master.

    Do you have to add any additional components under ComponentManager section in CODESYSControl.cfg?

    PlcLog 3.5.5.0.xml [22.59 KiB]

    codesyscontrol.log.V3.5.5.0.txt [16.18 KiB]

     
  • eschwellinger

    eschwellinger - 2014-09-02

    Hi,
    you need to add the Component 2-5 into
    /etc/CODESYSControl.cfg

    [ComponentManager]
    Component.1=CmpOpenSSL
    Component.2=CmpCAACanL2
    Component.3=CmpCAASdoServer
    Component.4=CmpCAASdoClient
    Component.5=CmpSocketCanDrv

    If you have a running SocketCAN implementation in your Linux Kernel, this should work.
    (This is not the case in the delivered standard wheezy image which comes with the store download!)

    Regards
    Edwin

     
  • jzhvymetal - 2014-09-03

    Edwin,

    Now I can download to the PI without error but Canbus still does not work. See attached PLClog. It gives Script: rts_set_baud.sh not found- can 0 will not work.

    With CanSocket I bring up the Canbus and match the bus speed used in the Codesys program. This is the command I use to bring up the adapter:

    ip link set can0 up type can bitrate 250000

    Also I have disable the Codesyscontrol service on boot up. If I don't CanSocket will not load because Codesyscontrol locks control. I believe is caused by Codesyscontrol locking out SPI driver. If I load CanSocket first then start Codesyscontrol it will let me download but as above Can still is not working.

    IMG: CanError.jpg

    PlcLog.xml.txt [17.36 KiB]

     
  • eschwellinger

    eschwellinger - 2014-09-03

    Hi,

    beside the problem with the baudrate script, which we could workaround with using the same baudrate as you set with ip link set can0 up type can bitrate 250000.

    This line with the missing license file may lead to the read triangle in the device tree on the left side and maybe to an
    not working CAN.
    How do you start the plc manually it seems that the runtime does not find the licensefile?

    BR
    Edwin

     
  • jzhvymetal - 2014-09-03

    Edwin,

    I am still using the demo version. I can purchase a license if required. Right now I am playing with different images and different PI's. My plan was to buy a license once I get Canbus working. This way I do not have keep moving the license when using a new image or PI.

    I have to done the following:

    #1 Disable codesyscontrol at boot up
    #This was required because CanSocket does not want to load if codesyscontrol is first loaded
    sudo update-rc.d codesyscontrol disable
    reboot

    #2 Load all required drivers and start CanSocket
    modprobe spi-config devices=bus=0:cs=0:modalias=mcp2515:speed=10000000:gpioirq=17:pd=20:pds32-0=10000000:pdu32-4=0x2002:force_release
    modprobe spi-bcm2708
    modprobe can
    modprobe can-dev
    modprobe can-raw
    modprobe can-bcm
    modprobe mcp251x
    ip link set can0 up type can bitrate 250000

    #3 Start codesyscontrol
    service codesyscontrol start

    #4 From windows download the codesys program with Canbus->Canopen Manager

    #5 Start PLC from windows codesys

     
  • eschwellinger

    eschwellinger - 2014-09-03

    Hi,

    no, you do not need to purchase a license, but please use 3.5.5.0 because then you have the same
    version that I use and in my case I do not see the error in the log: license file missing - License File missing -> think fieldbus will not work
    even not in Demo mode.

    The limitation of the demo pi runtime is:
    All fieldbus feature are running but the runtime quits after 2h of working in full mode.
    So for sure you do not need to buy a license for our tests.

    BR
    Edwin

    IMG: Licensefilemissing.jpg

     
  • jzhvymetal - 2014-09-03

    Edwin,

    The error in the log: license file missing - License File missing only happens when I modified the /etc/CODESYSControl.cfg with the CanBus lines. If take out the CanBus in the codesys program and I comment out the lines below and restart the codesyscontrol service it does not have an error. See attached screenshot.

    #/etc/CODESYSControl.cfg CanBus commented out
    [ComponentManager]
    Component.1=CmpOpenSSL
    ;Component.2=CmpCAACanL2
    ;Component.3=CmpCAASdoServer
    ;Component.4=CmpCAASdoClient
    ;Component.5=CmpSocketCanDrv

    IMG: CoDeSys license file missing .jpg

     
  • eschwellinger

    eschwellinger - 2014-09-03

    Hi,

    I need to reproduce this with real CAN hardware, at the moment I have only a Pi
    without PiCan. Give me a few day's to investigate it.

    BR
    Edwin

     
  • jzhvymetal - 2014-09-03

    Edwin,

    Thanks. Let me know if you need remote access to my PI.

    Also if the creator of the pre-compiled modules would also compile the virtual driver, true hardware would not be required for testing. I did not have the time to set up a cross compiler to compile the modules myself. I will try to e-mail the creator if he would also include the virtual driver.

    jzhvymetal

     
  • eschwellinger

    eschwellinger - 2014-09-10

    Hi jzhvymetal,

    I've tested this now,
    to be honest not with MCP2515 (because my hardware seems to be somehow broken)
    I've used Peak CAN Dongle but this makes no difference both are based on socketcan.

    How does it work:
    You need to copy the rts_set_baud.sh to the /root/ directory and make it executeable by chmod +x rts_set_baud.sh

    #!/bin/sh
    CAN_NUM=$(echo $1 | sed 's,^can([0-9]).,\1,')
    ./pcan /dev/pcanusb$CAN_NUM $2

    (In case of MCP2515 check if you need to modify it slightly)

    For other users reading this post:
    For using peak usb can or MCP2515 CAN interface it is needed to compile the kernel and enable CAN and the modules/interfaces which you want to use. This is the missing part - but we could not add this to the standard store image - because
    there is a lot of effort to test it and the experience till now was that on high load you might loose CAN Messages.

    BR
    Edwin

    IMG: CAN_on_Raspi.jpg

    rts_set_baud.zip [211 Bytes]

     
  • eschwellinger

    eschwellinger - 2014-09-16

    Hi jzhvymetal,

    now additional for others and you the description how it works with MCP2515 PICan interface.
    @jzhvymetal skip the parts with the kernel update.. you need only the scripts to set the baud and the start the runtime after loading the can modules.
    http://skpang.co.uk/catalog/pican-canbu ... -1196.html

    I have used the how to on that link:
    http://www.raspberrypi.org/forums/viewt ... &start=550

    Summerize:

    Write following commands in terminal:
    sudo rpi-update bfd37013c701f10c6df585346ee1d3d3bdf88b3b
    #reboot

    cd /tmp; wget http://lnxpps.de/rpie/rpi-can-3.12.22+.tar.bz2
    cd /; sudo tar jxvf /tmp/rpi-can-3.12.22+.tar.bz2
    sudo depmod -a
    sudo reboot

    sudo modprobe spi-config devices=bus=0:cs=0:modalias=mcp2515:speed=10000000:gpioirq=25:pd=20:pds32-0=16000000:pdu32-4=0x2002:force_release
    sudo modprobe mcp251x
    sudo ip link set can0 up type can bitrate 250000
    sudo ip -s -d link show can0

    At this point CAN works now (under Linux Wheezy)

    What is additional needed to run it in the CODESYS runtime:

    add in
    /etc/CODESYSControl.cfg

    [ComponentManager]
    Component.1=CmpOpenSSL
    Component.2=CmpCAACanL2Component.3=CmpCAASdoServerComponent.4=CmpCAASdoClientComponent.5=CmpSocketCanDrv

    then for testing purpose add a script in /home/pi
    startplc.sh (this is needed for have the right start order.. 1. load modules .2 start plc)

    #!/bin/sh
    sudo /etc/init.d/codesyscontrol stop
    sudo modprobe spi-config devices=bus=0:cs=0:modalias=mcp2515:speed=10000000:gpioirq=25:pd=20:pds32-0=16000000:pdu32-4=0x2002:force_release
    sudo modprobe mcp251x
    sudo ip link set can0 up type can bitrate 250000
    sudo ip -s -d link show can0
    sudo /etc/init.d/codesyscontrol start

    this stops the runtime
    load the modules
    and starts the plc again

    additional you need a script in the runtime directory (in our case /root)

    cat /root/
    rts_set_baud.sh

    #!/bin/sh
    BITRATE=expr $2 \\* 1000
    ifconfig $1 down
    echo ip link set $1 type can bitrate $BITRATE
    ip link set $1 type can bitrate $BITRATE
    ifconfig $1 up

    Let's see if this work for you and others too
    and let's see how stable the implementation is (according of losing messages etc)

    BR
    Edwin

    IMG: pican.jpg

    rts_set_baudPican.zip [231 Bytes]

     
  • jzhvymetal - 2014-09-19

    Edwin,

    I am still having trouble with the CAN SPI model with codesys. I can get the can SPI working directly from the console and able to send message to my PeakCan usb adapter.

    As soon as I start the codesyscontrol by typing the following "service codesyscontrol start" the Can adapter drops out. If I type ifconfig the can0 is no longer listed. Also if type dmesg I get the following output that repeats

    [ 482.345440] mcp251x spi0.0: MCP251x didn't enter in conf mode after reset

    It looks like when I run the codesyscontrol service it trys to reinitialize SPI causing the CAN apapter to drop out.

    Also you no longer have to use the older Raspbian 3.10.22+ there is a new complied version of the drivers for the latest kernel Raspbian 3.12.28+ (http://lnxpps.de/rpie/)

    IMG: Can Send OK.jpg

    IMG: CAN CoDeSys Not Ok.jpg

     
  • eschwellinger

    eschwellinger - 2014-09-19

    Hi,
    think at this point we should check online together...
    Think you should send a pm to arrange this.
    BR
    Edwin

     
  • jzhvymetal - 2014-09-25

    Edwin,

    I got CAN Bus working. The problem was the GPIO pins for the interrupt for mcp251x (GPIO17 in my case) and for some reason GPIO04 need to be disabled in the Codesys program. Also every time you do a full download it will reinitialize the GPIO pins which causes the CAN SPI module to stop working. This does not happen if you disable all the GPIO pin in the Codesys program.

    IMG: CanOpen Working.jpg

     
  • jzhvymetal - 2014-10-01

    Edwin,

    I did a stress testing using the CanEcho program and USB PCAN adapter. Running the 1ms cyclic task for the CanEcho program and had PCANView sending every 2ms with CanBus set for 250kbps. The results show that every transmission was echoed by the PI with no drop outs for 8hours and still going. I just can not explain why the PI transmitted more message than the PCAN transmitted. Any case I am impressed by the results. This was done with a non-optimized PI without overclocking.

    IMG: Pi CoDeSys Can Stress Test.jpg

     
1 2 3 > >> (Page 1 of 3)

Log in to post a comment.