OV5642 auto-focus on i.MX6

Before

before

After

after



Source is here.

‘Nuff said?

Probably not.

More detail is probably useful.

A word of thanks

Before the details, I’d first like to thank the folks at Ray Business Technologies for helping put this together. They have a strong team of embedded developers with competency in embedded Linux, i.MX6, and gstreamer and we wouldn’t have gotten this done without their help.

Okay, more details

For those of you using our Omnivision OV5642 parallel camera module, you may have noticed that it doesn’t do auto focus, but defaults to a long-range (infinite) focal length.

This doesn’t mean that the hardware doesn’t support it, only that the drivers supplied by Freescale were missing some key pieces.

The camera modules themselves contain a voice coil motor controlling the lense assembly and the OV5642 contains a micro-controller that knows how to drive it.

What was missing were:

How to test

We’re not going to delve too far into the details about this, but there’s a simple gstreamer utility that you can use to test it in this package:

It should compile and run on Yocto, Timesys or LTIB distributions using a command-line something like this:
~/gstcamera$ export RFS=$HOME/ltib-01.01/rootfs
~/gstcamera$ arm-fsl-linux-gnueabi-gcc \
               -L${RFS}/usr/lib/ \
               -I${RFS}/usr/include/gstreamer-0.10/ \
               -I${RFS}/usr/include/glib-2.0 \
               -I${RFS}/usr/lib/glib-2.0/include/ \
               -I${RFS}/usr/include/libxml2 \
               -o gstcamera \
               gstcamera.cpp \
               -lsupc++ -lpthread -lgstreamer-0.10 \
               -lgobject-2.0 -lgthread-2.0 \
               -lgmodule-2.0 -lglib-2.0 -lxml2 -lz
This app is a very simple wrapper around some gstreamer bits and is a total hack. It uses the gst_parse_launch() routine to parse the command line a.la. gst-launch and totally hijacks the camera file descriptor to perform the ioctl calls for auto-focus and manual focus.

Typical usage is:
~/$ gstcamera mfw_v4lsrc ! mfw_v4lsink
AF
SP0
SP255
 or  to exit
The commands supported include:
  • AF – triggers auto-focus
  • SPn – Set focus to Position n where n in the range [0..255]. 0 is furthest focal length. 255 is nearest.

Limitations

Note that this patch only applies to the OV5642 module and only in the non-Android kernel branch at the moment. Support for the OV5640 MIPI module and Android support will be added soon.

Stay tuned for more…

Tags:

