From 8347901a406a924d3715ea730bc0649934016027 Mon Sep 17 00:00:00 2001 From: board707 <50185434+board707@users.noreply.github.com> Date: Thu, 29 Aug 2024 18:08:48 +0300 Subject: [PATCH 1/3] Add support for hardwire I2C2 on blackpill F401 --- STM32F4/cores/maple/libmaple/gpio_def.h | 1 + STM32F4/cores/maple/libmaple/i2c.c | 14 +++++++++++--- STM32F4/platform.txt | 4 ++-- STM32F4/variants/blackpill_f401/blackpill_f401.h | 6 +++--- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/STM32F4/cores/maple/libmaple/gpio_def.h b/STM32F4/cores/maple/libmaple/gpio_def.h index 970386c9a..1f26e8160 100644 --- a/STM32F4/cores/maple/libmaple/gpio_def.h +++ b/STM32F4/cores/maple/libmaple/gpio_def.h @@ -243,6 +243,7 @@ typedef enum { GPIO_AFMODE_USART4_6 = 8, GPIO_AFMODE_CAN1_2 = 9, GPIO_AFMODE_TIM12_14 = 9, + GPIO_AFMODE_I2C2_3 = 9, GPIO_AFMODE_OTG_FS = 10, GPIO_AFMODE_ETH = 11, GPIO_AFMODE_FSMC = 12, diff --git a/STM32F4/cores/maple/libmaple/i2c.c b/STM32F4/cores/maple/libmaple/i2c.c index 4cf413bd9..156b1b7be 100644 --- a/STM32F4/cores/maple/libmaple/i2c.c +++ b/STM32F4/cores/maple/libmaple/i2c.c @@ -55,8 +55,10 @@ i2c_dev i2c_dev1 = { /** I2C2 device */ i2c_dev i2c_dev2 = { .regs = I2C2_BASE, - .sda_pin = PB11, - .scl_pin = PB10, + // .sda_pin = PB3, + //.scl_pin = PB10, + .sda_pin = BOARD_I2C2_SDA_PIN, + .scl_pin = BOARD_I2C2_SCL_PIN, .clk_id = RCC_I2C2, .ev_nvic_line = NVIC_I2C2_EV, .er_nvic_line = NVIC_I2C2_ER, @@ -206,7 +208,13 @@ void i2c_master_enable(i2c_dev *dev, uint32 flags) { delay_us(2); gpio_set_af_mode(dev->scl_pin, GPIO_AFMODE_I2C1_3); delay_us(2); - gpio_set_af_mode(dev->sda_pin, GPIO_AFMODE_I2C1_3); + if ((dev->sda_pin == PB3) || (dev->sda_pin == PB4)) { + gpio_set_af_mode(dev->sda_pin, GPIO_AFMODE_I2C2_3); + } + else { + gpio_set_af_mode(dev->sda_pin, GPIO_AFMODE_I2C1_3); + } + i2c_init(dev); diff --git a/STM32F4/platform.txt b/STM32F4/platform.txt index 8a329007e..450b023ab 100755 --- a/STM32F4/platform.txt +++ b/STM32F4/platform.txt @@ -121,7 +121,7 @@ tools.stlink_upload.cmd=stlink_upload tools.stlink_upload.cmd.windows=stlink_upload.bat tools.stlink_upload.path.windows={runtime.hardware.path}/tools/win tools.stlink_upload.path.macosx={runtime.hardware.path}/tools/macosx -tools.stlink_upload.path.linux={runtime.hardware.path}/tools/linux +tools.stlink_upload.path.linux={runtime.hardware.path}/tools/linux64 tools.stlink_upload.path.linux64={runtime.hardware.path}/tools/linux64 tools.stlink_upload.upload.params.verbose=-d tools.stlink_upload.upload.params.quiet= @@ -132,7 +132,7 @@ tools.hid_upload.cmd=hid_upload tools.hid_upload.cmd.windows=hid_upload.bat tools.hid_upload.path.windows={runtime.hardware.path}/tools/win tools.hid_upload.path.macosx={runtime.hardware.path}/tools/macosx -tools.hid_upload.path.linux={runtime.hardware.path}/tools/linux +tools.hid_upload.path.linux={runtime.hardware.path}/tools/linux64 tools.hid_upload.path.linux64={runtime.hardware.path}/tools/linux64 tools.hid_upload.upload.params.verbose=-d tools.hid_upload.upload.params.quiet=n diff --git a/STM32F4/variants/blackpill_f401/blackpill_f401.h b/STM32F4/variants/blackpill_f401/blackpill_f401.h index acd990142..78e7a55c4 100644 --- a/STM32F4/variants/blackpill_f401/blackpill_f401.h +++ b/STM32F4/variants/blackpill_f401/blackpill_f401.h @@ -52,11 +52,11 @@ //#define BOARD_USART6_TX_PIN PA11 // USB_DM //#define BOARD_USART6_RX_PIN PA12 // USB_DP -#define BOARD_NR_I2C 1 +#define BOARD_NR_I2C 2 #define BOARD_I2C1_SCL_PIN PB6 #define BOARD_I2C1_SDA_PIN PB7 -#define BOARD_I2C1A_SCL_PIN PB8 -#define BOARD_I2C1A_SDA_PIN PB9 +#define BOARD_I2C2_SCL_PIN PB10 +#define BOARD_I2C2_SDA_PIN PB3 #define BOARD_NR_SPI 3 #define BOARD_SPI1_NSS_PIN PA4 From 951c1ace8d50e14622d176664cb3b1d690c09fad Mon Sep 17 00:00:00 2001 From: board707 <50185434+board707@users.noreply.github.com> Date: Fri, 30 Aug 2024 13:03:52 +0300 Subject: [PATCH 2/3] Add conditional select of I2C pins. --- STM32F4/cores/maple/libmaple/i2c.c | 25 +++++++++++++++---- .../variants/blackpill_f401/blackpill_f401.h | 2 ++ .../variants/blackpill_f411/blackpill_f411.h | 4 ++- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/STM32F4/cores/maple/libmaple/i2c.c b/STM32F4/cores/maple/libmaple/i2c.c index 156b1b7be..492fd7412 100644 --- a/STM32F4/cores/maple/libmaple/i2c.c +++ b/STM32F4/cores/maple/libmaple/i2c.c @@ -43,8 +43,16 @@ /** I2C1 device */ i2c_dev i2c_dev1 = { .regs = I2C1_BASE, - .sda_pin = PB7, // PB8 +#ifdef BOARD_I2C1_SDA_PIN + .sda_pin = BOARD_I2C1_SDA_PIN, +#else + .sda_pin = PB7, +#endif +#ifdef BOARD_I2C1_SCL_PIN + .scl_pin = BOARD_I2C1_SCL_PIN, +#else .scl_pin = PB6, +#endif .clk_id = RCC_I2C1, .ev_nvic_line = NVIC_I2C1_EV, .er_nvic_line = NVIC_I2C1_ER, @@ -55,10 +63,16 @@ i2c_dev i2c_dev1 = { /** I2C2 device */ i2c_dev i2c_dev2 = { .regs = I2C2_BASE, - // .sda_pin = PB3, - //.scl_pin = PB10, - .sda_pin = BOARD_I2C2_SDA_PIN, - .scl_pin = BOARD_I2C2_SCL_PIN, +#ifdef BOARD_I2C2_SDA_PIN + .sda_pin = BOARD_I2C2_SDA_PIN, +#else + .sda_pin = PB11, +#endif +#ifdef BOARD_I2C2_SCL_PIN + .scl_pin = BOARD_I2C2_SCL_PIN, +#else + .scl_pin = PB10, +#endif .clk_id = RCC_I2C2, .ev_nvic_line = NVIC_I2C2_EV, .er_nvic_line = NVIC_I2C2_ER, @@ -208,6 +222,7 @@ void i2c_master_enable(i2c_dev *dev, uint32 flags) { delay_us(2); gpio_set_af_mode(dev->scl_pin, GPIO_AFMODE_I2C1_3); delay_us(2); + /* specific SDA pin remap for I2C2 on F4xx mcu*/ if ((dev->sda_pin == PB3) || (dev->sda_pin == PB4)) { gpio_set_af_mode(dev->sda_pin, GPIO_AFMODE_I2C2_3); } diff --git a/STM32F4/variants/blackpill_f401/blackpill_f401.h b/STM32F4/variants/blackpill_f401/blackpill_f401.h index 78e7a55c4..8e2e6aac1 100644 --- a/STM32F4/variants/blackpill_f401/blackpill_f401.h +++ b/STM32F4/variants/blackpill_f401/blackpill_f401.h @@ -55,6 +55,8 @@ #define BOARD_NR_I2C 2 #define BOARD_I2C1_SCL_PIN PB6 #define BOARD_I2C1_SDA_PIN PB7 +#define BOARD_I2C1A_SCL_PIN PB8 +#define BOARD_I2C1A_SDA_PIN PB9 #define BOARD_I2C2_SCL_PIN PB10 #define BOARD_I2C2_SDA_PIN PB3 diff --git a/STM32F4/variants/blackpill_f411/blackpill_f411.h b/STM32F4/variants/blackpill_f411/blackpill_f411.h index 103a12251..4d984b8a7 100644 --- a/STM32F4/variants/blackpill_f411/blackpill_f411.h +++ b/STM32F4/variants/blackpill_f411/blackpill_f411.h @@ -53,11 +53,13 @@ //#define BOARD_USART6_TX_PIN PA11 // USB_DM //#define BOARD_USART6_RX_PIN PA12 // USB_DP -#define BOARD_NR_I2C 1 +#define BOARD_NR_I2C 2 #define BOARD_I2C1_SCL_PIN PB6 #define BOARD_I2C1_SDA_PIN PB7 #define BOARD_I2C1A_SCL_PIN PB8 #define BOARD_I2C1A_SDA_PIN PB9 +#define BOARD_I2C2_SCL_PIN PB10 +#define BOARD_I2C2_SDA_PIN PB3 #define BOARD_NR_SPI 3 #define BOARD_SPI1_NSS_PIN PA4 From a1e6f3f7c3fd2dfaa4283620a3f958a5270518a4 Mon Sep 17 00:00:00 2001 From: board707 <50185434+board707@users.noreply.github.com> Date: Fri, 30 Aug 2024 14:31:02 +0300 Subject: [PATCH 3/3] Add support of I2C3 on blackpill boards --- STM32F4/cores/maple/libmaple/i2c.c | 31 +++++++++++++++++++ STM32F4/cores/maple/libmaple/i2c.h | 6 ++++ STM32F4/libraries/Wire/Wire.cpp | 5 +++ STM32F4/platform.txt | 4 +-- .../variants/blackpill_f401/blackpill_f401.h | 8 +++-- .../variants/blackpill_f411/blackpill_f411.h | 8 +++-- 6 files changed, 54 insertions(+), 8 deletions(-) diff --git a/STM32F4/cores/maple/libmaple/i2c.c b/STM32F4/cores/maple/libmaple/i2c.c index 492fd7412..cc173def4 100644 --- a/STM32F4/cores/maple/libmaple/i2c.c +++ b/STM32F4/cores/maple/libmaple/i2c.c @@ -80,6 +80,27 @@ i2c_dev i2c_dev2 = { }; #endif +#if BOARD_NR_I2C>2 +/** I2C2 device */ +i2c_dev i2c_dev3 = { + .regs = I2C3_BASE, +#ifdef BOARD_I2C3_SDA_PIN + .sda_pin = BOARD_I2C3_SDA_PIN, +#else + .sda_pin = PC9, +#endif +#ifdef BOARD_I2C3_SCL_PIN + .scl_pin = BOARD_I2C3_SCL_PIN, +#else + .scl_pin = PA8, +#endif + .clk_id = RCC_I2C3, + .ev_nvic_line = NVIC_I2C3_EV, + .er_nvic_line = NVIC_I2C3_ER, + .state = I2C_STATE_DISABLED +}; +#endif + static inline int32 wait_for_state_change(i2c_dev *dev, i2c_state state, uint32 timeout); @@ -535,6 +556,11 @@ void __irq_i2c2_ev(void) { i2c_irq_handler(&i2c_dev2); } #endif +#if BOARD_NR_I2C>2 +void __irq_i2c3_ev(void) { + i2c_irq_handler(&i2c_dev3); +} +#endif /** * @brief Interrupt handler for I2C error conditions * @param dev I2C device @@ -564,6 +590,11 @@ void __irq_i2c2_er(void) { i2c_irq_error_handler(&i2c_dev2); } #endif +#if BOARD_NR_I2C>2 +void __irq_i2c3_er(void) { + i2c_irq_error_handler(&i2c_dev3); +} +#endif /* * CCR/TRISE configuration helper */ diff --git a/STM32F4/cores/maple/libmaple/i2c.h b/STM32F4/cores/maple/libmaple/i2c.h index d962124d8..cc36a161e 100644 --- a/STM32F4/cores/maple/libmaple/i2c.h +++ b/STM32F4/cores/maple/libmaple/i2c.h @@ -100,6 +100,10 @@ extern i2c_dev i2c_dev1; extern i2c_dev i2c_dev2; #define I2C2 (&i2c_dev2) #endif +#if BOARD_NR_I2C>2 +extern i2c_dev i2c_dev3; +#define I2C3 (&i2c_dev3) +#endif /* * Register map base pointers @@ -109,6 +113,8 @@ extern i2c_dev i2c_dev2; #define I2C1_BASE ((struct i2c_reg_map*)0x40005400) /** I2C2 register map base pointer */ #define I2C2_BASE ((struct i2c_reg_map*)0x40005800) +/** I2C3 register map base pointer */ +#define I2C3_BASE ((struct i2c_reg_map*)0x40005C00) /* * Register bit definitions diff --git a/STM32F4/libraries/Wire/Wire.cpp b/STM32F4/libraries/Wire/Wire.cpp index dbc61f0e3..351aa8a6f 100644 --- a/STM32F4/libraries/Wire/Wire.cpp +++ b/STM32F4/libraries/Wire/Wire.cpp @@ -67,6 +67,11 @@ TwoWire::TwoWire(uint8 dev_sel, uint8 flags) { else if (dev_sel == 2) { sel_hard = I2C2; } +#endif +#if BOARD_NR_I2C>2 + else if (dev_sel == 3) { + sel_hard = I2C3; + } #endif else { ASSERT(1); diff --git a/STM32F4/platform.txt b/STM32F4/platform.txt index 450b023ab..8a329007e 100755 --- a/STM32F4/platform.txt +++ b/STM32F4/platform.txt @@ -121,7 +121,7 @@ tools.stlink_upload.cmd=stlink_upload tools.stlink_upload.cmd.windows=stlink_upload.bat tools.stlink_upload.path.windows={runtime.hardware.path}/tools/win tools.stlink_upload.path.macosx={runtime.hardware.path}/tools/macosx -tools.stlink_upload.path.linux={runtime.hardware.path}/tools/linux64 +tools.stlink_upload.path.linux={runtime.hardware.path}/tools/linux tools.stlink_upload.path.linux64={runtime.hardware.path}/tools/linux64 tools.stlink_upload.upload.params.verbose=-d tools.stlink_upload.upload.params.quiet= @@ -132,7 +132,7 @@ tools.hid_upload.cmd=hid_upload tools.hid_upload.cmd.windows=hid_upload.bat tools.hid_upload.path.windows={runtime.hardware.path}/tools/win tools.hid_upload.path.macosx={runtime.hardware.path}/tools/macosx -tools.hid_upload.path.linux={runtime.hardware.path}/tools/linux64 +tools.hid_upload.path.linux={runtime.hardware.path}/tools/linux tools.hid_upload.path.linux64={runtime.hardware.path}/tools/linux64 tools.hid_upload.upload.params.verbose=-d tools.hid_upload.upload.params.quiet=n diff --git a/STM32F4/variants/blackpill_f401/blackpill_f401.h b/STM32F4/variants/blackpill_f401/blackpill_f401.h index 8e2e6aac1..87e28c9f5 100644 --- a/STM32F4/variants/blackpill_f401/blackpill_f401.h +++ b/STM32F4/variants/blackpill_f401/blackpill_f401.h @@ -52,13 +52,15 @@ //#define BOARD_USART6_TX_PIN PA11 // USB_DM //#define BOARD_USART6_RX_PIN PA12 // USB_DP -#define BOARD_NR_I2C 2 +#define BOARD_NR_I2C 3 #define BOARD_I2C1_SCL_PIN PB6 #define BOARD_I2C1_SDA_PIN PB7 #define BOARD_I2C1A_SCL_PIN PB8 #define BOARD_I2C1A_SDA_PIN PB9 -#define BOARD_I2C2_SCL_PIN PB10 -#define BOARD_I2C2_SDA_PIN PB3 +#define BOARD_I2C2_SCL_PIN PB10 +#define BOARD_I2C2_SDA_PIN PB3 +#define BOARD_I2C3_SCL_PIN PA8 +#define BOARD_I2C3_SDA_PIN PB4 #define BOARD_NR_SPI 3 #define BOARD_SPI1_NSS_PIN PA4 diff --git a/STM32F4/variants/blackpill_f411/blackpill_f411.h b/STM32F4/variants/blackpill_f411/blackpill_f411.h index 4d984b8a7..5d7b5cb34 100644 --- a/STM32F4/variants/blackpill_f411/blackpill_f411.h +++ b/STM32F4/variants/blackpill_f411/blackpill_f411.h @@ -53,13 +53,15 @@ //#define BOARD_USART6_TX_PIN PA11 // USB_DM //#define BOARD_USART6_RX_PIN PA12 // USB_DP -#define BOARD_NR_I2C 2 +#define BOARD_NR_I2C 3 #define BOARD_I2C1_SCL_PIN PB6 #define BOARD_I2C1_SDA_PIN PB7 #define BOARD_I2C1A_SCL_PIN PB8 #define BOARD_I2C1A_SDA_PIN PB9 -#define BOARD_I2C2_SCL_PIN PB10 -#define BOARD_I2C2_SDA_PIN PB3 +#define BOARD_I2C2_SCL_PIN PB10 +#define BOARD_I2C2_SDA_PIN PB3 +#define BOARD_I2C3_SCL_PIN PA8 +#define BOARD_I2C3_SDA_PIN PB4 #define BOARD_NR_SPI 3 #define BOARD_SPI1_NSS_PIN PA4