1 /******************************************************************************
2 * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK")
3 * All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *****************************************************************************/
18
19 /********************************************************************************************************
20 * @file gpio.h
21 *
22 * @brief This is the header file for B91
23 *
24 * @author Driver Group
25 *
26 *******************************************************************************************************/
27 /** @page GPIO
28 *
29 * Introduction
30 * ===============
31 * B91 contain two six group gpio(A~F), total 44 gpio pin.
32 *
33 * API Reference
34 * ===============
35 * Header File: gpio.h
36 */
37 #ifndef DRIVERS_GPIO_H_
38 #define DRIVERS_GPIO_H_
39
40
41 #include "analog.h"
42 #include "plic.h"
43 #include "reg_include/gpio_reg.h"
44 /**********************************************************************************************************************
45 * global constants *
46 *********************************************************************************************************************/
47
48 /**********************************************************************************************************************
49 * global macro *
50 *********************************************************************************************************************/
51
52
53 /**********************************************************************************************************************
54 * global data type *
55 *********************************************************************************************************************/
56 /**
57 * @brief Define GPIO types
58 */
59 typedef enum{
60 GPIO_GROUPA = 0x000,
61 GPIO_GROUPB = 0x100,
62 GPIO_GROUPC = 0x200,
63 GPIO_GROUPD = 0x300,
64 GPIO_GROUPE = 0x400,
65 GPIO_GROUPF = 0x500,
66 GPIO_ALL = 0x600,
67 GPIO_PA0 = GPIO_GROUPA | BIT(0),
68 GPIO_PA1 = GPIO_GROUPA | BIT(1),
69 GPIO_PA2 = GPIO_GROUPA | BIT(2),
70 GPIO_PA3 = GPIO_GROUPA | BIT(3),
71 GPIO_PA4 = GPIO_GROUPA | BIT(4),
72 GPIO_PA5 = GPIO_GROUPA | BIT(5),GPIO_DM=GPIO_PA5,
73 GPIO_PA6 = GPIO_GROUPA | BIT(6),GPIO_DP=GPIO_PA6,
74 GPIO_PA7 = GPIO_GROUPA | BIT(7),GPIO_SWS=GPIO_PA7,
75 GPIOA_ALL = GPIO_GROUPA | 0x00ff,
76
77 GPIO_PB0 = GPIO_GROUPB | BIT(0),
78 GPIO_PB1 = GPIO_GROUPB | BIT(1),
79 GPIO_PB2 = GPIO_GROUPB | BIT(2),
80 GPIO_PB3 = GPIO_GROUPB | BIT(3),
81 GPIO_PB4 = GPIO_GROUPB | BIT(4),
82 GPIO_PB5 = GPIO_GROUPB | BIT(5),
83 GPIO_PB6 = GPIO_GROUPB | BIT(6),
84 GPIO_PB7 = GPIO_GROUPB | BIT(7),
85
86 GPIO_PC0 = GPIO_GROUPC | BIT(0),GPIO_SWM=GPIO_PC0,
87 GPIO_PC1 = GPIO_GROUPC | BIT(1),
88 GPIO_PC2 = GPIO_GROUPC | BIT(2),
89 GPIO_PC3 = GPIO_GROUPC | BIT(3),
90 GPIO_PC4 = GPIO_GROUPC | BIT(4),
91 GPIO_PC5 = GPIO_GROUPC | BIT(5),
92 GPIO_PC6 = GPIO_GROUPC | BIT(6),
93 GPIO_PC7 = GPIO_GROUPC | BIT(7),
94 GPIOC_ALL = GPIO_GROUPC | 0x00ff,
95
96 GPIO_PD0 = GPIO_GROUPD | BIT(0),
97 GPIO_PD1 = GPIO_GROUPD | BIT(1),
98 GPIO_PD2 = GPIO_GROUPD | BIT(2),
99 GPIO_PD3 = GPIO_GROUPD | BIT(3),
100 GPIO_PD4 = GPIO_GROUPD | BIT(4),
101 GPIO_PD5 = GPIO_GROUPD | BIT(5),
102 GPIO_PD6 = GPIO_GROUPD | BIT(6),
103 GPIO_PD7 = GPIO_GROUPD | BIT(7),
104
105 GPIO_PE0 = GPIO_GROUPE | BIT(0),
106 GPIO_PE1 = GPIO_GROUPE | BIT(1),
107 GPIO_PE2 = GPIO_GROUPE | BIT(2),
108 GPIO_PE3 = GPIO_GROUPE | BIT(3),
109 GPIO_PE4 = GPIO_GROUPE | BIT(4),
110 GPIO_PE5 = GPIO_GROUPE | BIT(5),
111 GPIO_PE6 = GPIO_GROUPE | BIT(6),
112 GPIO_PE7 = GPIO_GROUPE | BIT(7),
113 GPIOE_ALL = GPIO_GROUPE | 0x00ff,
114
115 GPIO_PF0 = GPIO_GROUPF | BIT(0),
116 GPIO_PF1 = GPIO_GROUPF | BIT(1),
117 GPIO_PF2 = GPIO_GROUPF | BIT(2),
118 GPIO_PF3 = GPIO_GROUPF | BIT(3),
119
120 }gpio_pin_e;
121
122 /**
123 * @brief Define GPIO mux func
124 */
125 typedef enum{
126 AS_GPIO,
127 AS_MSPI,
128
129 AS_SWS,
130 AS_SWM,
131
132 AS_USB_DP,
133 AS_USB_DM,
134
135 AS_TDI,
136 AS_TDO,
137 AS_TMS,
138 AS_TCK,
139
140
141
142 }gpio_fuc_e;
143
144
145
146 /**
147 * @brief Define rising/falling types
148 */
149 typedef enum{
150 POL_RISING = 0,
151 POL_FALLING = 1,
152 }gpio_pol_e;
153
154
155 /**
156 * @brief Define interrupt types
157 */
158 typedef enum{
159 INTR_RISING_EDGE=0,
160 INTR_FALLING_EDGE ,
161 INTR_HIGH_LEVEL,
162 INTR_LOW_LEVEL,
163 } gpio_irq_trigger_type_e;
164
165 /**
166 * @brief Define pull up or down types
167 */
168 typedef enum {
169 GPIO_PIN_UP_DOWN_FLOAT = 0,
170 GPIO_PIN_PULLUP_1M = 1,
171 GPIO_PIN_PULLDOWN_100K = 2,
172 GPIO_PIN_PULLUP_10K = 3,
173 }gpio_pull_type_e;
174
175
176
177
178 /**
179 * @brief This function servers to enable gpio function.
180 * @param[in] pin - the selected pin.
181 * @return none.
182 */
gpio_function_en(gpio_pin_e pin)183 static inline void gpio_function_en(gpio_pin_e pin)
184 {
185 unsigned char bit = pin & 0xff;
186 BM_SET(reg_gpio_func(pin), bit);
187 }
188
189
190 /**
191 * @brief This function servers to disable gpio function.
192 * @param[in] pin - the selected pin.
193 * @return none.
194 */
gpio_function_dis(gpio_pin_e pin)195 static inline void gpio_function_dis(gpio_pin_e pin)
196 {
197 unsigned char bit = pin & 0xff;
198 BM_CLR(reg_gpio_func(pin), bit);
199 }
200
201
202
203 /**
204 * @brief This function set the pin's output high level.
205 * @param[in] pin - the pin needs to set its output level.
206 * @return none.
207 */
gpio_set_high_level(gpio_pin_e pin)208 static inline void gpio_set_high_level(gpio_pin_e pin)
209 {
210 unsigned char bit = pin & 0xff;
211 BM_SET(reg_gpio_out(pin), bit);
212
213 }
214
215
216 /**
217 * @brief This function set the pin's output low level.
218 * @param[in] pin - the pin needs to set its output level.
219 * @return none.
220 */
gpio_set_low_level(gpio_pin_e pin)221 static inline void gpio_set_low_level(gpio_pin_e pin)
222 {
223 unsigned char bit = pin & 0xff;
224 BM_CLR(reg_gpio_out(pin), bit);
225
226 }
227
228 /**
229 * @brief This function set the pin's output level.
230 * @param[in] pin - the pin needs to set its output level
231 * @param[in] value - value of the output level(1: high 0: low)
232 * @return none
233 */
gpio_set_level(gpio_pin_e pin,unsigned char value)234 static inline void gpio_set_level(gpio_pin_e pin, unsigned char value)
235 {
236 if(value)
237 {
238 gpio_set_high_level(pin);
239 }
240 else
241 {
242 gpio_set_low_level(pin);
243 }
244 }
245
246 /**
247 * @brief This function read the pin's input/output level.
248 * @param[in] pin - the pin needs to read its level.
249 * @return 1: the pin's level is high.
250 * 0: the pin's level is low.
251 */
gpio_get_level(gpio_pin_e pin)252 static inline _Bool gpio_get_level(gpio_pin_e pin)
253 {
254 return BM_IS_SET(reg_gpio_in(pin), pin & 0xff);
255 }
256
257
258 /**
259 * @brief This function read all the pins' input level.
260 * @param[out] p - the buffer used to store all the pins' input level
261 * @return none
262 */
gpio_get_level_all(unsigned char * p)263 static inline void gpio_get_level_all(unsigned char *p)
264 {
265 p[0] = reg_gpio_pa_in;
266 p[1] = reg_gpio_pb_in;
267 p[2] = reg_gpio_pc_in;
268 p[3] = reg_gpio_pd_in;
269 p[4] = reg_gpio_pe_in;
270 }
271
272
273
274 /**
275 * @brief This function set the pin toggle.
276 * @param[in] pin - the pin needs to toggle.
277 * @return none.
278 */
gpio_toggle(gpio_pin_e pin)279 static inline void gpio_toggle(gpio_pin_e pin)
280 {
281 reg_gpio_out(pin) ^= (pin & 0xFF);
282 }
283
284
285
286 /**
287 * @brief This function enable the output function of a pin.
288 * @param[in] pin - the pin needs to set the output function.
289 * @return none.
290 */
gpio_output_en(gpio_pin_e pin)291 static inline void gpio_output_en(gpio_pin_e pin)
292 {
293 unsigned char bit = pin & 0xff;
294 BM_CLR(reg_gpio_oen(pin), bit);
295 }
296
297 /**
298 * @brief This function disable the output function of a pin.
299 * @param[in] pin - the pin needs to set the output function.
300 * @return none.
301 */
gpio_output_dis(gpio_pin_e pin)302 static inline void gpio_output_dis(gpio_pin_e pin)
303 {
304 unsigned char bit = pin & 0xff;
305 BM_SET(reg_gpio_oen(pin), bit);
306 }
307
308 /**
309 * @brief This function enable set output function of a pin.
310 * @param[in] pin - the pin needs to set the output function (1: enable,0: disable)
311 * @return none
312 */
gpio_set_output(gpio_pin_e pin,unsigned char value)313 static inline void gpio_set_output(gpio_pin_e pin, unsigned char value)
314 {
315 if(value)
316 {
317 gpio_output_en(pin);
318 }
319 else
320 {
321 gpio_output_dis(pin);
322 }
323
324 }
325 /**
326 * @brief This function determines whether the output function of a pin is enabled.
327 * @param[in] pin - the pin needs to determine whether its output function is enabled.
328 * @return 1: the pin's output function is enabled.
329 * 0: the pin's output function is disabled.
330 */
gpio_is_output_en(gpio_pin_e pin)331 static inline _Bool gpio_is_output_en(gpio_pin_e pin)
332 {
333 return !BM_IS_SET(reg_gpio_oen(pin), pin & 0xff);
334 }
335
336
337 /**
338 * @brief This function determines whether the input function of a pin is enabled.
339 * @param[in] pin - the pin needs to determine whether its input function is enabled(not include group_pc).
340 * @return 1: the pin's input function is enabled.
341 * 0: the pin's input function is disabled.
342 */
gpio_is_input_en(gpio_pin_e pin)343 static inline _Bool gpio_is_input_en(gpio_pin_e pin)
344 {
345 return BM_IS_SET(reg_gpio_ie(pin), pin & 0xff);
346 }
347
348 /**
349 * @brief This function serves to enable gpio irq function.
350 * @param[in] pin - the pin needs to enable its IRQ.
351 * @return none.
352 */
gpio_irq_en(gpio_pin_e pin)353 static inline void gpio_irq_en(gpio_pin_e pin)
354 {
355 BM_SET(reg_gpio_irq_en(pin), pin & 0xff);
356 }
357
358 /**
359 * @brief This function serves to disable gpio irq function.
360 * @param[in] pin - the pin needs to disable its IRQ.
361 * @return none.
362 */
gpio_irq_dis(gpio_pin_e pin)363 static inline void gpio_irq_dis(gpio_pin_e pin)
364 {
365 BM_CLR(reg_gpio_irq_en(pin), pin & 0xff);
366 }
367
368 /**
369 * @brief This function serves to enable gpio risc0 irq function.
370 * @param[in] pin - the pin needs to enable its IRQ.
371 * @return none.
372 */
gpio_gpio2risc0_irq_en(gpio_pin_e pin)373 static inline void gpio_gpio2risc0_irq_en(gpio_pin_e pin)
374 {
375 BM_SET(reg_gpio_irq_risc0_en(pin), pin & 0xff);
376 }
377 /**
378 * @brief This function serves to disable gpio risc0 irq function.
379 * @param[in] pin - the pin needs to disable its IRQ.
380 * @return none.
381 */
gpio_gpio2risc0_irq_dis(gpio_pin_e pin)382 static inline void gpio_gpio2risc0_irq_dis(gpio_pin_e pin)
383 {
384 BM_CLR(reg_gpio_irq_risc0_en(pin), pin & 0xff);
385 }
386 /**
387 * @brief This function serves to enable gpio risc1 irq function.
388 * @param[in] pin - the pin needs to enable its IRQ.
389 * @return none.
390 */
gpio_gpio2risc1_irq_en(gpio_pin_e pin)391 static inline void gpio_gpio2risc1_irq_en(gpio_pin_e pin)
392 {
393 BM_SET(reg_gpio_irq_risc1_en(pin), pin & 0xff);
394 }
395
396 /**
397 * @brief This function serves to disable gpio risc1 irq function.
398 * @param[in] pin - the pin needs to disable its IRQ.
399 * @return none.
400 */
gpio_gpio2risc1_irq_dis(gpio_pin_e pin)401 static inline void gpio_gpio2risc1_irq_dis(gpio_pin_e pin)
402 {
403 BM_CLR(reg_gpio_irq_risc1_en(pin), pin & 0xff);
404 }
405 /**
406 * @brief This function serves to clr gpio irq status.
407 * @param[in] status - the pin needs to disable its IRQ.
408 * @return none.
409 */
gpio_clr_irq_status(gpio_irq_status_e status)410 static inline void gpio_clr_irq_status(gpio_irq_status_e status)
411 {
412 reg_gpio_irq_clr=status;
413 }
414
415 /**
416 * @brief This function set the pin's driving strength at strong.
417 * @param[in] pin - the pin needs to set the driving strength.
418 * @return none.
419 */
420 void gpio_ds_en(gpio_pin_e pin);
421
422
423 /**
424 * @brief This function set the pin's driving strength.
425 * @param[in] pin - the pin needs to set the driving strength at poor.
426 * @return none.
427 */
428 void gpio_ds_dis(gpio_pin_e pin);
429
430
431
432
433 void gpio_set_irq(gpio_pin_e pin, gpio_irq_trigger_type_e trigger_type);
434
435 /**
436 * @brief This function set a pin's IRQ_RISC0.
437 * @param[in] pin - the pin needs to enable its IRQ.
438 * @param[in] trigger_type - gpio interrupt type 0 rising edge 1 falling edge 2 high level 3 low level
439 * @return none.
440 */
441 void gpio_set_gpio2risc0_irq(gpio_pin_e pin, gpio_irq_trigger_type_e trigger_type);
442
443 /**
444 * @brief This function set a pin's IRQ_RISC1.
445 * @param[in] pin - the pin needs to enable its IRQ.
446 * @param[in] trigger_type - gpio interrupt type 0 rising edge 1 falling edge 2 high level 3 low level
447 * @return none.
448 */
449 void gpio_set_gpio2risc1_irq(gpio_pin_e pin, gpio_irq_trigger_type_e trigger_type);
450
451
452 /**
453 * @brief This function set the input function of a pin.
454 * @param[in] pin - the pin needs to set the input function.
455 * @return none.
456 */
457 void gpio_input_en(gpio_pin_e pin);
458
459 /**
460 * @brief This function disable the input function of a pin.
461 * @param[in] pin - the pin needs to set the input function.
462 * @return none.
463 */
464 void gpio_input_dis(gpio_pin_e pin);
465
466 /**
467 * @brief This function set the input function of a pin.
468 * @param[in] pin - the pin needs to set the input function
469 * @param[in] value - enable or disable the pin's input function(1: enable,0: disable )
470 * @return none
471 */
472 void gpio_set_input(gpio_pin_e pin, unsigned char value);
473 /**
474 * @brief This function servers to set the specified GPIO as high resistor.
475 * @param[in] pin - select the specified GPIO.
476 * @return none.
477 */
478 void gpio_shutdown(gpio_pin_e pin);
479
480 /**
481 * @brief This function set a pin's pull-up/down resistor.
482 * @param[in] pin - the pin needs to set its pull-up/down resistor.
483 * @param[in] up_down_res - the type of the pull-up/down resistor.
484 * @return none.
485 */
486 void gpio_set_up_down_res(gpio_pin_e pin, gpio_pull_type_e up_down_res);
487
488 /**
489 * @brief This function set pin's 30k pull-up registor.
490 * @param[in] pin - the pin needs to set its pull-up registor.
491 * @return none.
492 */
493 void gpio_set_pullup_res_30k(gpio_pin_e pin);
494
495
496 #endif
497
498
499
500
501