Sonder Design Team / Memory
Revision:
4:bb4fd1147054
Parent:
3:339efdc5134f
Child:
5:2fa79108a29b
diff -r 339efdc5134f -r bb4fd1147054 Memory.cpp
--- a/Memory.cpp	Wed Feb 08 22:23:02 2017 +0000
+++ b/Memory.cpp	Thu Feb 09 00:51:57 2017 +0000
@@ -103,34 +103,48 @@
         printf("\nLength %i exceeds Max Length\n",length);
         return address;
     }
+    
+    //Enable the memory for wiring. This segment is only required if not writing to the start of a page.
+    if(address%256!=0){
+        _cs_mem=1;
+        _cs_mem=0;                      //Selet memory
+        my_spi.write(06);               //Set Write Enable flag in the status reg
+        _cs_mem=1;                      //Deslect memory
+        wait_us(10);
+        _cs_mem=0;                      //Selet memory
+        my_spi.write(0x02);             //Send write comand
+        my_spi.write(address>>16);      //Send high address byte
+        my_spi.write(address>>8);       //Send middle adress byte
+        my_spi.write(address);          //Send low address
+    }    
+    
     for(int i =0; i<length; i++) {
-        if(address%256==0) {                //Handle start and end of pages
-            _cs_mem=1;
-            wait_us(10);
-            //wait for the WIP bit to go low
-            _cs_mem=0;                           //Selet memory
-            my_spi.write(0x05);             //Send read status register command
-            int byte1 = my_spi.write(dummy);//Send dummy byte to read status reg
-            while ((byte1&1)>0) {
+        //Handle start and end of pages. aAt the page boundry the memory must be deselected and and re-enabled for writng to the next page.
+        if(address%256==0) {                
+                _cs_mem=1;
                 wait_us(10);
-                my_spi.write(0x05);         //Send read status register command
-                byte1 = my_spi.write(dummy);//Send dummy byte to read status reg
-            }
-            _cs_mem=1;
-
-            _cs_mem=0;                           //Selet memory
-            my_spi.write(06);               //Set Write Enable flag in the status reg
-            _cs_mem=1;
-            wait_us(10);
-
-            _cs_mem=0;                           //Selet memory
-            my_spi.write(0x02);               //Send read comand
-            my_spi.write(address>>16);      //Send high address byte
-            my_spi.write(address>>8);       //Send middle adress byte
-            my_spi.write(address);          //Send low address
-
+                //wait for the WIP bit to go low
+                _cs_mem=0;                           //Selet memory
+                my_spi.write(0x05);             //Send read status register command
+                int byte1 = my_spi.write(dummy);//Send dummy byte to read status reg
+                while ((byte1&1)>0) {
+                    wait_us(10);
+                    my_spi.write(0x05);         //Send read status register command
+                    byte1 = my_spi.write(dummy);//Send dummy byte to read status reg
+                }
+                _cs_mem=1;
+                _cs_mem=0;                      //Selet memory
+                my_spi.write(06);               //Set Write Enable flag in the status reg
+                _cs_mem=1;                      //Deslect memory
+                wait_us(10);
+                _cs_mem=0;                      //Selet memory
+                my_spi.write(0x02);             //Send write comand
+                my_spi.write(address>>16);      //Send high address byte
+                my_spi.write(address>>8);       //Send middle adress byte
+                my_spi.write(address);          //Send low address
         }
-        my_spi.write(buffer[i]);            //Write the calue of the buffer to memory
+        
+        my_spi.write(buffer[i]);            //Write the value of the buffer to memory
         wait_us(5);
         address=address+1;                  //Increment address
     }