How to setup a VC MIPI OV9281 on a Raspberry PI Model 3B+

Hardware and Software Setup

Revision: 0.1
Date: 2019-03-06
Copyright: 2019 Vision Components GmbH Ettlingen, Germany
Author: VC Support
Foreword and Disclaimer

This documentation has been prepared with most possible care. However Vision Components GmbH does not take any liability for possible errors. In the interest of progress, Vision Components GmbH reserves the right to perform technical changes without further notice.

Please notify support@vision-components.com if you become aware of any errors in this manual or if a certain topic requires more detailed documentation.

This manual is intended for information of Vision Component’s customers only. Any publication of this document or parts thereof requires written permission by Vision Components GmbH.

Image symbols used in this document
Symbol Meaning
Note Sign The Light bulb highlights hints and ideas that may be helpful for a development.
Warning Sign This warning sign alerts of possible pitfalls to avoid. Please pay careful attention to sections marked with this sign.
Example Sign This is a sign for an example.

Trademarks

Linux, Debian, the Tux logo, Vivado, Xilinx and Zynq, ARM, Cortex, Windows XP, Total Commander, Tera Term, Motorola, HALCON, Vision Components are registered Trademarks. All trademarks are the property of their respective owners.

MIPI, Raspberry Pi and Raspbian are also registered Trademarks.

ESD sensitivity

Warning

Warning Sign The components are very sensitive to electrostatic discharge (ESD)! Please take all the precautions necessary to avoid ESD!

ESD

ESD Sign The electronic components and circuits are sensitive to ElectroStatic Discharge (ESD). When handling any circuit board assemblies, it is necessary that ESD safety precautions be observed.

ESD safe best practices include, but are not limited to:

This note is not an exhaustive information about the protection against electrostatic discharge (ESD).

Table of Contents

1   Overview

./images/overview.jpg

Overview of relevant components excluding power supply, monitor, USB keyboard, cables

./images/powersupply.png

The power supply must at least provide 2.5A at 5V.

2   Hardware Setup

2.1   Hardware Pre-Check: Install Raspbian

First step is to install Raspbian from https://www.raspberrypi.org/downloads/raspbian/ to a Micro-SD card. The driver is known to work with the kernel version 4.14, so download the appropriate Raspbian version. Raspbian Stretch Lite is sufficient, and this guide expects this version to be installed not only for the framebuffer output handling. The installation procedure usually has to be done using a normal PC with a Micro-SD card reader to write an image of Raspbian to the Mircro-SD card. For more instructions see the Raspbian Installation Manual.

./images/raspbian.png

(Original site looks different) Install Raspbian by following the instructions provided there

Connect a monitor to the Raspberry Pi HDMI output and a USB keyboard. Do not forget to insert the Micro-SD card with the installed Raspbian into the raspberry Pi.

./images/hdmiKeyboard.jpg

First setup to check if Raspbian is running: USB keyboard, HDMI Display, Power Supply, Raspbian Micro-SD card

The display shows a login prompt after successful installation. If this is not the case, you have to check your Raspbian installation. The most relevant information to succeed can be found at the Raspbian website or at the web.

./images/login.jpg

Raspbian showing login screen (user is usually pi with password raspberry)

2.2   Connect the MIPI module

./images/disconnect.png

Always disconnect other cables before connecting or disconnecting the MIPI module!

The ends of the MIPI module connector cable is descripted with the hardware to connect to. Open the socket connectors first by raising their lid, insert the cable and press their lid back. You should then not be able to pull the cable out.

./images/module.png

Open the MIPI module socket, put in the cable, close the MIPI module socket

Warning

Warning Sign

The connection at this type of socket is not protected against bad alignment, so always check the orthogonality, and if it is bent, correct it! Also watch out for the right orientation of the cable! The MIPI module or the Raspberry Pi can be irrevocably damaged if the cable is not inserted the right way, and warranty is lost!

./images/bentCable.png

Harmfully angled (left) and good (right) cable fixation

The socket type is also not protected against wrong orientation, so compare your setup to the figures below before switching the power on.

