-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathpinmap.c
295 lines (264 loc) · 9.34 KB
/
pinmap.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
/* mbed Microcontroller Library
*******************************************************************************
* Copyright (c) 2017, STMicroelectronics
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************
*/
#include "mbed_assert.h"
#include "pinmap.h"
#include "PortNames.h"
#include "mbed_error.h"
#include "pin_device.h"
extern GPIO_TypeDef *Set_GPIO_Clock(uint32_t port_idx);
#if defined(DUAL_PAD) // STM32H7
typedef struct {
PinName pin;
uint32_t LL_AnalogSwitch;
} PinAnalogSwitch;
const PinAnalogSwitch PinMapAnalogSwitch[] = {
{PA_0, LL_SYSCFG_ANALOG_SWITCH_PA0},
{PA_1, LL_SYSCFG_ANALOG_SWITCH_PA1},
{PC_2, LL_SYSCFG_ANALOG_SWITCH_PC2},
{PC_3, LL_SYSCFG_ANALOG_SWITCH_PC3},
{NC, 0}
};
/**
* Configure Analog dualpad switch if necessary
* LL_AnalogSwitch: LL define to be used to configure Analog switch
*/
static void configure_dualpad_switch(PinName pin, int function, uint32_t LL_AnalogSwitch)
{
if (LL_AnalogSwitch == 0) {
return ;
}
if (((function & STM_MODE_ANALOG) != STM_MODE_ANALOG)
&& ((pin & DUAL_PAD) == DUAL_PAD)) {
/**
* We don't configure an analog function but the pin is an analog pad
* Pxy_C. In this cases Analog switch should be closed
*/
LL_SYSCFG_CloseAnalogSwitch(LL_AnalogSwitch);
return ;
} else {
/**
* Either we configure an analog function,
* or it is not an analog function but it is not an analog pad Pxy_C.
* In both cases Analog switch should be opened
* Note: direct ADC is restricted to Pxy_C, pin only
*/
LL_SYSCFG_OpenAnalogSwitch(LL_AnalogSwitch);
return ;
}
}
/**
* In case of dual pad, determine whether gpio needs to be configured
* pLL_AnalogSwitch: pointer used to retrun LL define to be used to configure
* Analog switch
* return: true when gpio must be configured
*/
static bool is_dualpad_switch_gpio_configurable(PinName pin, int function, uint32_t *pLL_AnalogSwitch)
{
PinAnalogSwitch *AnalogSwitch = (PinAnalogSwitch *) PinMapAnalogSwitch;
/* Read through PinMapAnalogSwitch array */
while (AnalogSwitch->pin != NC) {
/* Check whether pin is or is associated to dualpad Analog Input */
if ((AnalogSwitch->pin | DUAL_PAD) == (pin | DUAL_PAD)) {
*pLL_AnalogSwitch = AnalogSwitch->LL_AnalogSwitch;
if (((function & STM_MODE_ANALOG) == STM_MODE_ANALOG)
&& ((pin & DUAL_PAD) == DUAL_PAD)) {
/**
* We configure an analog function and the pin is an analog pad Pxy_C
* In this case gpio configuration must be skipped
*/
return false;
} else {
return true;
}
}
AnalogSwitch ++;
}
*pLL_AnalogSwitch = 0;
return true;
}
#endif /* DUAL_PAD */
const uint32_t ll_pin_defines[16] = {
LL_GPIO_PIN_0,
LL_GPIO_PIN_1,
LL_GPIO_PIN_2,
LL_GPIO_PIN_3,
LL_GPIO_PIN_4,
LL_GPIO_PIN_5,
LL_GPIO_PIN_6,
LL_GPIO_PIN_7,
LL_GPIO_PIN_8,
LL_GPIO_PIN_9,
LL_GPIO_PIN_10,
LL_GPIO_PIN_11,
LL_GPIO_PIN_12,
LL_GPIO_PIN_13,
LL_GPIO_PIN_14,
LL_GPIO_PIN_15
};
/**
* Configure pin (mode, speed, output type and pull-up/pull-down)
*/
void pin_function(PinName pin, int data)
{
if (pin == NC) {
return;
}
// Get the pin informations
uint32_t mode = STM_PIN_FUNCTION(data);
uint32_t afnum = STM_PIN_AFNUM(data);
uint32_t speed = STM_PIN_SPEED(data);
uint32_t port = STM_PORT(pin);
uint32_t ll_pin = ll_pin_defines[STM_PIN(pin)];
uint32_t ll_mode = 0;
#if defined(DUAL_PAD)
uint32_t LL_AnalogSwitch = 0;
if (!is_dualpad_switch_gpio_configurable(pin, data, &LL_AnalogSwitch)) {
/* Skip gpio configuration */
configure_dualpad_switch(pin, data, LL_AnalogSwitch);
return;
}
#endif /* DUAL_PAD */
// Enable GPIO clock
GPIO_TypeDef *const gpio = Set_GPIO_Clock(port);
/* Set default speed to high.
* For most families there are dedicated registers so it is
* not so important, register can be set at any time.
* But for families like F1, speed only applies to output.
*/
#if defined (TARGET_STM32F1)
if (mode == STM_PIN_OUTPUT) {
#endif
#if defined(DUAL_CORE) && (TARGET_STM32H7)
while (LL_HSEM_1StepLock(HSEM, CFG_HW_GPIO_SEMID)) {
}
#endif /* DUAL_CORE */
switch (speed) {
/* Default value for backward compatibility */
case STM_PIN_SPEED_MASK:
#if defined (LL_GPIO_SPEED_FREQ_VERY_HIGH)
LL_GPIO_SetPinSpeed(gpio, ll_pin, LL_GPIO_SPEED_FREQ_VERY_HIGH);
#else
LL_GPIO_SetPinSpeed(gpio, ll_pin, LL_GPIO_SPEED_FREQ_HIGH);
#endif
break;
default:
LL_GPIO_SetPinSpeed(gpio, ll_pin, speed);
break;
}
#if defined(DUAL_CORE) && (TARGET_STM32H7)
LL_HSEM_ReleaseLock(HSEM, CFG_HW_GPIO_SEMID, HSEM_CR_COREID_CURRENT);
#endif /* DUAL_CORE */
#if defined (TARGET_STM32F1)
}
#endif
switch (mode) {
case STM_PIN_INPUT:
ll_mode = LL_GPIO_MODE_INPUT;
break;
case STM_PIN_OUTPUT:
ll_mode = LL_GPIO_MODE_OUTPUT;
break;
case STM_PIN_ALTERNATE:
ll_mode = LL_GPIO_MODE_ALTERNATE;
// In case of ALT function, also set he afnum
stm_pin_SetAFPin(gpio, pin, afnum);
break;
case STM_PIN_ANALOG:
ll_mode = LL_GPIO_MODE_ANALOG;
break;
default:
MBED_ASSERT(0);
break;
}
#if defined(DUAL_CORE) && (TARGET_STM32H7)
while (LL_HSEM_1StepLock(HSEM, CFG_HW_GPIO_SEMID)) {
}
#endif /* DUAL_CORE */
LL_GPIO_SetPinMode(gpio, ll_pin, ll_mode);
#if defined(GPIO_ASCR_ASC0)
/* For families where Analog Control ASC0 register is present */
if (STM_PIN_ANALOG_CONTROL(data)) {
LL_GPIO_EnablePinAnalogControl(gpio, ll_pin);
} else {
LL_GPIO_DisablePinAnalogControl(gpio, ll_pin);
}
#endif
/* For now by default use Speed HIGH for output or alt modes */
if ((mode == STM_PIN_OUTPUT) || (mode == STM_PIN_ALTERNATE)) {
if (STM_PIN_OD(data)) {
LL_GPIO_SetPinOutputType(gpio, ll_pin, LL_GPIO_OUTPUT_OPENDRAIN);
} else {
LL_GPIO_SetPinOutputType(gpio, ll_pin, LL_GPIO_OUTPUT_PUSHPULL);
}
}
stm_pin_PullConfig(gpio, ll_pin, STM_PIN_PUPD(data));
#if defined(DUAL_PAD)
configure_dualpad_switch(pin, data, LL_AnalogSwitch);
#endif /* DUAL_PAD */
stm_pin_DisconnectDebug(pin);
#if defined(DUAL_CORE) && (TARGET_STM32H7)
LL_HSEM_ReleaseLock(HSEM, CFG_HW_GPIO_SEMID, HSEM_CR_COREID_CURRENT);
#endif /* DUAL_CORE */
}
/**
* Configure pin pull-up/pull-down
*/
void pin_mode(PinName pin, PinMode mode)
{
if (pin == NC) {
return;
}
uint32_t port_index = STM_PORT(pin);
uint32_t ll_pin = ll_pin_defines[STM_PIN(pin)];
// Enable GPIO clock
GPIO_TypeDef *gpio = Set_GPIO_Clock(port_index);
#if defined(DUAL_CORE) && (TARGET_STM32H7)
while (LL_HSEM_1StepLock(HSEM, CFG_HW_GPIO_SEMID)) {
}
#endif /* DUAL_CORE */
uint32_t function = LL_GPIO_GetPinMode(gpio, ll_pin);
if ((function == LL_GPIO_MODE_OUTPUT) || (function == LL_GPIO_MODE_ALTERNATE)) {
if ((mode == OpenDrainNoPull) || (mode == OpenDrainPullUp) || (mode == OpenDrainPullDown)) {
LL_GPIO_SetPinOutputType(gpio, ll_pin, LL_GPIO_OUTPUT_OPENDRAIN);
} else {
LL_GPIO_SetPinOutputType(gpio, ll_pin, LL_GPIO_OUTPUT_PUSHPULL);
}
}
if ((mode == OpenDrainPullUp) || (mode == PullUp)) {
stm_pin_PullConfig(gpio, ll_pin, GPIO_PULLUP);
} else if ((mode == OpenDrainPullDown) || (mode == PullDown)) {
stm_pin_PullConfig(gpio, ll_pin, GPIO_PULLDOWN);
} else {
stm_pin_PullConfig(gpio, ll_pin, GPIO_NOPULL);
}
#if defined(DUAL_CORE) && (TARGET_STM32H7)
LL_HSEM_ReleaseLock(HSEM, CFG_HW_GPIO_SEMID, HSEM_CR_COREID_CURRENT);
#endif /* DUAL_CORE */
}