element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • Product Groups
  • Store
    Store
    • Visit Your Store
    • Choose Another Store
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
Freedom development platform
  • Products
  • Dev Tools
  • Freedom development platform
  • More
  • Cancel
Freedom development platform
Blog Interfacing / Porting SDcard using MBED-OS[yotta] on Freedom board FRDM-K64F
  • Blog
  • Forum
  • Documents
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Freedom development platform requires membership for participation - click to join
Blog Post Actions
  • Subscribe by email
  • More
  • Cancel
  • Share
  • Subscribe by email
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: bheemarao
  • Date Created: 10 May 2016 5:13 AM Date Created
  • Views 278 views
  • Likes 2 likes
  • Comments 1 comment
  • frdm-k64f
  • mbed0s
  • sdcard
Related
Recommended

Interfacing / Porting SDcard using MBED-OS[yotta] on Freedom board FRDM-K64F

bheemarao
bheemarao
10 May 2016

In continuation to my earlier blog on demonstration of porting MBED-OS here is another blog on interfacing sdcard using mbed os yotta modules using frdm-k64f freedom board

 

Refer to my earlier blog HERE

Introduction and demonstration of porting MBED-OS [yotta]on FRDM-K64F Freedom board

 

We begin our tutorial from here

Yotta Installing on Windows

To install yotta on windows you can either use the one shot windows installer or install all the dependencies and yotta manually.

  1. 1.      Download the latest yotta windows installer.
  2. 2.      Run the installer.
  3. 3.      Click on Run Yotta shortcut on desktop or in start menu to run session with yotta path temporarily pre-pended to system path.

Let us proceed to test blinky example on yotta using  FRDM-K64FFRDM-K64F freedom board

After installing yotta you can see the icon on your desktop as shown

imageimage

Double click on this icon and you will see the command prompt window as shown

 

Create an application

First create a directory with the name we want, and cd into it. I have created directory by name “k64-sdcard-mbedos”

image

 

Next we need to create module description by typing command

>yotta init

 

Initialize the module with yotta init and fill in the details.

Make sure you select executable as the module type: “yes”

image

You should now have several folders and files in your project directory as shown below

image

 

  • The module.json file contains all the settings for your application; everything you just entered can be found in that file, so if you want to edit it later, for example to add a repository URL, that’s not a problem.
  • The /source directory contains all the source files.
  • The /test directory contains all tests you’ll write to test your module.
  • The /blinky directory is where header files for your application will be created.

 

 

Select a target board:

yotta can build the same code for multiple targets it therefore needs to be told which target every build is for. So now that you have created a basic application, you need to set the target.

 

In this example we are going to use the Freescale  FRDM-K64FFRDM-K64F board configured for building with gcc so you use target‘frdm-k64f-gcc

Type below command to build the target for frdm-K64 board

>yotta target frdm-k64f-gcc

 

image

To check that the target has been set correctly run the ‘target’ command without parameters. It shows what yotta is currently targeting for its builds:

The information for this target has been downloaded to a directory named /yotta_targets. Do not edit or modify files here because they can be modified without your knowledge.

image

 

Install dependencies:

 

When you have a dependency, you need to install it before you can build your own project. You need to do this from the module directory.

Now you need to install the dependencies. In this application, you have mbed-drivers as your dependency:

>yotta install mbed-drivers

 

You need to be online for the installation to work, because yotta downloads the modules from the yotta Registry.

yotta downloads and installs the modules to a directory named /yotta_modules. Do not edit or modify files here as they can be modified without your knowing.

image

To proceed further we need FAT File System driver and SD File System drivers which need to be download from below links:

https://developer.mbed.org/teams/mbed-official/code/FATFileSystem/

 

image

 

and other FAT file system from below link:

https://developer.mbed.org/teams/mbed-official/code/FATFileSystem/file/094f84646b9f/ChaN

image

 

SD File system from below link:

https://developer.mbed.org/teams/Akafugu/code/SDFileSystem/

image

 

After downloading the contents of FAT and SD File system unzip them and copy all the *.cpp files into a ‘source’ folder inside our project folder

“C:\k64-sdcard-mbedos\source”

image

Create one folder inside the project folder named ‘sdcard-header’

i.e “C:\k64-sdcard-mbedos\sdcard-header”

 

copy all the *.h header files of FAT/SD File system inside this folder:

image

 

Browse the project folder to “C:\k64-sdcard-mbedos\yotta_targets\frdm-k64f-gcc”

You will find“target.json file which contains the hardware information about the target board here in our case is  FRDM-K64FFRDM-K64F board below is the snap shot of the target.json file

image

Add the below spi port information in the file:

 

mosi > connected to PTE-3

miso > connected to PTE1

sclk > connected to PTE-2

cs > connected to PTE-4

 

image

Now we need to write our application code to access the sdcard present in FRDM-K64 board, here we are creating two folder “frdm1” and “frdm2” create ‘1.txt’ inside ‘frdm1’ folder and create ‘2.txt’ inside the folder ‘frdm2’

 

And also it will list all the files inside the directories

 

Note: the application code has been extracted from mbed developer platform

 

Below is the application code “main.cpp” which need to be created inside the source folder of our project.

“C:\k64-sdcard-mbedos\source”

image

 

 

#include "mbed-drivers/mbed.h"
#include "minar/minar.h"
#include "core-util/Event.h"
#include "sdcard-header/SDFileSystem.h"

//SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS 
SDFileSystem sd(YOTTA_CFG_HARDWARE_K64F_MICRO_SD_SPI_MOSI, 
            YOTTA_CFG_HARDWARE_K64F_MICRO_SD_SPI_MISO,
            YOTTA_CFG_HARDWARE_K64F_MICRO_SD_SPI_SCLK, 
            YOTTA_CFG_HARDWARE_K64F_MICRO_SD_SPI_CS,
            "sd");

Serial pc(USBTX, USBRX);
FILE *fp;

int file_copy(const char *src, const char *dst)
{
    int retval = 0;
    int ch;

    FILE *fpsrc = fopen(src, "r");   // src file
    FILE *fpdst = fopen(dst, "w");   // dest file
    
    while (1) {                  // Copy src to dest
        ch = fgetc(fpsrc);       // until src EOF read.
        if (ch == EOF) break;
        fputc(ch, fpdst);
    }
    fclose(fpsrc);
    fclose(fpdst);
  
    fpdst = fopen(dst, "r");     // Reopen dest to insure
    if (fpdst == NULL) {          // that it was created.
        retval = -1;           // Return error.
    } else {
        fclose(fpdst);
        retval = 0;              // Return success.
    }
    return retval;
}

uint32_t do_list(const char *fsrc)
{
    DIR *d = opendir(fsrc);
    struct dirent *p;
    uint32_t counter = 0;

    while ((p = readdir(d)) != NULL) {
        counter++;
        printf("%s\n", p->d_name);
    }
    closedir(d);
    return counter;
}
void do_remove(const char *fsrc)
{
    DIR *d = opendir(fsrc);
    struct dirent *p;
    char path[30] = {0};
    while((p = readdir(d)) != NULL) {
        strcpy(path, fsrc);
        strcat(path, "/");
        strcat(path, p->d_name);
        remove(path);
    }
    closedir(d);
    remove(fsrc);
}

using namespace minar;

class sdtest {
public:

   void start() {
    pc.printf("Initializing \n");
    wait(2);

    printf("\nCreating two folders. \n");
    mkdir("/sd/frdm1", 0777);
    mkdir("/sd/frdm2", 0777);

    fp = fopen("/sd/frdm1/1.txt", "w");
    if (fp == NULL) {
        pc.printf("Unable to write the file \n");
    } else {
        fprintf(fp, "1.txt in frdm 1");
        fclose(fp);
    }

    fp = fopen("/sd/frdm2/2.txt", "w");
    if (fp == NULL) {
        pc.printf("Unable to write the file \n");
    } else {
        fprintf(fp, "2.txt in frdm 2");
        fclose(fp);
    }

    printf("\nList all directories/files /sd.\n");
    do_list("/sd");

    printf("\nList all files within /sd/frdm1.\n");
    do_list("/sd/frdm1");


    printf("\nList all files within /sd/frdm2.\n");
    do_list("/sd/frdm2");

       int status = file_copy("/sd/frdm2/2.txt", "/sd/frdm1/2_copy.txt");
    if (status == -1) {
        printf("Error, file was not copied.\n");
    }
    printf("Removing frdm2 folder and 2.txt file inside.");
    remove("/sd/frdm2/2.txt");
    remove("/sd/frdm2");

    printf("\nList all directories/files /sd.\n");
    do_list("/sd");

    printf("\nList all files within /sd/frdm1.\n");
    do_list("/sd/frdm1");

    printf("\nEnd of complete Lab 5. \n");
}   
};

void app_start(int, char *[])
{
    static sdtest test;
 //minar::Scheduler::postCallback(sdtest).period(minar::milliseconds(2000));
 Scheduler::postCallback(mbed::util::FunctionPointer0<void>(&test, &sdtest::start).bind());   
}

 

 

Building the module

 

The build command (like the target command) must be performed from within the directory of the module you’re trying to build. It must also be performed after selecting a yotta target, as explained above.

 

To build the application:

 

Run the yotta build command in the top level directory:

>yt build

image

 

After the build is executed we can see the folder “build” is created which includes all the necessary headers required for the mbed os target. yotta compiles the binary to the /build folder. i.e “C:\k64-sdcard-mbedos\build\frdm-k64f-gcc\source”

“k64-sdcard-mbedos.bin” is an executable file need to be used.

 

image

Run the application on your board

Meanwhile open a hyperterminal with the mbed com port (com43 in my case) with baud rate of 9600 as shown

image

image

 

Connect FRDM-K64 (pre loaded with mbed driver) board to your computer over USB. It should appear as removable storage.

Copy the binary “k64-sdcard-mbedos.bin” or From your file explorer, drag and drop the file onto your mbed board.

The application is now on your board. This boards needs reset to start your application running.

 

Output execution:

image

image

 

The project folder is attached along with this for quick evaluation

 

Happy interfacing sdcard using  mbedos...  imageimageimageimage

 

 

Credits: I would like to thank my colleague Aravind Bayari who has helped me in this blog and has worked on IoT wearables.

Attachments:
k64-sdcard-mbedos.rar
k64-sdcard-mbedos-bin.rar
  • Sign in to reply
  • DAB
    DAB over 6 years ago

    Nice detailed tutorial.

     

    DAB

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2023 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • Facebook
  • Twitter
  • linkedin
  • YouTube