#include #include #include #include #include ///////////////////////////////////////////////////////////////////// // Constants ///////////////////////////////////////////////////////////////////// // GPIO FSEL Types #define INPUT 0 #define OUTPUT 1 #define ALT0 4 #define ALT1 5 #define ALT2 6 #define ALT3 7 #define ALT4 3 #define ALT5 2 // Physical addresses #define BCM2836_PERI_BASE 0x3F000000 #define GPIO_BASE (BCM2836_PERI_BASE + 0x200000) #define BLOCK_SIZE (4*1024) #define LEDPIN 21 volatile unsigned int *gpio; //pointer to base of gpio void pioInit() { int mem_fd; void *reg_map; // /dev/mem is a psuedo-driver for accessing memory in the Linux filesystem if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) { printf("can't open /dev/mem \n"); exit(-1); } reg_map = mmap( NULL, //Address at which to start local mapping (null means don't-care) BLOCK_SIZE, //Size of mapped memory block PROT_READ|PROT_WRITE,// Enable both reading and writing to the mapped memory MAP_SHARED, // This program does not have exclusive access to this memory mem_fd, // Map to /dev/mem GPIO_BASE); // Offset to GPIO peripheral if (reg_map == MAP_FAILED) { printf("gpio mmap error %d\n", (int)reg_map); close(mem_fd); exit(-1); } gpio = (volatile unsigned *)reg_map; } // assign a pin to a mode based on its function void pinMode(int pin, int function) { unsigned index, shift; // make sure the range of the pin to is [0, 53] if (pin > 53 || pin < 0) { printf("Pin out of range, must be 0-53 \n"); exit(-1); } // find the position of the gpio pin index = pin / 10; shift = (pin % 10) * 3; // put function to the found positions gpio[index] &= ~(((~function) & 7) << shift); gpio[index] |= function << shift; } // write HIGH or LOW to the specified pin void digitalWrite(int pin, int val) { unsigned set, clr; // make sure range of the pin to is [0, 53] if (pin > 53 || pin < 0) { printf("Pin out of range, must be 0-53 \n"); exit(-1); } // find the correct bits to write set = pin < 32 ? 7 : 8; clr = pin < 32 ? 10: 11; // set pin based on val if (val) gpio[set] = (0x1) << (pin % 32); else gpio[clr] = (0x1) << (pin % 32); }