Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of ELEC351 by
SD_CARD.cpp
- Committer:
- thomasmorris
- Date:
- 2018-01-09
- Revision:
- 52:99915f5240b2
- Parent:
- 51:47f5db68500b
File content as of revision 52:99915f5240b2:
#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 { SD_Write = 0; //Set global variable SD_Write to 0 if(Log_Value==5){pc.printf("Closing File Ejecting SD Card\n");} //If logging is enabled, print debugging statement fclose(fp); //Close the opened file sd.deinit(); //Deinitialise the SD card LCD.Display_Clear(); //Clear the LCD Display LCD.DDRAM_Address(0x00); //Set the LCD Write Address to 0x00 LCD.Write_String("SD Card"); //Write "SD Card" to the first line of the LCD LCD.DDRAM_Address(0x40); //Set the LCD Write Address to 0x40 LCD.Write_String("Unmounted"); //Write "Unmounted" to the second line of the LCD LCD.DDRAM_Address(0x00); //Set the LCD Write Address to 0x00 pc.printf("SD Card Unmounted\n"); //Write "SD Card Unmounted" to the terminal } void SD_Write_Control(const char* SD_Write_FileToOpen, string Write_Append_Select) //Function to write data to a file { if (Write_Append_Select == "Append"){fp = fopen(SD_Write_FileToOpen,"a");} //If "Append" is selected, continue writing to existing file else if (Write_Append_Select == "Write" ){fp = fopen(SD_Write_FileToOpen, "w");} //If "Write" is selected, create and write to new file if(fp == NULL) //If file cannot be opened { 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(SD_FATAL); //Run error function - toggles red LED } if(Log_Value==5){pc.printf("File Opened\n");} //If logging is enabled, print debug statement 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 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 int temp_year = (Time_Pointer->tm_year+1900); //Assign the year to a temporary value 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 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,\n",temp_light); //Print the light into column 5 } fclose(fp); //Close the file } void SD_Card_Write() //Function to write to the SD card { if(SD_Write == 0) //If the SD card is not present { 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 { if(Log_Value==5){pc.printf("In SD_Card Thread\n");} //If logging is enabled, print debug statement 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 int File_temp_year = (File_Time_Pointer->tm_year+1900); //Assign the year to a temporary value //Create a const char* using year, month and day and create a file using that name char FileToOpen[50] = {}; snprintf (FileToOpen, sizeof(char) * sizeof(FileToOpen), "/sd/%d_%02d_%02d.csv",File_temp_year,File_temp_month,File_temp_day); if(fp = fopen(FileToOpen,"r")) //If the file already exists { fclose(fp); //Close the file SD_Write_Control(FileToOpen, "Append"); //Append the existing file } else //If the file doesn't exist { SD_Write_Control(FileToOpen, "Write"); //Create a new file } if(Log_Value==5){pc.printf("Dumped data to SD Card\n");} //If logging is enabled, print debug statement } }