Page 1 of 2

STM32F407VE SDfat error compiling

Posted: Thu Jan 02, 2020 8:54 am
by xtream123
Hi there guys,
I'm trying to use the Sdfat library by greiman ( https://github.com/greiman/SdFat ) but I'm not able to compile it successfully.
I'm compiling the example "SdInfo.ino" from the example folder.

the error is

Code: Select all

C:\Users\LAPTOP\Documents\Arduino\libraries\SdFat\src\SpiDriver\SdSpiSTM32.cpp: In member function 'uint8_t SdSpiAltDriver::receive(uint8_t*, size_t)':

C:\Users\LAPTOP\Documents\Arduino\libraries\SdFat\src\SpiDriver\SdSpiSTM32.cpp:78:44: error: void value not ignored as it ought to be

   return m_spi->dmaTransfer(nullptr, buf, n);

                                            ^

C:\Users\LAPTOP\Documents\Arduino\libraries\SdFat\src\SpiDriver\SdSpiSTM32.cpp:83:1: warning: control reaches end of non-void function [-Wreturn-type]

 }

 ^
Thank you.
BTW, I successfully compiled and tested the built-in SD library not the SDfat one.
Do you have any sugestion or work around on this or I just use the built-in sd library at the moment?

Im Using:
OS: Windows 10
IDE : Arduino IDE 1.8.9
Core : stevstrong core (https://github.com/stevstrong/Arduino_STM32)

Board settings: Generic STM32f407V series
USB Configuration: USB Serial (CDC)
Upload method : ST link (im using jlink to upload the bin file)

Re: STM32F407VE SDfat error compiling

Posted: Fri Jan 03, 2020 2:28 pm
by stevestrong
I recommend to use my fork of SdFat.

And get the latest core files from my repo, because I pushed today a commit to solve an issue which is also relevant for SPI and other peripherals.

Re: STM32F407VE SDfat error compiling

Posted: Sat Jan 04, 2020 8:35 am
by xtream123
Thanks. I will try this :)

Re: STM32F407VE SDfat error compiling

Posted: Mon Jan 06, 2020 3:37 am
by xtream123
stevestrong wrote:
Fri Jan 03, 2020 2:28 pm
I recommend to use my fork of SdFat.

And get the latest core files from my repo, because I pushed today a commit to solve an issue which is also relevant for SPI and other peripherals.
Hi,
I have tested your forked SDFat and it works but in SPI1 only.

My purpose is to use SD card in other SPI port so I tested it to SPI2 and SPI3 and it didn't work. it is always in SPI1 when I accidentally forget to change my wiring to spi2 or 3.

Digging down to the SPI library I saw at the last part of SPI.cpp the

Code: Select all

#ifndef DEFAULT_SPI_PORT
  #define DEFAULT_SPI_PORT 1
#endif

SPIClass SPI(DEFAULT_SPI_PORT);
this is in line 928 to 932.


What I did to make it work on other SPI port is I commented this out

Code: Select all

/*
#ifndef DEFAULT_SPI_PORT
  #define DEFAULT_SPI_PORT 1
#endif

SPIClass SPI(DEFAULT_SPI_PORT);
*/
Maybe you can take a look at it if this is really need to be removed or there is a work around in this.
Will there it be no problem since for SPI1 I will use ethernet module(W5500) and SD card on SPI2 or SPI3?
Thank you.

Re: STM32F407VE SDfat error compiling

Posted: Mon Jan 06, 2020 4:24 am
by xtream123
Or I have missed something in setting up to use other SPI port?

This is the code that I used to switch to other SPI port(frowm ReadWrite example of SDFat library)

Code: Select all

#include <SPI.h>
#include "SdFat.h"
#define SD_CS_PIN PB12

// Use second SPI port
SPIClass SPI_3(3);
SdFat sd2(&SPI_3);
File myFile;


