It appears there is a bug at the end of the option byte write function for STM32F2:
stm32f2x.c:375, in stm32x_write_options(). FLASH_OPTCR (option byte config register) is locked by writing 0x00000001 to it, but this clears the rest of the register bits, which appears to cause problems for the 'flash write_image' command with the unlock options specified. The subsequent sector erase fails with the WRPERR (write protect error) flag set in FLASH_SR. Instead, reading FLASH_OPTCR and ORing it with 0x00000001 seems to work fine.
Another solution would be to issue a reset after locking FLASH_OPTCR, which would reload the option byte configuration.
I can attach logs if necessary, but this seems pretty self-explanatory after looking at the source.
Thanks for spotting this, are you able to push a patch to our review server, if not then I am happy to do this.
perhaps the easiest thing todo @ line 375 is:
retval = target_write_u32(target, STM32_FLASH_OPTCR, optiondata | OPT_LOCK);
I'll get the patch in. It's about time I figure out the process for this
anyways, I use OpenOCD enough for it.
Last edit: Elliott Partridge 2014-05-29
patch submitted - http://openocd.zylin.com/2155
merged