| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | #include <stdio.h> |
|---|
| 35 | #include <stdlib.h> |
|---|
| 36 | #include <unistd.h> |
|---|
| 37 | #include <fcntl.h> |
|---|
| 38 | #include <errno.h> |
|---|
| 39 | #include <stdarg.h> |
|---|
| 40 | #include <time.h> |
|---|
| 41 | |
|---|
| 42 | #include <sys/ioctl.h> |
|---|
| 43 | #include <linux/ppdev.h> |
|---|
| 44 | #include <linux/parport.h> |
|---|
| 45 | |
|---|
| 46 | #include <sys/time.h> // debugging |
|---|
| 47 | |
|---|
| 48 | #define PARPORT_DEVICE "/dev/parport0" |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | #define DPRINTF(bla...) { if(debug_flag) fprintf(stderr,bla); } |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | unsigned char debug_flag; |
|---|
| 60 | size_t write_puncher(int fd, unsigned char buf); |
|---|
| 61 | void visualize_byte(unsigned char buf); |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | int mtime(long long since) { |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | struct timeval time; |
|---|
| 69 | gettimeofday(&time, NULL); |
|---|
| 70 | |
|---|
| 71 | return (time.tv_sec * 1000 + (long long)(time.tv_usec / 1000)) - since; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | int u_time(long long since) { |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | struct timeval time; |
|---|
| 79 | gettimeofday(&time, NULL); |
|---|
| 80 | |
|---|
| 81 | return (time.tv_sec * 1000 * 1000 + time.tv_usec) - since; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | int main(int argc, char **argv) { |
|---|
| 86 | int parport_fd; |
|---|
| 87 | int mode; |
|---|
| 88 | long long time_since_beginning; |
|---|
| 89 | int x; |
|---|
| 90 | |
|---|
| 91 | fprintf(stderr, "FER 201 Paper tape reader user space driver\n"); |
|---|
| 92 | fprintf(stderr, "time ms |123.45678| hex=dec\n"); |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | debug_flag = 0; |
|---|
| 96 | opterr = 0; |
|---|
| 97 | int c; |
|---|
| 98 | while( (c = getopt(argc, argv, "dh")) != -1) |
|---|
| 99 | switch(c) { |
|---|
| 100 | case 'd': |
|---|
| 101 | debug_flag = 1; |
|---|
| 102 | break; |
|---|
| 103 | case 'h': |
|---|
| 104 | fprintf(stderr, "Usage: program [-d] [-h]\n"); |
|---|
| 105 | return 0; |
|---|
| 106 | |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | DPRINTF("opening device...\n"); |
|---|
| 110 | parport_fd = open(PARPORT_DEVICE, O_RDWR); |
|---|
| 111 | if(parport_fd == -1) { |
|---|
| 112 | perror("opening device failed"); |
|---|
| 113 | return 1; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | DPRINTF("claiming port...\n"); |
|---|
| 118 | if(ioctl(parport_fd, PPCLAIM)) { |
|---|
| 119 | perror ("claiming port (PPCLAIM)"); |
|---|
| 120 | close (parport_fd); |
|---|
| 121 | return 1; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | DPRINTF("Setting IEEE netogation...\n"); |
|---|
| 134 | mode = IEEE1284_MODE_COMPAT; |
|---|
| 135 | if(ioctl(parport_fd, PPNEGOT, &mode)) { |
|---|
| 136 | perror ("Setting compatibilty mode (PPNEGOT)"); |
|---|
| 137 | close (parport_fd); |
|---|
| 138 | return 1; |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | DPRINTF("Setting off data line drivers..\n"); |
|---|
| 142 | mode = 1; |
|---|
| 143 | if(ioctl(parport_fd, PPDATADIR, &mode)) { |
|---|
| 144 | perror("Setting PPDATADIR"); |
|---|
| 145 | close(parport_fd); |
|---|
| 146 | return 1; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | DPRINTF("Resetting reader...\n"); |
|---|
| 150 | { |
|---|
| 151 | unsigned char mask = (PARPORT_CONTROL_STROBE|PARPORT_CONTROL_AUTOFD); |
|---|
| 152 | if(ioctl(parport_fd, PPWCONTROL, &mask)) |
|---|
| 153 | return -3; |
|---|
| 154 | usleep(20); |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | time_since_beginning = mtime(0); |
|---|
| 170 | for(x=0;;x++) { |
|---|
| 171 | unsigned char buf; |
|---|
| 172 | int ret; |
|---|
| 173 | |
|---|
| 174 | ret = read_byte(parport_fd, &buf); |
|---|
| 175 | if(ret < 0) { |
|---|
| 176 | printf("read byte: %i!", ret); |
|---|
| 177 | |
|---|
| 178 | close(parport_fd); |
|---|
| 179 | return 1; |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | fprintf(stderr, "%5ims ", (int)(mtime(time_since_beginning))); |
|---|
| 184 | visualize_byte(buf); |
|---|
| 185 | fprintf(stderr, " 0x%02x=%03i\n", buf, buf); |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | putchar((int)buf); |
|---|
| 189 | fflush(stdout); |
|---|
| 190 | |
|---|
| 191 | usleep(10*1000); |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | DPRINTF("finished\n"); |
|---|
| 195 | ioctl(parport_fd, PPRELEASE); |
|---|
| 196 | close(parport_fd); |
|---|
| 197 | return 0; |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | void visualize_byte(unsigned char buf) { |
|---|
| 201 | |
|---|
| 202 | unsigned char against = 0x01; |
|---|
| 203 | unsigned char check; |
|---|
| 204 | int pos; |
|---|
| 205 | |
|---|
| 206 | fprintf(stderr, "|"); |
|---|
| 207 | for(pos=0; pos < 8; pos++) { |
|---|
| 208 | if(pos == 3) |
|---|
| 209 | fprintf(stderr, "."); |
|---|
| 210 | check = buf; |
|---|
| 211 | check >>= pos; |
|---|
| 212 | |
|---|
| 213 | if((check & against) == 0) |
|---|
| 214 | fprintf(stderr, " "); |
|---|
| 215 | else |
|---|
| 216 | fprintf(stderr, "*"); |
|---|
| 217 | } |
|---|
| 218 | fprintf(stderr, "|"); |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | int read_byte(int fd, unsigned char *byte) { |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | static unsigned char mask_strobe = (PARPORT_CONTROL_STROBE|PARPORT_CONTROL_INIT); |
|---|
| 228 | static unsigned char mask_null = (0x00); |
|---|
| 229 | unsigned char status; |
|---|
| 230 | long long time; |
|---|
| 231 | int x; |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | if(ioctl(fd, PPRSTATUS, &status)) |
|---|
| 235 | return -5; |
|---|
| 236 | if(!(status & PARPORT_STATUS_SELECT)) { |
|---|
| 237 | DPRINTF("Warte, bis Schalter geschlossen ist"); |
|---|
| 238 | while(!(status & PARPORT_STATUS_SELECT)) { |
|---|
| 239 | if(ioctl(fd, PPRSTATUS, &status)) |
|---|
| 240 | return -5; |
|---|
| 241 | DPRINTF("."); |
|---|
| 242 | sleep(1); |
|---|
| 243 | } |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | if(ioctl(fd, PPWCONTROL, &mask_strobe)) |
|---|
| 248 | return -3; |
|---|
| 249 | time = u_time(0); |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | for(x=0;;) { |
|---|
| 253 | |
|---|
| 254 | if(ioctl(fd, PPRSTATUS, &status)) |
|---|
| 255 | return -4; |
|---|
| 256 | if(!(status & PARPORT_STATUS_ACK)) { |
|---|
| 257 | |
|---|
| 258 | if(!x) { |
|---|
| 259 | if(u_time(time) > 4*1000) { |
|---|
| 260 | DPRINTF("Lesesignal erst nach 4ms!\n"); |
|---|
| 261 | } |
|---|
| 262 | ioctl(fd, PPRDATA, byte); |
|---|
| 263 | ioctl(fd, PPWCONTROL, &mask_null); |
|---|
| 264 | x++; |
|---|
| 265 | } |
|---|
| 266 | } else if(u_time(time) > 9*1000) { |
|---|
| 267 | DPRINTF("Lesesignal kommt nicht!\n"); |
|---|
| 268 | usleep(2); |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | if(!(status & PARPORT_STATUS_BUSY)) { |
|---|
| 272 | |
|---|
| 273 | if(u_time(time) > 10*1000) { |
|---|
| 274 | break; |
|---|
| 275 | } |
|---|
| 276 | } else if(u_time(time) > 90*1000) { |
|---|
| 277 | DPRINTF("Busy missing nach 9ms!\n"); |
|---|
| 278 | DPRINTF("BEende.\n"); |
|---|
| 279 | exit(0); |
|---|
| 280 | } |
|---|
| 281 | |
|---|
| 282 | if(!(status & PARPORT_STATUS_SELECT)) |
|---|
| 283 | DPRINTF("Schalter offen! "); |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | if(0==1) { |
|---|
| 287 | struct timespec a; |
|---|
| 288 | a.tv_sec = 0; |
|---|
| 289 | a.tv_nsec = 1000; |
|---|
| 290 | nanosleep(&a, NULL); |
|---|
| 291 | } |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | |
|---|
| 297 | |
|---|
| 298 | |
|---|
| 299 | |
|---|
| 300 | |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | |
|---|
| 310 | |
|---|
| 311 | |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | |
|---|
| 316 | |
|---|
| 317 | |
|---|
| 318 | |
|---|
| 319 | |
|---|
| 320 | |
|---|
| 321 | |
|---|
| 322 | |
|---|
| 323 | return 0; |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| 326 | |
|---|
| 327 | |
|---|