STM32F4xx_StdPeriph_Driver/src/stm32f4xx_gpio.c
Go to the documentation of this file.
1 
83 /* Includes ------------------------------------------------------------------*/
84 #include "stm32f4xx_gpio.h"
85 #include "stm32f4xx_rcc.h"
86 
96 /* Private typedef -----------------------------------------------------------*/
97 /* Private define ------------------------------------------------------------*/
98 /* Private macro -------------------------------------------------------------*/
99 /* Private variables ---------------------------------------------------------*/
100 /* Private function prototypes -----------------------------------------------*/
101 /* Private functions ---------------------------------------------------------*/
102 
126 void GPIO_DeInit(GPIO_TypeDef* GPIOx)
127 {
128  /* Check the parameters */
130 
131  if (GPIOx == GPIOA)
132  {
135  }
136  else if (GPIOx == GPIOB)
137  {
140  }
141  else if (GPIOx == GPIOC)
142  {
145  }
146  else if (GPIOx == GPIOD)
147  {
150  }
151  else if (GPIOx == GPIOE)
152  {
155  }
156  else if (GPIOx == GPIOF)
157  {
160  }
161  else if (GPIOx == GPIOG)
162  {
165  }
166  else if (GPIOx == GPIOH)
167  {
170  }
171  else
172  {
173  if (GPIOx == GPIOI)
174  {
177  }
178  }
179 }
180 
189 void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
190 {
191  uint32_t pinpos = 0x00, pos = 0x00 , currentpin = 0x00;
192 
193  /* Check the parameters */
195  assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));
196  assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));
197  assert_param(IS_GPIO_PUPD(GPIO_InitStruct->GPIO_PuPd));
198 
199  /* ------------------------- Configure the port pins ---------------- */
200  /*-- GPIO Mode Configuration --*/
201  for (pinpos = 0x00; pinpos < 0x10; pinpos++)
202  {
203  pos = ((uint32_t)0x01) << pinpos;
204  /* Get the port pins position */
205  currentpin = (GPIO_InitStruct->GPIO_Pin) & pos;
206 
207  if (currentpin == pos)
208  {
209  GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (pinpos * 2));
210  GPIOx->MODER |= (((uint32_t)GPIO_InitStruct->GPIO_Mode) << (pinpos * 2));
211 
212  if ((GPIO_InitStruct->GPIO_Mode == GPIO_Mode_OUT) || (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_AF))
213  {
214  /* Check Speed mode parameters */
215  assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));
216 
217  /* Speed mode configuration */
218  GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (pinpos * 2));
219  GPIOx->OSPEEDR |= ((uint32_t)(GPIO_InitStruct->GPIO_Speed) << (pinpos * 2));
220 
221  /* Check Output mode parameters */
222  assert_param(IS_GPIO_OTYPE(GPIO_InitStruct->GPIO_OType));
223 
224  /* Output mode configuration*/
225  GPIOx->OTYPER &= ~((GPIO_OTYPER_OT_0) << ((uint16_t)pinpos)) ;
226  GPIOx->OTYPER |= (uint16_t)(((uint16_t)GPIO_InitStruct->GPIO_OType) << ((uint16_t)pinpos));
227  }
228 
229  /* Pull-up Pull down resistor configuration*/
230  GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << ((uint16_t)pinpos * 2));
231  GPIOx->PUPDR |= (((uint32_t)GPIO_InitStruct->GPIO_PuPd) << (pinpos * 2));
232  }
233  }
234 }
235 
241 void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct)
242 {
243  /* Reset GPIO init structure parameters values */
244  GPIO_InitStruct->GPIO_Pin = GPIO_Pin_All;
245  GPIO_InitStruct->GPIO_Mode = GPIO_Mode_IN;
246  GPIO_InitStruct->GPIO_Speed = GPIO_Speed_2MHz;
247  GPIO_InitStruct->GPIO_OType = GPIO_OType_PP;
248  GPIO_InitStruct->GPIO_PuPd = GPIO_PuPd_NOPULL;
249 }
250 
263 void GPIO_PinLockConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
264 {
265  __IO uint32_t tmp = 0x00010000;
266 
267  /* Check the parameters */
269  assert_param(IS_GPIO_PIN(GPIO_Pin));
270 
271  tmp |= GPIO_Pin;
272  /* Set LCKK bit */
273  GPIOx->LCKR = tmp;
274  /* Reset LCKK bit */
275  GPIOx->LCKR = GPIO_Pin;
276  /* Set LCKK bit */
277  GPIOx->LCKR = tmp;
278  /* Read LCKK bit*/
279  tmp = GPIOx->LCKR;
280  /* Read LCKK bit*/
281  tmp = GPIOx->LCKR;
282 }
283 
308 uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
309 {
310  uint8_t bitstatus = 0x00;
311 
312  /* Check the parameters */
314  assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
315 
316  if ((GPIOx->IDR & GPIO_Pin) != (uint32_t)Bit_RESET)
317  {
318  bitstatus = (uint8_t)Bit_SET;
319  }
320  else
321  {
322  bitstatus = (uint8_t)Bit_RESET;
323  }
324  return bitstatus;
325 }
326 
333 uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx)
334 {
335  /* Check the parameters */
337 
338  return ((uint16_t)GPIOx->IDR);
339 }
340 
349 uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
350 {
351  uint8_t bitstatus = 0x00;
352 
353  /* Check the parameters */
355  assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
356 
357  if (((GPIOx->ODR) & GPIO_Pin) != (uint32_t)Bit_RESET)
358  {
359  bitstatus = (uint8_t)Bit_SET;
360  }
361  else
362  {
363  bitstatus = (uint8_t)Bit_RESET;
364  }
365  return bitstatus;
366 }
367 
374 uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx)
375 {
376  /* Check the parameters */
378 
379  return ((uint16_t)GPIOx->ODR);
380 }
381 
393 void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
394 {
395  /* Check the parameters */
397  assert_param(IS_GPIO_PIN(GPIO_Pin));
398 
399  GPIOx->BSRRL = GPIO_Pin;
400 }
401 
413 void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
414 {
415  /* Check the parameters */
417  assert_param(IS_GPIO_PIN(GPIO_Pin));
418 
419  GPIOx->BSRRH = GPIO_Pin;
420 }
421 
434 void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal)
435 {
436  /* Check the parameters */
438  assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
440 
441  if (BitVal != Bit_RESET)
442  {
443  GPIOx->BSRRL = GPIO_Pin;
444  }
445  else
446  {
447  GPIOx->BSRRH = GPIO_Pin ;
448  }
449 }
450 
458 void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal)
459 {
460  /* Check the parameters */
462 
463  GPIOx->ODR = PortVal;
464 }
465 
473 void GPIO_ToggleBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
474 {
475  /* Check the parameters */
477 
478  GPIOx->ODR ^= GPIO_Pin;
479 }
480 
552 void GPIO_PinAFConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_PinSource, uint8_t GPIO_AF)
553 {
554  uint32_t temp = 0x00;
555  uint32_t temp_2 = 0x00;
556 
557  /* Check the parameters */
559  assert_param(IS_GPIO_PIN_SOURCE(GPIO_PinSource));
560  assert_param(IS_GPIO_AF(GPIO_AF));
561 
562  temp = ((uint32_t)(GPIO_AF) << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4)) ;
563  GPIOx->AFR[GPIO_PinSource >> 0x03] &= ~((uint32_t)0xF << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4)) ;
564  temp_2 = GPIOx->AFR[GPIO_PinSource >> 0x03] | temp;
565  GPIOx->AFR[GPIO_PinSource >> 0x03] = temp_2;
566 }
567 
584 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
void GPIO_PinAFConfig(GPIO_TypeDef *GPIOx, uint16_t GPIO_PinSource, uint8_t GPIO_AF)
Changes the mapping of the specified pin.
__IO uint32_t ODR
Definition: stm32f4xx.h:1288
#define GPIOA
Definition: stm32f4xx.h:2110
uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
Reads the specified input port pin.
BitAction
GPIO Bit SET and Bit RESET enumeration.
#define GPIOB
Definition: stm32f4xx.h:2111
#define GPIOG
Definition: stm32f4xx.h:2116
void RCC_AHB1PeriphResetCmd(uint32_t RCC_AHB1Periph, FunctionalState NewState)
Forces or releases AHB1 peripheral reset.
__IO uint16_t BSRRH
Definition: stm32f4xx.h:1290
void assert_param(int val)
#define GPIOC
Definition: stm32f4xx.h:2112
__IO uint32_t MODER
Definition: stm32f4xx.h:1283
void GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_InitStruct)
Initializes the GPIOx peripheral according to the specified parameters in the GPIO_InitStruct.
__IO uint16_t BSRRL
Definition: stm32f4xx.h:1289
#define GPIOI
Definition: stm32f4xx.h:2118
#define GPIO_OSPEEDER_OSPEEDR0
Definition: stm32f4xx.h:6507
void GPIO_StructInit(GPIO_InitTypeDef *GPIO_InitStruct)
Fills each GPIO_InitStruct member with its default value.
void GPIO_Write(GPIO_TypeDef *GPIOx, uint16_t PortVal)
Writes data to the specified GPIO data port.
#define GPIO_MODER_MODER0
Definition: stm32f4xx.h:6424
__IO uint32_t PUPDR
Definition: stm32f4xx.h:1286
General Purpose I/O.
Definition: stm32f4xx.h:1281
#define GPIOD
Definition: stm32f4xx.h:2113
#define GPIOH
Definition: stm32f4xx.h:2117
#define __IO
Definition: core_cm0.h:198
#define GPIO_OTYPER_OT_0
Definition: stm32f4xx.h:6489
GPIO_Pin
Definition: drv_gpio.h:41
void GPIO_WriteBit(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, BitAction BitVal)
Sets or clears the selected data port bit.
static volatile int16_t temp
Definition: drv_mpu6050.c:278
#define GPIO_PUPDR_PUPDR0
Definition: stm32f4xx.h:6572
void GPIO_PinLockConfig(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
Locks GPIO Pins configuration registers.
#define GPIOF
Definition: stm32f4xx.h:2115
#define GPIOE
Definition: stm32f4xx.h:2114
uint16_t GPIO_ReadInputData(GPIO_TypeDef *GPIOx)
Reads the specified GPIO input data port.
void GPIO_DeInit(GPIO_TypeDef *GPIOx)
De-initializes the GPIOx peripheral registers to their default reset values.
__IO uint32_t LCKR
Definition: stm32f4xx.h:1291
uint16_t GPIO_ReadOutputData(GPIO_TypeDef *GPIOx)
Reads the specified GPIO output data port.
void GPIO_ResetBits(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
Clears the selected data port bits.
void GPIO_ToggleBits(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
Toggles the specified GPIO pins..
void GPIO_SetBits(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
Sets the selected data port bits.
__IO uint32_t IDR
Definition: stm32f4xx.h:1287
uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
Reads the specified output data port bit.
__IO uint32_t OTYPER
Definition: stm32f4xx.h:1284
__IO uint32_t AFR[2]
Definition: stm32f4xx.h:1292
__IO uint32_t OSPEEDR
Definition: stm32f4xx.h:1285


rosflight_firmware
Author(s): Daniel Koch , James Jackson
autogenerated on Thu Apr 15 2021 05:07:49