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. Download the latest yotta windows installer.
- 2. Run the installer.
- 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
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”
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”
You should now have several folders and files in your project directory as shown below
- 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
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.
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.
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/
and other FAT file system from below link:
https://developer.mbed.org/teams/mbed-official/code/FATFileSystem/file/094f84646b9f/ChaN
SD File system from below link:
https://developer.mbed.org/teams/Akafugu/code/SDFileSystem/
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”
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:
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
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
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”
#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
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.
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
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:
The project folder is attached along with this for quick evaluation
Happy interfacing sdcard using mbedos...
Credits: I would like to thank my colleague Aravind Bayari who has helped me in this blog and has worked on IoT wearables.