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.
This commit is contained in:
Scott Allen 2020-07-17 15:18:07 -04:00
parent 5563599c6d
commit d7758249fa
3 changed files with 31 additions and 1 deletions

View File

@ -107,6 +107,7 @@ setTextColor KEYWORD2
setTextSize KEYWORD2
setTextWrap KEYWORD2
SPItransfer KEYWORD2
SPItransferAndRead KEYWORD2
systemButtons KEYWORD2
toggle KEYWORD2
waitNoButtons KEYWORD2

View File

@ -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)

View File

@ -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.
*