summaryrefslogtreecommitdiffstats
path: root/humidity_sensors/app/gpio.c
blob: 38d57d3eec9d4299b2111e6a8c587a067a3bae15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "project.h"

u8
GPIO_ReadInputData (GPIO_TypeDef *GPIOx)
{
  return ((u8) GPIOx->IDR);
}

void
GPIO_WriteHigh (GPIO_TypeDef *GPIOx, GPIO_Pin_TypeDef PortPins)
{
  GPIOx->ODR |= (u8) PortPins;
}

void
GPIO_WriteLow (GPIO_TypeDef *GPIOx, GPIO_Pin_TypeDef PortPins)
{
  GPIOx->ODR &= (u8) (~PortPins);
}


/**
  * @brief  Initializes the GPIOx according to the specified parameters.
  * @param  GPIOx : Select the GPIO peripheral number (x = A to I).
  * @param  GPIO_Pin : This parameter contains the pin number, it can be any value
  *         of the @ref GPIO_Pin_TypeDef enumeration.
  * @param  GPIO_Mode : This parameter can be a value of the
  *         @ref GPIO_Mode_TypeDef enumeration.
  * @retval None
  */

void
GPIO_Init (GPIO_TypeDef *GPIOx, GPIO_Pin_TypeDef GPIO_Pin,
           GPIO_Mode_TypeDef GPIO_Mode)
{
  /*----------------------*/
  /* Check the parameters */
  /*----------------------*/

  /* Reset corresponding bit to GPIO_Pin in CR2 register */
  GPIOx->CR2 &= (u8) (~ (GPIO_Pin));

  /*-----------------------------*/
  /* Input/Output mode selection */
  /*-----------------------------*/

  if ((((u8) (GPIO_Mode)) & (u8) 0x80) != (u8) 0x00) { /* Output mode */
    if ((((u8) (GPIO_Mode)) & (u8) 0x10) != (u8) 0x00) /* High level */
      GPIOx->ODR |= (u8) GPIO_Pin;
    else                      /* Low level */
      GPIOx->ODR &= (u8) (~ (GPIO_Pin));

    /* Set Output mode */
    GPIOx->DDR |= (u8) GPIO_Pin;
  } else {                      /* Input mode */
    /* Set Input mode */
    GPIOx->DDR &= (u8) (~ (GPIO_Pin));
  }

  /*------------------------------------------------------------------------*/
  /* Pull-Up/Float (Input) or Push-Pull/Open-Drain (Output) modes selection */
  /*------------------------------------------------------------------------*/

  if ((((u8) (GPIO_Mode)) & (u8) 0x40) != (u8) 0x00) /* Pull-Up or Push-Pull */
    GPIOx->CR1 |= (u8) GPIO_Pin;
  else                          /* Float or Open-Drain */
    GPIOx->CR1 &= (u8) (~ (GPIO_Pin));

  /*-----------------------------------------------------*/
  /* Interrupt (Input) or Slope (Output) modes selection */
  /*-----------------------------------------------------*/

  if ((((u8) (GPIO_Mode)) & (u8) 0x20) != (u8) 0x00) /* Interrupt or Slow slope */
    GPIOx->CR2 |= (u8) GPIO_Pin;
  else                          /* No external interrupt or No slope control */
    GPIOx->CR2 &= (u8) (~ (GPIO_Pin));
}