@@ -46,11 +46,11 @@ STATIC void i2c_clock_disable(uint8_t mask);
46
46
47
47
void i2c_reset (void ) {
48
48
uint16_t never_reset_mask = 0x00 ;
49
- for (int i = 0 ; i < MAX_I2C ;i ++ ) {
49
+ for (int i = 0 ; i < MAX_I2C ; i ++ ) {
50
50
if (!never_reset_i2c [i ]) {
51
51
reserved_i2c [i ] = false;
52
52
} else {
53
- never_reset_mask |= 1 << i ;
53
+ never_reset_mask |= 1 << i ;
54
54
}
55
55
}
56
56
i2c_clock_disable (ALL_CLOCKS & ~(never_reset_mask ));
@@ -61,17 +61,17 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
61
61
62
62
//match pins to I2C objects
63
63
I2C_TypeDef * I2Cx ;
64
- uint8_t sda_len = sizeof (mcu_i2c_sda_list )/ sizeof (* mcu_i2c_sda_list );
65
- uint8_t scl_len = sizeof (mcu_i2c_scl_list )/ sizeof (* mcu_i2c_scl_list );
64
+ uint8_t sda_len = sizeof (mcu_i2c_sda_list ) / sizeof (* mcu_i2c_sda_list );
65
+ uint8_t scl_len = sizeof (mcu_i2c_scl_list ) / sizeof (* mcu_i2c_scl_list );
66
66
bool i2c_taken = false;
67
67
68
- for (uint i = 0 ; i < sda_len ;i ++ ) {
68
+ for (uint i = 0 ; i < sda_len ; i ++ ) {
69
69
if (mcu_i2c_sda_list [i ].pin == sda ) {
70
- for (uint j = 0 ; j < scl_len ;j ++ ) {
70
+ for (uint j = 0 ; j < scl_len ; j ++ ) {
71
71
if ((mcu_i2c_scl_list [j ].pin == scl )
72
72
&& (mcu_i2c_scl_list [j ].i2c_index == mcu_i2c_sda_list [i ].i2c_index )) {
73
73
//keep looking if the I2C is taken, could be another SCL that works
74
- if (reserved_i2c [mcu_i2c_scl_list [i ].i2c_index - 1 ]) {
74
+ if (reserved_i2c [mcu_i2c_scl_list [i ].i2c_index - 1 ]) {
75
75
i2c_taken = true;
76
76
continue ;
77
77
}
@@ -84,8 +84,8 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
84
84
}
85
85
86
86
//handle typedef selection, errors
87
- if (self -> sda != NULL && self -> scl != NULL ) {
88
- I2Cx = mcu_i2c_banks [self -> sda -> i2c_index - 1 ];
87
+ if (self -> sda != NULL && self -> scl != NULL ) {
88
+ I2Cx = mcu_i2c_banks [self -> sda -> i2c_index - 1 ];
89
89
} else {
90
90
if (i2c_taken ) {
91
91
mp_raise_ValueError (translate ("Hardware busy, try alternative pins" ));
@@ -111,7 +111,7 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
111
111
HAL_GPIO_Init (pin_port (scl -> port ), & GPIO_InitStruct );
112
112
113
113
//Note: due to I2C soft reboot issue, do not relocate clock init.
114
- i2c_clock_enable (1 << (self -> sda -> i2c_index - 1 ));
114
+ i2c_clock_enable (1 << (self -> sda -> i2c_index - 1 ));
115
115
reserved_i2c [self -> sda -> i2c_index - 1 ] = true;
116
116
117
117
self -> handle .Instance = I2Cx ;
@@ -124,15 +124,15 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
124
124
self -> handle .Init .GeneralCallMode = I2C_GENERALCALL_DISABLE ;
125
125
self -> handle .Init .NoStretchMode = I2C_NOSTRETCH_DISABLE ;
126
126
self -> handle .State = HAL_I2C_STATE_RESET ;
127
- if (HAL_I2C_Init (& (self -> handle )) != HAL_OK ) {
127
+ if (HAL_I2C_Init (& (self -> handle )) != HAL_OK ) {
128
128
mp_raise_RuntimeError (translate ("I2C Init Error" ));
129
129
}
130
130
claim_pin (sda );
131
131
claim_pin (scl );
132
132
}
133
133
134
134
void common_hal_busio_i2c_never_reset (busio_i2c_obj_t * self ) {
135
- for (size_t i = 0 ; i < MP_ARRAY_SIZE (mcu_i2c_banks ); i ++ ) {
135
+ for (size_t i = 0 ; i < MP_ARRAY_SIZE (mcu_i2c_banks ); i ++ ) {
136
136
if (self -> handle .Instance == mcu_i2c_banks [i ]) {
137
137
never_reset_i2c [i ] = true;
138
138
@@ -152,7 +152,7 @@ void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
152
152
return ;
153
153
}
154
154
155
- i2c_clock_disable (1 << (self -> sda -> i2c_index - 1 ));
155
+ i2c_clock_disable (1 << (self -> sda -> i2c_index - 1 ));
156
156
reserved_i2c [self -> sda -> i2c_index - 1 ] = false;
157
157
never_reset_i2c [self -> sda -> i2c_index - 1 ] = false;
158
158
@@ -163,7 +163,7 @@ void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
163
163
}
164
164
165
165
bool common_hal_busio_i2c_probe (busio_i2c_obj_t * self , uint8_t addr ) {
166
- return HAL_I2C_IsDeviceReady (& (self -> handle ), (uint16_t )(addr << 1 ),2 , 2 ) == HAL_OK ;
166
+ return HAL_I2C_IsDeviceReady (& (self -> handle ), (uint16_t )(addr << 1 ), 2 , 2 ) == HAL_OK ;
167
167
}
168
168
169
169
bool common_hal_busio_i2c_try_lock (busio_i2c_obj_t * self ) {
@@ -195,13 +195,15 @@ void common_hal_busio_i2c_unlock(busio_i2c_obj_t *self) {
195
195
196
196
uint8_t common_hal_busio_i2c_write (busio_i2c_obj_t * self , uint16_t addr ,
197
197
const uint8_t * data , size_t len , bool transmit_stop_bit ) {
198
- HAL_StatusTypeDef result = HAL_I2C_Master_Transmit (& (self -> handle ), (uint16_t )(addr <<1 ), (uint8_t * )data , (uint16_t )len , 500 );
198
+ HAL_StatusTypeDef result = HAL_I2C_Master_Transmit (& (self -> handle ), (uint16_t )(addr << 1 ),
199
+ (uint8_t * )data , (uint16_t )len , 500 );
199
200
return result == HAL_OK ? 0 : MP_EIO ;
200
201
}
201
202
202
203
uint8_t common_hal_busio_i2c_read (busio_i2c_obj_t * self , uint16_t addr ,
203
204
uint8_t * data , size_t len ) {
204
- return HAL_I2C_Master_Receive (& (self -> handle ), (uint16_t )(addr <<1 ), data , (uint16_t )len , 500 ) == HAL_OK ? 0 : MP_EIO ;
205
+ return HAL_I2C_Master_Receive (& (self -> handle ), (uint16_t )(addr <<1 ), data , (uint16_t )len , 500 )
206
+ == HAL_OK ? 0 : MP_EIO ;
205
207
}
206
208
207
209
STATIC void i2c_clock_enable (uint8_t mask ) {
@@ -231,12 +233,18 @@ STATIC void i2c_clock_enable(uint8_t mask) {
231
233
232
234
STATIC void i2c_clock_disable (uint8_t mask ) {
233
235
#ifdef I2C1
234
- if (mask & 1 <<0 ) __HAL_RCC_I2C1_CLK_DISABLE ();
236
+ if (mask & 1 <<0 ) {
237
+ __HAL_RCC_I2C1_CLK_DISABLE ();
238
+ }
235
239
#endif
236
240
#ifdef I2C2
237
- if (mask & 1 <<1 ) __HAL_RCC_I2C2_CLK_DISABLE ();
241
+ if (mask & 1 <<1 ) {
242
+ __HAL_RCC_I2C2_CLK_DISABLE ();
243
+ }
238
244
#endif
239
245
#ifdef I2C3
240
- if (mask & 1 <<2 ) __HAL_RCC_I2C3_CLK_DISABLE ();
246
+ if (mask & 1 <<2 ) {
247
+ __HAL_RCC_I2C3_CLK_DISABLE ();
248
+ }
241
249
#endif
242
250
}
0 commit comments