drv_gpio.c 2.4 KB
Newer Older
B
bigmagic 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 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
/*
 * Copyright (c) 2006-2020, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author         Notes
 * 2020-04-16     bigmagic       first version
 */

#include "drv_gpio.h"

#ifdef BSP_USING_PIN

static void raspi_pin_mode(struct rt_device *dev, rt_base_t pin, rt_base_t mode)
{
    uint32_t fselnum = pin / 10;
    uint32_t fselrest = pin % 10;

    uint32_t gpfsel = 0;
    gpfsel &= ~((uint32_t)(0x07 << (fselrest * 3)));
    gpfsel |= (uint32_t)(mode << (fselrest * 3));

    switch (fselnum)
    {
    case 0:
        GPIO_REG_GPFSEL0(GPIO_BASE) = gpfsel;
        break;
    case 1:
        GPIO_REG_GPFSEL1(GPIO_BASE) = gpfsel;
        break;
    case 2:
        GPIO_REG_GPFSEL2(GPIO_BASE) = gpfsel;
        break;
    case 3:
        GPIO_REG_GPFSEL3(GPIO_BASE) = gpfsel;
        break;
    case 4:
        GPIO_REG_GPFSEL4(GPIO_BASE) = gpfsel;
        break;
    case 5:
        GPIO_REG_GPFSEL5(GPIO_BASE) = gpfsel;
        break;
    default:
        break;
    }
}

static void raspi_pin_write(struct rt_device *dev, rt_base_t pin, rt_base_t value)
{
    uint32_t num = pin / 32;

    if(num == 0)
    {
        if(value == 0)
        {
            GPIO_REG_GPSET0(GPIO_BASE) = 1 << (pin % 32);
        }
        else
        {
            GPIO_REG_GPCLR0(GPIO_BASE) = 1 << (pin % 32);
        }
    }
    else
    {
        if(value == 0)
        {
            GPIO_REG_GPSET1(GPIO_BASE) = 1 << (pin % 32);
        }
        else
        {
            GPIO_REG_GPCLR1(GPIO_BASE) = 1 << (pin % 32);
        }
        
    }
}

static int raspi_pin_read(struct rt_device *device, rt_base_t pin)
{
    return 0;
}

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)
{
    return RT_EOK;
}

static rt_err_t raspi_pin_detach_irq(struct rt_device *device, rt_int32_t pin)
{
    return RT_EOK;
}

rt_err_t raspi_pin_irq_enable(struct rt_device *device, rt_base_t pin, rt_uint32_t enabled)
{
    return RT_EOK;
}

static const struct rt_pin_ops ops =
{
    raspi_pin_mode,
    raspi_pin_write,
    raspi_pin_read,
    raspi_pin_attach_irq,
    raspi_pin_detach_irq,
    raspi_pin_irq_enable,
};
#endif

int rt_hw_gpio_init(void)
{
#ifdef BSP_USING_PIN
    rt_device_pin_register("gpio", &ops, RT_NULL);
#endif

    return 0;
}
INIT_DEVICE_EXPORT(rt_hw_gpio_init);