Skip to content

Commit

Permalink
Add STM32F4 - may be unstable
Browse files Browse the repository at this point in the history
  • Loading branch information
greiman committed Sep 19, 2017
1 parent 12a0199 commit c95f1d4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 50 deletions.
7 changes: 2 additions & 5 deletions examples/STM32Test/STM32Test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ void setup() {
Serial.begin(9600);
// Wait for USB Serial
while (!Serial) {
SysCall::yield();
}
Serial.print(F("FreeStack: "));

Serial.println(FreeStack());

// fill buffer with known data
for (size_t i = 0; i < sizeof(buf); i++) {
Expand All @@ -46,8 +42,9 @@ void setup() {

Serial.println(F("type any character to start"));
while (!Serial.available()) {
SysCall::yield();
}
Serial.print(F("FreeStack: "));
Serial.println(FreeStack());

// initialize the first card
if (!sd1.begin(SD1_CS, SD_SCK_MHZ(18))) {
Expand Down
2 changes: 1 addition & 1 deletion src/SdFatConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@
/**
* Determine the default SPI configuration.
*/
#if defined(__STM32F1__)
#if defined(__STM32F1__) || defined(__STM32F4__)
// has multiple SPI ports
#define SD_HAS_CUSTOM_SPI 2
#elif defined(__AVR__)\
Expand Down
2 changes: 1 addition & 1 deletion src/SpiDriver/SdSpiDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ class SdSpiAltDriver {
void setPort(uint8_t portNumber);

private:
uint8_t m_spiPort;
SPIClass* m_spi;
#else // IMPLEMENT_SPI_PORT_SELECTION
private:
#endif // IMPLEMENT_SPI_PORT_SELECTION
Expand Down
87 changes: 44 additions & 43 deletions src/SpiDriver/SdSpiSTM32F1.cpp → src/SpiDriver/SdSpiSTM32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,33 @@
* along with the Arduino SdSpiAltDriver Library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#if defined(__STM32F1__)
#if defined(__STM32F1__) || defined(__STM32F4__)
#include "SdSpiDriver.h"
#define USE_STM32F1_DMAC 1
#if defined(__STM32F1__)
#define USE_STM32_DMA 1
#elif defined(__STM32F4__)
#define USE_STM32_DMA 0
#else // defined(__STM32F1__)
#error Unknown STM32 type
#endif // defined(__STM32F1__)
//------------------------------------------------------------------------------
static SPIClass m_SPI1(1);
#if BOARD_NR_SPI > 1
#if BOARD_NR_SPI >= 2
static SPIClass m_SPI2(2);
#endif // BOARD_NR_SPI > 1
#if BOARD_NR_SPI > 2
#endif // BOARD_NR_SPI >= 2
#if BOARD_NR_SPI >= 3
static SPIClass m_SPI3(3);
#endif // BOARD_NR_SPI > 2
//
static SPIClass* pSpi[] =
#if BOARD_NR_SPI == 1
{&m_SPI1};
#elif BOARD_NR_SPI == 2
{&m_SPI1, &m_SPI2};
#elif BOARD_NR_SPI == 3
{&m_SPI1, &m_SPI2, &m_SPI3};
#else // BOARD_NR_SPI
#error "BOARD_NR_SPI too large"
#endif // BOARD_NR_SPI
#endif // BOARD_NR_SPI >= 3
#if BOARD_NR_SPI > 3
#error BOARD_NR_SPI too large
#endif
//------------------------------------------------------------------------------
/** 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() {
pSpi[m_spiPort]->beginTransaction(m_spiSettings);
m_spi->beginTransaction(m_spiSettings);
}
//------------------------------------------------------------------------------
/** Initialize the SPI bus.
Expand All @@ -56,22 +54,22 @@ void SdSpiAltDriver::begin(uint8_t csPin) {
m_csPin = csPin;
pinMode(m_csPin, OUTPUT);
digitalWrite(m_csPin, HIGH);
pSpi[m_spiPort]->begin();
m_spi->begin();
}
//------------------------------------------------------------------------------
/**
* End SPI transaction.
*/
void SdSpiAltDriver::deactivate() {
pSpi[m_spiPort]->endTransaction();
m_spi->endTransaction();
}
//------------------------------------------------------------------------------
/** Receive a byte.
*
* \return The byte.
*/
uint8_t SdSpiAltDriver::receive() {
return pSpi[m_spiPort]->transfer(0XFF);
return m_spi->transfer(0XFF);
}
//------------------------------------------------------------------------------
/** Receive multiple bytes.
Expand All @@ -82,27 +80,20 @@ uint8_t SdSpiAltDriver::receive() {
* \return Zero for no error or nonzero error code.
*/
uint8_t SdSpiAltDriver::receive(uint8_t* buf, size_t n) {
int rtn = 0;
#if USE_STM32F1_DMAC
rtn = pSpi[m_spiPort]->dmaTransfer(0, buf, n);
#else // USE_STM32F1_DMAC
#if 1 // set to zero if multi-byte read() fails.
pSpi[m_spiPort]->read(buf, n);
#else // Try multi-byte read again
for (size_t i = 0; i < n; i++) {
buf[i] = pSpi[m_spiPort]->transfer(0XFF);
}
#endif // Try multi-byte read again
#endif // USE_STM32F1_DMAC
return rtn;
#if USE_STM32_DMA
return m_spi->dmaTransfer(0, buf, n);
#else // USE_STM32_DMA
m_spi->read(buf, n);
return 0;
#endif // USE_STM32_DMA
}
//------------------------------------------------------------------------------
/** Send a byte.
*
* \param[in] b Byte to send
*/
void SdSpiAltDriver::send(uint8_t b) {
pSpi[m_spiPort]->transfer(b);
m_spi->transfer(b);
}
//------------------------------------------------------------------------------
/** Send multiple bytes.
Expand All @@ -111,14 +102,24 @@ void SdSpiAltDriver::send(uint8_t b) {
* \param[in] n Number of bytes to send.
*/
void SdSpiAltDriver::send(const uint8_t* buf , size_t n) {
#if USE_STM32F1_DMAC
pSpi[m_spiPort]->dmaSend(const_cast<uint8*>(buf), n);
#else // #if USE_STM32F1_DMAC
pSpi[m_spiPort]->write(const_cast<uint8*>(buf), n);
#endif // USE_STM32F1_DMAC
#if USE_STM32_DMA
m_spi->dmaTransfer(const_cast<uint8*>(buf), 0, n);
#else // USE_STM32_DMA
m_spi->write(const_cast<uint8*>(buf), n);
#endif // USE_STM32_DMA
}
//-----------------------------------------------------------------------------
//------------------------------------------------------------------------------
void SdSpiAltDriver::setPort(uint8_t portNumber) {
m_spiPort = portNumber < 1 || portNumber > BOARD_NR_SPI ? 0 : portNumber -1;
m_spi = &m_SPI1;
#if BOARD_NR_SPI >= 2
if (portNumber == 2) {
m_spi = &m_SPI2;
}
#endif // BOARD_NR_SPI >= 2
#if BOARD_NR_SPI >= 3
if (portNumber == 3) {
m_spi = &m_SPI3;
}
#endif // BOARD_NR_SPI >= 2
}
#endif // defined(__STM32F1__)
#endif // defined(__STM32F1__) || defined(__STM32F4__)

0 comments on commit c95f1d4

Please sign in to comment.