Skip to content

Commit f738996

Browse files
dhalberttannewt
authored andcommitted
Allow writing buffer of length zero to I2C device; it can be used to poll for existence.
1 parent bd7abcd commit f738996

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

shared-bindings/busio/I2C.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(busio_i2c_unlock_obj, busio_i2c_obj_unlock);
171171
//|
172172
//| Read into ``buffer`` from the slave specified by ``address``.
173173
//| The number of bytes read will be the length of ``buffer``.
174+
//| At least one byte must be read.
174175
//|
175176
//| If ``start`` or ``end`` is provided, then the buffer will be sliced
176177
//| as if ``buffer[start:end]``. This will not cause an allocation like
@@ -223,6 +224,9 @@ MP_DEFINE_CONST_FUN_OBJ_KW(busio_i2c_readfrom_into_obj, 3, busio_i2c_readfrom_in
223224
//| as if ``buffer[start:end]``. This will not cause an allocation like
224225
//| ``buffer[start:end]`` will so it saves memory.
225226
//|
227+
//| Writing a buffer or slice of length zero is permitted, as it can be used
228+
//| to poll for the existence of a device.
229+
//|
226230
//| :param int address: 7-bit device address
227231
//| :param bytearray buffer: buffer containing the bytes to write
228232
//| :param int start: Index to start writing from
@@ -254,10 +258,6 @@ STATIC mp_obj_t busio_i2c_writeto(size_t n_args, const mp_obj_t *pos_args, mp_ma
254258
uint32_t length = bufinfo.len;
255259
normalize_buffer_bounds(&start, args[ARG_end].u_int, &length);
256260

257-
if (length == 0) {
258-
mp_raise_ValueError("Buffer must be at least length 1");
259-
}
260-
261261
// do the transfer
262262
uint8_t status = common_hal_busio_i2c_write(self, args[ARG_address].u_int,
263263
((uint8_t*) bufinfo.buf) + start, length, args[ARG_stop].u_bool);

shared-bindings/busio/SPI.c

+2
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(busio_spi_unlock_obj, busio_spi_obj_unlock);
207207
//| .. method:: SPI.write(buffer, \*, start=0, end=len(buffer))
208208
//|
209209
//| Write the data contained in ``buf``. Requires the SPI being locked.
210+
//| At least one byte must be written.
210211
//|
211212
//| :param bytearray buffer: buffer containing the bytes to write
212213
//| :param int start: Index to start writing from
@@ -247,6 +248,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(busio_spi_write_obj, 2, busio_spi_write);
247248
//| .. method:: SPI.readinto(buffer, \*, start=0, end=len(buffer), write_value=0)
248249
//|
249250
//| Read into the buffer specified by ``buf`` while writing zeroes. Requires the SPI being locked.
251+
//| At least one byte must be read.
250252
//|
251253
//| :param bytearray buffer: buffer to write into
252254
//| :param int start: Index to start writing at

0 commit comments

Comments
 (0)