shithub: util

Download patch

ref: 542129a476c8222b0b1bc07a29cc9b5d612e1162
parent: 3716520dbca0010ad5456de51365b79261054968
author: glenda <glenda@kingship>
date: Tue Oct 28 18:45:46 EDT 2025

ads7830

--- /dev/null
+++ b/ads7830.c
@@ -1,0 +1,126 @@
+#include <u.h>
+#include <libc.h>
+#include <fcall.h>
+#include <thread.h>
+#include <9p.h>
+
+#define DATA "%s/i2c.%x.data"
+#define CTL "%s/i2c.%x.ctl"
+
+char *i2cdir = "/dev/i2c1";
+
+enum {
+	Addr=	0x48,
+		Diff01=	0x00,
+		Diff23,
+		Diff45,
+		Diff67,
+		Diff10,
+		Diff32,
+		Diff54,
+		Diff76,
+		Single0,
+		Single2,
+		Single4,
+		Single6,
+		Single8,
+		Single1,
+		Single3,
+		Single5,
+		Single7,
+};
+
+void
+dataread(char *reg, int r, char *buf, int n)
+{
+	char *fname;
+	int fd;
+
+	fname = smprint(DATA, i2cdir, Addr);
+	if (fname == nil)
+		sysfatal("smprint: %r");
+
+	fd = open(fname, ORDWR);
+	if (fd == -1)
+		sysfatal("open: %r");
+
+	free(fname);
+
+	if (write(fd, reg, r) != r)
+		sysfatal("write: %r");
+
+	if (read(fd, buf, n) != n)
+		sysfatal("read: %r");
+
+	close(fd);
+}
+
+uchar
+readch(uchar ch)
+{
+	char cmd;
+	char value;
+
+	if (ch > 7)
+		return 0;
+
+	cmd = 0;
+	if ((ch % 2) == 0)
+		cmd |= (Single0 + (ch / 2));
+	else
+		cmd |= (Single1 + ((ch - 1) / 2));
+	cmd <<= 4;
+	cmd |= 0x03;
+
+	dataread(&cmd, 1, &value, 1);
+
+	return (uchar)value;
+}
+
+void
+fsread(Req *r)
+{
+	int ch;
+	uchar value;
+	char *buf;
+
+	switch ((intptr)(r->fid->file->aux)) {
+	case 1:
+	case 2:
+	case 3:
+	case 4:
+	case 5:
+	case 6:
+	case 7:
+	case 8:
+		ch = ((intptr)(r->fid->file->aux) - 1);
+		value = readch((uchar)ch);
+		buf = smprint("%4d\n", value);
+
+		readstr(r, buf);
+		respond(r, nil);
+
+		free(buf);
+		break;
+	default:
+		respond(r, "error");
+		break;
+	}
+}
+
+Srv fs = {
+.read	= fsread,
+};
+
+void
+main()
+{
+	int i;
+	char buf[32];
+	fs.tree = alloctree("ads7830", "ads7830", DMDIR|0555, nil);
+	for (i = 0; i < 8; i++) {
+		snprint(buf, 32, "adc%d", i);
+		createfile(fs.tree->root, buf, "ads7830", 0444, (void*)(i+1));
+	}
+	postmountsrv(&fs, "ads7830", "/dev", MBEFORE);
+}
--