Skip to content

Commit

Permalink
c_gpio cleanup: pep7, added documentation, removed experimental event…
Browse files Browse the repository at this point in the history
… methods
  • Loading branch information
Chris Hager committed Feb 23, 2013
1 parent bb0a640 commit 4af43b9
Show file tree
Hide file tree
Showing 4 changed files with 402 additions and 629 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
*.py[cod]

# C extensions
*.so

# Packages
*.egg
*.egg-info
Expand Down Expand Up @@ -33,5 +30,6 @@ nosetests.xml
.mr.developer.cfg
.project
.pydevproject
.cproject

documentation/build/
184 changes: 57 additions & 127 deletions source/c_gpio/c_gpio.c
Original file line number Diff line number Diff line change
@@ -1,30 +1,15 @@
/*
Copyright (c) 2012-2013 Ben Croston
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
Based on c_gpio.c by Ben Croston.
Changes:
- PEP7 cleanup
- Added some documentation
- Removed experimental event detection methods
*/
#include <stdint.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/mman.h>
#include "c_gpio.h"

#define BCM2708_PERI_BASE 0x20000000
#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000)
Expand All @@ -43,27 +28,42 @@ SOFTWARE.
#define PAGE_SIZE (4*1024)
#define BLOCK_SIZE (4*1024)

#define SETUP_OK 0
#define SETUP_DEVMEM_FAIL 1
#define SETUP_MALLOC_FAIL 2
#define SETUP_MMAP_FAIL 3

#define INPUT 1 // is really 0 for control register!
#define OUTPUT 0 // is really 1 for control register!
#define ALT0 4

#define HIGH 1
#define LOW 0

#define PUD_OFF 0
#define PUD_DOWN 1
#define PUD_UP 2

static volatile uint32_t *gpio_map;

void short_wait(void)
// `short_wait` waits 150 cycles
void
short_wait(void)
{
int i;

for (i=0; i<150; i++) // wait 150 cycles
{
asm volatile("nop");
for (int i=0; i<150; i++) {
asm volatile("nop");
}
}

int setup(void)
// `setup` is run when GPIO is imported in Python
int
setup(void)
{
int mem_fd;
uint8_t *gpio_mem;

if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0)
{
return SETUP_DEVMEM_FAIL;
}

if ((gpio_mem = malloc(BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
return SETUP_MALLOC_FAIL;
Expand All @@ -79,103 +79,31 @@ int setup(void)
return SETUP_OK;
}

void clear_event_detect(int gpio)
{
int offset = EVENT_DETECT_OFFSET + (gpio/32);
int shift = (gpio%32);

*(gpio_map+offset) |= (1 << shift);
short_wait();
*(gpio_map+offset) = 0;
}

int event_detected(int gpio)
{
int offset, value, bit;

offset = EVENT_DETECT_OFFSET + (gpio/32);
bit = (1 << (gpio%32));
value = *(gpio_map+offset) & bit;
if (value)
{
clear_event_detect(gpio);
}
return value;
}

void set_rising_event(int gpio, int enable)
{
int offset = RISING_ED_OFFSET + (gpio/32);
int shift = (gpio%32);

if (enable)
*(gpio_map+offset) |= 1 << shift;
else
*(gpio_map+offset) &= ~(1 << shift);
clear_event_detect(gpio);
}

void set_falling_event(int gpio, int enable)
{
int offset = FALLING_ED_OFFSET + (gpio/32);
int shift = (gpio%32);

if (enable)
{
*(gpio_map+offset) |= (1 << shift);
*(gpio_map+offset) = (1 << shift);
} else {
*(gpio_map+offset) &= ~(1 << shift);
}
clear_event_detect(gpio);
}

void set_high_event(int gpio, int enable)
{
int offset = HIGH_DETECT_OFFSET + (gpio/32);
int shift = (gpio%32);

if (enable)
{
*(gpio_map+offset) |= (1 << shift);
} else {
*(gpio_map+offset) &= ~(1 << shift);
}
clear_event_detect(gpio);
}

void set_low_event(int gpio, int enable)
{
int offset = LOW_DETECT_OFFSET + (gpio/32);
int shift = (gpio%32);

if (enable)
*(gpio_map+offset) |= 1 << shift;
else
*(gpio_map+offset) &= ~(1 << shift);
clear_event_detect(gpio);
}

void set_pullupdn(int gpio, int pud)
// Sets a pullup or -down resistor on a GPIO
void
set_pullupdn(int gpio, int pud)
{
int clk_offset = PULLUPDNCLK_OFFSET + (gpio/32);
int shift = (gpio%32);

if (pud == PUD_DOWN)
*(gpio_map+PULLUPDN_OFFSET) = (*(gpio_map+PULLUPDN_OFFSET) & ~3) | PUD_DOWN;
else if (pud == PUD_UP)
*(gpio_map+PULLUPDN_OFFSET) = (*(gpio_map+PULLUPDN_OFFSET) & ~3) | PUD_UP;
else // pud == PUD_OFF
*(gpio_map+PULLUPDN_OFFSET) &= ~3;

short_wait();
*(gpio_map+clk_offset) = 1 << shift;
short_wait();
*(gpio_map+PULLUPDN_OFFSET) &= ~3;
*(gpio_map+clk_offset) = 0;
}

void setup_gpio(int gpio, int direction, int pud)
// Sets a GPIO to either output or input (input can have an optional pullup
// or -down resistor).
void
setup_gpio(int gpio, int direction, int pud)
{
int offset = FSEL_OFFSET + (gpio/10);
int shift = (gpio%10)*3;
Expand All @@ -187,42 +115,44 @@ void setup_gpio(int gpio, int direction, int pud)
*(gpio_map+offset) = (*(gpio_map+offset) & ~(7<<shift));
}

// Returns the function of a GPIO: 0=input, 1=output, 4=alt0
// Contribution by Eric Ptak <[email protected]>
int gpio_function(int gpio)
int
gpio_function(int gpio)
{
int offset = FSEL_OFFSET + (gpio/10);
int shift = (gpio%10)*3;
int value = *(gpio_map+offset);
value >>= shift;
value &= 7;
return value; // 0=input, 1=output, 4=alt0
int offset = FSEL_OFFSET + (gpio/10);
int shift = (gpio%10)*3;
int value = *(gpio_map+offset);
value >>= shift;
value &= 7;
return value;
}

void output_gpio(int gpio, int value)
// Sets a GPIO output to 1 or 0
void
output_gpio(int gpio, int value)
{
int offset, shift;

if (value) // value == HIGH
offset = SET_OFFSET + (gpio/32);
else // value == LOW
offset = CLR_OFFSET + (gpio/32);

shift = (gpio%32);

*(gpio_map+offset) = 1 << shift;
*(gpio_map+offset) = 1 << gpio % 32;
}

int input_gpio(int gpio)
// Returns the value of a GPIO input (1 or 0)
int
input_gpio(int gpio)
{
int offset, value, mask;

offset = PINLEVEL_OFFSET + (gpio/32);
mask = (1 << gpio%32);
value = *(gpio_map+offset) & mask;
return value;
}

void cleanup(void)
void
cleanup(void)
{
// fixme - set all gpios back to input
munmap((caddr_t)gpio_map, BLOCK_SIZE);
Expand Down
49 changes: 0 additions & 49 deletions source/c_gpio/c_gpio.h

This file was deleted.

Loading

0 comments on commit 4af43b9

Please sign in to comment.