gpio.c 3.7 KB
Newer Older
W
weety 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * File      : gpio.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2006, RT-Thread Development Team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Change Logs:
 * Date           Author		Notes
 * 2011-01-13     weety		first version
 */

W
weety 已提交
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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
#include <rtthread.h>
#include "gpio.h"

#define GPIO0_BASE			(DAVINCI_GPIO_BASE + 0x10)
#define GPIO1_BASE			(DAVINCI_GPIO_BASE + 0x38)
#define GPIO2_BASE			(DAVINCI_GPIO_BASE + 0x60)
#define GPIO3_BASE			(DAVINCI_GPIO_BASE + 0x88)


static unsigned int dm365_gpio_base = (unsigned int)GPIO0_BASE;

#define GPIO_OE			(dm365_gpio_base + 0x00)
#define GPIO_DATAIN		(dm365_gpio_base + 0x10)
#define GPIO_DATAOUT		(dm365_gpio_base + 0x04)
#define GPIO_CLROUT		(dm365_gpio_base + 0x0C)
#define GPIO_SETOUT		(dm365_gpio_base + 0x08)

#define gpio_dirin(n)		*(volatile unsigned int *)((GPIO_OE)) |= 1<<(n)
#define gpio_dirout(n)	*(volatile unsigned int *)((GPIO_OE)) &= ~(1u<<(n))
#define gpio_set(n)		*(volatile unsigned int *)((GPIO_SETOUT)) = 1<<(n)
#define gpio_clr(n)		*(volatile unsigned int *)((GPIO_CLROUT)) = 1<<(n)
#define gpio_get(n)		( ( *(volatile unsigned int *)((GPIO_DATAIN)) & (1<<(n)) ) ? 1 : 0 )

 #define GPIO_GRP_MASK (5)
 
static int gpio_to_base(unsigned int gpio)
{
	unsigned int grp_idx;
	int ret;

	grp_idx = gpio >> GPIO_GRP_MASK;  

	switch (grp_idx) {
		case 0:
			dm365_gpio_base = (unsigned int)GPIO0_BASE;
			ret = 0;
			break;
		case 1:
			dm365_gpio_base = (unsigned int)GPIO1_BASE;
			ret = 0;
			break;
		case 2:
			dm365_gpio_base = (unsigned int)GPIO2_BASE;
			ret = 0;
			break;    
		case 3:
			dm365_gpio_base = (unsigned int)GPIO3_BASE;
			ret = 0;
			break;   
		default:
			ret =-RT_EIO;
			break;      
	}
	return ret;
}


int gpio_direction_input(unsigned int gpio)
{
	unsigned int offset;
	int ret=0;

	rt_ubase_t temp = rt_hw_interrupt_disable();
	ret = gpio_to_base(gpio); 
	if (ret < 0) {
		goto gpio_free;
	}
	offset =  gpio & ((1 << GPIO_GRP_MASK) -1);

	gpio_dirin(offset);

gpio_free:
	rt_hw_interrupt_enable(temp);

	return ret;
}

int gpio_direction_output(unsigned int gpio, int value)
{
	unsigned int offset;
	int ret=0;

	rt_ubase_t temp = rt_hw_interrupt_disable();
	ret = gpio_to_base(gpio); 
	if (ret < 0) {
		goto gpio_free;
	}

	offset =  gpio & ((1 << GPIO_GRP_MASK) -1);

	if (value) {
		gpio_set(offset);
	}
	else {
		gpio_clr(offset);
	}

	gpio_dirout(offset);

gpio_free:
	rt_hw_interrupt_enable(temp);

	return ret;
}

int  gpio_set_value(unsigned int gpio, int value)
{
	unsigned int offset;
	int ret=0;

	rt_ubase_t temp = rt_hw_interrupt_disable();
	ret = gpio_to_base(gpio); 
	if (ret < 0) {
		goto gpio_free;
	}

	offset =  gpio & ((1 << GPIO_GRP_MASK) -1);

	if (value) {
		gpio_set(offset);
	}
	else {
		gpio_clr(offset);
	}

gpio_free:
	rt_hw_interrupt_enable(temp);

	return ret;
}

int gpio_get_value(unsigned int gpio)
{
	unsigned int offset;
	int ret=0;

	rt_ubase_t temp = rt_hw_interrupt_disable();
	ret = gpio_to_base(gpio); 
	if (ret < 0) {
		goto gpio_free;
	}

	offset =  gpio & ((1 << GPIO_GRP_MASK) -1);
	ret = gpio_get(offset);

gpio_free:
	rt_hw_interrupt_enable(temp);

	return ret;
}