PlatformIO Have A BUG(SD Lib)

Hello folks,
I am using Teensy 3.6 board. When I upload “SDCard datalooger” example code to teensy in arduino IDE, I see “card initialized” but when I upload the same code with PlatformIO in VsCode but I see “Card failed, or not present”.

What is the problem in here. First time, I used platformio and arduino but now not working?

[env:teensy36]
platform = https://github.com/platformio/platform-teensy.git
framework = arduino
board = teensy36
lib_deps = 
    arkhipenko/TaskScheduler@^3.3.0
    mmarchetti/DirectIO@^1.2.0
    mikalhart/TinyGPSPlus@^1.0.2
    arduino-libraries/SD@^1.2.4
; change microcontroller
board_build.mcu = MK66FX1M0
; change MCU frequency
board_build.f_cpu = 180000000L
build_flags = -std=gnu++17 -D DEBUG=true
build_unflags = -std=gnu++11
platform_packages = 
     toolchain-gccarmnoneeabi@>1.50000.0
#include <SPI.h>
#include <SD.h>

const int chipSelect = 254;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


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

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }
  Serial.println("card initialized.");
}

void loop() {
  // make a string for assembling the data to log:
  String dataString = "";

  // read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 2) {
      dataString += ",";
    }
  }

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

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
}

No, remove that line. The Teensy Arduino core already comes with the SD card library that you need, specific to the Teensy. Delete the .pio folder too before rebuild. Using the wrong SD card library should be the main culprit why your program isn’t working.

I don’t see any of these libraries being used in the code.

And you are you sure that changing the C++ version has 0 effect on the correctness of the program?

Thank you for fast responsemaxgerhardt. I did as you said and it works.
I want to use C++17 because I want to use std::optional library but I don’t. How can I use C++17 library features.

What’s the errormessage?

optional: No such file or directory

You’re explicitly selecting an older >5.0.0 compiler here? Pretty sure that’ll give you a 1.50401.210106 version, aka 5.4.1.

Have you tried

platform_packages =
  toolchain-gccarmnoneeabi@~1.90301.0

