Skip to content

Commit 51e2e83

Browse files
committed
i2cslave: Support uselect.poll
This adds MP_STREAM_POLL support to i2cslave.
1 parent fbf3cf0 commit 51e2e83

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

ports/atmel-samd/common-hal/i2cslave/I2CSlave.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,22 @@ int common_hal_i2cslave_i2c_slave_is_addressed(i2cslave_i2c_slave_obj_t *self, u
143143

144144
self->writing = false;
145145

146-
*address = self->sercom->I2CS.DATA.reg >> 1;
147-
*is_read = self->sercom->I2CS.STATUS.bit.DIR;
148-
*is_restart = self->sercom->I2CS.STATUS.bit.SR;
146+
uint8_t addr = self->sercom->I2CS.DATA.reg >> 1;
147+
if (address) {
148+
*address = addr;
149+
}
150+
if (is_read) {
151+
*is_read = self->sercom->I2CS.STATUS.bit.DIR;
152+
}
153+
if (is_restart) {
154+
*is_restart = self->sercom->I2CS.STATUS.bit.SR;
155+
}
149156

150157
for (unsigned int i = 0; i < self->num_addresses; i++) {
151-
if (*address == self->addresses[i]) {
152-
common_hal_i2cslave_i2c_slave_ack(self, true);
158+
if (addr == self->addresses[i]) {
159+
if (address) {
160+
common_hal_i2cslave_i2c_slave_ack(self, true);
161+
}
153162
return 1;
154163
}
155164
}

shared-bindings/i2cslave/I2CSlave.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "py/obj.h"
3939
#include "py/objproperty.h"
4040
#include "py/runtime.h"
41+
#include "py/stream.h"
4142

4243
STATIC mp_obj_t mp_obj_new_i2cslave_i2c_slave_request(i2cslave_i2c_slave_obj_t *slave, uint8_t address, bool is_read, bool is_restart) {
4344
i2cslave_i2c_slave_request_obj_t *self = m_new_obj(i2cslave_i2c_slave_request_obj_t);
@@ -226,10 +227,28 @@ STATIC const mp_rom_map_elem_t i2cslave_i2c_slave_locals_dict_table[] = {
226227

227228
STATIC MP_DEFINE_CONST_DICT(i2cslave_i2c_slave_locals_dict, i2cslave_i2c_slave_locals_dict_table);
228229

230+
STATIC mp_uint_t i2cslave_ioctl(mp_obj_t obj, mp_uint_t request, uintptr_t arg, int *errcode) {
231+
i2cslave_i2c_slave_obj_t *self = MP_OBJ_TO_PTR(obj);
232+
int status = -MP_EINVAL;
233+
if (request == MP_STREAM_POLL) {
234+
status = common_hal_i2cslave_i2c_slave_is_addressed(self, NULL, NULL, NULL);
235+
}
236+
if (status < 0) {
237+
*errcode = -status;
238+
return MP_STREAM_ERROR;
239+
}
240+
return status;
241+
}
242+
243+
STATIC const mp_stream_p_t i2cslave_p = {
244+
.ioctl = i2cslave_ioctl,
245+
};
246+
229247
const mp_obj_type_t i2cslave_i2c_slave_type = {
230248
{ &mp_type_type },
231249
.name = MP_QSTR_I2CSlave,
232250
.make_new = i2cslave_i2c_slave_make_new,
251+
.protocol = &i2cslave_p,
233252
.locals_dict = (mp_obj_dict_t*)&i2cslave_i2c_slave_locals_dict,
234253
};
235254

0 commit comments

Comments
 (0)