r4544 - developers/werner/poke

werner at docs.openmoko.org werner at docs.openmoko.org
Sun Jul 20 22:34:37 CEST 2008


Author: werner
Date: 2008-07-20 22:34:37 +0200 (Sun, 20 Jul 2008)
New Revision: 4544

Modified:
   developers/werner/poke/poke.c
Log:
Added word size selection.



Modified: developers/werner/poke/poke.c
===================================================================
--- developers/werner/poke/poke.c	2008-07-20 20:07:34 UTC (rev 4543)
+++ developers/werner/poke/poke.c	2008-07-20 20:34:37 UTC (rev 4544)
@@ -15,12 +15,13 @@
 #include <stdint.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <string.h>
 #include <fcntl.h>
 #include <sys/mman.h>
 
 
 #define PAGE_SIZE 4096
-#define MEM(addr) (*(uint32_t *) (mem+((addr) & (PAGE_SIZE-1))))
+#define MEM(bits, addr) (*(uint##bits##_t *) (mem+((addr) & (PAGE_SIZE-1))))
 
 static volatile void *mem;
 
@@ -30,7 +31,7 @@
 
 static void __attribute__((noreturn)) usage(const char *name)
 {
-	fprintf(stderr, "usage: %s hex_address [value]\n", name);
+	fprintf(stderr, "usage: %s [-8|-16|-32] hex_address [value]\n", name);
 	exit(1);
 }
 
@@ -41,20 +42,34 @@
 	char *end;
 	unsigned long addr;
 	uint32_t val;
+	int bits = 32;
+	char **args = argv+1;
 
 	fd = open("/dev/mem", O_RDWR);
         if (fd < 0) {
 		perror("/dev/mem");
 		exit(1);
 	}
+	if (argc > 1 && *argv[1] == '-') {
+		if (!strcmp(argv[1], "-8"))
+			bits = 8;
+		else if (!strcmp(argv[1], "-16"))
+			bits = 16;
+		else if (!strcmp(argv[1], "-32"))
+			bits = 32;
+		else
+		    usage(*argv);
+		args++;
+		argc--; /* dirty */
+	}
 	switch (argc) {
 	case 3:
-		val = strtoul(argv[2], &end, 0);
+		val = strtoul(args[1], &end, 0);
 		if (*end)
 			usage(*argv);
 		/* fall through */
 	case 2:
-		addr = strtoul(argv[1], &end, 16);
+		addr = strtoul(args[0], &end, 16);
 		if (*end)
 			usage(*argv);
 		break;
@@ -67,9 +82,31 @@
 		perror("mmap");
 		exit(1);
 	}
-	if (argc == 2)
-		printf("0x%08lx\n", (unsigned long) MEM(addr));
-	else
-		MEM(addr) = val;
+	if (argc == 2) {
+		switch (bits) {
+		case 8:
+			printf("0x%02lx\n", (unsigned long) MEM(8, addr));
+			break;
+		case 16:
+			printf("0x%04lx\n", (unsigned long) MEM(16, addr));
+			break;
+		case 32:
+			printf("0x%08lx\n", (unsigned long) MEM(32, addr));
+			break;
+		}
+	}
+	else {
+		switch (bits) {
+		case 8:
+			MEM(8, addr) = val;
+			break;
+		case 16:
+			MEM(16, addr) = val;
+			break;
+		case 32:
+			MEM(32, addr) = val;
+			break;
+		}
+	}
 	return 0;
 }





More information about the commitlog mailing list