# BPI-R1 CAN Bus

In order to port [can4linux](https://sourceforge.net/projects/can4linux/) to the BananaPi, the CAN module description is needed from the A20 hardware manual. can4linux is a character-driver-based Linux driver used already on desktop PCs and embedded platforms like Freescale FlexCAN (the i.MX series of micro controllers) or Xiliny Zynq.

There is a more detailed document about CAN on the A20 at [https://dl.linux-sunxi.org/A20/CAN Bus1.pdf](https://dl.linux-sunxi.org/A20/CAN%20Bus1.pdf)

**This is a tutorial for using CAN BUS on bananapi with bananian 15-01**

Thank selqcir share this example:

* Download and install "bananian-1501.img" into 8 GB SDCard.
* Expand the root file system using "bananian-config"
* Install missing package:

  ```
  apt-get install git
  apt-get update
  apt-get upgrade
  reboot
  ```
* Get last bananian available, and continu to install missing package:

  ```
  git clone https://github.com/Bananian/linux-bananapi
  apt-get install build-essential u-boot-tools uboot-mkimage 
  apt-get install libusb-1.0-0 libusb-1.0-0-dev git wget fakeroot kernel-package zlib1g-dev libncurses5-dev
  apt-get install subversion
  ```
* Build kernel:

  ```
  cd linux-bananapi
  make menuconfig
  ```
* Exit without saving when menu appears

  ```
  zcat /proc/config.gz > /root/linux-bananapi/.config
  make uImage modules
  make INSTALL_MOD_PATH=output modules_install
  ```
* At this step, kernel should be compiled and "Module.symvers" should be available
* Then rename modules and firmware provide by Bananian, and replace by the new one.

  ```
  mv /lib/modules /lib/modules.old
  mv /lib/firmware /lib/firmware.old
  mv /root/linux-bananapi/output/lib/modules /lib/modules
  mv /root/linux-bananapi/output/lib/firmware /lib/firmware
  ```
* Same for uImage:

  ```
  mount /dev/mmcblk0p1 /mnt 
  cd /mnt
  mv uImage uImage.old
  mv /root/linux-bananapi/arch/arm/boot/uImage /mnt
  reboot
  ```
* Create link for further build:

  ```
  cd /lib/modules/3.4.104/
  ln -s /root/linux-bananapi build
  cd ~
  ```
* Get Can4Linux and build it:

  ```
  svn checkout https://svn.code.sf.net/p/can4linux/code/trunk can4linux-code
  cd /root/can4linux-code/can4linux/
  make TARGET=BANANAPI
  ```
* Install module for each startup of the board:

  ```
  insmod can4linux.ko
  cp can4linux.ko /lib/modules/3.4.104/kernel/net/can/
  depmod -A -v
  modprobe -v can4linux
  echo "" >> /etc/modules ; echo "can4linux" >> /etc/modules
  reboot
  ```
* Build CAN example

  ```
  apt-get install swig
  apt-get install python-dev
  cd can4linux-code/can4linux-examples/
  ```
* Update CAN speed and device in file "pyCan-example.py"

  ex:

  ```
  # setting the device number
  device = 0
  defaultBaudrate = '250'
  ```
* Connect CAN transceiver and CAN bus, and check with for example:

```
   python pyCan-example.py
```

That's all

With this method, kernel version is "Linux bananapi 3.4.104" instead of "Linux bananapi 3.4.104+", because i was unable to find same sources than Bananian 15-01 , but CAN bus work !

Feel free to comment or update.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://bananapi.gitbook.io/bpi-r1/en/bpi-r1canbus.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
