📄 gpio.c.svn-base
字号:
/*
* File : gpio.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006, 2007, RT-Thread Develop Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://openlab.rt-thread.com/license/LICENSE
*
* Change Logs:
* Date Author Notes
* 2007-03-26 bernard xiong
*/
#include <rtthread.h>
#include <rthw.h>
#include <pxa270.h>
#include "gpio.h"
rt_uint32 rt_hw_gpio_get_function(rt_uint32 gpio)
{
rt_uint32 rv, io;
rv = PXA2X0_GPIO_GAFR(gpio) >> PXA2X0_GPIO_FN_SHIFT(gpio);
rv = rv & PXA2X0_GPIO_FN;
io = PXA2X0_GPIO_GPDR(gpio);
if (io & PXA2X0_GPIO_BIT(gpio)) rv |= PXA2X0_GPIO_OUT;
io = PXA2X0_GPIO_GPLR(gpio);
if (io & PXA2X0_GPIO_BIT(gpio)) rv |= PXA2X0_GPIO_SET;
return (rv);
}
rt_uint32 rt_hw_gpio_set_function(rt_uint32 gpio, rt_uint32 fn)
{
rt_uint32 rv, bit;
rt_uint32 oldfn;
oldfn = rt_hw_gpio_get_function(gpio);
if ((fn & PXA2X0_GPIO_FN) == (oldfn & PXA2X0_GPIO_FN) &&
(fn & PXA2X0_GPIO_OUT) == (oldfn & PXA2X0_GPIO_OUT))
{
/*
* The pin's function is not changing.
* For Alternate Functions and GPIO input, we can just
* return now.
* For GPIO output pins, check the initial state is
* the same.
*
* Return 'fn' instead of 'oldfn' so the caller can
* reliably detect that we didn't change anything.
* (The initial state might be different for non-
* GPIO output pins).
*/
if (!(fn & PXA2X0_GPIO_OUT) || (fn & PXA2X0_GPIO_SET) == (oldfn & PXA2X0_GPIO_SET))
return (fn);
}
/*
* See section 4.1.3.7 of the PXA2x0 Developer's Manual for
* the correct procedure for changing GPIO pin functions.
*/
bit = PXA2X0_GPIO_BIT(gpio);
/*
* 1. Configure the correct set/clear state of the pin
*/
if ((fn & PXA2X0_GPIO_SET)) PXA2X0_GPIO_GPSR(gpio) = bit;
else PXA2X0_GPIO_GPCR(gpio) = bit;
/*
* 2. Configure the pin as an input or output as appropriate
*/
rv = PXA2X0_GPIO_GPDR(gpio) & ~bit;
if ((fn & PXA2X0_GPIO_OUT)) rv |= bit;
PXA2X0_GPIO_GPDR(gpio) = rv;
/*
* 3. Configure the pin's function
*/
fn = ((fn) & PXA2X0_GPIO_FN) << PXA2X0_GPIO_FN_SHIFT(gpio);
rv = PXA2X0_GPIO_GAFR(gpio) & ~(PXA2X0_GPIO_FN << PXA2X0_GPIO_FN_SHIFT(gpio));
PXA2X0_GPIO_GAFR(gpio) = rv | ~(fn << PXA2X0_GPIO_FN_SHIFT(gpio));
return (oldfn);
}
void rt_hw_gpio_interrupt_handler_0(int vector);
void rt_hw_gpio_interrupt_handler_1(int vector);
void rt_hw_gpio_interrupt_handler_n(int vector);
void rt_hw_gpio_init()
{
/* install interrupt handler */ rt_hw_interrupt_install(PXA2X0_INT_GPION, rt_hw_gpio_interrupt_handler_n, RT_NULL); rt_hw_interrupt_umask(PXA2X0_INT_GPION);
}
void rt_hw_gpio_interrupt_install(int gpio, void (*handler)(int gpio, void* data), void* data)
{
}
void rt_hw_gpio_interrupt_mask(int gpio)
{
}
void rt_hw_gpio_interrupt_umask(int gpio)
{
}
void rt_hw_gpio_interrupt_handler_0(int vector)
{
/* ack pending interrupt */
PXA2X0_GPIO_GEDR0 = PXA2X0_GPIO_BIT(0);
}
void rt_hw_gpio_interrupt_handler_1(int vector)
{
/* ack pending interrupt */
PXA2X0_GPIO_GEDR1 = PXA2X0_GPIO_BIT(1);
}
extern int rt_hw_kbd_scankeyboard(void);
extern void rt_hw_led_set(rt_uint32 enable);void rt_hw_gpio_interrupt_handler_n(int vector)
{
rt_uint32 scan, gedr;
static rt_uint32 led = 1;
/* get interrupted gpio */
gedr = PXA2X0_GPIO_GEDR0;
if (gedr == 0)
{
/* try GEDR1 */
gedr = PXA2X0_GPIO_GEDR1;
if (gedr ==0)
{
/* try GEDR2 */
gedr = PXA2X0_GPIO_GEDR2;
if (gedr == 0) return;
else
{
/* ack pending interrupt */
PXA2X0_GPIO_GEDR2 = gedr;
}
}
else
{
/* ack pending interrupt */
PXA2X0_GPIO_GEDR1 = gedr;
}
}
else
{
/* ack pending interrupt */
PXA2X0_GPIO_GEDR0 = gedr;
}
scan = rt_hw_kbd_scankeyboard();
if (scan != -2 && scan != -1)
{
rt_kprintf("%d\n", scan);
scan ++;
rt_hw_led_set(led);
if (led) led = 0;
else led = 1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -