ESP8266 SD Card Interfacing Library

sdcore iotbits esp8266 sd card interfacing library data logging esp8266

IoTBits SDCore:
ESP8266 SD Card Interfacing Library

Add FAT32 formatted SD or SDHC cards to your IoT projects with this highly optimized SD card interfacing library for the ESP8266.

Why use SDCore?
- Full use of ESP8266 HSPI hardware capabilities
- Energy efficient
- Configurable speed
- Works with all SD cards


What can SDcore do?
- Read and write multiple files at a time
- Create/read/delete directories
- Format SD cards (optional)
- Optimized data logging

Evaluate IoTBits SDCore

Before requesting a copy of the library, you may evaluate the library on your hardware to make sure that you are ready to run the software library core functions.

There are 2 versions of the evaluation BIN available:
Low speed evaluation BIN, which uses a relatively low clock speed of 2 MHz for interfacing with the SD card. If you are prototyping on a breadboard, this is the recommended version.
High speed evaluation BIN, which uses very high clock speed (20 MHz) for fast data transfer. If you have a well-designed PCB with traces connecting the ESP8266 and SD connector, this is the version you should test with.

Download SDCore evaluation BIN files

Schematic

The connections required are minimal to interface an SD card to the ESP8266:

SD(HC) card   <=>    ESP8266 pin

GPIO2        <=>      CS
GPIO13      <=>      MOSI (or DI)
GPIO12      <=>      MISO (or DO)
GPIO14      <=>      SCLK

Note that the power supply to the SD card should be 3.3V and properly decoupled with a 10uF (or even better, with a 47uF and 100nF capacitors in parallel close to the SD card connector).

Evaluation BIN source code

The evaluation BIN files above are generated by compiling the following source code, with the SDCore library of course:
 /*
 * Copyright (C) 2016, IoTBits (Author: Pratik Panda), all right reserved.
 * Website: www.iot-bits.com
 * This file is a part of the IoTBits SDcore library
 * Find out more:
 * http://www.iot-bits.com/SDcore
 *
 * The code referencing this license is open source software. Redistribution
 * and use of the code in source and binary forms, with or without modification,
 * are permitted provided that the following condition is met:
 *
 * Redistribution of original and modified source code must retain the
 * above copyright notice, this condition and the following disclaimer.
 *
 * This software is provided by the copyright holder and contributors "AS IS"
 * and any warranties related to this software are DISCLAIMED. The copyright
 * owner or contributors are NOT LIABLE for any damages caused by use of this
 * software.
 */

#include "ets_sys.h"
#include "osapi.h"
#include "user_interface.h"
#include "ff.h"

void user_rf_pre_init(void)
{
}

void user_init(void)
{
  unsigned char buf[4096];      // Buffer for file content storage
  FATFS fs;                     // FileSystem object
  FIL f;                        // File object

  unsigned char res, valid_file;
  unsigned int count, i;

    os_printf("SDK version:%sn", system_get_sdk_version());
    wifi_set_opmode(SOFTAP_MODE);


    os_memset (buf, '&', 4096);        // Buffer filled with '&'

    os_printf("nnStarting SD card test...");

//#######################################################################
//##################### Mount SD Card ###################################

    os_printf("nPhase 1: Mount card");

      res = f_mount (&fs, "0:", 1);								// Mount disk
    		if(res != FR_OK)
    			os_printf ("nMount error:%d", (int)res);
        else
          os_printf ("nMount FS: OK");

  //#######################################################################
  //################### Delete old file, if exists ########################

  os_printf("nDelete old file: %d", f_unlink ("small.txt"));
  os_printf("nPhase 2: Open/create 'small.txt'");
// Open or create a file
    res = f_open (&f, "small.txt", FA_WRITE|FA_CREATE_ALWAYS);
      if(res != FR_OK)
       os_printf ("nFile open/create error:%d", (int)res);
      else
       os_printf ("nFile open/create: OK");

   //#######################################################################
   //################## Write to the created file ##########################

  os_printf("nPhase 3: Write to the file");
 // Write content to the file
     res = f_write (&f, buf, 4096, &count);
       if(res != FR_OK)
        os_printf ("nFile content write error:%d", (int)res);
       else
        os_printf ("nFile content write: OK");
    os_printf("nNumber of bytes written: %d", (int)count);

    os_printf("nFree heap: %dn", system_get_free_heap_size());
    os_memset(buf, 0x00, sizeof(buf));    // Clear buffer containing data

   //#######################################################################
   //################## Close (save) the written file ######################

  os_printf("nPhase 4: Close file");
 // This will close the file that was just written
     res = f_close (&f);
       if(res != FR_OK)
        os_printf ("nFile close error:%d", (int)res);
       else
        os_printf ("nFile close: OK");

    os_printf("nFree heap: %dn", system_get_free_heap_size());

   //#######################################################################
   //################## Reopen the file for reading ########################
   // NOTE: The following steps are repeated endlessly (read + verify)

while (1)
{
  os_printf("nPhase 5: Re-open file for reading");
 // Re-open the file for reading and verifying the contents
     res = f_open (&f, "small.txt", FA_READ);					// Re-open file
      if (res != FR_OK)
       os_printf ("nRead error: %d", (int)res);
      else
        os_printf ("nRead file: OK");

   //#######################################################################
   //################## Read file contents #################################

  os_printf("nPhase 6: Read content from file");
 // Read contents of the file to an array
     res = f_read (&f, buf, 4096, &count);
       if (res != FR_OK)
        os_printf ("nRead error: %d", (int)res);
       else
        os_printf ("nRead file: OK");
    os_printf("nNumber of bytes read: %d", (int)count);

    os_printf("nFree heap: %dn", system_get_free_heap_size());

   //#######################################################################
   //################## Close file #########################################

  os_printf("nPhase 7: Close file");
 // This will close the file that was just written
     res = f_close (&f);
       if(res != FR_OK)
        os_printf ("nFile close error:%d", (int)res);
       else
        os_printf ("nFile close: OK");

   //#######################################################################
   //################## Verify if data was written #########################

  os_printf("nPhase 8: Operation verification");
// Verify that the contents are 100% correct
 for (count = 0; count < 4096; count++)
     if (buf[count] != '&')
         {
          os_printf("nVERFICATION ERROR!");
          os_printf("nSomething might be wrong with your hardware...");
          os_printf("nFirst data mismatch index: %d", count);
           break;
         }

 if (count == 4096)
 {
  os_printf("nnnHARDWARE VERIFICATIOn SUCCESSFUL!");
  os_printf("nYour hardware is now ready to run IoTBits SDCore!nnn");
 }
}   // Loop back and reopen file for reading now
    // This can enable testing for probability of failed reads
  os_printf("nFree heap: %dn", system_get_free_heap_size());
}

// End of program //