summaryrefslogtreecommitdiffstatshomepage
path: root/extmod/machine_i2c.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2016-11-23 16:34:25 +1100
committerDamien George <damien.p.george@gmail.com>2016-11-23 17:05:38 +1100
commit07e83573c8f69c673a8bea78d6a2f242152da00c (patch)
treee9767170b4eeaed8e837e9daa8c44e24043278fa /extmod/machine_i2c.c
parentced240e72a933eec0bfabe236d96f383cb64241c (diff)
downloadmicropython-07e83573c8f69c673a8bea78d6a2f242152da00c.tar.gz
micropython-07e83573c8f69c673a8bea78d6a2f242152da00c.zip
extmod/machine_i2c: Add 'nack' argument to i2c.readinto.
Diffstat (limited to 'extmod/machine_i2c.c')
-rw-r--r--extmod/machine_i2c.c17
1 files changed, 10 insertions, 7 deletions
diff --git a/extmod/machine_i2c.c b/extmod/machine_i2c.c
index 85294dc985..906bea5720 100644
--- a/extmod/machine_i2c.c
+++ b/extmod/machine_i2c.c
@@ -346,8 +346,8 @@ STATIC mp_obj_t machine_i2c_stop(mp_obj_t self_in) {
}
MP_DEFINE_CONST_FUN_OBJ_1(machine_i2c_stop_obj, machine_i2c_stop);
-STATIC mp_obj_t machine_i2c_readinto(mp_obj_t self_in, mp_obj_t buf_in) {
- mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(self_in);
+STATIC mp_obj_t machine_i2c_readinto(size_t n_args, const mp_obj_t *args) {
+ mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(args[0]);
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t*)self->type->protocol;
if (i2c_p->read == NULL) {
mp_raise_msg(&mp_type_OSError, "I2C operation not supported");
@@ -355,17 +355,20 @@ STATIC mp_obj_t machine_i2c_readinto(mp_obj_t self_in, mp_obj_t buf_in) {
// get the buffer to read into
mp_buffer_info_t bufinfo;
- mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_WRITE);
+ mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
+
+ // work out if we want to send a nack at the end
+ bool nack = (n_args == 2) ? true : mp_obj_is_true(args[2]);
// do the read
- int ret = i2c_p->read(self, bufinfo.buf, bufinfo.len);
+ int ret = i2c_p->read(self, bufinfo.buf, bufinfo.len, nack);
if (ret != 0) {
mp_raise_OSError(-ret);
}
return mp_const_none;
}
-MP_DEFINE_CONST_FUN_OBJ_2(machine_i2c_readinto_obj, machine_i2c_readinto);
+MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_i2c_readinto_obj, 2, 3, machine_i2c_readinto);
STATIC mp_obj_t machine_i2c_write(mp_obj_t self_in, mp_obj_t buf_in) {
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) {
return mp_hal_i2c_stop(self);
}
-int mp_machine_soft_i2c_read(mp_obj_base_t *self_in, uint8_t *dest, size_t len) {
+int mp_machine_soft_i2c_read(mp_obj_base_t *self_in, uint8_t *dest, size_t len, bool nack) {
machine_i2c_obj_t *self = (machine_i2c_obj_t*)self_in;
while (len--) {
- int ret = mp_hal_i2c_read_byte(self, dest++, len == 0);
+ int ret = mp_hal_i2c_read_byte(self, dest++, nack && (len == 0));
if (ret != 0) {
return ret;
}