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

📄 testgpio.c

📁 AT91RM9200的嵌入式LINUX下GPIO驱动
💻 C
字号:
/* testgpio.c* test file for testing AT91RM9200 gpio, flashes the red and green LED's  alternatively*/#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/gpio.h>  /* get all the gpio ioctl def's */int verbose = 0;  /* be verbose? */const char* program_name;  /* The name of this program.  */  /* A string listing valid short options letters.  */const char* const short_options = "g:p:l:dfiv";  /* An array describing valid long options.  */const struct option long_options[] = {    { "gpio",      1, NULL, 'g' },    { "pinlevel",  1, NULL, 'p' },    { "loopcnt",   1, NULL, 'l' },    { "defib",     0, NULL, 'd' },    { "flash",     0, NULL, 'f' },    { "interrupt",  0, NULL, 'i' },    { "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,           "  -g  --gpio device         GPIO. [/dev/gpio\n"           "  -p  --pinlevel               GPIO pin level 1=high. [10]\n"           "  -l  --loopcnt             loop count. [10]\n"           "  -d  --defib               Defib test\n"           "  -f  --flash               Flash LEDs.\n"           "  -i  --interrupt           Test interrupt function.\n"           "  -v  --verbose             Print verbose messages.\n");  exit (exit_code);}int main(int argc, char **argv)  {  int next_option;  unsigned int cntr;  char *gpio = NULL;  char filename1[20];  int file1;  char port_nr1 = 'b'; /* pick an LED */  char pin_nr1 = 25;  char filename2[20];  int file2;  char port_nr2 = 'b'; /* pick an LED */  char pin_nr2 = 26;  char filename3[20];  int file3;  char port_nr3 = 'a'; /* pick something that changes (DBG uart in) */  char pin_nr3 = 30;  struct at91_gpio_ioctl_data test, test2;  struct at91_gpio_wait_ioctl_data test3;  char *filename4 = "/dev/gpioa20";  /* pick an output pin */  int file4;  int  pin_on_off = -1;  char *filename5 = "/dev/gpioa24";  /* defib sync in */  int file5;  char *filename6 = "/dev/gpioa20";  /* defib sync out */  int file6;  int flash = 0;  int defib_test = 0;  int interrupt = 0;#define AT91C_START_CNT 1  /* just once, set to -1 for almost forever */  unsigned int loop_cnt = AT91C_START_CNT;  if (verbose)    printf ("gpio test program\n");  do    {    next_option = getopt_long (argc, argv, short_options,                               long_options, NULL);    switch (next_option)      {      case 'g':   /* -g or --gpio */        /* This option takes an argument, the initial baud rate */        gpio = optarg;        break;      case 'p':   /* -p or --pin_level */        /* This option takes an argument, the pin level */        pin_on_off = strtol (optarg, NULL, 0);        break;      case 'l':   /* -l or --loop */        /* This option takes an argument, the loop count.  */        loop_cnt = strtol (optarg, NULL, 0);        break;      case 'd':   /* -d or --defib */        defib_test = 1;        break;      case 'f':   /* -f or --flash */        flash = 1;        break;      case 'i':   /* -v or --verbose */        interrupt = 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);  if (flash)    {    sprintf(filename1,"/dev/gpio%c%d", port_nr1, pin_nr1);    if ((file1 = open(filename1, O_RDWR)) < 0)      {      printf ("open failed %s\n", filename1);      exit(1);      }    if (ioctl(file1, AT91_GPIO_OUTPUT_ENB, &test) != 0)  /* write the data */      {      perror ("AT91_GPIO_OUTPUT_ENB failed 1");      close (file1);      exit (1);      }     sprintf(filename2,"/dev/gpio%c%d", port_nr2, pin_nr2);    if ((file2 = open(filename2, O_RDWR)) < 0)      {      printf ("open failed %s\n", filename2);      exit(1);      }    if (ioctl(file2, AT91_GPIO_OUTPUT_ENB, &test) != 0)  /* write the data */      {      perror ("AT91_GPIO_OUTPUT_ENB failed 2");      close (file1);      close (file2);      exit (1);      }     }  if (interrupt)  /* interrupt enabled? */    { /* to check this you need to modify the "allow" table in         /opt/emerald/acq/rd/usr/src/linux-2.4.19/drivers/at91/gpio/at91_gpio.c */    sprintf(filename1, "/dev/%s", gpio);    if ((file3 = open(filename1, O_RDWR)) < 0)      {      printf ("open failed %s\n", filename3);      exit(1);      }#define TIMEOUT (3 * HZ)  /* set the timeout */    test3.timeout = TIMEOUT;  /* set the timeout */    if (ioctl(file3, AT91_GPIO_WAIT, &test3) != 0)  /* wait for some data to come in */      {      perror ("AT91_GPIO_WAIT failed");      close (file2);      close (file3);      exit (1);      }     if (test3.timeout == 0)  /* timeout ? */      printf ("finished waiting for interrupt, timeout occurred\n");    else      printf ("got interrupt, wait time = %d in %d Hz ticks, pin = 0x%x\n", TIMEOUT - test3.timeout, HZ, test3.data);    }  for (cntr = 0; cntr < loop_cnt; cntr++)    {    if (gpio)  /* someone wants a pin set/reset */      {      sprintf(filename1, "/dev/%s", gpio);      if ((file1 = open(filename1, O_RDWR)) < 0)        {        printf ("open failed %s\n", filename1);        exit(1);        }      if (pin_on_off != -1)        {         if (ioctl(file1, AT91_GPIO_OUTPUT_ENB, &test) != 0)  /* write the data */          {          perror ("AT91_GPIO_OUTPUT_ENB failed 2");          close (file1);          exit (1);          }         if (pin_on_off)          ioctl(file1, AT91_GPIO_OUTPUT_SET, &test);  /* set pin high to turn on PIC */        else          ioctl(file1, AT91_GPIO_OUTPUT_CLR, &test);  /* set pin high to turn on PIC */        }      if (verbose)        {        ioctl(file1, AT91_GPIO_PIN_DATA, &test);  /* set pin high to turn on PIC */        printf ("GPIO pin %s is now 0x%x\n", filename1, test.data);        }      close (file1);      }    if (defib_test)      {      if ((file6 = open(filename6, O_RDWR)) < 0)  /* defib sync out */        {        printf ("open failed %s\n", filename6);        exit(1);        }      if (ioctl(file6, AT91_GPIO_OUTPUT_ENB, &test) != 0)  /* write the data */        {        perror ("AT91_GPIO_OUTPUT_ENB failed 3");        close (file6);        exit (1);        }       if ((file5 = open(filename5, O_RDWR)) < 0)  /* defib sync in */        {        printf ("open failed %s\n", filename5);        exit(1);        }/* now set debib marker on, read, then off, then read */      ioctl(file6, AT91_GPIO_OUTPUT_SET, &test);  /* set pin high */      ioctl(file5, AT91_GPIO_PIN_DATA, &test);  /* defib sync in pin */      printf ("set defib sync out high, defib sync in pin 0x%x\n", test.data);      sleep (1);      ioctl(file6, AT91_GPIO_OUTPUT_CLR, &test2);  /* set pin low */      ioctl(file5, AT91_GPIO_PIN_DATA, &test2);  /* defib sync in pin */      printf ("set defib sync out low, defib sync in pin 0x%x\n", test2.data);      if (test.data && !test2.data)        printf ("test passed, loopback detected\n");      else        printf ("test failed, loopback not detected\n");      close (file5);      close (file6);      }    if (flash)      {      if (cntr & 1)        {        ioctl(file1, AT91_GPIO_OUTPUT_SET, &test);  /* green LED off */        ioctl(file2, AT91_GPIO_OUTPUT_CLR, &test);  /* red LED on */        }      else        {        ioctl(file1, AT91_GPIO_OUTPUT_CLR, &test);  /* green LED on */        ioctl(file2, AT91_GPIO_OUTPUT_SET, &test);  /* red LED off */        }      }    if (loop_cnt > 1)      usleep(250000);  /* wait 1/4 sec, only if we are continuing */    }  if (flash)    {    close (file1);    close(file2);    }#ifdef DO_INT_TEST  close(file3);#endif/* now read the PCB ID register, pick any dummy pin */  if ((file4 = open(filename4, O_RDWR)) < 0)    {    printf ("open failed %s\n", filename4);    exit(1);    }  if (ioctl(file4, AT91_GPIO_READ_PCB_ID, &test) != 0)  /* read pcb id port */    {    perror ("AT91_GPIO_READ_PCB_ID failed");    close (file4);    exit (1);    }   if (verbose)    printf ("PCB ID = 0x%x\n", test.data);  close (file4);  exit(0);  }

⌨️ 快捷键说明

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