Communication with the CAN bus

This topic handles about exchanging data with a CAN bus system by using the SPI interface of the Carambola 2 device. Additional to the Carambola it's necessary to build a hardware circuit with the MCP2515 (CAN controller) and the MCP2551 (CAN transceiver) from Microchip. This article is a summary of To-Do actions paired with the patches released by forum user Gerd. Big thanks for your enormous help!

Hardware

This chapter handles the hardware connection of the Carambola 2 with the 2-wire CAN bus.

CAN bus

Please catch the information out of the Wikipedia page: HERE The topic is much too complex to explain everything in here.

SPI bus

The SPI bus is a full-duplex and serial bus system with maximum speed of 11 MHz. It is a single master bus system where every slave must have a single connection to the master, the Chip Select (or Slave Select). The following lines are required to build a complete SPI bus system:

  • SPI-CLK (Clock, common line)
  • SPI-MISO (Data, Master In Slave Out, common line)
  • SPI-MOSI (Data, Master Out Slave In, common line)
  • SPI-CS (Chip Select, one line per slave, outgoing from the bus master)

The master initiates a data exchange by selecting a slave chip by enabling the SS-Pin for the appropriate slave. Further, the master begins to clock the SPI-CLK line to syncrhonize with the slave. On each Clock the Master is able to send a bit information to the slave over the MOSI-line. Coincident the slave can provide information to the master over the MISO line. After the transmission the SS-pin is disabled and the slave stops sending.

Hardwarelayout with the MCPs

<picture and description of the extension board>

HW-Modification on the Carambola 2

For SPI communication I used the following pins to connect the Carambola to my extension board.

  • J13, 12 (MISO → MCP2515 pin 15)
  • J13, 13 (MOSI → MCP2515 pin 14)
  • J13, 10 (Interrupt → MCP2515 pin 12)
  • J13, 20 (GND)
  • J12, 14 (GPIO SS → MCP2515 pin 16 (low active))

To connect them i soldered two pin strips on my carambola and used thin and short (!!) wires < 10cm. You should do the same caused of the spi bus speed to avoid reflections and latency problems.

Software

Functional description of the software parts.

Changes on the OpenWRT Image

Some changes have to be implemented in the OpenWRT Sourcecode. Therefore I want to refer to the forum thread SPI on Carambola2 where user Gerd released some patchfiles.

The patchfiles can be downloaded here: Patchfiles. You need to download the 800-MIPS-carambola2-mcp2515.patch and the 728-MIPS-ath79-add-gpio-irq.patch. Put it in your OpenWRT trunk under <openwrttrunkdir>/target/linux/ar71xx/patches-3.10. While make the patches will be applied in the sourcecode of OpenWRT.


Additional hint: If you want to use different GPIO pins to communicate with the CAN-to-SPI-Board, you can change the pins in the 800-… patchfile:

+#define MCP2515_CAN_INT_GPIO_PIN 20

+#define GPIO_SPI_CS1_MCP2515 22

where 20 and 22 are the GPIO numbers. For an allocation to the physical pins see here:

If you want to see, which gpio-pins you should use, see the table in the middle of the article here:

OpenWRT Carambola 2 Entry


Additional hint 2: While testing I noted, that the MCP2515 sometimes rests in /INT low state. So, the Carambola doesn't register an edge on his interrupt input pin and the communication freezes. A remmedy would be to change the interrupt trigger from edge to state. On this way the Carambola 2 recognizes the low state and starts the ISR. You can find the trigger in the patchfile 800-…:

+ .irq_flags = IRQF_TRIGGER_FALLING|IRQF_ONESHOT,

to

+ .irq_flags = IRQF_TRIGGER_LOW|IRQF_ONESHOT,.


Before building the image, we have to patch the file can.mk_diff manually in the trunk dir. Copy the file (can.mk_diff) to your main trunk dir. Afterwards apply the patch with

patch -p1 < can.mk_diff

Compiling the new image

After inserting the patches we are ready to build the image by using make. For detailed information see OpenWRT buildroot installation and OpenWRT buildroot usage.

Before executing the menuconfig to choose the necessary packages, we have to add install some feeds. I usually use the luci package for my routers. Further the can utilities are very useful for our project ;-)

To get the can-utils add the source

src-git railroad http://github.com/GBert/railroad

to the feeds.conf.default. Afterwards update the feeds with

./<openwrttrunkdir>/scripts/feeds/update -a

and install the two packages with

./<openwrttrunkdir>/scripts/feeds/install luci can-utils.


In the menuconfig, we have to choose the following packages: …

Testing

Softwaretests

candump, cansend

Hardwaretests

Voltages on the measurement points

Additional Infos

Gerd noticed that a full loaded CAN bus can cause packet loss on the MCP2515.