-
Notifications
You must be signed in to change notification settings - Fork 0
/
stm32f030xx_gpio_driver.c
324 lines (286 loc) · 10.1 KB
/
stm32f030xx_gpio_driver.c
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
* stm32f030xx_gpio_driver.c
*
* Created on: Jan 1, 2024
* Author: Aditya
*/
#include "stm32f030xx_gpio_driver.h"
/* GPIO initialization and de-initialization */
/********************************************************************
* Function Name : GPIO_Init
* Brief : Initializes a particular GPIO port / pin
* Input Parameters : Configuration parameters for a GPIO port
* Return Parameters : None
* Note / Remarks : None
********************************************************************/
void GPIO_Init (GPIO_Handle_t *pGPIOHandle)
{
uint32_t TempReg = 0u;
// Configure the mode of the GPIO pin
if (pGPIOHandle -> GPIO_PinConfig.GPIO_PinMode <= GPIO_MODE_ANALOG) /* Non-interrupt modes */
{
TempReg = pGPIOHandle -> GPIO_PinConfig.GPIO_PinMode << (2 * pGPIOHandle -> GPIO_PinConfig.GPIO_PinNumber);
pGPIOHandle -> pGPIOx->MODER |= TempReg;
// Clear TempReg
TempReg = 0u;
}
else
{
if (pGPIOHandle -> GPIO_PinConfig.GPIO_PinMode == GPIO_MODE_IT_FT)
{
EXTI->EXTI_FTSR |= (1u << pGPIOHandle -> GPIO_PinConfig.GPIO_PinNumber);
// Clear the corresponding RTSR bit
EXTI->EXTI_RTSR &= ~(1u << pGPIOHandle -> GPIO_PinConfig.GPIO_PinNumber);
}
else if (pGPIOHandle -> GPIO_PinConfig.GPIO_PinMode == GPIO_MODE_IT_RT)
{
EXTI->EXTI_RTSR |= (1u << pGPIOHandle -> GPIO_PinConfig.GPIO_PinNumber);
// Clear the corresponding FTSR bit
EXTI->EXTI_FTSR &= ~(1u << pGPIOHandle -> GPIO_PinConfig.GPIO_PinNumber);
}
else if (pGPIOHandle -> GPIO_PinConfig.GPIO_PinMode == GPIO_MODE_IT_RFT)
{
EXTI->EXTI_FTSR |= (1u << pGPIOHandle -> GPIO_PinConfig.GPIO_PinNumber);
EXTI->EXTI_RTSR |= (1u << pGPIOHandle -> GPIO_PinConfig.GPIO_PinNumber);
}
// Configure the GPIO port selection in SYSCFG_EXTICR
SYSCFG_PCLK_EN(); /* Enable PCLK for SYSCFG peripheral */
uint8_t temp1, temp2, portcode;
temp1 = pGPIOHandle -> GPIO_PinConfig.GPIO_PinNumber / 4;
temp2 = pGPIOHandle -> GPIO_PinConfig.GPIO_PinNumber % 4;
portcode = GPIO_BASEADDR_TO_PR(pGPIOHandle->pGPIOx);
SYSCFG ->SYSCFG_EXTICR[temp1] = portcode << (4 * temp2);
// Enable interrupt delivery in the Interrupt Mask Register
EXTI->EXTI_IMR |= (1u << pGPIOHandle -> GPIO_PinConfig.GPIO_PinNumber);
}
// Configure the speed of the GPIO pin
TempReg = pGPIOHandle -> GPIO_PinConfig.GPIO_PinSpeed << (2 * pGPIOHandle -> GPIO_PinConfig.GPIO_PinNumber);
pGPIOHandle -> pGPIOx->OSPEEDR |= TempReg;
TempReg = 0u;
// Configure the PUPD settings
TempReg = pGPIOHandle -> GPIO_PinConfig.GPIO_PinPuPdControl << (2 * pGPIOHandle -> GPIO_PinConfig.GPIO_PinNumber);
pGPIOHandle -> pGPIOx->PUPDR |= TempReg;
TempReg = 0u;
// Configure the output type
TempReg = pGPIOHandle -> GPIO_PinConfig.GPIO_PinOPType << (pGPIOHandle -> GPIO_PinConfig.GPIO_PinNumber);
pGPIOHandle -> pGPIOx->OTYPER |= TempReg;
TempReg = 0u;
// Configure the alternate functionality (if required)
if (pGPIOHandle -> GPIO_PinConfig.GPIO_PinMode == GPIO_MODE_ALTFN)
{
uint8_t temp1 = 0u, temp2 = 0u;
temp1 = (pGPIOHandle -> GPIO_PinConfig.GPIO_PinNumber) / 8;
temp2 = (pGPIOHandle -> GPIO_PinConfig.GPIO_PinNumber) % 8;
TempReg = pGPIOHandle -> GPIO_PinConfig.GPIO_PinAltFuncMode << (4u * temp2);
if (temp1 == 0)
{
pGPIOHandle ->pGPIOx->AFRL |= TempReg;
}
else
{
pGPIOHandle ->pGPIOx->AFRH |= TempReg;
}
}
}
/********************************************************************
* Function Name : GPIO_DeInit
* Brief : Deinitializes the GPIO peripheral back to its Reset values
* Input Parameters : Base address of the GPIO peripheral
* Return Parameters : None
* Note / Remarks : None
********************************************************************/
void GPIO_DeInit (GPIO_RegDef_t *pGPIOx) /* de-init will be done using the RCC peripheral reset register */
{
if (pGPIOx == GPIOA)
{
GPIOA_REG_RESET();
GPIOA_REG_RESET_CLEAR();
}
else if (pGPIOx == GPIOB)
{
GPIOB_REG_RESET();
GPIOB_REG_RESET_CLEAR();
}
else if (pGPIOx == GPIOC)
{
GPIOC_REG_RESET();
GPIOC_REG_RESET_CLEAR();
}
else if (pGPIOx == GPIOD)
{
GPIOD_REG_RESET();
GPIOD_REG_RESET_CLEAR();
}
else if (pGPIOx == GPIOF)
{
GPIOF_REG_RESET();
GPIOF_REG_RESET_CLEAR();
}
}
/* Peripheral clock setup */
/********************************************************************
* Function Name : GPIO_PClockControl
* Brief : Enables or disables the peripheral clock for a GPIO port
* Input Parameters : Base address of the GPIO peripheral; ENABLE / DISABLE
* Return Parameters : None
* Note / Remarks : None
********************************************************************/
void GPIO_PClockControl (GPIO_RegDef_t *pGPIOx, uint8_t EnOrDi)
{
if (EnOrDi == ENABLE)
{
if (pGPIOx == GPIOA) GPIOA_PCLK_EN();
else if (pGPIOx == GPIOB) GPIOB_PCLK_EN();
else if (pGPIOx == GPIOC) GPIOC_PCLK_EN();
else if (pGPIOx == GPIOD) GPIOD_PCLK_EN();
else if (pGPIOx == GPIOF) GPIOF_PCLK_EN();
}
else
{
if (pGPIOx == GPIOA) GPIOA_PCLK_DI();
else if (pGPIOx == GPIOB) GPIOB_PCLK_DI();
else if (pGPIOx == GPIOC) GPIOC_PCLK_DI();
else if (pGPIOx == GPIOD) GPIOD_PCLK_DI();
else if (pGPIOx == GPIOF) GPIOF_PCLK_DI();
}
}
/* GPIO input operations */
/********************************************************************
* Function Name : GPIO_ReadFromInputPin
* Brief : Returns the input data value of the selected pin of GPIO port
* Input Parameters : Base address of the GPIO peripheral; PinNumber of the pin to be read
* Return Parameters : Returns the value on the input pin of the GPIO peripheral
* Note / Remarks : None
********************************************************************/
uint8_t GPIO_ReadFromInputPin (GPIO_RegDef_t *pGPIOx, uint8_t PinNumber)
{
uint8_t value;
value = (uint8_t)((pGPIOx->IDR >> PinNumber) & 0x00000001);
return value;
}
/********************************************************************
* Function Name : GPIO_ReadFromInputPort
* Brief : Returns the input data value of the selected GPIO port
* Input Parameters : Base address of the GPIO peripheral
* Return Parameters : Returns the value on the GPIO port
* Note / Remarks : None
********************************************************************/
uint16_t GPIO_ReadFromInputPort (GPIO_RegDef_t *pGPIOx)
{
uint16_t value;
value = (uint16_t)(pGPIOx->IDR & 0x0000FFFF);
return value;
}
/* GPIO output operations */
/********************************************************************
* Function Name : GPIO_WriteToOutputPin
* Brief : Writes the input value to the output data register for corresponding pin
* Input Parameters : Base address of the GPIO peripheral; pin number of the GPIO port; Value to be written
* Return Parameters : None
* Note / Remarks : None
********************************************************************/
void GPIO_WriteToOutputPin (GPIO_RegDef_t *pGPIOx, uint8_t PinNumber, uint8_t Value)
{
if (Value == GPIO_PIN_SET)
{
pGPIOx ->ODR |= (1u << PinNumber);
}
else
{
pGPIOx ->ODR &= ~(1u << PinNumber);
}
}
/********************************************************************
* Function Name : GPIO_WriteToOutputPort
* Brief : Writes the input value to the output data register for corresponding port
* Input Parameters : Base address of the GPIO peripheral; Value to be written
* Return Parameters : None
* Note / Remarks : None
********************************************************************/
void GPIO_WriteToOutputPort (GPIO_RegDef_t *pGPIOx, uint8_t Value)
{
pGPIOx ->ODR = Value;
}
/********************************************************************
* Function Name : GPIO_ToggleOutputPin
* Brief : Toggles the bit field of the given GPIO port
* Input Parameters : Base address of the peripheral; Pin number of the pin to be toggled
* Return Parameters : None
* Note / Remarks : None
********************************************************************/
void GPIO_ToggleOutputPin (GPIO_RegDef_t *pGPIOx, uint8_t PinNumber)
{
pGPIOx->ODR = pGPIOx->ODR ^ (1u << PinNumber);
}
/* GPIO interrupt config and handling */
/********************************************************************
* Function Name : GPIO_IRQConfig
* Brief :
* Input Parameters :
* Return Parameters :
* Note / Remarks :
********************************************************************/
void GPIO_IRQConfig (uint8_t IRQNumber, uint32_t IRQPriority, uint8_t EnOrDi)
{
if (EnOrDi == ENABLE)
{
// Program ISER register to enable the interrupt
(*NVIC_ISER) |= (1u << IRQNumber);
}
else
{
// Program ICER register to disable the interrupt
(*NVIC_ICER) |= (1u << IRQNumber);
}
// Code to set the interrupt priority - lower the value higher the priority of the interrupt
uint8_t temp = IRQNumber % 4;
if (IRQNumber <= 3)
{
// Left shifting by 6 is required as Cortex M0 reads only bits [7:6] and ignores the bits [5:0]
(*NVIC_IPR0) |= ((IRQPriority) << (8 * temp)) << 6;
}
else if (IRQNumber <= 7)
{
(*NVIC_IPR1) |= ((IRQPriority) << (8 * temp)) << 6;
}
else if (IRQNumber <= 11)
{
(*NVIC_IPR2) |= ((IRQPriority) << (8 * temp)) << 6;
}
else if (IRQNumber <= 15)
{
(*NVIC_IPR3) |= ((IRQPriority) << (8 * temp)) << 6;
}
else if (IRQNumber <= 19)
{
(*NVIC_IPR4) |= ((IRQPriority) << (8 * temp)) << 6;
}
else if (IRQNumber <= 23)
{
(*NVIC_IPR5) |= ((IRQPriority) << (8 * temp)) << 6;
}
else if (IRQNumber <= 27)
{
(*NVIC_IPR6) |= ((IRQPriority) << (8 * temp)) << 6;
}
else if (IRQNumber <= 31)
{
(*NVIC_IPR7) |= ((IRQPriority) << (8 * temp)) << 6;
}
else {} // do nothing
}
/********************************************************************
* Function Name : GPIO_IRQHandling
* Brief :
* Input Parameters :
* Return Parameters :
* Note / Remarks :
********************************************************************/
void GPIO_IRQHandling (uint8_t PinNumber)
{
if (EXTI->EXTI_PR & (1 << PinNumber))
{
// Clear the pending IRQ
EXTI ->EXTI_PR |= (1u << PinNumber);
}
}