void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  //  while (!Serial) {
  //    ; // wait for serial port to connect. Needed for native USB port only
  //  }
  for (int x = 1; x < 7; x++) {
    Serial.println(x);
    delay(1000);
  }

  Serial.print("Initializing SD card...");

  if (!sd2.begin(SD_CS_PIN)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = sd2.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  myFile = sd2.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop() {
  // nothing happens after setup
}

Re: STM32F407VE SDfat error compiling

Posted: Tue Jan 07, 2020 9:52 pm
by TFTLCDCyg
Inside of the SdFat Config.h file, modify the value of this line:

Code: Select all

#define USE_STANDARD_SPI_LIBRARY 0
for this:

Code: Select all

#define USE_STANDARD_SPI_LIBRARY 2
Maybe it helps.

Re: STM32F407VE SDfat error compiling

Posted: Wed Jan 08, 2020 1:33 am
by xtream123
TFTLCDCyg wrote:
Tue Jan 07, 2020 9:52 pm
Inside of the SdFat Config.h file, modify the value of this line:

Code: Select all

#define USE_STANDARD_SPI_LIBRARY 0
for this:

Code: Select all

#define USE_STANDARD_SPI_LIBRARY 2
Maybe it helps.
Thank you for your suggestion. I will try it :)

Re: STM32F407VE SDfat error compiling

Posted: Wed Jan 08, 2020 2:34 am
by xtream123
Hi @TFTLCDCyg , I tried your suggestion but the response is the same...failed to initialize the sd card.

Re: STM32F407VE SDfat error compiling

Posted: Wed Jan 08, 2020 2:38 am
by xtream123
Hi,
I got it working now in different SPI port without commenting the last few line in the SPI.h.

What I did is i place "SPI_3.setModule(3);" before the initialization of sd2.begin.

Code: Select all

 
 SPI_3.setModule(3);
 
I saw this ".setModule()" in the Ethernet_STM32 example and tried in the sd example.

Thank you.

Re: STM32F407VE SDfat error compiling

Posted: Wed Jan 08, 2020 3:12 am
by xtream123
This is my working example for your reference.
No modification in the SPI ot SDFat library is needed.

Code: Select all

/*
  SD card read/write

  This example shows how to read and write data to and from an SD card file
  The circuit:
   SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13

  created   Nov 2010
  by David A. Mellis
  modified 9 Apr 2012
  by Tom Igoe

  This example code is in the public domain.

*/

#include <SPI.h>
#include "SdFat.h"
#define SD_CS_PIN PB12

//SPIClass SPI(1);
// Use second SPI port
SPIClass SPI_3(3);

SdFat sd2(&SPI_3);
File myFile;


/*
  #define BOARD_NR_SPI            3
  #define BOARD_SPI1_NSS_PIN      PA4
  #define BOARD_SPI1_SCK_PIN      PA5
  #define BOARD_SPI1_MISO_PIN     PA6
  #define BOARD_SPI1_MOSI_PIN     PA7
  #define BOARD_SPI1A_NSS_PIN     PA15
  #define BOARD_SPI1A_SCK_PIN     PB3
  #define BOARD_SPI1A_MISO_PIN    PB4
  #define BOARD_SPI1A_MOSI_PIN    PB5

  #define BOARD_SPI2_NSS_PIN      PB12
  #define BOARD_SPI2_SCK_PIN      PB13
  #define BOARD_SPI2_MISO_PIN     PB14
  #define BOARD_SPI2_MOSI_PIN     PB15
  #define BOARD_SPI2A_NSS_PIN     PB9
  #define BOARD_SPI2A_SCK_PIN     PB10
  #define BOARD_SPI2A_MISO_PIN    PC2
  #define BOARD_SPI2A_MOSI_PIN    PC3

  #define BOARD_SPI3_NSS_PIN      PA15
  #define BOARD_SPI3_SCK_PIN      PB3
  #define BOARD_SPI3_MISO_PIN     PB4
  #define BOARD_SPI3_MOSI_PIN     PB5
  // //overlap with the SDIO interface for SD card
  //#define BOARD_SPI3A_NSS_PIN     PA4
  //#define BOARD_SPI3A_SCK_PIN     PC10
  //#define BOARD_SPI3A_MISO_PIN    PC11
  //#define BOARD_SPI3A_MOSI_PIN    PC12

  #define BOARD_SDIO_D0           PC8
  #define BOARD_SDIO_D1           PC9
  #define BOARD_SDIO_D2           PC10
  #define BOARD_SDIO_D3           PC11
  #define BOARD_SDIO_CLK          PC12
  #define BOARD_SDIO_CMD          PD2
*/



void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  //  while (!Serial) {
  //    ; // wait for serial port to connect. Needed for native USB port only
  //  }
  for (int x = 1; x < 7; x++) {
    Serial.println(x);
    delay(1000);
  }

  Serial.print("Initializing SD card...");
  SPI_3.setModule(3);
  if (!sd2.begin(SD_CS_PIN)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = sd2.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  myFile = sd2.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop() {
  // nothing happens after setup
}