Menu

[5108d3]: / source / c_gpio.c  Maximize  Restore  History

Download this file

132 lines (104 with data), 3.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
Copyright (c) 2012 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.
*/
#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)
#define FSEL_OFFSET 0 // 0x0000
#define SET_OFFSET 7 // 0x001c / 4
#define CLR_OFFSET 10 // 0x0028 / 4
#define PINLEVEL_OFFSET 13 // 0x0034 / 4
#define PULLUPDN_OFFSET 37 // 0x0094 / 4
#define PULLUPDNCLK_OFFSET 38 // 0x0098 / 4
#define PAGE_SIZE (4*1024)
#define BLOCK_SIZE (4*1024)
static volatile uint32_t *gpio_map;
void short_wait(void)
{
int i;
for (i=0; i<100; i++)
{
i++;
i--;
}
}
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;
if ((uint32_t)gpio_mem % PAGE_SIZE)
gpio_mem += PAGE_SIZE - ((uint32_t)gpio_mem % PAGE_SIZE);
gpio_map = (uint32_t *)mmap( (caddr_t)gpio_mem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, mem_fd, GPIO_BASE);
if ((uint32_t)gpio_map < 0)
return SETUP_MMAP_FAIL;
return SETUP_OK;
}
void clear_pullupdn(int gpio)
{
int clk_offset = PULLUPDNCLK_OFFSET + (gpio/32);
int shift = (gpio%32);
*(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 offset = FSEL_OFFSET + (gpio/10);
int shift = (gpio%10)*3;
if (direction == OUTPUT)
*(gpio_map+offset) = (*(gpio_map+offset) & ~(7<<shift)) | (1<<shift);
else // direction == INPUT
*(gpio_map+offset) = (*(gpio_map+offset) & ~(7<<shift));
clear_pullupdn(gpio);
}
void output_gpio(int gpio, int value)
{
int offset, shift;
if (value) // value == LOW
offset = CLR_OFFSET + (gpio/32);
else // value == HIGH
offset = SET_OFFSET + (gpio/32);
shift = (gpio%32);
*(gpio_map+offset) = 1 << shift;
}
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)
{
// fixme - set all gpios back to input
munmap((caddr_t)gpio_map, BLOCK_SIZE);
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.