ft32f0xx_gpio.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /**
  2. ******************************************************************************
  3. * @file ft32f0xx_gpio.c
  4. * @author FMD AE
  5. * @brief This file provides firmware functions to manage the following
  6. * functionalities of the GPIO peripheral:
  7. * + Initialization and Configuration functions
  8. * + GPIO Read and Write functions
  9. * + GPIO Alternate functions configuration functions
  10. * @version V1.0.0
  11. * @data 2021-07-01
  12. ******************************************************************************
  13. */
  14. /* Includes ------------------------------------------------------------------*/
  15. #include "ft32f0xx_gpio.h"
  16. #include "ft32f0xx_rcc.h"
  17. /**
  18. * @brief Deinitializes the GPIOx peripheral registers to their default reset
  19. * values.
  20. * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
  21. * @retval None
  22. */
  23. void GPIO_DeInit(GPIO_TypeDef* GPIOx)
  24. {
  25. /* Check the parameters */
  26. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  27. if(GPIOx == GPIOA)
  28. {
  29. RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  30. RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOA, DISABLE);
  31. }
  32. else if(GPIOx == GPIOB)
  33. {
  34. RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOB, ENABLE);
  35. RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOB, DISABLE);
  36. }
  37. else if(GPIOx == GPIOC)
  38. {
  39. RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOC, ENABLE);
  40. RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOC, DISABLE);
  41. }
  42. else if(GPIOx == GPIOD)
  43. {
  44. RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOD, ENABLE);
  45. RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOD, DISABLE);
  46. }
  47. // else if(GPIOx == GPIOE)
  48. // {
  49. // RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOE, ENABLE);
  50. // RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOE, DISABLE);
  51. // }
  52. else
  53. {
  54. if(GPIOx == GPIOF)
  55. {
  56. RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOF, ENABLE);
  57. RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOF, DISABLE);
  58. }
  59. }
  60. }
  61. /**
  62. * @brief Initializes the GPIOx peripheral according to the specified
  63. * parameters in the GPIO_InitStruct.
  64. * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
  65. * @param GPIO_InitStruct: pointer to a GPIO_InitTypeDef structure that contains
  66. * the configuration information for the specified GPIO peripheral.
  67. * @retval None
  68. */
  69. void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
  70. {
  71. uint32_t pinpos = 0x00, pos = 0x00 , currentpin = 0x00;
  72. /* Check the parameters */
  73. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  74. assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));
  75. assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));
  76. assert_param(IS_GPIO_PUPD(GPIO_InitStruct->GPIO_PuPd));
  77. /*-------------------------- Configure the port pins -----------------------*/
  78. /*-- GPIO Mode Configuration --*/
  79. for (pinpos = 0x00; pinpos < 0x10; pinpos++)
  80. {
  81. pos = ((uint32_t)0x01) << pinpos;
  82. /* Get the port pins position */
  83. currentpin = (GPIO_InitStruct->GPIO_Pin) & pos;
  84. if (currentpin == pos)
  85. {
  86. if ((GPIO_InitStruct->GPIO_Mode == GPIO_Mode_OUT) || (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_AF))
  87. {
  88. /* Check Speed mode parameters */
  89. assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));
  90. /* Speed mode configuration */
  91. GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (pinpos * 2));
  92. GPIOx->OSPEEDR |= ((uint32_t)(GPIO_InitStruct->GPIO_Speed) << (pinpos * 2));
  93. /* Check Output mode parameters */
  94. assert_param(IS_GPIO_OTYPE(GPIO_InitStruct->GPIO_OType));
  95. /* Output mode configuration */
  96. GPIOx->OTYPER &= ~((GPIO_OTYPER_OT_0) << ((uint16_t)pinpos));
  97. GPIOx->OTYPER |= (uint16_t)(((uint16_t)GPIO_InitStruct->GPIO_OType) << ((uint16_t)pinpos));
  98. }
  99. GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (pinpos * 2));
  100. GPIOx->MODER |= (((uint32_t)GPIO_InitStruct->GPIO_Mode) << (pinpos * 2));
  101. /* Pull-up Pull down resistor configuration */
  102. GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << ((uint16_t)pinpos * 2));
  103. GPIOx->PUPDR |= (((uint32_t)GPIO_InitStruct->GPIO_PuPd) << (pinpos * 2));
  104. }
  105. }
  106. }
  107. /**
  108. * @brief Fills each GPIO_InitStruct member with its default value.
  109. * @param GPIO_InitStruct: pointer to a GPIO_InitTypeDef structure which will
  110. * be initialized.
  111. * @retval None
  112. */
  113. void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct)
  114. {
  115. /* Reset GPIO init structure parameters values */
  116. GPIO_InitStruct->GPIO_Pin = GPIO_Pin_All;
  117. GPIO_InitStruct->GPIO_Mode = GPIO_Mode_IN;
  118. GPIO_InitStruct->GPIO_Speed = GPIO_Speed_Level_2;
  119. GPIO_InitStruct->GPIO_OType = GPIO_OType_PP;
  120. GPIO_InitStruct->GPIO_PuPd = GPIO_PuPd_NOPULL;
  121. }
  122. /**
  123. * @brief Locks GPIO Pins configuration registers.
  124. * @note The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
  125. * GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
  126. * @note The configuration of the locked GPIO pins can no longer be modified
  127. * until the next device reset.
  128. * @param GPIOx: where x can be (A or B) to select the GPIO peripheral.
  129. * @param GPIO_Pin: specifies the port bit to be written.
  130. * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
  131. * @retval None
  132. */
  133. void GPIO_PinLockConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  134. {
  135. __IO uint32_t tmp = 0x00010000;
  136. /* Check the parameters */
  137. assert_param(IS_GPIO_LIST_PERIPH(GPIOx));
  138. assert_param(IS_GPIO_PIN(GPIO_Pin));
  139. tmp |= GPIO_Pin;
  140. /* Set LCKK bit */
  141. GPIOx->LCKR = tmp;
  142. /* Reset LCKK bit */
  143. GPIOx->LCKR = GPIO_Pin;
  144. /* Set LCKK bit */
  145. GPIOx->LCKR = tmp;
  146. /* Read LCKK bit */
  147. tmp = GPIOx->LCKR;
  148. /* Read LCKK bit */
  149. tmp = GPIOx->LCKR;
  150. }
  151. /**
  152. * @}
  153. */
  154. /**
  155. * @brief Reads the specified input port pin.
  156. * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
  157. * @param GPIO_Pin: specifies the port bit to read.
  158. * @note This parameter can be GPIO_Pin_x where x can be:
  159. * (0..15) for GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, (0..10) for GPIOF.
  160. * @retval The input port pin value.
  161. */
  162. uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  163. {
  164. uint8_t bitstatus = 0x00;
  165. /* Check the parameters */
  166. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  167. assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
  168. if ((GPIOx->IDR & GPIO_Pin) != (uint32_t)Bit_RESET)
  169. {
  170. bitstatus = (uint8_t)Bit_SET;
  171. }
  172. else
  173. {
  174. bitstatus = (uint8_t)Bit_RESET;
  175. }
  176. return bitstatus;
  177. }
  178. /**
  179. * @brief Reads the specified input port pin.
  180. * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
  181. * @retval The input port pin value.
  182. */
  183. uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx)
  184. {
  185. /* Check the parameters */
  186. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  187. return ((uint16_t)GPIOx->IDR);
  188. }
  189. /**
  190. * @brief Reads the specified output data port bit.
  191. * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
  192. * @param GPIO_Pin: Specifies the port bit to read.
  193. * @note This parameter can be GPIO_Pin_x where x can be:
  194. * (0..15) for GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, (0..10) for GPIOF.
  195. * @retval The output port pin value.
  196. */
  197. uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  198. {
  199. uint8_t bitstatus = 0x00;
  200. /* Check the parameters */
  201. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  202. assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
  203. if ((GPIOx->ODR & GPIO_Pin) != (uint32_t)Bit_RESET)
  204. {
  205. bitstatus = (uint8_t)Bit_SET;
  206. }
  207. else
  208. {
  209. bitstatus = (uint8_t)Bit_RESET;
  210. }
  211. return bitstatus;
  212. }
  213. /**
  214. * @brief Reads the specified GPIO output data port.
  215. * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
  216. * @retval GPIO output data port value.
  217. */
  218. uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx)
  219. {
  220. /* Check the parameters */
  221. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  222. return ((uint16_t)GPIOx->ODR);
  223. }
  224. /**
  225. * @brief Sets the selected data port bits.
  226. * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
  227. * @param GPIO_Pin: specifies the port bits to be written.
  228. * @note This parameter can be GPIO_Pin_x where x can be:
  229. * (0..15) for GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, (0..10) for GPIOF.
  230. * @retval None
  231. */
  232. void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  233. {
  234. /* Check the parameters */
  235. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  236. assert_param(IS_GPIO_PIN(GPIO_Pin));
  237. GPIOx->BSRR = GPIO_Pin;
  238. }
  239. /**
  240. * @brief Clears the selected data port bits.
  241. * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
  242. * @param GPIO_Pin: specifies the port bits to be written.
  243. * @note This parameter can be GPIO_Pin_x where x can be:
  244. * (0..15) for GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, (0..10) for GPIOF.
  245. * @retval None
  246. */
  247. void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  248. {
  249. /* Check the parameters */
  250. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  251. assert_param(IS_GPIO_PIN(GPIO_Pin));
  252. GPIOx->BRR = GPIO_Pin;
  253. }
  254. /**
  255. * @brief Sets or clears the selected data port bit.
  256. * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
  257. * @param GPIO_Pin: specifies the port bit to be written.
  258. * @param BitVal: specifies the value to be written to the selected bit.
  259. * This parameter can be one of the BitAction enumeration values:
  260. * @arg Bit_RESET: to clear the port pin
  261. * @arg Bit_SET: to set the port pin
  262. * @note This parameter can be GPIO_Pin_x where x can be:
  263. * (0..15) for GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, (0..10) for GPIOF.
  264. * @retval None
  265. */
  266. void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal)
  267. {
  268. /* Check the parameters */
  269. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  270. assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
  271. assert_param(IS_GPIO_BIT_ACTION(BitVal));
  272. if (BitVal != Bit_RESET)
  273. {
  274. GPIOx->BSRR = GPIO_Pin;
  275. }
  276. else
  277. {
  278. GPIOx->BRR = GPIO_Pin ;
  279. }
  280. }
  281. /**
  282. * @brief Writes data to the specified GPIO data port.
  283. * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
  284. * @param PortVal: specifies the value to be written to the port output data register.
  285. * @retval None
  286. */
  287. void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal)
  288. {
  289. /* Check the parameters */
  290. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  291. GPIOx->ODR = PortVal;
  292. }
  293. /**
  294. * @}
  295. */
  296. /**
  297. * @brief Writes data to the specified GPIO data port.
  298. * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
  299. * @param GPIO_PinSource: specifies the pin for the Alternate function.
  300. * This parameter can be GPIO_PinSourcex where x can be (0..15) for GPIOA, GPIOB, GPIOD, GPIOE
  301. * and (0..12) for GPIOC and (0, 2..5, 9..10) for GPIOF.
  302. * @param GPIO_AF: selects the pin to used as Alternate function.
  303. * This parameter can be one of the following value:
  304. * @arg GPIO_AF_0: WKUP, EVENTOUT, TIM15, SPI1, TIM17, MCO, SWDAT, SWCLK,
  305. * TIM14, BOOT, USART1, CEC, IR_OUT, SPI2, TIM3, USART4,
  306. * CAN, USART2, CRS, TIM16, TIM1, TS, USART8
  307. * @arg GPIO_AF_1: USART2, CEC, TIM3, USART1, USART2, EVENTOUT, I2C1,
  308. * I2C2, TIM15, SPI2, USART3, TS, SPI1, USART7, USART8
  309. * USART5, USART4, USART6, I2C1
  310. * @arg GPIO_AF_2: TIM2, TIM1, EVENTOUT, TIM16, TIM17, USB, USART6, USART5,
  311. * USART8, USART7, USART6
  312. * @arg GPIO_AF_3: TS, I2C1, TIM15, EVENTOUT
  313. * @arg GPIO_AF_4: TIM14, USART4, USART3, CRS, CAN, I2C1, USART5
  314. * @arg GPIO_AF_5: TIM16, TIM17, TIM15, SPI2, I2C2, USART6, MCO
  315. * @arg GPIO_AF_6: EVENTOUT
  316. * @arg GPIO_AF_7: COMP1 OUT, COMP2 OUT
  317. * @note The pin should already been configured in Alternate Function mode(AF)
  318. * using GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF
  319. * @note Refer to the Alternate function mapping table in the device datasheet
  320. * for the detailed mapping of the system and peripherals'alternate
  321. * function I/O pins.
  322. * @retval None
  323. */
  324. void GPIO_PinAFConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_PinSource, uint8_t GPIO_AF)
  325. {
  326. uint32_t temp = 0x00;
  327. uint32_t temp_2 = 0x00;
  328. /* Check the parameters */
  329. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  330. assert_param(IS_GPIO_PIN_SOURCE(GPIO_PinSource));
  331. assert_param(IS_GPIO_AF(GPIO_AF));
  332. temp = ((uint32_t)(GPIO_AF) << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4));
  333. GPIOx->AFR[GPIO_PinSource >> 0x03] &= ~((uint32_t)0xF << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4));
  334. temp_2 = GPIOx->AFR[GPIO_PinSource >> 0x03] | temp;
  335. GPIOx->AFR[GPIO_PinSource >> 0x03] = temp_2;
  336. }
  337. /**
  338. * @param GPIOx:GPIOA,GPIOB
  339. * GPIO_LEDMx:
  340. * GPIO_LEDM_0
  341. * GPIO_LEDM_1
  342. * GPIO_LEDM_3
  343. * GPIO_LEDM_4
  344. * GPIO_LEDM_5
  345. * GPIO_LEDM_6
  346. * GPIO_LEDM_7
  347. * GPIO_LEDM_8
  348. * GPIO_LEDM_9
  349. * GPIO_LEDM_10
  350. * GPIO_LEDM_13
  351. * GPIO_LEDM_14
  352. * GPIO_LEDM_15
  353. */
  354. void GPIO_LedmConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_LEDMx)
  355. {
  356. /* Check the parameters */
  357. assert_param(IS_GPIO_LIST_PERIPH(GPIOx));
  358. assert_param(IS_GPIO_LEDM(GPIO_LEDMx));
  359. GPIOx->LEDM |= (uint16_t)GPIO_LEDMx;
  360. }
  361. /**
  362. * @}
  363. */
  364. /**
  365. * @}
  366. */
  367. /**
  368. * @}
  369. */
  370. /**
  371. * @}
  372. */
  373. /************************ (C) COPYRIGHT FMD *****END OF FILE****/