Posts: 6
Joined: Tue Oct 04, 2016 6:48 am

Successful I2C/CSI2 raspiraw capture with V2 camera IMX219

Thu Oct 13, 2016 7:02 pm

For anyone interested in using raspiraw with V2 IMX219 camera:
In my persuit of triggering "instant" 5s exposure images without delay, I have now successfully captured raw bayer still images from the RPi V2 NoIR (IMX219) using the I2C/CSI2 support in vc.ril.rawcam from the MMAL API. (Many thanks to 6by9 for this!)
I mostly followed raspiraw V1 camera setup instructions in the forum thread viewtopic.php?f=43&t=109137&start=150

IMX219 registers I've found so far at the bottom of this post. All findings & experience welcome!

My IMX219 sensor turned out to be on address 0x10 using "i2cdetect -y 0".
Initially I would get random results; picture, no picture, pure noise or half noise/half picture.
After some trial and error with register settings, I finally got repeatedly successful captures .
Currently I set streaming off (0x0100=0x00), setup all registers, then wait 3s before enabling streaming and save the first capture.
I can change analog (0x0157), digital (0x0158, 0x0159) gain and coarse integration (0x015A, 0x015B) registers fine.
Now I'll be digging into 5s exposure time, which seems to involve extra long lines & frames. Will be interesting.

Here's what I did to capture with raspiraw and v2 camera:

git clone git://git.drogon.net/wiringPi; cd wiringPi/; git pull origin; ./build
wget https://raw.githubusercontent.com/6by9/ ... camera_i2c; chmod +x camera_i2c
#Added these lines at end of /boot/config.txt

#Added these lines at end of /etc/rc.local
sudo modprobe i2c_bcm2708 # I needed to probe this for /dev/i2cX to show up
sudo modprobe i2c_dev

sudo apt-get install cmake; git clone https://github.com/6by9/userland.git;
cd userland; git fetch origin; git checkout -b rawcam origin/rawcam; sudo ./buildme; sudo ln -s /opt/vc/bin/raspiraw /usr/bin/raspiraw

~/camera_i2c # setup GPIO pins for camera operation
raspiraw # run camera and record raw frames

