stlink-common: Update STM32L0/1 loaders to return remaining count in r2
[fw/stlink] / src / stlink-common.c
index f15738e7f04527faa8e975c437c0f67e3822ca81..dce100daaec73e7189d08d2cdabed792cc54b9f2 100644 (file)
@@ -1501,20 +1501,19 @@ int write_loader_to_sram(stlink_t *sl, stm32_addr_t* addr, size_t* size) {
     static const uint8_t loader_code_stm32l[] = {
 
         /* openocd.git/contrib/loaders/flash/stm32lx.S
-           r0, input, dest addr
-           r1, input, source addr
+           r0, input, source addr
+           r1, input, dest addr
            r2, input, word count
-           r3, output, word count
+           r2, output, remaining word count
            */
 
-        0x00, 0x23,
         0x04, 0xe0,
 
-        0x51, 0xf8, 0x04, 0xcb,
-        0x40, 0xf8, 0x04, 0xcb,
-        0x01, 0x33,
+        0x50, 0xf8, 0x04, 0xcb,
+        0x41, 0xf8, 0x04, 0xcb,
+        0x01, 0x3a,
 
-        0x93, 0x42,
+        0x00, 0x2a,
         0xf8, 0xd3,
         0x00, 0xbe
     };
@@ -1522,22 +1521,21 @@ int write_loader_to_sram(stlink_t *sl, stm32_addr_t* addr, size_t* size) {
     static const uint8_t loader_code_stm32l0[] = {
 
         /*
-           r0, input, dest addr
-           r1, input, source addr
+           r0, input, source addr
+           r1, input, dest addr
            r2, input, word count
-           r3, output, word count
+           r2, output, remaining word count
          */
 
-        0x00, 0x23,
         0x04, 0xe0,
 
-        0x0c, 0x68,
-        0x04, 0x60,
-        0x01, 0x33,
+        0x04, 0x68,
+        0x0c, 0x60,
+        0x01, 0x3a,
         0x04, 0x31,
         0x04, 0x30,
 
-        0x93, 0x42,
+        0x00, 0x2a,
         0xf8, 0xd3,
         0x00, 0xbe
     };
@@ -2065,6 +2063,8 @@ int run_flash_loader(stlink_t *sl, flash_loader_t* fl, stm32_addr_t target, cons
 
     reg rr;
     int i = 0;
+    size_t count;
+
     DLOG("Running flash loader, write address:%#x, size: %zd\n", target, size);
     // FIXME This can never return -1
     if (write_buffer_to_sram(sl, fl, buf, size) == -1) {
@@ -2073,47 +2073,32 @@ int run_flash_loader(stlink_t *sl, flash_loader_t* fl, stm32_addr_t target, cons
         return -1;
     }
 
-    if (sl->flash_type == FLASH_TYPE_L0) {
-
-        size_t count = size / sizeof(uint32_t);
-        if (size % sizeof(uint32_t)) ++count;
-
-        /* setup core */
-        stlink_write_reg(sl, target, 0); /* target */
-        stlink_write_reg(sl, fl->buf_addr, 1); /* source */
-        stlink_write_reg(sl, count, 2); /* count (32 bits words) */
-        stlink_write_reg(sl, fl->loader_addr, 15); /* pc register */
-
-    } else if (sl->flash_type == FLASH_TYPE_F0) {
-
-        size_t count = size / sizeof(uint16_t);
-        if (size % sizeof(uint16_t)) ++count;
-
-        /* setup core */
-        stlink_write_reg(sl, fl->buf_addr, 0); /* source */
-        stlink_write_reg(sl, target, 1); /* target */
-        stlink_write_reg(sl, count, 2); /* count (16 bits half words) */
-        stlink_write_reg(sl, 0, 3); /* flash bank 0 (input) */
-        stlink_write_reg(sl, fl->loader_addr, 15); /* pc register */
-
-    } else if (sl->flash_type == FLASH_TYPE_F4 || sl->flash_type == FLASH_TYPE_L4) {
-        size_t count = size / sizeof(uint32_t);
-        if (size % sizeof(uint32_t)) ++count;
+    if (sl->flash_type == FLASH_TYPE_F0) {
+        count = size / sizeof(uint16_t);
+        if (size % sizeof(uint16_t))
+            ++count;
+    } else if (sl->flash_type == FLASH_TYPE_F4 ||
+            sl->flash_type == FLASH_TYPE_L4 ||
+            sl->flash_type == FLASH_TYPE_L0) {
+        count = size / sizeof(uint32_t);
+        if (size % sizeof(uint32_t))
+            ++count;
         if (sl->chip_id == STM32_CHIPID_L4) {
-            if (count % 2) ++count;
+            if (count % 2)
+                ++count;
         }
-
-        /* setup core */
-        stlink_write_reg(sl, fl->buf_addr, 0); /* source */
-        stlink_write_reg(sl, target, 1); /* target */
-        stlink_write_reg(sl, count, 2); /* count (32 bits words) */
-        stlink_write_reg(sl, fl->loader_addr, 15); /* pc register */
-
     } else {
         fprintf(stderr, "unknown coreid 0x%x, don't know what flash loader to use\n", sl->core_id);
         return -1;
     }
 
+    /* setup core */
+    stlink_write_reg(sl, fl->buf_addr, 0); /* source */
+    stlink_write_reg(sl, target, 1); /* target */
+    stlink_write_reg(sl, count, 2); /* count */
+    stlink_write_reg(sl, 0, 3); /* flash bank 0 (input), only used on F0, but armless fopr others */
+    stlink_write_reg(sl, fl->loader_addr, 15); /* pc register */
+
     /* run loader */
     stlink_run(sl);
 
@@ -2131,35 +2116,10 @@ int run_flash_loader(stlink_t *sl, flash_loader_t* fl, stm32_addr_t target, cons
     }
 
     /* check written byte count */
-    if (sl->flash_type == FLASH_TYPE_L0) {
-        size_t count = size / sizeof(uint32_t);
-        if (size % sizeof(uint32_t)) ++count;
-
-        stlink_read_reg(sl, 3, &rr);
-        if (rr.r[3] != count) {
-            fprintf(stderr, "write error, count == %u\n", rr.r[3]);
-            return -1;
-        }
-
-    } else if (sl->flash_type == FLASH_TYPE_F0) {
-        stlink_read_reg(sl, 2, &rr);
-        if (rr.r[2] != 0) {
-            fprintf(stderr, "write error, count == %u\n", rr.r[2]);
-            return -1;
-        }
-
-    } else if (sl->flash_type == FLASH_TYPE_F4 || sl->flash_type == FLASH_TYPE_L4) {
-        stlink_read_reg(sl, 2, &rr);
-        if (rr.r[2] != 0) {
-            fprintf(stderr, "write error, count == %u\n", rr.r[2]);
-            return -1;
-        }
-
-    } else {
-
-        fprintf(stderr, "unknown coreid 0x%x, can't check written byte count\n", sl->core_id);
+    stlink_read_all_regs(sl, &rr);
+    if (rr.r[2] != 0) {
+        fprintf(stderr, "write error, count == %u\n", rr.r[2]);
         return -1;
-
     }
 
     return 0;