20 Responses to “OV5642 auto-focus on i.MX6”

  1. S├ębastien Taylor May 15, 2013 6:41 pm #

    Seeing errors when using the latest 3.0.35_1.1.1 kernel from Boundary with this gstcamera software. A diff of the link to your github confirms I’m using the same ov5642.c driver.

    AF
    ioctl V4L2_CID_AUTO_FOCUS_START fail: Invalid argument

  2. Andrew May 17, 2013 6:06 pm #

    you need to rebuild the ov5642_camera and mxc_v4l2_camera kernel modules and install them on the target. I was getting the same error and it went away after I rebuilt.

  3. Andrew May 17, 2013 6:06 pm #

    I meant mxc_v4l2_capture module

  4. Andrew May 19, 2013 12:48 am #

    I have a FIXED focus ov5642 camera module (from here:http://www.kailaptech.com/Product.aspx?id=762&l1=711) The pinout is similar to the auto-focus ov5642 I bought from your site. Only difference is AF_VCC and AF_GND in auto-focus camera is now D1 and D0 in fixed-focus camera. I assume D1 and D0 are therefore unused. however, I get the following error when i run ‘gst-launch mfw_v4lsrc ! mfw_v4lsink’

    mx6_csi0_powerdown: powerdown=0, power_gp=0x6
    mx6_csi0_powerdown: powerdown=1, power_gp=0x6
    ERROR: v4l2 capture: slave not found!
    MFW_GST_V4LSINK_PLUGIN 3.0.5 build on Apr 13 2013 04:18:14.
    Setting pipeline to PAUSED ...
    mx6_csi0_powerdown: powerdown=0, power_gp=0x6
    mx6_csi0_powerdown: powerdown=0, power_gp=0x6
    Pipeline is live and does not need PREROLL ...
    Setting pipeline to PLAYING ...
    New clock: GstSystemClock
    ERROR: v4l2 capture: mxc_v4l_dqueue timeout enc_counter 0
    ERROR: from element /GstPipeline:pipeline0/MFWGstV4LSrc:mfwgstv4lsrc0: Internal data flow error.
    Additional debug info:
    /home/foo/yocto/build/tmp/work/armv7a-vfp-neon-poky-linux-gnueabi/gstreamer/0.10.36-r2/gstreamer-0.10.36/libs/gst/base/gstbasesrc.c(2625): gst_base_src_loop (): /GstPipeline:p
    ipeline0/MFWGstV4LSrc:mfwgstv4lsrc0:
    streaming task paused, reason error (-5)
    Execution ended after 10004917680 ns.
    Setting pipeline to PAUSED ...
    Setting pipeline to READY ...
    mx6_csi0_powerdown: powerdown=1, power_gp=0x6
    Setting pipeline to NULL ...
    Total rendered:0
    Freeing pipeline ...
    [--->FINALIZE v4l_sink
    

    so it seems like not a single camera frame buffer is not being dequeued. I think the same ov5642 firmware and software should run for the fixed-focus mode without errors. am i overlooking something? (auto-focus camera works with same software and command as a reference)

    • ericn May 20, 2013 6:35 am #

      I suspect a connection issue here. The firmware is a bit specific to the Truly auto-focus camera (tuned for the particular VCM), but the firmware isn’t invoked until asked.

      Did you try this camera before the patch?

  5. lisarden July 22, 2013 3:56 am #

    Hi!

    I want to enable this camera+Lite in linphone application. Will it work? Is there any guide how to test this camera in ubuntu?

    • ericn July 22, 2013 7:03 am #

      This Ubuntu image has Freescale’s gstreamer plugins, so it will allow testing as described above.

      It’s unlikely that Linphone will just work because the ioctl’s supported and required by the camera driver are not completely standard. In other words, some hacking is likely necessary.

  6. scott July 24, 2013 4:46 pm #

    Eric,
    The gstcamera compile command string you show above will not compile on the native nitrogen-6x platform, at least for the Ubuntu 4.0.0 kernel file system due to glibconfig.h path issues. One can use pkg-config to automagically solve the path issues:

    gcc -L/usr/lib/ -I/usr/include/gstreamer-0.10/ \
    	`pkg-config --cflags glib-2.0` -I/usr/lib/glib-2.0/include/ \
    	-I/usr/include/libxml2 -I/usr/include/libxml2/libxml/ \
    	 -o gstcamera \
    	gstcamera.cpp -lsupc++ -lpthread -lgstreamer-0.10 \
    	-lgobject-2.0 -lgthread-2.0 -lgmodule-2.0 \
    	`pkg-config --libs glib-2.0`\
    	-lglib-2.0 -lglib-2.0 \
    	-lxml2 \
    	 -lz
    

    By the way, will you have a compiled kernel with the necessary changes to ov5642.c in an updated kernel anytime soon? (Save me the time in re-compiling the kernel.) Also, any time frame for auto focus or manual focus adjustment on the MIPI camera?

    Scott

    • ericn July 24, 2013 5:31 pm #

      Hi Scott,

      The package imx6_kernel_overlay.tar.gz contains a kernel and modules with a very recent kernel and auto-focus support for the OV5642.

      If you’d have asked me two weeks ago about the auto-focus code and OV5640, I probably would have lied and said it would be available by now. I’ll try to scare up some cycles to get this out in the next week.

      • scott July 29, 2013 10:59 am #

        Eric,

        This worked like a champ for simple video streaming – example:

        gstcamera mfw_src capture-mode=4 ! mfw_sink disp-width=640 disp-height=480

        enter either AF (or use SP0 through SP255 for setting manual focus to a setpoint distance)

        Two questions I have at this point relative to your simple hack:

        1. How do you incorporate focusing into a frame capture stream; for example I normally use the following type of gst-launch file capture to create jpeg’s of captured video – I don’t see how I could easily manipulate or modify gstcamera to handle this type of capture stream.

        gst-launch mfw_v4lsrc num-buffers=1 ! jpegenc ! filesink location=/home/linaro/Desktop/test.jpg

        2. Would it be easy to set the ov5642 camera to start up in AF mode? I looked at the code but I am not familiar with the driver/capture code enough to easily hack this together.

        Thanks.

        Scott

        • ericn July 29, 2013 11:10 am #

          Hi Scott,

          The short answer is that this needs to be done inside of an application of some sort that controls the pipeline such that the following can occur in response to a capture request:

          • – auto-focus is triggered
          • – jpegenc and filesink are sliced into the pipeline (possibly through a tee), and
          • – pipeline is restored

          Unfortunately, I don’t have such a sample handy.

          It should also be easy enough to modify gstcamera.cpp to trigger an auto-focus event on startup (or shortly thereafter), but I’m not sure that satisfies your need.

    • ericn September 21, 2013 2:14 pm #

      Hi Scott,

      Auto-focus is now available in both our non-Android and Android kernel branches.

      The Android HAL has also been updated to support auto-focus. It will be invoked immediately prior to photo capture.

      • scott October 1, 2013 7:09 am #

        Eric,

        It doesn’t appear that the freescale gstreamer source and sink exist in the 4.1.0 kernel? I can modprobe the mxc_v4l2_capture but when I invoke gst-launch with mfw_v4lsrc (or sink), I get the ‘no element’ message.

        Scott

        • ericn October 1, 2013 9:04 am #

          Hi Scott,

          The mfw_v4lsrc and mfw_v4lsink components are a part of the userspace, not the kernel itself.

          If you check out the comments in this thread, you’ll hear about some of the difficulties of getting the gstreamer plugins up on arbitrary userspaces.

  7. zmb October 14, 2013 8:00 am #

    Eric,

    Any update on auto-focus implementation for OV5640 MIPI?

  8. ericn October 14, 2013 8:09 am #

    Hi zmb,

    Auto-focus is now in place for OV5640-MIPI in both the Android and non-Android kernel trees.

    Code to trigger auto-focus is also present in the Android 4.2.2 code base, though we haven’t generated a new image.

  9. Michael October 29, 2013 10:25 am #

    Does this driver can support camera module e-CAM57_MI5640_MOD?

    • ericn October 30, 2013 9:31 am #

      Hi Michael,

      I can’t say, since we don’t have one, but the folks at e-Con-Systems are pros, and can guide you through their use.

      My understanding (from reading their product announcements) is that they’re doing a lot more than simply auto-focus, and enabling lots of features of the cameras they supply.

  10. Daniel O. December 6, 2013 5:49 pm #

    Hi:

    How could I change the auto-focus in a gst-launch pipeline?

    • ericn December 6, 2013 8:20 pm #

      Hi Daniel,

      The code here in Github shows a hacked way of doing things, but a better solution would involve re-working the mfw_v4lsrc gstreamer element to issue the ioctl() calls.

Leave a Reply

Subscribe

Subscribe to our e-mail newsletter to receive updates.