Skip to content

Commit 7a89342

Browse files
committed
Replace HAL_GPIO_Init by pinmap_pinout
Signed-off-by: Frederic.Pillon <[email protected]>
1 parent a3114aa commit 7a89342

File tree

4 files changed

+25
-172
lines changed

4 files changed

+25
-172
lines changed

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/spi_com.c

+13-65
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "stm32_def.h"
5151
#include "spi_com.h"
5252
#include "PinAF_STM32F1.h"
53+
#include "pinconfig.h"
5354

5455
#ifdef __cplusplus
5556
extern "C" {
@@ -192,9 +193,8 @@ void spi_init(spi_t *obj, uint32_t speed, spi_mode_e mode, uint8_t msb)
192193
return;
193194

194195
SPI_HandleTypeDef *handle = &(obj->handle);
195-
GPIO_InitTypeDef GPIO_InitStruct;
196-
GPIO_TypeDef *port;
197196
uint32_t spi_freq = 0;
197+
uint32_t pull = 0;
198198

199199
// Determine the SPI to use
200200
SPI_TypeDef *spi_mosi = pinmap_peripheral(obj->pin_mosi, PinMap_SPI_MOSI);
@@ -280,69 +280,17 @@ void spi_init(spi_t *obj, uint32_t speed, spi_mode_e mode, uint8_t msb)
280280
handle->Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
281281
#endif
282282

283-
if(obj->pin_mosi != NC) {
284-
port = set_GPIO_Port_Clock(STM_PORT(obj->pin_mosi));
285-
GPIO_InitStruct.Pin = STM_GPIO_PIN(obj->pin_mosi);
286-
GPIO_InitStruct.Mode = STM_PIN_MODE(pinmap_function(obj->pin_mosi,PinMap_SPI_MOSI));
287-
GPIO_InitStruct.Pull = STM_PIN_PUPD(pinmap_function(obj->pin_mosi,PinMap_SPI_MOSI));
288-
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
289-
#ifdef STM32F1xx
290-
pin_SetF1AFPin(STM_PIN_AFNUM(pinmap_function(obj->pin_mosi,PinMap_SPI_MOSI)));
291-
#else
292-
GPIO_InitStruct.Alternate = STM_PIN_AFNUM(pinmap_function(obj->pin_mosi,PinMap_SPI_MOSI));
293-
#endif /* STM32F1xx */
294-
HAL_GPIO_Init(port, &GPIO_InitStruct);
295-
}
296-
297-
if(obj->pin_miso != NC) {
298-
port = set_GPIO_Port_Clock(STM_PORT(obj->pin_miso));
299-
GPIO_InitStruct.Pin = STM_GPIO_PIN(obj->pin_miso);
300-
GPIO_InitStruct.Mode = STM_PIN_MODE(pinmap_function(obj->pin_miso,PinMap_SPI_MISO));
301-
GPIO_InitStruct.Pull = STM_PIN_PUPD(pinmap_function(obj->pin_miso,PinMap_SPI_MISO));
302-
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
303-
#ifdef STM32F1xx
304-
pin_SetF1AFPin(STM_PIN_AFNUM(pinmap_function(obj->pin_miso,PinMap_SPI_MISO)));
305-
#else
306-
GPIO_InitStruct.Alternate = STM_PIN_AFNUM(pinmap_function(obj->pin_miso,PinMap_SPI_MISO));
307-
#endif /* STM32F1xx */
308-
HAL_GPIO_Init(port, &GPIO_InitStruct);
309-
}
310-
311-
if(obj->pin_sclk != NC) {
312-
port = set_GPIO_Port_Clock(STM_PORT(obj->pin_sclk));
313-
GPIO_InitStruct.Pin = STM_GPIO_PIN(obj->pin_sclk);
314-
GPIO_InitStruct.Mode = STM_PIN_MODE(pinmap_function(obj->pin_sclk,PinMap_SPI_SCLK));
315-
/*
316-
* According the STM32 Datasheet for SPI peripheral we need to PULLDOWN
317-
* or PULLUP the SCK pin according the polarity used.
318-
*/
319-
if(handle->Init.CLKPolarity == SPI_POLARITY_LOW) {
320-
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
321-
} else {
322-
GPIO_InitStruct.Pull = GPIO_PULLUP;
323-
}
324-
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
325-
#ifdef STM32F1xx
326-
pin_SetF1AFPin(STM_PIN_AFNUM(pinmap_function(obj->pin_sclk,PinMap_SPI_SCLK)));
327-
#else
328-
GPIO_InitStruct.Alternate = STM_PIN_AFNUM(pinmap_function(obj->pin_sclk,PinMap_SPI_SCLK));
329-
#endif /* STM32F1xx */
330-
HAL_GPIO_Init(port, &GPIO_InitStruct);
331-
}
332-
333-
if(obj->pin_ssel != NC) {
334-
port = set_GPIO_Port_Clock(STM_PORT(obj->pin_ssel));
335-
GPIO_InitStruct.Pin = STM_GPIO_PIN(obj->pin_ssel);
336-
GPIO_InitStruct.Mode = STM_PIN_MODE(pinmap_function(obj->pin_ssel,PinMap_SPI_SSEL));
337-
GPIO_InitStruct.Pull = STM_PIN_PUPD(pinmap_function(obj->pin_ssel,PinMap_SPI_SSEL));
338-
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
339-
#ifdef STM32F1xx
340-
pin_SetF1AFPin(STM_PIN_AFNUM(pinmap_function(obj->pin_ssel,PinMap_SPI_SSEL)));
341-
#else
342-
GPIO_InitStruct.Alternate = STM_PIN_AFNUM(pinmap_function(obj->pin_ssel,PinMap_SPI_SSEL));
343-
#endif /* STM32F1xx */
344-
HAL_GPIO_Init(port, &GPIO_InitStruct);
345-
}
283+
/* Configure SPI GPIO pins */
284+
pinmap_pinout(obj->pin_mosi, PinMap_SPI_MOSI);
285+
pinmap_pinout(obj->pin_miso, PinMap_SPI_MISO);
286+
pinmap_pinout(obj->pin_sclk, PinMap_SPI_SCLK);
287+
/*
288+
* According the STM32 Datasheet for SPI peripheral we need to PULLDOWN
289+
* or PULLUP the SCK pin according the polarity used.
290+
*/
291+
pull = (handle->Init.CLKPolarity == SPI_POLARITY_LOW) ? GPIO_PULLDOWN: GPIO_PULLUP;
292+
pin_PullConfig(get_GPIO_Port(STM_PORT(obj->pin_sclk)), STM_LL_GPIO_PIN(obj->pin_sclk), pull);
293+
pinmap_pinout(obj->pin_ssel, PinMap_SPI_SSEL);
346294

347295
#if defined SPI1_BASE
348296
// Enable SPI clock

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

+3-27
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,6 @@ void i2c_custom_init(i2c_t *obj, i2c_timing_e timing, uint32_t addressingMode, u
157157
if(obj == NULL)
158158
return;
159159

160-
GPIO_InitTypeDef GPIO_InitStruct;
161-
GPIO_TypeDef *port;
162160
I2C_HandleTypeDef *handle = &(obj->handle);
163161

164162
// Determine the I2C to use
@@ -232,31 +230,9 @@ void i2c_custom_init(i2c_t *obj, i2c_timing_e timing, uint32_t addressingMode, u
232230
}
233231
#endif // I2C4_BASE
234232

235-
//SCL
236-
port = set_GPIO_Port_Clock(STM_PORT(obj->scl));
237-
GPIO_InitStruct.Pin = STM_GPIO_PIN(obj->scl);
238-
GPIO_InitStruct.Mode = STM_PIN_MODE(pinmap_function(obj->scl,PinMap_I2C_SCL));
239-
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
240-
GPIO_InitStruct.Pull = STM_PIN_PUPD(pinmap_function(obj->scl,PinMap_I2C_SCL));
241-
#ifdef STM32F1xx
242-
pin_SetF1AFPin(STM_PIN_AFNUM(pinmap_function(obj->scl,PinMap_I2C_SCL)));
243-
#else
244-
GPIO_InitStruct.Alternate = STM_PIN_AFNUM(pinmap_function(obj->scl,PinMap_I2C_SCL));
245-
#endif /* STM32F1xx */
246-
HAL_GPIO_Init(port, &GPIO_InitStruct);
247-
248-
//SDA
249-
port = set_GPIO_Port_Clock(STM_PORT(obj->sda));
250-
GPIO_InitStruct.Pin = STM_GPIO_PIN(obj->sda);
251-
GPIO_InitStruct.Mode = STM_PIN_MODE(pinmap_function(obj->sda,PinMap_I2C_SDA));
252-
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
253-
GPIO_InitStruct.Pull = STM_PIN_PUPD(pinmap_function(obj->sda,PinMap_I2C_SDA));
254-
#ifdef STM32F1xx
255-
pin_SetF1AFPin(STM_PIN_AFNUM(pinmap_function(obj->sda,PinMap_I2C_SDA)));
256-
#else
257-
GPIO_InitStruct.Alternate = STM_PIN_AFNUM(pinmap_function(obj->sda,PinMap_I2C_SDA));
258-
#endif /* STM32F1xx */
259-
HAL_GPIO_Init(port, &GPIO_InitStruct);
233+
/* Configure I2C GPIO pins */
234+
pinmap_pinout(obj->scl, PinMap_I2C_SCL);
235+
pinmap_pinout(obj->sda, PinMap_I2C_SDA);
260236

261237
handle->Instance = obj->i2c;
262238
#if defined (STM32F0xx) || defined (STM32F3xx) || defined (STM32F7xx) ||\

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

+3-34
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,6 @@ void uart_init(serial_t *obj)
9292
}
9393

9494
UART_HandleTypeDef *huart = &(obj->handle);
95-
GPIO_InitTypeDef GPIO_InitStruct;
96-
GPIO_TypeDef *port;
97-
uint32_t function = (uint32_t)NC;
9895

9996
/* Determine the U(S)ART peripheral to use (USART1, USART2, ...) */
10097
USART_TypeDef *uart_tx = pinmap_peripheral(obj->pin_tx, PinMap_UART_TX);
@@ -260,37 +257,9 @@ void uart_init(serial_t *obj)
260257
__HAL_RCC_SYSCFG_CLK_ENABLE();
261258
#endif
262259

263-
/* Configure GPIOs */
264-
/* RX */
265-
port = set_GPIO_Port_Clock(STM_PORT(obj->pin_rx));
266-
function = pinmap_function(obj->pin_rx, PinMap_UART_RX);
267-
GPIO_InitStruct.Pin = STM_GPIO_PIN(obj->pin_rx);
268-
GPIO_InitStruct.Mode = STM_PIN_MODE(function);
269-
GPIO_InitStruct.Pull = STM_PIN_PUPD(function);
270-
/* Common */
271-
#ifdef STM32F1xx
272-
pin_SetF1AFPin(STM_PIN_AFNUM(function));
273-
#else
274-
GPIO_InitStruct.Alternate = STM_PIN_AFNUM(function);
275-
#endif /* STM32F1xx */
276-
#ifdef GPIO_SPEED_FREQ_VERY_HIGH
277-
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
278-
#else
279-
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
280-
#endif
281-
HAL_GPIO_Init(port, &GPIO_InitStruct);
282-
283-
/* TX */
284-
port = set_GPIO_Port_Clock(STM_PORT(obj->pin_tx));
285-
function = pinmap_function(obj->pin_tx, PinMap_UART_TX);
286-
GPIO_InitStruct.Pin = STM_GPIO_PIN(obj->pin_tx);
287-
GPIO_InitStruct.Mode = STM_PIN_MODE(function);
288-
GPIO_InitStruct.Pull = STM_PIN_PUPD(function);
289-
#ifndef STM32F1xx
290-
GPIO_InitStruct.Alternate = STM_PIN_AFNUM(function);
291-
#endif /* STM32F1xx */
292-
HAL_GPIO_Init(port, &GPIO_InitStruct);
293-
260+
/* Configure UART GPIO pins */
261+
pinmap_pinout(obj->pin_tx, PinMap_UART_TX);
262+
pinmap_pinout(obj->pin_rx, PinMap_UART_RX);
294263

295264
/* Configure uart */
296265
uart_handlers[obj->index] = huart;

0 commit comments

Comments
 (0)