changeset 3:06e900c54ae3

fc-lcdpoll program put together, compiles
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 14 Mar 2018 23:04:44 +0000
parents bff57443b0f7
children 839a63f0efda
files .hgignore lcdpoll/Makefile lcdpoll/connect.c lcdpoll/interf.c lcdpoll/launchrvif.c lcdpoll/main.c lcdpoll/memops.c
diffstat 7 files changed, 548 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Wed Mar 14 18:29:06 2018 +0000
+++ b/.hgignore	Wed Mar 14 23:04:44 2018 +0000
@@ -3,3 +3,4 @@
 \.[oa]$
 
 ^lcdemu/fc-lcdemu$
+^lcdpoll/fc-lcdpoll$
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lcdpoll/Makefile	Wed Mar 14 23:04:44 2018 +0000
@@ -0,0 +1,17 @@
+CC=	gcc
+CFLAGS=	-O2 -I/opt/freecalypso/include
+PROG=	fc-lcdpoll
+OBJS=	connect.o interf.o launchrvif.o main.o memops.o
+INSTBIN=/opt/freecalypso/bin
+
+all:	${PROG}
+
+${PROG}: ${OBJS}
+	${CC} ${CFLAGS} -o $@ ${OBJS}
+
+install:	${PROG}
+	mkdir -p ${INSTBIN}
+	install -c ${PROG} ${INSTBIN}
+
+clean:
+	rm -f *.o *.out *errs ${PROG}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lcdpoll/connect.c	Wed Mar 14 23:04:44 2018 +0000
@@ -0,0 +1,55 @@
+/*
+ * Connecting to an already running rvinterf process
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <stdio.h>
+#include <string.h>
+#include <strings.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <rvinterf/localsock.h>
+#include <rvinterf/exitcodes.h>
+
+char *socket_pathname = "/tmp/rvinterf_socket";
+int sock;
+
+connect_local_socket()
+{
+	/* local socket binding voodoo copied from osmocon */
+	struct sockaddr_un local;
+	unsigned int namelen;
+	int rc;
+
+	sock = socket(AF_UNIX, SOCK_STREAM, 0);
+	if (sock < 0) {
+		perror("socket(AF_UNIX, SOCK_STREAM, 0)");
+		exit(ERROR_UNIX);
+	}
+
+	local.sun_family = AF_UNIX;
+	strncpy(local.sun_path, socket_pathname, sizeof(local.sun_path));
+	local.sun_path[sizeof(local.sun_path) - 1] = '\0';
+
+	/* we use the same magic that X11 uses in Xtranssock.c for
+	 * calculating the proper length of the sockaddr */
+#if defined(BSD44SOCKETS) || defined(__UNIXWARE__)
+	local.sun_len = strlen(local.sun_path);
+#endif
+#if defined(BSD44SOCKETS) || defined(SUN_LEN)
+	namelen = SUN_LEN(&local);
+#else
+	namelen = strlen(local.sun_path) +
+		  offsetof(struct sockaddr_un, sun_path) + 1;
+#endif
+
+	rc = connect(sock, (struct sockaddr *) &local, namelen);
+	if (rc != 0) {
+		perror(socket_pathname);
+		exit(ERROR_RVINTERF);
+	}
+
+	return(0);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lcdpoll/interf.c	Wed Mar 14 23:04:44 2018 +0000
@@ -0,0 +1,156 @@
+/*
+ * In this module we implement our synchronous interface to the target
+ * via rvinterf.
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <rvinterf/limits.h>
+#include <rvinterf/localsock.h>
+#include <rvinterf/pktmux.h>
+#include <rvinterf/exitcodes.h>
+
+extern int sock;
+
+int rx_enable_state;
+u_char rvi_msg[LOCALSOCK_MAX_MSG];
+int rvi_msg_len;
+
+static void
+collect_bytes_from_rvi(buf, nbytes)
+	u_char *buf;
+{
+	int cc;
+
+	while (nbytes) {
+		cc = read(sock, buf, nbytes);
+		if (cc <= 0) {
+			perror("read from rvinterf socket");
+			exit(ERROR_RVINTERF);
+		}
+		buf += cc;
+		nbytes -= cc;
+	}
+}
+
+collect_rvi_msg()
+{
+	u_char lenbuf[2];
+
+	collect_bytes_from_rvi(lenbuf, 2);
+	rvi_msg_len = lenbuf[0] << 8 | lenbuf[1];
+	if (rvi_msg_len < 1 || rvi_msg_len > LOCALSOCK_MAX_MSG) {
+		fprintf(stderr, "Invalid length from rvinterf: %02X%02X\n",
+			lenbuf[0], lenbuf[1]);
+		exit(ERROR_RVINTERF);
+	}
+	collect_bytes_from_rvi(rvi_msg, rvi_msg_len);
+	return(0);
+}
+
+send_rvimisc_command(cmdpkt, cmdlen)
+	u_char *cmdpkt;
+{
+	u_char lenbuf[2];
+
+	lenbuf[0] = 0;
+	lenbuf[1] = cmdlen;
+	write(sock, lenbuf, 2);
+	write(sock, cmdpkt, cmdlen);
+}
+
+rx_control(enable)
+{
+	u_char cmdpkt[2];
+	int cmdlen;
+
+	/* are we already in the desired state? */
+	if (rx_enable_state == enable)
+		return(0);
+	/* no, do the work */
+	if (enable) {
+		cmdpkt[0] = CLI2RVI_WANT_MUXPROTO;
+		cmdpkt[1] = RVT_TM_HEADER;
+		cmdlen = 2;
+	} else {
+		cmdpkt[0] = CLI2RVI_RESET_PACKET_RX;
+		cmdlen = 1;
+	}
+	send_rvimisc_command(cmdpkt, cmdlen);
+	collect_rvi_msg();
+	if (rvi_msg[0] != RVI2CLI_LOCAL_CMD_RESP || rvi_msg_len < 2) {
+		fprintf(stderr,
+		"error: unexpected response to rvinterf local command\n");
+		exit(ERROR_RVINTERF);
+	}
+	if (rvi_msg[1] != '+') {
+		fprintf(stderr, "Error from rvinterf: %.*s\n", rvi_msg_len - 1,
+			rvi_msg + 1);
+		exit(ERROR_RVINTERF);
+	}
+	rx_enable_state = enable;
+	return(0);
+}
+
+send_pkt_to_target(pkt, pktlen)
+	u_char *pkt;
+{
+	u_char hdrbuf[3];
+	int len1;
+
+	len1 = pktlen + 1;
+	hdrbuf[0] = len1 >> 8;
+	hdrbuf[1] = len1 & 0xFF;
+	hdrbuf[2] = CLI2RVI_PKT_TO_TARGET;
+	write(sock, hdrbuf, 3);
+	write(sock, pkt, pktlen);
+}
+
+target_pkt_exch(outpkt, outpktlen)
+	u_char *outpkt;
+{
+	rx_control(1);
+	send_pkt_to_target(outpkt, outpktlen);
+	collect_rvi_msg();
+	if (rvi_msg[0] != RVI2CLI_PKT_FROM_TARGET) {
+		fprintf(stderr,
+			"error: unexpected response type from rvinterf\n");
+		exit(ERROR_RVINTERF);
+	}
+	return(0);
+}
+
+etm_pkt_exch(outbuf, outlen)
+	u_char *outbuf;
+{
+	int i, c;
+
+	outbuf[0] = RVT_TM_HEADER;
+	c = 0;
+	for (i = 1; i <= outlen; i++)
+		c ^= outbuf[i];
+	outbuf[i] = c;
+	target_pkt_exch(outbuf, outlen + 2);
+	if (rvi_msg[1] != RVT_TM_HEADER) {
+		printf("error: packet from target is not ETM!\n");
+		return(ERROR_TARGET);
+	}
+	if (rvi_msg_len < 5) {
+		printf("error: ETM response packet is too short\n");
+		return(ERROR_TARGET);
+	}
+	c = 0;
+	for (i = 2; i < rvi_msg_len; i++)
+		c ^= rvi_msg[i];
+	if (c) {
+		printf("ETM response checksum error!\n");
+		return(ERROR_TARGET);
+	}
+	if (rvi_msg[2] != outbuf[1]) {
+		printf("error: target response is from wrong ETM component\n");
+		return(ERROR_TARGET);
+	}
+	return(0);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lcdpoll/launchrvif.c	Wed Mar 14 23:04:44 2018 +0000
@@ -0,0 +1,63 @@
+/*
+ * This module implements the optional "behind the scenes" invokation
+ * of rvinterf from fc-fsio etc.
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <rvinterf/exitcodes.h>
+
+static char rvinterf_pathname[] = "/opt/freecalypso/bin/rvinterf";
+
+extern int sock;
+
+char *rvinterf_ttyport, *rvinterf_Bopt, *rvinterf_lopt, *rvinterf_wopt;
+
+launch_rvinterf()
+{
+	int sp[2], rc;
+	char *rvif_argv[11], Sarg[16], **ap;
+
+	rc = socketpair(AF_UNIX, SOCK_STREAM, 0, sp);
+	if (rc < 0) {
+		perror("socketpair");
+		exit(ERROR_UNIX);
+	}
+	sock = sp[0];
+	sprintf(Sarg, "-S%d", sp[1]);
+	ap = rvif_argv;
+	*ap++ = "rvinterf";
+	*ap++ = Sarg;
+	*ap++ = "-n";
+	if (rvinterf_Bopt) {
+		*ap++ = "-B";
+		*ap++ = rvinterf_Bopt;
+	}
+	if (rvinterf_lopt) {
+		*ap++ = "-l";
+		*ap++ = rvinterf_lopt;
+	}
+	if (rvinterf_wopt) {
+		*ap++ = "-w";
+		*ap++ = rvinterf_wopt;
+	}
+	*ap++ = rvinterf_ttyport;
+	*ap = 0;
+	rc = vfork();
+	if (rc < 0) {
+		perror("vfork for launching rvinterf");
+		exit(ERROR_UNIX);
+	}
+	if (!rc) {
+		/* we are in the child - do the exec */
+		close(sp[0]);
+		execv(rvinterf_pathname, rvif_argv);
+		perror(rvinterf_pathname);
+		_exit(1);
+	}
+	close(sp[1]);
+	return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lcdpoll/main.c	Wed Mar 14 23:04:44 2018 +0000
@@ -0,0 +1,108 @@
+/*
+ * This is the main module for fc-lcdpoll.
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <unistd.h>
+#include <rvinterf/etm.h>
+#include <rvinterf/localtypes.h>
+#include <rvinterf/exitcodes.h>
+
+extern char *socket_pathname;
+extern char *rvinterf_ttyport, *rvinterf_Bopt, *rvinterf_lopt, *rvinterf_wopt;
+
+u32 framebuffer_base_addr;
+
+emit_lcd_string(row, col, buf)
+	unsigned row, col;
+	u_char *buf;
+{
+	unsigned pos;
+
+	printf("%u %u ", row, col);
+	for (pos = 0; pos < 176; pos += 2)
+		printf("%02X%02X", buf[pos+1] ^ 0xFF, buf[pos] ^ 0xFF);
+}
+
+fb_poll()
+{
+	unsigned row;
+	u_char buf[176];
+	int rc;
+
+	for (row = 0; row < 220; row++) {
+		rc = do_memory_read_32(framebuffer_base_addr + row * 356,
+					buf, 44);
+		if (rc)
+			exit(rc);
+		emit_lcd_string(row, 0, buf);
+		rc = do_memory_read_32(framebuffer_base_addr + row * 356 + 176,
+					buf, 44);
+		if (rc)
+			exit(rc);
+		emit_lcd_string(row, 88, buf);
+		fflush(stdout);
+	}
+}
+
+main(argc, argv)
+	char **argv;
+{
+	extern int optind;
+	extern char *optarg;
+	int c, sopt = 0;
+
+	while ((c = getopt(argc, argv, "B:l:p:s:w:")) != EOF)
+		switch (c) {
+		case 'B':
+			rvinterf_Bopt = optarg;
+			continue;
+		case 'l':
+			rvinterf_lopt = optarg;
+			continue;
+		case 'p':
+			rvinterf_ttyport = optarg;
+			continue;
+		case 's':
+			socket_pathname = optarg;
+			sopt++;
+			continue;
+		case 'w':
+			rvinterf_wopt = optarg;
+			continue;
+		case '?':
+		default:
+			/* error msg already printed */
+			exit(ERROR_USAGE);
+		}
+	if (rvinterf_ttyport) {
+		if (sopt) {
+			fprintf(stderr,
+			"%s error: -p and -s options are mutually exclusive\n",
+				argv[0]);
+			exit(ERROR_USAGE);
+		}
+		launch_rvinterf();
+	} else {
+		if (rvinterf_Bopt || rvinterf_lopt || rvinterf_wopt) {
+			fprintf(stderr,
+"%s error: -B, -l and -w options are meaningful only when launching rvinterf\n",
+				argv[0]);
+			exit(ERROR_USAGE);
+		}
+		connect_local_socket();
+	}
+	if (argc != optind + 1) {
+		fprintf(stderr, "usage: %s [options] framebuffer_base_addr\n",
+			argv[0]);
+		exit(ERROR_USAGE);
+	}
+	framebuffer_base_addr = strtoul(argv[optind], 0, 16);
+
+	for (;;)
+		fb_poll();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lcdpoll/memops.c	Wed Mar 14 23:04:44 2018 +0000
@@ -0,0 +1,148 @@
+/*
+ * Functions for ETM memory read requests
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <rvinterf/etm.h>
+#include <rvinterf/limits.h>
+#include <rvinterf/localtypes.h>
+#include <rvinterf/exitcodes.h>
+
+#ifndef	MAX_MEMREAD_BYTES
+#define	MAX_MEMREAD_BYTES	238
+#endif
+
+#ifndef	MAX_MEMREAD_16BIT
+#define	MAX_MEMREAD_16BIT	119
+#endif
+
+#ifndef	MAX_MEMREAD_32BIT
+#define	MAX_MEMREAD_32BIT	59
+#endif
+
+extern u_char rvi_msg[];
+extern int rvi_msg_len;
+
+do_memory_read(memaddr, databuf, nbytes)
+	u32 memaddr;
+	u_char *databuf;
+{
+	u_char cmdpkt[10];
+	int rc;
+
+	if (nbytes > MAX_MEMREAD_BYTES) {
+		printf("error: # of bytes to read may not exceed %d\n",
+			MAX_MEMREAD_BYTES);
+		return(ERROR_USAGE);
+	}
+	cmdpkt[1] = ETM_CORE;
+	cmdpkt[2] = TMCORE_OPC_MEM;
+	cmdpkt[3] = 0x01;
+	cmdpkt[4] = nbytes;
+	cmdpkt[5] = memaddr;
+	cmdpkt[6] = memaddr >> 8;
+	cmdpkt[7] = memaddr >> 16;
+	cmdpkt[8] = memaddr >> 24;
+	rc = etm_pkt_exch(cmdpkt, 8);
+	if (rc)
+		return(rc);
+	if (rvi_msg[3]) {
+		printf("ETM error response to mem read request: 0x%02X\n",
+			rvi_msg[3]);
+		return(ERROR_TARGET);
+	}
+	if (rvi_msg_len != nbytes + 7) {
+		printf("error: mem read response has wrong length\n");
+		return(ERROR_TARGET);
+	}
+	if (rvi_msg[4] != TMCORE_OPC_MEM || rvi_msg[5] != 0x01) {
+		printf("error: mem read response has wrong opcode\n");
+		return(ERROR_TARGET);
+	}
+	bcopy(rvi_msg + 6, databuf, nbytes);
+	return(0);
+}
+
+do_memory_read_16(memaddr, databuf, nwords)
+	u32 memaddr;
+	u_char *databuf;
+{
+	u_char cmdpkt[10];
+	int rc;
+
+	if (nwords > MAX_MEMREAD_16BIT) {
+		printf("error: # of 16-bit words to read may not exceed %d\n",
+			MAX_MEMREAD_16BIT);
+		return(ERROR_USAGE);
+	}
+	cmdpkt[1] = ETM_CORE;
+	cmdpkt[2] = TMCORE_OPC_MEM;
+	cmdpkt[3] = 0x02;
+	cmdpkt[4] = nwords;
+	cmdpkt[5] = memaddr;
+	cmdpkt[6] = memaddr >> 8;
+	cmdpkt[7] = memaddr >> 16;
+	cmdpkt[8] = memaddr >> 24;
+	rc = etm_pkt_exch(cmdpkt, 8);
+	if (rc)
+		return(rc);
+	if (rvi_msg[3]) {
+		printf("ETM error response to mem read 16 request: 0x%02X\n",
+			rvi_msg[3]);
+		return(ERROR_TARGET);
+	}
+	if (rvi_msg_len != nwords * 2 + 7) {
+		printf("error: mem read 16 response has wrong length\n");
+		return(ERROR_TARGET);
+	}
+	if (rvi_msg[4] != TMCORE_OPC_MEM || rvi_msg[5] != 0x02) {
+		printf("error: mem read 16 response has wrong opcode\n");
+		return(ERROR_TARGET);
+	}
+	bcopy(rvi_msg + 6, databuf, nwords * 2);
+	return(0);
+}
+
+do_memory_read_32(memaddr, databuf, nwords)
+	u32 memaddr;
+	u_char *databuf;
+{
+	u_char cmdpkt[10];
+	int rc;
+
+	if (nwords > MAX_MEMREAD_32BIT) {
+		printf("error: # of 32-bit words to read may not exceed %d\n",
+			MAX_MEMREAD_32BIT);
+		return(ERROR_USAGE);
+	}
+	cmdpkt[1] = ETM_CORE;
+	cmdpkt[2] = TMCORE_OPC_MEM;
+	cmdpkt[3] = 0x04;
+	cmdpkt[4] = nwords;
+	cmdpkt[5] = memaddr;
+	cmdpkt[6] = memaddr >> 8;
+	cmdpkt[7] = memaddr >> 16;
+	cmdpkt[8] = memaddr >> 24;
+	rc = etm_pkt_exch(cmdpkt, 8);
+	if (rc)
+		return(rc);
+	if (rvi_msg[3]) {
+		printf("ETM error response to mem read 32 request: 0x%02X\n",
+			rvi_msg[3]);
+		return(ERROR_TARGET);
+	}
+	if (rvi_msg_len != nwords * 4 + 7) {
+		printf("error: mem read 32 response has wrong length\n");
+		return(ERROR_TARGET);
+	}
+	if (rvi_msg[4] != TMCORE_OPC_MEM || rvi_msg[5] != 0x04 && rvi_msg[5]) {
+		printf("error: mem read 32 response has wrong opcode\n");
+		return(ERROR_TARGET);
+	}
+	bcopy(rvi_msg + 6, databuf, nwords * 4);
+	return(0);
+}