summaryrefslogtreecommitdiffstatshomepage
path: root/esp8266
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2016-10-03 12:39:31 +1100
committerDamien George <damien.p.george@gmail.com>2016-10-03 12:39:31 +1100
commit5bb28c7f10ebd1036302cf7ac0b24a7a233de2aa (patch)
tree6f8546bc8346bee9193251443122212f76d2af94 /esp8266
parenta0d97fe40872b46872657bedc0e47cfd704c59aa (diff)
downloadmicropython-5bb28c7f10ebd1036302cf7ac0b24a7a233de2aa.tar.gz
micropython-5bb28c7f10ebd1036302cf7ac0b24a7a233de2aa.zip
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.
Diffstat (limited to 'esp8266')
-rw-r--r--esp8266/modpybhspi.c32
-rw-r--r--esp8266/modpybspi.c15
2 files changed, 15 insertions, 32 deletions
diff --git a/esp8266/modpybhspi.c b/esp8266/modpybhspi.c
index c1cd7f662d..10a090269f 100644
--- a/esp8266/modpybhspi.c
+++ b/esp8266/modpybhspi.c
@@ -50,23 +50,23 @@ typedef struct _pyb_hspi_obj_t {
} pyb_hspi_obj_t;
-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) {
+STATIC void hspi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
(void)self_in;
- if (dest_len == 0) {
+ if (dest == NULL) {
// fast case when we only need to write data
size_t chunk_size = 1024;
- size_t count = src_len / chunk_size;
+ size_t count = len / chunk_size;
size_t i = 0;
for (size_t j = 0; j < count; ++j) {
for (size_t k = 0; k < chunk_size; ++k) {
- spi_tx8fast(HSPI, src_buf[i]);
+ spi_tx8fast(HSPI, src[i]);
++i;
}
ets_loop_iter();
}
- while (i < src_len) {
- spi_tx8fast(HSPI, src_buf[i]);
+ while (i < len) {
+ spi_tx8fast(HSPI, src[i]);
++i;
}
} else {
@@ -74,29 +74,17 @@ STATIC void hspi_transfer(mp_obj_base_t *self_in, size_t src_len, const uint8_t
// Process data in chunks, let the pending tasks run in between
size_t chunk_size = 1024; // TODO this should depend on baudrate
- size_t count = dest_len / chunk_size;
+ size_t count = len / chunk_size;
size_t i = 0;
for (size_t j = 0; j < count; ++j) {
for (size_t k = 0; k < chunk_size; ++k) {
- uint32_t data_out;
- if (src_len == 1) {
- data_out = src_buf[0];
- } else {
- data_out = src_buf[i];
- }
- dest_buf[i] = spi_transaction(HSPI, 0, 0, 0, 0, 8, data_out, 8, 0);
+ dest[i] = spi_transaction(HSPI, 0, 0, 0, 0, 8, src[i], 8, 0);
++i;
}
ets_loop_iter();
}
- while (i < dest_len) {
- uint32_t data_out;
- if (src_len == 1) {
- data_out = src_buf[0];
- } else {
- data_out = src_buf[i];
- }
- dest_buf[i] = spi_transaction(HSPI, 0, 0, 0, 0, 8, data_out, 8, 0);
+ while (i < len) {
+ dest[i] = spi_transaction(HSPI, 0, 0, 0, 0, 8, src[i], 8, 0);
++i;
}
}
diff --git a/esp8266/modpybspi.c b/esp8266/modpybspi.c
index 4c4b843c5d..b9650954f3 100644
--- a/esp8266/modpybspi.c
+++ b/esp8266/modpybspi.c
@@ -47,17 +47,12 @@ typedef struct _pyb_spi_obj_t {
mp_hal_pin_obj_t miso;
} pyb_spi_obj_t;
-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) {
+STATIC void mp_hal_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
pyb_spi_obj_t *self = (pyb_spi_obj_t*)self_in;
// only MSB transfer is implemented
uint32_t delay_half = 500000 / self->baudrate + 1;
- for (size_t i = 0; i < src_len || i < dest_len; ++i) {
- uint8_t data_out;
- if (src_len == 1) {
- data_out = src_buf[0];
- } else {
- data_out = src_buf[i];
- }
+ for (size_t i = 0; i < len; ++i) {
+ uint8_t data_out = src[i];
uint8_t data_in = 0;
for (int j = 0; j < 8; ++j, data_out <<= 1) {
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
ets_delay_us(delay_half);
}
}
- if (dest_len != 0) {
- dest_buf[i] = data_in;
+ if (dest != NULL) {
+ dest[i] = data_in;
}
// make sure pending tasks have a chance to run
ets_loop_iter();