small test code for IAP

Dependencies:   mbed

Revision:
2:806eb710bbb0
Parent:
1:90c9fa3e7462
Child:
3:09b4670e337a
diff -r 90c9fa3e7462 -r 806eb710bbb0 main.cpp
--- a/main.cpp	Thu Feb 05 08:07:05 2015 +0000
+++ b/main.cpp	Sun Feb 08 12:28:24 2015 +0000
@@ -23,6 +23,7 @@
 IAP iap_entry   = (IAP)0x1FFF1FF1;
 
 #define         FLASH_TARGET_ADDR   ((char *)0x00008100)
+#define         FLASH_TARGET_SECTOR 8
 
 int main(void)
 {
@@ -46,17 +47,53 @@
 
 void IAP_Write(void)
 {
-    command[0]  = 50;          //  "prepare" cmmand
-    command[1]  =  8;           //  Start sector number is 8
-    command[2]  =  8;           //  Finish sector number is 8
+    //  blank check
+    
+    command[0]  = 53;                   //  "blank check" command
+    command[1]  = FLASH_TARGET_SECTOR;  //  Start sector number is 8
+    command[2]  = FLASH_TARGET_SECTOR;  //  Finish sector number is 8
+    iap_entry( command, result );
+
+    printf( "error code for \"blank check\" = %d\r\n", result[ 0 ] );
+
+    //  if the sector is not blank
+    
+    if ( result[ 0 ] ) {
+
+        //  prepare for erasing
+        
+        command[0]  = 50;                   //  "prepare" command
+        command[1]  = FLASH_TARGET_SECTOR;  //  Start sector number is 8
+        command[2]  = FLASH_TARGET_SECTOR;  //  Finish sector number is 8
+        iap_entry( command, result );
+
+        printf( "error code for \"prepare\" = %d\r\n", result[ 0 ] );
+
+        //  erase
+
+        command[0]  = 52;                   //  "erase" command
+        command[1]  = FLASH_TARGET_SECTOR;  //  Start sector number is 8
+        command[2]  = FLASH_TARGET_SECTOR;  //  Finish sector number is 8
+        command[3]  = 96000;                //  Frequency in khz
+        iap_entry( command, result );
+
+        printf( "error code for \"erase\" = %d\r\n", result[ 0 ] );
+    }
+
+    //  prepare for writing
+
+    command[0]  = 50;                   //  "prepare" command
+    command[1]  = FLASH_TARGET_SECTOR;  //  Start sector number is 8
+    command[2]  = FLASH_TARGET_SECTOR;  //  Finish sector number is 8
     iap_entry( command, result );
 
     printf( "error code for \"prepare\" = %d\r\n", result[ 0 ] );
 
+    //  write
 
     command[0]  = 51;                               //  "copy from ram to flash" command
-    command[1]  = (unsigned int)FLASH_TARGET_ADDR;  //  Addrees of Flash 
-    command[2]  = (unsigned int)a;                  //  Addrees of Sram 
+    command[1]  = (unsigned int)FLASH_TARGET_ADDR;  //  Addrees of Flash
+    command[2]  = (unsigned int)a;                  //  Addrees of Sram
     command[3]  = 256;                              //  How many bytes want to transfer
     command[4]  = 96000;                            //  Frequency in khz
     iap_entry( command, result );