Skip to content

Commit a70b679

Browse files
committed
bitbangio.SPI.read: Support write_value, fix some other nits
1 parent 2bc61b4 commit a70b679

File tree

3 files changed

+42
-16
lines changed

3 files changed

+42
-16
lines changed

shared-bindings/bitbangio/SPI.c

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -224,32 +224,53 @@ STATIC mp_obj_t bitbangio_spi_write(mp_obj_t self_in, mp_obj_t wr_buf) {
224224
MP_DEFINE_CONST_FUN_OBJ_2(bitbangio_spi_write_obj, bitbangio_spi_write);
225225

226226

227-
//| def readinto(self, buf: WriteableBuffer) -> None:
228-
//| """Read into the buffer specified by ``buf`` while writing zeroes.
229-
//| Requires the SPI being locked.
230-
//| If the number of bytes to read is 0, nothing happens."""
227+
//| def readinto(self, buffer: WriteableBuffer, *, start: int = 0, end: Optional[int] = None, write_value: int = 0) -> None:
228+
//| """Read into ``buffer`` while writing ``write_value`` for each byte read.
229+
//| The SPI object must be locked.
230+
//| If the number of bytes to read is 0, nothing happens.
231+
//|
232+
//| :param bytearray buffer: Read data into this buffer
233+
//| :param int start: Start of the slice of ``buffer`` to read into: ``buffer[start:end]``
234+
//| :param int end: End of the slice; this index is not included. Defaults to ``len(buffer)``
235+
//| :param int write_value: Value to write while reading."""
231236
//| ...
232237
//|
233-
// TODO(tannewt): Add support for start and end kwargs.
234-
STATIC mp_obj_t bitbangio_spi_readinto(size_t n_args, const mp_obj_t *args) {
235-
bitbangio_spi_obj_t *self = MP_OBJ_TO_PTR(args[0]);
238+
239+
STATIC mp_obj_t bitbangio_spi_readinto(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
240+
enum { ARG_buffer, ARG_start, ARG_end, ARG_write_value };
241+
static const mp_arg_t allowed_args[] = {
242+
{ MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
243+
{ MP_QSTR_start, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
244+
{ MP_QSTR_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} },
245+
{ MP_QSTR_write_value,MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
246+
};
247+
bitbangio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
236248
check_for_deinit(self);
249+
check_lock(self);
250+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
251+
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
252+
237253
mp_buffer_info_t bufinfo;
238-
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
239-
if (bufinfo.len == 0) {
254+
mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_WRITE);
255+
int32_t start = args[ARG_start].u_int;
256+
size_t length = bufinfo.len;
257+
normalize_buffer_bounds(&start, args[ARG_end].u_int, &length);
258+
259+
if (length == 0) {
240260
return mp_const_none;
241261
}
242-
check_lock(args[0]);
243-
bool ok = shared_module_bitbangio_spi_read(self, bufinfo.buf, bufinfo.len);
262+
263+
bool ok = shared_module_bitbangio_spi_read(self, ((uint8_t*)bufinfo.buf) + start, length, args[ARG_write_value].u_int);
244264
if (!ok) {
245265
mp_raise_OSError(MP_EIO);
246266
}
247267
return mp_const_none;
248268
}
249-
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bitbangio_spi_readinto_obj, 2, 2, bitbangio_spi_readinto);
269+
MP_DEFINE_CONST_FUN_OBJ_KW(bitbangio_spi_readinto_obj, 2, bitbangio_spi_readinto);
250270

251-
//| def write_readinto(self, buffer_out: ReadableBuffer, buffer_in: WriteableBuffer, *, out_start: int = 0, out_end: Optional[int] = None, in_start: int = 0, in_end: Optional[int] = None) -> None:
271+
//| def write_readinto(self, buffer_out: ReadableBuffer, buffer_in: ReadableBuffer, *, out_start: int = 0, out_end: Optional[int] = None, in_start: int = 0, in_end: Optional[int] = None) -> None:
252272
//| """Write out the data in ``buffer_out`` while simultaneously reading data into ``buffer_in``.
273+
//| The SPI object must be locked.
253274
//| The lengths of the slices defined by ``buffer_out[out_start:out_end]`` and ``buffer_in[in_start:in_end]``
254275
//| must be equal.
255276
//| If buffer slice lengths are both 0, nothing happens.
@@ -274,6 +295,7 @@ STATIC mp_obj_t bitbangio_spi_write_readinto(size_t n_args, const mp_obj_t *pos_
274295
};
275296
bitbangio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
276297
check_for_deinit(self);
298+
check_lock(self);
277299

278300
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
279301
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);

shared-bindings/bitbangio/SPI.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ extern void shared_module_bitbangio_spi_unlock(bitbangio_spi_obj_t *self);
5454
extern bool shared_module_bitbangio_spi_write(bitbangio_spi_obj_t *self, const uint8_t *data, size_t len);
5555

5656
// Reads in len bytes while outputting zeroes.
57-
extern bool shared_module_bitbangio_spi_read(bitbangio_spi_obj_t *self, uint8_t *data, size_t len);
57+
extern bool shared_module_bitbangio_spi_read(bitbangio_spi_obj_t *self, uint8_t *data, size_t len, uint8_t write_value);
5858

5959
// Transfer out len bytes while reading len bytes
6060
extern bool shared_module_bitbangio_spi_transfer(bitbangio_spi_obj_t *self, const uint8_t *dout, uint8_t *din, size_t len);

shared-module/bitbangio/SPI.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ bool shared_module_bitbangio_spi_write(bitbangio_spi_obj_t *self, const uint8_t
176176
}
177177

178178
// Reads in len bytes while outputting zeroes.
179-
bool shared_module_bitbangio_spi_read(bitbangio_spi_obj_t *self, uint8_t *data, size_t len) {
179+
bool shared_module_bitbangio_spi_read(bitbangio_spi_obj_t *self, uint8_t *data, size_t len, uint8_t write_data) {
180180
if (len > 0 && !self->has_miso) {
181181
mp_raise_ValueError(translate("Cannot read without MISO pin."));
182182
}
@@ -210,8 +210,12 @@ bool shared_module_bitbangio_spi_read(bitbangio_spi_obj_t *self, uint8_t *data,
210210
common_hal_digitalio_digitalinout_set_value(&self->mosi, false);
211211
}
212212
for (size_t i = 0; i < len; ++i) {
213+
uint8_t data_out = write_data;
213214
uint8_t data_in = 0;
214-
for (int j = 0; j < 8; ++j) {
215+
for (int j = 0; j < 8; ++j, data_out <<= 1) {
216+
if (self->has_mosi) {
217+
common_hal_digitalio_digitalinout_set_value(&self->mosi, (data_out >> 7) & 1);
218+
}
215219
if (self->phase == 0) {
216220
common_hal_mcu_delay_us(delay_half);
217221
common_hal_digitalio_digitalinout_set_value(&self->clock, 1 - self->polarity);

0 commit comments

Comments
 (0)