OLD | NEW |
(Empty) | |
| 1 #include <unistd.h> |
| 2 #include <sys/types.h> |
| 3 #include <sys/stat.h> |
| 4 #include <fcntl.h> |
| 5 #include <stdio.h> |
| 6 #include <string.h> |
| 7 #include <errno.h> |
| 8 |
| 9 int |
| 10 getbyte(int fd, unsigned char *b) |
| 11 { |
| 12 return(read(fd, b, 1) == 1); |
| 13 } |
| 14 |
| 15 int |
| 16 putbyte(int fd, unsigned char b) |
| 17 { |
| 18 unsigned char ack; |
| 19 |
| 20 printf("write %02X\n", b); |
| 21 if (write(fd, &b, 1) != 1) { |
| 22 fprintf(stderr, "error write: %s\n", strerror(errno)); |
| 23 return 0; |
| 24 } |
| 25 |
| 26 if (!getbyte(fd, &ack)) { |
| 27 fprintf(stderr, "error read: %s\n", strerror(errno)); |
| 28 return 0; |
| 29 } |
| 30 printf("read %02X\n", ack); |
| 31 |
| 32 if (ack != 0xFA) { |
| 33 fprintf(stderr, "error ack\n"); |
| 34 return 0; |
| 35 } |
| 36 |
| 37 return 1; |
| 38 } |
| 39 |
| 40 int |
| 41 special_cmd(int fd, unsigned char cmd) |
| 42 { |
| 43 int i; |
| 44 |
| 45 if (putbyte(fd, 0xE6)) |
| 46 for (i = 0; i < 4; i++) { |
| 47 printf("special_cmd %i\n", i); |
| 48 if ((!putbyte(fd, 0xE8)) || (!putbyte(fd, (cmd>>6)&0x3))) |
| 49 return 0; |
| 50 cmd<<=2; |
| 51 } |
| 52 else |
| 53 return 0; |
| 54 return 1; |
| 55 } |
| 56 |
| 57 int |
| 58 send_cmd(int fd, unsigned char cmd) |
| 59 { |
| 60 return (special_cmd(fd, cmd) && |
| 61 putbyte(fd, 0xE9)); |
| 62 } |
| 63 |
| 64 int |
| 65 identify(int fd, unsigned long int *ident) |
| 66 { |
| 67 unsigned char id[3]; |
| 68 |
| 69 if (send_cmd(fd, 0x00) && |
| 70 getbyte(fd, &id[0]) && |
| 71 getbyte(fd, &id[1]) && |
| 72 getbyte(fd, &id[2])) { |
| 73 *ident = (id[0]<<16)|(id[1]<<8)|id[2]; |
| 74 printf("ident %06X\n", *ident); |
| 75 return 1; |
| 76 } else { |
| 77 fprintf(stderr, "error identify\n"); |
| 78 return 0; |
| 79 } |
| 80 } |
| 81 |
| 82 int |
| 83 reset(int fd) |
| 84 { |
| 85 unsigned char r[2]; |
| 86 |
| 87 if (!putbyte(fd, 0xFF)) { |
| 88 fprintf(stderr, "error reset\n"); |
| 89 return 0; |
| 90 } |
| 91 |
| 92 sleep(5); |
| 93 |
| 94 if (getbyte(fd, &r[0]) && getbyte(fd, &r[1])) |
| 95 if (r[0] == 0xAA && r[1] == 0x00) { |
| 96 fprintf(stderr, "reset done\n"); |
| 97 return 1; |
| 98 } |
| 99 fprintf(stderr, "error reset ack\n"); |
| 100 return 0; |
| 101 } |
| 102 |
| 103 int |
| 104 main(int argc, char* argv[]) |
| 105 { |
| 106 int fd; |
| 107 unsigned long int ident; |
| 108 |
| 109 fd = open("/dev/psaux", O_RDWR); |
| 110 if (fd == -1) { |
| 111 fprintf(stderr, "error open: %s\n", strerror(errno)); |
| 112 exit(0); |
| 113 } |
| 114 |
| 115 reset(fd); |
| 116 identify(fd, &ident); |
| 117 |
| 118 close(fd); |
| 119 |
| 120 exit(0); |
| 121 } |
OLD | NEW |