From d7eb070eb4f74634d14c3e5e48b9149573c824af Mon Sep 17 00:00:00 2001 From: MrKevinWeiss Date: Fri, 7 Dec 2018 16:30:54 +0100 Subject: [PATCH] cpu/stm32f1: Add remap support for stm32f1 cpus and boards This commit fixes configuration problems when trying to use i2c pins that need to be remapped. All B8 and B9 pins for STM32F1 need to be remapped, so a check is done if the remappable pins are selected. --- boards/common/stm32f103c8/include/periph_conf.h | 3 ++- boards/nucleo-f103rb/include/periph_conf.h | 3 ++- cpu/stm32_common/periph/i2c_2.c | 8 ++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/boards/common/stm32f103c8/include/periph_conf.h b/boards/common/stm32f103c8/include/periph_conf.h index 2f56a63677..297948a65b 100644 --- a/boards/common/stm32f103c8/include/periph_conf.h +++ b/boards/common/stm32f103c8/include/periph_conf.h @@ -155,7 +155,8 @@ static const uart_conf_t uart_config[] = { /** @} */ /** - * @name I2C configuration + * @name I2C configuration + * @note This board may require external pullup resistors for i2c operation. * @{ */ static const i2c_conf_t i2c_config[] = { diff --git a/boards/nucleo-f103rb/include/periph_conf.h b/boards/nucleo-f103rb/include/periph_conf.h index 3863bfbed5..d48875edca 100644 --- a/boards/nucleo-f103rb/include/periph_conf.h +++ b/boards/nucleo-f103rb/include/periph_conf.h @@ -144,7 +144,8 @@ static const uart_conf_t uart_config[] = { /** @} */ /** - * @name I2C configuration + * @name I2C configuration + * @note This board may require external pullup resistors for i2c operation. * @{ */ static const i2c_conf_t i2c_config[] = { diff --git a/cpu/stm32_common/periph/i2c_2.c b/cpu/stm32_common/periph/i2c_2.c index f89ea229db..39ab925982 100644 --- a/cpu/stm32_common/periph/i2c_2.c +++ b/cpu/stm32_common/periph/i2c_2.c @@ -109,6 +109,14 @@ void i2c_init(i2c_t dev) gpio_init(i2c_config[dev].scl_pin, GPIO_OD_PU); gpio_init(i2c_config[dev].sda_pin, GPIO_OD_PU); #ifdef CPU_FAM_STM32F1 + /* This is needed in case the remapped pins are used */ + if (i2c_config[dev].scl_pin == GPIO_PIN(PORT_B, 8) || + i2c_config[dev].sda_pin == GPIO_PIN(PORT_B, 9)) { + /* The remapping periph clock must first be enabled */ + RCC->APB2ENR |= RCC_APB2ENR_AFIOEN; + /* Then the remap can occur */ + AFIO->MAPR |= AFIO_MAPR_I2C1_REMAP; + } gpio_init_af(i2c_config[dev].scl_pin, GPIO_AF_OUT_OD); gpio_init_af(i2c_config[dev].sda_pin, GPIO_AF_OUT_OD); #else