Skip to content

Commit

Permalink
Add the ability to do SPI reads (#12)
Browse files Browse the repository at this point in the history
* Add the ability to do SPI reads

* lint

Co-authored-by: Mark Harfouche <mark.harfouche@gmail.com>
  • Loading branch information
vetonsaliu8 and hmaarrfk authored Sep 17, 2021
1 parent fd12baf commit f44c511
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
3 changes: 3 additions & 0 deletions src/commandconstants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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}};
13 changes: 13 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit f44c511

Please sign in to comment.