SDfat library not working on particle electron

I have tried the trymefirst example in the SD fat library and the electron just went into connecting state rather than running the set up in the program. The setup and output is attached in the hyperlink below. The codes are attached below the hyperlink
SDfat library post

Codes


#include "application.h"
#include "SdFat.h"

// Pick an SPI configuration.
// See SPI configuration section below (comments are for photon).
#define SPI_CONFIGURATION 0
//------------------------------------------------------------------------------
// Setup SPI configuration.
#if SPI_CONFIGURATION == 0
// Primary SPI with DMA
#define SCK A3
#define  MISO A4
#define MOSI A5
#define  SS A2 
SdFat sd;
const uint8_t chipSelect = SS;
#elif SPI_CONFIGURATION == 1
// Secondary SPI with DMA
// SCK => D4, MISO => D3, MOSI => D2, SS => D1
SdFat sd(1);
const uint8_t chipSelect = D1;
#elif SPI_CONFIGURATION == 2
// Primary SPI with Arduino SPI library style byte I/O.
// SCK => A3, MISO => A4, MOSI => A5, SS => A2 (default)
SdFatLibSpi sd;
const uint8_t chipSelect = SS;
#elif SPI_CONFIGURATION == 3
// Software SPI.  Use any digital pins.
// MISO => D5, MOSI => D6, SCK => D7, SS => D0
SdFatSoftSpi<D5, D6, D7> sd;
const uint8_t chipSelect = D0;
#endif  // SPI_CONFIGURATION
//------------------------------------------------------------------------------

File myFile;

void setup() {
  Serial.begin(9600);
  // Wait for USB Serial 
  while (!Serial) {
    SysCall::yield();
  }
  
  Serial.println("Type any character to start");
  while (Serial.read() <= 0) {
    SysCall::yield();
  }

  // Initialize SdFat or print a detailed error message and halt
  // Use half speed like the native library.
  // Change to SPI_FULL_SPEED for more performance.
  if (!sd.begin(chipSelect, SPI_HALF_SPEED)) {
    sd.initErrorHalt();
  }

  // open the file for write at end like the "Native SD library"
  if (!myFile.open("test.txt", O_RDWR | O_CREAT | O_AT_END)) {
    sd.errorHalt("opening test.txt for write failed");
  }
  // if the file opened okay, write to it:
  Serial.print("Writing to test.txt...");
  myFile.println("testing 1, 2, 3.");
  myFile.printf("fileSize: %d\n", myFile.fileSize());
  
  // close the file:
  myFile.close();
  Serial.println("done.");

  // re-open the file for reading:
  if (!myFile.open("test.txt", O_READ)) {
    sd.errorHalt("opening test.txt for read failed");
  }
  Serial.println("test.txt content:");

  // read from the file until there's nothing else in it:
  int data;
  while ((data = myFile.read()) >= 0) {
    Serial.write(data);
  }
  // close the file:
  myFile.close();
}

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

What’s wrong? The electron will need to first connect to the :cloud: before code runs.

That’s the default behavior if you don’t change the SYSTEM_MODE

@kennethlimcp do u mean need to change the system mode to manual in order to bypass the cloud connection to run the application codes?

Yes. If you want to do so, you can try SYSTEM_MODE(MANUAL).

https://docs.particle.io/reference/firmware/#system-modes

Have a read :slight_smile:

While discussing SDfat, I am having problems compiling using Desktop IDE and Photon.

First I create a new project then select the SDfat library and “Copy to Current project”. This puts all the sdfat libary and example files into the lib folder in my project directory ok.

Next, I copy the code from the “trymefirst” project into my main “ino” project file but keep getting "sdfat.h: No such file or directory - TryMeFirst.cpp 1:19.

I am not sure how to reference these library files, I thought by copying to the project that they would be available?

Thanks for any help.

Is this on the first line of the .ino file?

#include "SdFat.h"

@kennethlimcp yes it is the first line

Can you try changing the file to .cpp extension and see if it compiles?

@kennethlimcp .cpp compiled with errors, .h compiled but the program was not running. I picked out relevant codes from the examples into a running program writes to the sd card successfully.

weird… Does it work for you now?

I will need to test and see what’s wrong.

@kennethlimcp I have picked out these few lines of code and place it in a running program. The code works.

#include "application.h"
#include "SdFat.h"

#define SCK A3
#define  MISO A4
#define MOSI A5
#define  SS A2 
SdFat sd;
const uint8_t chipSelect = SS;
File myFile;

myFile.open("test.txt", O_RDWR | O_CREAT | O_AT_END)
  Serial.print("Writing to test.txt...");
  myFile.println("testing 1, 2, 3.");
  myFile.printf("fileSize: %d\n", myFile.fileSize());	
  myFile.close();
  Serial.println("done.");

I guess it should be:

#include "application.h"
#include "SdFat.h"

#define SCK A3
#define  MISO A4
#define MOSI A5
#define  SS A2 
SdFat sd;
const uint8_t chipSelect = SS;
File myFile;

void setup() {
  myFile.open("test.txt", O_RDWR | O_CREAT | O_AT_END);
  Serial.print("Writing to test.txt...");
  myFile.println("testing 1, 2, 3.");
  myFile.printf("fileSize: %d\n", myFile.fileSize());	
  myFile.close();
  Serial.println("done.");
}

@orbitcoms, when copying a library into your project in Dev, you need to remove this folder ./lib/<LibraryName>/src/<LibraryName> and you should also remove the ./lib/<LibraryName>/examples folder.
There are some issues about that


My project structure (after copying sdfat to project) is like this…
SdLogger
Lib
SdFat
examples
src
sdFat
… All the cpp and h files
src
MyMain Project

I note sdfat is duplicated under lib/sdfat/src ,

I deleted both the example folder and the duplicated folder under src and it compiles now, thanks.

This needs to be done with all libraries I copy to project?

What if I “add to project” instead? I did try that but could not find where the program placed the library files.

I also notice I get the “Compiler timed out or encountered and error” a Lot. If I click this message the status shows there are “no compiler errors”

What I tried next was using the online IDE. I included the SDfat library and compiled and now online I get this “Error: Could not compile. Please review your code”. Not the most helpful error message I have ever seen. It might as well say, “something is not working”. Is there major issues with the compiler error reporting in both online and Desktop IDE?

In that case the library files won't be copied to your project, but only a dependencies line will be added to the project.properties file for the build farm to use the remote version of the library.

With Copy to Current Project you should only find the library files under SdLogger/lib/SdFat - the extra SdFat folder inside src should only contain .h files and is the "offending" compatibility directory mentioned in my issue from above.

To test your project you should use CLI

particle compile photon .

Does this mean the Desktop and Web IDE do not work properly (to need to use CLI)?

My expectation is the compiler errors should be identified inside the IDE.

Branching out to DOS on windows machine all the time is cumbersome. Is this what others are doing for their commercial projects? Or, can we use something like Coocox IDE to write and compile the code and then the CLI for programming?

I like the Photon and Electron hardware and connectivity but the development tools seem to not be up to scratch.

Nope, at least not for the Web IDE, but using CLI in parallel to Dev is a lot simpler than having manually sync the changes between Dev and Web IDE.
And since I'm not a fan of Dev, I use Visual Studio as my editor and build/flash with CLI - you can also "integrate" external tools into full IDEs like VS.

As a programmer since the 80ies I don't have a problem with using and mixing tools.
Don't need a smart hammer for putting nails into walls either :wink:

Considerable commercial products will often use the local toolchain, which also uses lots of console applications.
The Particle build farm does too.

But I agree, IDEs could do (a lot) better.

1 Like

Do you have any links to getting started tutorial for setting up VS to integrate the particle build tools?

I am not sure what you mean about manual synch. I have been using the DEV only to write the code , compile and flash.
Usually it works but you can never tell day by day whether it will compile this time. I don’t understand why the IDE cannot show the error properly so I know where to look to fix it.

The WEB IDE was no better with the current project, it simply states there is an error and to review my code. But without knowing what sort of error it is and no highlighting, it is like finding a needle in a haystack. I am sure lots of thos who love working ion the command line and the old DOS command lines will have tips to run certain utilities to attempt to find the error but all the other IDE I work with will show me the error. I use Visual Studio, PSOC creator, Arduino IDE, Atmel Studio and they all highlight the errors without need to jump around the hard drive using command line utilities.

I have managed to get Visual Micro installed into Visual Studio 17 and the STM32 “boards”.

This allows Arduino compatible C++ projects to be written and compiled and updated via serial port.

How do I install the appropriate “board” for the STM32F205 used in Photon?

NB. I found an article online at hackster that stepped through setting up Visual Studio for Particle and it works great. I also added the Visual Micro plugin for my Arduino projects. Now I have an IDE I am familiar with, it appears fast and provides appropriate error messages. Here is the link if anyone else wants to do this and not sure how…

Hi I am unable to get access to my sd card using this library on my electron
Platform Particle Electron
Firmware version 0.6.0
SD Card Library for Particle 0.0.7
SD CARD INTERFACE Like this

Using Scandisk 4GB Micro SD Card

With memory card plugged in I get INVALID FORMAT ; REFORMAT SD ERROR
With card out I get CANT ACCESS SD CARD ERROR

So I assume my connections and config is correct to SPI1
I have also tested the card and am able to access it from two different mobile phones
Also I have had the card and interface working on Arduino but cant get working on Electron

Code below…
Questions in code

    // SD CARD TEST NEW

    // why are some includes with or without "" or <>
    // what does application.h do?
    #include "application.h"
    #include <SdFat.h>

    // tried auto and manual config as below
    #define SPI_CONFIGURATION 2

    // tried with and without this
    //#define SCK A3
    //#define MISO A4
    //#define MOSI A5
    //#define SS A2 

    // Tried both config 0 and 2
    //SdFat sd;
    SdFatLibSpi sd;

    const uint8_t chipSelect = SS;


    void setup() {
    Serial.begin(9600);
    
    // Wait for USB Serial 
    while (!Serial) {
        // what does syscall do
        SysCall::yield();
    }
    delay(4000);
    Serial.println("Serial OK");
    Serial.println(System.version());
    delay(4000);
    
    // i rem this out as unable to send any char using DOS serial monitor on WIN 10
    // otherwise locks here and then unable to do OTA flash
    //Serial.println("Type any character to start..");
    //while (Serial.read() <= 0) {
        //SysCall::yield();
    //}

    Serial.println("Checking card..");
    // Initialize SdFat or print a detailed error message and halt
    // Use half speed like the native library.
    // Change to SPI_FULL_SPEED for more performance.
    if (!sd.begin(chipSelect, SPI_HALF_SPEED)) {
        sd.initErrorHalt();
    } else {
        Serial.println("SD OK");
    }
}

void loop() {

}