rt-thread/bsp/raspberry-pi/raspi3-64/driver/drv_gpio.c

320 lines
8.4 KiB
C
Raw Normal View History

2020-01-10 10:38:21 +08:00
/*
* Copyright (c) 2006-2019, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2019-07-29 zdzn first version
*/
#include "raspi.h"
#include "drv_gpio.h"
2020-02-28 17:49:50 +08:00
#ifdef BSP_USING_PIN
2020-01-10 10:38:21 +08:00
/*
* gpio_int[0] for BANK0 (pins 0-27)
* gpio_int[1] for BANK1 (pins 28-45)
* gpio_int[2] for BANK2 (pins 46-53)
*/
static struct gpio_irq_def _g_gpio_irq_tbl[GPIO_IRQ_NUM];
2020-02-28 17:49:50 +08:00
void gpio_set_pud(rt_uint8_t pin, rt_uint8_t pud)
2020-01-10 10:38:21 +08:00
{
2020-02-28 17:49:50 +08:00
rt_uint8_t num = pin / 32;
rt_uint8_t shift = pin % 32;
BCM283X_GPIO_GPPUD = pud;
DELAY_MICROS(10);
BCM283X_GPIO_GPPUDCLK(num) = 1 << shift;
DELAY_MICROS(10);
BCM283X_GPIO_GPPUD = BCM283X_GPIO_PUD_OFF;
BCM283X_GPIO_GPPUDCLK(num) = 0 << shift;
}
2020-01-10 10:38:21 +08:00
2020-02-28 17:49:50 +08:00
static void gpio_ack_irq(int irq, bcm_gpio_pin pin)
{
rt_uint32_t data;
data = IRQ_PEND2;
data &= (0x0 << (irq - 32));
IRQ_PEND2 = data;
2020-01-10 10:38:21 +08:00
2020-02-28 17:49:50 +08:00
data = IRQ_DISABLE2;
data |= (0x1 << (irq - 32));
IRQ_DISABLE2 = data;
2020-01-10 10:38:21 +08:00
}
2020-02-28 17:49:50 +08:00
void gpio_irq_disable(rt_uint8_t index, bcm_gpio_pin pin)
2020-01-10 10:38:21 +08:00
{
2020-02-28 17:49:50 +08:00
int irq = 0;
rt_uint32_t reg_value;
rt_uint8_t irq_type;
irq = IRQ_GPIO0 + index;
2020-01-10 10:38:21 +08:00
2020-02-28 17:49:50 +08:00
gpio_ack_irq(irq, pin);
irq_type = _g_gpio_irq_tbl[index].irq_type[pin];
rt_uint8_t shift = pin % 32;
rt_uint32_t mask = 1 << shift;
2020-01-10 10:38:21 +08:00
2020-02-28 17:49:50 +08:00
switch (irq_type)
2020-01-10 10:38:21 +08:00
{
2020-02-28 17:49:50 +08:00
case PIN_IRQ_MODE_RISING:
reg_value = BCM283X_GPIO_GPREN(pin /32);
BCM283X_GPIO_GPREN(pin /32) = (reg_value & ~ mask) | (PIN_LOW & mask);
break;
case PIN_IRQ_MODE_FALLING:
reg_value = BCM283X_GPIO_GPFEN(pin /32);
BCM283X_GPIO_GPFEN(pin /32) = (reg_value & ~ mask) | (PIN_LOW & mask);
break;
case PIN_IRQ_MODE_RISING_FALLING:
reg_value = BCM283X_GPIO_GPAREN(pin /32);
BCM283X_GPIO_GPAREN(pin /32) = (reg_value & ~ mask) | (PIN_LOW & mask);
reg_value = BCM283X_GPIO_GPAFEN(pin /32);
BCM283X_GPIO_GPAFEN(pin /32) = (reg_value & ~ mask) | (PIN_LOW & mask);
break;
case PIN_IRQ_MODE_HIGH_LEVEL:
reg_value = BCM283X_GPIO_GPHEN(pin /32);
BCM283X_GPIO_GPHEN(pin /32) = (reg_value & ~ mask) | (PIN_LOW & mask);
break;
case PIN_IRQ_MODE_LOW_LEVEL:
reg_value = BCM283X_GPIO_GPLEN(pin /32);
BCM283X_GPIO_GPLEN(pin /32) = (reg_value & ~ mask) | (PIN_LOW & mask);
break;
2020-01-10 10:38:21 +08:00
}
}
2020-02-28 17:49:50 +08:00
void gpio_irq_enable(rt_uint8_t index, bcm_gpio_pin pin)
2020-01-10 10:38:21 +08:00
{
2020-02-28 17:49:50 +08:00
rt_uint32_t offset;
rt_uint32_t data;
2020-01-10 10:38:21 +08:00
2020-02-28 17:49:50 +08:00
offset = pin;
if (index == 0)
offset = IRQ_GPIO0 - 32;
else if (index == 1)
offset = IRQ_GPIO1 - 32;
else
offset = IRQ_GPIO2 - 32;
2020-01-10 10:38:21 +08:00
2020-02-28 17:49:50 +08:00
data = IRQ_ENABLE2;
data |= 0x1 << offset;
IRQ_ENABLE2 = data;
2020-01-10 10:38:21 +08:00
}
2020-02-28 17:49:50 +08:00
static void raspi_pin_mode(struct rt_device *dev, rt_base_t pin, rt_base_t mode)
2020-01-10 10:38:21 +08:00
{
2020-02-28 17:49:50 +08:00
RT_ASSERT((BCM_GPIO_PIN_0 <= pin) && (pin < BCM_GPIO_PIN_NULL));
RT_ASSERT(!(mode & 0x8));
2020-01-10 10:38:21 +08:00
2020-02-28 17:49:50 +08:00
switch (mode)
2020-01-10 10:38:21 +08:00
{
2020-02-28 17:49:50 +08:00
case PIN_MODE_OUTPUT:
GPIO_FSEL(pin, BCM283X_GPIO_FSEL_OUTP);
2020-01-10 10:38:21 +08:00
break;
2020-02-28 17:49:50 +08:00
case PIN_MODE_INPUT:
GPIO_FSEL(pin, BCM283X_GPIO_FSEL_INPT);
2020-01-10 10:38:21 +08:00
break;
2020-02-28 17:49:50 +08:00
case PIN_MODE_INPUT_PULLUP:
gpio_set_pud(pin, BCM283X_GPIO_PUD_UP);
GPIO_FSEL(pin, BCM283X_GPIO_FSEL_INPT);
2020-01-10 10:38:21 +08:00
break;
2020-02-28 17:49:50 +08:00
case PIN_MODE_INPUT_PULLDOWN:
gpio_set_pud(pin, BCM283X_GPIO_PUD_DOWN);
GPIO_FSEL(pin, BCM283X_GPIO_FSEL_INPT);
2020-01-10 10:38:21 +08:00
break;
2020-02-28 17:49:50 +08:00
case PIN_MODE_OUTPUT_OD:
gpio_set_pud(pin, BCM283X_GPIO_PUD_OFF);
GPIO_FSEL(pin, BCM283X_GPIO_FSEL_OUTP);
2020-01-10 10:38:21 +08:00
break;
}
}
2020-02-28 17:49:50 +08:00
static void raspi_pin_write(struct rt_device *dev, rt_base_t pin, rt_base_t value)
2020-01-10 10:38:21 +08:00
{
2020-02-28 17:49:50 +08:00
RT_ASSERT((BCM_GPIO_PIN_0 <= pin) && (pin < BCM_GPIO_PIN_NULL));
RT_ASSERT(!(value & 0xE));
if (value)
BCM283X_GPIO_GPSET(pin / 32) |= (1 << (pin %32));
else
BCM283X_GPIO_GPCLR(pin / 32) |= (0 << (pin %32));
2020-01-10 10:38:21 +08:00
}
2020-02-28 17:49:50 +08:00
static int raspi_pin_read(struct rt_device *device, rt_base_t pin)
2020-01-10 10:38:21 +08:00
{
2020-02-28 17:49:50 +08:00
RT_ASSERT((BCM_GPIO_PIN_0 <= pin) && (pin < BCM_GPIO_PIN_NULL));
return (BCM2835_GPIO_GPLEV(pin / 32) & (1 << (pin % 32)))? PIN_HIGH : PIN_LOW;
}
static rt_err_t raspi_pin_attach_irq(struct rt_device *device, rt_int32_t pin, rt_uint32_t mode, void (*hdr)(void *args), void *args)
{
RT_ASSERT((BCM_GPIO_PIN_0 <= pin) && (pin < BCM_GPIO_PIN_NULL));
2020-01-10 10:38:21 +08:00
2020-02-28 17:49:50 +08:00
rt_uint8_t index;
rt_uint32_t reg_value;
2020-01-10 10:38:21 +08:00
if (pin <= 27)
index = 0;
2020-02-28 17:49:50 +08:00
else if (pin <= 45)
2020-01-10 10:38:21 +08:00
index = 1;
2020-02-28 17:49:50 +08:00
else
2020-01-10 10:38:21 +08:00
index = 2;
2020-02-28 17:49:50 +08:00
_g_gpio_irq_tbl[index].irq_cb[pin] = hdr;
_g_gpio_irq_tbl[index].irq_arg[pin] = args;
_g_gpio_irq_tbl[index].irq_type[pin] = mode;
2020-01-10 10:38:21 +08:00
2020-02-28 17:49:50 +08:00
rt_uint8_t shift = pin % 32;
rt_uint32_t mask = 1 << shift;
2020-01-10 10:38:21 +08:00
2020-02-28 17:49:50 +08:00
switch (mode)
2020-01-10 10:38:21 +08:00
{
2020-02-28 17:49:50 +08:00
case PIN_IRQ_MODE_RISING:
reg_value = BCM283X_GPIO_GPREN(pin /32);
BCM283X_GPIO_GPREN(pin /32) = (reg_value & ~ mask) | (PIN_HIGH & mask);
2020-01-10 10:38:21 +08:00
break;
2020-02-28 17:49:50 +08:00
case PIN_IRQ_MODE_FALLING:
reg_value = BCM283X_GPIO_GPFEN(pin /32);
BCM283X_GPIO_GPFEN(pin /32) = (reg_value & ~ mask) | (PIN_HIGH & mask);
2020-01-10 10:38:21 +08:00
break;
2020-02-28 17:49:50 +08:00
case PIN_IRQ_MODE_RISING_FALLING:
reg_value = BCM283X_GPIO_GPAREN(pin /32);
BCM283X_GPIO_GPAREN(pin /32) = (reg_value & ~ mask) | (PIN_HIGH & mask);
reg_value = BCM283X_GPIO_GPAFEN(pin /32);
BCM283X_GPIO_GPAFEN(pin /32) = (reg_value & ~ mask) | (PIN_HIGH & mask);
2020-01-10 10:38:21 +08:00
break;
2020-02-28 17:49:50 +08:00
case PIN_IRQ_MODE_HIGH_LEVEL:
reg_value = BCM283X_GPIO_GPHEN(pin /32);
BCM283X_GPIO_GPHEN(pin /32) = (reg_value & ~ mask) | (PIN_HIGH & mask);
2020-01-10 10:38:21 +08:00
break;
2020-02-28 17:49:50 +08:00
case PIN_IRQ_MODE_LOW_LEVEL:
reg_value = BCM283X_GPIO_GPLEN(pin /32);
BCM283X_GPIO_GPLEN(pin /32) = (reg_value & ~ mask) | (PIN_HIGH & mask);
2020-01-10 10:38:21 +08:00
break;
}
2020-02-28 17:49:50 +08:00
return RT_EOK;
2020-01-10 10:38:21 +08:00
}
2020-02-28 17:49:50 +08:00
static rt_err_t raspi_pin_detach_irq(struct rt_device *device, rt_int32_t pin)
2020-01-10 10:38:21 +08:00
{
2020-02-28 17:49:50 +08:00
RT_ASSERT((BCM_GPIO_PIN_0 <= pin) && (pin < BCM_GPIO_PIN_NULL));
2020-01-10 10:38:21 +08:00
2020-02-28 17:49:50 +08:00
rt_uint8_t index;
2020-01-10 10:38:21 +08:00
if (pin <= 27)
index = 0;
else if (pin <= 45)
index = 1;
else
index = 2;
2020-02-28 17:49:50 +08:00
gpio_irq_disable(index, pin);
2020-01-10 10:38:21 +08:00
_g_gpio_irq_tbl[index].irq_cb[pin] = RT_NULL;
_g_gpio_irq_tbl[index].irq_arg[pin] = RT_NULL;
_g_gpio_irq_tbl[index].irq_type[pin] = RT_NULL;
2020-02-28 17:49:50 +08:00
return RT_EOK;
2020-01-10 10:38:21 +08:00
}
2020-02-28 17:49:50 +08:00
rt_err_t raspi_pin_irq_enable(struct rt_device *device, rt_base_t pin, rt_uint32_t enabled)
2020-01-10 10:38:21 +08:00
{
2020-02-28 17:49:50 +08:00
RT_ASSERT((BCM_GPIO_PIN_0 <= pin) && (pin < BCM_GPIO_PIN_NULL));
2020-01-10 10:38:21 +08:00
2020-02-28 17:49:50 +08:00
rt_uint8_t index;
2020-01-10 10:38:21 +08:00
if (pin <= 27)
2020-02-28 17:49:50 +08:00
index = 0;
2020-01-10 10:38:21 +08:00
else if (pin <= 45)
2020-02-28 17:49:50 +08:00
index = 1;
2020-01-10 10:38:21 +08:00
else
2020-02-28 17:49:50 +08:00
index = 2;
2020-01-10 10:38:21 +08:00
2020-02-28 17:49:50 +08:00
if (enabled)
gpio_irq_enable(index, pin);
else
gpio_irq_disable(index, pin);
2020-01-10 10:38:21 +08:00
2020-02-28 17:49:50 +08:00
return RT_EOK;
2020-01-10 10:38:21 +08:00
}
static void gpio_irq_handler(int irq, void *param)
{
struct gpio_irq_def *irq_def = (struct gpio_irq_def *)param;
rt_uint32_t pin;
rt_uint32_t value;
rt_uint32_t tmpvalue;
if (irq == IRQ_GPIO0)
{
/* 0~27 */
2020-02-28 17:49:50 +08:00
value = BCM283X_GPIO_GPEDS(0);
2020-01-10 10:38:21 +08:00
value &= 0x0fffffff;
pin = 0;
2020-02-28 17:49:50 +08:00
BCM283X_GPIO_GPEDS(0) = 0;
2020-01-10 10:38:21 +08:00
}
else if (irq == IRQ_GPIO1)
{
/* 28-45 */
2020-02-28 17:49:50 +08:00
tmpvalue = BCM283X_GPIO_GPEDS(0);
2020-01-10 10:38:21 +08:00
tmpvalue &= (~0x0fffffff);
2020-02-28 17:49:50 +08:00
value = BCM283X_GPIO_GPEDS(1);
2020-01-10 10:38:21 +08:00
value &= 0x3fff;
value = (value<<4) | tmpvalue;
pin = 28;
2020-02-28 17:49:50 +08:00
BCM283X_GPIO_GPEDS(0) = 0;
BCM283X_GPIO_GPEDS(1) = 0;
2020-01-10 10:38:21 +08:00
}
else if (irq == IRQ_GPIO2)
{
/* 46-53 */
2020-02-28 17:49:50 +08:00
value = BCM283X_GPIO_GPEDS(1);
2020-01-10 10:38:21 +08:00
value &= (~0x3fff);
value &= 0xff600000;
pin = 46;
2020-02-28 17:49:50 +08:00
BCM283X_GPIO_GPEDS(1) = 0;
2020-01-10 10:38:21 +08:00
}
while (value)
{
if ((value & 0x1) && (irq_def->irq_cb[pin] != RT_NULL))
{
irq_def->irq_cb[pin](irq_def->irq_arg[pin]);
gpio_ack_irq(irq,pin);
}
pin++;
value = value >> 1;
}
}
static const struct rt_pin_ops ops =
{
2020-02-28 17:49:50 +08:00
raspi_pin_mode,
raspi_pin_write,
raspi_pin_read,
raspi_pin_attach_irq,
raspi_pin_detach_irq,
raspi_pin_irq_enable,
2020-01-10 10:38:21 +08:00
};
#endif
int rt_hw_gpio_init(void)
{
#ifdef BSP_USING_PIN
rt_device_pin_register("gpio", &ops, RT_NULL);
/* install ISR */
rt_hw_interrupt_install(IRQ_GPIO0, gpio_irq_handler, &_g_gpio_irq_tbl[0], "gpio0_irq");
rt_hw_interrupt_umask(IRQ_GPIO0);
rt_hw_interrupt_install(IRQ_GPIO1, gpio_irq_handler, &_g_gpio_irq_tbl[1], "gpio1_irq");
rt_hw_interrupt_umask(IRQ_GPIO1);
rt_hw_interrupt_install(IRQ_GPIO2, gpio_irq_handler, &_g_gpio_irq_tbl[2], "gpio2_irq");
rt_hw_interrupt_umask(IRQ_GPIO2);
2020-02-28 17:49:50 +08:00
#endif
2020-01-10 10:38:21 +08:00
return 0;
}
INIT_DEVICE_EXPORT(rt_hw_gpio_init);