to grab a 9.3.1 toolchain? (per https://api.registry.platformio.org/v3/packages/platformio/tool/toolchain-gccarmnoneeabi)

How I can install the tool chain? In the link, there is a just json.

You replace the part in the platformio.ini above with the part I wrote. The link to the JSON API is just for reference on what versions are available.

I changed but anew didn’t work.
I took error in below,

In file included from .pio\libdeps\teensy36\DirectIO/DirectIO.h:23,
                 from src\MainTest\maintest.cpp:6:
.pio\libdeps\teensy36\DirectIO/include/ports.h:32:2: warning: #warning "Unsupported Arduino architecture - falling back to digitalRead and digitalWrite." [-Wcpp]
   32 | #warning "Unsupported Arduino architecture - falling back to digitalRead and digitalWrite."
      |  ^~~~~~~
In file included from C:\Users\enesa\.platformio\packages\framework-arduinoteensy\cores\teensy3/core_pins.h:34,
                 from C:\Users\enesa\.platformio\packages\framework-arduinoteensy\cores\teensy3/wiring.h:39,
                 from C:\Users\enesa\.platformio\packages\framework-arduinoteensy\cores\teensy3/WProgram.h:45,
                 from C:\Users\enesa\.platformio\packages\framework-arduinoteensy\cores\teensy3/Arduino.h:6,
                 from C:\Users\enesa\.platformio\packages\framework-arduinoteensy\libraries\Wire\WireKinetis.cpp:27:
C:\Users\enesa\.platformio\packages\framework-arduinoteensy\cores\teensy3/kinetis.h:1193:22: error: reinterpret_cast from integer to pointer
 1193 | #define SIM_SCGC4  (*(volatile uint32_t *)0x40048034) // System Clock Gating Control Register 4
      |                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\enesa\.platformio\packages\framework-arduinoteensy\libraries\Wire\WireKinetis.cpp:840:2: note: in expansion of macro 'SIM_SCGC4'
  840 |  SIM_SCGC4, SIM_SCGC4_I2C0,
      |  ^~~~~~~~~
C:\Users\enesa\.platformio\packages\framework-arduinoteensy\cores\teensy3/kinetis.h:1193:22: error: reinterpret_cast from integer to pointer
 1193 | #define SIM_SCGC4  (*(volatile uint32_t *)0x40048034) // System Clock Gating Control Register 4
      |                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\enesa\.platformio\packages\framework-arduinoteensy\libraries\Wire\WireKinetis.cpp:857:2: note: in expansion of macro 'SIM_SCGC4'
  857 |  SIM_SCGC4, SIM_SCGC4_I2C1,
      |  ^~~~~~~~~
C:\Users\enesa\.platformio\packages\framework-arduinoteensy\cores\teensy3/kinetis.h:1168:22: error: reinterpret_cast from integer to pointer
 1168 | #define SIM_SCGC1  (*(volatile uint32_t *)0x40048028) // System Clock Gating Control Register 1
      |                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\enesa\.platformio\packages\framework-arduinoteensy\libraries\Wire\WireKinetis.cpp:880:2: note: in expansion of macro 'SIM_SCGC1'
  880 |  SIM_SCGC1, SIM_SCGC1_I2C2,
      |  ^~~~~~~~~
C:\Users\enesa\.platformio\packages\framework-arduinoteensy\cores\teensy3/kinetis.h:1168:22: error: reinterpret_cast from integer to pointer
 1168 | #define SIM_SCGC1  (*(volatile uint32_t *)0x40048028) // System Clock Gating Control Register 1
      |                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\enesa\.platformio\packages\framework-arduinoteensy\libraries\Wire\WireKinetis.cpp:893:2: note: in expansion of macro 'SIM_SCGC1'
  893 |  SIM_SCGC1, SIM_SCGC1_I2C3,
      |  ^~~~~~~~~
C:\Users\enesa\.platformio\packages\framework-arduinoteensy\libraries\Wire\WireKinetis.cpp:910:54: error: reinterpret_cast from integer to pointer
  907 | #define MAKE_CONST(x) (__builtin_constant_p(x) ? (x) : (x))
      |                                                  ~~~  
......
  910 | constexpr uintptr_t i2c0_addr = uintptr_t(MAKE_CONST(&KINETIS_I2C0));
C:\Users\enesa\.platformio\packages\framework-arduinoteensy\libraries\Wire\WireKinetis.cpp:907:51: note: in definition of macro 'MAKE_CONST'
  907 | #define MAKE_CONST(x) (__builtin_constant_p(x) ? (x) : (x))
      |                                                   ^
C:\Users\enesa\.platformio\packages\framework-arduinoteensy\libraries\Wire\WireKinetis.cpp:915:54: error: reinterpret_cast from integer to pointer
  907 | #define MAKE_CONST(x) (__builtin_constant_p(x) ? (x) : (x))
      |                                                  ~~~
......
  915 | constexpr uintptr_t i2c1_addr = uintptr_t(MAKE_CONST(&KINETIS_I2C1));
C:\Users\enesa\.platformio\packages\framework-arduinoteensy\libraries\Wire\WireKinetis.cpp:907:51: note: in definition of macro 'MAKE_CONST'
  907 | #define MAKE_CONST(x) (__builtin_constant_p(x) ? (x) : (x))
      |                                                   ^
C:\Users\enesa\.platformio\packages\framework-arduinoteensy\libraries\Wire\WireKinetis.cpp:920:54: error: reinterpret_cast from integer to pointer
  907 | #define MAKE_CONST(x) (__builtin_constant_p(x) ? (x) : (x))
      |                                                  ~~~
......
  920 | constexpr uintptr_t i2c2_addr = uintptr_t(MAKE_CONST(&KINETIS_I2C2));
C:\Users\enesa\.platformio\packages\framework-arduinoteensy\libraries\Wire\WireKinetis.cpp:907:51: note: in definition of macro 'MAKE_CONST'
  907 | #define MAKE_CONST(x) (__builtin_constant_p(x) ? (x) : (x))
      |                                                   ^
*** [.pio\build\teensy36\libbbf\Wire\WireKinetis.cpp.o] Error 1
src\MainTest\maintest.cpp:19:6: error: 'optional' in namespace 'std' does not name a template type
   19 | std::optional<int> opt{1};
      |      ^~~~~~~~
src\MainTest\maintest.cpp:19:1: note: 'std::optional' is only available from C++17 onwards
   19 | std::optional<int> opt{1};
      | ^~~
src\MainTest\maintest.cpp:20:12: error: 'opt' was not declared in this scope
   20 | auto val = opt.value();
      |            ^~~
*** [.pio\build\teensy36\src\MainTest\maintest.cpp.o] Error 1

Yeah doesn’t seem like the core is C++17 compatible with these pointer cast errors. You can try opening an issue at GitHub - PaulStoffregen/cores: Teensy Core Libraries for Arduino to get them to upgrade to C++17.