⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testtc.c

📁 AT91RM9200的定时器驱动(linux2.4内核)
💻 C
字号:
/* testtc.c* test file for testing AT91RM9200 tc, 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/tc.h>  /* get all the tc ioctl def's */const char* program_name;  /* The name of this program.  */  /* A string listing valid short options letters.  */const char* const short_options = "d:p:l:p:t:rswyfv";  /* An array describing valid long options.  */const struct option long_options[] = {    { "device",     1, NULL, 'd' },    { "pio",     1, NULL, 'i' },    { "loop",     1, NULL, 'l' },    { "period",      1, NULL, 'p' },    { "timeout",      1, NULL, 't' },    { "restart",   0, NULL, 'r' },    { "set",   0, NULL, 's' },    { "wait",   0, NULL, 'w' },    { "delay",   0, NULL, 'y' },    { "fast",   0, NULL, 'f' },    { "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,           "  -d  --device device       TC. [/dev/tc0]\n"           "  -i  --pio pio_pin         Set/clr pio pin.\n"           "  -l  --loop count          How many times to loop. [2]\n"           "  -p  --period value        Peroid in us. [8333]\n"           "  -t  --timeout value       Rcv timeout in bit times. [100]\n"           "  -r  --restart             Restart timer/counter upon delay.\n"           "  -s  --set_clock           Set the clock of a TC.\n"           "  -w  --wait                Test wait on period feature.\n"           "  -y  --delay               Delay for 1 second after 20 count.\n"           "  -f  --fast                Program 288 kHz fast clock out.\n"           "  -v  --verbose             Print verbose messages.\n");  exit (exit_code);}int main(int argc, char **argv)  {  int next_option;  unsigned int cntr;  struct at91_tc_ioctl_clock tc_clock;  struct at91_pck_ioctl pmc_pck;  char *filename = "/dev/tc0";  char *wfilename = "/dev/tc0";  int fd;  int verbose = 0;  /* be verbose? */  int restart = 0;  int set_clock = 0;  int fast_clock = 0;  int timeout = 0;  int period = 1000000 / 240; /* 240 Hz */  int do_wait = 0;  int delay = 0;  struct at91_tc_ioctl_data old_timer, ticks;  long diff;#define AT91C_START_CNT 1  /* one time */  unsigned int loop_cnt = AT91C_START_CNT;  do    {    next_option = getopt_long (argc, argv, short_options,                               long_options, NULL);    switch (next_option)      {      case 'd':   /* -d or --device */        /* This option takes an argument, the name of the file.  */        filename = optarg;        break;      case 'i':   /* -i or --pio */        /* This option takes an argument, the name of the file.  */        wfilename = optarg;        break;      case 'l':   /* -l or --loop */        /* This option takes an argument, the loop count. */        loop_cnt = strtol (optarg, NULL, 0);        break;      case 'p':   /* -p or --period */        /* This option takes an argument, the period in us. */        period = strtol (optarg, NULL, 0);        break;      case 't':   /* -t or --timeout */        /* This option takes an argument, the usart rcv timeout. */        timeout = strtol (optarg, NULL, 0);        break;      case 'r':   /* -r or --restart */        restart = 1;        break;      case 's':   /* -v or --verbose */        set_clock = 1;        break;      case 'y':   /* -y or --delay */        delay = 1;        break;      case 'w':   /* -v or --verbose */        do_wait = 1;        break;      case 'f':   /* -v or --verbose */        fast_clock = 1;        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);  printf ("tc test program\n");  if ((fd = open(filename, O_RDWR)) < 0)    {    printf ("open failed %s\n", filename);    exit(1);    }#define AT91_DAS_FREQ 960  if (set_clock)    {    tc_clock.tc_rc = AT91C_TC_TIMER_DIV1_CLK / AT91_DAS_FREQ;  /* set to 960 Hz */    tc_clock.tc_ra = ((AT91C_TC_TIMER_DIV1_CLK / AT91_DAS_FREQ) * 31) / 32;  /* make 1/32 high pulse */    tc_clock.tc_ccr = AT91C_TC_CLKEN | AT91C_TC_SWTRG;  /* Channel Control Register */    tc_clock.tc_cmr = AT91C_TC_ACPA_SET | AT91C_TC_ACPC_CLEAR | AT91C_TC_WAVE |       AT91C_TC_WAVESEL_UP_AUTO | AT91C_TC_TIMER_DIV1_CLOCK;  /* Channel Mode Register */    tc_clock.tcb_bcr = 0;  /* TC Block Control Register */    tc_clock.tcb_bmr = 0;  /* TC Block Mode Register */    tc_clock.p_pio = AT91C_BASE_PIOA;  /* PIO port if any */    tc_clock.pio_asr = 0;  /* ASR for above port if any */    tc_clock.pio_bsr = AT91C_PA19_TIOA1;  /* BSR for above port if any */    if (ioctl(fd, AT91_TC_SET_CLOCK, &tc_clock) != 0)  /* write the data */      {      perror ("AT91_TC_SET_CLOCK failed");      close (fd);      exit (1);      }     }  if (do_wait)    {    old_timer.flags = 0;  /* restart */    old_timer.ticks = (period * AT91_SLOW_CLOCK) / 1000000;  /* convert to 32768 ticks */    ioctl (fd, AT91_TC_WAIT_SETUP, &old_timer);  /* call setup, used to get init crtr val */    ticks.flags = 0;  /* restart */    for (cntr = 0; cntr < loop_cnt; cntr++)      {      ioctl (fd, AT91_TC_WAIT, &ticks);  /* wait for period */      diff = ticks.crtr_ticks - old_timer.crtr_ticks;  /* this is actually a 20 bit value */      if (diff < 0)  /* if less than zero, then it wrapped */        diff += 1 << 20;      old_timer.crtr_ticks = ticks.crtr_ticks;      if (cntr == 20 && delay)        sleep(1);      }    }/* now program the 288 kHz pin to the power supply 18.432 MHz / 64 */  if (fast_clock)    {    pmc_pck.pck_no = 0;  /* Select PCK0 */    pmc_pck.pmc_pck = AT91C_PMC_PRES_CLK_64 | AT91C_PMC_CSS_MAIN_CLK;  /* TC Block Mode Register */    pmc_pck.p_pio = AT91C_BASE_PIOB;  /* PIO port if any */    pmc_pck.pio_asr = AT91C_PB27_PCK0;  /* ASR for above port if any */    pmc_pck.pio_bsr = 0;  /* BSR for above port if any */    if (ioctl(fd, AT91_TC_ENABLE_PCK, &pmc_pck) != 0)  /* write the data */      {      perror ("AT91_TC_SET_CLOCK failed");      close (fd);      exit (1);      }     sleep(500);    }  close (fd);  exit(0);  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -