From 8b8bc22b5d9f54d0a4357245958098d1d92135a1 Mon Sep 17 00:00:00 2001 From: Lei Yu Date: Mon, 2 Dec 2019 11:18:10 +0800 Subject: [PATCH 1/3] correct i2c driver for stm32f4 --- drivers/arm/i2c_master.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/drivers/arm/i2c_master.h b/drivers/arm/i2c_master.h index efe3909a6610..228e504921f8 100644 --- a/drivers/arm/i2c_master.h +++ b/drivers/arm/i2c_master.h @@ -27,7 +27,7 @@ #include "ch.h" #include -#if defined(STM32F1XX) || defined(STM32F1xx) || defined(STM32F2xx) || defined(STM32F4xx) || defined(STM32L0xx) || defined(STM32L1xx) +#if defined(STM32F1XX) || defined(STM32F1xx) || defined(STM32F2xx) || defined(STM32F4xx) || defined(STM32F4XX) || defined(STM32L0xx) || defined(STM32L1xx) # define USE_I2CV1 #endif @@ -61,6 +61,10 @@ # ifndef I2C1_DUTY_CYCLE # define I2C1_DUTY_CYCLE STD_DUTY_CYCLE /* FAST_DUTY_CYCLE_2 */ # endif +# ifndef PAL_MODE_STM32_ALTERNATE_OPENDRAIN /* stm32f4 uses GPIOv2 */ +# define I2C_PAL_MODE 4 +# define PAL_MODE_STM32_ALTERNATE_OPENDRAIN PAL_MODE_ALTERNATE(I2C_PAL_MODE) | PAL_STM32_OTYPE_OPENDRAIN +# endif #else // The default PAL alternate modes are used to signal that the pins are used for I2C # ifndef I2C1_SCL_PAL_MODE From d24520c76f7900c9649c48e60a9c8cc6f91f5bbe Mon Sep 17 00:00:00 2001 From: Lei Yu Date: Mon, 2 Dec 2019 12:35:21 +0800 Subject: [PATCH 2/3] update pin mode definitions --- drivers/arm/i2c_master.c | 2 +- drivers/arm/i2c_master.h | 28 ++++++++++++++++------------ 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/drivers/arm/i2c_master.c b/drivers/arm/i2c_master.c index 2a43ba23931c..f22b4d551cad 100644 --- a/drivers/arm/i2c_master.c +++ b/drivers/arm/i2c_master.c @@ -62,7 +62,7 @@ __attribute__((weak)) void i2c_init(void) { palSetPadMode(I2C1_SDA_BANK, I2C1_SDA, PAL_MODE_INPUT); chThdSleepMilliseconds(10); -#ifdef USE_I2CV1 +#if defined(USE_I2CV1) && !defined(USE_GPIOV2) palSetPadMode(I2C1_SCL_BANK, I2C1_SCL, PAL_MODE_STM32_ALTERNATE_OPENDRAIN); palSetPadMode(I2C1_SDA_BANK, I2C1_SDA, PAL_MODE_STM32_ALTERNATE_OPENDRAIN); #else diff --git a/drivers/arm/i2c_master.h b/drivers/arm/i2c_master.h index 228e504921f8..23bb9eac066b 100644 --- a/drivers/arm/i2c_master.h +++ b/drivers/arm/i2c_master.h @@ -51,6 +51,22 @@ # define I2C1_SDA 7 #endif +#if defined(STM32F4XX) || defined (STM32F3XX) +# ifndef USE_GPIOV2 +# define USE_GPIOV2 +# endif +#endif + +#ifdef USE_GPIOV2 +// The default PAL alternate modes are used to signal that the pins are used for I2C +# ifndef I2C1_SCL_PAL_MODE +# define I2C1_SCL_PAL_MODE 4 +# endif +# ifndef I2C1_SDA_PAL_MODE +# define I2C1_SDA_PAL_MODE 4 +# endif +#endif + #ifdef USE_I2CV1 # ifndef I2C1_OPMODE # define I2C1_OPMODE OPMODE_I2C @@ -61,19 +77,7 @@ # ifndef I2C1_DUTY_CYCLE # define I2C1_DUTY_CYCLE STD_DUTY_CYCLE /* FAST_DUTY_CYCLE_2 */ # endif -# ifndef PAL_MODE_STM32_ALTERNATE_OPENDRAIN /* stm32f4 uses GPIOv2 */ -# define I2C_PAL_MODE 4 -# define PAL_MODE_STM32_ALTERNATE_OPENDRAIN PAL_MODE_ALTERNATE(I2C_PAL_MODE) | PAL_STM32_OTYPE_OPENDRAIN -# endif #else -// The default PAL alternate modes are used to signal that the pins are used for I2C -# ifndef I2C1_SCL_PAL_MODE -# define I2C1_SCL_PAL_MODE 4 -# endif -# ifndef I2C1_SDA_PAL_MODE -# define I2C1_SDA_PAL_MODE 4 -# endif - // The default timing values below configures the I2C clock to 400khz assuming a 72Mhz clock // For more info : https://www.st.com/en/embedded-software/stsw-stm32126.html # ifndef I2C1_TIMINGR_PRESC From 47ede3b2c5cfef1a5d787199df5d189becc1a230 Mon Sep 17 00:00:00 2001 From: Lei Yu Date: Wed, 4 Dec 2019 09:07:17 +0800 Subject: [PATCH 3/3] update macro definition --- drivers/arm/i2c_master.c | 2 +- drivers/arm/i2c_master.h | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/drivers/arm/i2c_master.c b/drivers/arm/i2c_master.c index f22b4d551cad..b9eff0ad2e8b 100644 --- a/drivers/arm/i2c_master.c +++ b/drivers/arm/i2c_master.c @@ -62,7 +62,7 @@ __attribute__((weak)) void i2c_init(void) { palSetPadMode(I2C1_SDA_BANK, I2C1_SDA, PAL_MODE_INPUT); chThdSleepMilliseconds(10); -#if defined(USE_I2CV1) && !defined(USE_GPIOV2) +#if defined(USE_GPIOV1) palSetPadMode(I2C1_SCL_BANK, I2C1_SCL, PAL_MODE_STM32_ALTERNATE_OPENDRAIN); palSetPadMode(I2C1_SDA_BANK, I2C1_SDA, PAL_MODE_STM32_ALTERNATE_OPENDRAIN); #else diff --git a/drivers/arm/i2c_master.h b/drivers/arm/i2c_master.h index 23bb9eac066b..31cbfb9774a7 100644 --- a/drivers/arm/i2c_master.h +++ b/drivers/arm/i2c_master.h @@ -51,13 +51,11 @@ # define I2C1_SDA 7 #endif -#if defined(STM32F4XX) || defined (STM32F3XX) -# ifndef USE_GPIOV2 -# define USE_GPIOV2 -# endif +#if defined(STM32F1XX) || defined(STM32F1xx) +# define USE_GPIOV1 #endif -#ifdef USE_GPIOV2 +#ifndef USE_GPIOV1 // The default PAL alternate modes are used to signal that the pins are used for I2C # ifndef I2C1_SCL_PAL_MODE # define I2C1_SCL_PAL_MODE 4