Debug only works after chip erase in ST-LINK utility
Just wanted to chime in that I had the exact same problem where I could only program about 1 out of 5 attempts (sometimes 1/10) even though I had reduced the frequency and raised the voltage. I am using STM32L051 with an STLINKV2 ISOL:
I got the same:
Error: jtag status contains invalid mode value - communication failure
Info : Previous state query failed, trying to reconnect
Error: error writing to flash at address 0x08000000 at offset 0x00000000
The solution of reducing WORKAREASIZE to 0x1000 worked perfect! Now I'm 10/10.
Many thanks as it took about two months to finally stumble upon this post.