Note that there are some explanatory texts on larger screens.

plurals
  1. PODriving Beaglebone GPIO through /dev/mem
    primarykey
    data
    text
    <p>I'm trying to write a C program for blinking a LED on the Beaglebone. I know I can use the sysfs way...but I'd like to see if it is possible to get the same result mapping the physical address space with /dev/mem.</p> <p>I have a header file, beaglebone_gpio.h wit the following contents:</p> <pre><code>#ifndef _BEAGLEBONE_GPIO_H_ #define _BEAGLEBONE_GPIO_H_ #define GPIO1_START_ADDR 0x4804C000 #define GPIO1_END_ADDR 0x4804DFFF #define GPIO1_SIZE (GPIO1_END_ADDR - GPIO1_START_ADDR) #define GPIO_OE 0x134 #define GPIO_SETDATAOUT 0x194 #define GPIO_CLEARDATAOUT 0x190 #define USR0_LED (1&lt;&lt;21) #define USR1_LED (1&lt;&lt;22) #define USR2_LED (1&lt;&lt;23) #define USR3_LED (1&lt;&lt;24) #endif </code></pre> <p>and then I have my C program, gpiotest.c</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;sys/mman.h&gt; #include &lt;sys/stat.h&gt; #include &lt;fcntl.h&gt; #include "beaglebone_gpio.h" int main(int argc, char *argv[]) { volatile void *gpio_addr = NULL; volatile unsigned int *gpio_oe_addr = NULL; volatile unsigned int *gpio_setdataout_addr = NULL; volatile unsigned int *gpio_cleardataout_addr = NULL; unsigned int reg; int fd = open("/dev/mem", O_RDWR); printf("Mapping %X - %X (size: %X)\n", GPIO1_START_ADDR, GPIO1_END_ADDR, GPIO1_SIZE); gpio_addr = mmap(0, GPIO1_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, GPIO1_START_ADDR); gpio_oe_addr = gpio_addr + GPIO_OE; gpio_setdataout_addr = gpio_addr + GPIO_SETDATAOUT; gpio_cleardataout_addr = gpio_addr + GPIO_CLEARDATAOUT; if(gpio_addr == MAP_FAILED) { printf("Unable to map GPIO\n"); exit(1); } printf("GPIO mapped to %p\n", gpio_addr); printf("GPIO OE mapped to %p\n", gpio_oe_addr); printf("GPIO SETDATAOUTADDR mapped to %p\n", gpio_setdataout_addr); printf("GPIO CLEARDATAOUT mapped to %p\n", gpio_cleardataout_addr); reg = *gpio_oe_addr; printf("GPIO1 configuration: %X\n", reg); reg = reg &amp; (0xFFFFFFFF - USR1_LED); *gpio_oe_addr = reg; printf("GPIO1 configuration: %X\n", reg); printf("Start blinking LED USR1\n"); while(1) { printf("ON\n"); *gpio_setdataout_addr= USR1_LED; sleep(1); printf("OFF\n"); *gpio_cleardataout_addr = USR1_LED; sleep(1); } close(fd); return 0; } </code></pre> <p>The output is:</p> <pre><code>Mapping 4804C000 - 4804DFFF (size: 1FFF) GPIO mapped to 0x40225000 GPIO OE mapped to 40225134 GPIO SEDATAOUTADDR mapped to 0x40225194 GPIO CLEARDATAOUTADDR mapped to 0x40225190 GPIO1 configuration: FE1FFFFF GPIO1 configuratino: FE1FFFFF Start blinking LED USR1 ON OFF ON OFF ... </code></pre> <p>but I can't see the led blinking.</p> <p>As you can see from the output of the program the configuration is correct, FE1FFFFF, is coherent since GPIO1_21, GPIO1_22, GPIO1_23 and GPIO1_24 are configured as outputs, each one driving a LED.</p> <p>Any idea about the reason?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload