This commit is contained in:
@@ -52,19 +52,24 @@ const uint8_t DEDICATED_SPI = 1;
|
||||
* \param[in] opt option field of SdSpiConfig.
|
||||
* \return true for dedicated SPI.
|
||||
*/
|
||||
inline bool spiOptionDedicated(uint8_t opt) {return opt & DEDICATED_SPI;}
|
||||
#else // ENABLE_DEDICATED_SPI
|
||||
inline bool spiOptionDedicated(uint8_t opt) { return opt & DEDICATED_SPI; }
|
||||
#else // ENABLE_DEDICATED_SPI
|
||||
/**
|
||||
* \param[in] opt option field of SdSpiConfig.
|
||||
* \return true for dedicated SPI.
|
||||
*/
|
||||
inline bool spiOptionDedicated(uint8_t opt) {(void)opt; return false;}
|
||||
inline bool spiOptionDedicated(uint8_t opt) {
|
||||
(void)opt;
|
||||
return false;
|
||||
}
|
||||
#endif // ENABLE_DEDICATED_SPI
|
||||
/** The user will call begin. Useful for custom SPI configurations. */
|
||||
const uint8_t USER_SPI_BEGIN = 2;
|
||||
//------------------------------------------------------------------------------
|
||||
/** SPISettings for SCK frequency in Hz. */
|
||||
#define SD_SCK_HZ(maxSpeed) (maxSpeed)
|
||||
/** SPISettings for SCK frequency in MHz. */
|
||||
#define SD_SCK_MHZ(maxMhz) (1000000UL*(maxMhz))
|
||||
#define SD_SCK_MHZ(maxMhz) (1000000UL * (maxMhz))
|
||||
// SPI divisor constants - obsolete.
|
||||
/** Set SCK to max rate. */
|
||||
#define SPI_FULL_SPEED SD_SCK_MHZ(50)
|
||||
@@ -93,8 +98,8 @@ typedef SdSpiSoftDriver SpiPort_t;
|
||||
class SdSpiBaseClass;
|
||||
/** Port type for extrernal SPI driver. */
|
||||
typedef SdSpiBaseClass SpiPort_t;
|
||||
#else // SPI_DRIVER_SELECT
|
||||
typedef void* SpiPort_t;
|
||||
#else // SPI_DRIVER_SELECT
|
||||
typedef void* SpiPort_t;
|
||||
#endif // SPI_DRIVER_SELECT
|
||||
//------------------------------------------------------------------------------
|
||||
/**
|
||||
@@ -103,15 +108,15 @@ typedef void* SpiPort_t;
|
||||
*/
|
||||
class SdSpiConfig {
|
||||
public:
|
||||
/** SdSpiConfig constructor.
|
||||
/** SdSpiConfig constructor.
|
||||
*
|
||||
* \param[in] cs Chip select pin.
|
||||
* \param[in] opt Options.
|
||||
* \param[in] maxSpeed Maximum SCK frequency.
|
||||
* \param[in] port The SPI port to use.
|
||||
*/
|
||||
SdSpiConfig(SdCsPin_t cs, uint8_t opt, uint32_t maxSpeed, SpiPort_t* port) :
|
||||
csPin(cs), options(opt), maxSck(maxSpeed), spiPort(port) {}
|
||||
SdSpiConfig(SdCsPin_t cs, uint8_t opt, uint32_t maxSpeed, SpiPort_t* port)
|
||||
: csPin(cs), options(opt), maxSck(maxSpeed), spiPort(port) {}
|
||||
|
||||
/** SdSpiConfig constructor.
|
||||
*
|
||||
@@ -119,8 +124,8 @@ class SdSpiConfig {
|
||||
* \param[in] opt Options.
|
||||
* \param[in] maxSpeed Maximum SCK frequency.
|
||||
*/
|
||||
SdSpiConfig(SdCsPin_t cs, uint8_t opt, uint32_t maxSpeed) :
|
||||
csPin(cs), options(opt), maxSck(maxSpeed) {}
|
||||
SdSpiConfig(SdCsPin_t cs, uint8_t opt, uint32_t maxSpeed)
|
||||
: csPin(cs), options(opt), maxSck(maxSpeed) {}
|
||||
/** SdSpiConfig constructor.
|
||||
*
|
||||
* \param[in] cs Chip select pin.
|
||||
|
||||
@@ -30,9 +30,7 @@ static void SD_SPI_DMA_TransferComplete_Callback() {
|
||||
SPI_DMA_TransferCompleted = true;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void SdSpiArduinoDriver::activate() {
|
||||
m_spi->beginTransaction(m_spiSettings);
|
||||
}
|
||||
void SdSpiArduinoDriver::activate() { m_spi->beginTransaction(m_spiSettings); }
|
||||
//------------------------------------------------------------------------------
|
||||
void SdSpiArduinoDriver::begin(SdSpiConfig spiConfig) {
|
||||
if (spiConfig.spiPort) {
|
||||
@@ -43,36 +41,29 @@ void SdSpiArduinoDriver::begin(SdSpiConfig spiConfig) {
|
||||
m_spi->begin();
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void SdSpiArduinoDriver::deactivate() {
|
||||
m_spi->endTransaction();
|
||||
}
|
||||
void SdSpiArduinoDriver::deactivate() { m_spi->endTransaction(); }
|
||||
//------------------------------------------------------------------------------
|
||||
void SdSpiArduinoDriver::end() {
|
||||
m_spi->end();
|
||||
}
|
||||
void SdSpiArduinoDriver::end() { m_spi->end(); }
|
||||
//------------------------------------------------------------------------------
|
||||
uint8_t SdSpiArduinoDriver::receive() {
|
||||
return m_spi->transfer(0XFF);
|
||||
}
|
||||
uint8_t SdSpiArduinoDriver::receive() { return m_spi->transfer(0XFF); }
|
||||
//------------------------------------------------------------------------------
|
||||
uint8_t SdSpiArduinoDriver::receive(uint8_t* buf, size_t count) {
|
||||
SPI_DMA_TransferCompleted = false;
|
||||
m_spi->transfer(nullptr, buf, count, SD_SPI_DMA_TransferComplete_Callback);
|
||||
while (!SPI_DMA_TransferCompleted) {}
|
||||
while (!SPI_DMA_TransferCompleted) {
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void SdSpiArduinoDriver::send(uint8_t data) {
|
||||
m_spi->transfer(data);
|
||||
}
|
||||
void SdSpiArduinoDriver::send(uint8_t data) { m_spi->transfer(data); }
|
||||
//------------------------------------------------------------------------------
|
||||
void SdSpiArduinoDriver::send(const uint8_t* buf , size_t count) {
|
||||
void SdSpiArduinoDriver::send(const uint8_t* buf, size_t count) {
|
||||
SPI_DMA_TransferCompleted = false;
|
||||
|
||||
m_spi->transfer(const_cast<uint8_t*>(buf), nullptr, count,
|
||||
SD_SPI_DMA_TransferComplete_Callback);
|
||||
SD_SPI_DMA_TransferComplete_Callback);
|
||||
|
||||
while (!SPI_DMA_TransferCompleted) {}
|
||||
while (!SPI_DMA_TransferCompleted) {
|
||||
}
|
||||
}
|
||||
#endif // defined(SD_USE_CUSTOM_SPI) && defined(PLATFORM_ID)
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Copyright (c) 2011-2018 Bill Greiman
|
||||
* Copyright (c) 2011-2022 Bill Greiman
|
||||
* This file is part of the SdFat library for SD memory cards.
|
||||
*
|
||||
* MIT License
|
||||
@@ -22,8 +22,9 @@
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
#if defined(__STM32F1__) || defined(__STM32F4__)
|
||||
// Driver for: https://github.com/rogerclarkmelbourne/Arduino_STM32
|
||||
#include "SdSpiDriver.h"
|
||||
#if defined(SD_USE_CUSTOM_SPI) && (defined(__STM32F1__) || defined(__STM32F4__))
|
||||
#if defined(__STM32F1__)
|
||||
#define USE_STM32_DMA 1
|
||||
#elif defined(__STM32F4__)
|
||||
@@ -32,74 +33,39 @@
|
||||
#error Unknown STM32 type
|
||||
#endif // defined(__STM32F1__)
|
||||
//------------------------------------------------------------------------------
|
||||
/** Set SPI options for access to SD/SDHC cards.
|
||||
*
|
||||
* \param[in] divisor SCK clock divider relative to the APB1 or APB2 clock.
|
||||
*/
|
||||
void SdSpiAltDriver::activate() {
|
||||
m_spi->beginTransaction(m_spiSettings);
|
||||
}
|
||||
void SdSpiArduinoDriver::activate() { m_spi->beginTransaction(m_spiSettings); }
|
||||
//------------------------------------------------------------------------------
|
||||
/** Initialize the SPI bus.
|
||||
*
|
||||
* \param[in] chipSelectPin SD card chip select pin.
|
||||
*/
|
||||
void SdSpiAltDriver::begin(uint8_t csPin) {
|
||||
m_csPin = csPin;
|
||||
pinMode(m_csPin, OUTPUT);
|
||||
digitalWrite(m_csPin, HIGH);
|
||||
void SdSpiArduinoDriver::begin(SdSpiConfig spiConfig) {
|
||||
if (spiConfig.spiPort) {
|
||||
m_spi = spiConfig.spiPort;
|
||||
} else {
|
||||
m_spi = &SPI;
|
||||
}
|
||||
m_spi->begin();
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/**
|
||||
* End SPI transaction.
|
||||
*/
|
||||
void SdSpiAltDriver::deactivate() {
|
||||
m_spi->endTransaction();
|
||||
}
|
||||
void SdSpiArduinoDriver::deactivate() { m_spi->endTransaction(); }
|
||||
//------------------------------------------------------------------------------
|
||||
/** Receive a byte.
|
||||
*
|
||||
* \return The byte.
|
||||
*/
|
||||
uint8_t SdSpiAltDriver::receive() {
|
||||
return m_spi->transfer(0XFF);
|
||||
}
|
||||
void SdSpiArduinoDriver::end() { m_spi->end(); }
|
||||
//------------------------------------------------------------------------------
|
||||
/** Receive multiple bytes.
|
||||
*
|
||||
* \param[out] buf Buffer to receive the data.
|
||||
* \param[in] n Number of bytes to receive.
|
||||
*
|
||||
* \return Zero for no error or nonzero error code.
|
||||
*/
|
||||
uint8_t SdSpiAltDriver::receive(uint8_t* buf, size_t n) {
|
||||
uint8_t SdSpiArduinoDriver::receive() { return m_spi->transfer(0XFF); }
|
||||
//------------------------------------------------------------------------------
|
||||
uint8_t SdSpiArduinoDriver::receive(uint8_t* buf, size_t count) {
|
||||
#if USE_STM32_DMA
|
||||
return m_spi->dmaTransfer(nullptr, buf, n);
|
||||
#else // USE_STM32_DMA
|
||||
m_spi->read(buf, n);
|
||||
return m_spi->dmaTransfer(nullptr, buf, count);
|
||||
#else // USE_STM32_DMA
|
||||
m_spi->read(buf, count);
|
||||
return 0;
|
||||
#endif // USE_STM32_DMA
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/** Send a byte.
|
||||
*
|
||||
* \param[in] b Byte to send
|
||||
*/
|
||||
void SdSpiAltDriver::send(uint8_t b) {
|
||||
m_spi->transfer(b);
|
||||
}
|
||||
void SdSpiArduinoDriver::send(uint8_t data) { m_spi->transfer(data); }
|
||||
//------------------------------------------------------------------------------
|
||||
/** Send multiple bytes.
|
||||
*
|
||||
* \param[in] buf Buffer for data to be sent.
|
||||
* \param[in] n Number of bytes to send.
|
||||
*/
|
||||
void SdSpiAltDriver::send(const uint8_t* buf , size_t n) {
|
||||
void SdSpiArduinoDriver::send(const uint8_t* buf, size_t count) {
|
||||
#if USE_STM32_DMA
|
||||
m_spi->dmaTransfer(const_cast<uint8*>(buf), nullptr, n);
|
||||
#else // USE_STM32_DMA
|
||||
m_spi->write(const_cast<uint8*>(buf), n);
|
||||
m_spi->dmaTransfer(const_cast<uint8*>(buf), nullptr, count);
|
||||
#else // USE_STM32_DMA
|
||||
m_spi->write(const_cast<uint8*>(buf), count);
|
||||
#endif // USE_STM32_DMA
|
||||
}
|
||||
#endif // defined(__STM32F1__) || defined(__STM32F4__)
|
||||
#endif // defined(SD_USE_CUSTOM_SPI) && defined(__STM32F1__)
|
||||
|
||||
@@ -23,12 +23,10 @@
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
#include "SdSpiDriver.h"
|
||||
#if defined(SD_USE_CUSTOM_SPI) && defined(__arm__) && defined(CORE_TEENSY)
|
||||
#if defined(SD_USE_CUSTOM_SPI) && defined(__arm__) && defined(CORE_TEENSY)
|
||||
#define USE_BLOCK_TRANSFER 1
|
||||
//------------------------------------------------------------------------------
|
||||
void SdSpiArduinoDriver::activate() {
|
||||
m_spi->beginTransaction(m_spiSettings);
|
||||
}
|
||||
void SdSpiArduinoDriver::activate() { m_spi->beginTransaction(m_spiSettings); }
|
||||
//------------------------------------------------------------------------------
|
||||
void SdSpiArduinoDriver::begin(SdSpiConfig spiConfig) {
|
||||
if (spiConfig.spiPort) {
|
||||
@@ -46,23 +44,17 @@ void SdSpiArduinoDriver::begin(SdSpiConfig spiConfig) {
|
||||
m_spi->begin();
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void SdSpiArduinoDriver::deactivate() {
|
||||
m_spi->endTransaction();
|
||||
}
|
||||
void SdSpiArduinoDriver::deactivate() { m_spi->endTransaction(); }
|
||||
//------------------------------------------------------------------------------
|
||||
void SdSpiArduinoDriver::end() {
|
||||
m_spi->end();
|
||||
}
|
||||
void SdSpiArduinoDriver::end() { m_spi->end(); }
|
||||
//------------------------------------------------------------------------------
|
||||
uint8_t SdSpiArduinoDriver::receive() {
|
||||
return m_spi->transfer(0XFF);
|
||||
}
|
||||
uint8_t SdSpiArduinoDriver::receive() { return m_spi->transfer(0XFF); }
|
||||
//------------------------------------------------------------------------------
|
||||
uint8_t SdSpiArduinoDriver::receive(uint8_t* buf, size_t count) {
|
||||
#if USE_BLOCK_TRANSFER
|
||||
memset(buf, 0XFF, count);
|
||||
m_spi->transfer(buf, count);
|
||||
#else // USE_BLOCK_TRANSFER
|
||||
#else // USE_BLOCK_TRANSFER
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
buf[i] = m_spi->transfer(0XFF);
|
||||
}
|
||||
@@ -70,11 +62,9 @@ uint8_t SdSpiArduinoDriver::receive(uint8_t* buf, size_t count) {
|
||||
return 0;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void SdSpiArduinoDriver::send(uint8_t data) {
|
||||
m_spi->transfer(data);
|
||||
}
|
||||
void SdSpiArduinoDriver::send(uint8_t data) { m_spi->transfer(data); }
|
||||
//------------------------------------------------------------------------------
|
||||
void SdSpiArduinoDriver::send(const uint8_t* buf , size_t count) {
|
||||
void SdSpiArduinoDriver::send(const uint8_t* buf, size_t count) {
|
||||
#if USE_BLOCK_TRANSFER
|
||||
uint32_t tmp[128];
|
||||
if (0 < count && count <= 512) {
|
||||
|
||||
Reference in New Issue
Block a user