Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- component imx6gpio """Driver for iMX6 Phytec board""";
- pin in bit pin-##-out[4] "Output pins";
- pin out bit pin-##-in[4] "Input pins";
- function read nofp "Reads each of the digital inputs and updates the HAL pins";
- function write nofp "Writes the values of the output HAL pins to the digital IO";
- variable u64 write_time;
- option count_function no;
- license "GPL";
- author "Paul S";
- ;;
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <unistd.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <sys/ioctl.h>
- #include <signal.h>
- #include <sys/mman.h>
- #define MAX_CHAN 5
- //#define MAX_CHAN 4
- //for x21 MAX_CHAN should be equal to 4 and for x51 MAX_CHAN is 5
- int fd;
- //addresses for x21 expantion on PBA-B-01 1364.1 revision of Phytec PhyFLEX development board
- //using 4 gpios as input and 4 as output
- //int inaddr[MAX_CHAN] = {0x20a8000, 0x209c000, 0x209c000, 0x20b4000};
- //int outaddr[MAX_CHAN] = {0x20b4000, 0x20a8000, 0x20a0000, 0x20a0000};
- //addresses for x54 expantion on PBA-B-01 1364.2 revision of Phytec PhyFLEX development board
- //using 5 gpios as input and 5 as output, leaving gpio136 behind for now
- int inaddr[MAX_CHAN] = {0x20ac000, 0x20a8000, 0x20a8000, 0x209c000, 0x209c000};
- int outaddr[MAX_CHAN] = {0x20b4000, 0x20b4000, 0x20a8000, 0x20a0000, 0x20a0000};
- FUNCTION(read){
- int i;
- //offsets for 1364.1 revision
- //int offset[MAX_CHAN] = {13, 5, 23, 24};
- //offsets for 1364.2 revision
- int offset[MAX_CHAN] = {7, 18, 19, 6, 9};
- volatile uint32_t *gpio;
- if ((fd = open ("/dev/mem", O_RDWR | O_SYNC) ) < 0) {
- printf("Unable to open /dev/mem: %s\n", strerror(errno));
- return -1;
- }
- for (i = 0; i < MAX_CHAN; i++){
- gpio = (uint32_t *)mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, inaddr[i]);
- if ((int32_t)gpio < 0){
- printf("Mmap failed: %s\n", strerror(errno));
- return -1;
- }
- //setting the registers to read mode
- *(gpio + 4) &= ~(1 << offset[i]);
- pin_in(i) = ((*gpio & (1 << offset[i])) >> offset[i]);
- }
- close(fd);
- }
- FUNCTION(write){
- int i;
- //offsets for 1364.1 revision
- //int offset[MAX_CHAN] = {19, 6, 9, 12};
- //offsets for 1364.2 revision
- int offset[MAX_CHAN] = {12, 13, 5, 23, 24};
- volatile uint32_t *gpio;
- if ((fd = open ("/dev/mem", O_RDWR | O_SYNC) ) < 0) {
- printf("Unable to open /dev/mem: %s\n", strerror(errno));
- return -1;
- }
- for (i = 0; i < MAX_CHAN; i++){
- gpio = (uint32_t *)mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, outaddr[i]);
- if ((int32_t)gpio < 0){
- printf("Mmap failed: %s\n", strerror(errno));
- return -1;
- }
- //setting the registers to write mode
- *(gpio + 4) |= (1 << offset[i]);
- //writing data
- if( pin_out(i) == 0)
- *gpio &= ~(1 << offset[i]);
- else
- *gpio |= (1 << offset[i]);
- }
- close(fd);
- }
Advertisement
Add Comment
Please, Sign In to add comment