vr1.1

Dependencies:   FreescaleIAP mbed-rtos mbed

Fork of CDMS_RTOS_v1_1 by Team Fox

Revision:
12:cb3ee1ac3638
Parent:
11:d6dc9074075b
Child:
15:2c8629da6ec9
--- a/SDCard.cpp	Mon Jul 06 06:25:14 2015 +0000
+++ b/SDCard.cpp	Mon Jul 06 10:31:29 2015 +0000
@@ -2,38 +2,78 @@
 #include "mbed_debug.h"
 #include "SDCard.h"
 #include "all_funcs.h"
+#include "Flags.h"
 
 SPI spi_SD(PTD6, PTD7, PTD5); // mosi, miso, sclk
 DigitalOut cs_SD(D2);
 
-int *FCTN_INIT_SD()
+void FCTN_INIT_SD()
 {
+    uint8_t sd_init_fail_count = 0;
+    uint8_t sd_disk_init_fail_count = 0;
     all_flags = all_flags|SDCARD_INIT_STATUS;
-    int sd_response[2] = {initialise_card(),disk_initialize()};
-    return sd_response;    
+    int sd_response = initialise_card();
+    while(sd_response == 1)
+    {
+        sd_init_fail_count++;
+        sd_response = initialise_card();
+        if(sd_init_fail_count>3)
+        {
+            all_flags = all_flags|SDCARD_INIT_FAIL;
+            printf("\rSDCard init failed\r\n");
+            break;
+        }
+    }
+    sd_response = disk_initialize();
+    while(sd_response == 1)
+    {
+        sd_disk_init_fail_count++;
+        sd_response = disk_initialize();
+        if(sd_disk_init_fail_count>3)
+        {
+            all_flags = all_flags|SDCARD_DISK_FAIL;
+            printf("\rSDCard disk init failed\r\n");
+            break;
+        }
+    }
+    if(sd_disk_init_fail_count<=3 && sd_init_fail_count<=3 )
+    {
+        printf("\rSDCard successfully initialized\r\n");
+    }
+    all_flags = all_flags&(~SDCARD_INIT_STATUS);
 }
 
-int  FCTN_RD_SD(uint8_t *buffer, uint64_t block_number) {
+int  FCTN_RD_SD(uint8_t *buffer, uint64_t block_number) 
+{
+    all_flags = all_flags|SD_RD_STATUS;
     // set read address for single block (CMD17)
-    if (cmd(17, block_number * cdv) != 0) {
+    if (cmd(17, block_number * cdv) != 0) 
+    {
+        all_flags = all_flags|SD_RD_FAIL;
+        printf("\rReading from SDCard failed\r\n");
         return 1;
     }
-    
     // receive the data
     read(buffer, 512);
-    printf("Written Successfully to SDCard\r\n");
+    all_flags = all_flags&(~SD_RD_STATUS);
+    printf("Read Successfully from SDCard\r\n");
     return 0;
 }
 
 int FCTN_WR_SD(const uint8_t *buffer, uint64_t block_number)
 {
+    all_flags = all_flags|SD_WR_STATUS;
     // set write address for single block (CMD24)
-    if (cmd(24, block_number * cdv) != 0) {
+    if (cmd(24, block_number * cdv) != 0) 
+    {
+        all_flags = all_flags|SD_WR_FAIL;
+        printf("\rWriting to SDCard Failed\r\n");
         return 1;
     }
     
     // send the data block
     write(buffer, 512);
+    all_flags = all_flags&(~SD_WR_STATUS);
     printf("Written Successfully to SDCard\r\n");
     return 0;
 }
@@ -50,20 +90,20 @@
     // send CMD0, should return with all zeros except IDLE STATE set (bit 0)
     if (cmd(0, 0) != R1_IDLE_STATE) {
         debug("No disk, or could not put SD card in to SPI idle state\r\n");
-        return SDCARD_FAIL;
+        return 1;
     }
  
  // send CMD8 to determine whther it is ver 2.x
     int r = cmd8();
     if (r == R1_IDLE_STATE) 
     {
-        printf("Entering v2 bro\r\n");
+        printf("Entering version2\r\n");
         return initialise_card_v2();    
     }   
     else 
     {
         debug("Not in idle state after sending CMD8 (not an SD card?)\r\n");
-        return SDCARD_FAIL;
+        return 1;
     }
 }
  
@@ -78,12 +118,12 @@
             debug_if(SD_DBG, "\r\n\rInit: SDCARD_V2\r\n\r");
             cdv = 1;
         
-            return SDCARD_V2;
+            return 0;
         }
     }
     
     debug("Timeout waiting for v2.x card\r\n");
-    return SDCARD_FAIL;
+    return 1;
 }
 
 int disk_initialize() {