From 32d3063cf55fd4fc36d17b89647d86ea3c7cf9ab Mon Sep 17 00:00:00 2001 From: Bruno FLEURETTE Date: Tue, 10 Jan 2012 16:21:00 +0100 Subject: [PATCH] flash: pre-check flash unlock for stm32f2x MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit add checking of the current flash lock status before performing the unlock sequence (which would fail in an unlocked state) Change-Id: I693294c9cd2f59e69cb5bf3338120052fd680b1e Signed-off-by: Bruno FLEURETTE Signed-off-by: Øyvind Harboe Reviewed-on: http://openocd.zylin.com/363 Reviewed-by: Øyvind Harboe Reviewed-by: Spencer Oliver Tested-by: jenkins Reviewed-by: Mathias Küster --- src/flash/nor/stm32f2x.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/flash/nor/stm32f2x.c b/src/flash/nor/stm32f2x.c index 479df8af0..591b326b5 100644 --- a/src/flash/nor/stm32f2x.c +++ b/src/flash/nor/stm32f2x.c @@ -234,8 +234,18 @@ static int stm32x_unlock_reg(struct target *target) { uint32_t ctrl; + /* first check if not already unlocked + * otherwise writing on STM32_FLASH_KEYR will fail + */ + int retval = target_read_u32(target, STM32_FLASH_CR, &ctrl); + if (retval != ERROR_OK) + return retval; + + if ((ctrl & FLASH_LOCK) == 0) + return ERROR_OK; + /* unlock flash registers */ - int retval = target_write_u32(target, STM32_FLASH_KEYR, KEY1); + retval = target_write_u32(target, STM32_FLASH_KEYR, KEY1); if (retval != ERROR_OK) return retval; -- 2.11.4.GIT