jtag-mpsse

JTAG tools for FTDI MPSSE transports
git clone http://frotz.net/git/jtag-mpsse.git
Log | Files | Refs

i2c.c (2819B)


      1 /* Copyright 2014 Brian Swetland <swetland@frotz.net>
      2  *
      3  * Licensed under the Apache License, Version 2.0 (the "License");
      4  * you may not use this file except in compliance with the License.
      5  * You may obtain a copy of the License at
      6  *
      7  *     http://www.apache.org/licenses/LICENSE-2.0
      8  *
      9  * Unless required by applicable law or agreed to in writing, software
     10  * distributed under the License is distributed on an "AS IS" BASIS,
     11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12  * See the License for the specific language governing permissions and
     13  * limitations under the License.
     14  */
     15 
     16 #include <stdio.h>
     17 #include <stdlib.h>
     18 #include <unistd.h>
     19 
     20 #include "jtag.h"
     21 #include "zynq.h"
     22 
     23 static JTAG *jtag;
     24 
     25 void jconnect(void) {
     26 	if (!(jtag = jtag_open())) {
     27 		fprintf(stderr, "jconnect: cannot open usb jtag ifc\n");
     28 		exit(-1);
     29 	}
     30 	if (jtag_enumerate(jtag) <= 0) {
     31 		fprintf(stderr, "jconnect: cannot enumerate jtag devices\n");
     32 		exit(-1);
     33 	}
     34 	if (jtag_select(jtag, 0x13722093)) {
     35 		fprintf(stderr, "jconnect: cannot connect to ZYNQ\n");
     36 		exit(-1);
     37 	}
     38 
     39 	jtag_ir_wr(jtag, XIL_USER4);
     40 }
     41 
     42 u32 jrd(void) {
     43 	u64 u;
     44 	jtag_dr_io(jtag, 32, 0, &u);
     45 	return (u32) u;
     46 }
     47 
     48 u32 jwr(u32 n) {
     49 	u64 u;
     50 	u = n;
     51 	u |= 0x100000000ULL;
     52 	jtag_dr_io(jtag, 33, u, &u);
     53 	return (u32) u;
     54 }
     55 
     56 //              0x00XX // wo - data to write
     57 //              0x00XX // ro - last data read
     58 #define STA	0x0100 // rw - issue start on next rd or wr
     59 #define STP	0x0200 // rw - issue stop
     60 #define WR	0x0400 // rw - issue write
     61 #define RD	0x0800 // rw - issue read
     62 #define RACK    0x1000 // wo - master ACK/NAK state for reads
     63 #define TIP	0x1000 // ro - transaction in progress
     64 #define ACK     0x2000 // ro - slave ACK/NAK received on write
     65 #define LOST    0x4000 // ro - arbitration lost
     66 #define BUSY    0x8000 // ro - I2C bus busy
     67 
     68 /* example:  ./i2c sw:a0 w:fa sw:a1 r r r r r r p  */
     69 
     70 int main(int argc, char **argv) {
     71 	unsigned n,x;
     72 	unsigned c;
     73 
     74 	argc--;
     75 	argv++;
     76 
     77 	jconnect();
     78 
     79 	while (argc > 0) {
     80 		char *cmd = argv[0];
     81 		n = 0;
     82 		while (*cmd) {
     83 			switch(*cmd) {
     84 			case 's': case 'S': n |= STA; break;
     85 			case 'p': case 'P': n |= STP; break;
     86 			case 'w': case 'W': n |= WR; break;
     87 			case 'r': case 'R': n |= RD; break;
     88 			case 'z': case 'Z': n |= RACK; break;
     89 			case ':':
     90 				cmd++;
     91 				n |= (strtoul(cmd, 0, 16) & 0xFF);
     92 				goto done;
     93 			default:
     94 				fprintf(stderr,"syntax error\n");
     95 				return -1;
     96 			}
     97 			cmd++;
     98 		}
     99 done:
    100 		jwr(n);
    101 		c = 1;
    102 		while ((x = jrd()) & TIP) {
    103 			c++;
    104 			if (c == 100000) {
    105 				fprintf(stderr,"timeout\n");
    106 				return -1;
    107 			}
    108 		}
    109 
    110 		fprintf(stderr, "%c%c%c%c %02x -> %02x %c%c%c (%d)\n",
    111 			(n&RD)?'R':'-', (n&WR)?'W':'-',
    112 			(n&STP)?'P':'-', (n&STA)?'S':'-',
    113 			n & 0xFF, x & 0xFF,
    114 			(x&ACK)?'N':'A', (x&LOST)?'L':'-',
    115 			(x&BUSY)?'B':'-', c);
    116 		argc--;
    117 		argv++;
    118 	}
    119 
    120 	return 0;
    121 }