Procházet zdrojové kódy

Send minimal amount of data

Send just the needed amount of data when writing to RAM and copying to Flash.
Mateusz Bugdalski před 13 roky
rodič
revize
8764525468
1 změnil soubory, kde provedl 32 přidání a 8 odebrání
  1. 32 8
      lpc.c

+ 32 - 8
lpc.c

@@ -92,12 +92,14 @@ void lpc_init(struct lpc_device *dev) {
 }
 
 void lpc_write(struct lpc_device *dev, const struct sprog_data *d) {
-  static const int chunk_sizes[] = {256, 512, 1024, 4096, 0};
+  static const int chunk_sizes[] = {256, 512, 1024, 4096, 0}; /* keep it sorted! */
   struct sprog_data chunk;
   int i;
   int j;
+  int status;
   int offset;
   int chunk_size;
+  int max_chunk_index;
   int data_size;
   offset = 0;
   
@@ -134,14 +136,25 @@ void lpc_write(struct lpc_device *dev, const struct sprog_data *d) {
       }
     }
     
+    if(chunk_sizes[i]==0)
+      i--;
+    
     chunk_size = chunk_sizes[i];
-    if(chunk_size==0)
-      chunk_size = chunk_sizes[i-1];
-    sprog_alloc_data(&chunk, chunk_size);
+    max_chunk_index = i;
     
     data_size = chunk_size;
-    if(data_size > d->size-offset)
+    
+    if(data_size > d->size-offset) {
       data_size = d->size - offset;
+      /* we assume that all of the available chunk sizes are divisible by the first one */
+      /* TODO: Optimize the following code, assuming that the chunk sizes are powers of 2 */
+      chunk_size = data_size - data_size % chunk_sizes[0];
+      if(data_size % chunk_sizes[0])
+	chunk_size += chunk_sizes[0];
+    }
+    
+      
+    sprog_alloc_data(&chunk, chunk_size);
     
     for(i=0; i<data_size; i++)
       chunk.data[i] = d->data[offset+i];
@@ -166,10 +179,21 @@ void lpc_write(struct lpc_device *dev, const struct sprog_data *d) {
     lpc_write_ram(dev, &chunk, 0x10000400);
     
     sprog_info("Copying to Flash...\n");
-    if(lpc_command(dev, "C %d %u %d\r\n", offset, 0x10000400, chunk_size))
-      sprog_error("Error while copying to flash\n");
-    else
+    
+    j = max_chunk_index;
+    status = 1;
+    for(i=chunk_size; i>0; i-=chunk_sizes[j]) {
+      for(j=j; j>0; j--)
+	if(chunk_sizes[j]<=i)
+	  break;
+      if(lpc_command(dev, "C %d %u %d\r\n", offset+(chunk_size-i), 0x10000400+(chunk_size-i), chunk_sizes[j]))
+	status = 0;
+    }
+    
+    if(status)
       sprog_info("Done!\n");
+    else
+      sprog_info("Error\n");
     
     offset += data_size;
   }