flash: add stm32lx loader Hard Fault workaround
[openocd.git] / src / flash / nor / stm32lx.c
index 264a45ca6e8bf905b70e9d8842f04ccdf27b276e..fc0fb9e1aef8cb824e972d0a2b4a789337825e0b 100644 (file)
@@ -32,6 +32,7 @@
 #include <helper/binarybuffer.h>
 #include <target/algorithm.h>
 #include <target/armv7m.h>
+#include <target/cortex_m.h>
 
 /* stm32lx flash register locations */
 
@@ -299,6 +300,18 @@ static int stm32lx_write_half_pages(struct flash_bank *bank, uint8_t *buffer,
                return retval;
        }
 
+       struct armv7m_common *armv7m = target_to_armv7m(target);
+       if (armv7m == NULL) {
+
+               /* something is very wrong if armv7m is NULL */
+               LOG_ERROR("unable to get armv7m target");
+               return retval;
+       }
+
+       /* save any DEMCR flags and configure target to catch any Hard Faults */
+       uint32_t demcr_save = armv7m->demcr;
+       armv7m->demcr = VC_HARDERR;
+
        /* Loop while there are bytes to write */
        while (count > 0) {
                uint32_t this_count;
@@ -324,6 +337,10 @@ static int stm32lx_write_half_pages(struct flash_bank *bank, uint8_t *buffer,
                if (retval != ERROR_OK)
                        break;
 
+               /* check for Hard Fault */
+               if (armv7m->exception_number == 3)
+                       break;
+
                /* 6: Wait while busy */
                retval = stm32lx_wait_until_bsy_clear(bank);
                if (retval != ERROR_OK)
@@ -334,6 +351,42 @@ static int stm32lx_write_half_pages(struct flash_bank *bank, uint8_t *buffer,
                count -= this_count;
        }
 
+       /* restore previous flags */
+       armv7m->demcr = demcr_save;
+
+       if (armv7m->exception_number == 3) {
+
+               /* the stm32l15x devices seem to have an issue when blank.
+                * if a ram loader is executed on a blank device it will
+                * Hard Fault, this issue does not happen for a already programmed device.
+                * A related issue is described in the stm32l151xx errata (Doc ID 17721 Rev 6 - 2.1.3).
+                * The workaround of handling the Hard Fault exception does work, but makes the
+                * loader more complicated, as a compromise we manually write the pages, programming time
+                * is reduced by 50% using this slower method.
+                */
+
+               LOG_WARNING("couldn't use loader, falling back to page memory writes");
+
+               while (count > 0) {
+                       uint32_t this_count;
+                       this_count = (count > 128) ? 128 : count;
+
+                       /* Write the next half pages */
+                       retval = target_write_buffer(target, address, this_count, buffer);
+                       if (retval != ERROR_OK)
+                               break;
+
+                       /* Wait while busy */
+                       retval = stm32lx_wait_until_bsy_clear(bank);
+                       if (retval != ERROR_OK)
+                               break;
+
+                       buffer += this_count;
+                       address += this_count;
+                       count -= this_count;
+               }
+       }
+
        if (retval == ERROR_OK)
                retval = stm32lx_lock_program_memory(bank);
 

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)