From 9d745a0690f6f5c24d914ebfa3c1d6b824a19a5e Mon Sep 17 00:00:00 2001 From: Jens Bauer Date: Fri, 16 Jan 2015 23:57:09 +0100 Subject: [PATCH] stm32f2x: Fix byte order bug. Do not use buf_set_u32 on integers; they're not buffers. If using buf_set_u32 on integers, bytes will be exchanged on Big Endian targets. In this particular case, FLASH_OPTCR was incorrectly written, causing it to often contain one of these values: 0x00aaaae1, 0x00aaffef, 0x00ffabe1 or 0x00abffe1. This write-protected the device before flash-programming, causing this command... flash write_image erase unlock myfile.elf ... to fail, complaining about write-protection. Repeating the above command would change the OPTCR register each time. After applying this patch, the OPTCR remains "unchanged". Change-Id: I73d510fcc2e81a01973ad5c6e1aa22715ebd2743 Signed-off-by: Jens Bauer Reviewed-on: http://openocd.zylin.com/2466 Tested-by: jenkins Reviewed-by: Andreas Fritiofson --- src/flash/nor/stm32f2x.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/flash/nor/stm32f2x.c b/src/flash/nor/stm32f2x.c index 8f35845b45..422eac1b4f 100644 --- a/src/flash/nor/stm32f2x.c +++ b/src/flash/nor/stm32f2x.c @@ -344,8 +344,8 @@ static int stm32x_write_options(struct flash_bank *bank) /* rebuild option data */ optiondata = stm32x_info->option_bytes.user_options; - buf_set_u32(&optiondata, 8, 8, stm32x_info->option_bytes.RDP); - buf_set_u32(&optiondata, 16, 12, stm32x_info->option_bytes.protection); + optiondata |= stm32x_info->option_bytes.RDP << 8; + optiondata |= (stm32x_info->option_bytes.protection & 0x0fff) << 16; /* program options */ retval = target_write_u32(target, STM32_FLASH_OPTCR, optiondata); @@ -355,7 +355,7 @@ static int stm32x_write_options(struct flash_bank *bank) if (stm32x_info->has_large_mem) { uint32_t optiondata2 = 0; - buf_set_u32(&optiondata2, 16, 12, stm32x_info->option_bytes.protection >> 12); + optiondata2 |= (stm32x_info->option_bytes.protection & 0x00fff000) << 4; retval = target_write_u32(target, STM32_FLASH_OPTCR1, optiondata2); if (retval != ERROR_OK) return retval; -- 2.30.2