stm32f0xx_gpio.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_gpio.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 23-March-2012
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the GPIO peripheral:
  9. * + Initialization and Configuration functions
  10. * + GPIO Read and Write functions
  11. * + GPIO Alternate functions configuration functions
  12. *
  13. * @verbatim
  14. *
  15. *
  16. ===========================================================================
  17. ##### How to use this driver #####
  18. ===========================================================================
  19. [..]
  20. (#) Enable the GPIO AHB clock using RCC_AHBPeriphClockCmd()
  21. (#) Configure the GPIO pin(s) using GPIO_Init()
  22. Four possible configuration are available for each pin:
  23. (++) Input: Floating, Pull-up, Pull-down.
  24. (++) Output: Push-Pull (Pull-up, Pull-down or no Pull)
  25. Open Drain (Pull-up, Pull-down or no Pull).
  26. In output mode, the speed is configurable: Low, Medium, Fast or High.
  27. (++) Alternate Function: Push-Pull (Pull-up, Pull-down or no Pull)
  28. Open Drain (Pull-up, Pull-down or no Pull).
  29. (++) Analog: required mode when a pin is to be used as ADC channel,
  30. DAC output or comparator input.
  31. (#) Peripherals alternate function:
  32. (++) For ADC, DAC and comparators, configure the desired pin in analog
  33. mode using GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AN
  34. (++) For other peripherals (TIM, USART...):
  35. (+++) Connect the pin to the desired peripherals' Alternate
  36. Function (AF) using GPIO_PinAFConfig() function. For PortC,
  37. PortD and PortF, no configuration is needed.
  38. (+++) Configure the desired pin in alternate function mode using
  39. GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF
  40. (+++) Select the type, pull-up/pull-down and output speed via
  41. GPIO_PuPd, GPIO_OType and GPIO_Speed members
  42. (+++) Call GPIO_Init() function
  43. (#) To get the level of a pin configured in input mode use GPIO_ReadInputDataBit()
  44. (#) To set/reset the level of a pin configured in output mode use
  45. GPIO_SetBits()/GPIO_ResetBits()
  46. (#) During and just after reset, the alternate functions are not active and
  47. the GPIO pins are configured in input floating mode (except JTAG pins).
  48. (#) The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as
  49. general-purpose (PC14 and PC15, respectively) when the LSE oscillator
  50. is off. The LSE has priority over the GPIO function.
  51. (#) The HSE oscillator pins OSC_IN/OSC_OUT can be used as general-purpose
  52. PD0 and PD1, respectively, when the HSE oscillator is off. The HSE has
  53. priority over the GPIO function.
  54. @endverbatim
  55. ******************************************************************************
  56. * @attention
  57. *
  58. * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  59. *
  60. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  61. * You may not use this file except in compliance with the License.
  62. * You may obtain a copy of the License at:
  63. *
  64. * http://www.st.com/software_license_agreement_liberty_v2
  65. *
  66. * Unless required by applicable law or agreed to in writing, software
  67. * distributed under the License is distributed on an "AS IS" BASIS,
  68. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  69. * See the License for the specific language governing permissions and
  70. * limitations under the License.
  71. *
  72. ******************************************************************************
  73. */
  74. /* Includes ------------------------------------------------------------------*/
  75. #include "stm32f0xx_gpio.h"
  76. #include "stm32f0xx_rcc.h"
  77. /** @addtogroup STM32F0xx_StdPeriph_Driver
  78. * @{
  79. */
  80. /** @defgroup GPIO
  81. * @brief GPIO driver modules
  82. * @{
  83. */
  84. /* Private typedef -----------------------------------------------------------*/
  85. /* Private define ------------------------------------------------------------*/
  86. /* Private macro -------------------------------------------------------------*/
  87. /* Private variables ---------------------------------------------------------*/
  88. /* Private function prototypes -----------------------------------------------*/
  89. /* Private functions ---------------------------------------------------------*/
  90. /** @defgroup GPIO_Private_Functions
  91. * @{
  92. */
  93. /** @defgroup GPIO_Group1 Initialization and Configuration
  94. * @brief Initialization and Configuration
  95. *
  96. @verbatim
  97. ===============================================================================
  98. ##### Initialization and Configuration #####
  99. ===============================================================================
  100. @endverbatim
  101. * @{
  102. */
  103. /**
  104. * @brief Deinitializes the GPIOx peripheral registers to their default reset
  105. * values.
  106. * @param GPIOx: where x can be (A, B, C, D or F) to select the GPIO peripheral.
  107. * @retval None
  108. */
  109. void GPIO_DeInit(GPIO_TypeDef* GPIOx)
  110. {
  111. /* Check the parameters */
  112. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  113. if(GPIOx == GPIOA)
  114. {
  115. RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  116. RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOA, DISABLE);
  117. }
  118. else if(GPIOx == GPIOB)
  119. {
  120. RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOB, ENABLE);
  121. RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOB, DISABLE);
  122. }
  123. else if(GPIOx == GPIOC)
  124. {
  125. RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOC, ENABLE);
  126. RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOC, DISABLE);
  127. }
  128. else if(GPIOx == GPIOD)
  129. {
  130. RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOD, ENABLE);
  131. RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOD, DISABLE);
  132. }
  133. else
  134. {
  135. if(GPIOx == GPIOF)
  136. {
  137. RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOF, ENABLE);
  138. RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOF, DISABLE);
  139. }
  140. }
  141. }
  142. /**
  143. * @brief Initializes the GPIOx peripheral according to the specified
  144. * parameters in the GPIO_InitStruct.
  145. * @param GPIOx: where x can be (A, B, C, D or F) to select the GPIO peripheral.
  146. * @param GPIO_InitStruct: pointer to a GPIO_InitTypeDef structure that contains
  147. * the configuration information for the specified GPIO peripheral.
  148. * @note The configured pins can be: GPIO_Pin_0 -> GPIO_Pin_15 for GPIOA, GPIOB and GPIOC,
  149. * GPIO_Pin_0 -> GPIO_Pin_2 for GPIOD, GPIO_Pin_0 -> GPIO_Pin_3 for GPIOF.
  150. * @retval None
  151. */
  152. void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
  153. {
  154. uint32_t pinpos = 0x00, pos = 0x00 , currentpin = 0x00;
  155. /* Check the parameters */
  156. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  157. assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));
  158. assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));
  159. assert_param(IS_GPIO_PUPD(GPIO_InitStruct->GPIO_PuPd));
  160. /*-------------------------- Configure the port pins -----------------------*/
  161. /*-- GPIO Mode Configuration --*/
  162. for (pinpos = 0x00; pinpos < 0x10; pinpos++)
  163. {
  164. pos = ((uint32_t)0x01) << pinpos;
  165. /* Get the port pins position */
  166. currentpin = (GPIO_InitStruct->GPIO_Pin) & pos;
  167. if (currentpin == pos)
  168. {
  169. if ((GPIO_InitStruct->GPIO_Mode == GPIO_Mode_OUT) || (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_AF))
  170. {
  171. /* Check Speed mode parameters */
  172. assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));
  173. /* Speed mode configuration */
  174. GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (pinpos * 2));
  175. GPIOx->OSPEEDR |= ((uint32_t)(GPIO_InitStruct->GPIO_Speed) << (pinpos * 2));
  176. /* Check Output mode parameters */
  177. assert_param(IS_GPIO_OTYPE(GPIO_InitStruct->GPIO_OType));
  178. /* Output mode configuration */
  179. GPIOx->OTYPER &= ~((GPIO_OTYPER_OT_0) << ((uint16_t)pinpos));
  180. GPIOx->OTYPER |= (uint16_t)(((uint16_t)GPIO_InitStruct->GPIO_OType) << ((uint16_t)pinpos));
  181. }
  182. GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (pinpos * 2));
  183. GPIOx->MODER |= (((uint32_t)GPIO_InitStruct->GPIO_Mode) << (pinpos * 2));
  184. /* Pull-up Pull down resistor configuration */
  185. GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << ((uint16_t)pinpos * 2));
  186. GPIOx->PUPDR |= (((uint32_t)GPIO_InitStruct->GPIO_PuPd) << (pinpos * 2));
  187. }
  188. }
  189. }
  190. /**
  191. * @brief Fills each GPIO_InitStruct member with its default value.
  192. * @param GPIO_InitStruct: pointer to a GPIO_InitTypeDef structure which will
  193. * be initialized.
  194. * @retval None
  195. */
  196. void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct)
  197. {
  198. /* Reset GPIO init structure parameters values */
  199. GPIO_InitStruct->GPIO_Pin = GPIO_Pin_All;
  200. GPIO_InitStruct->GPIO_Mode = GPIO_Mode_IN;
  201. GPIO_InitStruct->GPIO_Speed = GPIO_Speed_Level_2;
  202. GPIO_InitStruct->GPIO_OType = GPIO_OType_PP;
  203. GPIO_InitStruct->GPIO_PuPd = GPIO_PuPd_NOPULL;
  204. }
  205. /**
  206. * @brief Locks GPIO Pins configuration registers.
  207. * The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
  208. * GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
  209. * @note The configuration of the locked GPIO pins can no longer be modified
  210. * until the next reset.
  211. * @param GPIOx: where x can be (A or B) to select the GPIO peripheral.
  212. * @param GPIO_Pin: specifies the port bit to be written.
  213. * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
  214. * @retval None
  215. */
  216. void GPIO_PinLockConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  217. {
  218. __IO uint32_t tmp = 0x00010000;
  219. /* Check the parameters */
  220. assert_param(IS_GPIO_LIST_PERIPH(GPIOx));
  221. assert_param(IS_GPIO_PIN(GPIO_Pin));
  222. tmp |= GPIO_Pin;
  223. /* Set LCKK bit */
  224. GPIOx->LCKR = tmp;
  225. /* Reset LCKK bit */
  226. GPIOx->LCKR = GPIO_Pin;
  227. /* Set LCKK bit */
  228. GPIOx->LCKR = tmp;
  229. /* Read LCKK bit */
  230. tmp = GPIOx->LCKR;
  231. /* Read LCKK bit */
  232. tmp = GPIOx->LCKR;
  233. }
  234. /**
  235. * @}
  236. */
  237. /** @defgroup GPIO_Group2 GPIO Read and Write
  238. * @brief GPIO Read and Write
  239. *
  240. @verbatim
  241. ===============================================================================
  242. ##### GPIO Read and Write #####
  243. ===============================================================================
  244. @endverbatim
  245. * @{
  246. */
  247. /**
  248. * @brief Reads the specified input port pin.
  249. * @param GPIOx: where x can be (A, B, C, D or F) to select the GPIO peripheral.
  250. * @param GPIO_Pin: specifies the port bit to read.
  251. * @note This parameter can be GPIO_Pin_x where x can be:(0..15) for GPIOA,
  252. * GPIOB or GPIOC,(0..2) for GPIOD and(0..3) for GPIOF.
  253. * @retval The input port pin value.
  254. */
  255. uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  256. {
  257. uint8_t bitstatus = 0x00;
  258. /* Check the parameters */
  259. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  260. assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
  261. if ((GPIOx->IDR & GPIO_Pin) != (uint32_t)Bit_RESET)
  262. {
  263. bitstatus = (uint8_t)Bit_SET;
  264. }
  265. else
  266. {
  267. bitstatus = (uint8_t)Bit_RESET;
  268. }
  269. return bitstatus;
  270. }
  271. /**
  272. * @brief Reads the specified input port pin.
  273. * @param GPIOx: where x can be (A, B, C, D or F) to select the GPIO peripheral.
  274. * @retval The input port pin value.
  275. */
  276. uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx)
  277. {
  278. /* Check the parameters */
  279. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  280. return ((uint16_t)GPIOx->IDR);
  281. }
  282. /**
  283. * @brief Reads the specified output data port bit.
  284. * @param GPIOx: where x can be (A, B, C, D or F) to select the GPIO peripheral.
  285. * @param GPIO_Pin: Specifies the port bit to read.
  286. * @note This parameter can be GPIO_Pin_x where x can be:(0..15) for GPIOA,
  287. * GPIOB or GPIOC,(0..2) for GPIOD and(0..3) for GPIOF.
  288. * @retval The output port pin value.
  289. */
  290. uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  291. {
  292. uint8_t bitstatus = 0x00;
  293. /* Check the parameters */
  294. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  295. assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
  296. if ((GPIOx->ODR & GPIO_Pin) != (uint32_t)Bit_RESET)
  297. {
  298. bitstatus = (uint8_t)Bit_SET;
  299. }
  300. else
  301. {
  302. bitstatus = (uint8_t)Bit_RESET;
  303. }
  304. return bitstatus;
  305. }
  306. /**
  307. * @brief Reads the specified GPIO output data port.
  308. * @param GPIOx: where x can be (A, B, C, D or F) to select the GPIO peripheral.
  309. * @retval GPIO output data port value.
  310. */
  311. uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx)
  312. {
  313. /* Check the parameters */
  314. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  315. return ((uint16_t)GPIOx->ODR);
  316. }
  317. /**
  318. * @brief Sets the selected data port bits.
  319. * @param GPIOx: where x can be (A, B, C, D or F) to select the GPIO peripheral.
  320. * @param GPIO_Pin: specifies the port bits to be written.
  321. * @note This parameter can be GPIO_Pin_x where x can be:(0..15) for GPIOA,
  322. * GPIOB or GPIOC,(0..2) for GPIOD and(0..3) for GPIOF.
  323. * @retval None
  324. */
  325. void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  326. {
  327. /* Check the parameters */
  328. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  329. assert_param(IS_GPIO_PIN(GPIO_Pin));
  330. GPIOx->BSRR = GPIO_Pin;
  331. }
  332. /**
  333. * @brief Clears the selected data port bits.
  334. * @param GPIOx: where x can be (A, B, C, D or F) to select the GPIO peripheral.
  335. * @param GPIO_Pin: specifies the port bits to be written.
  336. * @note This parameter can be GPIO_Pin_x where x can be: (0..15) for GPIOA,
  337. * GPIOB or GPIOC,(0..2) for GPIOD and(0..3) for GPIOF.
  338. * @retval None
  339. */
  340. void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  341. {
  342. /* Check the parameters */
  343. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  344. assert_param(IS_GPIO_PIN(GPIO_Pin));
  345. GPIOx->BRR = GPIO_Pin;
  346. }
  347. /**
  348. * @brief Sets or clears the selected data port bit.
  349. * @param GPIOx: where x can be (A, B, C, D or F) to select the GPIO peripheral.
  350. * @param GPIO_Pin: specifies the port bit to be written.
  351. * @param BitVal: specifies the value to be written to the selected bit.
  352. * This parameter can be one of the BitAction enumeration values:
  353. * @arg Bit_RESET: to clear the port pin
  354. * @arg Bit_SET: to set the port pin
  355. * @note The GPIO_Pin parameter can be GPIO_Pin_x where x can be: (0..15) for GPIOA,
  356. * GPIOB or GPIOC,(0..2) for GPIOD and(0..3) for GPIOF.
  357. * @retval None
  358. */
  359. void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal)
  360. {
  361. /* Check the parameters */
  362. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  363. assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
  364. assert_param(IS_GPIO_BIT_ACTION(BitVal));
  365. if (BitVal != Bit_RESET)
  366. {
  367. GPIOx->BSRR = GPIO_Pin;
  368. }
  369. else
  370. {
  371. GPIOx->BRR = GPIO_Pin ;
  372. }
  373. }
  374. /**
  375. * @brief Writes data to the specified GPIO data port.
  376. * @param GPIOx: where x can be (A, B, C, D or F) to select the GPIO peripheral.
  377. * @param PortVal: specifies the value to be written to the port output data
  378. * register.
  379. * @retval None
  380. */
  381. void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal)
  382. {
  383. /* Check the parameters */
  384. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  385. GPIOx->ODR = PortVal;
  386. }
  387. /**
  388. * @}
  389. */
  390. /** @defgroup GPIO_Group3 GPIO Alternate functions configuration functions
  391. * @brief GPIO Alternate functions configuration functions
  392. *
  393. @verbatim
  394. ===============================================================================
  395. ##### GPIO Alternate functions configuration functions #####
  396. ===============================================================================
  397. @endverbatim
  398. * @{
  399. */
  400. /**
  401. * @brief Writes data to the specified GPIO data port.
  402. * @param GPIOx: where x can be (A or B) to select the GPIO peripheral.
  403. * @param GPIO_PinSource: specifies the pin for the Alternate function.
  404. * This parameter can be GPIO_PinSourcex where x can be (0..15).
  405. * @param GPIO_AF: selects the pin to used as Alternate function.
  406. * This parameter can be one of the following value:
  407. * @arg GPIO_AF_0:WKUP, EVENTOUT, TIM15, SPI1, TIM17,MCO, SWDAT, SWCLK, TIM14,
  408. * BOOT,USART1, CEC, IR_OUT, SPI2
  409. * @arg GPIO_AF_1:USART2, CEC, Tim3, USART1, USART2,EVENTOUT, I2C1, I2C2, TIM15
  410. * @arg GPIO_AF_2:TIM2, TIM1, EVENTOUT, TIM16, TIM17.
  411. * @arg GPIO_AF_3:TS, I2C1, TIM15, EVENTOUT
  412. * @arg GPIO_AF_4:TIM14.
  413. * @arg GPIO_AF_5:TIM16, TIM17.
  414. * @arg GPIO_AF_6:EVENTOUT.
  415. * @arg GPIO_AF_7:COMP1 OUT, COMP2 OUT
  416. * @note The pin should already been configured in Alternate Function mode(AF)
  417. * using GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF
  418. * @note Refer to the Alternate function mapping table in the device datasheet
  419. * for the detailed mapping of the system and peripherals'alternate
  420. * function I/O pins.
  421. * @retval None
  422. */
  423. void GPIO_PinAFConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_PinSource, uint8_t GPIO_AF)
  424. {
  425. uint32_t temp = 0x00;
  426. uint32_t temp_2 = 0x00;
  427. /* Check the parameters */
  428. assert_param(IS_GPIO_LIST_PERIPH(GPIOx));
  429. assert_param(IS_GPIO_PIN_SOURCE(GPIO_PinSource));
  430. assert_param(IS_GPIO_AF(GPIO_AF));
  431. temp = ((uint32_t)(GPIO_AF) << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4));
  432. GPIOx->AFR[GPIO_PinSource >> 0x03] &= ~((uint32_t)0xF << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4));
  433. temp_2 = GPIOx->AFR[GPIO_PinSource >> 0x03] | temp;
  434. GPIOx->AFR[GPIO_PinSource >> 0x03] = temp_2;
  435. }
  436. /**
  437. * @}
  438. */
  439. /**
  440. * @}
  441. */
  442. /**
  443. * @}
  444. */
  445. /**
  446. * @}
  447. */
  448. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/