• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2022 Beken Corporation
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 #include "iot_errno.h"
15 #include "iot_gpio.h"
16 
17 #include <driver/gpio.h>
18 //#include "bk_gpio.h"
19 
20 #define GPIO_PIN_INIT    1
21 #define GPIO_PIN_UNINIT  0
22 #define GPIONUM SOC_GPIO_NUM
23 
24 static unsigned char g_gpioMap[GPIONUM] = {0};
25 static unsigned char g_gpioInitNum = 0;
26 
27 #define GPIO_CHECK(gpio) do { if ((gpio) >= GPIONUM) return IOT_FAILURE; } while(0)
28 
IoTGpioInit(unsigned int id)29 unsigned int IoTGpioInit(unsigned int id)
30 {
31     GPIO_CHECK(id);
32 
33     if (g_gpioInitNum == 0) {
34         //(void)hi_gpio_init();
35     }
36 
37     if (g_gpioMap[id] == GPIO_PIN_INIT) {
38         return IOT_FAILURE;
39     } else {
40         g_gpioMap[id] = GPIO_PIN_INIT;
41         g_gpioInitNum++;
42     }
43 
44     return IOT_SUCCESS;
45 }
46 
IoTGpioDeinit(unsigned int id)47 unsigned int IoTGpioDeinit(unsigned int id)
48 {
49     GPIO_CHECK(id);
50 
51     if (g_gpioMap[id] == GPIO_PIN_INIT) {
52         g_gpioInitNum--;
53         g_gpioMap[id] = GPIO_PIN_UNINIT;
54     } else {
55         return IOT_FAILURE;
56     }
57 
58     if (g_gpioInitNum == 0) {
59         //return hi_gpio_deinit();
60         return IOT_SUCCESS;
61     } else {
62         return IOT_SUCCESS;
63     }
64 }
65 
IoTGpioSetDir(unsigned int id,IotGpioDir dir)66 unsigned int IoTGpioSetDir(unsigned int id, IotGpioDir dir)
67 {
68 	unsigned int ret;
69 	const gpio_config_t gpio_cfg_in = { GPIO_INPUT_ENABLE, GPIO_PULL_UP_EN,GPIO_SECOND_FUNC_DISABLE};
70 	const gpio_config_t gpio_cfg_out = { GPIO_OUTPUT_ENABLE, GPIO_PULL_DISABLE,GPIO_SECOND_FUNC_DISABLE};
71     GPIO_CHECK(id);
72 
73 	switch (dir) {
74 		case IOT_GPIO_DIR_IN :
75 			gpio_dev_unmap(id);
76 			ret = IOT_SUCCESS;
77 			bk_gpio_set_config(id, &gpio_cfg_in);
78 			break;
79 
80 		case IOT_GPIO_DIR_OUT :
81 			gpio_dev_unmap(id);
82 			bk_gpio_set_config(id, &gpio_cfg_out);
83 			ret = IOT_SUCCESS;
84 			break;
85 
86 		default :
87 			ret = IOT_FAILURE;
88 			break;
89 	}
90 	return ret;
91 }
92 
IoTGpioGetDir(unsigned int id,IotGpioDir * dir)93 unsigned int IoTGpioGetDir(unsigned int id, IotGpioDir *dir)
94 {
95 	unsigned int value = 0;
96 
97     GPIO_CHECK(id);
98 
99 	if (!dir)
100 		return IOT_FAILURE;
101 
102 	if(BK_ERR_GPIO_NOT_INPUT_MODE == bk_gpio_get_input(id))
103 		*dir = IOT_GPIO_DIR_OUT ;
104 	else
105 		*dir = IOT_GPIO_DIR_IN;
106 	return IOT_SUCCESS;
107 }
108 
IoTGpioSetOutputVal(unsigned int id,IotGpioValue val)109 unsigned int IoTGpioSetOutputVal(unsigned int id, IotGpioValue val)
110 {
111     GPIO_CHECK(id);
112 	if(val == IOT_GPIO_VALUE1)
113 		bk_gpio_set_output_high(id);
114 	else
115 		bk_gpio_set_output_low(id);
116 	return IOT_SUCCESS;
117 }
118 
IoTGpioGetOutputVal(unsigned int id,IotGpioValue * val)119 unsigned int IoTGpioGetOutputVal(unsigned int id, IotGpioValue *val)
120 {
121 	unsigned int value = 0;
122 
123     GPIO_CHECK(id);
124 
125 	if (!val)
126 		return IOT_FAILURE;
127 
128 	//fixme
129 	//gpio_get_output(id, &value);	//new
130 	*val = value ? IOT_GPIO_VALUE1 : IOT_GPIO_VALUE0;
131 	return IOT_SUCCESS;
132 }
133 
IoTGpioGetInputVal(unsigned int id,IotGpioValue * val)134 unsigned int IoTGpioGetInputVal(unsigned int id, IotGpioValue *val)
135 {
136 	unsigned int value;
137 
138     GPIO_CHECK(id);
139 
140 	if (!val)
141 		return IOT_FAILURE;
142 
143 	value = bk_gpio_get_input(id);
144 	if(BK_ERR_GPIO_NOT_INPUT_MODE == value)
145 		return IOT_FAILURE;
146 
147 	*val = value ? IOT_GPIO_VALUE1 : IOT_GPIO_VALUE0;
148 	return IOT_SUCCESS;
149 }
150 
151 struct gpio_isr_adapter {
152 	GpioIsrCallbackFunc func;
153 	char * arg;
154 };
155 
156 static struct gpio_isr_adapter gpio_isr_adapters[GPIONUM] = {
157 	[ 0 ... (GPIONUM - 1) ] = {NULL, NULL}
158 };
159 
gpio_default_handler(unsigned char gpio_id)160 static void gpio_default_handler(unsigned char gpio_id)
161 {
162 	if (gpio_id >= GPIONUM)
163 		return;
164 
165 	if (gpio_isr_adapters[gpio_id].func)
166 		gpio_isr_adapters[gpio_id].func(gpio_isr_adapters[gpio_id].arg);
167 }
168 
combine_isr_mode(IotGpioIntType intType,IotGpioIntPolarity intPolarity)169 static unsigned int combine_isr_mode(IotGpioIntType intType, IotGpioIntPolarity intPolarity)
170 {
171 	unsigned int mode;
172 
173 	if (intType == IOT_INT_TYPE_LEVEL) {
174 		mode = intPolarity ? 1 : 0;
175 	} else {
176 		mode = intPolarity ? 2 : 3;
177 	}
178 
179 	return mode;
180 }
181 
IoTGpioRegisterIsrFunc(unsigned int id,IotGpioIntType intType,IotGpioIntPolarity intPolarity,GpioIsrCallbackFunc func,char * arg)182 unsigned int IoTGpioRegisterIsrFunc(unsigned int id, IotGpioIntType intType, IotGpioIntPolarity intPolarity,
183                                     GpioIsrCallbackFunc func, char *arg)
184 {
185 	unsigned int mode;
186 
187     GPIO_CHECK(id);
188 
189 	mode = combine_isr_mode(intType, intPolarity);
190 	gpio_isr_adapters[id].func = func;
191 	gpio_isr_adapters[id].arg = arg;
192 	//gpio_int_enable(id, mode, gpio_default_handler);
193 	bk_gpio_set_interrupt_type(id, mode);
194 	bk_gpio_register_isr(id, gpio_default_handler);
195 	bk_gpio_enable_interrupt(id);
196 	return IOT_SUCCESS;
197 }
198 
IoTGpioUnregisterIsrFunc(unsigned int id)199 unsigned int IoTGpioUnregisterIsrFunc(unsigned int id)
200 {
201     GPIO_CHECK(id);
202 
203 	//gpio_int_disable(id);
204 	bk_gpio_disable_interrupt(id);
205 	return IOT_SUCCESS;
206 }
207 
IoTGpioSetIsrMask(unsigned int id,unsigned char mask)208 unsigned int IoTGpioSetIsrMask(unsigned int id, unsigned char mask)
209 {
210     GPIO_CHECK(id);
211 
212 	if (mask)
213 		bk_gpio_disable_interrupt(id);
214 	else
215 		bk_gpio_enable_interrupt(id);
216 	return IOT_SUCCESS;
217 }
218 
IoTGpioSetIsrMode(unsigned int id,IotGpioIntType intType,IotGpioIntPolarity intPolarity)219 unsigned int IoTGpioSetIsrMode(unsigned int id, IotGpioIntType intType, IotGpioIntPolarity intPolarity)
220 {
221     GPIO_CHECK(id);
222 
223 	if (gpio_isr_adapters[id].func)
224 		return IoTGpioRegisterIsrFunc(id, intType, intPolarity, gpio_isr_adapters[id].func, gpio_isr_adapters[id].arg);
225 	else
226 		return IOT_FAILURE;
227 }
228 
229