Wish Support for the SDFat library is recommended

santook

Member
The official SD is easy to use, but not powerful enough. SDFat library not only includes SD library functions, but also supports disk formatting, etc., including multiple SPI of STM32. It is a very good and fully functional library.
 

santook

Member
The SdFat 2.0.2 library must be downloaded from the Arduino IDE
A macro definition should be made in the Region Project Attributes of the Main
#DefineExtra:#include "SdFat.h"
#DefineExtra:#include "sdios.h"


Project Attributes:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 300
    #DefineExtra:#include "SdFat.h"
    #DefineExtra:#include "sdios.h"
#End Region

SDCardFormat.bas:
B4R=true
Group=Default Group
ModulesStructureVersion=1
Type=StaticCode
Version=3
@EndOfDesignText@


Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Private FormatStatus As Boolean
  
    Public CardSize As Double
End Sub

Public Sub DoSDFormat() As Boolean
    FormatStatus=False
    RunNative("DoFormat",Null)
    Return FormatStatus
End Sub

#if c
    static SPIClass SPI_2(2);

    uint32_t cardSectorCount = 0;
    uint8_t  sectorBuffer[512];
  
    SdCardFactory cardFactory;
    SdCard* m_card = nullptr;
  
    uint32_t const ERASE_SIZE = 262144L;
  
    bool formatCard()
    {
          ExFatFormatter exFatFormatter;
          FatFormatter fatFormatter;

          bool rtn = cardSectorCount > 67108864 ?
            exFatFormatter.format(m_card, sectorBuffer, &Serial) :
            fatFormatter.format(m_card, sectorBuffer, &Serial);

          return rtn;
    }
  
    void DoFormat(B4R::Object* O)
    {
        m_card = cardFactory.newCard(SdSpiConfig(PB12, DEDICATED_SPI, SD_SCK_MHZ(18), &SPI_2));
      if (!m_card || m_card->errorCode()) {
        b4r_sdcardformat::_formatstatus=false;
        return;
      }

      cardSectorCount = m_card->sectorCount();
      if (!cardSectorCount) {
        b4r_sdcardformat::_formatstatus=false;
        return;
      }
    
      b4r_sdcardformat::_cardsize=cardSectorCount/2097152.0;
      b4r_sdcardformat::_formatstatus=formatCard();
    }
#End If

How to use:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 300
    #DefineExtra:#include "SdFat.h"
#DefineExtra:#include "sdios.h"
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Delay(10000)
    Log("AppStart")
    If SDCardFormat.DoSDFormat  Then
        Log("Format ok!")
        Log(SDCardFormat.CardSize)
    Else
        Log("Format error!")
    End If
End Sub
 

Attachments

  • SDCardFormat.bas
    1.6 KB · Views: 251
Last edited:
Top