From f44c511c95792828f9b6fe9beef4ecff5ffb9053 Mon Sep 17 00:00:00 2001 From: vetonsaliu8 <87392786+vetonsaliu8@users.noreply.github.com> Date: Fri, 17 Sep 2021 10:32:04 -0400 Subject: [PATCH] Add the ability to do SPI reads (#12) * Add the ability to do SPI reads * lint Co-authored-by: Mark Harfouche --- CHANGELOG.md | 4 ++++ src/commandconstants.hpp | 3 +++ src/main.cpp | 13 +++++++++++++ 3 files changed, 20 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d255f6..9b9b7c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +### 0.0.17 + +* Add the ability to do SPI byte reads + ### 0.0.16 * Bugfix: Ensure that analog reads return unsigned values. diff --git a/src/commandconstants.hpp b/src/commandconstants.hpp index 162da02..67cce43 100644 --- a/src/commandconstants.hpp +++ b/src/commandconstants.hpp @@ -43,6 +43,7 @@ int spi_settings(CommandRouter *cmd, int argc, const char **argv); int spi_begin_transaction(CommandRouter *cmd, int argc, const char **argv); int spi_end_transaction(CommandRouter *cmd, int argc, const char **argv); int spi_transfer(CommandRouter *cmd, int argc, const char **argv); +int spi_read_byte(CommandRouter *cmd, int argc, const char **argv); int spi_transfer_bulk(CommandRouter *cmd, int argc, const char **argv); int spi_set_clock_divider(CommandRouter *cmd, int argc, const char **argv); // Syntax is: {short command, description, syntax} @@ -116,6 +117,8 @@ command_item_t command_list[] = { spi_end_transaction}, {"spi_transfer", "SPI Transfer 8bits of data", "spi_transfer data", spi_transfer}, + {"spi_read_byte", "SPI transfer register address and read a byte", + "spi_read_byte data", spi_read_byte}, {"spi_transfer_bulk", "SPI transfer multiple sets of 8 bits of data", "spi_transfer_bulk data[0] data[1] data[2] [...]", spi_transfer_bulk}, {nullptr, nullptr, nullptr, nullptr}}; diff --git a/src/main.cpp b/src/main.cpp index 420469f..e473e00 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -473,6 +473,19 @@ int spi_transfer(CommandRouter *cmd, int argc, const char **argv) { return 0; } +int spi_read_byte(CommandRouter *cmd, int argc, const char **argv) { + if (argc != 2) { + return EINVAL; + } + uint8_t data = strtol(argv[1], nullptr, 0); + SPI.beginTransaction(my_spi_settings()); + SPI.transfer(data); + uint8_t received_byte = SPI.transfer(0xff); + SPI.endTransaction(); + snprintf(cmd->buffer, cmd->buffer_size, "0x%02X", received_byte); + return 0; +} + int spi_transfer_bulk(CommandRouter *cmd, int argc, const char **argv) { uint8_t data; if (argc == 1) {