shithub: util

Download patch

ref: c77b44ee5997cb85d2eeee3faef6fd3eabc4df0a
parent: 284d1ce2d7f4992ef2edee3eaaeeecca70e535cd
author: glenda <glenda@kingship>
date: Fri Oct 17 22:09:51 EDT 2025

mcp7940x 64 byte sram file

--- a/mcp7940x.c
+++ b/mcp7940x.c
@@ -190,6 +190,9 @@
 	char *data;
 	int sign;
 	int trim;
+	int offs;
+	int len;
+	int i;
 
 	clk[0] = 0;
 
@@ -223,7 +226,7 @@
 			rtc.year += 1900;
 
 		t = rtc2sec(&rtc);
-		snprint(buf, 256, "%12lud", t);
+		snprint(buf, 256, "%11lud ", t);
 		readstr(r, buf);
 		respond(r, nil);
 	} else if ((intptr)(r->fid->file->aux) == 2) {
@@ -249,9 +252,43 @@
 		trim <<= 1;
 		trim *= sign;
 
-		snprint(buf, 256, "%12d", trim);
+		snprint(buf, 256, "%11d ", trim);
 		readstr(r, buf);
 		respond(r, nil);
+	} else if ((intptr)(r->fid->file->aux) == 3) {
+		offs = r->ifcall.offset;
+
+		if (offs >= 64) {
+			respond(r, nil);
+			return;
+		}
+
+		len = r->ifcall.count;
+
+		if (len > 64)
+			len = 64;
+
+		len -= offs;
+
+		data = smprint(DATA, i2cdir);
+		if (data == nil)
+			sysfatal("smprint: %r");
+		fd = open(data, ORDWR);
+		if (fd < 0)
+			sysfatal("open: %r");
+		free(data);
+
+		for (i = 0; i < len; i++) {
+			clk[0] = i + (uchar)offs + 0x20;
+			write(fd, clk, 1);
+
+			read(fd, &buf[i], 1);
+		}
+
+		r->ofcall.count = len;
+
+		readbuf(r, buf, len);
+		respond(r, nil);
 	} else {
 		respond(r, "file not found");
 	}
@@ -271,6 +308,9 @@
 	char *data;
 	long trim;
 	uchar reg[2];
+	int offs;
+	int len;
+	int i;
 
 	if ((intptr)(r->fid->file->aux) == 1) {
 		p = r->ifcall.data;
@@ -347,14 +387,57 @@
 			sysfatal("write: %r");
 
 		close(fd);
+
+		r->ofcall.count = r->ifcall.count;
+
+		respond(r, nil);
+	} else if ((intptr)(r->fid->file->aux) == 3) {
+		p = r->ifcall.data;
+		len = r->ifcall.count;
+		if (len > 64)
+			len = 64;
+		offs = r->ifcall.offset;
+		if (offs > 64)
+			offs = 64;
+		len -= offs;
+
+		data = smprint(DATA, i2cdir);
+		if (data == nil)
+			sysfatal("smprint: %r");
+		fd = open(data, ORDWR);
+		if (fd < 0)
+			sysfatal("open: %r");
+		free(data);
+
+		for (i = 0; i < len; i++) {
+			reg[0] = i + offs + 0x20;
+			reg[1] = p[i];
+			write(fd, reg, 2);
+		}
+
+		close(fd);
+
+		r->ofcall.count = len;
+
+		respond(r, nil);
 	} else {
 		respond(r, "file not found");
 	}
 }
 
+void
+fsstat(Req *r)
+{
+	if ((intptr)(r->fid->file->aux) == 3)
+		r->d.length = 64;
+
+	respond(r, nil);
+}
+
 Srv fs = {
 .read	= fsread,
 .write	= fswrite,
+.stat	= fsstat,
 };
 
 void
@@ -365,7 +448,7 @@
 	uchar reg;
 	uchar buf[2];
 
-	ctl("size 10");
+	ctl("size 256");
 	ctl("subaddress 0");
 
 	data = smprint(DATA, i2cdir);
@@ -390,5 +473,6 @@
 	fs.tree = alloctree("mcp7940x", "mcp7940x", DMDIR|0555, nil);
 	createfile(fs.tree->root, "rtc", "mcp7940x", 0666, (void*)1);
 	createfile(fs.tree->root, "rtctrim", "mcp7940x", 0666, (void*)2);
+	createfile(fs.tree->root, "rtcsram", "mcp7940x", 0666, (void*)3);
 	postmountsrv(&fs, "mcp7940x", "/dev", MBEFORE);
 }
--