Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Quick notes on the Nerves setup steps to enable an mcp2515 can controller on an rpi3

can0 setup

(for mcp2515 controller like the PiCAN2)

  1. fork (or clone) nerves_system_rpi3 locally into the parent directory of your project

  2. edit mix.exs and change the nerves_system_rpi3 lines to point to your local one:

     def system("rpi3"), do: [{:nerves_system_rpi3, path: "../nerves_system_rpi3", runtime: false}]
  3. mix deps.get

  4. mkdir config/rpi3 to set up a place to store some customized rpi configs

  5. copy over fwup.conf and config.txt from your local nerves_system_rpi3 repo

     cp ../nerves_system_rpi3/fwup.conf config/rpi3/fwup.conf
     cp ../nerves_system_rpi3/config.txt config/rpi3/config.txt
  6. in config/config.exs point nerves at new fwup.conf file:

    config :nerves, :firmware, fwup_conf: "config/rpi3/fwup.conf"

  7. edit config/rpi3/fwup.conf to include the additional overlays needed, by default the mcp2515-can0 overlay is not included in the base image. inside fwup.conf notice how the the overlay is set up for w1-gpio-pullup and use that as a pattern for adding our new overlay. search for all the lines containing w1-gpio-pullup, this will get you oriented for how the overlay is defined and then copied to each partition. You will need to do the same for the mcp2515-can0 overlay. There are four lines to add.

    1. set up a new file resource definition (around line 117) by adding:

      file-resource mcp2515-can0.dtbo { host-path = "${NERVES_SYSTEM}/images/rpi-firmware/overlays/mcp2515-can0.dtbo" }

    2. write the overlay file inside the task complete block (add around line 212) with all the other on-resource lines:

      on-resource mcp2515-can0.dtbo { fat_write(${BOOT_A_PART_OFFSET}, "overlays/mcp2515-can0.dtbo") }

    3. do the same inside the task upgrade.a block (around line 268):

      on-resource mcp2515-can0.dtbo { fat_write(${BOOT_A_PART_OFFSET}, "overlays/mcp2515-can0.dtbo") }

    4. finally, add an on-resource line for task upgrade.b (around line 328), note that the variable used in fat_write is different than for the previous steps:

      on-resource mcp2515-can0.dtbo { fat_write(${BOOT_B_PART_OFFSET}, "overlays/mcp2515-can0.dtbo") }

  8. inside config/rpi3/fwup.conf find the file-resource definition for config.txt and change it to point at the one you copied into config/rpi3/config.txt

     file-resource config.txt {
         host-path = "${NERVES_APP}/config/rpi3/config.txt"
  9. edit config/rpi3/config.txt and define the mcp2515 overlay by adding:

  10. Inside your project directory: mix and when that loads, do the following:

    1. configure the kernel: mix linux-menuconfig

      1. Networking support -> <*> CAN bus subsystem support
      2. Networking support -> CAN bus subsystem support -> CAN Device Drivers -> CAN SPI interfaces -> <*> Microchip MCP251x SPI CAN controllers
      3. Select <SAVE>
      4. Select <EXIT> as many times as needed until you are dropped back in the nerves system shell
    2. safe the kernel config: make linux-savedefconfig

    3. IMPORTANT: you need to copy the newly saved kernel defconfig back into the rpi3 system using something like:

       cp build/linux-04c8e47067d4873c584395e5cb260b4f170a99ea/defconfig /nerves/env/nerves_system_rpi3/linux-4.4.defconfig
    4. OPTIONAL: add usefull can-related packages: mix menuconfig

      1. Target packages -> Networking applications -> iproute2
      2. Target packages -> Networking applications -> can-utils
      3. Select <SAVE>
      4. Select <Exit> until you are dropped back to shell
    5. save the package config: make savedefconfig

    6. exit to leave

  11. mix compile NOTE: this will take forever the first time. on my four year old macbook pro this took about 4 hours

  12. mix firmware

  13. then depending on your project setup: mix firmware.burn / mix firmware.push ...

with any luck, you should now have a can0 device listed in /proc/net/dev on your rpi3. to pop into something similar to a console shell, run Nerves.Runtime.Shell.start from the iex prompt. From here you can do handy things like:

cat /proc/net/dev


ip link set can0 up type can bitrate 250000 (substitute 250000 with whatever bitrate you need for your vehicle)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment