STM32F407VE SDfat error compiling

TFTLCDCyg
Posts: 6
Joined: Tue Jan 07, 2020 9:50 pm

Re: STM32F407VE SDfat error compiling

Post by TFTLCDCyg »

This example is the one that worked in an F767ZI core, with the official STM32 core and the latest version of the Greimann SdFat library

Code: Select all

/*
 * This program attempts to initialize an SD card and analyze its structure.
 */
#include <SPI.h>
#include "SdFat.h"
#include "sdios.h"

// Set USE_SDIO to zero for SPI card access. 
#define USE_SDIO 0
/*
 * SD chip select pin.  Common values are:
 *
 * Arduino Ethernet shield, pin 4.
 * SparkFun SD shield, pin 8.
 * Adafruit SD shields and modules, pin 10.
 * Default SD chip select is the SPI SS pin.
 */
const uint8_t SD_CHIP_SELECT = PB11;  //SPI3 on Nucleo F767ZI
/*
 * Set DISABLE_CHIP_SELECT to disable a second SPI device.
 * For example, with the Ethernet shield, set DISABLE_CHIP_SELECT
 * to 10 to disable the Ethernet controller.
 */
const int8_t DISABLE_CHIP_SELECT = -1;

#if USE_SDIO
// Use faster SdioCardEX
SdFatSdioEX sd;
// SdFatSdio sd;
#else // USE_SDIO
//SdFat sd;
 SPIClass SPI_3(PB2, PB4, PB3);      //SPI3 on Nucleo F767ZI
 SdFat sd(&SPI_3);                   //SPI3 on Nucleo F767ZI
#endif  // USE_SDIO
// serial output steam
ArduinoOutStream cout(Serial);

// global for card size
uint32_t cardSize;

// global for card erase size
uint32_t eraseSize;
//------------------------------------------------------------------------------
// store error strings in flash
#define sdErrorMsg(msg) sd.errorPrint(F(msg));
//------------------------------------------------------------------------------
uint8_t cidDmp() {
  cid_t cid;
  if (!sd.card()->readCID(&cid)) {
    sdErrorMsg("readCID failed");
    return false;
  }
  cout << F("\nManufacturer ID: ");
  cout << hex << int(cid.mid) << dec << endl;
  cout << F("OEM ID: ") << cid.oid[0] << cid.oid[1] << endl;
  cout << F("Product: ");
  for (uint8_t i = 0; i < 5; i++) {
    cout << cid.pnm[i];
  }
  cout << F("\nVersion: ");
  cout << int(cid.prv_n) << '.' << int(cid.prv_m) << endl;
  cout << F("Serial number: ") << hex << cid.psn << dec << endl;
  cout << F("Manufacturing date: ");
  cout << int(cid.mdt_month) << '/';
  cout << (2000 + cid.mdt_year_low + 10 * cid.mdt_year_high) << endl;
  cout << endl;
  return true;
}
//------------------------------------------------------------------------------
uint8_t csdDmp() {
  csd_t csd;
  uint8_t eraseSingleBlock;
  if (!sd.card()->readCSD(&csd)) {
    sdErrorMsg("readCSD failed");
    return false;
  }
  if (csd.v1.csd_ver == 0) {
    eraseSingleBlock = csd.v1.erase_blk_en;
    eraseSize = (csd.v1.sector_size_high << 1) | csd.v1.sector_size_low;
  } else if (csd.v2.csd_ver == 1) {
    eraseSingleBlock = csd.v2.erase_blk_en;
    eraseSize = (csd.v2.sector_size_high << 1) | csd.v2.sector_size_low;
  } else {
    cout << F("csd version error\n");
    return false;
  }
  eraseSize++;
  cout << F("cardSize: ") << 0.000512*cardSize;
  cout << F(" MB (MB = 1,000,000 bytes)\n");

  cout << F("flashEraseSize: ") << int(eraseSize) << F(" blocks\n");
  cout << F("eraseSingleBlock: ");
  if (eraseSingleBlock) {
    cout << F("true\n");
  } else {
    cout << F("false\n");
  }
  return true;
}
//------------------------------------------------------------------------------
// print partition table
uint8_t partDmp() {
  mbr_t mbr;
  if (!sd.card()->readBlock(0, (uint8_t*)&mbr)) {
    sdErrorMsg("read MBR failed");
    return false;
  }
  for (uint8_t ip = 1; ip < 5; ip++) {
    part_t *pt = &mbr.part[ip - 1];
    if ((pt->boot & 0X7F) != 0 || pt->firstSector > cardSize) {
      cout << F("\nNo MBR. Assuming Super Floppy format.\n");
      return true;
    }
  }
  cout << F("\nSD Partition Table\n");
  cout << F("part,boot,type,start,length\n");
  for (uint8_t ip = 1; ip < 5; ip++) {
    part_t *pt = &mbr.part[ip - 1];
    cout << int(ip) << ',' << hex << int(pt->boot) << ',' << int(pt->type);
    cout << dec << ',' << pt->firstSector <<',' << pt->totalSectors << endl;
  }
  return true;
}
//------------------------------------------------------------------------------
void volDmp() {
  cout << F("\nVolume is FAT") << int(sd.vol()->fatType()) << endl;
  cout << F("blocksPerCluster: ") << int(sd.vol()->blocksPerCluster()) << endl;
  cout << F("clusterCount: ") << sd.vol()->clusterCount() << endl;
  cout << F("freeClusters: ");
  uint32_t volFree = sd.vol()->freeClusterCount();
  cout <<  volFree << endl;
  float fs = 0.000512*volFree*sd.vol()->blocksPerCluster();
  cout << F("freeSpace: ") << fs << F(" MB (MB = 1,000,000 bytes)\n");
  cout << F("fatStartBlock: ") << sd.vol()->fatStartBlock() << endl;
  cout << F("fatCount: ") << int(sd.vol()->fatCount()) << endl;
  cout << F("blocksPerFat: ") << sd.vol()->blocksPerFat() << endl;
  cout << F("rootDirStart: ") << sd.vol()->rootDirStart() << endl;
  cout << F("dataStartBlock: ") << sd.vol()->dataStartBlock() << endl;
  if (sd.vol()->dataStartBlock() % eraseSize) {
    cout << F("Data area is not aligned on flash erase boundaries!\n");
    cout << F("Download and use formatter from www.sdcard.org!\n");
  }
}
//------------------------------------------------------------------------------
void setup() {
  Serial.begin(9600);
  
  // Wait for USB Serial 
  while (!Serial) {
    SysCall::yield();
  }

  // use uppercase in hex and use 0X base prefix
  cout << uppercase << showbase << endl;

  // F stores strings in flash to save RAM
  cout << F("SdFat version: ") << SD_FAT_VERSION << endl;
#if !USE_SDIO  
  if (DISABLE_CHIP_SELECT < 0) {
    cout << F(
           "\nAssuming the SD is the only SPI device.\n"
           "Edit DISABLE_CHIP_SELECT to disable another device.\n");
  } else {
    cout << F("\nDisabling SPI device on pin ");
    cout << int(DISABLE_CHIP_SELECT) << endl;
    pinMode(DISABLE_CHIP_SELECT, OUTPUT);
    digitalWrite(DISABLE_CHIP_SELECT, HIGH);
  }
  cout << F("\nAssuming the SD chip select pin is: ") <<int(SD_CHIP_SELECT);
  cout << F("\nEdit SD_CHIP_SELECT to change the SD chip select pin.\n");
#endif  // !USE_SDIO  
}
//------------------------------------------------------------------------------
void loop() {
  // Read any existing Serial data.
  do {
    delay(10);
  } while (Serial.available() && Serial.read() >= 0);

  // F stores strings in flash to save RAM
  cout << F("\ntype any character to start\n");
  while (!Serial.available()) {
    SysCall::yield();
  }

  uint32_t t = millis();
#if USE_SDIO
  if (!sd.cardBegin()) {
    sdErrorMsg("\ncardBegin failed");
    return;
  }
#else  // USE_SDIO
  // Initialize at the highest speed supported by the board that is
  // not over 50 MHz. Try a lower speed if SPI errors occur.
  if (!sd.cardBegin(SD_CHIP_SELECT, SD_SCK_MHZ(50))) {
    sdErrorMsg("cardBegin failed");
    return;
  }
 #endif  // USE_SDIO 
  t = millis() - t;

  cardSize = sd.card()->cardSize();
  if (cardSize == 0) {
    sdErrorMsg("cardSize failed");
    return;
  }
  cout << F("\ninit time: ") << t << " ms" << endl;
  cout << F("\nCard type: ");
  switch (sd.card()->type()) {
  case SD_CARD_TYPE_SD1:
    cout << F("SD1\n");
    break;

  case SD_CARD_TYPE_SD2:
    cout << F("SD2\n");
    break;

  case SD_CARD_TYPE_SDHC:
    if (cardSize < 70000000) {
      cout << F("SDHC\n");
    } else {
      cout << F("SDXC\n");
    }
    break;

  default:
    cout << F("Unknown\n");
  }
  if (!cidDmp()) {
    return;
  }
  if (!csdDmp()) {
    return;
  }
  uint32_t ocr;
  if (!sd.card()->readOCR(&ocr)) {
    sdErrorMsg("\nreadOCR failed");
    return;
  }
  cout << F("OCR: ") << hex << ocr << dec << endl;
  if (!partDmp()) {
    return;
  }
  if (!sd.fsBegin()) {
    sdErrorMsg("\nFile System initialization failed.\n");
    return;
  }
  volDmp();
}

User avatar
fpiSTM
Posts: 664
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 25
Location: Le Mans
Contact:

Re: STM32F407VE SDfat error compiling

Post by fpiSTM »

Fine to know that STM32 core is compatible with SDFat fro Greiman.
Anyway, this topic is linked to use it with @stevestrong core for F4, if I'm not wrong?

ag123
Posts: 467
Joined: Thu Dec 19, 2019 5:30 am
Answers: 1

Re: STM32F407VE SDfat error compiling

Post by ag123 »

so far it seemed only stm32duino official core run on F7 ?
but that's good effort :D

xtream123
Posts: 22
Joined: Thu Dec 19, 2019 1:23 am

Re: STM32F407VE SDfat error compiling

Post by xtream123 »

fpiSTM wrote:
Wed Jan 08, 2020 8:12 am
Fine to know that STM32 core is compatible with SDFat fro Greiman.
Anyway, this topic is linked to use it with @stevestrong core for F4, if I'm not wrong?
yes it is @stevestrong core :)

TFTLCDCyg
Posts: 6
Joined: Tue Jan 07, 2020 9:50 pm

Re: STM32F407VE SDfat error compiling

Post by TFTLCDCyg »

Certainly, I experimented with the arrangement I had on the table hehehe. As mentioned by xtream123 previously:

Code: Select all

 SPIClass SPI_3(3);
 SdFat sd(&SPI_3); 
The most recent SdFat version threw me an error in DMA, I had to modify a line in the SdSpiSTM32.cpp file from the SdFat library:

From:

Code: Select all

#elif defined (__ STM32F4__)
#define USE_STM32_DMA 1
To:

Code: Select all

#elif defined (__ STM32F4__)
#define USE_STM32_DMA 0
Using the stevstrong core.

PD:
I must rescue the fact that the SdFat library in its latest version is 100% compatible with the official STM32 core, I used version 1.60

stevestrong
Posts: 268
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 5
Location: Munich, Germany
Contact:

Re: STM32F407VE SDfat error compiling

Post by stevestrong »

This commit most probably provides a fix for it.

xtream123
Posts: 22
Joined: Thu Dec 19, 2019 1:23 am

Re: STM32F407VE SDfat error compiling

Post by xtream123 »

Thanks. I will update you as soon as I have my board again :)

BennehBoy
Posts: 47
Joined: Sat Jan 04, 2020 2:38 am

Re: STM32F407VE SDfat error compiling

Post by BennehBoy »

fpiSTM wrote:
Wed Jan 08, 2020 8:12 am
Fine to know that STM32 core is compatible with SDFat fro Greiman.
Anyway, this topic is linked to use it with @stevestrong core for F4, if I'm not wrong?
It's compatible only if using SPI, SDIO is broken - or was the last time I checked. It can be fixed with a small merge in of a file from the STM32GENERIC port of sdfat, I opened an issue about this with Greiman https://github.com/greiman/SdFat/issues/124 - I don't think it's been fixed yet... he's still working on his new version.

xtream123
Posts: 22
Joined: Thu Dec 19, 2019 1:23 am

Re: STM32F407VE SDfat error compiling

Post by xtream123 »

stevestrong wrote:
Sat Jan 11, 2020 3:38 pm
This commit most probably provides a fix for it.
Sorry for the long response about this. I just want to confirm for future readers that this works with this commit.

Post Reply

Return to “STM32F4 based boards”