From d7758249fa2c98c025807e21c489ab38eb8c9834 Mon Sep 17 00:00:00 2001 From: Scott Allen Date: Fri, 17 Jul 2020 15:18:07 -0400 Subject: [PATCH] Add function SPItransferAndRead() Suggested by @MrBlinky Same as SPItransfer() but also returns the byte received. Provided for use with homemade or expanded units that have had additional peripherals added to the SPI bus that are capable of sending data. --- keywords.txt | 1 + src/Arduboy2Core.cpp | 7 +++++++ src/Arduboy2Core.h | 24 +++++++++++++++++++++++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/keywords.txt b/keywords.txt index e576da1..a678665 100644 --- a/keywords.txt +++ b/keywords.txt @@ -107,6 +107,7 @@ setTextColor KEYWORD2 setTextSize KEYWORD2 setTextWrap KEYWORD2 SPItransfer KEYWORD2 +SPItransferAndRead KEYWORD2 systemButtons KEYWORD2 toggle KEYWORD2 waitNoButtons KEYWORD2 diff --git a/src/Arduboy2Core.cpp b/src/Arduboy2Core.cpp index d475d26..e242793 100644 --- a/src/Arduboy2Core.cpp +++ b/src/Arduboy2Core.cpp @@ -251,6 +251,13 @@ void Arduboy2Core::SPItransfer(uint8_t data) while (!(SPSR & _BV(SPIF))) { } // wait } +// Write to and read from the SPI bus (out to MOSI pin, in from MISO pin) +uint8_t Arduboy2Core::SPItransferAndRead(uint8_t data) +{ + SPItransfer(data); + return SPDR; +} + void Arduboy2Core::safeMode() { if (buttonsState() == UP_BUTTON) diff --git a/src/Arduboy2Core.h b/src/Arduboy2Core.h index 0f595f3..8df55ce 100644 --- a/src/Arduboy2Core.h +++ b/src/Arduboy2Core.h @@ -402,10 +402,32 @@ class Arduboy2Core : public Arduboy2NoUSB * or as data to be placed on the screen, depending on the command/data * mode. * - * \see LCDDataMode() LCDCommandMode() sendLCDCommand() + * \see LCDDataMode() LCDCommandMode() sendLCDCommand() SPItransferAndRead() */ static void SPItransfer(uint8_t data); + /** \brief + * Transfer a byte to, and read a byte from, the SPI bus. + * + * \param data The byte to be sent. + * + * \return The byte that was received. + * + * \details + * This function does the same as the `SPItransfer()` function but also + * reads and returns the byte of data that was received during the + * transfer. + * + * This function is of no use for a standard Arduboy, since only the + * display is connected to the SPI bus and data cannot be received from + * the display. It has been provided for use with homemade or expanded + * units that have had additional peripherals added to the SPI bus that + * are capable of sending data. + * + * \see SPItransfer() + */ + static uint8_t SPItransferAndRead(uint8_t data); + /** \brief * Turn the display off. *