1 /*
2 * Copyright (c) 2006-2021, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Email: opensource_embedded@phytium.com.cn
7 *
8 * Change Logs:
9 * Date Author Notes
10 * 2022-12-10 liqiaozhong first commit
11 * 2025-04-08 liqiaozhong
12 *
13 */
14
15 #include "rtconfig.h"
16 #ifdef BSP_USING_GPIO
17 #include <rtthread.h>
18 #include <rtdevice.h>
19 #include <string.h>
20
21 #include "ftypes.h"
22 #include "fkernel.h"
23 #include "board.h"
24
25 #include "fgpio.h"
26 /**************************** Type Definitions *******************************/
27 /************************** Variable Definitions *****************************/
28 static rt_base_t input_pin_index = 0U;
29 static rt_base_t output_pin_index = 0U;
30 /***************** Macros (Inline Functions) Definitions *********************/
31 #define FGPIO_OPS_PIN_PORT_A 0U
32 #define FGPIO_OPS_PIN_PORT_B 1U
33 #define FGPIO_OPS_LEVEL_LOW 0U
34 #define FGPIO_OPS_LEVEL_HIGH 1U
35
36 /* Extract the index number from input_pin_index */
37 #define FGPIO_GET_CTRL(index) (((index) - FGPIO_GET_PIN(index) - (FGPIO_PORT_A * FGPIO_PIN_NUM)) / (FGPIO_PORT_NUM * FGPIO_PIN_NUM))
38
39 /* Extract the pin number from input_pin_index */
40 #define FGPIO_GET_PIN(index) ((index) % FGPIO_PIN_NUM)
41 /***************************** main function *********************************/
gpio_irq_test(s32 vector,void * param)42 static void gpio_irq_test(s32 vector, void *param)
43 {
44 FGpio *gpio_instance = (FGpio *)param;
45 rt_kprintf("GPIO-%d-%d, PIN_IRQ_MODE_RISING Interrupt Asserted!!! \r\n",
46 gpio_instance->config.ctrl,
47 gpio_instance->config.pin);
48 }
49 /* this function will toggle output pin and test intr of input pin */
gpio_toggle_sample()50 rt_err_t gpio_toggle_sample()
51 {
52 rt_err_t res = RT_EOK;
53 static u32 set_level = FGPIO_OPS_LEVEL_LOW;
54 u32 get_level;
55
56 #if defined(FIREFLY_DEMO_BOARD)
57 /* pin init */
58 input_pin_index = FGPIO_ID(FGPIO_CTRL_0, FGPIO_PIN_0);
59 rt_pin_mode(input_pin_index, PIN_MODE_INPUT);
60 rt_pin_attach_irq(input_pin_index, PIN_IRQ_MODE_RISING, gpio_irq_test, RT_NULL);
61 output_pin_index = FGPIO_ID(FGPIO_CTRL_4, FGPIO_PIN_13);
62 rt_pin_mode(output_pin_index, PIN_MODE_OUTPUT);
63 #endif
64
65 #if defined(E2000D_DEMO_BOARD)||defined(E2000Q_DEMO_BOARD)
66 /* pin init */
67 input_pin_index = FGPIO_ID(FGPIO_CTRL_4, FGPIO_PIN_13);
68 rt_pin_mode(input_pin_index, PIN_MODE_INPUT);
69 rt_pin_attach_irq(input_pin_index, PIN_IRQ_MODE_RISING, gpio_irq_test, RT_NULL);
70 output_pin_index = FGPIO_ID(FGPIO_CTRL_4, FGPIO_PIN_10);
71 rt_pin_mode(output_pin_index, PIN_MODE_OUTPUT);
72 #endif
73 /* toggle operation */
74 rt_pin_write(output_pin_index, set_level);
75 set_level = (FGPIO_OPS_LEVEL_LOW == set_level) ? FGPIO_OPS_LEVEL_HIGH : FGPIO_OPS_LEVEL_LOW;
76 rt_pin_irq_enable(input_pin_index, PIN_IRQ_ENABLE);
77 rt_pin_write(output_pin_index, set_level);
78 get_level = rt_pin_read(input_pin_index);
79 if (set_level != get_level)
80 {
81 rt_kprintf(" input level not equals to output level!!!\r\n");
82 res = RT_ERROR;
83 goto exit;
84 }
85 else
86 {
87 rt_kprintf(" ==> Set GPIO-%d-%d to %s \r\n",
88 FGPIO_GET_CTRL(output_pin_index),
89 FGPIO_GET_PIN(output_pin_index),
90 (set_level == FGPIO_PIN_LOW) ? "low" : "high");
91 rt_kprintf(" <== Get GPIO-%d-%d as %s \r\n",
92 FGPIO_GET_CTRL(input_pin_index),
93 FGPIO_GET_PIN(input_pin_index),
94 (get_level == FGPIO_PIN_LOW) ? "low" : "high");
95 }
96 exit:
97 /* print message on example run result */
98 if (res == RT_EOK)
99 {
100 rt_kprintf("%s@%d:rtthread gpio test example [success].\r\n", __func__, __LINE__);
101 }
102 else
103 {
104 rt_kprintf("%s@%d:rtthread gpio test example [failure], res = %d\r\n", __func__, __LINE__, res);
105 }
106
107 return res;
108 }
109
110 MSH_CMD_EXPORT(gpio_toggle_sample, FT GPIO toggle sample.);
111 #endif