./images/modulesideBadGood.png

Watch the orientation of the cable (left: bad, right: good)

There may be a dust prevention sticker at the socket named CAMERA at the raspberryPi, remove it first. Like at the sensor module, open the lid first, insert the cable to be orthogonally fixed after shutting the lid. Also check the orthogonality here and correct it if the cable is angled!

./images/cpusideBadGood.png

Connect the cable to the CAMERA socket at the raspberry Pi equally (left: bad, right: good)

Reconnect the other peripherials to the Raspberry Pi.

./images/connected.jpg

Connection setup for the first image acquisition test.

You should have the login prompt back after switching the system on.

./images/login.jpg

Raspbian showing login screen (user is usually pi with password raspberry)

Warning

Warning Sign

Do not connect other devices to the I²C bus 0, since it can affect the communication between the camera sensor and the driver!

For example, running the touch screen of the Raspberry PI 7 inch display will lead to communication problems between driver and camera sensor. The display may work with the following line appended to the /boot/config.txt, but test first without connecting it to the Raspberry PI to be sure everything works so far:

disable_touchscreen=1

Don't connect the SDA/SCK of the 7 inch display since this would connect the I²C bus VC from the socket named DISPLAY with the I²C bus ARM at the pinout!

3   Software Setup

3.1   Install necessary Raspbian packages

Before beginning with the installation, do the following steps first. This requires your Raspberry PI to already have an internet connection; otherwise you have to install the packages mentioned manually, search the web for the procedure needed.

  1. Update the raspberrypi-kernel package and your system by calling:

    sudo  apt-get update   &&   sudo  apt-get upgrade
    
  2. Reboot.

  3. Install the raspberrypi-kernel-headers and device-tree-compiler package by using the following command:

    sudo  apt-get install  raspberrypi-kernel-headers  device-tree-compiler
    
  4. Test if the version of the running kernel matches the version of the kernel headers, the following command should show the directory for compiling the sensor module kernel module driver:

    ls "/usr/src/linux-headers-$(uname -r)"
    
  5. If the test was successful you may succeed.

3.2   Get the driver and demo code

You can download the driver and demo code with sources from the website http://www.vcmipi-modules.com

The archive file to get is named as follows:

vc_mipi_package_raspberryPi3_ov9281.tar.gz

Transfer the archive file to your Raspberry PI, you can do this for example by copying it to an USB stick and mount it at the Raspbian system.

Extract the archive at your home directory, there should then be a subdirectory named vc_mipi_package_raspberryPi3_ov9281.

In this section, it is assumed, that you are in that subdirectory.

3.3   Driver Installation

  1. Change to the subdirectory of the extracted archive named vc_mipi_driver_raspberryPi3_ov9281.

  2. The directory with the sources contains a Makefile to compile the driver. Do so by calling:

    make clean all
    
  3. Calling make the following way will install the kernel module in your current kernel module path:

    make install
    
  4. Follow the instructions displayed by the make command, this may be similar to the following lines:

    ADD 'dtparam=i2c_vc=on' and 'dtoverlay=vc_mipi_ov9281' to your /boot/config.txt
    ADD 'disable_touchscreen=1' to your /boot/config.txt if a touchscreen is attached
    ADD 'cma=128M' to your /boot/cmdline.txt
    
  5. Reboot.

  6. After reboot check the installation success by reentering the subdirectory named vc_mipi_driver_raspberryPi3_ov9281 and executing the following command:

    ./check_installation.sh
    

    If one of the outputs show a failure, check first if you followed all instructions correctly. Should the failure not vanish refer to the Q/A section and/or read the Driver knowledge section for more information to be able to identify the source of your problem.

3.4   First Image Acquisition Test

A sensor device should be listed as Video input at the following command output:

v4l2-ctl --all

The following command dumps sensor data:

v4l2-ctl --stream-mmap --stream-count=-1 -d /dev/video0 --stream-to=/dev/null

It will output subsequent lines ending with a frames-per-second information until pressing CTRL-C:

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 126.00 fps
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 125.66 fps
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 125.80 fps
<<<<<<<<<<<<<<<<<<<<<<<^C

