?? testwatchdog.c
字號:
/* testwatchdog.c* test file for testing AT91RM9200 watchdog, deals with the Timer/Counter 1*/#include <getopt.h>#include <stdio.h>#include <stdlib.h>#include <pthread.h>#include <sched.h> #include <unistd.h>#include <string.h>#include <errno.h>#include <linux/termios.h>#include <fcntl.h>#include <asm/param.h>#include <asm/arch/hardware.h> /* get all the AT91 def's */#include <asm/arch/watchdog.h> /* get all the watchdog ioctl def's */ /* A string listing valid short options letters. */const char* program_name; /* The name of this program. */const char* const short_options = "t:w:d:v"; /* An array describing valid long options. */const struct option long_options[] = { { "timer", 1, NULL, 't' }, { "watchdog", 1, NULL, 'w' }, { "delay", 1, NULL, 'd' }, { "verbose", 0, NULL, 'v' }, { NULL, 0, NULL, 0 } /* Required at end of array. */ };void print_usage (FILE* stream, int exit_code){ fprintf (stream, "Usage: %s options\n", program_name); fprintf (stream, " -t --timer count read 32768 clock 'count' times. [0]\n" " -w --watchdog timeout set watchdog timeout. [5000]\n" " -d --delay microsecs set delay in microseconds. [0]\n" " -v --verbose Print verbose messages.\n"); exit (exit_code);}int main(int argc, char **argv) { struct at91_watchdog_ioctl_timer watchdog_timer; struct at91_watchdog_ioctl_set watchdog_set; int cntr, next_option; char *filename4 = "/dev/watchdog"; int file4;#define DEFAULT_TIMER_COUNT 0 int timer_count = DEFAULT_TIMER_COUNT;#define DEFAULT_WATCHDOG_TIMEOUT 32768 * 2 /* initial baud rate */ int watchdog_timeout = DEFAULT_WATCHDOG_TIMEOUT; int verbose = 0; /* be verbose? */ int set_watchdog_timeout = 0; int delay = 0; do { next_option = getopt_long (argc, argv, short_options, long_options, NULL); switch (next_option) { case 't': /* -t or --timer */ /* This option takes an argument, the initial baud rate */ timer_count = strtol (optarg, NULL, 0); break; case 'w': /* -i or --ibaud */ /* This option takes an argument, the initial baud rate */ watchdog_timeout = strtol (optarg, NULL, 0); set_watchdog_timeout = 1; break; case 'd': /* -d or --delay */ /* This option takes an argument, the initial baud rate */ delay = strtol (optarg, NULL, 0); break; case 'v': /* -v or --verbose */ verbose = 1; break; case '?': /* The user specified an invalid option. */ /* Print usage information to standard error, and exit with exit code one (indicating abonormal termination). */ print_usage (stderr, 1); break; case -1: /* Done with options. */ break; default: /* Something else: unexpected. */ abort (); } } while (next_option != -1); if (verbose) printf ("watchdog test program\n"); if (timer_count) /* doing timer checks? */ { unsigned int diff; unsigned int val_max = 0; unsigned int val_min = -1; long long *timer_values = malloc (timer_count * sizeof (long long));if ((file4 = open(filename4, O_RDWR)) < 0) { printf ("open failed %s\n", filename4); exit(1); } for (cntr = 0; cntr < timer_count; cntr++) { if (ioctl(file4, AT91_WATCHDOG_GET_32768_TIMER, &watchdog_timer) != 0) /* write the data */ { perror ("AT91_WATCHDOG_GET_32768_TIMER failed"); close (file4); exit (1); } timer_values[cntr] = watchdog_timer.timer_32768; if (cntr != 0 && (timer_values[cntr] - timer_values[cntr - 1] < 0)) { cntr++; break; } if (delay) usleep (delay); /* wait a bit */ } timer_count = cntr + 1; close (file4); for (cntr = 1; cntr < (timer_count - 1); cntr++) { diff = timer_values[cntr] - timer_values[cntr - 1]; if (diff < val_min) { printf ( "cntr = %d, diff = %d, last = %llx ticks, this = %llx ticks\n", cntr, diff, timer_values[cntr - 1], timer_values[cntr]); val_min = diff; } if (diff > val_max) { printf ( "cntr = %d, diff = %d, last = %llx ticks, this = %llx ticks\n", cntr, diff, timer_values[cntr - 1], timer_values[cntr]); val_max = diff; } } printf ( "total count = %d, max = %d ticks, min = %d ticks\n", timer_count, val_min, val_max); free (timer_values); } if ((file4 = open(filename4, O_RDWR)) < 0) { printf ("open failed %s\n", filename4); exit(1); } if (ioctl(file4, AT91_WATCHDOG_GET_32768_TIMER, &watchdog_timer) != 0) /* write the data */ { perror ("AT91_WATCHDOG_GET_32768_TIMER failed"); close (file4); exit (1); } printf ("[<%2lu:%3lu>]\n", (unsigned long) watchdog_timer.timer_32768 / 32768, (unsigned long) (watchdog_timer.timer_32768 % AT91_SLOW_CLOCK) / 33); if (set_watchdog_timeout) { watchdog_set.sram_watchdog_magic = 0xaa55aa55; /* pick a magic # */ watchdog_set.watchdog_wdmr = watchdog_timeout & AT91C_ST_WDV; /* WATCHDOG reg WDRM */ if (ioctl(file4, AT91_WATCHDOG_SET_WATCHDOG, &watchdog_set) != 0) /* write the data */ { perror ("AT91_WATCHDOG_SET_WATCHDOG failed"); close (file4); exit (1); } printf ("Watchdog, old magic number was 0x%x\n", watchdog_set.sram_watchdog_magic); /* pick a magic # */ } close (file4); exit(0); }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -