Hello,

I would like to use data from CAN bus on P9 pins 24,26 from the BBB to alter audio input from Bela based on a state machine. The CAN pins are not used by Bela but also not brought onto the cape. Is there a way to use them as inputs?

I would prefer to get this from the CAN bus, as the data is already available.

Alternatively, if I cannot gather the needed data from the vehicle CAN then I would need to implement Accelerator and GPS, to gather speed and acceleration. The airharp demo should help with accelerometer. Has anyone used GPS with Bela?

I don't know enough about the underlying Linux distro with Bela to know if anything was done to hopelessly break CAN compatibility, but I will comment from the assumption that CAN will work if you tweak the OS config in the right way. Then if you do this, does it break Bela? It's pretty easy to try since all you need to do is re-flash your image if you mess it up.

I did find several how-to's for compiling the device tree compiler and getting CAN working on the BBB. The one that seemed the most straightforward to me was this:
http://www.thomas-wedemeyer.de/beaglebone-canbus-python.html

The example interacts with the CAN bus using python but after you can talk to CAN using commandline utilities, then maybe looking at this could help figure out how to get CAN messages using sockets with a C program:
https://github.com/linux-can/can-tests/blob/master/canpump.c

If you can get the can-utils to work for you, then modifying one into a C program to further interpret the messages and print stuff from the CAN interface to the console will be easy, and then perhaps you would be most of the way to your goal.

Once you have a C program talking CAN, then I think plugging these messages into real-time audio control is going to be comparatively trivial.

Thanks @ryjobil, that's a great answer.

The CAN pins on the BBB are also used for the I2C-1 bus, but fortunately that is the unused I2C bus which is broken out onto the white 4-pin connector on the cape, so they should be useable for CAN. Kernel-wise everything should be fine too, but it will be necessary to edit the device tree to both activate the CAN peripheral and make sure those pins are used for CAN and not I2C. There's (another) good guide to that here.

Right on thank you @ryjobil and @LiamDonovan I will work on this. I had CAN working on BBB before I re-flashed to Bela Distro. So I will try again and report back.

I have the CAN working on the I2C-1 connector of the Bela cape. You need to remove the I2C pullups on the board and attach the CAN lines to a CAN transceiver.

The am335x-boneblack.dts (or am335x-bonegreen.dts if using Beaglebone Green) needs to be modified to shift the I2C-1 lines onto other lines to free up the pins for CAN bus use.

And you need to load a device tree overlay for the DCAN1.

I'm not sure how to include formatted code in this forum - perhaps someone would be so kind to let me know how or point me to some doc on how to format posts on this forum - then I can provide more details.

OK. Here goes with some code formatting ;-)

I couldn't for some reason attach a zip file -- so I've now put all the needed files on github -- so you should be able to get the files you need by clone the repository from your Beaglebone / Bela:

git clone https://github.com/glennsky/bela_CAN.git

Then to copy the files to their appropriate place - you can just run the copy_to_bela script:

cd bela_CAN
./copy_to_bela

Hopefully this will copy everything you need to the right place on the Beaglebone and upon reboot you CAN bus will be enabled! However, below are more details...

I've only tested things on a Beaglebone Green, but it works with Bela images v0.1.0a to v0.2.0b. I also included the Beaglebone Black .dtb/.dts file with the same changes so it should also work - but not tested. This is the relevant part that is changed -- moving the I2C1 pins to other pins to allow the DCAN1 to use those pins. Original line left there but commented out.

                pinmux_i2c1_pins {
                        //pinctrl-single,pins = <0x180 0x73 0x184 0x73>;
                        pinctrl-single,pins = <0x168 0x73 0x16C 0x73>;
                        linux,phandle = <0x7>;
                        phandle = <0x7>;
                };

To compile the .dts files you do:

dtc -O dtb -o am335x-boneblack.dtb -b 0 -@ am335x-boneblack.dts
dtc -O dtb -o am335x-bonegreen.dtb -b 0 -@ am335x-bonegreen.dts

Then they need to be copied to /boot/uboot/dtbs

The device tree overlay for the DCAN1 is BB-DCAN1-00A0.dts:

/dts-v1/;
/plugin/;
 
/ {
    compatible = "ti,beaglebone", "ti,beaglebone-black", "ti,beaglebone-green";
 
    /* identification */
    part-number = "dcan1pinmux";
 
    fragment@0 {
        target = <&am33xx_pinmux>;
        __overlay__ {
            dcan1_pins_s0: dcan1_pins_s0 {
                pinctrl-single,pins = <
                    0x180 0x12  /* d_can1_tx, SLEWCTRL_FAST | INPUT_PULLUP | MODE2 */
                    0x184 0x32  /* d_can1_rx, SLEWCTRL_FAST | RECV_ENABLE | INPUT_PULLUP | MODE2 */
                >;
            };
        };
    };
 
    fragment@1 {
        target = <&dcan1>;
        __overlay__ {
             #address-cells = <1>;
             #size-cells = <0>;
 
             status = "okay";
             pinctrl-names = "default";
             pinctrl-0 = <&dcan1_pins_s0>;
        };
    };
};

Which is compiled with:

dtc -O dtb -o BB-DCAN1-00A0.dtbo -b 0 -@ BB-DCAN1-00A0.dts

And then the .dtbo copied to /lib/firmware

Then you need to startup the CAN at bootup by adding these lines to /etc/rc.local

su -c "echo BB-DCAN1 > /sys/devices/bone_capemgr.9/slots"
su -c "modprobe can"
su -c "modprobe can-dev"
su -c "modprobe can-raw"

su -c "ip link set can0 up type can bitrate 500000"
su -c "ip link set can0 up txqueuelen 16"
su -c "ifconfig can0 up"

exit 0

You may want to change the bitrate to whatever you're using. I added the txqueuelen 16 to have a slightly larger transmit buffer.

If all goes well, upon reboot -- you'll be able to see the can0 by doing:

ifconfig

You can check for bootup messages regarding the CAN bus by doing:

dmesg | grep -i CAN

You should also install can-utils -- very useful socketcan routines.

sudo apt-get install can-utils

Then you should be able to run this command to see incoming packets on the CAN bus.:

candump can0

Good luck!! Let me know if it works...

Glenn

You sir are awesome!! Works like a charm on Beaglebone Black. 💯