add gpio-utils

This commit is contained in:
Mariano Alvira 2011-03-11 16:17:06 -05:00
parent c7dc2a45a4
commit b2b18b37fc
2 changed files with 91 additions and 0 deletions

63
lib/gpio-util.c Normal file
View File

@ -0,0 +1,63 @@
#include <mc1322x.h>
#include <board.h>
#include "gpio-util.h"
void gpio_select_function(uint8_t gpio, uint8_t func) {
uint32_t mask = 3;
uint8_t major, minor, shift;
volatile uint32_t *base = GPIO_FUNC_SEL0;
uint32_t value;
major = gpio >> 4;
minor = gpio & 0xF;
shift = 2 * minor;
value = base[major];
value &= ~(mask << shift);
value |= (func << shift);
base[major] = value;
}
void gpio_reg_set(volatile uint32_t* reg, uint8_t bit) {
uint8_t major, minor;
major = bit / 32;
minor = bit % 32;
*(reg + major) |= (1UL << minor);
}
void gpio_reg_clear(volatile uint32_t* reg, uint8_t bit) {
uint8_t major, minor;
major = bit / 32;
minor = bit % 32;
*(reg + major) &= ~(1UL << minor);
}
void gpio_set_pad_dir(uint8_t gpio, uint8_t dir) {
uint8_t major, minor;
major = gpio / 32;
minor = gpio % 32;
if (dir) gpio_reg_set(GPIO_PAD_DIR0 + major, minor);
else gpio_reg_clear(GPIO_PAD_DIR0 + major, minor);
}
void gpio_set(uint8_t gpio) {
uint8_t major, minor;
major = gpio / 32;
minor = gpio % 32;
*(GPIO_DATA_SET0 + major) = (1UL << minor);
}
void gpio_reset(uint8_t gpio) {
uint8_t major, minor;
major = gpio / 32;
minor = gpio % 32;
*(GPIO_DATA_RESET0 + major) = (1UL << minor);
}
bool gpio_read(uint8_t gpio) {
uint8_t major, minor;
major = gpio / 32;
minor = gpio % 32;
return (*(GPIO_DATA0 + major) >> minor) & 1;
}

28
lib/include/gpio-util.h Normal file
View File

@ -0,0 +1,28 @@
#ifndef GPIO_UTIL_H
#define GPIO_UTIL_H
#include <stdbool.h>
#include <stdint.h>
void gpio_select_function(uint8_t gpio, uint8_t func);
void gpio_reg_set(volatile uint32_t* reg, uint8_t bit);
void gpio_reg_clear(volatile uint32_t* reg, uint8_t bit);
#define PAD_DIR_INPUT 0
#define PAD_DIR_OUTPUT 1
void gpio_set_pad_dir(uint8_t gpio, uint8_t dir);
#undef gpio_set
#undef gpio_reset
#undef gpio_read
//#define gpio_set gpio_set_ian
//#define gpio_reset gpio_reset_ian
//#define gpio_read gpio_read_ian
void gpio_set(uint8_t gpio);
void gpio_reset(uint8_t gpio);
bool gpio_read(uint8_t gpio);
#endif