1
0
mirror of https://github.com/xtacocorex/CHIP_IO synced 2025-07-20 04:43:21 +00:00

pull up/down feature addition to close #48. fixed gpio cleanup() function again, 3rd time is a charm. version bump to 0.3.4

This commit is contained in:
Robert Wolterman
2017-01-29 03:50:37 +00:00
parent a839661c3b
commit 2b23e2d165
10 changed files with 285 additions and 79 deletions

View File

@ -38,17 +38,23 @@ SOFTWARE.
#include <pthread.h>
#include <sys/epoll.h>
#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>
#include "event_gpio.h"
#include "common.h"
const char *stredge[4] = {"none", "rising", "falling", "both"};
// Memory Map for PUD
uint8_t *memmap;
// file descriptors
struct fdx
{
@ -85,6 +91,63 @@ dyn_int_array_t *event_occurred = NULL;
int thread_running = 0;
int epfd = -1;
// Thanks to WereCatf and Chippy-Gonzales for the Memory Mapping code/help
int map_pio_memory()
{
if (DEBUG)
printf(" ** map_pio_memory: opening /dev/mem **\n");
int fd = open("/dev/mem", O_RDWR|O_SYNC);
if(fd < 0) {
char err[256];
snprintf(err, sizeof(err), "map_pio_memory: could not open /dev/mem (%s)", strerror(errno));
add_error_msg(err);
return -1;
}
// uint32_t addr = 0x01c20800 & ~(getpagesize() - 1);
//Requires memmap to be on pagesize-boundary
if (DEBUG)
printf(" ** map_pio_memory: mapping memory **\n");
memmap = (uint8_t *)mmap(NULL, getpagesize()*2, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x01C20000);
if(memmap == NULL) {
char err[256];
snprintf(err, sizeof(err), "map_pio_memory: mmap failed (%s)", strerror(errno));
add_error_msg(err);
return -1;
}
close(fd);
//Set memmap to point to PIO-registers
if (DEBUG)
printf(" ** map_pio_memory: moving to pio registers **\n");
memmap=memmap+0x800;
return 0;
}
int gpio_get_pud(int port, int pin)
{
if (DEBUG)
printf(" ** gpio_get_pud: port %d, pin %d **\n", port, pin);
volatile uint32_t *pioMem32, *configRegister;
pioMem32=(uint32_t *)(memmap+port*0x24+0x1c); //0x1c == pull-register
configRegister=pioMem32+(pin >> 4);
return *configRegister >> ((pin & 15) * 2) & 3;
}
int gpio_set_pud(int port, int pin, uint8_t value)
{
if (DEBUG)
printf(" ** gpio_set_pud: port %d, pin %d, value %d **\n", port, pin, value);
value &= 3;
volatile uint32_t *pioMem32, *configRegister;
uint32_t mask;
pioMem32=(uint32_t *)(memmap+port*0x24+0x1c); //0x1c == pull-register
configRegister=pioMem32+(pin >> 4);
mask = ~(3 << ((pin & 15) * 2));
*configRegister &= mask;
*configRegister |= value << ((pin & 15) * 2);
return 0;
}
int gpio_export(int gpio)
{
@ -119,7 +182,7 @@ int gpio_export(int gpio)
// add to list
if (DEBUG)
printf(" ** gpio_export: creating data struct **\n");
printf(" ** gpio_export: creating data struct **\n");
new_gpio = malloc(sizeof(struct gpio_exp)); ASSRT(new_gpio != NULL);
new_gpio->gpio = gpio;