Skip to content

Commit 37333cb

Browse files
committed
extmod/machine_i2c: Add 'stop' argument to i2c readfrom/writeto meths.
1 parent 0bc99b4 commit 37333cb

1 file changed

Lines changed: 21 additions & 15 deletions

File tree

extmod/machine_i2c.c

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -392,45 +392,51 @@ STATIC mp_obj_t machine_i2c_write(mp_obj_t self_in, mp_obj_t buf_in) {
392392
}
393393
MP_DEFINE_CONST_FUN_OBJ_2(machine_i2c_write_obj, machine_i2c_write);
394394

395-
STATIC mp_obj_t machine_i2c_readfrom(mp_obj_t self_in, mp_obj_t addr_in, mp_obj_t nbytes_in) {
396-
mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(self_in);
395+
STATIC mp_obj_t machine_i2c_readfrom(size_t n_args, const mp_obj_t *args) {
396+
mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(args[0]);
397397
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t*)self->type->protocol;
398+
mp_int_t addr = mp_obj_get_int(args[1]);
398399
vstr_t vstr;
399-
vstr_init_len(&vstr, mp_obj_get_int(nbytes_in));
400-
int ret = i2c_p->readfrom(self, mp_obj_get_int(addr_in), (uint8_t*)vstr.buf, vstr.len, true);
400+
vstr_init_len(&vstr, mp_obj_get_int(args[2]));
401+
bool stop = (n_args == 3) ? true : mp_obj_is_true(args[3]);
402+
int ret = i2c_p->readfrom(self, addr, (uint8_t*)vstr.buf, vstr.len, stop);
401403
if (ret < 0) {
402404
mp_raise_OSError(-ret);
403405
}
404406
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
405407
}
406-
MP_DEFINE_CONST_FUN_OBJ_3(machine_i2c_readfrom_obj, machine_i2c_readfrom);
408+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_i2c_readfrom_obj, 3, 4, machine_i2c_readfrom);
407409

408-
STATIC mp_obj_t machine_i2c_readfrom_into(mp_obj_t self_in, mp_obj_t addr_in, mp_obj_t buf_in) {
409-
mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(self_in);
410+
STATIC mp_obj_t machine_i2c_readfrom_into(size_t n_args, const mp_obj_t *args) {
411+
mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(args[0]);
410412
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t*)self->type->protocol;
413+
mp_int_t addr = mp_obj_get_int(args[1]);
411414
mp_buffer_info_t bufinfo;
412-
mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_WRITE);
413-
int ret = i2c_p->readfrom(self, mp_obj_get_int(addr_in), bufinfo.buf, bufinfo.len, true);
415+
mp_get_buffer_raise(args[2], &bufinfo, MP_BUFFER_WRITE);
416+
bool stop = (n_args == 3) ? true : mp_obj_is_true(args[3]);
417+
int ret = i2c_p->readfrom(self, addr, bufinfo.buf, bufinfo.len, stop);
414418
if (ret < 0) {
415419
mp_raise_OSError(-ret);
416420
}
417421
return mp_const_none;
418422
}
419-
MP_DEFINE_CONST_FUN_OBJ_3(machine_i2c_readfrom_into_obj, machine_i2c_readfrom_into);
423+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_i2c_readfrom_into_obj, 3, 4, machine_i2c_readfrom_into);
420424

421-
STATIC mp_obj_t machine_i2c_writeto(mp_obj_t self_in, mp_obj_t addr_in, mp_obj_t buf_in) {
422-
mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(self_in);
425+
STATIC mp_obj_t machine_i2c_writeto(size_t n_args, const mp_obj_t *args) {
426+
mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(args[0]);
423427
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t*)self->type->protocol;
428+
mp_int_t addr = mp_obj_get_int(args[1]);
424429
mp_buffer_info_t bufinfo;
425-
mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ);
426-
int ret = i2c_p->writeto(self, mp_obj_get_int(addr_in), bufinfo.buf, bufinfo.len, true);
430+
mp_get_buffer_raise(args[2], &bufinfo, MP_BUFFER_READ);
431+
bool stop = (n_args == 3) ? true : mp_obj_is_true(args[3]);
432+
int ret = i2c_p->writeto(self, addr, bufinfo.buf, bufinfo.len, stop);
427433
if (ret < 0) {
428434
mp_raise_OSError(-ret);
429435
}
430436
// return number of acks received
431437
return MP_OBJ_NEW_SMALL_INT(ret);
432438
}
433-
STATIC MP_DEFINE_CONST_FUN_OBJ_3(machine_i2c_writeto_obj, machine_i2c_writeto);
439+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_i2c_writeto_obj, 3, 4, machine_i2c_writeto);
434440

435441
STATIC int read_mem(mp_obj_t self_in, uint16_t addr, uint32_t memaddr, uint8_t addrsize, uint8_t *buf, size_t len) {
436442
mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(self_in);

0 commit comments

Comments
 (0)