From 0a43d3cb35afc8c5ee77e310a91bfda62169b62f Mon Sep 17 00:00:00 2001
From: "Wesley W. Terpstra" <w.terpstra@gsi.de>
Date: Mon, 4 Nov 2013 11:14:44 +0100
Subject: [PATCH] w1: remove need for an ifdef

---
 dev/dev.mk           |  2 +-
 dev/w1-eeprom.c      | 64 +-------------------------------
 dev/w1-shell.c       | 87 ++++++++++++++++++++++++++++++++++++++++++++
 dev/w1.c             | 38 -------------------
 include/std/unistd.h |  2 +
 tools/Makefile       |  2 +-
 6 files changed, 92 insertions(+), 103 deletions(-)
 create mode 100644 dev/w1-shell.c
 create mode 100644 include/std/unistd.h

diff --git a/dev/dev.mk b/dev/dev.mk
index 77a2a54..b36fb03 100644
--- a/dev/dev.mk
+++ b/dev/dev.mk
@@ -12,7 +12,7 @@ obj-y += \
 obj-$(CONFIG_LEGACY_EEPROM) += dev/eeprom.o
 obj-$(CONFIG_SDB_EEPROM) += dev/sdb-eeprom.o
 
-obj-$(CONFIG_W1) +=		dev/w1.o	dev/w1-hw.o
+obj-$(CONFIG_W1) +=		dev/w1.o	dev/w1-hw.o	dev/w1-shell.o
 obj-$(CONFIG_W1) +=		dev/w1-temp.o	dev/w1-eeprom.o
 obj-$(CONFIG_UART) +=		dev/uart.o
 obj-$(CONFIG_UART_SW) +=	dev/uart-sw.o
diff --git a/dev/w1-eeprom.c b/dev/w1-eeprom.c
index aa167cc..0576573 100644
--- a/dev/w1-eeprom.c
+++ b/dev/w1-eeprom.c
@@ -4,14 +4,8 @@
  */
 #include <stdlib.h>
 #include <string.h>
-#include <w1.h>
-
-#ifndef EXTERNAL_W1
-#include <wrc.h>
-#include <shell.h>
-#else
 #include <unistd.h>
-#endif
+#include <w1.h>
 
 #define LSB_ADDR(X) ((X) & 0xFF)
 #define MSB_ADDR(X) (((X) & 0xFF00)>>8)
@@ -142,59 +136,3 @@ int w1_write_eeprom_bus(struct w1_bus *bus,
 	/* not found */
 	return -1;
 }
