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..cc173def4 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,8 +63,16 @@ i2c_dev i2c_dev1 = { /** I2C2 device */ i2c_dev i2c_dev2 = { .regs = I2C2_BASE, - .sda_pin = PB11, +#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, @@ -64,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); @@ -206,7 +243,14 @@ 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); + /* 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); + } + else { + gpio_set_af_mode(dev->sda_pin, GPIO_AFMODE_I2C1_3); + } + i2c_init(dev); @@ -512,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 @@ -541,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/variants/blackpill_f401/blackpill_f401.h b/STM32F4/variants/blackpill_f401/blackpill_f401.h index acd990142..87e28c9f5 100644 --- a/STM32F4/variants/blackpill_f401/blackpill_f401.h +++ b/STM32F4/variants/blackpill_f401/blackpill_f401.h @@ -52,11 +52,15 @@ //#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 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_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 103a12251..5d7b5cb34 100644 --- a/STM32F4/variants/blackpill_f411/blackpill_f411.h +++ b/STM32F4/variants/blackpill_f411/blackpill_f411.h @@ -53,11 +53,15 @@ //#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 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_I2C3_SCL_PIN PA8 +#define BOARD_I2C3_SDA_PIN PB4 #define BOARD_NR_SPI 3 #define BOARD_SPI1_NSS_PIN PA4