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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
////////////////////////////////////////////////////////////////////////////////
/// @file hal_gpio.c
/// @author AE TEAM
/// @brief THIS FILE PROVIDES ALL THE GPIO FIRMWARE FUNCTIONS.
////////////////////////////////////////////////////////////////////////////////
/// @attention
///
/// THE EXISTING FIRMWARE IS ONLY FOR REFERENCE, WHICH IS DESIGNED TO PROVIDE
/// CUSTOMERS WITH CODING INFORMATION ABOUT THEIR PRODUCTS SO THEY CAN SAVE
/// TIME. THEREFORE, MINDMOTION SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT OR
/// CONSEQUENTIAL DAMAGES ABOUT ANY CLAIMS ARISING OUT OF THE CONTENT OF SUCH
/// HARDWARE AND/OR THE USE OF THE CODING INFORMATION CONTAINED HEREIN IN
/// CONNECTION WITH PRODUCTS MADE BY CUSTOMERS.
///
/// <H2><CENTER>© COPYRIGHT MINDMOTION </CENTER></H2>
////////////////////////////////////////////////////////////////////////////////
// Define to prevent recursive inclusion
#define _HAL_GPIO_C_
// Files includes
#include "reg_exti.h"
#include "hal_rcc.h"
#include "hal_gpio.h"
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup MM32_Hardware_Abstract_Layer
/// @{
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup GPIO_HAL
/// @{
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup GPIO_Exported_Functions
/// @{
////////////////////////////////////////////////////////////////////////////////
/// @brief Deinitializes the gpio peripheral registers to their default reset
/// values.
/// @param gpio: select the GPIO peripheral.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void GPIO_DeInit(GPIO_TypeDef* gpio)
{
switch (*(vu32*)&gpio) {
case (u32)GPIOA:
RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOA, DISABLE);
break;
case (u32)GPIOB:
RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOB, DISABLE);
break;
case (u32)GPIOC:
RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOC, DISABLE);
break;
case (u32)GPIOD:
RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOD, DISABLE);
break;
case (u32)GPIOE:
RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOE, DISABLE);
break;
default:
break;
}
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Deinitializes the Alternate Functions (remap, event control
/// and EXTI configuration) registers to their default reset values.
/// @param None.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void GPIO_AFIODeInit()
{
GPIOA->AFRL = 0xFFFFFFFF;
GPIOA->AFRH = 0xF00FFFFF; // PA14:SWCLK, PA13:PSWDIO
GPIOB->AFRL = 0xFFFFFFFF;
GPIOB->AFRH = 0xFFFFFFFF;
GPIOC->AFRL = 0xFFFFFFFF;
GPIOC->AFRH = 0xFFFFFFFF;
GPIOD->AFRL = 0xFFFFFFFF;
GPIOD->AFRH = 0xFFFFFFFF;
GPIOE->AFRL = 0xFFFFFFFF;
GPIOE->AFRH = 0xFFFFFFFF;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Initializes the gpio peripheral according to the specified
/// parameters in the init_struct.
/// @param gpio: select the GPIO peripheral.
/// @param init_struct: pointer to a GPIO_InitTypeDef structure that
/// contains the configuration information for the specified GPIO
/// peripheral.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void GPIO_Init(GPIO_TypeDef* gpio, GPIO_InitTypeDef* init_struct)
{
u8 idx;
u8 i;
u32 tmp;
__IO u32* reg ;
// 1x
u32 dat = init_struct->GPIO_Mode & 0x0F;
if (init_struct->GPIO_Mode & 0x10)
dat |= init_struct->GPIO_Speed;
// 0x
reg = &gpio->CRL;
for (i = 0; i < 8; i++) {
idx = i * 4;
if ((init_struct->GPIO_Pin) & (1 << i)) {
*reg = (*reg & ~(0xF << idx)) | (dat << idx);
}
}
reg = &gpio->CRH;
tmp = init_struct->GPIO_Pin >> 8;
for (i = 0; i < 8; i++) {
idx = i * 4;
if (tmp & (1 << i)) {
*reg = (*reg & ~(0xF << idx)) | (dat << idx);
}
}
// 2x,4x
if (init_struct->GPIO_Mode == GPIO_Mode_IPD)
gpio->BRR |= init_struct->GPIO_Pin;
else if (init_struct->GPIO_Mode == GPIO_Mode_IPU)
gpio->BSRR |= init_struct->GPIO_Pin;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Fills each init_struct member with its default value.
/// @param init_struct : pointer to a GPIO_InitTypeDef structure
/// which will be initialized.
/// @retval : None
////////////////////////////////////////////////////////////////////////////////
void GPIO_StructInit(GPIO_InitTypeDef* init_struct)
{
// Reset GPIO init structure parameters values
init_struct->GPIO_Pin = GPIO_Pin_All;
init_struct->GPIO_Speed = GPIO_Speed_2MHz;
init_struct->GPIO_Mode = GPIO_Mode_FLOATING;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Reads the input data of specified GPIO port pin.
/// @param gpio: select the GPIO peripheral.
/// @param pin: specifies the port pin to be read.
/// This parameter can be GPIO_Pin_x where x can be (0..15).
/// @retval The input port pin value.
////////////////////////////////////////////////////////////////////////////////
bool GPIO_ReadInputDataBit(GPIO_TypeDef* gpio, u16 pin)
{
return ((gpio->IDR & pin)) ? Bit_SET : Bit_RESET;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Reads all GPIO port pins input data.
/// @param gpio: select the GPIO peripheral.
/// @retval GPIO port input data value.
////////////////////////////////////////////////////////////////////////////////
u16 GPIO_ReadInputData(GPIO_TypeDef* gpio)
{
return gpio->IDR;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Reads the output data of specified GPIO port pin.
/// @param gpio: select the GPIO peripheral.
/// @param pin: specifies the port bit to be read.
/// This parameter can be GPIO_Pin_x where x can be (0..15).
/// @retval The output port pin value.
////////////////////////////////////////////////////////////////////////////////
bool GPIO_ReadOutputDataBit(GPIO_TypeDef* gpio, u16 pin)
{
return (gpio->ODR & pin) ? Bit_SET : Bit_RESET;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Reads all GPIO port pins output data.
/// @param gpio: select the GPIO peripheral.
/// @retval GPIO port output data value.
////////////////////////////////////////////////////////////////////////////////
u16 GPIO_ReadOutputData(GPIO_TypeDef* gpio)
{
return gpio->ODR;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Sets the selected GPIO port pin.
/// @param gpio: where x can be (A..D) to select the GPIO peripheral.
/// @param pin: specifies the port pins to be written.
/// This parameter can be any combination of GPIO_Pin_x where x can be
/// (0..15).
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void GPIO_SetBits(GPIO_TypeDef* gpio, u16 pin)
{
gpio->BSRR = pin;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Clears the selected GPIO port bit.
/// @param gpio: where x can be (A..D) to select the GPIO peripheral.
/// @param pin: specifies the port pins to be written.
/// This parameter can be any combination of GPIO_Pin_x where x can be
/// (0..15).
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void GPIO_ResetBits(GPIO_TypeDef* gpio, u16 pin)
{
gpio->BRR = pin;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Sets or clears the selected GPIO port pin.
/// @param gpio: select the GPIO peripheral.
/// @param pin: specifies the port bit to be written.
/// This parameter can be one of GPIO_Pin_x where x can be (0..15).
/// @param value: specifies the value to be written to the selected bit.
/// This parameter can be one of the BitAction enum values:
/// @arg Bit_RESET: to clear the port pin
/// @arg Bit_SET: to set the port pin
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void GPIO_WriteBit(GPIO_TypeDef* gpio, u16 pin, BitAction value)
{
(value) ? (gpio->BSRR = pin) : (gpio->BRR = pin);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Writes data to all GPIO port pins.
/// @param gpio: where x can be (A..D) to select the GPIO peripheral.
/// @param value: specifies the value to be written to the port output data
/// register.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void GPIO_Write(GPIO_TypeDef* gpio, u16 value)
{
gpio->ODR = value;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Locks GPIO Pins configuration.
/// @param gpio: to select the GPIO peripheral.
/// @param pin: specifies the port bit to be written.
/// This parameter can be any combination of GPIO_Pin_x where x can be
/// (0..15).
/// @param state: new lock state of the port pin.
/// This parameter can be: ENABLE or DISABLE.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void GPIO_PinLock(GPIO_TypeDef* gpio, u16 pin, FunctionalState state)
{
(state) ? (gpio->LCKR |= pin) : (gpio->LCKR &= ~pin);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Locks GPIO Pins configuration registers until next system reset.
/// @param gpio: to select the GPIO peripheral.
/// @param pin: specifies the port bit to be written.
/// This parameter can be any combination of GPIO_Pin_x where x can be
/// (0..15).
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void GPIO_PinLockConfig(GPIO_TypeDef* gpio, u16 pin)
{
gpio->LCKR = GPIO_LCKR_LCKK | pin;
gpio->LCKR = pin;
gpio->LCKR = GPIO_LCKR_LCKK | pin;
gpio->LCKR;
gpio->LCKR;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Enables or disables the port pin remapping.
/// @param remap: selects the pin to remap.
/// @param mask: the corresponding remapping mask of the remapping pin.
/// @param state: new state of the port pin remapping.
/// This parameter can be: ENABLE or DISABLE.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// @brief Writes data to the specified GPIO data port.
/// @param gpio: select the GPIO peripheral.
/// @param pin: specifies the pin for the Alternate function.
/// This parameter can be GPIO_PinSourcex where x can be (0..15) for
/// GPIOA, GPIOB, GPIOD and (0..12) for GPIOC .
/// @param alternate_function: selects the pin to used as Alternate function.
/// This parameter can be the GPIO_AF_x where x can be (0..7).
/// @note The pin should be used for Digital IP.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void GPIO_PinAFConfig(GPIO_TypeDef* gpio, u8 pin, u8 alternate_function)
{
u8 shift = (pin & 0x07) * 4;
u32* ptr = (pin < 8) ? (u32*)&gpio->AFRL : (u32*)&gpio->AFRH;
*ptr = (*ptr & ~(0x0F << shift)) | (alternate_function << shift);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Set the remap function and AF function of the GPIO pin.
/// @param gpio:select the GPIO peripheral.
/// @param pin: specifies the pin for the Alternate function.
/// This parameter can be GPIO_Pin_x where x can be (0..15) for
/// GPIOA, GPIOB, GPIOD and (0..12) for GPIOC .
/// @param remap: selects the pin to remap.
/// @param alternate_function: selects the pin to used as Alternate function.
/// This parameter can be the GPIO_AF_x where x can be (0..7).
/// @note The pin should be used for Digital IP.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void exGPIO_PinAFConfig(GPIO_TypeDef* gpio, u16 pin, s32 remap, s8 alternate_function)
{
u8 i;
u8 shift;
u32* ptr;
if (alternate_function >= 0) {
for (i = 0; i < 32; i++) {
if (pin & 0x01) {
pin = i;
break;
}
pin >>= 1;
}
shift = (pin & 0x07) * 4;
ptr = (pin < 8) ? (u32*)&gpio->AFRL : (u32*)&gpio->AFRH;
*ptr = (*ptr & ~(0x0F << shift)) | (alternate_function << shift);
}
}
/// @}
/// @}
/// @}
|