FreeCalypso > hg > freecalypso-hwlab
changeset 28:de3d3cfcbb35
lcdtest: lcdphone program put together, compiles
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sun, 27 May 2018 22:26:58 +0000 |
parents | 4b7cac119fb5 |
children | 60bcd401363a |
files | .hgignore lcdtest/Makefile lcdtest/connect.c lcdtest/interf.c lcdtest/memops.c lcdtest/phonemain.c lcdtest/testmain.c |
diffstat | 7 files changed, 501 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/.hgignore Sun May 27 21:49:46 2018 +0000 +++ b/.hgignore Sun May 27 22:26:58 2018 +0000 @@ -6,4 +6,5 @@ ^ee2232/ee2232-prog$ ^ee2232/ee2232-read$ +^lcdtest/lcdphone$ ^lcdtest/lcdtest$
--- a/lcdtest/Makefile Sun May 27 21:49:46 2018 +0000 +++ b/lcdtest/Makefile Sun May 27 22:26:58 2018 +0000 @@ -1,12 +1,18 @@ CC= gcc -CFLAGS= -O2 -PROGS= lcdtest +CFLAGS= -O2 -I/opt/freecalypso/include +PROGS= lcdphone lcdtest + +LCDPHONE_OBJS= busops.o connect.o haoran.o interf.o memops.o phonemain.o \ + startek.o LCDTEST_OBJS= busops.o commands.o dispatch.o haoran.o readops.o showppm.o \ startek.o testmain.o all: ${PROGS} +lcdphone: ${LCDPHONE_OBJS} + ${CC} -o $@ ${LCDPHONE_OBJS} -lftdi + lcdtest: ${LCDTEST_OBJS} ${CC} -o $@ ${LCDTEST_OBJS} -lftdi
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lcdtest/connect.c Sun May 27 22:26:58 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 "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/lcdtest/interf.c Sun May 27 22:26:58 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 "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/lcdtest/memops.c Sun May 27 22:26:58 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 "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); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lcdtest/phonemain.c Sun May 27 22:26:58 2018 +0000 @@ -0,0 +1,132 @@ +#include <sys/types.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <strings.h> +#include <unistd.h> +#include <ftdi.h> +#include <rvinterf/etm.h> +#include <rvinterf/localtypes.h> +#include "exitcodes.h" + +extern int init_haoran(); +extern int init_startek(); + +extern char *socket_pathname; + +char *device_serial, *display_type; +struct ftdi_context ftdi; +int (*lcd_init_func)(); +u32 framebuffer_base_addr; + +process_cmdline(argc, argv) + char **argv; +{ + int c; + extern int optind; + extern char *optarg; + + while ((c = getopt(argc, argv, "d:s:")) != EOF) { + switch (c) { + case 'd': + device_serial = optarg; + continue; + case 's': + socket_pathname = optarg; + continue; + default: + /* error msg already printed */ + exit(ERROR_USAGE); + } + } + if (argc != optind + 2) { + fprintf(stderr, + "usage: %s [options] display_type framebuffer_base_addr\n", + argv[0]); + exit(ERROR_USAGE); + } + display_type = argv[optind]; + if (!strcmp(display_type, "haoran")) + lcd_init_func = init_haoran; + else if (!strcmp(display_type, "startek")) + lcd_init_func = init_startek; + else { + fprintf(stderr, "error: display type \"%s\" unknown\n", + display_type); + exit(ERROR_USAGE); + } + framebuffer_base_addr = strtoul(argv[optind+1], 0, 16); +} + +ftdi_setup() +{ + ftdi_init(&ftdi); + if (ftdi_usb_open_desc(&ftdi, 0x0403, 0x7157, 0, device_serial) < 0) { + fprintf(stderr, "FTDI USB open failed: %s\n", ftdi.error_str); + exit(ERROR_FTDI); + } + if (ftdi_set_bitmode(&ftdi, 0, BITMODE_MCU) < 0) { + fprintf(stderr, "unable to enter MCU mode: %s\n", + ftdi.error_str); + exit(ERROR_FTDI); + } +} + +lcd_init() +{ + reset_pulse(); + usleep(50000); + lcd_init_func(); + usleep(50000); + write_ir(0x20); + write_dr(0); + write_ir(0x21); + write_dr(0); + write_ir(0x22); + set_gpio_pins(1, 1); +} + +fb_poll() +{ + unsigned row, col; + u_char membuf[176*2], *sp; + u_char ftbuf[176*6], *dp; + int rc; + + for (row = 0; row < 220; row++) { + rc = do_memory_read_32(framebuffer_base_addr + row * 356, + membuf, 44); + if (rc) + exit(rc); + rc = do_memory_read_32(framebuffer_base_addr + row * 356 + 176, + membuf + 176, 44); + if (rc) + exit(rc); + sp = membuf; + dp = ftbuf; + for (col = 0; col < 176; col++) { + /* upper byte */ + *dp++ = 0x92; + *dp++ = 0; /* dummy addr */ + *dp++ = sp[1]; + /* lower byte */ + *dp++ = 0x92; + *dp++ = 0; /* dummy addr */ + *dp++ = sp[0]; + sp += 2; + } + do_ftdi_write(ftbuf, sizeof ftbuf); + } +} + +main(argc, argv) + char **argv; +{ + process_cmdline(argc, argv); + ftdi_setup(); + connect_local_socket(); + lcd_init(); + + for (;;) + fb_poll(); +}