3.5   Running the Demo

The demo itself is a program named vcmipidemo and its source code is mainly at the file vcmipidemo.c. However more programs are provided, namely the vcimgnetsrv, a network image server, and its counterpart vcimgnetclient.py. The vcimgnetsrv is started as background service, and the vcmipidemo connects to it. Then you can use the vcimgnetclient.py on your PC to view live captured images.

But for the first run it is better to just run the vcmipidemo and check if it shows the ascii representation. This works without any network cable attached. You can then output the captured image to the framebuffer of the display by using the -f command line switch.

3.5.1   Compile the programs

  1. Change to the subdirectory named vc_mipi_demo/src.

  2. The source directory contains a Makefile to compile the driver. Do so by calling:

    make clean all
    

3.5.2   Execute the demo

Just run the demo itself:

./vcmipidemo

or with framebuffer output:

./vcmipidemo -f

or with live view over ethernet:

./vcimgnetsrv &
./vcmipidemo

For live view over ethernet, execute the vcimgnetclient.py at the client (needs Python 2 and PyGTK).

4   Troubleshooting and Background Information

4.1   Q/A

Problem:

Running make fails with an error:

⋮
make[1]: *** /lib/modules/4.14.79-v7+/build: No such file or directory.  Stop.
⋮

Solution:

The system needs the build tools of the kernel to build the sensor driver (which itself is a kernel module). They can be obtained by installing the RaspberryPi Kernel Headers package named raspberrypi-kernel-headers, see https://www.raspberrypi.org/documentation/linux/kernel/headers.md

Problem:

The sensor module driver cannot be started, it shows an error:

[    4.766527] dummy 0-0010: Reading register 101 from 10 failed
[    4.766546] ov9281 0-0060: VC Sensor MODE=1 PowerOn STATUS=0xffffff87 i=1
[    4.766657] ov9281 0-0060: GAIN = 10
[    4.773298] ov9281 0-0060: Error -5 setting default controls
[    4.773346] ov9281: probe of 0-0060 failed with error -5

Solution:

Be sure no other device is connected to the I²C bus 0! For example, the touch screen controller of the Raspberry PI display may not be connected.

Check the orientation of the cable at the sensor side as well as at the cpu side. Also check if the cable and the sockets are orthogonal.

4.2   Driver Knowledge

The following tasks have to be done to do an image acquisition with the camera sensor:

  • Contiguous memory must be reserved for the captured image.
  • Information about the new sensor hardware must be provided to the kernel by adding it to the so-called kernel device tree as overlay.
  • This device tree overlay must be applied to the kernel device tree.
  • For the driver to communicate with the sensor the I²C bus must be set up to connect between the CPU and the MIPI socket.
  • The driver itself must be installed as kernel modules.

The next figures will be clearer after reading the whole driver knowledge section.

./images/raspberryPi_startup_overview.png

Raspbian Boot overview

A deeper insight into the device tree overlays and parameters can be found at https://www.raspberrypi.org/documentation/configuration/device-tree.md

./images/raspberryPi_i2c.png

Raspbian I²C connection: Influence of the parameter dtparam=i2c_vc=on

Note

Note Sign

Some hardware like the touch display demands exclusiveness over the I²C Bus VC or their drivers assume the I²C Bus VC is connected to the RTOS. Since the sensor driver must communicate with the sensor module connected to the MIPI socket, neither the exclusiveness nor the RTOS connectedness is given. So the I²C bus VC cannot be used when the sensor is attached.

4.2.1   Reserving Contiguous Memory for the Image Captures

In contrast to the normally used non-contiguous memory the capture hardware needs a contiguous memory region to transfer pixel data to by using direct memory access (DMA).

To reserve 128MByte of memory for capturing images, edit the file named

/boot/cmdline.txt

and append the following key-value-pair to the current line:

cma=128M

After reboot the kernel message line beginning with Memory: will show an additional entry:

131072K cma-reserved

Example

