@@ -54,9 +54,9 @@ typedef struct _machine_hw_i2c_obj_t {
5454 mp_obj_base_t base ;
5555 i2c_master_bus_handle_t bus_handle ;
5656 i2c_master_dev_handle_t dev_handle ;
57- uint8_t port ;
58- gpio_num_t scl ;
59- gpio_num_t sda ;
57+ uint8_t port : 8 ;
58+ gpio_num_t scl : 8 ;
59+ gpio_num_t sda : 8 ;
6060} machine_hw_i2c_obj_t ;
6161
6262static machine_hw_i2c_obj_t machine_hw_i2c_obj [I2C_NUM_MAX ];
@@ -103,8 +103,8 @@ int machine_hw_i2c_transfer(mp_obj_base_t *self_in, uint16_t addr, size_t n, mp_
103103 /* 1. Create a temporary device handle for this transaction */
104104 i2c_device_config_t dev_cfg = {
105105 .dev_addr_length = I2C_ADDR_BIT_LEN_7 ,
106- .device_address = addr ,
107- .scl_speed_hz = 100000 , /* Use bus frequency */
106+ .device_address = addr ,
107+ .scl_speed_hz = 100000 , /* Use bus frequency */
108108 };
109109 i2c_master_dev_handle_t dev_handle ;
110110 err = i2c_master_bus_add_device (self -> bus_handle , & dev_cfg , & dev_handle );
@@ -117,8 +117,10 @@ int machine_hw_i2c_transfer(mp_obj_base_t *self_in, uint16_t addr, size_t n, mp_
117117 /* 2. If WRITE1 segment exists, perform the write first */
118118 if (flags & MP_MACHINE_I2C_FLAG_WRITE1 ) {
119119 if (bufs -> len ) {
120- err = i2c_master_transmit (dev_handle ,bufs -> buf ,bufs -> len ,1000 ); /* Block with 1 s timeout */
121- if (err != ESP_OK ) goto cleanup ;
120+ err = i2c_master_transmit (dev_handle , bufs -> buf , bufs -> len , 1000 ); /* Block with 1 s timeout */
121+ if (err != ESP_OK ) {
122+ goto cleanup ;
123+ }
122124 }
123125 data_len += bufs -> len ;
124126 -- n ;
@@ -127,9 +129,13 @@ int machine_hw_i2c_transfer(mp_obj_base_t *self_in, uint16_t addr, size_t n, mp_
127129 if (flags & MP_MACHINE_I2C_FLAG_READ ) {
128130 /* 3. Main loop: remaining segments */
129131 for (; n -- ; ++ bufs ) {
130- if (bufs -> len == 0 ) continue ;
131- err = i2c_master_receive (dev_handle ,bufs -> buf ,bufs -> len ,1000 );
132- if (err != ESP_OK ) break ;
132+ if (bufs -> len == 0 ) {
133+ continue ;
134+ }
135+ err = i2c_master_receive (dev_handle , bufs -> buf , bufs -> len , 1000 );
136+ if (err != ESP_OK ) {
137+ break ;
138+ }
133139
134140 data_len += bufs -> len ;
135141 }
@@ -150,7 +156,9 @@ int machine_hw_i2c_transfer(mp_obj_base_t *self_in, uint16_t addr, size_t n, mp_
150156 n = yuann ;
151157 // Dynamically allocate write_buf
152158 uint8_t * write_buf = (uint8_t * )malloc (total_len );
153- if (write_buf == NULL ) return - MP_ENOMEM ;
159+ if (write_buf == NULL ) {
160+ return - MP_ENOMEM ;
161+ }
154162
155163 // Copy data into write_buf
156164 size_t index = 0 ;
@@ -161,7 +169,9 @@ int machine_hw_i2c_transfer(mp_obj_base_t *self_in, uint16_t addr, size_t n, mp_
161169
162170 // Transmit data
163171 err = i2c_master_transmit (dev_handle , write_buf , total_len , 1000 );
164- if (err != ESP_OK ) goto cleanup ;
172+ if (err != ESP_OK ) {
173+ goto cleanup ;
174+ }
165175
166176 // Free dynamically allocated memory
167177 free (write_buf );
@@ -172,18 +182,23 @@ int machine_hw_i2c_transfer(mp_obj_base_t *self_in, uint16_t addr, size_t n, mp_
172182 i2c_master_bus_rm_device (dev_handle );
173183
174184 /* 5. Map errors */
175- if (err == ESP_FAIL ) return - MP_ENODEV ;
176- if (err == ESP_ERR_TIMEOUT ) return - MP_ETIMEDOUT ;
177- if (err != ESP_OK ) return - abs (err );
185+ if (err == ESP_FAIL ) {
186+ return - MP_ENODEV ;
187+ }
188+ if (err == ESP_ERR_TIMEOUT ) {
189+ return - MP_ETIMEDOUT ;
190+ }
191+ if (err != ESP_OK ) {
192+ return - abs (err );
193+ }
178194
179195 return data_len ;
180196}
181197
182198// ---------------- Print ----------------
183199static void machine_hw_i2c_print (const mp_print_t * print , mp_obj_t self_in , mp_print_kind_t kind ) {
184200 machine_hw_i2c_obj_t * self = MP_OBJ_TO_PTR (self_in );
185- mp_printf (print , "I2C(%u, scl=%u, sda=%u)" ,
186- self -> port , self -> scl , self -> sda );
201+ mp_printf (print , "I2C(%u, scl=%u, sda=%u)" , self -> port , self -> scl , self -> sda );
187202}
188203
189204// ---------------- Constructor ----------------
@@ -203,11 +218,11 @@ mp_obj_t machine_hw_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_
203218 { MP_QSTR_timeout , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = I2C_DEFAULT_TIMEOUT_US } },
204219 };
205220 mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
206- mp_arg_parse_all_kw_array (n_args , n_kw , all_args ,MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
221+ mp_arg_parse_all_kw_array (n_args , n_kw , all_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
207222
208223 mp_int_t i2c_id = args [ARG_id ].u_int ;
209224 if (!(I2C_NUM_0 <= i2c_id && i2c_id < I2C_NUM_MAX )) {
210- mp_raise_msg_varg (& mp_type_ValueError ,MP_ERROR_TEXT ("I2C(%d) doesn't exist" ), i2c_id );
225+ mp_raise_msg_varg (& mp_type_ValueError , MP_ERROR_TEXT ("I2C(%d) doesn't exist" ), i2c_id );
211226 }
212227
213228 machine_hw_i2c_obj_t * self = & machine_hw_i2c_obj [i2c_id ];
@@ -227,7 +242,7 @@ mp_obj_t machine_hw_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_
227242 self -> sda = machine_pin_get_id (args [ARG_sda ].u_obj );
228243 }
229244
230- machine_hw_i2c_init (self ,args [ARG_freq ].u_int ,args [ARG_timeout ].u_int ,first_init );
245+ machine_hw_i2c_init (self , args [ARG_freq ].u_int , args [ARG_timeout ].u_int , first_init );
231246 return MP_OBJ_FROM_PTR (self );
232247}
233248
0 commit comments