====== 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 [[http://ww1.microchip.com/downloads/en/DeviceDoc/21801e.pdf|MCP2515]] (CAN controller) and the [[http://ww1.microchip.com/downloads/en/DeviceDoc/21667f.pdf|MCP2551]] (CAN transceiver) from [[http://www.microchip.com/|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: [[http://en.wikipedia.org/wiki/CAN_bus|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 ===== ===== 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 [[http://www.8devices.com/community/viewtopic.php?f=13&t=782&start=50|SPI on Carambola2]] where user Gerd released some patchfiles. The patchfiles can be downloaded here: [[https://github.com/GBert/openwrt-misc/tree/master/gpio-test/src|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 /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: {{:carambola:2:pinout_c2dev.png?200|}} If you want to see, which gpio-pins you should use, see the table in the middle of the article here: [[http://wiki.openwrt.org/toh/8devices/carambola2|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 ([[https://github.com/GBert/openwrt-misc/blob/master/gpio-test/src/can.mk_diff|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 [[http://wiki.openwrt.org/de/doc/howto/buildroot.exigence|OpenWRT buildroot installation]] and [[http://wiki.openwrt.org/doc/howto/build|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 '' .//scripts/feeds/update -a'' and install the two packages with '' .//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.