Output will be a 1,0237,440 bytes large raw bayer file. (Matches jbeales comment in viewtopic.php?f=43&t=146310&p=1001567&h ... 9#p1001567)
This data needs to be converted to RGB, so I used PiCameras documentation Python script [http://picamera.readthedocs.io/en/relea ... a-captures].
Modified it to read in my raw file (filename as argument) directly with no JPEG headers and setting resolution manually:

Code: Select all

from numpy.lib.stride_tricks import as_strided
file = open(sys.argv[1], 'rb')
stream = io.BytesIO(file.read())
data = stream.getvalue()
data = np.fromstring(data, dtype=np.uint8)
reshape, crop = {
    1: ((1952, 3264), (1944, 3240)),
    2: ((2480, 4128), (2464, 4100)),
Spits out a binary I was able to open in GIMP using a 3280x2462 RGB format.

Also had some help from reading these threads:
"Pi Camera V2 raw bayer data" viewtopic.php?f=43&t=146310&p=1001567&h ... 9#p1001567
"Raw output information" viewtopic.php?f=43&t=44918&sid=1db39dfe ... &start=200

Register information was gathered mainly from these sources:
MediaTek https://github.com/omlet05/MediaTek-Hel ... w_Sensor.c
Chromium https://chromium.googlesource.com/chrom ... a/imx219.c
Android https://android.googlesource.com/kernel ... o/imx219.c
Asus ZenFone5 https://github.com/ZenfoneArea/android_ ... 9/imx219.h
Linux V4L2 Driver http://dl.linux-sunxi.org/SDK/A80/A80_S ... e/imx219.c, http://dl.linux-sunxi.org/SDK/A80/A80_S ... e/camera.h

Color & white balancing aside, so far I've found the following setup to give successful captures every time:

Code: Select all

* 3280x2464@15 FPS */
{0x0100, 0x00}, 		// 0=OFF, 1=Stream, 2=MAX
	{0x30EB, 0x05},
	{0x30EB, 0x0C},
	{0x300A, 0xFF},
	{0x300B, 0xFF},
	{0x30EB, 0x05},
	{0x30EB, 0x09},
	{0x0114, 0x01},		// CSI MIPI Lanes [1:0]  (0x01=2, 0x03=4)
	{0x0128, 0x00},		// DPHY_CNTRL
	{0x012A, 0x18},		// EXCK_FREQ [15:8]
	{0x012B, 0x00},		// EXCK_FREQ [7:0]
	{0x0157, 0x00},         // Analog Gain
	{0x0158, 0x00},         // Digital Gain [15:8]
	{0x0159, 0x00}          // Digital Gain [7:0]
        // {0x015A, 0x01},		// Shutter/Integration Time [15:8]
        // {0x015B, 0x00},		// Shutter/Integration Time [7:0]
	{0x0160, 0x09},		// Frame Length [15:8]
	{0x0161, 0xC8},		// Frame Length [7:0]
	{0x0162, 0x0D},		// Line Length [15:8]
	{0x0163, 0x78},		// Line Length [7:0]
	{0x0164, 0x00},
	{0x0165, 0x00},
	{0x0166, 0x0C},
	{0x0167, 0xCF},
	{0x0168, 0x00},
	{0x0169, 0x00},
	{0x016A, 0x09},
	{0x016B, 0x9F},
	{0x016C, 0x0C},
	{0x016D, 0xD0},
	{0x016E, 0x09},
	{0x016F, 0xA0},
	{0x0170, 0x01},		// X_ODD_INC [2:0]
	{0x0171, 0x01},		// Y_ODD_INC [2:0]
	{0x0172, 0x03},
	{0x0174, 0x00},		// Binning Mode H_A
	{0x0175, 0x00},		// Binning Mode V_A
	{0x018C, 0x0A},		// CSI Data Format [15:8]
	{0x018D, 0x0A},		// CSI Data Format [7:0]
	{0x0301, 0x05},		// VTPXCK_DIV
	{0x0303, 0x01},		// VTSYCK_DIV
	{0x0304, 0x03},		// PREPLLCK_VT_DIV [3:0]
	{0x0305, 0x03},		// PREPLLCK_OP_DIV [3:0]
	{0x0306, 0x00},		// PLL_VT_MPY [10:8]
	{0x0307, 0x2B},		// PLL_VT_MPY [7:0]
	{0x0309, 0x0A},		// OPPXCK_DIV [4:0]
	{0x030B, 0x01},		// OPSYCK_DIV 
	{0x030C, 0x00},		// PLL_OP_MPY [10:8]
	{0x030D, 0x55},		// PLL_OP_MPY [7:0]
	{0x455E, 0x00},		// CIS Tuning ?
	{0x471E, 0x4B},		// CIS Tuning ?
	{0x4767, 0x0F},		// CIS Tuning ?
	{0x4750, 0x14},		// CIS Tuning ?
	{0x4540, 0x00},		// CIS Tuning ?
	{0x47B4, 0x14},		// CIS Tuning ?
	{0x4713, 0x30},		// CIS Tuning ?
	{0x478B, 0x10},		// CIS Tuning ?
	{0x478F, 0x10},		// CIS Tuning ?
	{0x4797, 0x0E},		// CIS Tuning ?
	{0x479B, 0x0E},		// CIS Tuning ?
{0x0100, 0x01}

Posts: 56
Joined: Wed Jan 17, 2018 9:52 am

Re: Successful I2C/CSI2 raspiraw capture with V2 camera IMX219

Mon Jan 22, 2018 7:16 am

Have you timed this to measure how long it takes to capture? Have you tried taking multiple shots continuously?

Return to “Camera board”