1 /*
2  * Copyright (c) 2006-2023, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author              Notes
8  * 2023-10-25     Raman Gopalan       Initial version
9  * 2023-11-06     Raman Gopalan       Abstraction for GPIO driver boilerplate
10  * 2023-12-01     Raman Gopalan       Use Microchip's updated drivers for abstraction
11  */
12 
13 #include <rtthread.h>
14 #include <rtdevice.h>
15 #include "gpio.h"
16 #include <rtdbg.h>
17 #include "drv_gpio.h"
18 
19 #ifdef RT_USING_PIN
20 
at32uc3_pin_mode(struct rt_device * dev,rt_base_t pin,rt_uint8_t mode)21 static void at32uc3_pin_mode(struct rt_device *dev, rt_base_t pin, rt_uint8_t mode)
22 {
23     RT_ASSERT((AVR32_BSP_GPIO_PMIN <= pin) && (pin <= AVR32_BSP_GPIO_PMAX));
24     uint32_t gpio_flag = GPIO_DIR_INPUT;
25 
26     gpio_enable_gpio_pin(pin);
27     /* Decide based on required mode */
28     switch (mode)
29     {
30     case PIN_MODE_OUTPUT:
31         gpio_flag = GPIO_DIR_OUTPUT;
32         break;
33     case PIN_MODE_INPUT:
34         gpio_flag = GPIO_DIR_INPUT;
35         break;
36     case PIN_MODE_INPUT_PULLUP:
37         gpio_flag = GPIO_PULL_UP;
38         break;
39     case PIN_MODE_INPUT_PULLDOWN:
40         gpio_flag = GPIO_PULL_DOWN;
41         break;
42     case PIN_MODE_OUTPUT_OD:
43         gpio_flag = GPIO_OPEN_DRAIN;
44         break;
45     }
46     gpio_configure_pin(pin, gpio_flag);
47 }
48 
at32uc3_pin_write(struct rt_device * dev,rt_base_t pin,rt_uint8_t value)49 static void at32uc3_pin_write(struct rt_device *dev, rt_base_t pin, rt_uint8_t value)
50 {
51     RT_ASSERT((AVR32_BSP_GPIO_PMIN <= pin) && (pin <= AVR32_BSP_GPIO_PMAX));
52     if (value == PIN_HIGH)
53     {
54         gpio_set_gpio_pin(pin);
55     }
56     else
57     {
58         gpio_clr_gpio_pin(pin);
59     }
60 }
61 
at32uc3_pin_read(struct rt_device * device,rt_base_t pin)62 static rt_ssize_t at32uc3_pin_read(struct rt_device *device, rt_base_t pin)
63 {
64     RT_ASSERT((AVR32_BSP_GPIO_PMIN <= pin) && (pin <= AVR32_BSP_GPIO_PMAX));
65     return (gpio_get_pin_value(pin) ? PIN_HIGH : PIN_LOW);
66 }
67 
68 static const struct rt_pin_ops ops =
69 {
70     at32uc3_pin_mode,
71     at32uc3_pin_write,
72     at32uc3_pin_read,
73     RT_NULL,
74     RT_NULL,
75     RT_NULL,
76     RT_NULL,
77 };
78 
rt_hw_gpio_init(void)79 int rt_hw_gpio_init(void)
80 {
81     rt_device_pin_register("gpio", &ops, RT_NULL);
82 
83     return 0;
84 }
85 
86 #endif /* RT_USING_PIN */
87