Skip to content

Commit 0b1f59a

Browse files
authored
Merge pull request #369 from fpistm/pinmode
LL used for GPIO pin configuration
2 parents ddfcab8 + 49d1df8 commit 0b1f59a

16 files changed

+385
-415
lines changed

Diff for: cores/arduino/pins_arduino.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ extern "C" {
2424

2525
WEAK uint32_t pinNametoDigitalPin(PinName p)
2626
{
27-
uint32_t i = NC;
27+
uint32_t i = NUM_DIGITAL_PINS;
2828
if(STM_VALID_PINNAME(p)) {
2929
for(i = 0; i < NUM_DIGITAL_PINS; i++) {
3030
if (digitalPin[i] == p)

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

+1-20
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ extern "C" {
3636
/* STM PIN data as used in pin_function is coded on 32 bits as below
3737
* [2:0] Function (like in MODER reg) : Input / Output / Alt / Analog
3838
* [3] Output Push-Pull / Open Drain (as in OTYPER reg)
39-
* [5:4] as in PUPDR reg: No Pull, Pull-up, Pull-Donc
39+
* [5:4] as in PUPDR reg: No Pull, Pull-up, Pull-Down
4040
* [7:6] Reserved for speed config (as in OSPEEDR), but not used yet
4141
* [14:8] Alternate Num (as in AFRL/AFRG reg)
4242
* [19:15] Channel (Analog/Timer specific)
@@ -135,25 +135,6 @@ typedef enum {
135135
#define STM_VALID_PINNAME(X) (STM_PORT(X) <= LastPort)
136136

137137
#define STM_GPIO_PIN(X) ((uint16_t)(1<<STM_PIN(X)))
138-
/* Defines to be used by application */
139-
typedef enum {
140-
PIN_INPUT = 0,
141-
PIN_OUTPUT
142-
} PinDirection;
143-
144-
typedef enum {
145-
PullNone = 0,
146-
PullUp = 1,
147-
PullDown = 2,
148-
OpenDrainPullUp = 3,
149-
OpenDrainNoPull = 4,
150-
OpenDrainPullDown = 5,
151-
PushPullNoPull = PullNone,
152-
PushPullPullUp = PullUp,
153-
PushPullPullDown = PullDown,
154-
OpenDrain = OpenDrainPullUp,
155-
PullDefault = PullNone
156-
} PinMode;
157138

158139
#ifdef __cplusplus
159140
}

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

+6-46
Original file line numberDiff line numberDiff line change
@@ -257,27 +257,17 @@ static uint32_t get_dac_channel(PinName pin)
257257
*/
258258
void HAL_DAC_MspInit(DAC_HandleTypeDef *hdac)
259259
{
260-
GPIO_InitTypeDef GPIO_InitStruct;
261-
GPIO_TypeDef *port;
262260
UNUSED(hdac);
263261

264-
/*##-1- Enable peripherals and GPIO Clocks #################################*/
265-
/* Enable GPIO clock ****************************************/
266-
port = set_GPIO_Port_Clock(STM_PORT(g_current_pin));
267-
268262
/* DAC Periph clock enable */
269263
#ifdef __HAL_RCC_DAC1_CLK_ENABLE
270264
__HAL_RCC_DAC1_CLK_ENABLE();
271265
#endif
272266
#ifdef __HAL_RCC_DAC_CLK_ENABLE
273267
__HAL_RCC_DAC_CLK_ENABLE();
274268
#endif
275-
/*##-2- Configure peripheral GPIO ##########################################*/
276-
/* DAC Channel1 GPIO pin configuration */
277-
GPIO_InitStruct.Pin = STM_GPIO_PIN(g_current_pin);
278-
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
279-
GPIO_InitStruct.Pull = GPIO_NOPULL;
280-
HAL_GPIO_Init(port, &GPIO_InitStruct);
269+
/* Configure DAC GPIO pins */
270+
pinmap_pinout(g_current_pin, PinMap_DAC);
281271
}
282272

283273

@@ -393,8 +383,6 @@ void dac_stop(PinName pin)
393383
*/
394384
void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc)
395385
{
396-
GPIO_InitTypeDef GPIO_InitStruct;
397-
GPIO_TypeDef *port;
398386
/*##-1- Enable peripherals and GPIO Clocks #################################*/
399387
/* ADC Periph clock enable */
400388
if(hadc->Instance == ADC1) {
@@ -440,19 +428,8 @@ void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc)
440428
__HAL_RCC_ADC_CONFIG(RCC_ADCCLKSOURCE_SYSCLK);
441429
#endif
442430

443-
/* Enable GPIO clock ****************************************/
444-
port = set_GPIO_Port_Clock(STM_PORT(g_current_pin));
445-
446-
/*##-2- Configure peripheral GPIO ##########################################*/
447-
/* ADC Channel GPIO pin configuration */
448-
GPIO_InitStruct.Pin = STM_GPIO_PIN(g_current_pin);
449-
#ifdef GPIO_MODE_ANALOG_ADC_CONTROL
450-
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG_ADC_CONTROL;
451-
#else
452-
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
453-
#endif
454-
GPIO_InitStruct.Pull = GPIO_NOPULL;
455-
HAL_GPIO_Init(port, &GPIO_InitStruct);
431+
/* Configure ADC GPIO pin */
432+
pinmap_pinout(g_current_pin, PinMap_ADC);
456433
}
457434

458435
/**
@@ -682,29 +659,12 @@ uint16_t adc_read_value(PinName pin)
682659
*/
683660
void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim)
684661
{
685-
GPIO_InitTypeDef GPIO_InitStruct;
686-
GPIO_TypeDef *port;
687-
uint32_t function = pinmap_function(g_current_pin, PinMap_PWM);
688662
/*##-1- Enable peripherals and GPIO Clocks #################################*/
689663
/* TIMx Peripheral clock enable */
690664
timer_enable_clock(htim);
691665

692-
/* Enable GPIO Channels Clock */
693-
/* Enable GPIO clock ****************************************/
694-
port = set_GPIO_Port_Clock(STM_PORT(g_current_pin));
695-
696-
/* Common configuration for all channels */
697-
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
698-
GPIO_InitStruct.Pull = GPIO_NOPULL;
699-
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
700-
#ifdef STM32F1xx
701-
pin_SetF1AFPin(STM_PIN_AFNUM(function));
702-
#else
703-
GPIO_InitStruct.Alternate = STM_PIN_AFNUM(function);
704-
#endif /* STM32F1xx */
705-
GPIO_InitStruct.Pin = STM_GPIO_PIN(g_current_pin);
706-
707-
HAL_GPIO_Init(port, &GPIO_InitStruct);
666+
/* Configure PWM GPIO pins */
667+
pinmap_pinout(g_current_pin, PinMap_PWM);
708668
}
709669

710670
/**

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

-72
This file was deleted.

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

-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ extern "C" {
4747
#endif
4848

4949
/* Exported functions ------------------------------------------------------- */
50-
void digital_io_init(PinName pin, uint32_t mode, uint32_t pull);
51-
5250
/**
5351
* @brief This function set a value to an IO
5452
* @param port : one of the gpio port

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

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
#else
42+
UNUSED(pin);
43+
#endif /* STM32F1xx */
44+
}
45+
46+
static inline void pin_PullConfig(GPIO_TypeDef *gpio, uint32_t ll_pin, uint32_t pull_config)
47+
{
48+
#ifdef STM32F1xx
49+
uint32_t function = LL_GPIO_GetPinMode(gpio, ll_pin);
50+
#endif
51+
52+
switch (pull_config) {
53+
case GPIO_PULLUP:
54+
#ifdef STM32F1xx
55+
if (function == LL_GPIO_MODE_FLOATING) {
56+
LL_GPIO_SetPinMode(gpio, ll_pin, LL_GPIO_MODE_INPUT);
57+
}
58+
#endif
59+
LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_UP);
60+
break;
61+
case GPIO_PULLDOWN:
62+
#ifdef STM32F1xx
63+
if (function == LL_GPIO_MODE_FLOATING) {
64+
LL_GPIO_SetPinMode(gpio, ll_pin, LL_GPIO_MODE_INPUT);
65+
}
66+
#endif
67+
LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_DOWN);
68+
break;
69+
default:
70+
#ifdef STM32F1xx
71+
/* Input+NoPull = Floating for F1 family */
72+
if (function == LL_GPIO_MODE_INPUT) {
73+
LL_GPIO_SetPinMode(gpio, ll_pin, LL_GPIO_MODE_FLOATING);
74+
}
75+
#else
76+
LL_GPIO_SetPinPull(gpio, ll_pin, LL_GPIO_PULL_NO);
77+
#endif
78+
break;
79+
}
80+
}
81+
82+
static inline void pin_SetAFPin(GPIO_TypeDef *gpio, PinName pin, uint32_t afnum)
83+
{
84+
#ifdef STM32F1xx
85+
UNUSED(gpio);
86+
UNUSED(pin);
87+
pin_SetF1AFPin(afnum);
88+
#else
89+
uint32_t ll_pin = STM_LL_GPIO_PIN(pin);
90+
91+
if (STM_PIN(pin) > 7) {
92+
LL_GPIO_SetAFPin_8_15(gpio, ll_pin, afnum);
93+
} else {
94+
LL_GPIO_SetAFPin_0_7(gpio, ll_pin, afnum);
95+
}
96+
#endif
97+
}
98+
99+
#endif

0 commit comments

Comments
 (0)