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@51:47f5db68500b, 2018-01-09 (annotated)
- Committer:
- thomasmorris
- Date:
- Tue Jan 09 15:15:08 2018 +0000
- Revision:
- 51:47f5db68500b
- Parent:
- 50:3d61ca637399
- Child:
- 52:99915f5240b2
Commenting Left; Sort out LCD number problem.;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
thomasmorris | 47:6d128e500875 | 1 | #include "SD_CARD.hpp" |
thomasmorris | 51:47f5db68500b | 2 | FILE* fp; //Declaration of FILE pointer named fp |
thomasmorris | 51:47f5db68500b | 3 | InterruptIn SD_CARD_DETECT(D0); //Declaration of Interrupt to detect if the SD Card is present |
thomasmorris | 49:d51f96a46cc3 | 4 | |
thomasmorris | 49:d51f96a46cc3 | 5 | |
thomasmorris | 51:47f5db68500b | 6 | bool SD_Write = 1; //Declare global variable SD_Write and initialise it to 0 |
thomasmorris | 51:47f5db68500b | 7 | void SD_Init() //Function to initialise the SD Card |
thomasmorris | 47:6d128e500875 | 8 | { |
thomasmorris | 47:6d128e500875 | 9 | |
thomasmorris | 47:6d128e500875 | 10 | } |
thomasmorris | 51:47f5db68500b | 11 | |
thomasmorris | 51:47f5db68500b | 12 | void SD_Card_Eject() //Function to eject the SD Card |
thomasmorris | 51:47f5db68500b | 13 | { |
thomasmorris | 51:47f5db68500b | 14 | SD_Write = 0; //Set global variable SD_Write to 0 |
thomasmorris | 51:47f5db68500b | 15 | if(Log_Value==5){pc.printf("Closing File Ejecting SD Card\n");} //If logging is enabled, print debugging statement |
thomasmorris | 51:47f5db68500b | 16 | fclose(fp); //Close the opened file |
thomasmorris | 51:47f5db68500b | 17 | sd.deinit(); //Deinitialise the SD card |
thomasmorris | 51:47f5db68500b | 18 | LCD.Display_Clear(); //Clear the LCD Display |
thomasmorris | 51:47f5db68500b | 19 | LCD.DDRAM_Address(0x00); //Set the LCD Write Address to 0x00 |
thomasmorris | 51:47f5db68500b | 20 | LCD.Write_String("SD Card"); //Write "SD Card" to the first line of the LCD |
thomasmorris | 51:47f5db68500b | 21 | LCD.DDRAM_Address(0x40); //Set the LCD Write Address to 0x40 |
thomasmorris | 51:47f5db68500b | 22 | LCD.Write_String("Unmounted"); //Write "Unmounted" to the second line of the LCD |
thomasmorris | 51:47f5db68500b | 23 | LCD.DDRAM_Address(0x00); //Set the LCD Write Address to 0x00 |
thomasmorris | 51:47f5db68500b | 24 | pc.printf("SD Card Unmounted\n"); //Write "SD Card Unmounted" to the terminal |
thomasmorris | 51:47f5db68500b | 25 | |
thomasmorris | 48:244d6d81bb52 | 26 | } |
thomasmorris | 51:47f5db68500b | 27 | void SD_Write_Control(const char* SD_Write_FileToOpen, string Write_Append_Select) //Function to write data to a file |
thomasmorris | 47:6d128e500875 | 28 | { |
thomasmorris | 51:47f5db68500b | 29 | if (Write_Append_Select == "Append"){fp = fopen(SD_Write_FileToOpen,"a");} //If "Append" is selected, continue writing to existing file |
thomasmorris | 51:47f5db68500b | 30 | else if (Write_Append_Select == "Write" ){fp = fopen(SD_Write_FileToOpen, "w");} //If "Write" is selected, create and write to new file |
thomasmorris | 51:47f5db68500b | 31 | |
thomasmorris | 51:47f5db68500b | 32 | if(fp == NULL) //If file cannot be opened |
thomasmorris | 51:47f5db68500b | 33 | { |
thomasmorris | 51:47f5db68500b | 34 | error("Could not open file for write in SD_Card\n"); //Print error message to terminal |
thomasmorris | 51:47f5db68500b | 35 | LCD.Display_Clear(); //Clear the LCD display |
thomasmorris | 51:47f5db68500b | 36 | LCD.Write_String("CANNOT OPEN FILE"); //Write error message to LCD |
thomasmorris | 51:47f5db68500b | 37 | errorCode(FATAL); //Run error function - toggles red LED |
thomasmorris | 51:47f5db68500b | 38 | } |
thomasmorris | 51:47f5db68500b | 39 | |
thomasmorris | 51:47f5db68500b | 40 | if(Log_Value==5){pc.printf("File Opened\n");} //If logging is enabled, print debug statement |
thomasmorris | 51:47f5db68500b | 41 | |
thomasmorris | 51:47f5db68500b | 42 | for(int SD_Card_Data_Pointer = 0; SD_Card_Data_Pointer != mailsize; SD_Card_Data_Pointer++) //For all addresses in the DATA buffer |
thomasmorris | 48:244d6d81bb52 | 43 | { |
thomasmorris | 51:47f5db68500b | 44 | if(Log_Value==5){pc.printf("Copying from address: %d\n", SD_Card_Data_Pointer);} //If logging is enabled, print debug statement |
thomasmorris | 51:47f5db68500b | 45 | |
thomasmorris | 51:47f5db68500b | 46 | time_t Time = Data_Buffer[SD_Card_Data_Pointer].get_time(); //Store the time from the DATA buffer |
thomasmorris | 51:47f5db68500b | 47 | tm* Time_Pointer = localtime(&Time); //Create pointer to stored time |
thomasmorris | 51:47f5db68500b | 48 | int temp_day = Time_Pointer->tm_mday; //Assign the day to a temporary value |
thomasmorris | 51:47f5db68500b | 49 | int temp_month = (Time_Pointer->tm_mon+1); //Assign the month to a temporary value |
thomasmorris | 51:47f5db68500b | 50 | int temp_year = (Time_Pointer->tm_year+1900); //Assign the year to a temporary value |
thomasmorris | 51:47f5db68500b | 51 | int temp_hours = Time_Pointer->tm_hour; //Assign the hour to a temporary value |
thomasmorris | 51:47f5db68500b | 52 | int temp_minute = Time_Pointer->tm_min; //Assign the minute to a temporary value |
thomasmorris | 51:47f5db68500b | 53 | int temp_seconds = Time_Pointer->tm_sec; //Assign the second to a temporary value |
thomasmorris | 51:47f5db68500b | 54 | float temp_temperature = Data_Buffer[SD_Card_Data_Pointer].get_temperature(); //Assign the temperature to a temporary value |
thomasmorris | 51:47f5db68500b | 55 | float temp_pressure = Data_Buffer[SD_Card_Data_Pointer].get_pressure(); //Assign the pressure to a temporary value |
thomasmorris | 51:47f5db68500b | 56 | float temp_light = Data_Buffer[SD_Card_Data_Pointer].get_light(); //Assign the light to a temporary value |
thomasmorris | 51:47f5db68500b | 57 | |
thomasmorris | 51:47f5db68500b | 58 | fprintf(fp, "Date: %02d/%02d/%d,",temp_day,temp_month,temp_year); //Print the date into column 1 |
thomasmorris | 51:47f5db68500b | 59 | fprintf(fp, "Time: %02d:%02d:%02d,",temp_hours,temp_minute, temp_seconds); //Print the time into column 2 |
thomasmorris | 51:47f5db68500b | 60 | fprintf(fp, "Temperature: %1.1f,",temp_temperature); //Print the temperature into column 3 |
thomasmorris | 51:47f5db68500b | 61 | fprintf(fp, "Pressure: %1.1f,",temp_pressure); //Print the pressure into column 4 |
thomasmorris | 51:47f5db68500b | 62 | fprintf(fp, "Light: %5.3f,",temp_light); //Print the light into column 5 |
thomasmorris | 51:47f5db68500b | 63 | } |
thomasmorris | 51:47f5db68500b | 64 | fclose(fp); //Close the file |
thomasmorris | 51:47f5db68500b | 65 | |
thomasmorris | 51:47f5db68500b | 66 | } |
thomasmorris | 51:47f5db68500b | 67 | void SD_Card_Write() //Function to write to the SD card |
thomasmorris | 51:47f5db68500b | 68 | { |
thomasmorris | 51:47f5db68500b | 69 | if(SD_Write == 0) //If the SD card is not present |
thomasmorris | 51:47f5db68500b | 70 | { |
thomasmorris | 51:47f5db68500b | 71 | if(Log_Value==5){pc.printf("SD Has Been Ejected Please do a Hardware Restart\n");} //If logging is enabled, print debug statement |
thomasmorris | 51:47f5db68500b | 72 | return; //Exit function |
thomasmorris | 48:244d6d81bb52 | 73 | } |
thomasmorris | 51:47f5db68500b | 74 | if(Write_Pointer == mailsize - 1) //If the end of the buffer is being written to |
thomasmorris | 47:6d128e500875 | 75 | { |
thomasmorris | 51:47f5db68500b | 76 | if(Log_Value==5){pc.printf("In SD_Card Thread\n");} //If logging is enabled, print debug statement |
thomasmorris | 51:47f5db68500b | 77 | |
thomasmorris | 51:47f5db68500b | 78 | FATFileSystem fs("sd", &sd); //Sets working directory as "sd" |
thomasmorris | 50:3d61ca637399 | 79 | |
thomasmorris | 51:47f5db68500b | 80 | time_t File_Time = Data_Buffer[Write_Pointer - 1].get_time(); //Store the time from the data buffer |
thomasmorris | 51:47f5db68500b | 81 | tm* File_Time_Pointer = localtime(&File_Time); //Create pointer to stored time |
thomasmorris | 51:47f5db68500b | 82 | int File_temp_day = File_Time_Pointer->tm_mday; //Assign the day to a temporary value |
thomasmorris | 51:47f5db68500b | 83 | int File_temp_month = (File_Time_Pointer->tm_mon+1); //Assign the month to a temporary value |
thomasmorris | 51:47f5db68500b | 84 | int File_temp_year = (File_Time_Pointer->tm_year+1900); //Assign the year to a temporary value |
thomasmorris | 47:6d128e500875 | 85 | |
thomasmorris | 51:47f5db68500b | 86 | //Create a const char* using year, month and day and create a file using that name |
thomasmorris | 50:3d61ca637399 | 87 | char FileToOpen[50] = {}; |
thomasmorris | 50:3d61ca637399 | 88 | snprintf (FileToOpen, sizeof(char) * sizeof(FileToOpen), "/sd/%d_%02d_%02d.csv",File_temp_year,File_temp_month,File_temp_day); |
thomasmorris | 50:3d61ca637399 | 89 | |
thomasmorris | 51:47f5db68500b | 90 | if(fp = fopen(FileToOpen,"r")) //If the file already exists |
thomasmorris | 47:6d128e500875 | 91 | { |
thomasmorris | 51:47f5db68500b | 92 | fclose(fp); //Close the file |
thomasmorris | 51:47f5db68500b | 93 | SD_Write_Control(FileToOpen, "Append"); //Append the existing file |
thomasmorris | 47:6d128e500875 | 94 | } |
thomasmorris | 51:47f5db68500b | 95 | else //If the file doesn't exist |
thomasmorris | 51:47f5db68500b | 96 | { |
thomasmorris | 51:47f5db68500b | 97 | SD_Write_Control(FileToOpen, "Write"); //Create a new file |
thomasmorris | 51:47f5db68500b | 98 | } |
thomasmorris | 51:47f5db68500b | 99 | if(Log_Value==5){pc.printf("Dumped data to SD Card\n");} //If logging is enabled, print debug statement |
thomasmorris | 50:3d61ca637399 | 100 | |
thomasmorris | 47:6d128e500875 | 101 | } |
thomasmorris | 47:6d128e500875 | 102 | } |
thomasmorris | 49:d51f96a46cc3 | 103 |