Skip to content

Commit ad0dc95

Browse files
committed
Use LL for pin config
Signed-off-by: Frederic.Pillon <[email protected]>
1 parent 518aaa5 commit ad0dc95

File tree

2 files changed

+177
-1
lines changed

2 files changed

+177
-1
lines changed

Diff for: cores/arduino/stm32/pinconfig.h

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
*******************************************************************************
3+
* Copyright (c) 2018, STMicroelectronics
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are met:
8+
*
9+
* 1. Redistributions of source code must retain the above copyright notice,
10+
* this list of conditions and the following disclaimer.
11+
* 2. Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation
13+
* and/or other materials provided with the distribution.
14+
* 3. Neither the name of STMicroelectronics nor the names of its contributors
15+
* may be used to endorse or promote products derived from this software
16+
* without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
*******************************************************************************
29+
* Based on mbed-os/target/TARGET_STM/TARGET_STMYY/pin_device.h
30+
*/
31+
#ifndef _PINCONFIG_H
32+
#define _PINCONFIG_H
33+
34+
#include "PinAF_STM32F1.h"
35+
#include "stm32yyxx_ll_gpio.h"
36+
37+
static inline void pin_DisconnectDebug(PinName pin)
38+
{
39+
#ifdef STM32F1xx
40+
pinF1_DisconnectDebug(pin);
41+
#endif /* STM32F1xx */
42+
}
43+
44+
static inline void pin_PullConfig(GPIO_TypeDef *gpio, uint32_t ll_pin, uint32_t pull_config)
45+
{
46+
#ifdef STM32F1xx
47+
uint32_t function = LL_GPIO_GetPinMode(gpio, ll_pin);
48+
#endif
49+
50+
switch (pull_config) {
51+
case GPIO_PULLUP:
52+
#ifdef STM32F1xx
53+
if (function == LL_GPIO_MODE_FLOATING) {
54+
LL_GPIO_SetPinMode(gpio, ll_pin, LL_GPIO_MODE_INPUT);
55+
}
56+
#endif
57+
LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_UP);
58+
break;
59+
case GPIO_PULLDOWN:
60+
#ifdef STM32F1xx
61+
if (function == LL_GPIO_MODE_FLOATING) {
62+
LL_GPIO_SetPinMode(gpio, ll_pin, LL_GPIO_MODE_INPUT);
63+
}
64+
#endif
65+
LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_DOWN);
66+
break;
67+
default:
68+
#ifdef STM32F1xx
69+
/* Input+NoPull = Floating for F1 family */
70+
if (function == LL_GPIO_MODE_INPUT) {
71+
LL_GPIO_SetPinMode(gpio, ll_pin, LL_GPIO_MODE_FLOATING);
72+
}
73+
#else
74+
LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_NO);
75+
#endif
76+
break;
77+
}
78+
}
79+
80+
static inline void pin_SetAFPin(GPIO_TypeDef *gpio, PinName pin, uint32_t afnum)
81+
{
82+
#ifdef STM32F1xx
83+
UNUSED(gpio);
84+
UNUSED(pin);
85+
pin_SetF1AFPin(afnum);
86+
#else
87+
uint32_t ll_pin = STM_LL_GPIO_PIN(pin);
88+
89+
if (STM_PIN(pin) > 7) {
90+
LL_GPIO_SetAFPin_8_15(gpio, ll_pin, afnum);
91+
} else {
92+
LL_GPIO_SetAFPin_0_7(gpio, ll_pin, afnum);
93+
}
94+
#endif
95+
}
96+
97+
#endif

Diff for: cores/arduino/stm32/pinmap.c

+80-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
* limitations under the License.
1515
*/
1616
//Based on mbed-os/hal/mbed_pinmap_common.c
17-
1817
#include "pinmap.h"
18+
#include "pinconfig.h"
1919
#include "stm32yyxx_ll_gpio.h"
2020

2121
/* Map STM_PIN to LL */
@@ -51,6 +51,85 @@ bool pin_in_pinmap(PinName pin, const PinMap *map)
5151
return false;
5252
}
5353

54+
/**
55+
* Configure pin (mode, speed, output type and pull-up/pull-down)
56+
*/
57+
void pin_function(PinName pin, int function)
58+
{
59+
/* Get the pin informations */
60+
uint32_t mode = STM_PIN_FUNCTION(function);
61+
uint32_t afnum = STM_PIN_AFNUM(function);
62+
uint32_t port = STM_PORT(pin);
63+
uint32_t ll_pin = STM_LL_GPIO_PIN(pin);
64+
uint32_t ll_mode = 0;
65+
66+
if (pin == (PinName)NC) {
67+
Error_Handler();
68+
}
69+
70+
/* Enable GPIO clock */
71+
GPIO_TypeDef *gpio = set_GPIO_Port_Clock(port);
72+
73+
/* Set default speed to high.
74+
* For most families there are dedicated registers so it is
75+
* not so important, register can be set at any time.
76+
* But for families like F1, speed only applies to output.
77+
*/
78+
#if defined (STM32F1xx)
79+
if (mode == STM_PIN_OUTPUT) {
80+
#endif
81+
#ifdef LL_GPIO_SPEED_FREQ_VERY_HIGH
82+
LL_GPIO_SetPinSpeed(gpio, ll_pin, LL_GPIO_SPEED_FREQ_VERY_HIGH);
83+
#else
84+
LL_GPIO_SetPinSpeed(gpio, ll_pin, LL_GPIO_SPEED_FREQ_HIGH);
85+
#endif
86+
#if defined (STM32F1xx)
87+
}
88+
#endif
89+
90+
switch (mode) {
91+
case STM_PIN_INPUT:
92+
ll_mode = LL_GPIO_MODE_INPUT;
93+
break;
94+
case STM_PIN_OUTPUT:
95+
ll_mode = LL_GPIO_MODE_OUTPUT;
96+
break;
97+
case STM_PIN_ALTERNATE:
98+
ll_mode = LL_GPIO_MODE_ALTERNATE;
99+
/* In case of ALT function, also set the afnum */
100+
pin_SetAFPin(gpio, pin, afnum);
101+
break;
102+
case STM_PIN_ANALOG:
103+
ll_mode = LL_GPIO_MODE_ANALOG;
104+
break;
105+
default:
106+
Error_Handler();
107+
break;
108+
}
109+
LL_GPIO_SetPinMode(gpio, ll_pin, ll_mode);
110+
111+
#if defined(GPIO_ASCR_ASC0)
112+
/* For families where Analog Control ASC0 register is present */
113+
if (STM_PIN_ANALOG_CONTROL(function)) {
114+
LL_GPIO_EnablePinAnalogControl(gpio, ll_pin);
115+
} else {
116+
LL_GPIO_DisablePinAnalogControl(gpio, ll_pin);
117+
}
118+
#endif
119+
120+
if ((mode == STM_PIN_OUTPUT) || (mode == STM_PIN_ALTERNATE)) {
121+
if (STM_PIN_OD(function)) {
122+
LL_GPIO_SetPinOutputType(gpio, ll_pin, LL_GPIO_OUTPUT_OPENDRAIN);
123+
} else {
124+
LL_GPIO_SetPinOutputType(gpio, ll_pin, LL_GPIO_OUTPUT_PUSHPULL);
125+
}
126+
}
127+
128+
pin_PullConfig(gpio, ll_pin, STM_PIN_PUPD(function));
129+
130+
pin_DisconnectDebug(pin);
131+
}
132+
54133
void *pinmap_find_peripheral(PinName pin, const PinMap *map)
55134
{
56135
while (map->pin != NC) {

0 commit comments

Comments
 (0)