Skip to content

Commit 07e8357

Browse files
committed
extmod/machine_i2c: Add 'nack' argument to i2c.readinto.
1 parent ced240e commit 07e8357

2 files changed

Lines changed: 11 additions & 8 deletions

File tree

extmod/machine_i2c.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -346,26 +346,29 @@ STATIC mp_obj_t machine_i2c_stop(mp_obj_t self_in) {
346346
}
347347
MP_DEFINE_CONST_FUN_OBJ_1(machine_i2c_stop_obj, machine_i2c_stop);
348348

349-
STATIC mp_obj_t machine_i2c_readinto(mp_obj_t self_in, mp_obj_t buf_in) {
350-
mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(self_in);
349+
STATIC mp_obj_t machine_i2c_readinto(size_t n_args, const mp_obj_t *args) {
350+
mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(args[0]);
351351
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t*)self->type->protocol;
352352
if (i2c_p->read == NULL) {
353353
mp_raise_msg(&mp_type_OSError, "I2C operation not supported");
354354
}
355355

356356
// get the buffer to read into
357357
mp_buffer_info_t bufinfo;
358-
mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_WRITE);
358+
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
359+
360+
// work out if we want to send a nack at the end
361+
bool nack = (n_args == 2) ? true : mp_obj_is_true(args[2]);
359362

360363
// do the read
361-
int ret = i2c_p->read(self, bufinfo.buf, bufinfo.len);
364+
int ret = i2c_p->read(self, bufinfo.buf, bufinfo.len, nack);
362365
if (ret != 0) {
363366
mp_raise_OSError(-ret);
364367
}
365368

366369
return mp_const_none;
367370
}
368-
MP_DEFINE_CONST_FUN_OBJ_2(machine_i2c_readinto_obj, machine_i2c_readinto);
371+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_i2c_readinto_obj, 2, 3, machine_i2c_readinto);
369372

370373
STATIC mp_obj_t machine_i2c_write(mp_obj_t self_in, mp_obj_t buf_in) {
371374
mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(self_in);
@@ -579,10 +582,10 @@ int mp_machine_soft_i2c_stop(mp_obj_base_t *self_in) {
579582
return mp_hal_i2c_stop(self);
580583
}
581584

582-
int mp_machine_soft_i2c_read(mp_obj_base_t *self_in, uint8_t *dest, size_t len) {
585+
int mp_machine_soft_i2c_read(mp_obj_base_t *self_in, uint8_t *dest, size_t len, bool nack) {
583586
machine_i2c_obj_t *self = (machine_i2c_obj_t*)self_in;
584587
while (len--) {
585-
int ret = mp_hal_i2c_read_byte(self, dest++, len == 0);
588+
int ret = mp_hal_i2c_read_byte(self, dest++, nack && (len == 0));
586589
if (ret != 0) {
587590
return ret;
588591
}

extmod/machine_i2c.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
typedef struct _mp_machine_i2c_p_t {
3535
int (*start)(mp_obj_base_t *obj);
3636
int (*stop)(mp_obj_base_t *obj);
37-
int (*read)(mp_obj_base_t *obj, uint8_t *dest, size_t len);
37+
int (*read)(mp_obj_base_t *obj, uint8_t *dest, size_t len, bool nack);
3838
int (*write)(mp_obj_base_t *obj, const uint8_t *src, size_t len);
3939
int (*readfrom)(mp_obj_base_t *obj, uint16_t addr, uint8_t *dest, size_t len, bool stop);
4040
int (*writeto)(mp_obj_base_t *obj, uint16_t addr, const uint8_t *src, size_t len, bool stop);

0 commit comments

Comments
 (0)