yc_gpio.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. File Name : yc_gpio.c
  3. Author : Yichip
  4. Version : V1.0
  5. Date : 2019/12/03
  6. Description : gpio encapsulation.
  7. */
  8. #ifndef __YC_GPIO_H__
  9. #define __YC_GPIO_H__
  10. #include "yc3121.h"
  11. /**
  12. * @brief Configuration Mode enumeration
  13. */
  14. typedef enum
  15. {
  16. GPIO_Mode_IN_FLOATING = 0x01,
  17. GPIO_Mode_IPU = 0x02,
  18. GPIO_Mode_IPD = 0x03,
  19. GPIO_Mode_AIN = 0x04,
  20. GPIO_Mode_Out_PP = 0x05 /*!< analog signal mode */
  21. } GPIO_ModeTypeDef;
  22. #define IS_GPIO_MODE(mode) (((mode) == GPIO_Mode_IN_FLOATING) || \
  23. ((mode) == GPIO_Mode_IPU) || \
  24. ((mode) == GPIO_Mode_IPD) || \
  25. ((mode) == GPIO_Mode_Out_PP) || \
  26. ((mode) == GPIO_Mode_AIN))
  27. /**
  28. * @brief Bit_SET and Bit_RESET enumeration
  29. */
  30. typedef enum
  31. {
  32. Bit_RESET = 0,
  33. Bit_SET
  34. } BitAction;
  35. /**
  36. * @brief gpio output enumeration
  37. */
  38. typedef enum
  39. {
  40. OutputLow = 0,
  41. OutputHigh = 1
  42. } GPIO_OutputTypeDef;
  43. #define IS_GPIO_WAKE_MODE(MODE) (((MODE) == GPIO_WakeMode_Now) || \
  44. ((MODE) == GPIO_WakeMode_AfterGlitch))
  45. /**
  46. * @brief GPIO function enumeration
  47. */
  48. typedef enum
  49. {
  50. INPUT = GPCFG_INPUT,
  51. QSPI_NCS = GPCFG_QSPI_NCS,
  52. QSPI_SCK = GPCFG_QSPI_SCK,
  53. QSPI_IO0 = GPCFG_QSPI_IO0,
  54. QSPI_IO1 = GPCFG_QSPI_IO1,
  55. QSPI_IO2 = GPCFG_QSPI_IO2,
  56. QSPI_IO3 = GPCFG_QSPI_IO3,
  57. UART0_TXD = GPCFG_UART0_TXD,
  58. UART0_RXD = GPCFG_UART0_RXD,
  59. UART0_RTS = GPCFG_UART0_RTS,
  60. UART0_CTS = GPCFG_UART0_CTS,
  61. UART1_TXD = GPCFG_UART1_TXD,
  62. UART1_RXD = GPCFG_UART1_RXD,
  63. UART1_RTS = GPCFG_UART1_RTS,
  64. UART1_CTS = GPCFG_UART1_CTS,
  65. PWM_OUT0 = GPCFG_PWM_OUT0,
  66. PWM_OUT1 = GPCFG_PWM_OUT1,
  67. PWM_OUT2 = GPCFG_PWM_OUT2,
  68. PWM_OUT3 = GPCFG_PWM_OUT3,
  69. PWM_OUT4 = GPCFG_PWM_OUT4,
  70. PWM_OUT5 = GPCFG_PWM_OUT5,
  71. PWM_OUT6 = GPCFG_PWM_OUT6,
  72. PWM_OUT7 = GPCFG_PWM_OUT7,
  73. PWM_OUT8 = GPCFG_PWM_OUT8,
  74. SPID0_NCS = GPCFG_SPID0_NCS,
  75. SPID0_SCK = GPCFG_SPID0_SCK,
  76. SPID0_MOSI = GPCFG_SPID0_MOSI,
  77. SPID0_SDIO = GPCFG_SPID0_SDIO,
  78. SPID0_MISO = GPCFG_SPID0_MISO,
  79. SPID0_NCSIN = GPCFG_SPID0_NCSIN,
  80. SPID0_SCKIN = GPCFG_SPID0_SCKIN,
  81. SPID1_NCS = GPCFG_SPID1_NCS,
  82. SPID1_SCK = GPCFG_SPID1_SCK,
  83. SPID1_MOSI = GPCFG_SPID1_MOSI,
  84. SPID1_SDIO = GPCFG_SPID1_SDIO,
  85. SPID1_MISO = GPCFG_SPID1_MISO,
  86. SPID1_NCSIN = GPCFG_SPID1_NCSIN,
  87. SPID1_SCKIN = GPCFG_SPID1_SCKIN,
  88. NFC_CLK_OUT = GPCFG_NFC_CLK_OUT,
  89. SCI7816_IO = GPCFG_SCI7816_IO,
  90. IIC_SCL = GPCFG_IIC_SCL,
  91. IIC_SDA = GPCFG_IIC_SDA,
  92. JTAG_SWCLK = GPCFG_JTAG_SWCLK,
  93. JTAG_SWDAT = GPCFG_JTAG_SWDAT,
  94. OUTPUT_LOW = GPCFG_OUTPUT_LOW,
  95. OUTPUT_HIGH = GPCFG_OUTPUT_HIGH,
  96. PULL_UP = GPCFG_PU,
  97. PULL_DOWN = GPCFG_PD,
  98. ANALOG = GPCFG_ANALOG
  99. } GPIO_FunTypeDef;
  100. #define GPIO_Pin_0 ((uint16_t)0x0001) /*!< Pin 0 selected */
  101. #define GPIO_Pin_1 ((uint16_t)0x0002) /*!< Pin 1 selected */
  102. #define GPIO_Pin_2 ((uint16_t)0x0004) /*!< Pin 2 selected */
  103. #define GPIO_Pin_3 ((uint16_t)0x0008) /*!< Pin 3 selected */
  104. #define GPIO_Pin_4 ((uint16_t)0x0010) /*!< Pin 4 selected */
  105. #define GPIO_Pin_5 ((uint16_t)0x0020) /*!< Pin 5 selected */
  106. #define GPIO_Pin_6 ((uint16_t)0x0040) /*!< Pin 6 selected */
  107. #define GPIO_Pin_7 ((uint16_t)0x0080) /*!< Pin 7 selected */
  108. #define GPIO_Pin_8 ((uint16_t)0x0100) /*!< Pin 8 selected */
  109. #define GPIO_Pin_9 ((uint16_t)0x0200) /*!< Pin 9 selected */
  110. #define GPIO_Pin_10 ((uint16_t)0x0400) /*!< Pin 10 selected */
  111. #define GPIO_Pin_11 ((uint16_t)0x0800) /*!< Pin 11 selected */
  112. #define GPIO_Pin_12 ((uint16_t)0x1000) /*!< Pin 12 selected */
  113. #define GPIO_Pin_13 ((uint16_t)0x2000) /*!< Pin 13 selected */
  114. #define GPIO_Pin_14 ((uint16_t)0x4000) /*!< Pin 14 selected */
  115. #define GPIO_Pin_15 ((uint16_t)0x8000) /*!< Pin 15 selected */
  116. #define GPIO_Pin_All ((uint16_t)0xffff) /*!< Pin All selected */
  117. #define IS_GPIO_PIN(PIN) (((((PIN) & ~(uint16_t)0xFFFF)) == 0x00) && ((PIN) != (uint16_t)0x00))
  118. #define IS_GET_GPIO_PIN(PIN) (((PIN) == GPIO_Pin_0) || \
  119. ((PIN) == GPIO_Pin_1) || \
  120. ((PIN) == GPIO_Pin_2) || \
  121. ((PIN) == GPIO_Pin_3) || \
  122. ((PIN) == GPIO_Pin_4) || \
  123. ((PIN) == GPIO_Pin_5) || \
  124. ((PIN) == GPIO_Pin_6) || \
  125. ((PIN) == GPIO_Pin_7) || \
  126. ((PIN) == GPIO_Pin_8) || \
  127. ((PIN) == GPIO_Pin_9) || \
  128. ((PIN) == GPIO_Pin_10) || \
  129. ((PIN) == GPIO_Pin_11) || \
  130. ((PIN) == GPIO_Pin_12) || \
  131. ((PIN) == GPIO_Pin_13) || \
  132. ((PIN) == GPIO_Pin_14) || \
  133. ((PIN) == GPIO_Pin_15))
  134. /**
  135. * @brief GPIO group enumeration
  136. */
  137. typedef enum
  138. {
  139. GPIOA = 0,
  140. GPIOB,
  141. GPIOC
  142. } GPIO_TypeDef;
  143. #define ISGPIOGROUP(groupx) (groupx < GPIO_GROUP_NUM)
  144. /**
  145. * @brief GPIO Init structure definition
  146. */
  147. typedef struct
  148. {
  149. uint16_t GPIO_Pin;
  150. GPIO_ModeTypeDef GPIO_Mode;
  151. } GPIO_InitTypeDef;
  152. /**
  153. * @brief config gpio function(Only one can be configured at a time)
  154. *
  155. * @param GPIOx: where x can be (GPIOA...GPIOF) to select the GPIO group.
  156. *
  157. * @param GPIO_Pin: select the pin to read.(GPIO_Pin_0...GPIO_Pin_15)(Only one can be configured at a time)
  158. *
  159. * @param function:gpio function
  160. *
  161. * @retval none
  162. */
  163. void GPIO_Config(GPIO_TypeDef GPIOx, uint16_t GPIO_Pin, GPIO_FunTypeDef function);
  164. /**
  165. * @brief gpio mode Init
  166. *
  167. * @param GPIOx: where x can be (GPIOA...GPIOF) to select the GPIO group.
  168. *
  169. * @param GPIO_InitStruct:GPIO_InitStruct
  170. *
  171. * @retval none
  172. */
  173. void GPIO_Init(GPIO_TypeDef GPIOx, GPIO_InitTypeDef *GPIO_InitStruct);
  174. /**
  175. * @brief
  176. *
  177. * @param GPIOx: where x can be (GPIOA...GPIOF) to select the GPIO group.
  178. *
  179. * @param GPIO_Pin: select the pin to read.(GPIO_Pin_0...GPIO_Pin_7)
  180. *
  181. * @param NewState: new state of the port pin Pull Up.(ENABLE or DISABLE)
  182. *
  183. * @retval
  184. */
  185. void GPIO_PullUpCmd(GPIO_TypeDef GPIOx, uint16_t GPIO_Pin, FunctionalState NewState);
  186. /**
  187. * @brief Reads the GPIO input data(status) for byte.
  188. *
  189. * @param GPIOx: where x can be (GPIOA...GPIOF) to select the GPIO group.
  190. *
  191. * @retval GPIO input data(status).
  192. */
  193. uint16_t GPIO_ReadInputData(GPIO_TypeDef GPIOx);
  194. /**
  195. * @brief Reads the GPIO input data(status) for bit.
  196. *
  197. * @param GPIOx: where x can be (GPIOA...GPIOF) to select the GPIO group.
  198. *
  199. * @param GPIO_Pin: select the pin to read.(GPIO_Pin_0...GPIO_Pin_15)
  200. *
  201. * @retval The input status
  202. */
  203. uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef GPIOx, uint16_t GPIO_Pin);
  204. /**
  205. * @brief Reads the GPIO output data(status) for byte.
  206. *
  207. * @param GPIOx: where x can be (GPIOA...GPIOF) to select the GPIO group.
  208. *
  209. * @retval GPIO output data(status).
  210. */
  211. uint16_t GPIO_ReadOutputData(GPIO_TypeDef GPIOx);
  212. /**
  213. * @brief Reads the GPIO output data(status) for bit.
  214. *
  215. * @param GPIOx: where x can be (GPIOA...GPIOF) to select the GPIO group.
  216. *
  217. * @param GPIO_Pin: select the pin to read.(GPIO_Pin_0...GPIO_Pin_15)
  218. *
  219. * @retval The output status
  220. */
  221. uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef GPIOx, uint16_t GPIO_Pin);
  222. /**
  223. * @brief Clears the selected pin(only output mode)
  224. *
  225. * @param GPIOx: where x can be (GPIOA...GPIOF) to select the GPIO group.
  226. *
  227. * @param GPIO_Pin: select the pin to read.(GPIO_Pin_0...GPIO_Pin_15)
  228. *
  229. * @retval None
  230. */
  231. void GPIO_ResetBits(GPIO_TypeDef GPIOx, uint16_t GPIO_Pin);
  232. /**
  233. * @brief sets the selected pin(only output mode)
  234. *
  235. * @param GPIOx: where x can be (GPIOA...GPIOF) to select the GPIO group.
  236. *
  237. * @param GPIO_Pin: select the pin to read.(GPIO_Pin_0...GPIO_Pin_15)
  238. *
  239. * @retval None
  240. */
  241. void GPIO_SetBits(GPIO_TypeDef GPIOx, uint16_t GPIO_Pin);
  242. /**
  243. * @brief init GPIO_InitStruct to default value.
  244. * @param GPIO_InitStruct : pointer to a GPIO_InitTypeDef structure which will be initialized.
  245. * @retval None
  246. */
  247. void GPIO_StructInit(GPIO_InitTypeDef *GPIO_InitStruct);
  248. /**
  249. * @brief Writes data to the GPIO group port(only output mode)
  250. *
  251. * @param GPIOx: where x can be (GPIOA...GPIOF) to select the GPIO group.
  252. *
  253. * @param value: specifies the value to be written to the port output data register.
  254. *
  255. * @retval None
  256. */
  257. void GPIO_Write(GPIO_TypeDef GPIOx, uint16_t value);
  258. /**
  259. * @brief Sets or clears the selected data port bit(only output mode)
  260. *
  261. * @param GPIOx: where x can be (GPIOA...GPIOF) to select the GPIO group.
  262. *
  263. * @param GPIO_Pin: select the pin to read.(GPIO_Pin_0...GPIO_Pin_7)
  264. *
  265. * @param BitVal: specifies the value to be written to the selected bit.
  266. * This parameter can be one of the BitAction enum values:
  267. * @arg Bit_RESET: to clear the port pin
  268. * @arg Bit_SET: to set the port pin
  269. * @retval None
  270. */
  271. void GPIO_WriteBit(GPIO_TypeDef GPIOx, uint16_t GPIO_Pin, BitAction BitVal);
  272. #endif /* __YC_GPIO_H__ */