diff --git a/include/libwebsockets/lws-spi.h b/include/libwebsockets/lws-spi.h index d4316341c4..58d1bbf5b1 100644 --- a/include/libwebsockets/lws-spi.h +++ b/include/libwebsockets/lws-spi.h @@ -86,4 +86,8 @@ typedef struct lws_spi_ops { LWS_VISIBLE LWS_EXTERN int lws_spi_table_issue(const lws_spi_ops_t *spi_ops, uint32_t flags, const uint8_t *p, size_t len); +LWS_VISIBLE LWS_EXTERN int +lws_spi_readback(const lws_spi_ops_t *spi_ops, uint32_t flags, + const uint8_t *p, size_t len, uint8_t *rb, size_t rb_len); + #endif diff --git a/lib/drivers/spi/lws-spi.c b/lib/drivers/spi/lws-spi.c index 1ee3dbbe1b..34ffe2ad2b 100644 --- a/lib/drivers/spi/lws-spi.c +++ b/lib/drivers/spi/lws-spi.c @@ -55,3 +55,39 @@ lws_spi_table_issue(const lws_spi_ops_t *spi_ops, uint32_t flags, return 0; } + +int +lws_spi_readback(const lws_spi_ops_t *spi_ops, uint32_t flags, + const uint8_t *p, size_t len, uint8_t *rb, size_t rb_len) +{ + lws_spi_desc_t desc; + size_t pos = 0; + + memset(&desc, 0, sizeof(desc)); + desc.count_cmd = 1; + desc.flags = flags; + + while (pos < len) { + + desc.count_write = p[pos++]; + + desc.src = (uint8_t *)&p[pos++]; + if (desc.count_write) + desc.data = (uint8_t *)&p[pos]; + else + desc.data = NULL; + + desc.dest = rb; + desc.count_read = rb_len; + + if (spi_ops->queue(spi_ops, &desc) != ESP_OK) { + lwsl_err("%s: unable to queue\n", __func__); + return 1; + } + + pos += desc.count_write; + } + + return 0; +} +