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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
/*
* Copyright : (C) 2022 Phytium Information Technology, Inc.
* All Rights Reserved.
*
* This program is OPEN SOURCE software: you can redistribute it and/or modify it
* under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd,
* either version 1.0 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Phytium Public License for more details.
*
*
* FilePath: fgpio.h
* Date: 2022-02-10 14:53:42
* LastEditTime: 2022-02-18 08:25:35
* Description: This files is for GPIO user API definition
*
* Modify History:
* Ver Who Date Changes
* ----- ------ -------- --------------------------------------
* 1.0 zhugengyu 2022-3-1 init commit
*/
#ifndef DRIVERS_FGPIO_H
#define DRIVERS_FGPIO_H
#ifdef __cplusplus
extern "C"
{
#endif
/***************************** Include Files *********************************/
#include "ftypes.h"
#include "fassert.h"
#include "ferror_code.h"
#include "sdkconfig.h"
/************************** Constant Definitions *****************************/
#define FGPIO_SUCCESS FT_SUCCESS
#define FGPIO_ERR_INVALID_PARA FT_MAKE_ERRCODE(ErrModBsp, ErrBspGpio, 0x0)
#define FGPIO_ERR_INVALID_STATE FT_MAKE_ERRCODE(ErrModBsp, ErrBspGpio, 0x1)
#define FGPIO_ERR_NOT_INIT FT_MAKE_ERRCODE(ErrModBsp, ErrBspGpio, 0x2)
#define FGPIO_ERR_ALREADY_INIT FT_MAKE_ERRCODE(ErrModBsp, ErrBspGpio, 0x3)
#if defined(CONFIG_TARGET_F2000_4) || defined(CONFIG_TARGET_D2000)
#define FGPIO_VERSION_1 /* 用于FT2000/4和D2000平台的GPIO 0 ~ 1 */
#elif defined(CONFIG_TARGET_E2000)
#define FGPIO_VERSION_2 /* 用于E2000平台的GPIO 3 ~ 5 */
#else
#error "Invalid target board !!!"
#endif
typedef enum
{
FGPIO_PORT_A = 0,
#if defined(FGPIO_VERSION_1) /* FT2000-4, D2000 */
FGPIO_PORT_B,
#endif
FGPIO_PORT_NUM
} FGpioPortIndex; /* GPIO引脚所在的组 */
typedef enum
{
FGPIO_PIN_0 = 0,
FGPIO_PIN_1,
FGPIO_PIN_2,
FGPIO_PIN_3,
FGPIO_PIN_4,
FGPIO_PIN_5,
FGPIO_PIN_6,
FGPIO_PIN_7,
#if defined(FGPIO_VERSION_2) /* E2000 GPIO 0 ~ 5 */
FGPIO_PIN_8,
FGPIO_PIN_9,
FGPIO_PIN_10,
FGPIO_PIN_11,
FGPIO_PIN_12,
FGPIO_PIN_13,
FGPIO_PIN_14,
FGPIO_PIN_15,
#endif
FGPIO_PIN_NUM
} FGpioPinIndex; /* GPIO引脚号 */
#if defined(FGPIO_VERSION_1) /* FT2000-4, D2000 */
FASSERT_STATIC(8 == FGPIO_PIN_NUM); /* pin 0 ~ 7 */
FASSERT_STATIC(2 == FGPIO_PORT_NUM); /* port a/b */
#elif defined(FGPIO_VERSION_2) /* E2000 GPIO 0 ~ 5 */
FASSERT_STATIC(16 == FGPIO_PIN_NUM); /* pin 0 ~ 15 */
FASSERT_STATIC(1 == FGPIO_PORT_NUM); /* port a */
#endif
typedef enum
{
FGPIO_DIR_INPUT = 0, /* 输入 */
FGPIO_DIR_OUTPUT /* 输出 */
} FGpioDirection; /* GPIO引脚的输入输出方向 */
typedef enum
{
FGPIO_IRQ_TYPE_EDGE_FALLING = 0, /* 上升沿中断,引脚检测到电平从低变高时触发 */
FGPIO_IRQ_TYPE_EDGE_RISING, /* 下降沿中断,引脚检测到电平从高变低时触发 */
FGPIO_IRQ_TYPE_LEVEL_LOW, /* 低电平中断,引脚电平为低时触发 */
FGPIO_IRQ_TYPE_LEVEL_HIGH /* 高电平中断,引脚电平为高时触发 */
} FGpioIrqType; /* GPIO引脚中断类型 */
typedef enum
{
FGPIO_IRQ_NOT_SUPPORT, /* 不支持引脚中断 */
FGPIO_IRQ_BY_CONTROLLER, /* 引脚中断控制器合并上报 */
#if defined(FGPIO_VERSION_2) /* E2000 GPIO 0 ~ 5 */
FGPIO_IRQ_BY_PIN, /* 引脚中断单独上报 */
#endif
} FGpioIrqSourceType;
typedef enum
{
FGPIO_PIN_LOW = 0, /* 低电平 */
FGPIO_PIN_HIGH /* 高电平 */
} FGpioPinVal; /* GPIO引脚电平类型 */
/**************************** Type Definitions *******************************/
typedef struct _FGpioPin FGpioPin;
typedef struct _FGpio FGpio;
typedef struct
{
u32 instance_id; /* GPIO实例ID */
uintptr base_addr; /* GPIO控制器基地址 */
#if defined(FGPIO_VERSION_1) /* FT2000-4, D2000 */
u32 irq_num; /* GPIO控制器中断号 */
#elif defined(FGPIO_VERSION_2) /* E2000 GPIO 0 ~ 5 */
u32 irq_num[FGPIO_PIN_NUM]; /* GPIO各引脚的中断号 */
#endif
u32 irq_priority; /* 中断优先级 */
} FGpioConfig; /* GPIO控制器配置 */
typedef struct
{
u32 ctrl; /* GPIO控制器号 */
FGpioPortIndex port; /* GPIO引脚所在的组 */
FGpioPinIndex pin; /* GPIO引脚号 */
} FGpioPinId; /* GPIO引脚索引 */
typedef void (*FGpioInterruptCallback)(s32 vector, void *param); /* GPIO引脚中断回调函数类型 */
typedef struct _FGpioPin
{
FGpioPinId index; /* 索引 */
u32 is_ready;
FGpio *instance;
FGpioInterruptCallback irq_cb; /* 中断回调函数, Port-A有效 */
void *irq_cb_params; /* 中断回调函数的入参, Port-A有效 */
boolean irq_one_time; /* Port-A有效, TRUE: 进入中断后关闭该引脚的中断,用于电平敏感中断,防止一直进入中断 */
} FGpioPin; /* GPIO引脚实例 */
typedef struct _FGpio
{
FGpioConfig config;
u32 is_ready;
FGpioPin *pins[FGPIO_PORT_NUM][FGPIO_PIN_NUM];
} FGpio; /* GPIO控制器实例 */
/************************** Variable Definitions *****************************/
/***************** Macros (Inline Functions) Definitions *********************/
/* 生成GPIO引脚索引 */
#define FGPIO_PIN(port, pin) \
(FGpioPinId) { \
(port), (pin) \
}
/************************** Function Prototypes ******************************/
/* 获取GPIO控制器的默认配置 */
const FGpioConfig *FGpioLookupConfig(u32 instance_id);
/* 初始化GPIO控制器实例 */
FError FGpioCfgInitialize(FGpio *const instance, const FGpioConfig *const config);
/* 初始化GPIO引脚实例 */
FError FGpioPinInitialize(FGpio *const instance, FGpioPin *const pin,
const FGpioPinId pin_id);
/* 去初始化GPIO引脚实例 */
void FGpioPinDeInitialize(FGpioPin *const pin);
/* 获取引脚中断的上报方式 */
FGpioIrqSourceType FGpioGetPinIrqSourceType(FGpioPinId pin_id);
/* 去初始化GPIO控制器实例 */
void FGpioDeInitialize(FGpio *const instance);
/* 设置GPIO引脚的输入输出方向 */
void FGpioSetDirection(FGpioPin *const pin, FGpioDirection dir);
/* 获取GPIO引脚的输入输出方向 */
FGpioDirection FGpioGetDirection(FGpioPin *const pin);
/* 设置GPIO引脚的输出值 */
FError FGpioSetOutputValue(FGpioPin *const pin, const FGpioPinVal output);
/* 获取GPIO引脚的输入值 */
FGpioPinVal FGpioGetInputValue(FGpioPin *const pin);
/* 获取GPIO A组引脚的中断屏蔽位 */
void FGpioGetInterruptMask(FGpio *const instance, u32 *mask, u32 *enabled);
/* 设置GPIO A组引脚的中断屏蔽位 */
void FGpioSetInterruptMask(FGpioPin *const pin, boolean enable);
/* 获取GPIO A组引脚的中断类型和中断极性 */
void FGpioGetInterruptType(FGpio *const instance, u32 *levels, u32 *polarity);
/* 设置GPIO A组引脚的中断类型 */
void FGpioSetInterruptType(FGpioPin *const pin, const FGpioIrqType type);
/* GPIO控制器中断处理函数 */
void FGpioInterruptHandler(s32 vector, void *param);
#if defined(FGPIO_VERSION_2) /* E2000 GPIO 0 ~ 2 */
/* GPIO引脚中断处理函数 */
void FGpioPinInterruptHandler(s32 vector, void *param);
#endif
/* 注册GPIO A组引脚中断回调函数 */
void FGpioRegisterInterruptCB(FGpioPin *const pin, FGpioInterruptCallback cb,
void *cb_param, boolean irq_one_time);
/* 打印GPIO控制寄存器信息 */
void FGpioDumpRegisters(uintptr base_addr);
#ifdef __cplusplus
}
#endif
#endif
|