forked from wudimina/Signal-Generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGPIO_DRV.c
More file actions
167 lines (155 loc) · 4.88 KB
/
GPIO_DRV.c
File metadata and controls
167 lines (155 loc) · 4.88 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/** @file GPIO_DRV.c
* @brief GPIO Driver for the STM32F072RB.
* @author Dennis Law
* @date April 2016
*/
#include "GPIO_DRV.h"
/** @brief Reads the input value of a GPIO pin.
* @param gpio Base pointer to the GPIO to be read.
* @param pinNum The pin number to the GPIO pin.
* @returns 1 if the GPIO is logic high and 0 if otherwise.
*/
int GPIO_readPin(GPIO_TypeDef *gpio, int pinNum)
{
return (int)((gpio->IDR >> pinNum) & 0x000000001);
}
/** @brief Reads the input values of a PORT.
* @param gpio Base pointer to the GPIO to be read.
* @param val Container for storing the output value.
*/
void GPIO_readPort(GPIO_TypeDef *gpio, uint16_t *val)
{
*val = (uint16_t)((gpio->IDR) & 0x0000FFFF);
}
/** @brief Writes a digital value to a pin of a GPIO.
* @param gpio The base pointer to the GPIO of interest.
* @param pinNum The pin number of the GPIO pin of interest.
* @param val The digital value to be written. The values for this
* parameter is defined in GPIO_outVal_t.
*/
void GPIO_writePin(GPIO_TypeDef *gpio, int pinNum, GPIO_outVal_t val)
{
if (val == GPIO_OUTVAL_LOW)
gpio->BSRR |= (1ul << (16+pinNum));
else
gpio->BSRR |= (1ul << pinNum);
}
/** @brief Writes a digital value to all pins of a port.
* @param gpio The base pointer to the GPIO of interest.
* @param The value to be written to the GPIO port. Each bit in this
* variable maps to the corresponding pin number of the GPIO in the
* same order.
*/
void GPIO_writePort(GPIO_TypeDef *gpio, uint16_t val)
{
gpio->ODR = (uint32_t)val;
}
/** @brief Enables the clock to the selected GPIO.
* @param gpio The base pointer to the GPIO of interest.
*/
void GPIO_enableClock(GPIO_TypeDef *gpio)
{
if (gpio == GPIOA)
RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
else if (gpio == GPIOB)
RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
else if (gpio == GPIOC)
RCC->AHBENR |= RCC_AHBENR_GPIOCEN;
else if (gpio == GPIOD)
RCC->AHBENR |= RCC_AHBENR_GPIODEN;
else if (gpio == GPIOE)
RCC->AHBENR |= RCC_AHBENR_GPIOEEN;
else if (gpio == GPIOF)
RCC->AHBENR |= RCC_AHBENR_GPIOFEN;
}
/** @brief Configures the direction of a GPIO pin.
* @param gpio The base pointer to the GPIO to configure.
* @param pinNum The pin number of the GPIO pin to configure.
* @param dir The desired direction of the pin. The values of this
* parameter is defined in GPIO_dir_t.
*/
void GPIO_setDir(GPIO_TypeDef *gpio, int pinNum, GPIO_dir_t dir)
{
switch (dir) {
case GPIO_DIR_INPUT:
gpio->MODER &= ~((3ul << 2*pinNum));
break;
case GPIO_DIR_OUTPUT:
gpio->MODER &= ~((3ul << 2*pinNum));
gpio->MODER |= ((1ul << 2*pinNum));
break;
}
}
/** @brief Sets the output type of a GPIO pin.
* @param gpio The base pointer to the GPIO to configure.
* @param pinNum the pin number of the GPIO pin to configure.
* @param type The desired configuration for the output type. Values for
* this parameter are defined in GPIO_outType_t.
*/
void GPIO_setOutType(GPIO_TypeDef *gpio, int pinNum, GPIO_outType_t type)
{
switch (type) {
case GPIO_OUTTYPE_PUSHPULL:
gpio->OTYPER &= ~((1ul << pinNum));
break;
case GPIO_OUTTYPE_OPENDRAIN:
gpio->OTYPER |= ((1ul << pinNum));
break;
}
}
/** @brief Sets up the GPIO pin speed.
* @param gpio The base pointer to the GPIO to configure.
* @param pinNum The pin number of the GPIO pin to configure.
* @param speed The desired speed configuration. The values for this
* parameter is defined in GPIO_speed_t.
*/
void GPIO_setSpeed(GPIO_TypeDef *gpio, int pinNum, GPIO_speed_t speed)
{
switch (speed) {
case GPIO_SPEED_SLOW:
gpio->OSPEEDR &= ~((3ul << 2*pinNum));
break;
case GPIO_SPEED_MEDIUM:
gpio->OSPEEDR &= ~((3ul << 2*pinNum));
gpio->OSPEEDR |= ((1ul << 2*pinNum));
break;
case GPIO_SPEED_FAST:
gpio->OSPEEDR |= ((3ul << 2*pinNum));
break;
}
}
/** @brief Sets up the pull resistor configuration of a GPIO pin.
* @param gpio The base pointer to the GPIO to configure.
* @param pinNum The pin number of the GPIO pin to configure.
* @param res The desired pull resistor configuration. The values
* for this parameter is defined in GPIO_pullRes_t.
*/
void GPIO_setPullRes(GPIO_TypeDef *gpio, int pinNum, GPIO_pullRes_t res)
{
switch (res) {
case GPIO_PULLRES_DISABLED:
gpio->PUPDR &= ~((3ul << 2*pinNum));
break;
case GPIO_PULLRES_PULLUP:
gpio->PUPDR &= ~((3ul << 2*pinNum));
gpio->PUPDR |= ((1ul << 2*pinNum));
break;
case GPIO_PULLRES_PULLDOWN:
gpio->PUPDR &= ~((3ul << 2*pinNum));
gpio->PUPDR |= ((2ul << 2*pinNum));
break;
}
}
/** @brief Initialized a GPIO pin
* @param gpio The base pointer to the GPIO to initialize.
* @param pinNum The number of the GPIO pin to initialize.
* @param config The configuration of the pin.
*/
void GPIO_initPin(GPIO_TypeDef *gpio, int pinNum, struct GPIO_config conf)
{
GPIO_enableClock(gpio);
GPIO_setDir(gpio, pinNum, conf.dir);
GPIO_setOutType(gpio, pinNum, conf.outType);
GPIO_setSpeed(gpio, pinNum, conf.speed);
GPIO_setPullRes(gpio, pinNum, conf.pullRes);
}