Created
June 6, 2013 19:24
-
-
Save omerk/5724180 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2013 Erlang Solutions Ltd. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
/* | |
* File : blink_mmap.c | |
* Author : Omer Kilic <[email protected]> | |
* Purpose : An example of using mmap'ed peripheral access on the Raspberry Pi. | |
* | |
* This example toggles the state of pin 17 in one second intervals. This change | |
* can be observed by looking at the LED connected to Pin 17 on the demo board. | |
* | |
* Original idea from: http://www.raspberrypi.org/phpBB3/viewtopic.php?f=33&t=8476 | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdint.h> | |
#include <unistd.h> | |
#include <errno.h> | |
#include <string.h> | |
#include <fcntl.h> | |
#include <sys/mman.h> | |
// GPIO registers, as detailed in page 90 of the RPi "datasheet": | |
// http://www.raspberrypi.org/wp-content/uploads/2012/02/BCM2835-ARM-Peripherals.pdf | |
#define GPIO_BASE 0x20200000 | |
// offsets | |
#define GPIO_GPSEL1 1 | |
#define GPIO_GPSET0 7 | |
#define GPIO_GPCLR0 10 | |
// globals | |
static volatile uint32_t *gpio; | |
int fd; | |
void | |
mem_open () | |
{ | |
if ((fd = open ("/dev/mem", O_RDWR | O_SYNC) ) < 0) { | |
printf("[fail] can't open /dev/mem: %s\n", strerror(errno)); | |
exit(EXIT_FAILURE); | |
} else { | |
printf("[ok] /dev/mem opened\n"); | |
} | |
} | |
void | |
mem_map () | |
{ | |
// Map a page of memory to access the GPIO peripheral at address 0x20200000 | |
// NULL = Delegate mmap to allocate memory (no malloc necessary) | |
// 1 = length in bytes from offset | |
// PROT_READ | PROT_WRITE = what we want to do to it | |
// MAP_SHARED = allow other processes to access the mapped memory too | |
gpio = (uint32_t *)mmap(NULL, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_BASE); | |
if (gpio == MAP_FAILED){ | |
printf("[fail] mmap error: %s\n", strerror(errno)); | |
exit(EXIT_FAILURE); | |
} else { | |
printf("[ok] mmap success\n"); | |
} | |
} | |
void | |
pin_out () | |
{ | |
// Set GPIO17 as an output | |
// Increment the pointer to 0x20200004 (GPFSEL1) | |
// Set the value through a little bit twiddling where we only modify the bits 21-23 in the register | |
*(gpio + GPIO_GPSEL1) = (*(gpio + GPIO_GPSEL1) & ~(7 << 21)) | (1 << 21); | |
} | |
void | |
pin_on () | |
{ | |
// Set the pin high | |
// Increment the pointer to 0x2020001C (GPSET0) | |
*(gpio + GPIO_GPSET0) = 1 << 17; | |
printf("pin on\n"); | |
} | |
void | |
pin_off () | |
{ | |
// Set the pin low | |
// Increment the pointer to 0x20200028 (GPCLR0) | |
*(gpio + GPIO_GPCLR0) = 1 << 17; | |
printf("pin off\n"); | |
} | |
int | |
main (int argc, char **argv) | |
{ | |
mem_open(); | |
mem_map(); | |
pin_out(); | |
for(;;){ | |
pin_on(); | |
sleep(1); | |
pin_off(); | |
sleep(1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment