File Creation on microSD Card Demo - MBED + SmartGPU2 board

Dependencies:   SMARTGPU2 mbed

Committer:
emmanuelchio
Date:
Thu Apr 17 21:44:20 2014 +0000
Revision:
1:4340adf495e4
Parent:
0:555eecb74312
SmartGPU2 FileCreation_SG2 demo- Please select(uncomment) your smartGPU2 board under SMARTGPU2.h file before compiling!!!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emmanuelchio 1:4340adf495e4 1 /**************************************************************************************/
emmanuelchio 0:555eecb74312 2 /**************************************************************************************/
emmanuelchio 0:555eecb74312 3 /*SMARTGPU2 intelligent embedded graphics processor unit
emmanuelchio 1:4340adf495e4 4 those examples are for using the SMARTGPU2 with the mbed microcontoller, just connect tx,rx,and reset
emmanuelchio 0:555eecb74312 5 Board:
emmanuelchio 1:4340adf495e4 6 http://www.vizictechnologies.com/
emmanuelchio 0:555eecb74312 7
emmanuelchio 0:555eecb74312 8 www.vizictechnologies.com
emmanuelchio 1:4340adf495e4 9 Vizic Technologies copyright 2014 */
emmanuelchio 1:4340adf495e4 10 /**************************************************************************************/
emmanuelchio 0:555eecb74312 11 /**************************************************************************************/
emmanuelchio 0:555eecb74312 12
emmanuelchio 0:555eecb74312 13 /********************************************************
emmanuelchio 0:555eecb74312 14 This simple sketch does the next:
emmanuelchio 0:555eecb74312 15 1.- try open the "test file.txt" file in the microSD root path in write only mode
emmanuelchio 0:555eecb74312 16 2.- if doesn't exist, create the file - if exist overwritte the file
emmanuelchio 0:555eecb74312 17 3.- open again the "test file.txt" file in the microSD root path in write only mode
emmanuelchio 0:555eecb74312 18 4.- write "Data Written by the SmartDRIVE Processor" to the .txt file
emmanuelchio 0:555eecb74312 19 5.- save contents and close file
emmanuelchio 0:555eecb74312 20 6.- open again the file in read only mode
emmanuelchio 0:555eecb74312 21 7.- read from file to a buffer
emmanuelchio 0:555eecb74312 22 8.- verify data read to be equal to message
emmanuelchio 0:555eecb74312 23 9.- if different erase the created file else keep the file
emmanuelchio 0:555eecb74312 24 10- umount drive
emmanuelchio 0:555eecb74312 25 11.- end
emmanuelchio 0:555eecb74312 26
emmanuelchio 0:555eecb74312 27 - remove microSD card and search for the file on a PC with the written contents
emmanuelchio 0:555eecb74312 28 ********************************************************/
emmanuelchio 0:555eecb74312 29
emmanuelchio 0:555eecb74312 30 #include "mbed.h"
emmanuelchio 0:555eecb74312 31 #include "SMARTGPU2.h"
emmanuelchio 0:555eecb74312 32
emmanuelchio 0:555eecb74312 33 SMARTGPU2 lcd(TXPIN,RXPIN,RESETPIN); //create our object called "lcd"
emmanuelchio 0:555eecb74312 34
emmanuelchio 0:555eecb74312 35 FILERESULT res; //create the variable that will store all SMARTGPU2 commands responses
emmanuelchio 0:555eecb74312 36
emmanuelchio 0:555eecb74312 37 char message[41]="Data Written by the SmartGPU 2 Processor";
emmanuelchio 0:555eecb74312 38 unsigned int row=10;
emmanuelchio 0:555eecb74312 39
emmanuelchio 0:555eecb74312 40 //function that loops forever on error
emmanuelchio 0:555eecb74312 41 void die(FILERESULT response){ //if the response is different than OK, print and loop forever
emmanuelchio 0:555eecb74312 42 NUMBEROFBYTES charsPrint;
emmanuelchio 0:555eecb74312 43 if(response!=F_OK){
emmanuelchio 0:555eecb74312 44 lcd.string(10,row,319,239,"Error... forever loop @",&charsPrint);
emmanuelchio 0:555eecb74312 45 while(1);
emmanuelchio 0:555eecb74312 46 }
emmanuelchio 0:555eecb74312 47 }
emmanuelchio 0:555eecb74312 48
emmanuelchio 0:555eecb74312 49 /***************************************************/
emmanuelchio 0:555eecb74312 50 /***************************************************/
emmanuelchio 0:555eecb74312 51 void initializeSmartGPU2(void){ //Initialize SMARTGPU2 Board
emmanuelchio 0:555eecb74312 52 lcd.reset(); //physically reset SMARTGPU2
emmanuelchio 0:555eecb74312 53 lcd.start(); //initialize the SMARTGPU2 processor
emmanuelchio 0:555eecb74312 54 }
emmanuelchio 0:555eecb74312 55
emmanuelchio 0:555eecb74312 56 /***************************************************/
emmanuelchio 0:555eecb74312 57 /***************************************************/
emmanuelchio 0:555eecb74312 58 /***************************************************/
emmanuelchio 0:555eecb74312 59 /***************************************************/
emmanuelchio 0:555eecb74312 60 int main() {
emmanuelchio 0:555eecb74312 61 NUMBEROFBYTES charsPrinted;
emmanuelchio 0:555eecb74312 62 char buffer[50]={0};
emmanuelchio 0:555eecb74312 63 unsigned int writtenBytes=0, readbytes=0, i=0;
emmanuelchio 0:555eecb74312 64
emmanuelchio 0:555eecb74312 65 initializeSmartGPU2(); //Init communication with SmartGPU2 board
emmanuelchio 0:555eecb74312 66
emmanuelchio 0:555eecb74312 67 //strings config
emmanuelchio 0:555eecb74312 68 lcd.setTextColour(GREEN);
emmanuelchio 0:555eecb74312 69 lcd.string(10,row,319,239,"FAT file open, read, write demo!",&charsPrinted); row+=15;
emmanuelchio 0:555eecb74312 70 lcd.string(10,row,319,239,"Open File <test file.txt> in WRITE mode...",&charsPrinted); row+=15;
emmanuelchio 0:555eecb74312 71
emmanuelchio 0:555eecb74312 72 //try to open the file
emmanuelchio 0:555eecb74312 73 res=lcd.SDFopenFile("test file.txt", WRITEONLY, WORKSPACE0); //Try to open a file called "testFile.txt" in write only mode in the workspace block 0
emmanuelchio 0:555eecb74312 74 if(res!=F_OK){ //If the file doesn't Open is because it doesn't exist
emmanuelchio 0:555eecb74312 75 lcd.string(10,row,319,239,"File doesn't exist, creating file...",&charsPrinted); row+=15;
emmanuelchio 0:555eecb74312 76 res=lcd.SDFnewFile("test file.txt"); //Try to create the file
emmanuelchio 0:555eecb74312 77 die(res); //If any error loop forever
emmanuelchio 0:555eecb74312 78 res=lcd.SDFopenFile("test file.txt", WRITEONLY, WORKSPACE0); //Try to open the created file
emmanuelchio 0:555eecb74312 79 die(res); //If any error loop forever
emmanuelchio 0:555eecb74312 80 }
emmanuelchio 0:555eecb74312 81
emmanuelchio 0:555eecb74312 82 //Up to here the file exist and is open
emmanuelchio 0:555eecb74312 83 lcd.string(10,row,319,239,"File Successfully Open in WRITE mode...",&charsPrinted); row+=15;
emmanuelchio 0:555eecb74312 84 lcd.string(10,row,319,239,"Write <Data Written by the SmartGPU 2 Processor>",&charsPrinted); row+=15;
emmanuelchio 0:555eecb74312 85 res=lcd.SDFwriteFile(message, sizeof(message), &writtenBytes, WORKSPACE0); //write to the open file in WORKSPACE0 size of message in bytes and store the successfully written Bytes on writtenBytes variable
emmanuelchio 0:555eecb74312 86 die(res); //If any error loop forever
emmanuelchio 0:555eecb74312 87 lcd.SDFsaveFile(WORKSPACE0); //Save changes in the file contained in WORKSPACE0
emmanuelchio 0:555eecb74312 88 lcd.string(10,row,319,239,"Closing File...",&charsPrinted); row+=15;
emmanuelchio 0:555eecb74312 89 lcd.SDFcloseFile(WORKSPACE0); //Close the file --------------------
emmanuelchio 0:555eecb74312 90
emmanuelchio 0:555eecb74312 91 //Now lets verify contents
emmanuelchio 0:555eecb74312 92 //open again the file in read only mode
emmanuelchio 0:555eecb74312 93 lcd.string(10,row,319,239,"Open File <test file.txt> in READ mode...",&charsPrinted); row+=15;
emmanuelchio 0:555eecb74312 94 res=lcd.SDFopenFile("test file.txt", READONLY, WORKSPACE0); //Try to open again the file read only mode in the workspace block 0
emmanuelchio 0:555eecb74312 95 die(res); //If any error loop forever
emmanuelchio 0:555eecb74312 96 //read the file
emmanuelchio 0:555eecb74312 97 lcd.string(10,row,319,239,"File Successfully Open in READ mode...",&charsPrinted); row+=15;
emmanuelchio 0:555eecb74312 98 lcd.string(10,row,319,239,"Read bytes from file to buffer...",&charsPrinted); row+=15;
emmanuelchio 0:555eecb74312 99 res=lcd.SDFreadFile(buffer, sizeof(message), &readbytes, WORKSPACE0); //read size of message in bytes from the open file in WORKSPACE0 and store the successfully read Bytes on readbytes variable
emmanuelchio 0:555eecb74312 100 die(res); //If any error loop forever
emmanuelchio 0:555eecb74312 101 lcd.string(10,row,319,239,"Closing File...",&charsPrinted); row+=15;
emmanuelchio 0:555eecb74312 102 lcd.SDFcloseFile(WORKSPACE0); //Close the file --------------------
emmanuelchio 0:555eecb74312 103
emmanuelchio 0:555eecb74312 104 //check contents
emmanuelchio 0:555eecb74312 105 lcd.string(10,row,319,239,"Verify/Compare contents...",&charsPrinted); row+=15;
emmanuelchio 0:555eecb74312 106 for(i=0;i<sizeof(message);i++){
emmanuelchio 0:555eecb74312 107 if(message[i]!=buffer[i]){ //if contents are different
emmanuelchio 0:555eecb74312 108 lcd.string(10,row,319,239,"Contents differ, erasing the created file...END",&charsPrinted); row+=10;
emmanuelchio 0:555eecb74312 109 lcd.SDFeraseDirFile("test file.txt"); //erase the File
emmanuelchio 0:555eecb74312 110 while(1);
emmanuelchio 0:555eecb74312 111 }
emmanuelchio 0:555eecb74312 112 }
emmanuelchio 0:555eecb74312 113 lcd.string(10,row,319,239,"Contents are equal...END",&charsPrinted);
emmanuelchio 0:555eecb74312 114 while(1);
emmanuelchio 0:555eecb74312 115 }