Skip to content

Instantly share code, notes, and snippets.

@Hermann-SW
Last active January 28, 2022 13:47
Show Gist options
  • Save Hermann-SW/3c0387c4340e10866af32cc5a3d21036 to your computer and use it in GitHub Desktop.
Save Hermann-SW/3c0387c4340e10866af32cc5a3d21036 to your computer and use it in GitHub Desktop.
Determine Raspberry camera version connected to CSI-2
#!/bin/bash
dt=`vcgencmd get_camera | grep "detected=1"`
if [ "$dt" = "" ]; then
echo "no camera detected"
else
if [ "`which i2cdetect`" = "" ]; then
echo "i2cdetect not installed" ; exit
fi
cd `dirname $0`
if [[ ! -a camera_i2c ]]; then
wget https://raw.githubusercontent.com/6by9/raspiraw/master/camera_i2c \
2>/dev/null
fi
r=`uname -r | head --bytes 1`
if [ "$r" = "4" ]; then i2c=0; else i2c=10; fi
bash camera_i2c 2>&1 | cat > /dev/null
v1=`i2cdetect -y $i2c 54 54 | grep " 36"`
v2=`i2cdetect -y $i2c 16 16 | grep " 10"`
hq=`i2cdetect -y $i2c 26 26 | grep " 1a"`
if [ "$v1" != "" ]; then echo -n "v1"; fi
if [ "$v2" != "" ]; then echo -n "v2"; fi
if [ "$hq" != "" ]; then echo -n "hq"; fi
echo " camera found"
fi
@hailfinger
Copy link

For better readability you can specify the address on the bus in hex:

  v1=`i2cdetect -y $i2c 0x36 0x36 | grep " 36"`
  v2=`i2cdetect -y $i2c 0x10 0x10 | grep " 10"`
  hq=`i2cdetect -y $i2c 0x1a 0x1a | grep " 1a"`

@hailfinger
Copy link

If you want to detect the Auvidea B101, try this:

  hdmicaptureb101=`i2cdetect -y $i2c 0x0f 0x0f | grep " 0f"`
  if [ "$hdmicaptureb101" != "" ]; then echo -n "hdmicaptureb101"; fi

@hailfinger
Copy link

By the way, on the Pi 4 the wget command is not sufficient because camera_i2c will call rpi3-gpiovirtbuf which needs to be downloaded as well.

@hailfinger
Copy link

hailfinger commented Nov 22, 2020

There is no error handling for missing VC I2C support in the device tree. Enabling I2C in raspi-config only enables the ARM I2C, not the VC I2C. If dtparam=i2c_vc=on is not present in /boot/config.txt, you get this output:
$ ./camver
Error: Could not open file /dev/i2c-10' or /dev/i2c/10': No such file or directory
Error: Could not open file /dev/i2c-10' or /dev/i2c/10': No such file or directory
Error: Could not open file /dev/i2c-10' or /dev/i2c/10': No such file or directory
camera found

That's suboptimal. The same problem happens if the module i2c-dev is not loaded.

@hailfinger
Copy link

My current version is here.
Changelog:
Switch from decimal to hex in i2cget
Handle the TC358743 HDMI-to-CSI converter
Handle the case where tc358743 dtoverlay is active, both if TC358743 or a Pi camera are present
Remove useless use of cat
Assume camera_i2c is directly executable in $PATH
@Hermann-SW I was unable to find which license applies to the code, but I hope you'll accept the changes.

#!/bin/bash
# From Hermann Stamm-Wilbrandt, https://gist.github.com/Hermann-SW/3c0387c4340e10866af32cc5a3d21036
# Added TC358743 HDMI-to-CSI converter support (Auvidea B101) by Carl-Daniel Hailfinger
# License unclear
r=`uname -r | head --bytes 1`
if [ "$r" = "4" ]; then i2c=0; else i2c=10; fi
dt=`vcgencmd get_camera | grep "detected=1"`
# Ignore the result of vcgencmd get_camera as it claims no camera is installed if the tc358743 dtoverlay is loaded
if [ "`which i2cdetect`" = "" ]; then
  echo "i2cdetect not installed" ; exit
fi
if [ "`which camera_i2c`" = "" ]; then
  echo "camera_i2c not installed" ; exit
fi
camera_i2c >/dev/null 2>&1
v1=`i2cdetect -y $i2c 0x36 0x36 | grep " 36"`
v2=`i2cdetect -y $i2c 0x10 0x10 | grep " 10"`
hq=`i2cdetect -y $i2c 0x1a 0x1a | grep " 1a"`
tc358743hdmicapture=`i2cdetect -y $i2c 0x0f 0x0f | grep " 0f\| UU"`
if [ "$v1" != "" ]; then cam="v1 camera"; fi
if [ "$v2" != "" ]; then cam="v2 camera"; fi
if [ "$hq" != "" ]; then cam="hq camera"; fi
if [ "$tc358743hdmicapture" != "" ]; then cam="tc358743 hdmi capture"; fi
if [ "$cam" != "" ]; then
        echo "$cam found"
else
        echo "no camera detected"
fi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment