Mathew Swabey / SDCard

Dependents:   ELEC350_Project2 SDcard

Files at this revision

API Documentation at this revision

Comitter:
Swabey89
Date:
Wed Dec 19 15:29:27 2018 +0000
Parent:
9:113f4934b907
Child:
11:89960c1f2234
Commit message:
Added locks

Changed in this revision

SDCard.cpp Show annotated file Show diff for this revision Revisions of this file
SDCard.hpp Show annotated file Show diff for this revision Revisions of this file
--- a/SDCard.cpp	Wed Dec 19 13:09:15 2018 +0000
+++ b/SDCard.cpp	Wed Dec 19 15:29:27 2018 +0000
@@ -7,7 +7,9 @@
         
     //Initialise the SD card
     if (sd.init() != 0) {
+        printlock.lock();
         pc->printf("WARNING:SD CARD INITIALISATION FAILED\n\r");
+        printlock.unlock();
         sd_init = false;
         //lcd.cls();
         //lcd.printf("CANNOT INIT SD");        
@@ -17,51 +19,61 @@
     {
         //Create a filing system for SD Card
         fs = new FATFileSystem("sd", &sd);
+        printlock.lock();
         pc->printf("SD CARD INITIALISED\n\r");
+        printlock.unlock();
 
         //Open to WRITE    
         char fileDate[30];
+        timeLock.lock();
         seconds = time(NULL);
         timeData = localtime(&seconds);
         set_time(mktime(timeData));
         strftime(fileDate, 30, "sd/log_%d_%m_%y.csv", timeData);
-        
+        timeLock.unlock();
         fp = fopen(fileDate,"a");
         
         if (fp == NULL) 
         {
+            printlock.lock();
             pc->printf("WARNING: COULD NOT OPEN FILE FOR WRITE\n\r");
+            printlock.unlock();
             //lcd.cls();
             //lcd.printf("CANNOT OPEN FILE\n\n");
             //errorCode(FATAL);
         }
         else
         {
-            pc->printf("FILE OPEN FOR WRITING\n\r"); 
+            printlock.lock();
+            pc->printf("FILE OPEN FOR WRITING\n\n\n\r");
+            printlock.unlock();
             sd_init = true;
         }     
         fclose(fp);
     }
-    
-    //Last message before sampling begins - probably remove
-    lcd.cls();
-    lcd.printf("SD CARD INIT...\n\n");
 }
 
 void SDread(int n)
 {
     //Read n samples from the SD card
+    printlock.lock();
     if (n == -1) puts("Received command to read all");
     else printf("Received command to read %d\n", n);
+    printlock.unlock();
 }
 
 void SDdelete(int n)
 {
     //Delete n samples from the SD card
+    printlock.lock();
     if (n == -1) puts("Received command to delete all");
-    else printf("Received command to delete %d\n", n);   
+    else printf("Received command to delete %d\n", n); 
+    printlock.unlock();  
 }
 
+
+
+//UNUSED
 void SDaddSample(double temp, double pressure)
 {
     //Add the sampled data to the SD card    
@@ -82,22 +94,39 @@
     while(true)
     {
         Thread::signal_wait(SIGNAL_SD);
-        
-        //Change state of SD card
+                
         if (sd_init)
         {
             fclose(fp);
             sd.deinit();
+            printlock.lock();
             pc->printf("SD CARD UNMOUNTED\n\r");
+            printlock.unlock();
+            
+            LCDlock.lock();
             lcd.cls();
-            lcd.printf("Unmounted..\n\n");
+            lcd.printf("Unmounted..");
+            Thread::wait(5000);
+            LCDlock.unlock();
+            
             sd_init = false;
               
         }
         else
         {
-            //try to init, if failed say cannot init, if pass then say init passed and change state of sd_init
             SDcard();
+            LCDlock.lock();
+            lcd.cls();
+            if (sd_init)
+            {
+                lcd.printf("SD mounted..");
+            }
+            else
+            {
+                lcd.printf("SD FAILED..");
+            }
+            Thread::wait(5000);
+            LCDlock.unlock();        
         } 
          
     }   
--- a/SDCard.hpp	Wed Dec 19 13:09:15 2018 +0000
+++ b/SDCard.hpp	Wed Dec 19 15:29:27 2018 +0000
@@ -13,6 +13,11 @@
 extern FILE* fp;
 extern FATFileSystem* fs;
 
+//TEST SD
+extern Mutex printlock;
+extern Mutex LCDlock;
+extern Mutex timeLock;
+
 void SDcard(void);
 void SDread(int n);
 void SDaddSample(double temp, double pressure);