A collection of examples organized from basics to advanced.
Dependencies: mbed SDFileSystem
Mbed online compiler has no facility to easily manage a lot of programs or organized them in to related folders. This makes creating an examples and sample pack difficult.
This repository contains a single main.cpp file (which does very little), and a BuildOptions.h file. Simply uncomment the example you would like to compile from the build options. Each example is wrapped in a compiler directive.
If the directive does not include a description comment, it likely does not exist yet. If you would like to contribute to the Examples project, please contact me or fork and issue a pull request.
Revision 2:17a5c34b3a79, committed 2019-07-09
- Comitter:
- epremeaux
- Date:
- Tue Jul 09 02:23:18 2019 +0000
- Parent:
- 1:9a043ee174de
- Commit message:
- Added SD card examples. Had to roll back the MBED library to maintain SDFileSystem compatability
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/02_Digital/Digital_In.cpp Tue Jul 09 02:23:18 2019 +0000 @@ -0,0 +1,14 @@ +#include "mbed.h" + +DigitalOut myled(LED1); +DigitalIn boton(D2); + +int main() { + while(1) { + if (boton == 0){ + myled = 1; + }else{ + myled = 0; + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/09_Files_SD/SD_Index_File.cpp Tue Jul 09 02:23:18 2019 +0000 @@ -0,0 +1,44 @@ +/* + * Opens index.txt, reads a number from the file (as charactor binary value) + * Closes that file, then creates a file name string by adding one to the index number + * Creates and opens a new file with the file name string, and writes some text before closing. + * + */ + + +#ifdef COMPILE_SD_Index_File + +#include "SDFileSystem.h" + +SDFileSystem sd(PB_5, PB_4, PB_3, PA_11, "sd"); // MOSI, MISO SCLK, CS, "name" + +int last_file_num; + + +int main() { + FILE *IndexFile = fopen("/sd/index.txt", "r"); // get the last file number from the index file + last_file_num = fgetc(IndexFile); + printf("Got: %i \n", last_file_num); + fclose(IndexFile); + last_file_num++; + IndexFile = fopen("/sd/index.txt", "w"); // wrte the new number into the index file + fputc(last_file_num, IndexFile); + fclose(IndexFile); + + char filename[12]; + sprintf(filename, "/sd/%i.txt", last_file_num); // build new filename to open + + FILE *File = fopen(filename, "w"); + if(File == NULL) { + printf("Could not open file to write\n"); + } + else { + printf("SD card file opened: %s\n", filename); + fprintf(File, "heres some data!"); + fclose(File); + printf("closed\n"); + } +} + +#endif + \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/09_Files_SD/SD_Speed_Test.cpp Tue Jul 09 02:23:18 2019 +0000 @@ -0,0 +1,146 @@ +/* https://os.mbed.com/users/neilt6/code/SDFileSystem/ + * + * Mounts an SD card, creates a buffer of random data, then writes and reads + * back that data. Finishes with a report of the size of the buffer file + * and read / write speeds. + * + * SDFileSystem library requires MBED Release 165, dated Feb 20, 2019. Updates will break it. + * If having difficulty, import the link above as independant program. + */ + +#ifdef COMPILE_SD_Speed_Test + +#include "SDFileSystem.h" + +Timer timer; +DigitalIn button(D7, PullUp); +// MOSI, MISO, CLK, CS, "name", detect +SDFileSystem sd(PB_5, PB_4, PB_3, PA_11, "sd", PA_8, SDFileSystem::SWITCH_NEG_NC, 25000000); +char buffer[4096]; + +void writeTest() +{ + //Test write performance by creating a 1MB file + printf("Testing %iB write performance...", sizeof(buffer)); + FileHandle* file = sd.open("Test File.bin", O_WRONLY | O_CREAT | O_TRUNC); + if (file != NULL) { + timer.start(); + for (int i = 0; i < (1048576 / sizeof(buffer)); i++) { + if (file->write(buffer, sizeof(buffer)) != sizeof(buffer)) { + timer.stop(); + printf("write error!\n"); + timer.reset(); + return; + } + } + timer.stop(); + if (file->close()) + printf("failed to close file!\n"); + else + printf("done!\n\tResult: %.2fKB/s\n", 1024 / (timer.read_us() / 1000000.0)); + timer.reset(); + } else { + printf("failed to create file!\n"); + } +} + +void readTest() +{ + //Test read performance by reading the 1MB file created by writeTest() + printf("Testing %iB read performance...", sizeof(buffer)); + FileHandle* file = sd.open("Test File.bin", O_RDONLY); + if (file != NULL) { + timer.start(); + int iterations = 0; + while (file->read(buffer, sizeof(buffer)) == sizeof(buffer)) + iterations++; + timer.stop(); + if (iterations != (1048576 / sizeof(buffer))) + printf("read error!\n"); + else if (file->close()) + printf("failed to close file!\n"); + else if (sd.remove("Test File.bin")) + printf("failed to delete file!\n"); + else + printf("done!\n\tResult: %.2fKB/s\n", 1024 / (timer.read_us() / 1000000.0)); + timer.reset(); + } else { + printf("failed to open file!\n"); + } +} + +int main() +{ + printf("SD Card Tester...\n"); + //Configure CRC, large frames, and write validation + sd.crc(true); + sd.large_frames(true); + sd.write_validation(true); + + printf("Filling buffer with random data for write test...\n"); + //Fill the buffer with random data for the write test + srand(time(NULL)); + for (int i = 0; i < sizeof(buffer); i++) + buffer[i] = rand(); + printf("Done. Press button to test\n"); + + while(1) { + //Simple button debouncing + wait(0.5); + + //Print the start message + printf("\nStarting Tests: "); + + //Wait for the button to be pressed + while(button); + + //Make sure a card is present + if (!sd.card_present()) { + printf("\nNo card present!\n"); + continue; + } + + //Try to mount the SD card + printf("\nMounting SD card..."); + if (sd.mount() != 0) { + printf("failed!\n"); + continue; + } + printf("success!\n"); + + //Display the card type + printf("\tCard type: "); + SDFileSystem::CardType cardType = sd.card_type(); + if (cardType == SDFileSystem::CARD_NONE) + printf("None\n"); + else if (cardType == SDFileSystem::CARD_MMC) + printf("MMC\n"); + else if (cardType == SDFileSystem::CARD_SD) + printf("SD\n"); + else if (cardType == SDFileSystem::CARD_SDHC) + printf("SDHC\n"); + else + printf("Unknown\n"); + + //Display the card capacity + printf("\tSectors: %u\n", sd.disk_sectors()); + printf("\tCapacity: %.1fMB\n", sd.disk_sectors() / 2048.0); + + /*//Format the card + printf("Formatting SD card..."); + if (sd.format() != 0) { + printf("failed!\n"); + continue; + } + printf("success!\n");*/ + + //Perform a read/write test + writeTest(); + readTest(); + + //Unmount the SD card + sd.unmount(); + } +} + +#endif \ No newline at end of file
--- a/Digital_In.cpp Fri Jul 05 04:52:18 2019 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,14 +0,0 @@ -#include "mbed.h" - -DigitalOut myled(LED1); -DigitalIn boton(D2); - -int main() { - while(1) { - if (boton == 0){ - myled = 1; - }else{ - myled = 0; - } - } -}
--- a/buildOptions.h Fri Jul 05 04:52:18 2019 +0000 +++ b/buildOptions.h Tue Jul 09 02:23:18 2019 +0000 @@ -1,6 +1,14 @@ -// uncomment only ONE file to build in the example set -// if there is no description, there is likely no code file yet. -// (feel free to support the project by adding one and issuing a pull request ;) +/* uncomment only ONE file to build in the example set + * if there is no description, there is likely no code file yet. + * (feel free to support the project by adding one and issuing a pull request ;) + * + * BE CAREFUL when updating the MBED library! + * New versions often break libraries. + * Example: the included SDFileSystem library requires Release 165, dated Feb 20, 2019. Updates will break it. + * + */ + + // 01_Basics: //#define COMPILE_Blink @@ -13,7 +21,7 @@ // 02_Digital: //#define COMPILE_Button_Interrupt // reads a button using interrupts //#define COMPILE_BlinkWithoutDelay -#define COMPILE_Debounce // reads button using interrupt and timer + callback to debounce the input +//#define COMPILE_Debounce // reads button using interrupt and timer + callback to debounce the input //#define COMPILE_DigitalInputPullup //#define COMPILE_StateChangeDetection //#define COMPILE_ToneKeyboard @@ -89,8 +97,9 @@ // 09_Files_SD: +//#define COMPILE_SD_Speed_Test // Mounts card, creates file, builds random data buffer, write, read, then report speeds +#define COMPILE_SD_Index_File // reads an index.txt file for an integer (as charactor binary), creates new file with that number+1, writes data. Closes. //#define COMPILE_SD_Hello -//#define COMPILE_SD_SpeedTest //#define COMPILE_FatFileSystem //#define COMPILE_Index_and_CSV
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libraries/SDFileSystem.lib Tue Jul 09 02:23:18 2019 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/neilt6/code/SDFileSystem/#e4d2567200db
--- a/mbed.bld Fri Jul 05 04:52:18 2019 +0000 +++ b/mbed.bld Tue Jul 09 02:23:18 2019 +0000 @@ -1,1 +1,1 @@ -https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400 \ No newline at end of file +http://mbed.org/users/mbed_official/code/mbed/builds/2241e3a39974 \ No newline at end of file