samples: driver: i2c scanner

Scans i2c bus for any devices present.

Signed-off-by: Tavish Naruka <tavishnaruka@gmail.com>
diff --git a/samples/drivers/i2c_scanner/CMakeLists.txt b/samples/drivers/i2c_scanner/CMakeLists.txt
new file mode 100644
index 0000000..92afaba
--- /dev/null
+++ b/samples/drivers/i2c_scanner/CMakeLists.txt
@@ -0,0 +1,6 @@
+cmake_minimum_required(VERSION 3.8.2)
+include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
+project(i2c_scanner)
+
+FILE(GLOB app_sources src/*.c)
+target_sources(app PRIVATE ${app_sources})
diff --git a/samples/drivers/i2c_scanner/README.rst b/samples/drivers/i2c_scanner/README.rst
new file mode 100644
index 0000000..93cdf42
--- /dev/null
+++ b/samples/drivers/i2c_scanner/README.rst
@@ -0,0 +1,27 @@
+.. _i2c_scanner:
+
+I2C Scanner sample
+##################
+
+Overview
+********
+This sample sends I2C messages without any data (i.e. stop condition
+after sending just the address). If there is an ACK for the
+address, it prints the address as ``FOUND``.
+
+.. warning:: As  there  is  no  standard I2C detection command, this sample
+   uses arbitrary SMBus commands (namely SMBus quick write and SMBus
+   receive byte) to probe for devices.  This sample program can confuse
+   your I2C bus, cause data loss, and is known to corrupt
+   the Atmel AT24RF08 EEPROM found on many IBM Thinkpad laptops.
+   See also the `i2cdetect man page
+   <http://manpages.ubuntu.com/manpages/bionic/man8/i2cdetect.8.html>`_
+
+Building and Running
+********************
+.. zephyr-app-commands::
+   :zephyr-app: samples/drivers/i2c_scanner
+   :board: nrf52840_blip
+   :conf: "prj.conf overlay-nrf52.conf"
+   :goals: build flash
+
diff --git a/samples/drivers/i2c_scanner/overlay-nrf52.conf b/samples/drivers/i2c_scanner/overlay-nrf52.conf
new file mode 100644
index 0000000..822d475
--- /dev/null
+++ b/samples/drivers/i2c_scanner/overlay-nrf52.conf
@@ -0,0 +1,2 @@
+CONFIG_I2C_NRFX=y
+CONFIG_I2C_0=y
diff --git a/samples/drivers/i2c_scanner/prj.conf b/samples/drivers/i2c_scanner/prj.conf
new file mode 100644
index 0000000..cc1b04c
--- /dev/null
+++ b/samples/drivers/i2c_scanner/prj.conf
@@ -0,0 +1,2 @@
+CONFIG_PRINTK=y
+CONFIG_I2C=y
diff --git a/samples/drivers/i2c_scanner/sample.yaml b/samples/drivers/i2c_scanner/sample.yaml
new file mode 100644
index 0000000..9b14ce0
--- /dev/null
+++ b/samples/drivers/i2c_scanner/sample.yaml
@@ -0,0 +1,13 @@
+sample:
+  name: I2C scanner
+tests:
+  test:
+    platform_whitelist: nrf52840_blip nrf51_ble400
+    tags: drivers
+    depends_on: i2c
+    harness: console
+    harness_config:
+        type: one_line
+        regex:
+            - "0x50 FOUND"
+        fixture: fixture_i2c_FRAM
diff --git a/samples/drivers/i2c_scanner/src/main.c b/samples/drivers/i2c_scanner/src/main.c
new file mode 100644
index 0000000..1e467b3
--- /dev/null
+++ b/samples/drivers/i2c_scanner/src/main.c
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2018 Tavish Naruka <tavishnaruka@gmail.com>
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#include <errno.h>
+#include <zephyr.h>
+#include <misc/printk.h>
+#include <device.h>
+#include <i2c.h>
+
+#define I2C_DEV "I2C_0"
+
+/**
+ * @file This app scans I2C bus for any devices present
+ */
+
+void main(void)
+{
+	struct device *i2c_dev;
+
+	printk("Starting i2c scanner...\n");
+
+	i2c_dev = device_get_binding(I2C_DEV);
+	if (!i2c_dev) {
+		printk("I2C: Device driver not found.\n");
+		return;
+	}
+
+	for (u8_t i = 4; i < 0x77; i++) {
+		struct i2c_msg msgs[1];
+
+		/* Send the address to read from */
+		msgs[0].buf = NULL;
+		msgs[0].len = 0;
+		msgs[0].flags = I2C_MSG_WRITE | I2C_MSG_STOP;
+
+		if (i2c_transfer(i2c_dev, &msgs[0], 1, i) == 0) {
+			printk("0x%2x FOUND\n", i);
+		}
+	}
+}