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