rt-thread/bsp/apollo2/board/gpio.c

218 lines
5.1 KiB
C
Raw Normal View History

2017-09-18 17:45:46 +08:00
/*
2021-03-29 07:20:47 +08:00
* Copyright (c) 2006-2021, RT-Thread Development Team
2017-09-18 17:45:46 +08:00
*
2021-03-29 07:20:47 +08:00
* SPDX-License-Identifier: Apache-2.0
2017-09-18 17:45:46 +08:00
*
* Change Logs:
* Date Author Notes
* 2017-09-16 Haley the first version
*/
#include <rtthread.h>
#include <rtdevice.h>
2018-09-21 16:10:44 +08:00
#include <rthw.h>
2017-09-18 17:45:46 +08:00
#include "am_mcu_apollo.h"
#ifdef RT_USING_PIN
2018-09-21 16:10:44 +08:00
#define APLLO2_PIN_NUMBERS 64 //[34, 64]
struct rt_pin_irq_hdr am_pin_irq_hdr_tab[64];
void am_pin_mode(rt_device_t dev, rt_base_t pin, rt_uint8_t mode)
2017-09-18 17:45:46 +08:00
{
if (mode == PIN_MODE_OUTPUT)
{
/* output setting */
am_hal_gpio_pin_config(pin, AM_HAL_GPIO_OUTPUT);
}
else if (mode == PIN_MODE_INPUT)
{
/* input setting: not pull. */
am_hal_gpio_pin_config(pin, AM_HAL_GPIO_INPUT);
}
else if (mode == PIN_MODE_INPUT_PULLUP)
{
/* input setting: pull up. */
2018-09-21 16:10:44 +08:00
am_hal_gpio_pin_config(pin, AM_HAL_GPIO_INPUT);
}
else if (mode == PIN_MODE_INPUT_PULLDOWN)
{
/* input setting: pull down. */
2017-09-18 17:45:46 +08:00
am_hal_gpio_pin_config(pin, AM_HAL_GPIO_OPENDRAIN);
}
else
{
/* input setting:default. */
2018-09-21 16:10:44 +08:00
am_hal_gpio_pin_config(pin, AM_HAL_GPIO_3STATE);
2017-09-18 17:45:46 +08:00
}
}
void am_pin_write(rt_device_t dev, rt_base_t pin, rt_uint8_t value)
2017-09-18 17:45:46 +08:00
{
if (value == PIN_LOW)
{
am_hal_gpio_out_bit_clear(pin);
}
2018-09-21 16:10:44 +08:00
else if (value == PIN_HIGH)
2017-09-18 17:45:46 +08:00
{
am_hal_gpio_out_bit_set(pin);
2021-03-29 07:20:47 +08:00
}
2017-09-18 17:45:46 +08:00
}
rt_int8_t am_pin_read(rt_device_t dev, rt_base_t pin)
2017-09-18 17:45:46 +08:00
{
rt_int8_t value = PIN_LOW;
2017-09-18 17:45:46 +08:00
2018-09-21 16:10:44 +08:00
if (am_hal_gpio_pin_config_read(pin) == AM_HAL_GPIO_OUTPUT)
2017-09-18 17:45:46 +08:00
{
2018-09-21 16:10:44 +08:00
if (am_hal_gpio_out_bit_read(pin) == 0)
{
value = PIN_LOW;
}
else
{
value = PIN_HIGH;
}
2017-09-18 17:45:46 +08:00
}
else
{
2018-09-21 16:10:44 +08:00
if (am_hal_gpio_input_bit_read(pin) == 0)
{
value = PIN_LOW;
}
else
{
value = PIN_HIGH;
}
2017-09-18 17:45:46 +08:00
}
return value;
}
rt_err_t am_pin_attach_irq(struct rt_device *device, rt_base_t pin,
rt_uint8_t mode, void (*hdr)(void *args), void *args)
2018-09-21 16:10:44 +08:00
{
rt_base_t level;
rt_int32_t irqindex = -1;
irqindex = pin;
level = rt_hw_interrupt_disable();
if(am_pin_irq_hdr_tab[irqindex].pin == pin &&
am_pin_irq_hdr_tab[irqindex].hdr == hdr &&
am_pin_irq_hdr_tab[irqindex].mode == mode &&
am_pin_irq_hdr_tab[irqindex].args == args
)
{
rt_hw_interrupt_enable(level);
return RT_EOK;
}
if(am_pin_irq_hdr_tab[irqindex].pin != -1)
{
rt_hw_interrupt_enable(level);
return -RT_EBUSY;
}
am_pin_irq_hdr_tab[irqindex].pin = pin;
am_pin_irq_hdr_tab[irqindex].hdr = hdr;
am_pin_irq_hdr_tab[irqindex].mode = mode;
am_pin_irq_hdr_tab[irqindex].args = args;
rt_hw_interrupt_enable(level);
return RT_EOK;
}
rt_err_t am_pin_dettach_irq(struct rt_device *device, rt_base_t pin)
2018-09-21 16:10:44 +08:00
{
rt_base_t level;
rt_int32_t irqindex = -1;
irqindex = pin;
level = rt_hw_interrupt_disable();
if(am_pin_irq_hdr_tab[irqindex].pin == -1)
{
rt_hw_interrupt_enable(level);
return RT_EOK;
}
am_pin_irq_hdr_tab[irqindex].pin = -1;
am_pin_irq_hdr_tab[irqindex].hdr = RT_NULL;
am_pin_irq_hdr_tab[irqindex].mode = 0;
am_pin_irq_hdr_tab[irqindex].args = RT_NULL;
rt_hw_interrupt_enable(level);
return RT_EOK;
}
rt_err_t am_pin_irq_enable(struct rt_device *device, rt_base_t pin, rt_uint8_t enabled)
2018-09-21 16:10:44 +08:00
{
rt_base_t level;
rt_int32_t irqindex = -1;
irqindex = pin;
if (enabled == PIN_IRQ_ENABLE)
{
level = rt_hw_interrupt_disable();
/* Configure the GPIO/button interrupt polarity */
if (am_pin_irq_hdr_tab[irqindex].mode == PIN_IRQ_MODE_RISING)
{
am_hal_gpio_int_polarity_bit_set(am_pin_irq_hdr_tab[irqindex].pin, AM_HAL_GPIO_RISING);
}
else if (am_pin_irq_hdr_tab[irqindex].mode == PIN_IRQ_MODE_FALLING)
{
am_hal_gpio_int_polarity_bit_set(am_pin_irq_hdr_tab[irqindex].pin, AM_HAL_GPIO_FALLING);
}
/* Clear the GPIO Interrupt (write to clear) */
am_hal_gpio_int_clear(AM_HAL_GPIO_BIT(am_pin_irq_hdr_tab[irqindex].pin));
/* Enable the GPIO/button interrupt */
am_hal_gpio_int_enable(AM_HAL_GPIO_BIT(am_pin_irq_hdr_tab[irqindex].pin));
rt_hw_interrupt_enable(level);
}
else if (enabled == PIN_IRQ_DISABLE)
{
if (am_hal_gpio_int_enable_get() != AM_HAL_GPIO_BIT(am_pin_irq_hdr_tab[irqindex].pin))
{
return -RT_ENOSYS;
2018-09-21 16:10:44 +08:00
}
/* Disable the GPIO/button interrupt */
am_hal_gpio_int_disable(AM_HAL_GPIO_BIT(am_pin_irq_hdr_tab[irqindex].pin));
}
else
{
return -RT_ENOSYS;
2018-09-21 16:10:44 +08:00
}
return RT_EOK;
}
const static struct rt_pin_ops am_pin_ops =
2017-09-18 17:45:46 +08:00
{
am_pin_mode,
am_pin_write,
am_pin_read,
2018-09-21 16:10:44 +08:00
am_pin_attach_irq,
am_pin_dettach_irq,
am_pin_irq_enable,
RT_NULL,
2017-09-18 17:45:46 +08:00
};
int rt_hw_pin_init(void)
2017-09-18 17:45:46 +08:00
{
2018-09-21 16:10:44 +08:00
rt_device_pin_register("pin", &am_pin_ops, RT_NULL);
2018-09-21 16:10:44 +08:00
//rt_device_pin_irq_register("pin", &am_pin_ops, RT_NULL);
//rt_kprintf("pin_init!\n");
2017-09-18 17:45:46 +08:00
return 0;
}
INIT_BOARD_EXPORT(rt_hw_pin_init);
2017-09-18 17:45:46 +08:00
#endif
/*@}*/