#include #include #include #include #include #include #include /******************************************************************/ static unsigned char *xdio_start; static unsigned char *xdio_r0; static unsigned char *xdio_r1; static unsigned char *xdio_r2; static unsigned char *xdio_r3; static unsigned short *ptr16; #define TV_ELAPSED_US(x, y) ((((x).tv_sec - (y).tv_sec) * 1000000) + \ ((x).tv_usec - (y).tv_usec)) /*********************************************************************/ long calc_dif(long count, long last) { if (last > count) { // Ok, we rolled over return count + 65536 - last; } else { return count - last; } } /*********************************************************************/ /******************************************************************/ int main(int argc, char *argv[]) { int fd = open("/dev/mem", O_RDWR|O_SYNC); //xdio at 0x12c00000-0x12c00003 xdio_start = (unsigned char *) mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x12C00000); xdio_r0 = (unsigned char *) (xdio_start + 0); xdio_r1 = (unsigned char *) (xdio_start + 1); xdio_r2 = (unsigned char *) (xdio_start + 2); xdio_r3 = (unsigned char *) (xdio_start + 3); ptr16 = (unsigned short *) xdio_r2; *xdio_r0 = 0x40; // ?? give me the value to write here *xdio_r1 = 0x00; // ?? give me the value to write here /*** *xdio_r0 = 0x82; *xdio_r1 = 0x00; for discrete outputs *xdio_r0 = 0x00; *xdio_r1 = 0xFF; ****/ printf("Wrote, r0 = 0x43, r1 = 0x00\n"); struct timeval now; struct timeval last_time; gettimeofday(&last_time, NULL); unsigned short last_count = *ptr16; while (1) { sleep(1); gettimeofday(&now, NULL); unsigned short count16 = *ptr16; int elapsed_time = TV_ELAPSED_US(now, last_time); long n_pulses = calc_dif((long) count16, (long) last_count); double frequency = ((double) n_pulses) / (((double) elapsed_time) / 1000000); unsigned char byte0 = *xdio_r2; unsigned char byte1 = *xdio_r3; printf("--- %02hhx %02hhx %02hhx %02hhx - %hx %hu %d %d %0.0lf\n", xdio_r0, *xdio_r1, byte0, byte1, count16, count16, n_pulses, elapsed_time, frequency); last_count = count16; last_time = now; } } /******************************************************************/