HAL_gpio.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /**
  2. ******************************************************************************
  3. * @file HAL_gpio.h
  4. * @author AE Team
  5. * @version V2.0.0
  6. * @date 22/08/2017
  7. * @brief This file contains all the functions prototypes for the GPIO
  8. * firmware library.
  9. ******************************************************************************
  10. * @copy
  11. *
  12. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  13. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  14. * TIME. AS A RESULT, MindMotion SHALL NOT BE HELD LIABLE FOR ANY
  15. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  16. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  17. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  18. *
  19. * <h2><center>&copy; COPYRIGHT 2017 MindMotion</center></h2>
  20. */
  21. /* Define to prevent recursive inclusion -------------------------------------*/
  22. #ifndef __HAL_GPIO_H
  23. #define __HAL_GPIO_H
  24. /* Includes ------------------------------------------------------------------*/
  25. #include "HAL_device.h"
  26. /** @addtogroup StdPeriph_Driver
  27. * @{
  28. */
  29. /** @addtogroup GPIO
  30. * @{
  31. */
  32. /** @defgroup GPIO_Exported_Types
  33. * @{
  34. */
  35. #define IS_GPIO_ALL_PERIPH(PERIPH) (((*(uint32_t*)&(PERIPH)) == GPIOA_BASE) || \
  36. ((*(uint32_t*)&(PERIPH)) == GPIOB_BASE) || \
  37. ((*(uint32_t*)&(PERIPH)) == GPIOC_BASE) || \
  38. ((*(uint32_t*)&(PERIPH)) == GPIOD_BASE) || \
  39. ((*(uint32_t*)&(PERIPH)) == GPIOE_BASE) || \
  40. ((*(uint32_t*)&(PERIPH)) == GPIOF_BASE) || \
  41. ((*(uint32_t*)&(PERIPH)) == GPIOG_BASE))
  42. /**
  43. * @brief Output Maximum frequency selection
  44. */
  45. typedef enum
  46. {
  47. GPIO_Speed_10MHz = 1,
  48. GPIO_Speed_2MHz,
  49. GPIO_Speed_50MHz
  50. }GPIOSpeed_TypeDef;
  51. #define IS_GPIO_SPEED(SPEED) (((SPEED) == GPIO_Speed_10MHz) || ((SPEED) == GPIO_Speed_2MHz) || \
  52. ((SPEED) == GPIO_Speed_50MHz))
  53. /**
  54. * @brief Configuration Mode enumeration
  55. */
  56. typedef enum
  57. { GPIO_Mode_AIN = 0x0, //模拟输入
  58. GPIO_Mode_IN_FLOATING = 0x04, //浮空输入
  59. GPIO_Mode_IPD = 0x28, //下拉输入
  60. GPIO_Mode_IPU = 0x48, //上拉输入
  61. GPIO_Mode_Out_OD = 0x14,//通用开漏输出
  62. GPIO_Mode_Out_PP = 0x10,//通用推免输出
  63. GPIO_Mode_AF_OD = 0x1C, // 复用开漏输出
  64. GPIO_Mode_AF_PP = 0x18 //复用推免输出
  65. }GPIOMode_TypeDef;
  66. #define IS_GPIO_MODE(MODE) (((MODE) == GPIO_Mode_AIN) || ((MODE) == GPIO_Mode_IN_FLOATING) || \
  67. ((MODE) == GPIO_Mode_IPD) || ((MODE) == GPIO_Mode_IPU) || \
  68. ((MODE) == GPIO_Mode_Out_OD) || ((MODE) == GPIO_Mode_Out_PP) || \
  69. ((MODE) == GPIO_Mode_AF_OD) || ((MODE) == GPIO_Mode_AF_PP))
  70. /**
  71. * @brief GPIO Init structure definition
  72. */
  73. typedef struct
  74. {
  75. uint16_t GPIO_Pin;
  76. GPIOSpeed_TypeDef GPIO_Speed;
  77. GPIOMode_TypeDef GPIO_Mode;
  78. }GPIO_InitTypeDef;
  79. /**
  80. * @brief Bit_SET and Bit_RESET enumeration
  81. */
  82. typedef enum
  83. { Bit_RESET = 0,
  84. Bit_SET
  85. }BitAction;
  86. #define IS_GPIO_BIT_ACTION(ACTION) (((ACTION) == Bit_RESET) || ((ACTION) == Bit_SET))
  87. /**
  88. * @}
  89. */
  90. /** @defgroup GPIO_Exported_Constants
  91. * @{
  92. */
  93. /** @defgroup GPIO_pins_define
  94. * @{
  95. */
  96. #define GPIO_Pin_0 ((uint16_t)0x0001) /* Pin 0 selected */
  97. #define GPIO_Pin_1 ((uint16_t)0x0002) /* Pin 1 selected */
  98. #define GPIO_Pin_2 ((uint16_t)0x0004) /* Pin 2 selected */
  99. #define GPIO_Pin_3 ((uint16_t)0x0008) /* Pin 3 selected */
  100. #define GPIO_Pin_4 ((uint16_t)0x0010) /* Pin 4 selected */
  101. #define GPIO_Pin_5 ((uint16_t)0x0020) /* Pin 5 selected */
  102. #define GPIO_Pin_6 ((uint16_t)0x0040) /* Pin 6 selected */
  103. #define GPIO_Pin_7 ((uint16_t)0x0080) /* Pin 7 selected */
  104. #define GPIO_Pin_8 ((uint16_t)0x0100) /* Pin 8 selected */
  105. #define GPIO_Pin_9 ((uint16_t)0x0200) /* Pin 9 selected */
  106. #define GPIO_Pin_10 ((uint16_t)0x0400) /* Pin 10 selected */
  107. #define GPIO_Pin_11 ((uint16_t)0x0800) /* Pin 11 selected */
  108. #define GPIO_Pin_12 ((uint16_t)0x1000) /* Pin 12 selected */
  109. #define GPIO_Pin_13 ((uint16_t)0x2000) /* Pin 13 selected */
  110. #define GPIO_Pin_14 ((uint16_t)0x4000) /* Pin 14 selected */
  111. #define GPIO_Pin_15 ((uint16_t)0x8000) /* Pin 15 selected */
  112. #define GPIO_Pin_All ((uint16_t)0xFFFF) /* All pins selected */
  113. #define IS_GPIO_PIN(PIN) ((((PIN) & (uint16_t)0x00) == 0x00) && ((PIN) != (uint16_t)0x00))
  114. #define IS_GET_GPIO_PIN(PIN) (((PIN) == GPIO_Pin_0) || \
  115. ((PIN) == GPIO_Pin_1) || \
  116. ((PIN) == GPIO_Pin_2) || \
  117. ((PIN) == GPIO_Pin_3) || \
  118. ((PIN) == GPIO_Pin_4) || \
  119. ((PIN) == GPIO_Pin_5) || \
  120. ((PIN) == GPIO_Pin_6) || \
  121. ((PIN) == GPIO_Pin_7) || \
  122. ((PIN) == GPIO_Pin_8) || \
  123. ((PIN) == GPIO_Pin_9) || \
  124. ((PIN) == GPIO_Pin_10) || \
  125. ((PIN) == GPIO_Pin_11) || \
  126. ((PIN) == GPIO_Pin_12) || \
  127. ((PIN) == GPIO_Pin_13) || \
  128. ((PIN) == GPIO_Pin_14) || \
  129. ((PIN) == GPIO_Pin_15))
  130. /**
  131. * @}
  132. */
  133. /** @defgroup GPIO_Remap_define
  134. * @{
  135. */
  136. #define GPIO_Remap_SPI1 ((uint32_t)0x00000001) /* SPI1 Alternate Function mapping */
  137. #define GPIO_Remap_I2C1 ((uint32_t)0x00000002) /* I2C1 Alternate Function mapping */
  138. #define GPIO_Remap_UART1 ((uint32_t)0x00000004) /* UART1 Alternate Function mapping */
  139. #define GPIO_PartialRemap_TIM1 ((uint32_t)0x00160040) /* TIM1 Partial Alternate Function mapping */
  140. #define GPIO_FullRemap_TIM1 ((uint32_t)0x001600C0) /* TIM1 Full Alternate Function mapping */
  141. #define GPIO_PartialRemap1_TIM2 ((uint32_t)0x00180100) /* TIM2 Partial1 Alternate Function mapping */
  142. #define GPIO_PartialRemap2_TIM2 ((uint32_t)0x00180200) /* TIM2 Partial2 Alternate Function mapping */
  143. #define GPIO_FullRemap_TIM2 ((uint32_t)0x00180300) /* TIM2 Full Alternate Function mapping */
  144. #define GPIO_PartialRemap_TIM3 ((uint32_t)0x001A0800) /* TIM3 Partial Alternate Function mapping */
  145. #define GPIO_FullRemap_TIM3 ((uint32_t)0x001A0C00) /* TIM3 Full Alternate Function mapping */
  146. #define GPIO_Remap_PD01 ((uint32_t)0x00008000) /* PD01 Alternate Function mapping */
  147. #define GPIO_Remap_ADC1_ETRGINJ ((uint32_t)0x00200002) /* ADC1 External Trigger Injected Conversion remapping */
  148. #define GPIO_Remap_ADC1_ETRGREG ((uint32_t)0x00200004) /* ADC1 External Trigger Regular Conversion remapping */
  149. #define GPIO_Remap_SWJ_NoJTRST ((uint32_t)0x00300100) /* Full SWJ Enabled (JTAG-DP + SW-DP) but without JTRST */
  150. #define GPIO_Remap_SWJ_JTAGDisable ((uint32_t)0x00300200) /* JTAG-DP Disabled and SW-DP Enabled */
  151. #define GPIO_Remap_SWJ_Disable ((uint32_t)0x00300400) /* Full SWJ Disabled (JTAG-DP + SW-DP) */
  152. #define IS_GPIO_REMAP(REMAP) (((REMAP) == GPIO_Remap_SPI1) || ((REMAP) == GPIO_Remap_I2C1) || \
  153. ((REMAP) == GPIO_Remap_UART1) || ((REMAP) == GPIO_PartialRemap_TIM1) || \
  154. ((REMAP) == GPIO_FullRemap_TIM1) || ((REMAP) == GPIO_PartialRemap1_TIM2) ||\
  155. ((REMAP) == GPIO_PartialRemap2_TIM2) || ((REMAP) == GPIO_FullRemap_TIM2) || \
  156. ((REMAP) == GPIO_PartialRemap_TIM3) || ((REMAP) == GPIO_FullRemap_TIM3) ||\
  157. ((REMAP) == GPIO_Remap_TIM4) || ((REMAP) == GPIO_Remap_PD01) || \
  158. ((REMAP) == GPIO_Remap_ADC1_ETRGINJ) ||((REMAP) == GPIO_Remap_ADC1_ETRGREG) || \
  159. ((REMAP) == GPIO_Remap_SWJ_NoJTRST) || ((REMAP) == GPIO_Remap_SWJ_JTAGDisable)|| \
  160. ((REMAP) == GPIO_Remap_SWJ_Disable))
  161. /**
  162. * @}
  163. */
  164. /** @defgroup GPIO_Port_Sources
  165. * @{
  166. */
  167. #define GPIO_PortSourceGPIOA ((uint8_t)0x00)
  168. #define GPIO_PortSourceGPIOB ((uint8_t)0x01)
  169. #define GPIO_PortSourceGPIOC ((uint8_t)0x02)
  170. #define GPIO_PortSourceGPIOD ((uint8_t)0x03)
  171. #define GPIO_PortSourceGPIOE ((uint8_t)0x04)
  172. #define GPIO_PortSourceGPIOF ((uint8_t)0x05)
  173. #define GPIO_PortSourceGPIOG ((uint8_t)0x06)
  174. #define IS_GPIO_EVENTOUT_PORT_SOURCE(PORTSOURCE) (((PORTSOURCE) == GPIO_PortSourceGPIOA) || \
  175. ((PORTSOURCE) == GPIO_PortSourceGPIOB) || \
  176. ((PORTSOURCE) == GPIO_PortSourceGPIOC) || \
  177. ((PORTSOURCE) == GPIO_PortSourceGPIOD) || \
  178. ((PORTSOURCE) == GPIO_PortSourceGPIOE))
  179. #define IS_GPIO_EXTI_PORT_SOURCE(PORTSOURCE) (((PORTSOURCE) == GPIO_PortSourceGPIOA) || \
  180. ((PORTSOURCE) == GPIO_PortSourceGPIOB) || \
  181. ((PORTSOURCE) == GPIO_PortSourceGPIOC) || \
  182. ((PORTSOURCE) == GPIO_PortSourceGPIOD) || \
  183. ((PORTSOURCE) == GPIO_PortSourceGPIOE) || \
  184. ((PORTSOURCE) == GPIO_PortSourceGPIOF) || \
  185. ((PORTSOURCE) == GPIO_PortSourceGPIOG))
  186. /**
  187. * @}
  188. */
  189. /** @defgroup GPIO_Pin_sources
  190. * @{
  191. */
  192. #define GPIO_PinSource0 ((uint8_t)0x00)
  193. #define GPIO_PinSource1 ((uint8_t)0x01)
  194. #define GPIO_PinSource2 ((uint8_t)0x02)
  195. #define GPIO_PinSource3 ((uint8_t)0x03)
  196. #define GPIO_PinSource4 ((uint8_t)0x04)
  197. #define GPIO_PinSource5 ((uint8_t)0x05)
  198. #define GPIO_PinSource6 ((uint8_t)0x06)
  199. #define GPIO_PinSource7 ((uint8_t)0x07)
  200. #define GPIO_PinSource8 ((uint8_t)0x08)
  201. #define GPIO_PinSource9 ((uint8_t)0x09)
  202. #define GPIO_PinSource10 ((uint8_t)0x0A)
  203. #define GPIO_PinSource11 ((uint8_t)0x0B)
  204. #define GPIO_PinSource12 ((uint8_t)0x0C)
  205. #define GPIO_PinSource13 ((uint8_t)0x0D)
  206. #define GPIO_PinSource14 ((uint8_t)0x0E)
  207. #define GPIO_PinSource15 ((uint8_t)0x0F)
  208. #define IS_GPIO_PIN_SOURCE(PINSOURCE) (((PINSOURCE) == GPIO_PinSource0) || \
  209. ((PINSOURCE) == GPIO_PinSource1) || \
  210. ((PINSOURCE) == GPIO_PinSource2) || \
  211. ((PINSOURCE) == GPIO_PinSource3) || \
  212. ((PINSOURCE) == GPIO_PinSource4) || \
  213. ((PINSOURCE) == GPIO_PinSource5) || \
  214. ((PINSOURCE) == GPIO_PinSource6) || \
  215. ((PINSOURCE) == GPIO_PinSource7) || \
  216. ((PINSOURCE) == GPIO_PinSource8) || \
  217. ((PINSOURCE) == GPIO_PinSource9) || \
  218. ((PINSOURCE) == GPIO_PinSource10) || \
  219. ((PINSOURCE) == GPIO_PinSource11) || \
  220. ((PINSOURCE) == GPIO_PinSource12) || \
  221. ((PINSOURCE) == GPIO_PinSource13) || \
  222. ((PINSOURCE) == GPIO_PinSource14) || \
  223. ((PINSOURCE) == GPIO_PinSource15))
  224. /**
  225. * @}
  226. */
  227. /**
  228. * @}
  229. */
  230. /** @defgroup GPIO_Alternate_function_selection_define
  231. * @{
  232. */
  233. /**
  234. * @brief AF 0 selection
  235. */
  236. #define GPIO_AF_0 ((uint8_t)0x00) /* SPI1, MC0, TIM17_BKIN, SWDIO,SWCLK,
  237. UART1*/
  238. /**
  239. * @brief AF 1 selection
  240. */
  241. #define GPIO_AF_1 ((uint8_t)0x01) /* UART1, TIM3_CH1, TIM3_CH2, TIM3_CH3,
  242. TIM3_CH4, I2C1*/
  243. /**
  244. * @brief AF 2 selection
  245. */
  246. #define GPIO_AF_2 ((uint8_t)0x02) /* TIM2_CH1_ETR, TIM2_CH2, TIM2_CH3,
  247. TIM2_CH3, TIM2_CH4, TIM1_BKIN,
  248. TIM1_CH1N, TIM1_CH1, TIM1_CH2,
  249. TIM1_CH3, TIM1_CH4, TIM1_ETR,
  250. TIM1_CH2N, TIM1_CH3N, TIM2_CH2,
  251. TIM1 6_BKIN, TIM16_CH1N, TIM17_CH1N,
  252. TIM1 6_CH1, TIM17_CH1 */
  253. /**
  254. * @brief AF 3 selection
  255. */
  256. #define GPIO_AF_3 ((uint8_t)0x03) /* */
  257. /**
  258. * @brief AF 4 selection
  259. */
  260. #define GPIO_AF_4 ((uint8_t)0x04) /* TIM14_CH1, I2C1*/
  261. /**
  262. * @brief AF 5 selection
  263. */
  264. #define GPIO_AF_5 ((uint8_t)0x05) /* */
  265. /**
  266. * @brief AF 6 selection
  267. */
  268. #define GPIO_AF_6 ((uint8_t)0x06) /* */
  269. /**
  270. * @brief AF 7 selection
  271. */
  272. #define GPIO_AF_7 ((uint8_t)0x07) /* */
  273. #define IS_GPIO_AF(AF) (((AF) == GPIO_AF_0) || ((AF) == GPIO_AF_1) || \
  274. ((AF) == GPIO_AF_2) || ((AF) == GPIO_AF_3) || \
  275. ((AF) == GPIO_AF_4) || ((AF) == GPIO_AF_5) || \
  276. ((AF) == GPIO_AF_6) || ((AF) == GPIO_AF_7))
  277. /**
  278. * @}
  279. */
  280. /** @defgroup GPIO_Exported_Macros
  281. * @{
  282. */
  283. /**
  284. * @}
  285. */
  286. /** @defgroup GPIO_Exported_Functions
  287. * @{
  288. */
  289. void GPIO_DeInit(GPIO_TypeDef* GPIOx);
  290. void GPIO_AFIODeInit(void);
  291. void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct);
  292. void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct);
  293. uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
  294. uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx);
  295. uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
  296. uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx);
  297. void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
  298. void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
  299. void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal);
  300. void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal);
  301. void GPIO_PinLockConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
  302. void GPIO_PinAFConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_PinSource, uint8_t GPIO_AF);
  303. #endif /* __HAL_GPIO_H */
  304. /**
  305. * @}
  306. */
  307. /**
  308. * @}
  309. */
  310. /**
  311. * @}
  312. */
  313. /*-------------------------(C) COPYRIGHT 2017 MindMotion ----------------------*/