2023-10-23 14:00:07 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2006-2023, RT-Thread Development Team
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
2023-11-16 14:04:21 +08:00
|
|
|
* 2023-10-25 Raman Gopalan Initial version
|
|
|
|
* 2023-11-06 Raman Gopalan Abstraction for GPIO driver boilerplate
|
2023-12-01 20:31:38 +08:00
|
|
|
* 2023-12-01 Raman Gopalan Use Microchip's updated drivers for abstraction
|
2023-10-23 14:00:07 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rtthread.h>
|
|
|
|
#include <rtdevice.h>
|
|
|
|
#include "gpio.h"
|
|
|
|
#include <rtdbg.h>
|
2023-11-16 14:04:21 +08:00
|
|
|
#include "drv_gpio.h"
|
2023-10-23 14:00:07 +08:00
|
|
|
|
|
|
|
#ifdef RT_USING_PIN
|
|
|
|
|
2023-11-16 14:04:21 +08:00
|
|
|
static void at32uc3_pin_mode(struct rt_device *dev, rt_base_t pin, rt_uint8_t mode)
|
2023-10-23 14:00:07 +08:00
|
|
|
{
|
2023-11-16 14:04:21 +08:00
|
|
|
RT_ASSERT((AVR32_BSP_GPIO_PMIN <= pin) && (pin <= AVR32_BSP_GPIO_PMAX));
|
2023-12-01 20:31:38 +08:00
|
|
|
uint32_t gpio_flag = GPIO_DIR_INPUT;
|
2023-10-23 14:00:07 +08:00
|
|
|
|
2023-12-01 20:31:38 +08:00
|
|
|
gpio_enable_gpio_pin(pin);
|
2023-10-23 14:00:07 +08:00
|
|
|
/* Decide based on required mode */
|
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case PIN_MODE_OUTPUT:
|
2023-12-01 20:31:38 +08:00
|
|
|
gpio_flag = GPIO_DIR_OUTPUT;
|
2023-10-23 14:00:07 +08:00
|
|
|
break;
|
|
|
|
case PIN_MODE_INPUT:
|
2023-12-01 20:31:38 +08:00
|
|
|
gpio_flag = GPIO_DIR_INPUT;
|
2023-10-23 14:00:07 +08:00
|
|
|
break;
|
|
|
|
case PIN_MODE_INPUT_PULLUP:
|
2023-12-01 20:31:38 +08:00
|
|
|
gpio_flag = GPIO_PULL_UP;
|
2023-10-23 14:00:07 +08:00
|
|
|
break;
|
|
|
|
case PIN_MODE_INPUT_PULLDOWN:
|
2023-12-01 20:31:38 +08:00
|
|
|
gpio_flag = GPIO_PULL_DOWN;
|
2023-10-23 14:00:07 +08:00
|
|
|
break;
|
|
|
|
case PIN_MODE_OUTPUT_OD:
|
2023-12-01 20:31:38 +08:00
|
|
|
gpio_flag = GPIO_OPEN_DRAIN;
|
2023-10-23 14:00:07 +08:00
|
|
|
break;
|
|
|
|
}
|
2023-12-01 20:31:38 +08:00
|
|
|
gpio_configure_pin(pin, gpio_flag);
|
2023-10-23 14:00:07 +08:00
|
|
|
}
|
|
|
|
|
2023-11-16 14:04:21 +08:00
|
|
|
static void at32uc3_pin_write(struct rt_device *dev, rt_base_t pin, rt_uint8_t value)
|
2023-10-23 14:00:07 +08:00
|
|
|
{
|
2023-11-16 14:04:21 +08:00
|
|
|
RT_ASSERT((AVR32_BSP_GPIO_PMIN <= pin) && (pin <= AVR32_BSP_GPIO_PMAX));
|
2023-10-23 14:00:07 +08:00
|
|
|
if (value == PIN_HIGH)
|
|
|
|
{
|
|
|
|
gpio_set_gpio_pin(pin);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gpio_clr_gpio_pin(pin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-24 02:50:31 +08:00
|
|
|
static rt_ssize_t at32uc3_pin_read(struct rt_device *device, rt_base_t pin)
|
2023-10-23 14:00:07 +08:00
|
|
|
{
|
2023-11-16 14:04:21 +08:00
|
|
|
RT_ASSERT((AVR32_BSP_GPIO_PMIN <= pin) && (pin <= AVR32_BSP_GPIO_PMAX));
|
2023-10-23 14:00:07 +08:00
|
|
|
return (gpio_get_pin_value(pin) ? PIN_HIGH : PIN_LOW);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct rt_pin_ops ops =
|
|
|
|
{
|
2023-11-16 14:04:21 +08:00
|
|
|
at32uc3_pin_mode,
|
|
|
|
at32uc3_pin_write,
|
|
|
|
at32uc3_pin_read,
|
2023-10-23 14:00:07 +08:00
|
|
|
RT_NULL,
|
|
|
|
RT_NULL,
|
|
|
|
RT_NULL,
|
|
|
|
RT_NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
int rt_hw_gpio_init(void)
|
|
|
|
{
|
|
|
|
rt_device_pin_register("gpio", &ops, RT_NULL);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* RT_USING_PIN */
|