8787#define PYB_I2C_MASTER (0)
8888#define PYB_I2C_SLAVE (1)
8989
90- #if MICROPY_HW_ENABLE_I2C1
90+ #if defined( MICROPY_HW_I2C1_SCL )
9191I2C_HandleTypeDef I2CHandle1 = {.Instance = NULL };
9292#endif
93+ #if defined(MICROPY_HW_I2C2_SCL )
9394I2C_HandleTypeDef I2CHandle2 = {.Instance = NULL };
95+ #endif
96+ #if defined(MICROPY_HW_I2C3_SCL )
97+ I2C_HandleTypeDef I2CHandle3 = {.Instance = NULL };
98+ #endif
9499
95100void i2c_init0 (void ) {
96101 // reset the I2C1 handles
97- #if MICROPY_HW_ENABLE_I2C1
102+ #if defined( MICROPY_HW_I2C1_SCL )
98103 memset (& I2CHandle1 , 0 , sizeof (I2C_HandleTypeDef ));
99104 I2CHandle1 .Instance = I2C1 ;
100- #endif
105+ #endif
106+ #if defined(MICROPY_HW_I2C2_SCL )
101107 memset (& I2CHandle2 , 0 , sizeof (I2C_HandleTypeDef ));
102108 I2CHandle2 .Instance = I2C2 ;
109+ #endif
110+ #if defined(MICROPY_HW_I2C3_SCL )
111+ memset (& I2CHandle3 , 0 , sizeof (I2C_HandleTypeDef ));
112+ I2CHandle3 .Instance = I2C3 ;
113+ #endif
103114}
104115
105116void i2c_init (I2C_HandleTypeDef * i2c ) {
@@ -111,22 +122,27 @@ void i2c_init(I2C_HandleTypeDef *i2c) {
111122
112123 const pin_obj_t * pins [2 ];
113124 if (0 ) {
114- #if MICROPY_HW_ENABLE_I2C1
125+ #if defined( MICROPY_HW_I2C1_SCL )
115126 } else if (i2c == & I2CHandle1 ) {
116- // X-skin: X9=PB6=SCL, X10=PB7=SDA
117- pins [0 ] = & pin_B6 ;
118- pins [1 ] = & pin_B7 ;
127+ pins [0 ] = & MICROPY_HW_I2C1_SCL ;
128+ pins [1 ] = & MICROPY_HW_I2C1_SDA ;
119129 GPIO_InitStructure .Alternate = GPIO_AF4_I2C1 ;
120- // enable the I2C clock
121130 __I2C1_CLK_ENABLE ();
122- #endif
131+ #endif
132+ #if defined(MICROPY_HW_I2C2_SCL )
123133 } else if (i2c == & I2CHandle2 ) {
124- // Y-skin: Y9=PB10=SCL, Y10=PB11=SDA
125- pins [0 ] = & pin_B10 ;
126- pins [1 ] = & pin_B11 ;
134+ pins [0 ] = & MICROPY_HW_I2C2_SCL ;
135+ pins [1 ] = & MICROPY_HW_I2C2_SDA ;
127136 GPIO_InitStructure .Alternate = GPIO_AF4_I2C2 ;
128- // enable the I2C clock
129137 __I2C2_CLK_ENABLE ();
138+ #endif
139+ #if defined(MICROPY_HW_I2C3_SCL )
140+ } else if (i2c == & I2CHandle3 ) {
141+ pins [0 ] = & MICROPY_HW_I2C3_SCL ;
142+ pins [1 ] = & MICROPY_HW_I2C3_SDA ;
143+ GPIO_InitStructure .Alternate = GPIO_AF4_I2C3 ;
144+ __I2C3_CLK_ENABLE ();
145+ #endif
130146 } else {
131147 // I2C does not exist for this board (shouldn't get here, should be checked by caller)
132148 return ;
@@ -151,16 +167,24 @@ void i2c_init(I2C_HandleTypeDef *i2c) {
151167void i2c_deinit (I2C_HandleTypeDef * i2c ) {
152168 HAL_I2C_DeInit (i2c );
153169 if (0 ) {
154- #if MICROPY_HW_ENABLE_I2C1
170+ #if defined( MICROPY_HW_I2C1_SCL )
155171 } else if (i2c -> Instance == I2C1 ) {
156172 __I2C1_FORCE_RESET ();
157173 __I2C1_RELEASE_RESET ();
158174 __I2C1_CLK_DISABLE ();
159- #endif
175+ #endif
176+ #if defined(MICROPY_HW_I2C2_SCL )
160177 } else if (i2c -> Instance == I2C2 ) {
161178 __I2C2_FORCE_RESET ();
162179 __I2C2_RELEASE_RESET ();
163180 __I2C2_CLK_DISABLE ();
181+ #endif
182+ #if defined(MICROPY_HW_I2C3_SCL )
183+ } else if (i2c -> Instance == I2C3 ) {
184+ __I2C3_FORCE_RESET ();
185+ __I2C3_RELEASE_RESET ();
186+ __I2C3_CLK_DISABLE ();
187+ #endif
164188 }
165189}
166190
@@ -175,20 +199,31 @@ typedef struct _pyb_i2c_obj_t {
175199STATIC inline bool in_master_mode (pyb_i2c_obj_t * self ) { return self -> i2c -> Init .OwnAddress1 == PYB_I2C_MASTER_ADDRESS ; }
176200
177201STATIC const pyb_i2c_obj_t pyb_i2c_obj [] = {
178- #if MICROPY_HW_ENABLE_I2C1
202+ #if defined( MICROPY_HW_I2C1_SCL )
179203 {{& pyb_i2c_type }, & I2CHandle1 },
180- #else
181- {{& pyb_i2c_type }, NULL },
182- #endif
183- {{& pyb_i2c_type }, & I2CHandle2 }
204+ #endif
205+ #if defined(MICROPY_HW_I2C2_SCL )
206+ {{& pyb_i2c_type }, & I2CHandle2 },
207+ #endif
208+ #if defined(MICROPY_HW_I2C3_SCL )
209+ {{& pyb_i2c_type }, & I2CHandle3 },
210+ #endif
184211};
185212
186213STATIC void pyb_i2c_print (const mp_print_t * print , mp_obj_t self_in , mp_print_kind_t kind ) {
187214 pyb_i2c_obj_t * self = self_in ;
188215
189- uint i2c_num ;
190- if (self -> i2c -> Instance == I2C1 ) { i2c_num = 1 ; }
191- else { i2c_num = 2 ; }
216+ uint i2c_num = 0 ;
217+ if (0 ) { }
218+ #if defined(MICROPY_HW_I2C1_SCL )
219+ else if (self -> i2c -> Instance == I2C1 ) { i2c_num = 1 ; }
220+ #endif
221+ #if defined(MICROPY_HW_I2C2_SCL )
222+ else if (self -> i2c -> Instance == I2C2 ) { i2c_num = 2 ; }
223+ #endif
224+ #if defined(MICROPY_HW_I2C3_SCL )
225+ else if (self -> i2c -> Instance == I2C3 ) { i2c_num = 3 ; }
226+ #endif
192227
193228 if (self -> i2c -> State == HAL_I2C_STATE_RESET ) {
194229 mp_printf (print , "I2C(%u)" , i2c_num );
0 commit comments