Plymouth ELEC351 Group T / Mbed OS ELEC351_Group_T

Fork of ELEC351 by Plymouth ELEC351 Group T

Revision:
52:99915f5240b2
Parent:
51:47f5db68500b
--- a/SD_CARD.cpp	Tue Jan 09 15:15:08 2018 +0000
+++ b/SD_CARD.cpp	Tue Jan 09 22:27:49 2018 +0000
@@ -1,13 +1,50 @@
 #include "SD_CARD.hpp"
+
 FILE* fp;                           //Declaration of FILE pointer named fp
 InterruptIn SD_CARD_DETECT(D0);     //Declaration of Interrupt to detect if the SD Card is present
-   
 
 bool SD_Write = 1;                  //Declare global variable SD_Write and initialise it to 0
+bool SD_CARD_PRESENT = 1;           //Sets SD Card Present by default
+
+//Mutex Lock
+Mutex SD_CARD_Lock;
+
 void SD_Init()                      //Function to initialise the SD Card
 {
-    
-}
+    if(SD_CARD_DETECT.read() == 0)  //Check to see when the SD card is re-inserted
+    {
+        if ( sd.init() != 0)        //Checks if the SD card is initialised
+        {
+            printf("SD Init failed \n");
+            LCD.Display_Clear();
+            LCD.Write_String("CANNOT INIT SD");
+            errorCode(SD_FATAL);    //Throws SD_FATAL code
+        }
+        //Create a filing system for SD Card
+        FATFileSystem fs("sd", &sd);     
+                
+        //Open to WRITE
+                    
+        FILE* fp = fopen("/sd/test.csv","w");
+        if (fp == NULL) 
+        {
+            error("Could not open file for write\n");
+            LCD.Display_Clear();
+            LCD.Write_String("CANNOT OPEN FILE");
+            errorCode(SD_FATAL);    //Throws SD_FATAL code
+        }
+        //Close File
+        fclose(fp);
+        LCD.Display_Clear();
+        LCD.DDRAM_Address(0x00);
+        LCD.Write_String("SD CARD");
+        LCD.DDRAM_Address(0x40);
+        LCD.Write_String("Inserted");
+        pc.printf("SD CARD INSERTED\n");
+        SD_Write = 1;
+        return;                     //Exits Initialisation routine
+    }   
+}           
 
 void SD_Card_Eject()                                                        //Function to eject the SD Card
 {
@@ -34,7 +71,7 @@
         error("Could not open file for write in SD_Card\n");                                        //Print error message to terminal
         LCD.Display_Clear();                                                                        //Clear the LCD display
         LCD.Write_String("CANNOT OPEN FILE");                                                       //Write error message to LCD
-        errorCode(FATAL);                                                                           //Run error function - toggles red LED
+        errorCode(SD_FATAL);                                                                           //Run error function - toggles red LED
     }                             
                 
     if(Log_Value==5){pc.printf("File Opened\n");}                                                   //If logging is enabled, print debug statement  
@@ -42,8 +79,14 @@
     for(int SD_Card_Data_Pointer = 0; SD_Card_Data_Pointer != mailsize; SD_Card_Data_Pointer++)     //For all addresses in the DATA buffer
     {
         if(Log_Value==5){pc.printf("Copying from address: %d\n", SD_Card_Data_Pointer);}            //If logging is enabled, print debug statement
-                        
-        time_t Time = Data_Buffer[SD_Card_Data_Pointer].get_time();                                 //Store the time from the DATA buffer 
+        
+        SD_CARD_Lock.lock();                                                                        //Apply MUTEX lock                
+        time_t Time = Data_Buffer[SD_Card_Data_Pointer].get_time();                                 //Store the time from the DATA buffer
+        float temp_temperature = Data_Buffer[SD_Card_Data_Pointer].get_temperature();               //Assign the temperature to a temporary value            
+        float temp_pressure = Data_Buffer[SD_Card_Data_Pointer].get_pressure();                     //Assign the pressure to a temporary value
+        float temp_light = Data_Buffer[SD_Card_Data_Pointer].get_light();                           //Assign the light to a temporary value
+        SD_CARD_Lock.unlock();                                                                      //Release MUTEX lock
+        
         tm* Time_Pointer = localtime(&Time);                                                        //Create pointer to stored time
         int temp_day = Time_Pointer->tm_mday;                                                       //Assign the day to a temporary value
         int temp_month = (Time_Pointer->tm_mon+1);                                                  //Assign the month to a temporary value
@@ -51,15 +94,12 @@
         int temp_hours = Time_Pointer->tm_hour;                                                     //Assign the hour to a temporary value 
         int temp_minute = Time_Pointer->tm_min;                                                     //Assign the minute to a temporary value
         int temp_seconds = Time_Pointer->tm_sec;                                                    //Assign the second to a temporary value
-        float temp_temperature = Data_Buffer[SD_Card_Data_Pointer].get_temperature();               //Assign the temperature to a temporary value            
-        float temp_pressure = Data_Buffer[SD_Card_Data_Pointer].get_pressure();                     //Assign the pressure to a temporary value
-        float temp_light = Data_Buffer[SD_Card_Data_Pointer].get_light();                           //Assign the light to a temporary value
                         
         fprintf(fp, "Date: %02d/%02d/%d,",temp_day,temp_month,temp_year);                           //Print the date into column 1
         fprintf(fp, "Time: %02d:%02d:%02d,",temp_hours,temp_minute, temp_seconds);                  //Print the time into column 2
         fprintf(fp, "Temperature: %1.1f,",temp_temperature);                                        //Print the temperature into column 3
         fprintf(fp, "Pressure: %1.1f,",temp_pressure);                                              //Print the pressure into column 4
-        fprintf(fp, "Light: %5.3f,",temp_light);                                                    //Print the light into column 5
+        fprintf(fp, "Light: %5.3f,\n",temp_light);                                                  //Print the light into column 5
     }            
     fclose(fp);                                                                                     //Close the file
      
@@ -68,7 +108,7 @@
 {
     if(SD_Write == 0)                                                                       //If the SD card is not present
     {
-        if(Log_Value==5){pc.printf("SD Has Been Ejected Please do a Hardware Restart\n");}  //If logging is enabled, print debug statement        
+        if(Log_Value==5){pc.printf("SD Has Been Ejected\n");}  //If logging is enabled, print debug statement        
         return;                                                                             //Exit function
     }
     if(Write_Pointer == mailsize - 1)                                                       //If the end of the buffer is being written to
@@ -77,7 +117,9 @@
     
             FATFileSystem fs("sd", &sd);                                                    //Sets working directory as "sd"
             
+            SD_CARD_Lock.lock();                                                            //Apply MUTEX lock    
             time_t File_Time = Data_Buffer[Write_Pointer - 1].get_time();                   //Store the time from the data buffer
+            SD_CARD_Lock.unlock();                                                          //Release MUTEX lock 
             tm* File_Time_Pointer = localtime(&File_Time);                                  //Create pointer to stored time
             int File_temp_day = File_Time_Pointer->tm_mday;                                 //Assign the day to a temporary value
             int File_temp_month = (File_Time_Pointer->tm_mon+1);                            //Assign the month to a temporary value