# BPI-M1 CAN Bus

为了您能正确的连接 CAN4LINUX 到 Banana Pi （BPI-M1）中，你需要阅读全志A20硬件手册中相关CAN的章节。CAN4LINUX 是基于字符驱动的Linux驱动程序。已经能在x86和嵌入式平台中运行如 Freescale FlexCAN (i.MX) 或者是Xiliny Zynq 平台。

关于在A20上运行CAN总线的详细文档请参考如下：\
<https://dl.linux-sunxi.org/A20/CAN%20Bus1.pdf>

**下面教程就是如何在Bananian 15-01中使用CAN 总线：**

感谢selqcir 分享他的成果：

* 下载并且烧录"bananian-1501.img" 到一张至少8GB的 tf卡上。
* 使用"bananian-config"命令扩展根文件系统。
* 安装缺少的扩展包：

  \`\`\`apt-get install git\
  apt-get update\
  apt-get upgrade\
  reboot'''
* 获取 Bananian 缺少的包，并且安装这个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'''
* 编译内核:\
  '''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-m1/zh/bpi-m1canbus.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.
