Skip to content

Commit

Permalink
Merge pull request #936 from board707/HardWire2
Browse files Browse the repository at this point in the history
STM32F4: Fix support of second I2C device (I2C2) and add a third (I2C3)
  • Loading branch information
stevstrong authored Sep 3, 2024
2 parents d05a128 + a1e6f3f commit 245b97a
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 5 deletions.
1 change: 1 addition & 0 deletions STM32F4/cores/maple/libmaple/gpio_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
60 changes: 57 additions & 3 deletions STM32F4/cores/maple/libmaple/i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -55,15 +63,44 @@ 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,
.state = I2C_STATE_DISABLED
};
#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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
*/
Expand Down
6 changes: 6 additions & 0 deletions STM32F4/cores/maple/libmaple/i2c.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions STM32F4/libraries/Wire/Wire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion STM32F4/variants/blackpill_f401/blackpill_f401.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion STM32F4/variants/blackpill_f411/blackpill_f411.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 245b97a

Please sign in to comment.