GPIO, MMAP: C Programming

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
jstampfl
 
Posts: 25
Joined: Mon Sep 17, 2012 2:15 am

GPIO, MMAP: C Programming

Post by jstampfl »

Just to toggle an LED via GPIO pin P9.12. (Angstrom V2012, 12 - 3.8.13)
The LED is in series with 470 Ohm resister, connected to GND & P9.12

Overlay file: Modified from BB-GPIOHELP-00A0.dts

Code: Select all

/*
* Copyright (C) 2013 Pantelis Antoniou <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
/dts-v1/;
/plugin/;

/ {
	compatible = "ti,beaglebone", "ti,beaglebone-black";

	/* identification */
	part-number = "BB-GPIOP912";
	version = "00A0";

	/* state the resources this cape uses */
	exclusive-use =
		/* the pin header uses */
		"P9.12",	/* gpio */
		/* the hardware IP uses */
		"gpio1_28";

	fragment@0 {
		target = <&am33xx_pinmux>;
		__overlay__ {
			gpio_helper_pins: pinmux_gpio_helper_pins {
				pinctrl-single,pins = <
					0x078 0x07 	/* P9.12 GPIO1_28:  MODE7 | OUTPUT */
				>;
			};
		};	
	};

	fragment@2 {
		target = <&ocp>;
		__overlay__ {

			gpio_helper {
				compatible = "gpio-of-helper";
				status = "okay";
				pinctrl-names = "default";
				pinctrl-0 = <&gpio_helper_pins>;

				/* declare your gpios */
				test_led {
					gpio-name = "test_led";
					gpio = <&gpio2 12 0x00>;	/* gpio2 is gpio1 */
					output;
				};

			};
		};
	};
};
The uEnv.txt file:

optargs=quiet drm.debug=7 capemgr.enable_partno=BB-GPIOP912

The C program. Written by: Chirag Nagpal (http://chiragnagpal.com/index.html)
I added the sleep(1) & printf between the toggles.

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h> 
#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 & (0xFFFFFFFF - PIN);
    *gpio_oe_addr = reg;
    printf("GPIO1 configuration: %X\n", reg);

    printf("Start toggling PIN \n");
    while(1) {
        
        *gpio_setdataout_addr= PIN;
	printf("on\n");
	sleep(1);
        *gpio_cleardataout_addr = PIN;
	printf("off\n");
	sleep(1);
      }

    close(fd);
    return 0;
}




Locked
Please be positive and constructive with your questions and comments.

Return to “Beagle Bone & Adafruit Beagle Bone products”