File Creation on microSD Card Demo - MBED + SmartGPU2 board

Dependencies:   SMARTGPU2 mbed

Committer:
emmanuelchio
Date:
Wed Jul 10 03:28:18 2013 +0000
Revision:
0:555eecb74312
Child:
1:4340adf495e4
FIle Creation - MBED + SmartGPU2 board

Who changed what in which revision?

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