efe

Dependencies:   mbed

Fork of raw_sd_card_disc_init by Pradeep Kotipalli

Revision:
3:d4bb1e13a897
Parent:
2:aea18e9b89d8
--- a/main.cpp	Sat Jun 06 11:07:06 2015 +0000
+++ b/main.cpp	Sat Jun 06 11:46:02 2015 +0000
@@ -17,6 +17,8 @@
 int initialise_card_v1();
 int initialise_card_v2();
 int disk_initialize();
+int disk_write(const uint8_t *, uint64_t);
+
 uint64_t sd_sectors();
 uint64_t sectors;
 
@@ -25,6 +27,7 @@
 int cmdx(int, int);
 int cmd8();
 int read(uint8_t*, uint32_t );
+int write(const uint8_t*, uint32_t );
 static uint32_t ext_bits(unsigned char *, int , int );
 
 int cdv;
@@ -38,18 +41,24 @@
 
 SPI spi(PTD6, PTD7, PTD5); // mosi, miso, sclk
 DigitalOut cs(PTD2);
+int count_bro;
 
 
 
 int main()
 {
-    while(1)
-    { initialise_card();
+    
+     initialise_card();
       int result= initialise_card();
       printf("initialise card result=%d\n",result);
       disk_initialize();
-      wait(5);
-    }
+       uint8_t buffer[512];
+      for(count_bro=0;count_bro<512;count_bro++)
+      {buffer[count_bro]='a';}
+      disk_write(buffer, 2);
+      
+      
+    
 }
 
 
@@ -89,7 +98,7 @@
     for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
         cmd(55, 0);
         if (cmd(41, 0) == 0) {
-            printf("Yuppie v1 successful");
+            printf("Yuppie v1 successful\n");
             cdv = 512;
             debug_if(SD_DBG, "\n\rInit: SEDCARD_V1\n\r");
             
@@ -108,7 +117,7 @@
         cmd58();
         cmd(55, 0);
         if (cmd(41, 0x40000000) == 0) {
-            printf("Yuppie,v2 successful");
+            printf("Yuppie,v2 successful\n");
             cmd58();
             debug_if(SD_DBG, "\n\rInit: SDCARD_V2\n\r");
             cdv = 1;
@@ -328,10 +337,54 @@
     }
     else
     {
-        printf("Hey,block init succesful");
+        printf("Hey,block init succesful\n");
     }
     
     spi.frequency(1000000); // Set to 1MHz for data transfer
     return 0;
 }
 
+int disk_write(const uint8_t *buffer, uint64_t block_number)
+
+{
+    // set write address for single block (CMD24)
+    if (cmd(24, block_number * cdv) != 0) {
+        return 1;
+    }
+    
+    // send the data block
+    write(buffer, 512);
+    printf("Written Successfully bro \n");
+    return 0;
+}
+
+int write(const uint8_t*buffer, uint32_t length) {
+    cs = 0;
+    
+    // indicate start of block
+    spi.write(0xFE);
+    
+    // write the data
+    for (int i = 0; i < length; i++) {
+        spi.write(buffer[i]);
+    }
+    
+    // write the checksum
+    spi.write(0xFF);
+    spi.write(0xFF);
+    
+    // check the response token
+    if ((spi.write(0xFF) & 0x1F) != 0x05) {
+        cs = 1;
+        spi.write(0xFF);
+        return 1;
+    }
+    
+    // wait for write to finish
+    while (spi.write(0xFF) == 0);
+    
+    cs = 1;
+    spi.write(0xFF);
+    return 0;
+}
+