Example Sign After reboot you can show the line by executing the following command:

dmesg | grep '^[^]]*\] Memory:'

Here is a sample output:

[    0.000000] Memory: 817108K/970752K available (7168K kernel code, 576K rwdata, 2076K rodata, 1024K init, 698K bss, 22572K reserved, 131072K cma-reserved)

4.2.2   Providing the device tree overlay

Here are the steps to compile the device tree overlay by yourself:

The driver source directory includes the device tree source file with the relevant information about the camera sensor named

vc_mipi_imx296-overlay.dts
  1. Install the device-tree-compiler package via sudo apt-get install device-tree-compiler

  2. Compile the dtbo kernel device tree overlay binary representation by using the following command:

    dtc -@ -I dts -O dtb -o vc_mipi_ov9281.dtbo vc_mipi_ov9281-overlay.dts
    
  3. Copy the binary vc_mipi_ov9281.dtbo to /boot/overlays/vc_mipi_ov9281.dtbo.

4.2.2.1   Telling the kernel to use the device tree overlay

Before starting the linux kernel, the Raspberry Pi first boots a real-time operating system (RTOS) on the GPU. This RTOS looks into the file /boot/config.txt. It loads a default Kernel device tree and patches it by overlaying the device tree parts listed by the dtoverlay entries at the file /boot/config.txt. To add information about the new sensor the config-file needs the following entry:

dtoverlay=vc_mipi_ov9281

The device tree will then be modified by the overlay at /boot/overlays/vc_mipi_ov9281.dtbo before starting the linux kernel, to provide the node /dev/video0 using the capture driver module at /lib/.../module/vc_mipi_ov9281.ko.

With this customized kernel device tree the linux kernel will be started.

To check the behaviour of the RTOS one can look at the output by the following command:

sudo vcdbg log msg

Example

Example Sign After reboot the applied overlays can be shown by executing the following command:

sudo vcdbg log msg  2>&1 | grep '^[0-9\.]\+: Loaded overlay'

Here is a sample output:

002143.555: Loaded overlay 'ov9281'

4.2.3   Set up the I²C bus for driver-sensor communication

The I²C bus is also configured by the RTOS. So the file '/boot/config.txt' needs an additional entry:

dtparam=i2c_vc=on

It changes the physical I²C bus 0 accessor from the default, the GPU, to the CPU, since the MIPI sensor communicates over the I²C bus with the CPU kernel driver.

Example

Example Sign After reboot the dtparam line can be shown by executing the following command:

sudo vcdbg log msg  2>&1 | grep '^[0-9\.]\+:  dtparam:'

Here is a sample output:

002077.358: dtparam: audio=on
002096.467: dtparam: i2c_vc=on

4.2.4   Providing the sensor driver as kernel module

Here are the steps to compile the kernel modules by yourself:

  1. Change to the subdirectory named vc_mipi_driver_raspberryPi3_ov9281.

  2. The source directory contains a Makefile to compile the driver. Do so by calling:

    make clean all
    

The directory then contains the driver as two modules. They must be copied to their places

  • vc_mipi_ov9281.ko to /lib/modules/????/kernel/drivers/media/i2c
  • bcm2835-unicam.ko to /lib/modules/????/kernel/drivers/media/platform/

Afterwards the new modules must be registered by calling depmod -a.

The module drivers will then be loaded by calling modprobe vc_mipi_ov9281 and modprobe bcm2835_unicam.

Example

Example Sign After reboot you can display the output of the vc_mipi_ov9281 kernel module by executing the following command:

dmesg | grep '^[^]]*\] vc_mipi_ov9281:'

Here is a sample output:

[    4.107734] ov9281: loading out-of-tree module taints kernel.
[    4.231604] ov9281 0-0060: VC Sensor MODE=1 PowerOn STATUS=0x80 i=1
[    4.231719] ov9281 0-0060: GAIN = 10
[    4.239752] ov9281 0-0060: EXPOSURE = 10896
[    4.252885] ov9281 0-0060: Model ID 0x9281, Lot ID 0x0000a1, Chip ID 0x0000