linux - Beaglebone am335x accessing GPIO by mmap, set and clear pin -
i'm writing simple program set , clear pin (the purpose use pin custom spi_cs). i'm able export pin (gpio1_17, port 9 pin 23 bb white) , use trough filesystem have drive faster.
this code:
uint32_t *gpio; int fd = open("/dev/mem", o_rdwr|o_sync); if (fd < 0){ fprintf(stderr, "unable open port\n\r"); exit(fd); } gpio =(uint32_t *) mmap(null, getpagesize(), prot_read|prot_write, map_shared, fd, gpio1_offset); // start of gpioa if(gpio == (void *) -1) { printf("memory map failed.\n"); exit(0); } else { printf("memory mapped @ address %p.\n", gpio); } printf("\ngpio_oe:%x\n",gpio[gpio_oe/4]); gpio[gpio_oe/4]=usr1; printf("\ngpio_oe:%x\n",gpio[gpio_oe/4]); printf("\ngpio_cleardataout:%x\n",gpio[gpio_cleardataout/4]); gpio[gpio_cleardataout/4]=usr1; printf("\ngpio_cleardataout:%x\n",gpio[gpio_cleardataout/4]); sleep(1); printf("\ngpio_setdataout%x\n",gpio[gpio_setdataout/4]); gpio[gpio_dataout/4]=usr1; printf("\ngpio_setdataout%x\n",gpio[gpio_setdataout/4]);
with
#define gpio1_offset 0x4804c000 #define gpio1_size 0x4804cfff-gpio1_offset #define gpio_oe 0x134 #define gpio_setdataout 0x194 #define gpio_cleardataout 0x190 #define gpio_dataout 0x13c #define usr1 1<<17
i'm able outputenable pin, beacuse if put high before running program, ping goes low. cannot set , reset it. ideas?
why directly modifying registers? way easier use linux gpio:
#define gpio_1_17 "49" int gpio; status_codes stat = status_success; //export our gpios use if((gpio = open("/sys/class/gpio/export", o_wronly)) >= 0) { write(gpio, gpio_1_17, strlen(gpio_1_17)); close(gpio); } else { stat = status_gpio_access_failure; break; } //set direction , pull low if((gpio = open("/sys/class/gpio/gpio" gpio_1_17 "/direction", o_wronly)) >= 0) { write(gpio, "out", 3); // set out direction close(gpio); } else { stat = status_gpio_access_failure; break; } if((gpio = open("/sys/class/gpio/gpio" gpio_1_17 "/value", o_wronly)) >= 0) { write(gpio, "0", 1); // pull low close(gpio); } else { stat = status_gpio_access_failure; break; }
then make sure muxed gpio in inits.
as far mmap method have above addressing looks correct. addresses in ref manual byte addresses , using 32-bit pointer have there correct. however, line: gpio[gpio_oe/4]=usr1 makes every pin on gpio1 output except 17 makes input (0 = output , 1 = input). meant this: gpio[gpio_oe/4] &= ~usr1
also believe meant have gpio[gpio_setdataout/4]=usr1; instead of gpio[gpio_dataout/4]=usr1; both cause gpio1_17 set; however, using have set other pins on gpio1 0.
i recommend using designed kernel interfaces because mmap'ing stuff controlled kernel can asking trouble.
good luck! : )
edit: bad realized said why not driving directly through file system because needed drive faster! may want consider writing/modifying spi driver stuff being done in kernel land if speed after. omap gpio interfaces simple use there well, request , set : ).
Comments
Post a Comment