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