Skip to content

SdSpiTeensy3.cpp - Could be faster and not need to use 512 bytes on the stack on each sector send. #506

Open
@KurtE

Description

@KurtE

@greiman @PaulStoffregen: While @mjs513 and myself have been playing with SdFat code on another board, I noticed that
the current versions of this file, use the option 1 like code as described in this version library SdFatConfig.h file:

/**
 * If USE_SPI_ARRAY_TRANSFER is one and the standard SPI library is
 * use, the array transfer function, transfer(buf, count), will be used.
 * This option will allocate a 512 byte temporary buffer for send.
 * This may be faster for some boards.  Do not use this with AVR boards.
 *
 * Warning: the next options are often fastest but only available for some
 * non-Arduino board packages.
 *
 * If USE_SPI_ARRAY_TRANSFER is two use transfer(nullptr, buf, count) for
 * receive and transfer(buf, nullptr, count) for send.
 *
 * If USE_SPI_ARRAY_TRANSFER is three use transfer(nullptr, buf, count) for
 * receive and transfer(buf, rxTmp, count) for send. Try this with Adafruit
 * SAMD51.
 *
 * If USE_SPI_ARRAY_TRANSFER is four use transfer(txTmp, buf, count) for
 * receive and transfer(buf, rxTmp, count) for send. Try this with STM32.
 */

This code on every multi byte (512 typically) send it uses a 512 byte array on the stack, that it copiesthe data
to be sent to and then uses the m_spi->transfer(tmp, count);
code to send it.

8 years ago we added the methods:

	static void setTransferWriteFill(uint8_t ch ) {_transferWriteFill = ch;}
	static void transfer(const void * buf, void * retbuf, uint32_t count);

to the Teensy SPI library.
so this code could simply be:

void SdSpiArduinoDriver::send(const uint8_t* buf, size_t count) {
    m_spi->transfer(buf, nullptr, count);
}

Likewise, currently the receive function does a memset(buf, 0xff, count);
before it calls: m_spi->transfer(buf, count);

Now if SD cards do require 0xff to be sent, you can specify what character should be sent when
you specify in the transfer, call that you are not passing it a buffer.

Something like:

uint8_t SdSpiArduinoDriver::receive(uint8_t* buf, size_t count) {
  m_spi->setTransferWriteFill(0xff);
  m_spi->transfer(nullptr, buf, count);
  return 0;
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions