• Using the MCP2515 CAN bus interface with the Acme AriaG25

    In this article I will explain how to get the Acme systems Terra board (or just the AriaG25 module) connected to a CAN Bus using the Microchip MCP2515 CAN controller.  I assume you already know how to wire this chip to the CAN bus, you need a line driver/receiver in between the MCP2515 and the bus. For example the MCP2551. Note that most of these drivers need a 5V supply. The MCP2515 itself normally also runs on 5Volts, but I recommend supplying it the 3.3V from the Terra/Aria (still within specs), as you will run into level problems with the SPI interface otherwise.  Don’t forget to tie the 5 and 3.3V grounds together when using an external 5V supply.

    Connecting the MCP2515 to the Terra board.

    I have wired the SPI pins on the MCP2515 to the Terra board, using the Daisy D15 connector. Signal names speak for themselves, you need MISO, MOSI, SCK (SPCK) and CS. (NPCS0). If you connect straight to the AriaG25 module, use pins S9 thru S12. Be sure to tie the /RESET pin on the MCP chip to VCC. You don’t need the ..RTS and ..BF pins. What you do need however is an interrupt line. A GPIO pin can be used on the Aria module for this purpose. However there’s no GPIO signal in the D15 connector so I used the D12 connector to get to the PC14 pin (N16 on the Aria module). You are free to choose any GPIO.

    Compiling the kernel

    Once all connections are made, the next step is to make sure you are able to compile a kernel. Use the article on the Acme site about kernel compiling to set things up. Once you are able to create a uImage file, you can start configuring the kernel for the CAN bus.

    1. In the kernel configuration menu, enable the SPI driver in Device Drivers -> SPI Support. Make sure the Atmel SPI is enabled.
    2. Ensure the board is set to SAM9e5ek, by going to System type. ARM system type should be AT91. Proceed into AT91 system on a chip. Under Board type there should be a line saying AT91SAM9X5 series evaluation kit. That should be selected.
    3. Go to Networking Support -> CAN bus subsystem support, and enable the RAW CAN protocoll. Proceed to CAN Device drivers, and enable the Platform CAN Drivers, and then the Microchip MCP251X.board-sam9x5ek.c
    4. In order to be able to verify the interrupt line operation, go to Kernel hacking, and enable the Debug Filesystem.
    5. Save the configuration.
    6. Edit the board specific configuration file. Since we selected the AT91SAM9X5 evaluation kit, the file is in arch/arm/mach-at91/board-sam9x5ek.c  In the includes list, add this line:

      #include <linux/can/platform/mcp251x.h>

      Then locate the line: static struct spi_board_info ariag25_spi_devices[] =
      Before this line, add this struct:

      static struct mcp251x_platform_data mcp251x_info = {
      .oscillator_frequency = 8000000,
      .board_specific_setup = NULL,
      .power_enable = NULL,
      .transceiver_enable = NULL,

      Change the oscillator frequency to match you crystal’s frequency.

      If there are no other SPI devices on your board, change the next struct to :

      static struct spi_board_info ariag25_spi_devices[] = {
      .modalias    = "mcp2515",
      .irq = AT91_PIN_PC14,
      .platform_data = &mcp251x_info,
      .chip_select    = 0,
      .max_speed_hz    = 8 * 1000 * 1000,
      .bus_num    = 0,
      .mode = SPI_MODE_0,

      If you are using an MCP2510, change the modalias to mcp2510. The irq section should point to the interrupt line you have chosen to use.

    7. Compile the kernel, and put the new uImage on the SD card.
    8. After booting the AriaG25, examine the kernel messages by entering this command: dmesg | grep mcp
      If all works well, you should see output like:

      mcp251x spi0.0: CANSTAT 0x80 CANCTRL 0x07
      mcp251x spi0.0: can0: Features changed: 0x00004804 -> 0x00004004
      mcp251x spi0.0: probed

      If not, check your signal connections.

    9. Configure the CAN bitrate by issuing this command:

      ip link set can0 type can bitrate 125000 triple-sampling on

      Where 125000 is your desired bitrate of course.

    10. Start the interface with:

      ifconfig can0 up

      If you see something like:

      mcp251x spi1.0: MCP251x didn't wake-up
      mcp251x spi1.0: CNF: 0x03 0xf5 0x01

      Then there is a problem with your interrupt signal.

    11. Mount the debug FS with:

      mount -t debugfs none /sys/kernel/debug.

      Check if your interrupt is registered correctly by viewing /sys/kernel/debug/at91_gpio.
      Your requested interrupt should be in this list, like
      14:    GPIO:1        GPIO:1        IRQ:edge-falling    GPIO:1

    12. You are now ready to send test messages. There is a set of tools that do this, but you have to download the sources and compile them on the AriaG25 first. You can obtain these sources from the BerliOS website.
    13. When the utilities have compiled and you have put the in the appropriate directories, you can now send a testmessage with the command:

      cansend 100#

      This command will send a CAN frame with identifier 100, and four databytes with values 11, 12 and 13.

      You can also test recieving of frames with the candump utility:

      candump can0

    This is the end of the article. You can now start writing your own applications using the CAN bus. A good point to start is this Wikipedia article that contains more links at the bottom of the page. Good luck!