fgpio.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright : (C) 2022 Phytium Information Technology, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is OPEN SOURCE software: you can redistribute it and/or modify it
  6. * under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd,
  7. * either version 1.0 of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY;
  10. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. * See the Phytium Public License for more details.
  12. *
  13. *
  14. * FilePath: fgpio.h
  15. * Date: 2022-02-10 14:53:42
  16. * LastEditTime: 2022-02-18 08:25:35
  17. * Description:  This files is for GPIO user API definition
  18. *
  19. * Modify History:
  20. * Ver   Who        Date         Changes
  21. * ----- ------     --------    --------------------------------------
  22. * 1.0 zhugengyu 2022-3-1 init commit
  23. */
  24. #ifndef DRIVERS_FGPIO_H
  25. #define DRIVERS_FGPIO_H
  26. #ifdef __cplusplus
  27. extern "C"
  28. {
  29. #endif
  30. /***************************** Include Files *********************************/
  31. #include "ftypes.h"
  32. #include "fassert.h"
  33. #include "ferror_code.h"
  34. #include "sdkconfig.h"
  35. /************************** Constant Definitions *****************************/
  36. #define FGPIO_SUCCESS FT_SUCCESS
  37. #define FGPIO_ERR_INVALID_PARA FT_MAKE_ERRCODE(ErrModBsp, ErrBspGpio, 0x0)
  38. #define FGPIO_ERR_INVALID_STATE FT_MAKE_ERRCODE(ErrModBsp, ErrBspGpio, 0x1)
  39. #define FGPIO_ERR_NOT_INIT FT_MAKE_ERRCODE(ErrModBsp, ErrBspGpio, 0x2)
  40. #define FGPIO_ERR_ALREADY_INIT FT_MAKE_ERRCODE(ErrModBsp, ErrBspGpio, 0x3)
  41. #if defined(CONFIG_TARGET_F2000_4) || defined(CONFIG_TARGET_D2000)
  42. #define FGPIO_VERSION_1 /* 用于FT2000/4和D2000平台的GPIO 0 ~ 1 */
  43. #elif defined(CONFIG_TARGET_E2000)
  44. #define FGPIO_VERSION_2 /* 用于E2000平台的GPIO 3 ~ 5 */
  45. #else
  46. #error "Invalid target board !!!"
  47. #endif
  48. typedef enum
  49. {
  50. FGPIO_PORT_A = 0,
  51. #if defined(FGPIO_VERSION_1) /* FT2000-4, D2000 */
  52. FGPIO_PORT_B,
  53. #endif
  54. FGPIO_PORT_NUM
  55. } FGpioPortIndex; /* GPIO引脚所在的组 */
  56. typedef enum
  57. {
  58. FGPIO_PIN_0 = 0,
  59. FGPIO_PIN_1,
  60. FGPIO_PIN_2,
  61. FGPIO_PIN_3,
  62. FGPIO_PIN_4,
  63. FGPIO_PIN_5,
  64. FGPIO_PIN_6,
  65. FGPIO_PIN_7,
  66. #if defined(FGPIO_VERSION_2) /* E2000 GPIO 0 ~ 5 */
  67. FGPIO_PIN_8,
  68. FGPIO_PIN_9,
  69. FGPIO_PIN_10,
  70. FGPIO_PIN_11,
  71. FGPIO_PIN_12,
  72. FGPIO_PIN_13,
  73. FGPIO_PIN_14,
  74. FGPIO_PIN_15,
  75. #endif
  76. FGPIO_PIN_NUM
  77. } FGpioPinIndex; /* GPIO引脚号 */
  78. #if defined(FGPIO_VERSION_1) /* FT2000-4, D2000 */
  79. FASSERT_STATIC(8 == FGPIO_PIN_NUM); /* pin 0 ~ 7 */
  80. FASSERT_STATIC(2 == FGPIO_PORT_NUM); /* port a/b */
  81. #elif defined(FGPIO_VERSION_2) /* E2000 GPIO 0 ~ 5 */
  82. FASSERT_STATIC(16 == FGPIO_PIN_NUM); /* pin 0 ~ 15 */
  83. FASSERT_STATIC(1 == FGPIO_PORT_NUM); /* port a */
  84. #endif
  85. typedef enum
  86. {
  87. FGPIO_DIR_INPUT = 0, /* 输入 */
  88. FGPIO_DIR_OUTPUT /* 输出 */
  89. } FGpioDirection; /* GPIO引脚的输入输出方向 */
  90. typedef enum
  91. {
  92. FGPIO_IRQ_TYPE_EDGE_FALLING = 0, /* 上升沿中断,引脚检测到电平从低变高时触发 */
  93. FGPIO_IRQ_TYPE_EDGE_RISING, /* 下降沿中断,引脚检测到电平从高变低时触发 */
  94. FGPIO_IRQ_TYPE_LEVEL_LOW, /* 低电平中断,引脚电平为低时触发 */
  95. FGPIO_IRQ_TYPE_LEVEL_HIGH /* 高电平中断,引脚电平为高时触发 */
  96. } FGpioIrqType; /* GPIO引脚中断类型 */
  97. typedef enum
  98. {
  99. FGPIO_IRQ_NOT_SUPPORT, /* 不支持引脚中断 */
  100. FGPIO_IRQ_BY_CONTROLLER, /* 引脚中断控制器合并上报 */
  101. #if defined(FGPIO_VERSION_2) /* E2000 GPIO 0 ~ 5 */
  102. FGPIO_IRQ_BY_PIN, /* 引脚中断单独上报 */
  103. #endif
  104. } FGpioIrqSourceType;
  105. typedef enum
  106. {
  107. FGPIO_PIN_LOW = 0, /* 低电平 */
  108. FGPIO_PIN_HIGH /* 高电平 */
  109. } FGpioPinVal; /* GPIO引脚电平类型 */
  110. /**************************** Type Definitions *******************************/
  111. typedef struct _FGpioPin FGpioPin;
  112. typedef struct _FGpio FGpio;
  113. typedef struct
  114. {
  115. u32 instance_id; /* GPIO实例ID */
  116. uintptr base_addr; /* GPIO控制器基地址 */
  117. #if defined(FGPIO_VERSION_1) /* FT2000-4, D2000 */
  118. u32 irq_num; /* GPIO控制器中断号 */
  119. #elif defined(FGPIO_VERSION_2) /* E2000 GPIO 0 ~ 5 */
  120. u32 irq_num[FGPIO_PIN_NUM]; /* GPIO各引脚的中断号 */
  121. #endif
  122. u32 irq_priority; /* 中断优先级 */
  123. } FGpioConfig; /* GPIO控制器配置 */
  124. typedef struct
  125. {
  126. u32 ctrl; /* GPIO控制器号 */
  127. FGpioPortIndex port; /* GPIO引脚所在的组 */
  128. FGpioPinIndex pin; /* GPIO引脚号 */
  129. } FGpioPinId; /* GPIO引脚索引 */
  130. typedef void (*FGpioInterruptCallback)(s32 vector, void *param); /* GPIO引脚中断回调函数类型 */
  131. typedef struct _FGpioPin
  132. {
  133. FGpioPinId index; /* 索引 */
  134. u32 is_ready;
  135. FGpio *instance;
  136. FGpioInterruptCallback irq_cb; /* 中断回调函数, Port-A有效 */
  137. void *irq_cb_params; /* 中断回调函数的入参, Port-A有效 */
  138. boolean irq_one_time; /* Port-A有效, TRUE: 进入中断后关闭该引脚的中断,用于电平敏感中断,防止一直进入中断 */
  139. } FGpioPin; /* GPIO引脚实例 */
  140. typedef struct _FGpio
  141. {
  142. FGpioConfig config;
  143. u32 is_ready;
  144. FGpioPin *pins[FGPIO_PORT_NUM][FGPIO_PIN_NUM];
  145. } FGpio; /* GPIO控制器实例 */
  146. /************************** Variable Definitions *****************************/
  147. /***************** Macros (Inline Functions) Definitions *********************/
  148. /* 生成GPIO引脚索引 */
  149. #define FGPIO_PIN(port, pin) \
  150. (FGpioPinId) { \
  151. (port), (pin) \
  152. }
  153. /************************** Function Prototypes ******************************/
  154. /* 获取GPIO控制器的默认配置 */
  155. const FGpioConfig *FGpioLookupConfig(u32 instance_id);
  156. /* 初始化GPIO控制器实例 */
  157. FError FGpioCfgInitialize(FGpio *const instance, const FGpioConfig *const config);
  158. /* 初始化GPIO引脚实例 */
  159. FError FGpioPinInitialize(FGpio *const instance, FGpioPin *const pin,
  160. const FGpioPinId pin_id);
  161. /* 去初始化GPIO引脚实例 */
  162. void FGpioPinDeInitialize(FGpioPin *const pin);
  163. /* 获取引脚中断的上报方式 */
  164. FGpioIrqSourceType FGpioGetPinIrqSourceType(FGpioPinId pin_id);
  165. /* 去初始化GPIO控制器实例 */
  166. void FGpioDeInitialize(FGpio *const instance);
  167. /* 设置GPIO引脚的输入输出方向 */
  168. void FGpioSetDirection(FGpioPin *const pin, FGpioDirection dir);
  169. /* 获取GPIO引脚的输入输出方向 */
  170. FGpioDirection FGpioGetDirection(FGpioPin *const pin);
  171. /* 设置GPIO引脚的输出值 */
  172. FError FGpioSetOutputValue(FGpioPin *const pin, const FGpioPinVal output);
  173. /* 获取GPIO引脚的输入值 */
  174. FGpioPinVal FGpioGetInputValue(FGpioPin *const pin);
  175. /* 获取GPIO A组引脚的中断屏蔽位 */
  176. void FGpioGetInterruptMask(FGpio *const instance, u32 *mask, u32 *enabled);
  177. /* 设置GPIO A组引脚的中断屏蔽位 */
  178. void FGpioSetInterruptMask(FGpioPin *const pin, boolean enable);
  179. /* 获取GPIO A组引脚的中断类型和中断极性 */
  180. void FGpioGetInterruptType(FGpio *const instance, u32 *levels, u32 *polarity);
  181. /* 设置GPIO A组引脚的中断类型 */
  182. void FGpioSetInterruptType(FGpioPin *const pin, const FGpioIrqType type);
  183. /* GPIO控制器中断处理函数 */
  184. void FGpioInterruptHandler(s32 vector, void *param);
  185. #if defined(FGPIO_VERSION_2) /* E2000 GPIO 0 ~ 2 */
  186. /* GPIO引脚中断处理函数 */
  187. void FGpioPinInterruptHandler(s32 vector, void *param);
  188. #endif
  189. /* 注册GPIO A组引脚中断回调函数 */
  190. void FGpioRegisterInterruptCB(FGpioPin *const pin, FGpioInterruptCallback cb,
  191. void *cb_param, boolean irq_one_time);
  192. /* 打印GPIO控制寄存器信息 */
  193. void FGpioDumpRegisters(uintptr base_addr);
  194. #ifdef __cplusplus
  195. }
  196. #endif
  197. #endif