-
-#ifndef EXTERNAL_W1
-#define BLEN 32
-
-/* A shell command, for testing write: "w1w <offset> <byte> [<byte> ...]" */
-static int cmd_w1_w(const char *args[])
-{
-	int offset, i, blen;
-	unsigned char buf[BLEN];
-
-	if (!args[0] || !args[1])
-		return -1;
-	offset = atoi(args[0]);
-	for (i = 1, blen = 0; args[i] && blen < BLEN; i++, blen++) {
-		buf[blen] = atoi(args[i]);
-		pp_printf("offset %4i (0x%03x): %3i (0x%02x)\n",
-			  offset + blen, offset + blen, buf[blen], buf[blen]);
-	}
-	i = w1_write_eeprom_bus(&wrpc_w1_bus, offset, buf, blen);
-	pp_printf("write(0x%x, %i): result = %i\n", offset, blen, i);
-	return i == blen ? 0 : -1;
-}
-
-DEFINE_WRC_COMMAND(w1w) = {
-	.name = "w1w",
-	.exec = cmd_w1_w,
-};
-
-/* A shell command, for testing read: "w1r <offset> <len> */
-static int cmd_w1_r(const char *args[])
-{
-	int offset, i, blen;
-	unsigned char buf[BLEN];
-
-	if (!args[0] || !args[1])
-		return -1;
-	offset = atoi(args[0]);
-	blen = atoi(args[1]);
-	if (blen > BLEN)
-		blen = BLEN;
-	i = w1_read_eeprom_bus(&wrpc_w1_bus, offset, buf, blen);
-	pp_printf("read(0x%x, %i): result = %i\n", offset, blen, i);
-	if (i <= 0 || i > blen) return -1;
-	for (blen = 0; blen < i; blen++) {
-		pp_printf("offset %4i (0x%03x): %3i (0x%02x)\n",
-			  offset + blen, offset + blen, buf[blen], buf[blen]);
-	}
-	return i == blen ? 0 : -1;
-}
-
-DEFINE_WRC_COMMAND(w1r) = {
-	.name = "w1r",
-	.exec = cmd_w1_r,
-};
-
-#endif
diff --git a/dev/w1-shell.c b/dev/w1-shell.c
new file mode 100644
index 0000000..7481401
--- /dev/null
+++ b/dev/w1-shell.c
@@ -0,0 +1,87 @@
+/*
+ * Onewire generic interface
+ * Alessandro Rubini, 2013 GNU GPL2 or later
+ */
+#include <wrc.h>
+#include <shell.h>
+#include <w1.h>
+
+#define BLEN 32
+
+/* A shell command, for testing write: "w1w <offset> <byte> [<byte> ...]" */
+static int cmd_w1_w(const char *args[])
+{
+	int offset, i, blen;
+	unsigned char buf[BLEN];
+
+	if (!args[0] || !args[1])
+		return -1;
+	offset = atoi(args[0]);
+	for (i = 1, blen = 0; args[i] && blen < BLEN; i++, blen++) {
+		buf[blen] = atoi(args[i]);
+		pp_printf("offset %4i (0x%03x): %3i (0x%02x)\n",
+			  offset + blen, offset + blen, buf[blen], buf[blen]);
+	}
+	i = w1_write_eeprom_bus(&wrpc_w1_bus, offset, buf, blen);
+	pp_printf("write(0x%x, %i): result = %i\n", offset, blen, i);
+	return i == blen ? 0 : -1;
+}
+
+DEFINE_WRC_COMMAND(w1w) = {
+	.name = "w1w",
+	.exec = cmd_w1_w,
+};
+
+/* A shell command, for testing read: "w1r <offset> <len> */
+static int cmd_w1_r(const char *args[])
+{
+	int offset, i, blen;
+	unsigned char buf[BLEN];
+
+	if (!args[0] || !args[1])
+		return -1;
+	offset = atoi(args[0]);
+	blen = atoi(args[1]);
+	if (blen > BLEN)
+		blen = BLEN;
+	i = w1_read_eeprom_bus(&wrpc_w1_bus, offset, buf, blen);
+	pp_printf("read(0x%x, %i): result = %i\n", offset, blen, i);
+	if (i <= 0 || i > blen) return -1;
+	for (blen = 0; blen < i; blen++) {
+		pp_printf("offset %4i (0x%03x): %3i (0x%02x)\n",
+			  offset + blen, offset + blen, buf[blen], buf[blen]);
+	}
+	return i == blen ? 0 : -1;
+}
+
+DEFINE_WRC_COMMAND(w1r) = {
+	.name = "w1r",
+	.exec = cmd_w1_r,
+};
+
+/* A shell command, for checking */
+static int cmd_w1(const char *args[])
+{
+	int i;
+	struct w1_dev *d;
+	int32_t temp;
+
+	w1_scan_bus(&wrpc_w1_bus);
+	for (i = 0; i < W1_MAX_DEVICES; i++) {
+		d = wrpc_w1_bus.devs + i;
+		if (d->rom) {
+			pp_printf("device %i: %08x%08x\n", i,
+				  (int)(d->rom >> 32), (int)d->rom);
+		temp = w1_read_temp(d, 0);
+		pp_printf("temp: %d.%04d\n", temp >> 16,
+			  (int)((temp & 0xffff) * 10 * 1000 >> 16));
+		}
+	}
+	return 0;
+}
+
+DEFINE_WRC_COMMAND(w1) = {
+	.name = "w1",
+	.exec = cmd_w1,
+};
+
diff --git a/dev/w1.c b/dev/w1.c
index ccea416..0948ba2 100644
--- a/dev/w1.c
+++ b/dev/w1.c
@@ -4,14 +4,7 @@
  */
 #include <string.h>
 #include <w1.h>
-
-#ifndef EXTERNAL_W1
-#include <wrc.h>
-#include <shell.h>
-#else
 #include <unistd.h>
-#define pp_printf(...) do {} while (0)
-#endif
 
 static const struct w1_ops *ops = &wrpc_w1_ops; /* local shorter name */
 
@@ -136,7 +129,6 @@ int w1_scan_bus(struct w1_bus *bus)
 			/* error on this one */
 			return i;
 		}
-		pp_printf("W1: %08x%08x\n", (int)(d->rom >> 32), (int)d->rom);
 	}
 	return i;
 }
@@ -151,33 +143,3 @@ void w1_match_rom(struct w1_dev *dev)
 		w1_write_byte(dev->bus, (int)(dev->rom >> i) );
 	}
 }
-
-#ifndef EXTERNAL_W1
-
-/* A shell command, for checking */
-static int cmd_w1(const char *args[])
-{
-	int i;
-	struct w1_dev *d;
-	int32_t temp;
-
-	w1_scan_bus(&wrpc_w1_bus);
-	for (i = 0; i < W1_MAX_DEVICES; i++) {
-		d = wrpc_w1_bus.devs + i;
-		if (d->rom) {
-			pp_printf("device %i: %08x%08x\n", i,
-				  (int)(d->rom >> 32), (int)d->rom);
-		temp = w1_read_temp(d, 0);
-		pp_printf("temp: %d.%04d\n", temp >> 16,
-			  (int)((temp & 0xffff) * 10 * 1000 >> 16));
-		}
-	}
-	return 0;
-}
-
-DEFINE_WRC_COMMAND(w1) = {
-	.name = "w1",
-	.exec = cmd_w1,
-};
-
-#endif
diff --git a/include/std/unistd.h b/include/std/unistd.h
new file mode 100644
index 0000000..0306882
--- /dev/null
+++ b/include/std/unistd.h
@@ -0,0 +1,2 @@
+/* usleep */
+#include <syscon.h>
diff --git a/tools/Makefile b/tools/Makefile
index a3d7750..9d2d5f6 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -1,7 +1,7 @@
 EB    ?= no
 SDBFS ?= no
 
-CFLAGS = -Wall -ggdb -DEXTERNAL_W1 -I../include
+CFLAGS = -Wall -ggdb -I../include
 LDFLAGS = -lutil
 ALL    = genraminit genramvhd genrammif wrpc-uart-sw
 ALL   += wrpc-w1-read wrpc-w1-write
-- 
GitLab