فهرست منبع

Add the reply_time field to lpc_device struct, automatically align data written to RAM, etc.

Some fixes that should make the software work more properly.
Mateusz Bugdalski 13 سال پیش
والد
کامیت
8ef3e905be
2فایلهای تغییر یافته به همراه17 افزوده شده و 5 حذف شده
  1. 16 5
      lpc.c
  2. 1 0
      lpc.h

+ 16 - 5
lpc.c

@@ -61,6 +61,7 @@ struct lpc_device *lpc_setup(struct serial_device *port) {
   dev = malloc(sizeof(struct lpc_device));
   dev->port = port;
   dev->part = NULL;
+  dev->reply_time = 500;
   lpc_reset(dev, 1);
   return dev;
 }
@@ -94,6 +95,7 @@ void lpc_write(struct lpc_device *dev, const struct sprog_data *d) {
   static const int chunk_sizes[] = {256, 512, 1024, 4096, 0};
   struct sprog_data chunk;
   int i;
+  int j;
   int offset;
   int chunk_size;
   int data_size;
@@ -107,12 +109,18 @@ void lpc_write(struct lpc_device *dev, const struct sprog_data *d) {
   lpc_command(dev, "P 0 %d\r\n", i);
   
   sprog_info("Erasing Flash memory... ");
+  j = dev->reply_time;
+  dev->reply_time = 5000;
   if(lpc_command(dev, "E 0 %d\r\n", i))
     sprog_info("Error\n");
   else
     sprog_info("OK\n");
+  dev->reply_time = j;
   
   i = d->size/4096 - 1;
+  if(d->size % 4096)
+    i++;
+  
   lpc_command(dev, "P 0 %d\r\n", i); /* prepare sectors for write */
   
   while(d->size-offset>0) {
@@ -127,7 +135,8 @@ void lpc_write(struct lpc_device *dev, const struct sprog_data *d) {
     }
     
     chunk_size = chunk_sizes[i];
-    if(chunk_size==0) chunk_size = chunk_sizes[i-1];
+    if(chunk_size==0)
+      chunk_size = chunk_sizes[i-1];
     sprog_alloc_data(&chunk, chunk_size);
     
     data_size = chunk_size;
@@ -173,10 +182,12 @@ void lpc_write_ram(struct lpc_device *dev, const struct sprog_data *d, unsigned
   int i;
   int j;
   
-  if(d->size & 3)
-    sprog_error("Invalid data size - should be aligned to 4\n");
+  /* align data size to 4 */
+  i = d->size;
+  if(i & 3)
+    i = (i & ~3) + 4;
   
-  lpc_command(dev, "W %u %u\r\n", addr, d->size);
+  lpc_command(dev, "W %u %u\r\n", addr, i);
   sprog_info("Writing %d bytes\n", d->size);
   
   last_i = 0;
@@ -235,7 +246,7 @@ int lpc_await_reply(struct lpc_device *dev, ...) {
   
 
 int lpc_getline(struct lpc_device *dev, char *buf) {
-  if(sprog_waitdata(dev->port, 5000)==0)
+  if(sprog_waitdata(dev->port, dev->reply_time)==0)
     return 0;
   fgets(buf, 4096, dev->port->f);
   return 1;

+ 1 - 0
lpc.h

@@ -7,6 +7,7 @@
 struct lpc_device {
   struct serial_device *port;
   const struct lpc_part *part;
+  int reply_time;
 };
 
 struct lpc_device *lpc_setup(struct serial_device *port);