Skip to content

Commit 245b97a

Browse files
authored
Merge pull request #936 from board707/HardWire2
STM32F4: Fix support of second I2C device (I2C2) and add a third (I2C3)
2 parents d05a128 + a1e6f3f commit 245b97a

File tree

6 files changed

+79
-5
lines changed

6 files changed

+79
-5
lines changed

STM32F4/cores/maple/libmaple/gpio_def.h

+1
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ typedef enum {
243243
GPIO_AFMODE_USART4_6 = 8,
244244
GPIO_AFMODE_CAN1_2 = 9,
245245
GPIO_AFMODE_TIM12_14 = 9,
246+
GPIO_AFMODE_I2C2_3 = 9,
246247
GPIO_AFMODE_OTG_FS = 10,
247248
GPIO_AFMODE_ETH = 11,
248249
GPIO_AFMODE_FSMC = 12,

STM32F4/cores/maple/libmaple/i2c.c

+57-3
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,16 @@
4343
/** I2C1 device */
4444
i2c_dev i2c_dev1 = {
4545
.regs = I2C1_BASE,
46-
.sda_pin = PB7, // PB8
46+
#ifdef BOARD_I2C1_SDA_PIN
47+
.sda_pin = BOARD_I2C1_SDA_PIN,
48+
#else
49+
.sda_pin = PB7,
50+
#endif
51+
#ifdef BOARD_I2C1_SCL_PIN
52+
.scl_pin = BOARD_I2C1_SCL_PIN,
53+
#else
4754
.scl_pin = PB6,
55+
#endif
4856
.clk_id = RCC_I2C1,
4957
.ev_nvic_line = NVIC_I2C1_EV,
5058
.er_nvic_line = NVIC_I2C1_ER,
@@ -55,15 +63,44 @@ i2c_dev i2c_dev1 = {
5563
/** I2C2 device */
5664
i2c_dev i2c_dev2 = {
5765
.regs = I2C2_BASE,
58-
.sda_pin = PB11,
66+
#ifdef BOARD_I2C2_SDA_PIN
67+
.sda_pin = BOARD_I2C2_SDA_PIN,
68+
#else
69+
.sda_pin = PB11,
70+
#endif
71+
#ifdef BOARD_I2C2_SCL_PIN
72+
.scl_pin = BOARD_I2C2_SCL_PIN,
73+
#else
5974
.scl_pin = PB10,
75+
#endif
6076
.clk_id = RCC_I2C2,
6177
.ev_nvic_line = NVIC_I2C2_EV,
6278
.er_nvic_line = NVIC_I2C2_ER,
6379
.state = I2C_STATE_DISABLED
6480
};
6581
#endif
6682

83+
#if BOARD_NR_I2C>2
84+
/** I2C2 device */
85+
i2c_dev i2c_dev3 = {
86+
.regs = I2C3_BASE,
87+
#ifdef BOARD_I2C3_SDA_PIN
88+
.sda_pin = BOARD_I2C3_SDA_PIN,
89+
#else
90+
.sda_pin = PC9,
91+
#endif
92+
#ifdef BOARD_I2C3_SCL_PIN
93+
.scl_pin = BOARD_I2C3_SCL_PIN,
94+
#else
95+
.scl_pin = PA8,
96+
#endif
97+
.clk_id = RCC_I2C3,
98+
.ev_nvic_line = NVIC_I2C3_EV,
99+
.er_nvic_line = NVIC_I2C3_ER,
100+
.state = I2C_STATE_DISABLED
101+
};
102+
#endif
103+
67104
static inline int32 wait_for_state_change(i2c_dev *dev,
68105
i2c_state state,
69106
uint32 timeout);
@@ -206,7 +243,14 @@ void i2c_master_enable(i2c_dev *dev, uint32 flags) {
206243
delay_us(2);
207244
gpio_set_af_mode(dev->scl_pin, GPIO_AFMODE_I2C1_3);
208245
delay_us(2);
209-
gpio_set_af_mode(dev->sda_pin, GPIO_AFMODE_I2C1_3);
246+
/* specific SDA pin remap for I2C2 on F4xx mcu*/
247+
if ((dev->sda_pin == PB3) || (dev->sda_pin == PB4)) {
248+
gpio_set_af_mode(dev->sda_pin, GPIO_AFMODE_I2C2_3);
249+
}
250+
else {
251+
gpio_set_af_mode(dev->sda_pin, GPIO_AFMODE_I2C1_3);
252+
}
253+
210254

211255
i2c_init(dev);
212256

@@ -512,6 +556,11 @@ void __irq_i2c2_ev(void) {
512556
i2c_irq_handler(&i2c_dev2);
513557
}
514558
#endif
559+
#if BOARD_NR_I2C>2
560+
void __irq_i2c3_ev(void) {
561+
i2c_irq_handler(&i2c_dev3);
562+
}
563+
#endif
515564
/**
516565
* @brief Interrupt handler for I2C error conditions
517566
* @param dev I2C device
@@ -541,6 +590,11 @@ void __irq_i2c2_er(void) {
541590
i2c_irq_error_handler(&i2c_dev2);
542591
}
543592
#endif
593+
#if BOARD_NR_I2C>2
594+
void __irq_i2c3_er(void) {
595+
i2c_irq_error_handler(&i2c_dev3);
596+
}
597+
#endif
544598
/*
545599
* CCR/TRISE configuration helper
546600
*/

STM32F4/cores/maple/libmaple/i2c.h

+6
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ extern i2c_dev i2c_dev1;
100100
extern i2c_dev i2c_dev2;
101101
#define I2C2 (&i2c_dev2)
102102
#endif
103+
#if BOARD_NR_I2C>2
104+
extern i2c_dev i2c_dev3;
105+
#define I2C3 (&i2c_dev3)
106+
#endif
103107

104108
/*
105109
* Register map base pointers
@@ -109,6 +113,8 @@ extern i2c_dev i2c_dev2;
109113
#define I2C1_BASE ((struct i2c_reg_map*)0x40005400)
110114
/** I2C2 register map base pointer */
111115
#define I2C2_BASE ((struct i2c_reg_map*)0x40005800)
116+
/** I2C3 register map base pointer */
117+
#define I2C3_BASE ((struct i2c_reg_map*)0x40005C00)
112118

113119
/*
114120
* Register bit definitions

STM32F4/libraries/Wire/Wire.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ TwoWire::TwoWire(uint8 dev_sel, uint8 flags) {
6767
else if (dev_sel == 2) {
6868
sel_hard = I2C2;
6969
}
70+
#endif
71+
#if BOARD_NR_I2C>2
72+
else if (dev_sel == 3) {
73+
sel_hard = I2C3;
74+
}
7075
#endif
7176
else {
7277
ASSERT(1);

STM32F4/variants/blackpill_f401/blackpill_f401.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,15 @@
5252
//#define BOARD_USART6_TX_PIN PA11 // USB_DM
5353
//#define BOARD_USART6_RX_PIN PA12 // USB_DP
5454

55-
#define BOARD_NR_I2C 1
55+
#define BOARD_NR_I2C 3
5656
#define BOARD_I2C1_SCL_PIN PB6
5757
#define BOARD_I2C1_SDA_PIN PB7
5858
#define BOARD_I2C1A_SCL_PIN PB8
5959
#define BOARD_I2C1A_SDA_PIN PB9
60+
#define BOARD_I2C2_SCL_PIN PB10
61+
#define BOARD_I2C2_SDA_PIN PB3
62+
#define BOARD_I2C3_SCL_PIN PA8
63+
#define BOARD_I2C3_SDA_PIN PB4
6064

6165
#define BOARD_NR_SPI 3
6266
#define BOARD_SPI1_NSS_PIN PA4

STM32F4/variants/blackpill_f411/blackpill_f411.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,15 @@
5353
//#define BOARD_USART6_TX_PIN PA11 // USB_DM
5454
//#define BOARD_USART6_RX_PIN PA12 // USB_DP
5555

56-
#define BOARD_NR_I2C 1
56+
#define BOARD_NR_I2C 3
5757
#define BOARD_I2C1_SCL_PIN PB6
5858
#define BOARD_I2C1_SDA_PIN PB7
5959
#define BOARD_I2C1A_SCL_PIN PB8
6060
#define BOARD_I2C1A_SDA_PIN PB9
61+
#define BOARD_I2C2_SCL_PIN PB10
62+
#define BOARD_I2C2_SDA_PIN PB3
63+
#define BOARD_I2C3_SCL_PIN PA8
64+
#define BOARD_I2C3_SDA_PIN PB4
6165

6266
#define BOARD_NR_SPI 3
6367
#define BOARD_SPI1_NSS_PIN PA4

0 commit comments

Comments
 (0)