11/*
2- * This file is part of the MicroPython project, http://micropython.org/
2+ * This file is part of the MicroPython project, http://micropython.org/
33 *
44 * The MIT License (MIT)
55 *
@@ -62,10 +62,7 @@ typedef struct _machine_hw_i2c_obj_t {
6262static machine_hw_i2c_obj_t machine_hw_i2c_obj [I2C_NUM_MAX ];
6363
6464// ---------------- Initialization ----------------
65- static void machine_hw_i2c_init (machine_hw_i2c_obj_t * self ,
66- uint32_t freq ,
67- uint32_t timeout_us ,
68- bool first_init ) {
65+ static void machine_hw_i2c_init (machine_hw_i2c_obj_t * self , uint32_t freq , uint32_t timeout_us , bool first_init ) {
6966
7067 // 1. If already initialized, uninstall the old driver first
7168 if (!first_init && self -> bus_handle ) {
@@ -95,12 +92,7 @@ static void machine_hw_i2c_init(machine_hw_i2c_obj_t *self,
9592 ESP_ERROR_CHECK (i2c_master_bus_add_device (self -> bus_handle , & dev_cfg , & self -> dev_handle ));
9693}
9794
98- int machine_hw_i2c_transfer (mp_obj_base_t * self_in ,
99- uint16_t addr ,
100- size_t n ,
101- mp_machine_i2c_buf_t * bufs ,
102- unsigned int flags )
103- {
95+ int machine_hw_i2c_transfer (mp_obj_base_t * self_in , uint16_t addr , size_t n , mp_machine_i2c_buf_t * bufs , unsigned int flags ) {
10496 machine_hw_i2c_obj_t * self = MP_OBJ_TO_PTR (self_in );
10597
10698 /* 0. Probe the address to see if any device responds */
@@ -125,10 +117,7 @@ int machine_hw_i2c_transfer(mp_obj_base_t *self_in,
125117 /* 2. If WRITE1 segment exists, perform the write first */
126118 if (flags & MP_MACHINE_I2C_FLAG_WRITE1 ) {
127119 if (bufs -> len ) {
128- err = i2c_master_transmit (dev_handle ,
129- bufs -> buf ,
130- bufs -> len ,
131- 1000 ); /* Block with 1 s timeout */
120+ err = i2c_master_transmit (dev_handle ,bufs -> buf ,bufs -> len ,1000 ); /* Block with 1 s timeout */
132121 if (err != ESP_OK ) goto cleanup ;
133122 }
134123 data_len += bufs -> len ;
@@ -139,11 +128,7 @@ int machine_hw_i2c_transfer(mp_obj_base_t *self_in,
139128 /* 3. Main loop: remaining segments */
140129 for (; n -- ; ++ bufs ) {
141130 if (bufs -> len == 0 ) continue ;
142- err = i2c_master_receive (dev_handle ,
143- bufs -> buf ,
144- //bufs->len,
145- 1 ,
146- 1000 );
131+ err = i2c_master_receive (dev_handle ,bufs -> buf ,bufs -> len ,1000 );
147132 if (err != ESP_OK ) break ;
148133
149134 data_len += bufs -> len ;
@@ -195,22 +180,20 @@ int machine_hw_i2c_transfer(mp_obj_base_t *self_in,
195180}
196181
197182// ---------------- Print ----------------
198- static void machine_hw_i2c_print (const mp_print_t * print ,
199- mp_obj_t self_in ,
200- mp_print_kind_t kind ) {
183+ static void machine_hw_i2c_print (const mp_print_t * print , mp_obj_t self_in , mp_print_kind_t kind ) {
201184 machine_hw_i2c_obj_t * self = MP_OBJ_TO_PTR (self_in );
202185 mp_printf (print , "I2C(%u, scl=%u, sda=%u)" ,
203186 self -> port , self -> scl , self -> sda );
204187}
205188
206189// ---------------- Constructor ----------------
207- mp_obj_t machine_hw_i2c_make_new (const mp_obj_type_t * type ,
208- size_t n_args , size_t n_kw ,
209- const mp_obj_t * all_args ) {
190+ mp_obj_t machine_hw_i2c_make_new (const mp_obj_type_t * type , size_t n_args , size_t n_kw , const mp_obj_t * all_args ) {
210191 // Create a SoftI2C instance if no id is specified (or is -1) but other arguments are given
211192 if (n_args != 0 ) {
212193 MP_MACHINE_I2C_CHECK_FOR_LEGACY_SOFTI2C_CONSTRUCTION (n_args , n_kw , all_args );
213194 }
195+
196+ // Parse args
214197 enum { ARG_id , ARG_scl , ARG_sda , ARG_freq , ARG_timeout };
215198 static const mp_arg_t allowed_args [] = {
216199 { MP_QSTR_id , MP_ARG_INT , {.u_int = I2C_NUM_0 } },
@@ -220,13 +203,11 @@ mp_obj_t machine_hw_i2c_make_new(const mp_obj_type_t *type,
220203 { MP_QSTR_timeout , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = I2C_DEFAULT_TIMEOUT_US } },
221204 };
222205 mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
223- mp_arg_parse_all_kw_array (n_args , n_kw , all_args ,
224- MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
206+ mp_arg_parse_all_kw_array (n_args , n_kw , all_args ,MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
225207
226208 mp_int_t i2c_id = args [ARG_id ].u_int ;
227209 if (!(I2C_NUM_0 <= i2c_id && i2c_id < I2C_NUM_MAX )) {
228- mp_raise_msg_varg (& mp_type_ValueError ,
229- MP_ERROR_TEXT ("I2C(%d) doesn't exist" ), i2c_id );
210+ mp_raise_msg_varg (& mp_type_ValueError ,MP_ERROR_TEXT ("I2C(%d) doesn't exist" ), i2c_id );
230211 }
231212
232213 machine_hw_i2c_obj_t * self = & machine_hw_i2c_obj [i2c_id ];
@@ -246,10 +227,7 @@ mp_obj_t machine_hw_i2c_make_new(const mp_obj_type_t *type,
246227 self -> sda = machine_pin_get_id (args [ARG_sda ].u_obj );
247228 }
248229
249- machine_hw_i2c_init (self ,
250- args [ARG_freq ].u_int ,
251- args [ARG_timeout ].u_int ,
252- first_init );
230+ machine_hw_i2c_init (self ,args [ARG_freq ].u_int ,args [ARG_timeout ].u_int ,first_init );
253231 return MP_OBJ_FROM_PTR (self );
254232}
255233
@@ -267,6 +245,6 @@ MP_DEFINE_CONST_OBJ_TYPE(
267245 print , machine_hw_i2c_print ,
268246 protocol , & machine_hw_i2c_p ,
269247 locals_dict , & mp_machine_i2c_locals_dict
270- );
248+ );
271249
272250#endif // MICROPY_PY_MACHINE_I2C || MICROPY_PY_MACHINE_SOFTI2C
0 commit comments