Guest User

imx6gpio.comp

a guest
Mar 17th, 2014
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.83 KB | None | 0 0
  1. component imx6gpio """Driver for iMX6 Phytec board""";
  2.  
  3. pin in bit pin-##-out[4] "Output pins";
  4. pin out bit pin-##-in[4] "Input pins";
  5.  
  6. function read nofp "Reads each of the digital inputs and updates the HAL pins";
  7. function write nofp "Writes the values of the output HAL pins to the digital IO";
  8.  
  9. variable u64 write_time;
  10.  
  11. option count_function no;
  12.  
  13. license "GPL";
  14. author "Paul S";
  15. ;;
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <fcntl.h>
  20. #include <errno.h>
  21. #include <unistd.h>
  22. #include <sys/stat.h>
  23. #include <sys/types.h>
  24. #include <sys/ioctl.h>
  25. #include <signal.h>
  26. #include <sys/mman.h>
  27. #define MAX_CHAN 5
  28. //#define MAX_CHAN 4
  29. //for x21 MAX_CHAN should be equal to 4 and for x51 MAX_CHAN is 5
  30.  
  31. int fd;
  32.  
  33. //addresses for x21 expantion on PBA-B-01 1364.1 revision of Phytec PhyFLEX development board
  34. //using 4 gpios as input and 4 as output
  35. //int inaddr[MAX_CHAN] = {0x20a8000, 0x209c000, 0x209c000, 0x20b4000};
  36. //int outaddr[MAX_CHAN] = {0x20b4000, 0x20a8000, 0x20a0000, 0x20a0000};
  37.  
  38. //addresses for x54 expantion on PBA-B-01 1364.2 revision of Phytec PhyFLEX development board
  39. //using 5 gpios as input and 5 as output, leaving gpio136 behind for now
  40. int inaddr[MAX_CHAN] = {0x20ac000, 0x20a8000, 0x20a8000, 0x209c000, 0x209c000};
  41. int outaddr[MAX_CHAN] = {0x20b4000, 0x20b4000, 0x20a8000, 0x20a0000, 0x20a0000};
  42.  
  43. FUNCTION(read){
  44.  
  45.     int i;
  46.     //offsets for 1364.1 revision
  47.     //int offset[MAX_CHAN] = {13, 5, 23, 24};
  48.     //offsets for 1364.2 revision
  49.     int offset[MAX_CHAN] = {7, 18, 19, 6, 9};
  50.     volatile uint32_t *gpio;
  51.  
  52.     if ((fd = open ("/dev/mem", O_RDWR | O_SYNC) ) < 0) {
  53.         printf("Unable to open /dev/mem: %s\n", strerror(errno));
  54.         return -1;
  55.     }
  56.  
  57.     for (i = 0; i < MAX_CHAN; i++){
  58.  
  59.         gpio = (uint32_t *)mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, inaddr[i]);
  60.  
  61.         if ((int32_t)gpio < 0){
  62.             printf("Mmap failed: %s\n", strerror(errno));
  63.             return -1;
  64.         }
  65.  
  66.         //setting the registers to read mode
  67.         *(gpio + 4) &= ~(1 << offset[i]);
  68.  
  69.         pin_in(i) = ((*gpio & (1 << offset[i])) >> offset[i]);
  70.  
  71.     }
  72.  
  73.     close(fd);
  74. }
  75.  
  76. FUNCTION(write){
  77.  
  78.     int i;
  79.     //offsets for 1364.1 revision
  80.     //int offset[MAX_CHAN] = {19, 6, 9, 12};
  81.     //offsets for 1364.2 revision
  82.     int offset[MAX_CHAN] = {12, 13, 5, 23, 24};
  83.     volatile uint32_t *gpio;
  84.  
  85.     if ((fd = open ("/dev/mem", O_RDWR | O_SYNC) ) < 0) {
  86.         printf("Unable to open /dev/mem: %s\n", strerror(errno));
  87.         return -1;
  88.     }
  89.  
  90.     for (i = 0; i < MAX_CHAN; i++){
  91.  
  92.         gpio = (uint32_t *)mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, outaddr[i]);
  93.  
  94.         if ((int32_t)gpio < 0){
  95.             printf("Mmap failed: %s\n", strerror(errno));
  96.             return -1;
  97.         }
  98.  
  99.         //setting the registers to write mode
  100.         *(gpio + 4) |= (1 << offset[i]);
  101.  
  102.         //writing data
  103.  
  104.         if( pin_out(i) == 0)
  105.             *gpio &= ~(1 << offset[i]);
  106.         else
  107.             *gpio |= (1 << offset[i]);
  108.  
  109.     }
  110.  
  111.     close(fd);
  112. }
Advertisement
Add Comment
Please, Sign In to add comment