Skip to content

Commit 5bb28c7

Browse files
committed
extmod/machine_spi: Simplify SPI xfer function to only take one buf len.
There is no need to take src_len and dest_len arguments. The case of reading-only with a single output byte (originally src_len=1, dest_len>1) is now handled by using the output buffer as the input buffer, and using memset to fill the output byte into this buffer. This simplifies the implementations of the spi_transfer protocol function.
1 parent a0d97fe commit 5bb28c7

5 files changed

Lines changed: 28 additions & 57 deletions

File tree

esp8266/modpybhspi.c

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,53 +50,41 @@ typedef struct _pyb_hspi_obj_t {
5050
} pyb_hspi_obj_t;
5151

5252

53-
STATIC void hspi_transfer(mp_obj_base_t *self_in, size_t src_len, const uint8_t *src_buf, size_t dest_len, uint8_t *dest_buf) {
53+
STATIC void hspi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
5454
(void)self_in;
5555

56-
if (dest_len == 0) {
56+
if (dest == NULL) {
5757
// fast case when we only need to write data
5858
size_t chunk_size = 1024;
59-
size_t count = src_len / chunk_size;
59+
size_t count = len / chunk_size;
6060
size_t i = 0;
6161
for (size_t j = 0; j < count; ++j) {
6262
for (size_t k = 0; k < chunk_size; ++k) {
63-
spi_tx8fast(HSPI, src_buf[i]);
63+
spi_tx8fast(HSPI, src[i]);
6464
++i;
6565
}
6666
ets_loop_iter();
6767
}
68-
while (i < src_len) {
69-
spi_tx8fast(HSPI, src_buf[i]);
68+
while (i < len) {
69+
spi_tx8fast(HSPI, src[i]);
7070
++i;
7171
}
7272
} else {
7373
// we need to read and write data
7474

7575
// Process data in chunks, let the pending tasks run in between
7676
size_t chunk_size = 1024; // TODO this should depend on baudrate
77-
size_t count = dest_len / chunk_size;
77+
size_t count = len / chunk_size;
7878
size_t i = 0;
7979
for (size_t j = 0; j < count; ++j) {
8080
for (size_t k = 0; k < chunk_size; ++k) {
81-
uint32_t data_out;
82-
if (src_len == 1) {
83-
data_out = src_buf[0];
84-
} else {
85-
data_out = src_buf[i];
86-
}
87-
dest_buf[i] = spi_transaction(HSPI, 0, 0, 0, 0, 8, data_out, 8, 0);
81+
dest[i] = spi_transaction(HSPI, 0, 0, 0, 0, 8, src[i], 8, 0);
8882
++i;
8983
}
9084
ets_loop_iter();
9185
}
92-
while (i < dest_len) {
93-
uint32_t data_out;
94-
if (src_len == 1) {
95-
data_out = src_buf[0];
96-
} else {
97-
data_out = src_buf[i];
98-
}
99-
dest_buf[i] = spi_transaction(HSPI, 0, 0, 0, 0, 8, data_out, 8, 0);
86+
while (i < len) {
87+
dest[i] = spi_transaction(HSPI, 0, 0, 0, 0, 8, src[i], 8, 0);
10088
++i;
10189
}
10290
}

esp8266/modpybspi.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,12 @@ typedef struct _pyb_spi_obj_t {
4747
mp_hal_pin_obj_t miso;
4848
} pyb_spi_obj_t;
4949

50-
STATIC void mp_hal_spi_transfer(mp_obj_base_t *self_in, size_t src_len, const uint8_t *src_buf, size_t dest_len, uint8_t *dest_buf) {
50+
STATIC void mp_hal_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
5151
pyb_spi_obj_t *self = (pyb_spi_obj_t*)self_in;
5252
// only MSB transfer is implemented
5353
uint32_t delay_half = 500000 / self->baudrate + 1;
54-
for (size_t i = 0; i < src_len || i < dest_len; ++i) {
55-
uint8_t data_out;
56-
if (src_len == 1) {
57-
data_out = src_buf[0];
58-
} else {
59-
data_out = src_buf[i];
60-
}
54+
for (size_t i = 0; i < len; ++i) {
55+
uint8_t data_out = src[i];
6156
uint8_t data_in = 0;
6257
for (int j = 0; j < 8; ++j, data_out <<= 1) {
6358
mp_hal_pin_write(self->mosi, (data_out >> 7) & 1);
@@ -77,8 +72,8 @@ STATIC void mp_hal_spi_transfer(mp_obj_base_t *self_in, size_t src_len, const ui
7772
ets_delay_us(delay_half);
7873
}
7974
}
80-
if (dest_len != 0) {
81-
dest_buf[i] = data_in;
75+
if (dest != NULL) {
76+
dest[i] = data_in;
8277
}
8378
// make sure pending tasks have a chance to run
8479
ets_loop_iter();

extmod/machine_spi.c

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,46 +25,41 @@
2525
*/
2626

2727
#include <stdio.h>
28+
#include <string.h>
2829

2930
#include "py/runtime.h"
3031
#include "extmod/machine_spi.h"
3132

3233
#if MICROPY_PY_MACHINE_SPI
3334

34-
STATIC void mp_machine_spi_transfer(mp_obj_t self, size_t slen, const uint8_t *src, size_t dlen, uint8_t *dest) {
35+
STATIC void mp_machine_spi_transfer(mp_obj_t self, size_t len, const void *src, void *dest) {
3536
mp_obj_base_t *s = (mp_obj_base_t*)MP_OBJ_TO_PTR(self);
3637
mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t*)s->type->protocol;
37-
spi_p->transfer(s, slen, src, dlen, dest);
38+
spi_p->transfer(s, len, src, dest);
3839
}
3940

4041
STATIC mp_obj_t mp_machine_spi_read(size_t n_args, const mp_obj_t *args) {
41-
uint8_t write_byte = 0;
42-
if (n_args == 3) {
43-
write_byte = mp_obj_get_int(args[2]);
44-
}
4542
vstr_t vstr;
4643
vstr_init_len(&vstr, mp_obj_get_int(args[1]));
47-
mp_machine_spi_transfer(args[0], 1, &write_byte, vstr.len, (uint8_t*)vstr.buf);
44+
memset(vstr.buf, n_args == 3 ? mp_obj_get_int(args[2]) : 0, vstr.len);
45+
mp_machine_spi_transfer(args[0], vstr.len, vstr.buf, vstr.buf);
4846
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
4947
}
5048
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_machine_spi_read_obj, 2, 3, mp_machine_spi_read);
5149

5250
STATIC mp_obj_t mp_machine_spi_readinto(size_t n_args, const mp_obj_t *args) {
5351
mp_buffer_info_t bufinfo;
5452
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
55-
uint8_t write_byte = 0;
56-
if (n_args == 3) {
57-
write_byte = mp_obj_get_int(args[2]);
58-
}
59-
mp_machine_spi_transfer(args[0], 1, &write_byte, bufinfo.len, (uint8_t*)bufinfo.buf);
53+
memset(bufinfo.buf, n_args == 3 ? mp_obj_get_int(args[2]) : 0, bufinfo.len);
54+
mp_machine_spi_transfer(args[0], bufinfo.len, bufinfo.buf, bufinfo.buf);
6055
return mp_const_none;
6156
}
6257
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_machine_spi_readinto_obj, 2, 3, mp_machine_spi_readinto);
6358

6459
STATIC mp_obj_t mp_machine_spi_write(mp_obj_t self, mp_obj_t wr_buf) {
6560
mp_buffer_info_t src;
6661
mp_get_buffer_raise(wr_buf, &src, MP_BUFFER_READ);
67-
mp_machine_spi_transfer(self, src.len, (const uint8_t*)src.buf, 0, NULL);
62+
mp_machine_spi_transfer(self, src.len, (const uint8_t*)src.buf, NULL);
6863
return mp_const_none;
6964
}
7065
MP_DEFINE_CONST_FUN_OBJ_2(mp_machine_spi_write_obj, mp_machine_spi_write);
@@ -75,9 +70,9 @@ STATIC mp_obj_t mp_machine_spi_write_readinto(mp_obj_t self, mp_obj_t wr_buf, mp
7570
mp_buffer_info_t dest;
7671
mp_get_buffer_raise(rd_buf, &dest, MP_BUFFER_WRITE);
7772
if (src.len != dest.len) {
78-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "buffers must be the same length"));
73+
mp_raise_ValueError("buffers must be the same length");
7974
}
80-
mp_machine_spi_transfer(self, src.len, (const uint8_t*)src.buf, dest.len, (uint8_t*)dest.buf);
75+
mp_machine_spi_transfer(self, src.len, src.buf, dest.buf);
8176
return mp_const_none;
8277
}
8378
MP_DEFINE_CONST_FUN_OBJ_3(mp_machine_spi_write_readinto_obj, mp_machine_spi_write_readinto);

extmod/machine_spi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
// SPI protocol
3333
typedef struct _mp_machine_spi_p_t {
34-
void (*transfer)(mp_obj_base_t *obj, size_t slen, const uint8_t *src, size_t dlen, uint8_t *dest);
34+
void (*transfer)(mp_obj_base_t *obj, size_t len, const uint8_t *src, uint8_t *dest);
3535
} mp_machine_spi_p_t;
3636

3737
MP_DECLARE_CONST_FUN_OBJ(mp_machine_spi_read_obj);

stmhal/spi.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -401,15 +401,8 @@ STATIC void spi_transfer(mp_obj_base_t *self_in, size_t src_len, const uint8_t *
401401
}
402402
}
403403

404-
STATIC void spi_transfer_machine(mp_obj_base_t *self_in, size_t src_len, const uint8_t *src_buf, size_t dest_len, uint8_t *dest_buf) {
405-
if (src_len == 1 && dest_len > 1) {
406-
// this catches read and readinto
407-
// copy the single output byte to the dest buffer and use that as source
408-
memset(dest_buf, src_buf[0], dest_len);
409-
src_len = dest_len;
410-
src_buf = dest_buf;
411-
}
412-
spi_transfer(self_in, src_len, src_buf, dest_len, dest_buf, 100);
404+
STATIC void spi_transfer_machine(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
405+
spi_transfer(self_in, len, src, dest == NULL ? 0 : len, dest, 100);
413406
}
414407

415408
/******************************************************************************/

0 commit comments

Comments
 (0)