CMSIS/CM4/DeviceSupport/ST/STM32F4xx/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 
128 {
129  /* Check the parameters */
131 
132  if (GPIOx == GPIOA)
133  {
136  }
137  else if (GPIOx == GPIOB)
138  {
141  }
142  else if (GPIOx == GPIOC)
143  {
146  }
147  else if (GPIOx == GPIOD)
148  {
151  }
152  else if (GPIOx == GPIOE)
153  {
156  }
157  else if (GPIOx == GPIOF)
158  {
161  }
162  else if (GPIOx == GPIOG)
163  {
166  }
167  else if (GPIOx == GPIOH)
168  {
171  }
172 
173  else if (GPIOx == GPIOI)
174  {
177  }
178  else if (GPIOx == GPIOJ)
179  {
182  }
183  else
184  {
185  if (GPIOx == GPIOK)
186  {
189  }
190  }
191 }
192 
202 void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
203 {
204  uint32_t pinpos = 0x00, pos = 0x00 , currentpin = 0x00;
205 
206  /* Check the parameters */
208  assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));
209  assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));
210  assert_param(IS_GPIO_PUPD(GPIO_InitStruct->GPIO_PuPd));
211 
212  /* ------------------------- Configure the port pins ---------------- */
213  /*-- GPIO Mode Configuration --*/
214  for (pinpos = 0x00; pinpos < 0x10; pinpos++)
215  {
216  pos = ((uint32_t)0x01) << pinpos;
217  /* Get the port pins position */
218  currentpin = (GPIO_InitStruct->GPIO_Pin) & pos;
219 
220  if (currentpin == pos)
221  {
222  GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (pinpos * 2));
223  GPIOx->MODER |= (((uint32_t)GPIO_InitStruct->GPIO_Mode) << (pinpos * 2));
224 
225  if ((GPIO_InitStruct->GPIO_Mode == GPIO_Mode_OUT) || (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_AF))
226  {
227  /* Check Speed mode parameters */
228  assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));
229 
230  /* Speed mode configuration */
231  GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (pinpos * 2));
232  GPIOx->OSPEEDR |= ((uint32_t)(GPIO_InitStruct->GPIO_Speed) << (pinpos * 2));
233 
234  /* Check Output mode parameters */
235  assert_param(IS_GPIO_OTYPE(GPIO_InitStruct->GPIO_OType));
236 
237  /* Output mode configuration*/
238  GPIOx->OTYPER &= ~((GPIO_OTYPER_OT_0) << ((uint16_t)pinpos)) ;
239  GPIOx->OTYPER |= (uint16_t)(((uint16_t)GPIO_InitStruct->GPIO_OType) << ((uint16_t)pinpos));
240  }
241 
242  /* Pull-up Pull down resistor configuration*/
243  GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << ((uint16_t)pinpos * 2));
244  GPIOx->PUPDR |= (((uint32_t)GPIO_InitStruct->GPIO_PuPd) << (pinpos * 2));
245  }
246  }
247 }
248 
254 void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct)
255 {
256  /* Reset GPIO init structure parameters values */
257  GPIO_InitStruct->GPIO_Pin = GPIO_Pin_All;
258  GPIO_InitStruct->GPIO_Mode = GPIO_Mode_IN;
259  GPIO_InitStruct->GPIO_Speed = GPIO_Speed_2MHz;
260  GPIO_InitStruct->GPIO_OType = GPIO_OType_PP;
261  GPIO_InitStruct->GPIO_PuPd = GPIO_PuPd_NOPULL;
262 }
263 
278 {
279  __IO uint32_t tmp = 0x00010000;
280 
281  /* Check the parameters */
283  assert_param(IS_GPIO_PIN(GPIO_Pin));
284 
285  tmp |= GPIO_Pin;
286  /* Set LCKK bit */
287  GPIOx->LCKR = tmp;
288  /* Reset LCKK bit */
289  GPIOx->LCKR = GPIO_Pin;
290  /* Set LCKK bit */
291  GPIOx->LCKR = tmp;
292  /* Read LCKK bit*/
293  tmp = GPIOx->LCKR;
294  /* Read LCKK bit*/
295  tmp = GPIOx->LCKR;
296 }
297 
323 uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
324 {
325  uint8_t bitstatus = 0x00;
326 
327  /* Check the parameters */
329  assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
330 
331  if ((GPIOx->IDR & GPIO_Pin) != (uint32_t)Bit_RESET)
332  {
333  bitstatus = (uint8_t)Bit_SET;
334  }
335  else
336  {
337  bitstatus = (uint8_t)Bit_RESET;
338  }
339  return bitstatus;
340 }
341 
350 {
351  /* Check the parameters */
353 
354  return ((uint16_t)GPIOx->IDR);
355 }
356 
367 {
368  uint8_t bitstatus = 0x00;
369 
370  /* Check the parameters */
372  assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
373 
374  if (((GPIOx->ODR) & GPIO_Pin) != (uint32_t)Bit_RESET)
375  {
376  bitstatus = (uint8_t)Bit_SET;
377  }
378  else
379  {
380  bitstatus = (uint8_t)Bit_RESET;
381  }
382  return bitstatus;
383 }
384 
393 {
394  /* Check the parameters */
396 
397  return ((uint16_t)GPIOx->ODR);
398 }
399 
412 void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
413 {
414  /* Check the parameters */
416  assert_param(IS_GPIO_PIN(GPIO_Pin));
417 
418  GPIOx->BSRRL = GPIO_Pin;
419 }
420 
433 void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
434 {
435  /* Check the parameters */
437  assert_param(IS_GPIO_PIN(GPIO_Pin));
438 
439  GPIOx->BSRRH = GPIO_Pin;
440 }
441 
455 void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal)
456 {
457  /* Check the parameters */
459  assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
461 
462  if (BitVal != Bit_RESET)
463  {
464  GPIOx->BSRRL = GPIO_Pin;
465  }
466  else
467  {
468  GPIOx->BSRRH = GPIO_Pin ;
469  }
470 }
471 
480 void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal)
481 {
482  /* Check the parameters */
484 
485  GPIOx->ODR = PortVal;
486 }
487 
496 void GPIO_ToggleBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
497 {
498  /* Check the parameters */
500 
501  GPIOx->ODR ^= GPIO_Pin;
502 }
503 
579 void GPIO_PinAFConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_PinSource, uint8_t GPIO_AF)
580 {
581  uint32_t temp = 0x00;
582  uint32_t temp_2 = 0x00;
583 
584  /* Check the parameters */
586  assert_param(IS_GPIO_PIN_SOURCE(GPIO_PinSource));
587  assert_param(IS_GPIO_AF(GPIO_AF));
588 
589  temp = ((uint32_t)(GPIO_AF) << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4)) ;
590  GPIOx->AFR[GPIO_PinSource >> 0x03] &= ~((uint32_t)0xF << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4)) ;
591  temp_2 = GPIOx->AFR[GPIO_PinSource >> 0x03] | temp;
592  GPIOx->AFR[GPIO_PinSource >> 0x03] = temp_2;
593 }
594 
611 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
#define GPIOK
Definition: stm32f4xx.h:2120
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
#define GPIOJ
Definition: stm32f4xx.h:2119
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