1 #ifndef _BFLB_GPIO_H
2 #define _BFLB_GPIO_H
3 
4 #include "bflb_core.h"
5 
6 /** @addtogroup LHAL
7   * @{
8   */
9 
10 /** @addtogroup GPIO
11   * @{
12   */
13 
14 /**
15  * @brief Gpio pin definitions
16  *
17  * BL602  : GPIO0/1/2/7/8/14/15/20/21/22
18  * BL604  : GPIO0 ~ GPIO5, GPIO7/8/11/12/14/16/17/20/21/22
19  * BL606  : GPIO0 ~ GPIO22
20  * BL702  : GPIO0/1/2/7/8/9/14/15/17/23/24/25/26/27/28
21  * BL704  : GPIO0 ~ GPIO3, GPIO7 ~ GPIO11, GPIO14/15, GPIO17 ~ GPIO28,
22  * BL706  : GPIO0 ~ GPIO31
23  * BL606P : GPIO0 ~ GPIO5, GPIO11 ~ GPIO12, GPIO16 ~ GPIO21, GPIO24 ~ GPIO28, GPIO34 ~ GPIO41
24  * BL616  : GPIO0 ~ GPIO3, GPIO10 ~ GPIO17, GPIO20 ~ GPIO22, GPIO27 ~ GPIO30
25  * BL618  : GPIO0 ~ GPIO34
26  * BL808C : GPIO0 ~ GPIO23, GPIO34 ~ GPIO45
27  * BL808D : GPIO0 ~ GPIO8, GPIO11 ~ GPIO41
28  *
29  */
30 
31 /** @defgroup GPIO_PIN gpio pin definition
32   * @{
33   */
34 #define GPIO_PIN_0      0
35 #define GPIO_PIN_1      1
36 #define GPIO_PIN_2      2
37 #define GPIO_PIN_3      3
38 #define GPIO_PIN_4      4
39 #define GPIO_PIN_5      5
40 #define GPIO_PIN_6      6
41 #define GPIO_PIN_7      7
42 #define GPIO_PIN_8      8
43 #define GPIO_PIN_9      9
44 #define GPIO_PIN_10     10
45 #define GPIO_PIN_11     11
46 #define GPIO_PIN_12     12
47 #define GPIO_PIN_13     13
48 #define GPIO_PIN_14     14
49 #define GPIO_PIN_15     15
50 #define GPIO_PIN_16     16
51 #define GPIO_PIN_17     17
52 #define GPIO_PIN_18     18
53 #define GPIO_PIN_19     19
54 #define GPIO_PIN_20     20
55 #define GPIO_PIN_21     21
56 #define GPIO_PIN_22     22
57 #define GPIO_PIN_23     23
58 #define GPIO_PIN_24     24
59 #define GPIO_PIN_25     25
60 #define GPIO_PIN_26     26
61 #define GPIO_PIN_27     27
62 #define GPIO_PIN_28     28
63 #define GPIO_PIN_29     29
64 #define GPIO_PIN_30     30
65 #define GPIO_PIN_31     31
66 #define GPIO_PIN_32     32
67 #define GPIO_PIN_33     33
68 #define GPIO_PIN_34     34
69 #define GPIO_PIN_35     35
70 #define GPIO_PIN_36     36
71 #define GPIO_PIN_37     37
72 #define GPIO_PIN_38     38
73 #define GPIO_PIN_39     39
74 #define GPIO_PIN_40     40
75 #define GPIO_PIN_41     41
76 #define GPIO_PIN_42     42
77 #define GPIO_PIN_43     43
78 #define GPIO_PIN_44     44
79 #define GPIO_PIN_45     45
80 /**
81   * @}
82   */
83 
84 /* cfgset list */
85 #define GPIO_FUNC_SHIFT (0) /* Bits 0-4: GPIO function */
86 #define GPIO_FUNC_MASK  (0x1f << GPIO_FUNC_SHIFT)
87 #if defined(BL602)
88 #define GPIO_FUNC_SDIO (1 << GPIO_FUNC_SHIFT)
89 #define GPIO_FUNC_SPI0 (4 << GPIO_FUNC_SHIFT)
90 #define GPIO_FUNC_I2C0 (6 << GPIO_FUNC_SHIFT)
91 #define GPIO_FUNC_PWM0 (8 << GPIO_FUNC_SHIFT)
92 #define GPIO_FUNC_JTAG (14 << GPIO_FUNC_SHIFT)
93 #elif defined(BL702)
94 #define GPIO_FUNC_CLK_OUT  (0 << GPIO_FUNC_SHIFT)
95 #define GPIO_FUNC_I2S      (3 << GPIO_FUNC_SHIFT)
96 #define GPIO_FUNC_SPI0     (4 << GPIO_FUNC_SHIFT)
97 #define GPIO_FUNC_I2C0     (6 << GPIO_FUNC_SHIFT)
98 #define GPIO_FUNC_PWM0     (8 << GPIO_FUNC_SHIFT)
99 #define GPIO_FUNC_CAM      (9 << GPIO_FUNC_SHIFT)
100 #define GPIO_FUNC_JTAG     (14 << GPIO_FUNC_SHIFT)
101 #define GPIO_FUNC_EMAC     (19 << GPIO_FUNC_SHIFT)
102 #define GPIO_FUNC_CAM_MCLK (23 << GPIO_FUNC_SHIFT)
103 #elif defined(BL702L)
104 #define GPIO_FUNC_SPI0    (4 << GPIO_FUNC_SHIFT)
105 #define GPIO_FUNC_I2C0    (6 << GPIO_FUNC_SHIFT)
106 #define GPIO_FUNC_PWM0    (8 << GPIO_FUNC_SHIFT)
107 #define GPIO_FUNC_KEYSCAN (13 << GPIO_FUNC_SHIFT)
108 #define GPIO_FUNC_JTAG    (14 << GPIO_FUNC_SHIFT)
109 #elif defined(BL616)
110 #define GPIO_FUNC_SDH       (0 << GPIO_FUNC_SHIFT)
111 #define GPIO_FUNC_SPI0      (1 << GPIO_FUNC_SHIFT)
112 #define GPIO_FUNC_I2S       (3 << GPIO_FUNC_SHIFT)
113 #define GPIO_FUNC_PDM       (4 << GPIO_FUNC_SHIFT)
114 #define GPIO_FUNC_I2C0      (5 << GPIO_FUNC_SHIFT)
115 #define GPIO_FUNC_I2C1      (6 << GPIO_FUNC_SHIFT)
116 #define GPIO_FUNC_EMAC      (8 << GPIO_FUNC_SHIFT)
117 #define GPIO_FUNC_CAM       (9 << GPIO_FUNC_SHIFT)
118 #define GPIO_FUNC_SDU       (12 << GPIO_FUNC_SHIFT)
119 #define GPIO_FUNC_PWM0      (16 << GPIO_FUNC_SHIFT)
120 #define GPIO_FUNC_DBI_B     (22 << GPIO_FUNC_SHIFT)
121 #define GPIO_FUNC_DBI_C     (23 << GPIO_FUNC_SHIFT)
122 #define GPIO_FUNC_DBI_QSPI  (24 << GPIO_FUNC_SHIFT)
123 #define GPIO_FUNC_AUDAC_PWM (25 << GPIO_FUNC_SHIFT)
124 #define GPIO_FUNC_JTAG      (26 << GPIO_FUNC_SHIFT)
125 #define GPIO_FUNC_CLKOUT    (31 << GPIO_FUNC_SHIFT)
126 #elif defined(BL606P) || defined(BL808)
127 #define GPIO_FUNC_SDH     (0 << GPIO_FUNC_SHIFT)
128 #define GPIO_FUNC_SPI0    (1 << GPIO_FUNC_SHIFT)
129 #define GPIO_FUNC_I2S     (3 << GPIO_FUNC_SHIFT)
130 #define GPIO_FUNC_PDM     (4 << GPIO_FUNC_SHIFT)
131 #define GPIO_FUNC_I2C0    (5 << GPIO_FUNC_SHIFT)
132 #define GPIO_FUNC_I2C1    (6 << GPIO_FUNC_SHIFT)
133 #define GPIO_FUNC_EMAC    (8 << GPIO_FUNC_SHIFT)
134 #define GPIO_FUNC_CAM     (9 << GPIO_FUNC_SHIFT)
135 #define GPIO_FUNC_SDU     (12 << GPIO_FUNC_SHIFT)
136 #define GPIO_FUNC_PWM0    (16 << GPIO_FUNC_SHIFT)
137 #define GPIO_FUNC_PWM1    (17 << GPIO_FUNC_SHIFT)
138 #define GPIO_FUNC_SPI1    (18 << GPIO_FUNC_SHIFT)
139 #define GPIO_FUNC_I2C2    (19 << GPIO_FUNC_SHIFT)
140 #define GPIO_FUNC_I2C3    (20 << GPIO_FUNC_SHIFT)
141 #define GPIO_FUNC_I2C3    (20 << GPIO_FUNC_SHIFT)
142 #define GPIO_FUNC_DBI_B   (22 << GPIO_FUNC_SHIFT)
143 #define GPIO_FUNC_DBI_C   (23 << GPIO_FUNC_SHIFT)
144 #define GPIO_FUNC_JTAG_LP (25 << GPIO_FUNC_SHIFT)
145 #define GPIO_FUNC_JTAG_M0 (26 << GPIO_FUNC_SHIFT)
146 #define GPIO_FUNC_JTAG_D0 (27 << GPIO_FUNC_SHIFT)
147 #define GPIO_FUNC_CLKOUT  (31 << GPIO_FUNC_SHIFT)
148 #elif defined(BL628)
149 #define GPIO_FUNC_SDH    (0 << GPIO_FUNC_SHIFT)
150 #define GPIO_FUNC_SPI0   (1 << GPIO_FUNC_SHIFT)
151 #define GPIO_FUNC_I2S    (3 << GPIO_FUNC_SHIFT)
152 #define GPIO_FUNC_PDM    (4 << GPIO_FUNC_SHIFT)
153 #define GPIO_FUNC_I2C0   (5 << GPIO_FUNC_SHIFT)
154 #define GPIO_FUNC_I2C1   (6 << GPIO_FUNC_SHIFT)
155 #define GPIO_FUNC_UART   (7 << GPIO_FUNC_SHIFT)
156 #define GPIO_FUNC_EMAC   (8 << GPIO_FUNC_SHIFT)
157 #define GPIO_FUNC_CAM    (9 << GPIO_FUNC_SHIFT)
158 #define GPIO_FUNC_SDU    (12 << GPIO_FUNC_SHIFT)
159 #define GPIO_FUNC_PWM0   (16 << GPIO_FUNC_SHIFT)
160 #define GPIO_FUNC_DBI_B  (22 << GPIO_FUNC_SHIFT)
161 #define GPIO_FUNC_DBI_C  (23 << GPIO_FUNC_SHIFT)
162 #define GPIO_FUNC_CLKOUT (31 << GPIO_FUNC_SHIFT)
163 #endif
164 
165 #define GPIO_MODE_SHIFT                      (5) /* Bits 5-6: Port Mode */
166 #define GPIO_MODE_MASK                       (3 << GPIO_MODE_SHIFT)
167 #define GPIO_INPUT                           (0 << GPIO_MODE_SHIFT) /* Input Enable */
168 #define GPIO_OUTPUT                          (1 << GPIO_MODE_SHIFT) /* Output Enable */
169 #define GPIO_ANALOG                          (2 << GPIO_MODE_SHIFT) /* Analog Enable */
170 #define GPIO_ALTERNATE                       (3 << GPIO_MODE_SHIFT) /* Alternate Enable */
171 
172 #define GPIO_PUPD_SHIFT                      (7) /* Bits 7-8: Pull-up/down */
173 #define GPIO_PUPD_MASK                       (3 << GPIO_PUPD_SHIFT)
174 #define GPIO_FLOAT                           (0 << GPIO_PUPD_SHIFT) /* No pull-up, pull-down */
175 #define GPIO_PULLUP                          (1 << GPIO_PUPD_SHIFT) /* Pull-up */
176 #define GPIO_PULLDOWN                        (2 << GPIO_PUPD_SHIFT) /* Pull-down */
177 
178 #define GPIO_SMT_SHIFT                       (9) /* Bits 9: SMT Enable */
179 #define GPIO_SMT_MASK                        (1 << GPIO_SMT_SHIFT)
180 #define GPIO_SMT_DIS                         (0 << GPIO_SMT_SHIFT)
181 #define GPIO_SMT_EN                          (1 << GPIO_SMT_SHIFT)
182 
183 #define GPIO_DRV_SHIFT                       (10) /* Bits 10-11: Drive */
184 #define GPIO_DRV_MASK                        (3 << GPIO_DRV_SHIFT)
185 #define GPIO_DRV_0                           (0 << GPIO_DRV_SHIFT)
186 #define GPIO_DRV_1                           (1 << GPIO_DRV_SHIFT)
187 #define GPIO_DRV_2                           (2 << GPIO_DRV_SHIFT)
188 #define GPIO_DRV_3                           (3 << GPIO_DRV_SHIFT)
189 
190 /** @defgroup GPIO_INT_TRIG_MODE gpio interrupt trigger mode definition
191   * @{
192   */
193 #define GPIO_INT_TRIG_MODE_SYNC_FALLING_EDGE 0
194 #define GPIO_INT_TRIG_MODE_SYNC_RISING_EDGE  1
195 #define GPIO_INT_TRIG_MODE_SYNC_LOW_LEVEL    2
196 #define GPIO_INT_TRIG_MODE_SYNC_HIGH_LEVEL   3
197 #if defined(BL602) || defined(BL702)
198 #define GPIO_INT_TRIG_MODE_ASYNC_FALLING_EDGE 4
199 #define GPIO_INT_TRIG_MODE_ASYNC_RISING_EDGE  5
200 #define GPIO_INT_TRIG_MODE_ASYNC_LOW_LEVEL    6
201 #define GPIO_INT_TRIG_MODE_ASYNC_HIGH_LEVEL   7
202 #else
203 #define GPIO_INT_TRIG_MODE_SYNC_FALLING_RISING_EDGE 4
204 #define GPIO_INT_TRIG_MODE_ASYNC_FALLING_EDGE       8
205 #define GPIO_INT_TRIG_MODE_ASYNC_RISING_EDGE        9
206 #define GPIO_INT_TRIG_MODE_ASYNC_LOW_LEVEL          10
207 #define GPIO_INT_TRIG_MODE_ASYNC_HIGH_LEVEL         11
208 #endif
209 /**
210   * @}
211   */
212 
213 /** @defgroup GPIO_UART_FUNC gpio uart function definition
214   * @{
215   */
216 #define GPIO_UART_FUNC_UART0_RTS 0
217 #define GPIO_UART_FUNC_UART0_CTS 1
218 #define GPIO_UART_FUNC_UART0_TX  2
219 #define GPIO_UART_FUNC_UART0_RX  3
220 #define GPIO_UART_FUNC_UART1_RTS 4
221 #define GPIO_UART_FUNC_UART1_CTS 5
222 #define GPIO_UART_FUNC_UART1_TX  6
223 #define GPIO_UART_FUNC_UART1_RX  7
224 #if defined(BL808) || defined(BL606P)
225 #define GPIO_UART_FUNC_UART2_RTS 8
226 #define GPIO_UART_FUNC_UART2_CTS 9
227 #define GPIO_UART_FUNC_UART2_TX  10
228 #define GPIO_UART_FUNC_UART2_RX  11
229 #endif
230 /**
231   * @}
232   */
233 
234 /** @defgroup GPIO_CMD gpio feature control cmd definition
235   * @{
236   */
237 #define GPIO_CMD_GET_GPIO_FUN (0x01)
238 /**
239   * @}
240   */
241 
242 #ifdef __cplusplus
243 extern "C" {
244 #endif
245 
246 /**
247  * @brief Initialize gpio pin.
248  *
249  * @param [in] dev device handle
250  * @param [in] pin gpio pin, use @ref GPIO_PIN
251  * @param [in] cfgset gpio config mask
252  */
253 void bflb_gpio_init(struct bflb_device_s *dev, uint8_t pin, uint32_t cfgset);
254 
255 /**
256  * @brief Deinitialize gpio pin with input float.
257  *
258  * @param [in] dev device handle
259  * @param [in] pin gpio pin, use @ref GPIO_PIN
260  */
261 void bflb_gpio_deinit(struct bflb_device_s *dev, uint8_t pin);
262 
263 /**
264  * @brief Write gpio pin with high level.
265  *
266  * @param [in] dev device handle
267  * @param [in] pin gpio pin, use @ref GPIO_PIN
268  */
269 void bflb_gpio_set(struct bflb_device_s *dev, uint8_t pin);
270 
271 /**
272  * @brief Write gpio pin with low level.
273  *
274  * @param [in] dev device handle
275  * @param [in] pin gpio pin, use @ref GPIO_PIN
276  */
277 void bflb_gpio_reset(struct bflb_device_s *dev, uint8_t pin);
278 
279 /**
280  * @brief Read level from gpio pin.
281  *
282  * @param [in] dev device handle
283  * @param [in] pin gpio pin, use @ref GPIO_PIN
284  * @return true means high level, otherwise low level
285  */
286 bool bflb_gpio_read(struct bflb_device_s *dev, uint8_t pin);
287 
288 /**
289  * @brief Write gpio pin 0~31.
290  *
291  * @param [in] dev device handle
292  * @param [in] val gpio pin 0~31 value
293  */
294 void bflb_gpio_pin0_31_write(struct bflb_device_s *dev, uint32_t val);
295 
296 /**
297  * @brief Write gpio pin 32~63.
298  *
299  * @param [in] dev device handle
300  * @param [in] val gpio pin 32~63 value
301  */
302 void bflb_gpio_pin32_63_write(struct bflb_device_s *dev, uint32_t val);
303 
304 /**
305  * @brief Read level from gpio pin 0~31.
306  *
307  * @param [in] dev device handle
308  * @return level of gpio pin 0~31
309  */
310 uint32_t bflb_gpio_pin0_31_read(struct bflb_device_s *dev);
311 
312 /**
313  * @brief Read level from gpio pin 32~63.
314  *
315  * @param [in] dev device handle
316  * @return level of gpio pin32~63
317  */
318 uint32_t bflb_gpio_pin32_63_read(struct bflb_device_s *dev);
319 
320 /**
321  * @brief Config gpio pin interrupt.
322  *
323  * @param [in] dev device handle
324  * @param [in] pin gpio pin, use @ref GPIO_PIN
325  * @param [in] trig_mode mode to trigger interrupt
326  */
327 void bflb_gpio_int_init(struct bflb_device_s *dev, uint8_t pin, uint8_t trig_mode);
328 
329 /**
330  * @brief Enable or disable gpio pin interrupt.
331  *
332  * @param [in] dev device handle
333  * @param [in] pin gpio pin, use @ref GPIO_PIN
334  * @param [in] mask true means disable, false means enable
335  */
336 void bflb_gpio_int_mask(struct bflb_device_s *dev, uint8_t pin, bool mask);
337 
338 /**
339  * @brief Get gpio pin interrupt status.
340  *
341  * @param [in] dev device handle
342  * @param [in] pin gpio pin, use @ref GPIO_PIN
343  * @return true means yes, false means no
344  */
345 bool bflb_gpio_get_intstatus(struct bflb_device_s *dev, uint8_t pin);
346 
347 /**
348  * @brief Clear gpio pin interrupt status.
349  *
350  * @param [in] dev device handle
351  * @param [in] pin gpio pin, use @ref GPIO_PIN
352  */
353 void bflb_gpio_int_clear(struct bflb_device_s *dev, uint8_t pin);
354 
355 /**
356  * @brief Config gpio pin with uart function.
357  *
358  * @param [in] dev device handle
359  * @param [in] pin gpio pin, use @ref GPIO_PIN
360  * @param [in] uart_func uart function, use @ref GPIO_UART_FUNC
361  */
362 void bflb_gpio_uart_init(struct bflb_device_s *dev, uint8_t pin, uint8_t uart_func);
363 
364 /**
365  * @brief Control gpio feature.
366  *
367  * @param [in] dev device handle
368  * @param [in] cmd feature command, use @ref GPIO_CMD
369  * @param [in] arg user data
370  * @return A negated errno value on failure.
371  */
372 
373 int bflb_gpio_feature_control(struct bflb_device_s *dev, int cmd, size_t arg);
374 
375 #ifdef __cplusplus
376 }
377 #endif
378 
379 /**
380   * @}
381   */
382 
383 /**
384   * @}
385   */
386 
387 #endif
388