• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15  * Description: Provides iot_gpio driver source \n
16  *
17  * History: \n
18  * 2023-10-18, Create file. \n
19  */
20 #include "iot_errno.h"
21 #include "gpio.h"
22 #include "iot_gpio.h"
23 
24 typedef struct {
25     GpioIsrCallbackFunc func;
26     char *arg;
27 } iot_gpio_callback_t;
28 
29 static bool g_iot_gpio_inited = false;
30 
31 static iot_gpio_callback_t g_iot_gpio_callback[PIN_NONE] = { NULL };
32 
IoTGpioInit(unsigned int id)33 unsigned int IoTGpioInit(unsigned int id)
34 {
35     unused(id);
36     if (!g_iot_gpio_inited) {
37         uapi_gpio_init();
38     }
39     g_iot_gpio_inited = true;
40     return IOT_SUCCESS;
41 }
42 
IoTGpioDeinit(unsigned int id)43 unsigned int IoTGpioDeinit(unsigned int id)
44 {
45     unused(id);
46     if (g_iot_gpio_inited) {
47         uapi_gpio_deinit();
48     }
49     g_iot_gpio_inited = false;
50     return IOT_SUCCESS;
51 }
52 
IoTGpioSetDir(unsigned int id,IotGpioDir dir)53 unsigned int IoTGpioSetDir(unsigned int id, IotGpioDir dir)
54 {
55     if (uapi_gpio_set_dir((pin_t)id, (gpio_direction_t)dir) != ERRCODE_SUCC) {
56         return IOT_FAILURE;
57     }
58     return IOT_SUCCESS;
59 }
60 
IoTGpioGetDir(unsigned int id,IotGpioDir * dir)61 unsigned int IoTGpioGetDir(unsigned int id, IotGpioDir *dir)
62 {
63     if (dir == NULL) {
64         return IOT_FAILURE;
65     }
66     *dir = (IotGpioDir)uapi_gpio_get_dir((pin_t)id);
67     return IOT_SUCCESS;
68 }
69 
IoTGpioSetOutputVal(unsigned int id,IotGpioValue val)70 unsigned int IoTGpioSetOutputVal(unsigned int id, IotGpioValue val)
71 {
72     if (uapi_gpio_set_val((pin_t)id, (gpio_level_t)val) != ERRCODE_SUCC) {
73         return IOT_FAILURE;
74     }
75     return IOT_SUCCESS;
76 }
77 
IoTGpioGetOutputVal(unsigned int id,IotGpioValue * val)78 unsigned int IoTGpioGetOutputVal(unsigned int id, IotGpioValue *val)
79 {
80     if (val == NULL) {
81         return IOT_FAILURE;
82     }
83     *val = (IotGpioValue)uapi_gpio_get_output_val((pin_t)id);
84     return IOT_SUCCESS;
85 }
86 
IoTGpioGetInputVal(unsigned int id,IotGpioValue * val)87 unsigned int IoTGpioGetInputVal(unsigned int id, IotGpioValue *val)
88 {
89     if (val == NULL) {
90         return IOT_FAILURE;
91     }
92     *val = (IotGpioValue)uapi_gpio_get_val((pin_t)id);
93     return IOT_SUCCESS;
94 }
95 
GpioCallbackFunc(pin_t id,uintptr_t arg)96 static void GpioCallbackFunc(pin_t id, uintptr_t arg)
97 {
98     unused(arg);
99     if (g_iot_gpio_callback[id].func) {
100         g_iot_gpio_callback[id].func(g_iot_gpio_callback[id].arg);
101     }
102 }
103 
IoTGpioRegisterIsrFunc(unsigned int id,IotGpioIntType intType,IotGpioIntPolarity intPolarity,GpioIsrCallbackFunc func,char * arg)104 unsigned int IoTGpioRegisterIsrFunc(unsigned int id, IotGpioIntType intType, IotGpioIntPolarity intPolarity,
105                                     GpioIsrCallbackFunc func, char *arg)
106 {
107     uint32_t trigger = 0;
108     if (intType == 0 && intPolarity == 0) {
109         trigger = GPIO_INTERRUPT_LOW;
110     } else if (intType == 0 && intPolarity == 1) {
111         trigger = GPIO_INTERRUPT_HIGH;
112     } else if (intType == 1 && intPolarity == 0) {
113         trigger = GPIO_INTERRUPT_FALLING_EDGE;
114     } else if (intType == 1 && intPolarity == 1) {
115         trigger = GPIO_INTERRUPT_RISING_EDGE;
116     } else {
117         return IOT_FAILURE;
118     }
119     g_iot_gpio_callback[id].func = func;
120     g_iot_gpio_callback[id].arg = arg;
121     if (uapi_gpio_register_isr_func((pin_t)id, trigger, (gpio_callback_t)GpioCallbackFunc) != ERRCODE_SUCC) {
122         return IOT_FAILURE;
123     }
124     return IOT_SUCCESS;
125 }
126 
IoTGpioUnregisterIsrFunc(unsigned int id)127 unsigned int IoTGpioUnregisterIsrFunc(unsigned int id)
128 {
129     if (uapi_gpio_unregister_isr_func((pin_t)id) != ERRCODE_SUCC) {
130         return IOT_FAILURE;
131     }
132     g_iot_gpio_callback[id].func = NULL;
133     g_iot_gpio_callback[id].arg = NULL;
134     return IOT_SUCCESS;
135 }
136 
IoTGpioSetIsrMode(unsigned int id,IotGpioIntType intType,IotGpioIntPolarity intPolarity)137 unsigned int IoTGpioSetIsrMode(unsigned int id, IotGpioIntType intType, IotGpioIntPolarity intPolarity)
138 {
139     uint32_t trigger = 0;
140     if (intType == 0 && intPolarity == 0) {
141         trigger = GPIO_INTERRUPT_LOW;
142     } else if (intType == 0 && intPolarity == 1) {
143         trigger = GPIO_INTERRUPT_HIGH;
144     } else if (intType == 1 && intPolarity == 0) {
145         trigger = GPIO_INTERRUPT_FALLING_EDGE;
146     } else if (intType == 1 && intPolarity == 1) {
147         trigger = GPIO_INTERRUPT_RISING_EDGE;
148     } else {
149         return IOT_FAILURE;
150     }
151     if (uapi_gpio_set_isr_mode((pin_t)id, trigger) != ERRCODE_SUCC) {
152         return IOT_FAILURE;
153     }
154     return IOT_SUCCESS;
155 }
156 
IoTGpioSetIsrMask(unsigned int id,unsigned char mask)157 unsigned int IoTGpioSetIsrMask(unsigned int id, unsigned char mask)
158 {
159     if (mask == 0) {
160         if (uapi_gpio_enable_interrupt((pin_t)id) != ERRCODE_SUCC) {
161             return IOT_FAILURE;
162         }
163     } else if (mask == 1) {
164         if (uapi_gpio_disable_interrupt((pin_t)id) != ERRCODE_SUCC) {
165             return IOT_FAILURE;
166         }
167     } else {
168         return IOT_FAILURE;
169     }
170     return IOT_SUCCESS;
171 }