1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2008 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
8 * Copyright (C) 2011 Øyvind Harboe *
9 * oyvind.harboe@zylin.com *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
23 ***************************************************************************/
30 #include <helper/binarybuffer.h>
31 #include <target/algorithm.h>
32 #include <target/armv7m.h>
34 /* Regarding performance:
36 * Short story - it might be best to leave the performance at
39 * You may see a jump in speed if you change to using
40 * 32bit words for the block programming.
42 * Its a shame you cannot use the double word as its
43 * even faster - but you require external VPP for that mode.
45 * Having said all that 16bit writes give us the widest vdd
46 * operating range, so may be worth adding a note to that effect.
50 /* Danger!!!! The STM32F1x and STM32F2x series actually have
51 * quite different flash controllers.
53 * What's more scary is that the names of the registers and their
54 * addresses are the same, but the actual bits and what they do are
55 * can be very different.
57 * To reduce testing complexity and dangers of regressions,
58 * a seperate file is used for stm32fx2x.
60 * Sector sizes in kiBytes:
61 * 1 MiByte part with 4 x 16, 1 x 64, 7 x 128.
62 * 1.5 MiByte part with 4 x 16, 1 x 64, 11 x 128.
63 * 2 MiByte part with 4 x 16, 1 x 64, 7 x 128, 4 x 16, 1 x 64, 7 x 128.
64 * 1 MiByte STM32F42x/43x part with DB1M Option set:
65 * 4 x 16, 1 x 64, 3 x 128, 4 x 16, 1 x 64, 3 x 128.
68 * 512 kiByte part with 4 x 16, 1 x 64, 3 x 128.
71 * 1 MiByte part with 4 x 32, 1 x 128, 3 x 256.
74 * 1 MiByte part in single bank mode with 4 x 32, 1 x 128, 3 x 256.
75 * 1 MiByte part in dual-bank mode two banks with 4 x 16, 1 x 64, 3 x 128 each.
76 * 2 MiByte part in single-bank mode with 4 x 32, 1 x 128, 7 x 256.
77 * 2 MiByte part in dual-bank mode two banks with 4 x 16, 1 x 64, 7 x 128 each.
79 * Protection size is sector size.
81 * Tested with STM3220F-EVAL board.
83 * STM32F4xx series for reference.
86 * http://www.st.com/web/en/resource/technical/document/reference_manual/DM00031020.pdf
89 * www.st.com/internet/com/TECHNICAL_RESOURCES/TECHNICAL_LITERATURE/
90 * PROGRAMMING_MANUAL/CD00233952.pdf
92 * STM32F7xx series for reference.
95 * http://www.st.com/web/en/resource/technical/document/reference_manual/DM00124865.pdf
98 * http://www.st.com/resource/en/reference_manual/dm00224583.pdf
101 * http://www.st.com/resource/en/reference_manual/dm00305666.pdf
104 * http://www.st.com/resource/en/reference_manual/dm00305990.pdf
106 * STM32F1x series - notice that this code was copy, pasted and knocked
107 * into a stm32f2x driver, so in case something has been converted or
108 * bugs haven't been fixed, here are the original manuals:
110 * RM0008 - Reference manual
112 * RM0042, the Flash programming manual for low-, medium- high-density and
113 * connectivity line STM32F10x devices
115 * PM0068, the Flash programming manual for XL-density STM32F10x devices.
119 /* Erase time can be as high as 1000ms, 10x this and it's toast... */
120 #define FLASH_ERASE_TIMEOUT 10000
121 #define FLASH_WRITE_TIMEOUT 5
123 /* Mass erase time can be as high as 32 s in x8 mode. */
124 #define FLASH_MASS_ERASE_TIMEOUT 33000
126 #define STM32_FLASH_BASE 0x40023c00
127 #define STM32_FLASH_ACR 0x40023c00
128 #define STM32_FLASH_KEYR 0x40023c04
129 #define STM32_FLASH_OPTKEYR 0x40023c08
130 #define STM32_FLASH_SR 0x40023c0C
131 #define STM32_FLASH_CR 0x40023c10
132 #define STM32_FLASH_OPTCR 0x40023c14
133 #define STM32_FLASH_OPTCR1 0x40023c18
134 #define STM32_FLASH_OPTCR2 0x40023c1c
136 /* FLASH_CR register bits */
137 #define FLASH_PG (1 << 0)
138 #define FLASH_SER (1 << 1)
139 #define FLASH_MER (1 << 2) /* MER/MER1 for f76x/77x */
140 #define FLASH_MER1 (1 << 15) /* MER2 for f76x/77x, confusing ... */
141 #define FLASH_STRT (1 << 16)
142 #define FLASH_PSIZE_8 (0 << 8)
143 #define FLASH_PSIZE_16 (1 << 8)
144 #define FLASH_PSIZE_32 (2 << 8)
145 #define FLASH_PSIZE_64 (3 << 8)
146 /* The sector number encoding is not straight binary for dual bank flash.
147 * Warning: evaluates the argument multiple times */
148 #define FLASH_SNB(a) ((((a) >= 12) ? 0x10 | ((a) - 12) : (a)) << 3)
149 #define FLASH_LOCK (1 << 31)
151 /* FLASH_SR register bits */
152 #define FLASH_BSY (1 << 16)
153 #define FLASH_PGSERR (1 << 7) /* Programming sequence error */
154 #define FLASH_PGPERR (1 << 6) /* Programming parallelism error */
155 #define FLASH_PGAERR (1 << 5) /* Programming alignment error */
156 #define FLASH_WRPERR (1 << 4) /* Write protection error */
157 #define FLASH_OPERR (1 << 1) /* Operation error */
159 #define FLASH_ERROR (FLASH_PGSERR | FLASH_PGPERR | FLASH_PGAERR | FLASH_WRPERR | FLASH_OPERR)
161 /* STM32_FLASH_OPTCR register bits */
162 #define OPTCR_LOCK (1 << 0)
163 #define OPTCR_START (1 << 1)
164 #define OPTCR_NDBANK (1 << 29) /* not dual bank mode */
165 #define OPTCR_DB1M (1 << 30) /* 1 MiB devices dual flash bank option */
166 #define OPTCR_SPRMOD (1 << 31) /* switches PCROPi/nWPRi interpretation */
168 /* STM32_FLASH_OPTCR2 register bits */
169 #define OPTCR2_PCROP_RDP (1 << 31) /* erase PCROP zone when decreasing RDP */
171 /* register unlock keys */
172 #define KEY1 0x45670123
173 #define KEY2 0xCDEF89AB
175 /* option register unlock key */
176 #define OPTKEY1 0x08192A3B
177 #define OPTKEY2 0x4C5D6E7F
179 struct stm32x_options
{
181 uint16_t user_options
; /* bit 0-7 usual options, bit 8-11 extra options */
184 uint32_t optcr2_pcrop
;
187 struct stm32x_flash_bank
{
188 struct stm32x_options option_bytes
;
190 bool has_large_mem
; /* F42x/43x/469/479/7xx in dual bank mode */
191 bool has_extra_options
; /* F42x/43x/469/479/7xx */
192 bool has_boot_addr
; /* F7xx */
193 bool has_optcr2_pcrop
; /* F72x/73x */
194 int protection_bits
; /* F413/423 */
195 uint32_t user_bank_size
;
198 /* flash bank stm32x <base> <size> 0 0 <target#>
200 FLASH_BANK_COMMAND_HANDLER(stm32x_flash_bank_command
)
202 struct stm32x_flash_bank
*stm32x_info
;
205 return ERROR_COMMAND_SYNTAX_ERROR
;
207 stm32x_info
= malloc(sizeof(struct stm32x_flash_bank
));
208 bank
->driver_priv
= stm32x_info
;
210 stm32x_info
->probed
= 0;
211 stm32x_info
->user_bank_size
= bank
->size
;
216 static inline int stm32x_get_flash_reg(struct flash_bank
*bank
, uint32_t reg
)
221 static inline int stm32x_get_flash_status(struct flash_bank
*bank
, uint32_t *status
)
223 struct target
*target
= bank
->target
;
224 return target_read_u32(target
, stm32x_get_flash_reg(bank
, STM32_FLASH_SR
), status
);
227 static int stm32x_wait_status_busy(struct flash_bank
*bank
, int timeout
)
229 struct target
*target
= bank
->target
;
231 int retval
= ERROR_OK
;
233 /* wait for busy to clear */
235 retval
= stm32x_get_flash_status(bank
, &status
);
236 if (retval
!= ERROR_OK
)
238 LOG_DEBUG("status: 0x%" PRIx32
"", status
);
239 if ((status
& FLASH_BSY
) == 0)
241 if (timeout
-- <= 0) {
242 LOG_ERROR("timed out waiting for flash");
249 if (status
& FLASH_WRPERR
) {
250 LOG_ERROR("stm32x device protected");
254 /* Clear but report errors */
255 if (status
& FLASH_ERROR
) {
256 /* If this operation fails, we ignore it and report the original
259 target_write_u32(target
, stm32x_get_flash_reg(bank
, STM32_FLASH_SR
),
260 status
& FLASH_ERROR
);
265 static int stm32x_unlock_reg(struct target
*target
)
269 /* first check if not already unlocked
270 * otherwise writing on STM32_FLASH_KEYR will fail
272 int retval
= target_read_u32(target
, STM32_FLASH_CR
, &ctrl
);
273 if (retval
!= ERROR_OK
)
276 if ((ctrl
& FLASH_LOCK
) == 0)
279 /* unlock flash registers */
280 retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY1
);
281 if (retval
!= ERROR_OK
)
284 retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY2
);
285 if (retval
!= ERROR_OK
)
288 retval
= target_read_u32(target
, STM32_FLASH_CR
, &ctrl
);
289 if (retval
!= ERROR_OK
)
292 if (ctrl
& FLASH_LOCK
) {
293 LOG_ERROR("flash not unlocked STM32_FLASH_CR: %" PRIx32
, ctrl
);
294 return ERROR_TARGET_FAILURE
;
300 static int stm32x_unlock_option_reg(struct target
*target
)
304 int retval
= target_read_u32(target
, STM32_FLASH_OPTCR
, &ctrl
);
305 if (retval
!= ERROR_OK
)
308 if ((ctrl
& OPTCR_LOCK
) == 0)
311 /* unlock option registers */
312 retval
= target_write_u32(target
, STM32_FLASH_OPTKEYR
, OPTKEY1
);
313 if (retval
!= ERROR_OK
)
316 retval
= target_write_u32(target
, STM32_FLASH_OPTKEYR
, OPTKEY2
);
317 if (retval
!= ERROR_OK
)
320 retval
= target_read_u32(target
, STM32_FLASH_OPTCR
, &ctrl
);
321 if (retval
!= ERROR_OK
)
324 if (ctrl
& OPTCR_LOCK
) {
325 LOG_ERROR("options not unlocked STM32_FLASH_OPTCR: %" PRIx32
, ctrl
);
326 return ERROR_TARGET_FAILURE
;
332 static int stm32x_read_options(struct flash_bank
*bank
)
335 struct stm32x_flash_bank
*stm32x_info
= NULL
;
336 struct target
*target
= bank
->target
;
338 stm32x_info
= bank
->driver_priv
;
340 /* read current option bytes */
341 int retval
= target_read_u32(target
, STM32_FLASH_OPTCR
, &optiondata
);
342 if (retval
!= ERROR_OK
)
345 /* caution: F2 implements 5 bits (WDG_SW only)
346 * whereas F7 6 bits (IWDG_SW and WWDG_SW) in user_options */
347 stm32x_info
->option_bytes
.user_options
= optiondata
& 0xfc;
348 stm32x_info
->option_bytes
.RDP
= (optiondata
>> 8) & 0xff;
349 stm32x_info
->option_bytes
.protection
=
350 (optiondata
>> 16) & (~(0xffff << stm32x_info
->protection_bits
) & 0xffff);
352 if (stm32x_info
->has_extra_options
) {
353 /* F42x/43x/469/479 and 7xx have up to 4 bits of extra options */
354 stm32x_info
->option_bytes
.user_options
|= (optiondata
>> 20) &
355 ((0xf00 << (stm32x_info
->protection_bits
- 12)) & 0xf00);
358 if (stm32x_info
->has_large_mem
|| stm32x_info
->has_boot_addr
) {
359 retval
= target_read_u32(target
, STM32_FLASH_OPTCR1
, &optiondata
);
360 if (retval
!= ERROR_OK
)
363 /* FLASH_OPTCR1 has quite diffent meanings ... */
364 if (stm32x_info
->has_boot_addr
) {
365 /* for F7xx it contains boot0 and boot1 */
366 stm32x_info
->option_bytes
.boot_addr
= optiondata
;
368 /* for F42x/43x/469/479 it contains 12 additional protection bits */
369 stm32x_info
->option_bytes
.protection
|= (optiondata
>> 4) & 0x00fff000;
373 if (stm32x_info
->has_optcr2_pcrop
) {
374 retval
= target_read_u32(target
, STM32_FLASH_OPTCR2
, &optiondata
);
375 if (retval
!= ERROR_OK
)
378 stm32x_info
->option_bytes
.optcr2_pcrop
= optiondata
;
379 if (stm32x_info
->has_optcr2_pcrop
&&
380 (stm32x_info
->option_bytes
.optcr2_pcrop
& ~OPTCR2_PCROP_RDP
)) {
381 LOG_INFO("PCROP Engaged");
384 stm32x_info
->option_bytes
.optcr2_pcrop
= 0x0;
387 if (stm32x_info
->option_bytes
.RDP
!= 0xAA)
388 LOG_INFO("Device Security Bit Set");
393 static int stm32x_write_options(struct flash_bank
*bank
)
395 struct stm32x_flash_bank
*stm32x_info
= NULL
;
396 struct target
*target
= bank
->target
;
397 uint32_t optiondata
, optiondata2
;
399 stm32x_info
= bank
->driver_priv
;
401 int retval
= stm32x_unlock_option_reg(target
);
402 if (retval
!= ERROR_OK
)
405 /* rebuild option data */
406 optiondata
= stm32x_info
->option_bytes
.user_options
& 0xfc;
407 optiondata
|= stm32x_info
->option_bytes
.RDP
<< 8;
408 optiondata
|= (stm32x_info
->option_bytes
.protection
&
409 (~(0xffff << stm32x_info
->protection_bits
))) << 16;
411 if (stm32x_info
->has_extra_options
) {
412 /* F42x/43x/469/479 and 7xx have up to 4 bits of extra options */
413 optiondata
|= (stm32x_info
->option_bytes
.user_options
&
414 ((0xf00 << (stm32x_info
->protection_bits
- 12)) & 0xf00)) << 20;
417 if (stm32x_info
->has_large_mem
|| stm32x_info
->has_boot_addr
) {
418 if (stm32x_info
->has_boot_addr
) {
419 /* F7xx uses FLASH_OPTCR1 for boot0 and boot1 ... */
420 optiondata2
= stm32x_info
->option_bytes
.boot_addr
;
422 /* F42x/43x/469/479 uses FLASH_OPTCR1 for additional protection bits */
423 optiondata2
= (stm32x_info
->option_bytes
.protection
& 0x00fff000) << 4;
426 retval
= target_write_u32(target
, STM32_FLASH_OPTCR1
, optiondata2
);
427 if (retval
!= ERROR_OK
)
431 /* program extra pcrop register */
432 if (stm32x_info
->has_optcr2_pcrop
) {
433 retval
= target_write_u32(target
, STM32_FLASH_OPTCR2
,
434 stm32x_info
->option_bytes
.optcr2_pcrop
);
435 if (retval
!= ERROR_OK
)
439 /* program options */
440 retval
= target_write_u32(target
, STM32_FLASH_OPTCR
, optiondata
);
441 if (retval
!= ERROR_OK
)
444 /* start programming cycle */
445 retval
= target_write_u32(target
, STM32_FLASH_OPTCR
, optiondata
| OPTCR_START
);
446 if (retval
!= ERROR_OK
)
449 /* wait for completion, this might trigger a security erase and take a while */
450 retval
= stm32x_wait_status_busy(bank
, FLASH_MASS_ERASE_TIMEOUT
);
451 if (retval
!= ERROR_OK
)
454 /* relock registers */
455 retval
= target_write_u32(target
, STM32_FLASH_OPTCR
, optiondata
| OPTCR_LOCK
);
456 if (retval
!= ERROR_OK
)
462 static int stm32x_protect_check(struct flash_bank
*bank
)
464 struct stm32x_flash_bank
*stm32x_info
= bank
->driver_priv
;
465 struct flash_sector
*prot_blocks
;
468 /* read write protection settings */
469 int retval
= stm32x_read_options(bank
);
470 if (retval
!= ERROR_OK
) {
471 LOG_DEBUG("unable to read option bytes");
475 if (bank
->prot_blocks
) {
476 num_prot_blocks
= bank
->num_prot_blocks
;
477 prot_blocks
= bank
->prot_blocks
;
479 num_prot_blocks
= bank
->num_sectors
;
480 prot_blocks
= bank
->sectors
;
483 for (int i
= 0; i
< num_prot_blocks
; i
++)
484 prot_blocks
[i
].is_protected
=
485 ~(stm32x_info
->option_bytes
.protection
>> i
) & 1;
490 static int stm32x_erase(struct flash_bank
*bank
, int first
, int last
)
492 struct target
*target
= bank
->target
;
495 assert((0 <= first
) && (first
<= last
) && (last
< bank
->num_sectors
));
497 if (bank
->target
->state
!= TARGET_HALTED
) {
498 LOG_ERROR("Target not halted");
499 return ERROR_TARGET_NOT_HALTED
;
503 retval
= stm32x_unlock_reg(target
);
504 if (retval
!= ERROR_OK
)
509 To erase a sector, follow the procedure below:
510 1. Check that no Flash memory operation is ongoing by checking the BSY bit in the
512 2. Set the SER bit and select the sector
513 you wish to erase (SNB) in the FLASH_CR register
514 3. Set the STRT bit in the FLASH_CR register
515 4. Wait for the BSY bit to be cleared
518 for (i
= first
; i
<= last
; i
++) {
519 retval
= target_write_u32(target
,
520 stm32x_get_flash_reg(bank
, STM32_FLASH_CR
), FLASH_SER
| FLASH_SNB(i
) | FLASH_STRT
);
521 if (retval
!= ERROR_OK
)
524 retval
= stm32x_wait_status_busy(bank
, FLASH_ERASE_TIMEOUT
);
525 if (retval
!= ERROR_OK
)
528 bank
->sectors
[i
].is_erased
= 1;
531 retval
= target_write_u32(target
, stm32x_get_flash_reg(bank
, STM32_FLASH_CR
), FLASH_LOCK
);
532 if (retval
!= ERROR_OK
)
538 static int stm32x_protect(struct flash_bank
*bank
, int set
, int first
, int last
)
540 struct target
*target
= bank
->target
;
541 struct stm32x_flash_bank
*stm32x_info
= bank
->driver_priv
;
543 if (target
->state
!= TARGET_HALTED
) {
544 LOG_ERROR("Target not halted");
545 return ERROR_TARGET_NOT_HALTED
;
548 /* read protection settings */
549 int retval
= stm32x_read_options(bank
);
550 if (retval
!= ERROR_OK
) {
551 LOG_DEBUG("unable to read option bytes");
555 for (int i
= first
; i
<= last
; i
++) {
557 stm32x_info
->option_bytes
.protection
&= ~(1 << i
);
559 stm32x_info
->option_bytes
.protection
|= (1 << i
);
562 retval
= stm32x_write_options(bank
);
563 if (retval
!= ERROR_OK
)
569 static int stm32x_write_block(struct flash_bank
*bank
, const uint8_t *buffer
,
570 uint32_t offset
, uint32_t count
)
572 struct target
*target
= bank
->target
;
573 uint32_t buffer_size
= 16384;
574 struct working_area
*write_algorithm
;
575 struct working_area
*source
;
576 uint32_t address
= bank
->base
+ offset
;
577 struct reg_param reg_params
[5];
578 struct armv7m_algorithm armv7m_info
;
579 int retval
= ERROR_OK
;
581 /* see contrib/loaders/flash/stm32f2x.S for src */
583 static const uint8_t stm32x_flash_write_code
[] = {
585 0xD0, 0xF8, 0x00, 0x80, /* ldr r8, [r0, #0] */
586 0xB8, 0xF1, 0x00, 0x0F, /* cmp r8, #0 */
587 0x1A, 0xD0, /* beq exit */
588 0x47, 0x68, /* ldr r7, [r0, #4] */
589 0x47, 0x45, /* cmp r7, r8 */
590 0xF7, 0xD0, /* beq wait_fifo */
592 0xDF, 0xF8, 0x34, 0x60, /* ldr r6, STM32_PROG16 */
593 0x26, 0x61, /* str r6, [r4, #STM32_FLASH_CR_OFFSET] */
594 0x37, 0xF8, 0x02, 0x6B, /* ldrh r6, [r7], #0x02 */
595 0x22, 0xF8, 0x02, 0x6B, /* strh r6, [r2], #0x02 */
596 0xBF, 0xF3, 0x4F, 0x8F, /* dsb sy */
598 0xE6, 0x68, /* ldr r6, [r4, #STM32_FLASH_SR_OFFSET] */
599 0x16, 0xF4, 0x80, 0x3F, /* tst r6, #0x10000 */
600 0xFB, 0xD1, /* bne busy */
601 0x16, 0xF0, 0xF0, 0x0F, /* tst r6, #0xf0 */
602 0x07, 0xD1, /* bne error */
604 0x8F, 0x42, /* cmp r7, r1 */
605 0x28, 0xBF, /* it cs */
606 0x00, 0xF1, 0x08, 0x07, /* addcs r7, r0, #8 */
607 0x47, 0x60, /* str r7, [r0, #4] */
608 0x01, 0x3B, /* subs r3, r3, #1 */
609 0x13, 0xB1, /* cbz r3, exit */
610 0xDF, 0xE7, /* b wait_fifo */
612 0x00, 0x21, /* movs r1, #0 */
613 0x41, 0x60, /* str r1, [r0, #4] */
615 0x30, 0x46, /* mov r0, r6 */
616 0x00, 0xBE, /* bkpt #0x00 */
618 /* <STM32_PROG16>: */
619 0x01, 0x01, 0x00, 0x00, /* .word 0x00000101 */
622 if (target_alloc_working_area(target
, sizeof(stm32x_flash_write_code
),
623 &write_algorithm
) != ERROR_OK
) {
624 LOG_WARNING("no working area available, can't do block memory writes");
625 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
628 retval
= target_write_buffer(target
, write_algorithm
->address
,
629 sizeof(stm32x_flash_write_code
),
630 stm32x_flash_write_code
);
631 if (retval
!= ERROR_OK
)
635 while (target_alloc_working_area_try(target
, buffer_size
, &source
) != ERROR_OK
) {
637 if (buffer_size
<= 256) {
638 /* we already allocated the writing code, but failed to get a
639 * buffer, free the algorithm */
640 target_free_working_area(target
, write_algorithm
);
642 LOG_WARNING("no large enough working area available, can't do block memory writes");
643 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
647 armv7m_info
.common_magic
= ARMV7M_COMMON_MAGIC
;
648 armv7m_info
.core_mode
= ARM_MODE_THREAD
;
650 init_reg_param(®_params
[0], "r0", 32, PARAM_IN_OUT
); /* buffer start, status (out) */
651 init_reg_param(®_params
[1], "r1", 32, PARAM_OUT
); /* buffer end */
652 init_reg_param(®_params
[2], "r2", 32, PARAM_OUT
); /* target address */
653 init_reg_param(®_params
[3], "r3", 32, PARAM_OUT
); /* count (halfword-16bit) */
654 init_reg_param(®_params
[4], "r4", 32, PARAM_OUT
); /* flash base */
656 buf_set_u32(reg_params
[0].value
, 0, 32, source
->address
);
657 buf_set_u32(reg_params
[1].value
, 0, 32, source
->address
+ source
->size
);
658 buf_set_u32(reg_params
[2].value
, 0, 32, address
);
659 buf_set_u32(reg_params
[3].value
, 0, 32, count
);
660 buf_set_u32(reg_params
[4].value
, 0, 32, STM32_FLASH_BASE
);
662 retval
= target_run_flash_async_algorithm(target
, buffer
, count
, 2,
665 source
->address
, source
->size
,
666 write_algorithm
->address
, 0,
669 if (retval
== ERROR_FLASH_OPERATION_FAILED
) {
670 LOG_ERROR("error executing stm32x flash write algorithm");
672 uint32_t error
= buf_get_u32(reg_params
[0].value
, 0, 32) & FLASH_ERROR
;
674 if (error
& FLASH_WRPERR
)
675 LOG_ERROR("flash memory write protected");
678 LOG_ERROR("flash write failed = %08" PRIx32
, error
);
679 /* Clear but report errors */
680 target_write_u32(target
, STM32_FLASH_SR
, error
);
685 target_free_working_area(target
, source
);
686 target_free_working_area(target
, write_algorithm
);
688 destroy_reg_param(®_params
[0]);
689 destroy_reg_param(®_params
[1]);
690 destroy_reg_param(®_params
[2]);
691 destroy_reg_param(®_params
[3]);
692 destroy_reg_param(®_params
[4]);
697 static int stm32x_write(struct flash_bank
*bank
, const uint8_t *buffer
,
698 uint32_t offset
, uint32_t count
)
700 struct target
*target
= bank
->target
;
701 uint32_t words_remaining
= (count
/ 2);
702 uint32_t bytes_remaining
= (count
& 0x00000001);
703 uint32_t address
= bank
->base
+ offset
;
704 uint32_t bytes_written
= 0;
707 if (bank
->target
->state
!= TARGET_HALTED
) {
708 LOG_ERROR("Target not halted");
709 return ERROR_TARGET_NOT_HALTED
;
713 LOG_WARNING("offset 0x%" PRIx32
" breaks required 2-byte alignment", offset
);
714 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
717 retval
= stm32x_unlock_reg(target
);
718 if (retval
!= ERROR_OK
)
721 /* multiple half words (2-byte) to be programmed? */
722 if (words_remaining
> 0) {
723 /* try using a block write */
724 retval
= stm32x_write_block(bank
, buffer
, offset
, words_remaining
);
725 if (retval
!= ERROR_OK
) {
726 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
) {
727 /* if block write failed (no sufficient working area),
728 * we use normal (slow) single dword accesses */
729 LOG_WARNING("couldn't use block writes, falling back to single memory accesses");
732 buffer
+= words_remaining
* 2;
733 address
+= words_remaining
* 2;
738 if ((retval
!= ERROR_OK
) && (retval
!= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
))
743 The Flash memory programming sequence is as follows:
744 1. Check that no main Flash memory operation is ongoing by checking the BSY bit in the
746 2. Set the PG bit in the FLASH_CR register
747 3. Perform the data write operation(s) to the desired memory address (inside main
748 memory block or OTP area):
749 – – Half-word access in case of x16 parallelism
750 – Word access in case of x32 parallelism
753 Byte access in case of x8 parallelism
754 Double word access in case of x64 parallelism
755 Wait for the BSY bit to be cleared
757 while (words_remaining
> 0) {
759 memcpy(&value
, buffer
+ bytes_written
, sizeof(uint16_t));
761 retval
= target_write_u32(target
, stm32x_get_flash_reg(bank
, STM32_FLASH_CR
),
762 FLASH_PG
| FLASH_PSIZE_16
);
763 if (retval
!= ERROR_OK
)
766 retval
= target_write_u16(target
, address
, value
);
767 if (retval
!= ERROR_OK
)
770 retval
= stm32x_wait_status_busy(bank
, FLASH_WRITE_TIMEOUT
);
771 if (retval
!= ERROR_OK
)
779 if (bytes_remaining
) {
780 retval
= target_write_u32(target
, stm32x_get_flash_reg(bank
, STM32_FLASH_CR
),
781 FLASH_PG
| FLASH_PSIZE_8
);
782 if (retval
!= ERROR_OK
)
784 retval
= target_write_u8(target
, address
, buffer
[bytes_written
]);
785 if (retval
!= ERROR_OK
)
788 retval
= stm32x_wait_status_busy(bank
, FLASH_WRITE_TIMEOUT
);
789 if (retval
!= ERROR_OK
)
793 return target_write_u32(target
, STM32_FLASH_CR
, FLASH_LOCK
);
796 static int setup_sector(struct flash_bank
*bank
, int start
, int num
, int size
)
799 for (int i
= start
; i
< (start
+ num
) ; i
++) {
800 assert(i
< bank
->num_sectors
);
801 bank
->sectors
[i
].offset
= bank
->size
;
802 bank
->sectors
[i
].size
= size
;
803 bank
->size
+= bank
->sectors
[i
].size
;
804 LOG_DEBUG("sector %d: %dkBytes", i
, size
>> 10);
810 static void setup_bank(struct flash_bank
*bank
, int start
,
811 uint16_t flash_size_in_kb
, uint16_t max_sector_size_in_kb
)
815 start
= setup_sector(bank
, start
, 4, (max_sector_size_in_kb
/ 8) * 1024);
816 start
= setup_sector(bank
, start
, 1, (max_sector_size_in_kb
/ 2) * 1024);
818 /* remaining sectors all of size max_sector_size_in_kb */
819 remain
= (flash_size_in_kb
/ max_sector_size_in_kb
) - 1;
820 start
= setup_sector(bank
, start
, remain
, max_sector_size_in_kb
* 1024);
823 static int stm32x_get_device_id(struct flash_bank
*bank
, uint32_t *device_id
)
825 /* this checks for a stm32f4x errata issue where a
826 * stm32f2x DBGMCU_IDCODE is incorrectly returned.
827 * If the issue is detected target is forced to stm32f4x Rev A.
828 * Only effects Rev A silicon */
830 struct target
*target
= bank
->target
;
833 /* read stm32 device id register */
834 int retval
= target_read_u32(target
, 0xE0042000, device_id
);
835 if (retval
!= ERROR_OK
)
838 if ((*device_id
& 0xfff) == 0x411) {
839 /* read CPUID reg to check core type */
840 retval
= target_read_u32(target
, 0xE000ED00, &cpuid
);
841 if (retval
!= ERROR_OK
)
844 /* check for cortex_m4 */
845 if (((cpuid
>> 4) & 0xFFF) == 0xC24) {
846 *device_id
&= ~((0xFFFF << 16) | 0xfff);
847 *device_id
|= (0x1000 << 16) | 0x413;
848 LOG_INFO("stm32f4x errata detected - fixing incorrect MCU_IDCODE");
854 static int stm32x_probe(struct flash_bank
*bank
)
856 struct target
*target
= bank
->target
;
857 struct stm32x_flash_bank
*stm32x_info
= bank
->driver_priv
;
858 int i
, num_prot_blocks
;
859 uint16_t flash_size_in_kb
;
860 uint32_t flash_size_reg
= 0x1FFF7A22;
861 uint16_t max_sector_size_in_kb
= 128;
862 uint16_t max_flash_size_in_kb
;
864 uint32_t base_address
= 0x08000000;
866 stm32x_info
->probed
= 0;
867 stm32x_info
->has_large_mem
= false;
868 stm32x_info
->has_boot_addr
= false;
869 stm32x_info
->has_extra_options
= false;
870 stm32x_info
->has_optcr2_pcrop
= false;
871 stm32x_info
->protection_bits
= 12; /* max. number of nWRPi bits (in FLASH_OPTCR !!!) */
876 bank
->num_sectors
= 0;
877 bank
->sectors
= NULL
;
880 if (bank
->prot_blocks
) {
881 free(bank
->prot_blocks
);
882 bank
->num_prot_blocks
= 0;
883 bank
->prot_blocks
= NULL
;
886 /* read stm32 device id register */
887 int retval
= stm32x_get_device_id(bank
, &device_id
);
888 if (retval
!= ERROR_OK
)
890 LOG_INFO("device id = 0x%08" PRIx32
"", device_id
);
891 device_id
&= 0xfff; /* only bits 0-11 are used further on */
893 /* set max flash size depending on family, id taken from AN2606 */
895 case 0x411: /* F20x/21x */
896 case 0x413: /* F40x/41x */
897 max_flash_size_in_kb
= 1024;
900 case 0x419: /* F42x/43x */
901 case 0x434: /* F469/479 */
902 stm32x_info
->has_extra_options
= true;
903 max_flash_size_in_kb
= 2048;
906 case 0x423: /* F401xB/C */
907 max_flash_size_in_kb
= 256;
910 case 0x421: /* F446 */
911 case 0x431: /* F411 */
912 case 0x433: /* F401xD/E */
913 case 0x441: /* F412 */
914 max_flash_size_in_kb
= 512;
917 case 0x458: /* F410 */
918 max_flash_size_in_kb
= 128;
921 case 0x449: /* F74x/75x */
922 max_flash_size_in_kb
= 1024;
923 max_sector_size_in_kb
= 256;
924 flash_size_reg
= 0x1FF0F442;
925 stm32x_info
->has_extra_options
= true;
926 stm32x_info
->has_boot_addr
= true;
929 case 0x451: /* F76x/77x */
930 max_flash_size_in_kb
= 2048;
931 max_sector_size_in_kb
= 256;
932 flash_size_reg
= 0x1FF0F442;
933 stm32x_info
->has_extra_options
= true;
934 stm32x_info
->has_boot_addr
= true;
937 case 0x452: /* F72x/73x */
938 max_flash_size_in_kb
= 512;
939 flash_size_reg
= 0x1FF07A22; /* yes, 0x1FF*0*7A22, not 0x1FF*F*7A22 */
940 stm32x_info
->has_extra_options
= true;
941 stm32x_info
->has_boot_addr
= true;
942 stm32x_info
->has_optcr2_pcrop
= true;
945 case 0x463: /* F413x/423x */
946 max_flash_size_in_kb
= 1536;
947 stm32x_info
->has_extra_options
= true;
948 stm32x_info
->protection_bits
= 15;
949 num_prot_blocks
= 15;
953 LOG_WARNING("Cannot identify target as a STM32 family.");
957 /* get flash size from target. */
958 retval
= target_read_u16(target
, flash_size_reg
, &flash_size_in_kb
);
960 /* failed reading flash size or flash size invalid (early silicon),
961 * default to max target family */
962 if (retval
!= ERROR_OK
|| flash_size_in_kb
== 0xffff || flash_size_in_kb
== 0) {
963 LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming %dk flash",
964 max_flash_size_in_kb
);
965 flash_size_in_kb
= max_flash_size_in_kb
;
968 /* if the user sets the size manually then ignore the probed value
969 * this allows us to work around devices that have a invalid flash size register value */
970 if (stm32x_info
->user_bank_size
) {
971 LOG_INFO("ignoring flash probed value, using configured bank size");
972 flash_size_in_kb
= stm32x_info
->user_bank_size
/ 1024;
975 LOG_INFO("flash size = %dkbytes", flash_size_in_kb
);
977 /* did we assign flash size? */
978 assert(flash_size_in_kb
!= 0xffff);
980 /* F42x/43x/469/479 1024 kiByte devices have a dual bank option */
981 if ((device_id
== 0x419) || (device_id
== 0x434)) {
983 retval
= target_read_u32(target
, STM32_FLASH_OPTCR
, &optiondata
);
984 if (retval
!= ERROR_OK
) {
985 LOG_DEBUG("unable to read option bytes");
988 if ((flash_size_in_kb
> 1024) || (optiondata
& OPTCR_DB1M
)) {
989 stm32x_info
->has_large_mem
= true;
990 LOG_INFO("Dual Bank %d kiB STM32F42x/43x/469/479 found", flash_size_in_kb
);
992 stm32x_info
->has_large_mem
= false;
993 LOG_INFO("Single Bank %d kiB STM32F42x/43x/469/479 found", flash_size_in_kb
);
997 /* F76x/77x devices have a dual bank option */
998 if (device_id
== 0x451) {
1000 retval
= target_read_u32(target
, STM32_FLASH_OPTCR
, &optiondata
);
1001 if (retval
!= ERROR_OK
) {
1002 LOG_DEBUG("unable to read option bytes");
1005 if (optiondata
& OPTCR_NDBANK
) {
1006 stm32x_info
->has_large_mem
= false;
1007 LOG_INFO("Single Bank %d kiB STM32F76x/77x found", flash_size_in_kb
);
1009 stm32x_info
->has_large_mem
= true;
1010 max_sector_size_in_kb
>>= 1; /* sector size divided by 2 in dual-bank mode */
1011 LOG_INFO("Dual Bank %d kiB STM32F76x/77x found", flash_size_in_kb
);
1015 /* calculate numbers of pages */
1016 int num_pages
= flash_size_in_kb
/ max_sector_size_in_kb
1017 + (stm32x_info
->has_large_mem
? 8 : 4);
1019 bank
->base
= base_address
;
1020 bank
->num_sectors
= num_pages
;
1021 bank
->sectors
= malloc(sizeof(struct flash_sector
) * num_pages
);
1022 for (i
= 0; i
< num_pages
; i
++) {
1023 bank
->sectors
[i
].is_erased
= -1;
1024 bank
->sectors
[i
].is_protected
= 0;
1027 LOG_DEBUG("allocated %d sectors", num_pages
);
1029 /* F76x/77x in dual bank mode */
1030 if ((device_id
== 0x451) && stm32x_info
->has_large_mem
)
1031 num_prot_blocks
= num_pages
>> 1;
1033 if (num_prot_blocks
) {
1034 bank
->prot_blocks
= malloc(sizeof(struct flash_sector
) * num_prot_blocks
);
1035 for (i
= 0; i
< num_prot_blocks
; i
++)
1036 bank
->prot_blocks
[i
].is_protected
= 0;
1037 LOG_DEBUG("allocated %d prot blocks", num_prot_blocks
);
1040 if (stm32x_info
->has_large_mem
) {
1042 setup_bank(bank
, 0, flash_size_in_kb
>> 1, max_sector_size_in_kb
);
1043 setup_bank(bank
, num_pages
>> 1, flash_size_in_kb
>> 1,
1044 max_sector_size_in_kb
);
1046 /* F767x/F77x in dual mode, one protection bit refers to two adjacent sectors */
1047 if (device_id
== 0x451) {
1048 for (i
= 0; i
< num_prot_blocks
; i
++) {
1049 bank
->prot_blocks
[i
].offset
= bank
->sectors
[i
<< 1].offset
;
1050 bank
->prot_blocks
[i
].size
= bank
->sectors
[i
<< 1].size
1051 + bank
->sectors
[(i
<< 1) + 1].size
;
1056 setup_bank(bank
, 0, flash_size_in_kb
, max_sector_size_in_kb
);
1058 /* F413/F423, sectors 14 and 15 share one common protection bit */
1059 if (device_id
== 0x463) {
1060 for (i
= 0; i
< num_prot_blocks
; i
++) {
1061 bank
->prot_blocks
[i
].offset
= bank
->sectors
[i
].offset
;
1062 bank
->prot_blocks
[i
].size
= bank
->sectors
[i
].size
;
1064 bank
->prot_blocks
[num_prot_blocks
- 1].size
<<= 1;
1067 bank
->num_prot_blocks
= num_prot_blocks
;
1068 assert((bank
->size
>> 10) == flash_size_in_kb
);
1070 stm32x_info
->probed
= 1;
1074 static int stm32x_auto_probe(struct flash_bank
*bank
)
1076 struct stm32x_flash_bank
*stm32x_info
= bank
->driver_priv
;
1077 if (stm32x_info
->probed
)
1079 return stm32x_probe(bank
);
1082 static int get_stm32x_info(struct flash_bank
*bank
, char *buf
, int buf_size
)
1084 uint32_t dbgmcu_idcode
;
1086 /* read stm32 device id register */
1087 int retval
= stm32x_get_device_id(bank
, &dbgmcu_idcode
);
1088 if (retval
!= ERROR_OK
)
1091 uint16_t device_id
= dbgmcu_idcode
& 0xfff;
1092 uint16_t rev_id
= dbgmcu_idcode
>> 16;
1093 const char *device_str
;
1094 const char *rev_str
= NULL
;
1096 switch (device_id
) {
1098 device_str
= "STM32F2xx";
1138 device_str
= "STM32F4xx";
1164 device_str
= "STM32F446";
1178 device_str
= "STM32F4xx (Low Power)";
1200 device_str
= "STM32F7[4|5]x";
1214 device_str
= "STM32F7[6|7]x";
1224 device_str
= "STM32F7[2|3]x";
1234 device_str
= "STM32F4[1|2]3";
1244 snprintf(buf
, buf_size
, "Cannot identify target as a STM32F2/4/7\n");
1248 if (rev_str
!= NULL
)
1249 snprintf(buf
, buf_size
, "%s - Rev: %s", device_str
, rev_str
);
1251 snprintf(buf
, buf_size
, "%s - Rev: unknown (0x%04x)", device_str
, rev_id
);
1256 COMMAND_HANDLER(stm32x_handle_lock_command
)
1258 struct target
*target
= NULL
;
1259 struct stm32x_flash_bank
*stm32x_info
= NULL
;
1262 return ERROR_COMMAND_SYNTAX_ERROR
;
1264 struct flash_bank
*bank
;
1265 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
1266 if (ERROR_OK
!= retval
)
1269 stm32x_info
= bank
->driver_priv
;
1270 target
= bank
->target
;
1272 if (target
->state
!= TARGET_HALTED
) {
1273 LOG_INFO("Target not halted");
1274 /* return ERROR_TARGET_NOT_HALTED; */
1277 if (stm32x_read_options(bank
) != ERROR_OK
) {
1278 command_print(CMD_CTX
, "%s failed to read options", bank
->driver
->name
);
1282 /* set readout protection */
1283 stm32x_info
->option_bytes
.RDP
= 0;
1285 if (stm32x_write_options(bank
) != ERROR_OK
) {
1286 command_print(CMD_CTX
, "%s failed to lock device", bank
->driver
->name
);
1290 command_print(CMD_CTX
, "%s locked", bank
->driver
->name
);
1295 COMMAND_HANDLER(stm32x_handle_unlock_command
)
1297 struct target
*target
= NULL
;
1298 struct stm32x_flash_bank
*stm32x_info
= NULL
;
1301 return ERROR_COMMAND_SYNTAX_ERROR
;
1303 struct flash_bank
*bank
;
1304 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
1305 if (ERROR_OK
!= retval
)
1308 stm32x_info
= bank
->driver_priv
;
1309 target
= bank
->target
;
1311 if (target
->state
!= TARGET_HALTED
) {
1312 LOG_INFO("Target not halted");
1313 /* return ERROR_TARGET_NOT_HALTED; */
1316 if (stm32x_read_options(bank
) != ERROR_OK
) {
1317 command_print(CMD_CTX
, "%s failed to read options", bank
->driver
->name
);
1321 /* clear readout protection and complementary option bytes
1322 * this will also force a device unlock if set */
1323 stm32x_info
->option_bytes
.RDP
= 0xAA;
1324 if (stm32x_info
->has_optcr2_pcrop
) {
1325 stm32x_info
->option_bytes
.optcr2_pcrop
= OPTCR2_PCROP_RDP
| (~1U << bank
->num_sectors
);
1328 if (stm32x_write_options(bank
) != ERROR_OK
) {
1329 command_print(CMD_CTX
, "%s failed to unlock device", bank
->driver
->name
);
1333 command_print(CMD_CTX
, "%s unlocked.\n"
1334 "INFO: a reset or power cycle is required "
1335 "for the new settings to take effect.", bank
->driver
->name
);
1340 static int stm32x_mass_erase(struct flash_bank
*bank
)
1344 struct target
*target
= bank
->target
;
1345 struct stm32x_flash_bank
*stm32x_info
= NULL
;
1347 if (target
->state
!= TARGET_HALTED
) {
1348 LOG_ERROR("Target not halted");
1349 return ERROR_TARGET_NOT_HALTED
;
1352 stm32x_info
= bank
->driver_priv
;
1354 retval
= stm32x_unlock_reg(target
);
1355 if (retval
!= ERROR_OK
)
1358 /* mass erase flash memory */
1359 if (stm32x_info
->has_large_mem
)
1360 flash_mer
= FLASH_MER
| FLASH_MER1
;
1362 flash_mer
= FLASH_MER
;
1364 retval
= target_write_u32(target
, stm32x_get_flash_reg(bank
, STM32_FLASH_CR
), flash_mer
);
1365 if (retval
!= ERROR_OK
)
1367 retval
= target_write_u32(target
, stm32x_get_flash_reg(bank
, STM32_FLASH_CR
),
1368 flash_mer
| FLASH_STRT
);
1369 if (retval
!= ERROR_OK
)
1372 retval
= stm32x_wait_status_busy(bank
, FLASH_MASS_ERASE_TIMEOUT
);
1373 if (retval
!= ERROR_OK
)
1376 retval
= target_write_u32(target
, stm32x_get_flash_reg(bank
, STM32_FLASH_CR
), FLASH_LOCK
);
1377 if (retval
!= ERROR_OK
)
1383 COMMAND_HANDLER(stm32x_handle_mass_erase_command
)
1388 command_print(CMD_CTX
, "stm32x mass_erase <bank>");
1389 return ERROR_COMMAND_SYNTAX_ERROR
;
1392 struct flash_bank
*bank
;
1393 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
1394 if (ERROR_OK
!= retval
)
1397 retval
= stm32x_mass_erase(bank
);
1398 if (retval
== ERROR_OK
) {
1399 /* set all sectors as erased */
1400 for (i
= 0; i
< bank
->num_sectors
; i
++)
1401 bank
->sectors
[i
].is_erased
= 1;
1403 command_print(CMD_CTX
, "stm32x mass erase complete");
1405 command_print(CMD_CTX
, "stm32x mass erase failed");
1411 COMMAND_HANDLER(stm32f2x_handle_options_read_command
)
1414 struct flash_bank
*bank
;
1415 struct stm32x_flash_bank
*stm32x_info
= NULL
;
1417 if (CMD_ARGC
!= 1) {
1418 command_print(CMD_CTX
, "stm32f2x options_read <bank>");
1419 return ERROR_COMMAND_SYNTAX_ERROR
;
1422 retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
1423 if (ERROR_OK
!= retval
)
1426 retval
= stm32x_read_options(bank
);
1427 if (ERROR_OK
!= retval
)
1430 stm32x_info
= bank
->driver_priv
;
1431 if (stm32x_info
->has_extra_options
) {
1432 if (stm32x_info
->has_boot_addr
) {
1433 uint32_t boot_addr
= stm32x_info
->option_bytes
.boot_addr
;
1435 command_print(CMD_CTX
, "stm32f2x user_options 0x%03X,"
1436 " boot_add0 0x%04X, boot_add1 0x%04X",
1437 stm32x_info
->option_bytes
.user_options
,
1438 boot_addr
& 0xffff, (boot_addr
& 0xffff0000) >> 16);
1439 if (stm32x_info
->has_optcr2_pcrop
) {
1440 command_print(CMD_CTX
, "stm32f2x optcr2_pcrop 0x%08X",
1441 stm32x_info
->option_bytes
.optcr2_pcrop
);
1444 command_print(CMD_CTX
, "stm32f2x user_options 0x%03X",
1445 stm32x_info
->option_bytes
.user_options
);
1448 command_print(CMD_CTX
, "stm32f2x user_options 0x%02X",
1449 stm32x_info
->option_bytes
.user_options
);
1456 COMMAND_HANDLER(stm32f2x_handle_options_write_command
)
1459 struct flash_bank
*bank
;
1460 struct stm32x_flash_bank
*stm32x_info
= NULL
;
1461 uint16_t user_options
, boot_addr0
, boot_addr1
, options_mask
;
1464 command_print(CMD_CTX
, "stm32f2x options_write <bank> ...");
1465 return ERROR_COMMAND_SYNTAX_ERROR
;
1468 retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
1469 if (ERROR_OK
!= retval
)
1472 retval
= stm32x_read_options(bank
);
1473 if (ERROR_OK
!= retval
)
1476 stm32x_info
= bank
->driver_priv
;
1477 if (stm32x_info
->has_boot_addr
) {
1478 if (CMD_ARGC
!= 4) {
1479 command_print(CMD_CTX
, "stm32f2x options_write <bank> <user_options>"
1480 " <boot_addr0> <boot_addr1>");
1481 return ERROR_COMMAND_SYNTAX_ERROR
;
1483 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[2], boot_addr0
);
1484 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[3], boot_addr1
);
1485 stm32x_info
->option_bytes
.boot_addr
= boot_addr0
| (((uint32_t) boot_addr1
) << 16);
1487 if (CMD_ARGC
!= 2) {
1488 command_print(CMD_CTX
, "stm32f2x options_write <bank> <user_options>");
1489 return ERROR_COMMAND_SYNTAX_ERROR
;
1493 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[1], user_options
);
1494 options_mask
= !stm32x_info
->has_extra_options
? ~0xfc :
1495 ~(((0xf00 << (stm32x_info
->protection_bits
- 12)) | 0xff) & 0xffc);
1496 if (user_options
& options_mask
) {
1497 command_print(CMD_CTX
, "stm32f2x invalid user_options");
1498 return ERROR_COMMAND_ARGUMENT_INVALID
;
1501 stm32x_info
->option_bytes
.user_options
= user_options
;
1503 if (stm32x_write_options(bank
) != ERROR_OK
) {
1504 command_print(CMD_CTX
, "stm32f2x failed to write options");
1508 /* switching between single- and dual-bank modes requires re-probe */
1509 /* ... and reprogramming of whole flash */
1510 stm32x_info
->probed
= 0;
1512 command_print(CMD_CTX
, "stm32f2x write options complete.\n"
1513 "INFO: a reset or power cycle is required "
1514 "for the new settings to take effect.");
1518 COMMAND_HANDLER(stm32f2x_handle_optcr2_write_command
)
1521 struct flash_bank
*bank
;
1522 struct stm32x_flash_bank
*stm32x_info
= NULL
;
1523 uint32_t optcr2_pcrop
;
1525 if (CMD_ARGC
!= 2) {
1526 command_print(CMD_CTX
, "stm32f2x optcr2_write <bank> <optcr2_value>");
1527 return ERROR_COMMAND_SYNTAX_ERROR
;
1530 retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
1531 if (ERROR_OK
!= retval
)
1534 stm32x_info
= bank
->driver_priv
;
1535 if (!stm32x_info
->has_optcr2_pcrop
) {
1536 command_print(CMD_CTX
, "no optcr2 register");
1537 return ERROR_COMMAND_ARGUMENT_INVALID
;
1540 command_print(CMD_CTX
, "INFO: To disable PCROP, set PCROP_RDP"
1541 " with PCROPi bits STILL SET, then\nlock device and"
1542 " finally unlock it. Clears PCROP and mass erases flash.");
1544 retval
= stm32x_read_options(bank
);
1545 if (ERROR_OK
!= retval
)
1548 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], optcr2_pcrop
);
1549 stm32x_info
->option_bytes
.optcr2_pcrop
= optcr2_pcrop
;
1551 if (stm32x_write_options(bank
) != ERROR_OK
) {
1552 command_print(CMD_CTX
, "stm32f2x failed to write options");
1556 command_print(CMD_CTX
, "stm32f2x optcr2_write complete.");
1560 static const struct command_registration stm32x_exec_command_handlers
[] = {
1563 .handler
= stm32x_handle_lock_command
,
1564 .mode
= COMMAND_EXEC
,
1566 .help
= "Lock entire flash device.",
1570 .handler
= stm32x_handle_unlock_command
,
1571 .mode
= COMMAND_EXEC
,
1573 .help
= "Unlock entire protected flash device.",
1576 .name
= "mass_erase",
1577 .handler
= stm32x_handle_mass_erase_command
,
1578 .mode
= COMMAND_EXEC
,
1580 .help
= "Erase entire flash device.",
1583 .name
= "options_read",
1584 .handler
= stm32f2x_handle_options_read_command
,
1585 .mode
= COMMAND_EXEC
,
1587 .help
= "Read and display device option bytes.",
1590 .name
= "options_write",
1591 .handler
= stm32f2x_handle_options_write_command
,
1592 .mode
= COMMAND_EXEC
,
1593 .usage
= "bank_id user_options [ boot_add0 boot_add1 ]",
1594 .help
= "Write option bytes",
1597 .name
= "optcr2_write",
1598 .handler
= stm32f2x_handle_optcr2_write_command
,
1599 .mode
= COMMAND_EXEC
,
1600 .usage
= "bank_id optcr2",
1601 .help
= "Write optcr2 word",
1604 COMMAND_REGISTRATION_DONE
1607 static const struct command_registration stm32x_command_handlers
[] = {
1610 .mode
= COMMAND_ANY
,
1611 .help
= "stm32f2x flash command group",
1613 .chain
= stm32x_exec_command_handlers
,
1615 COMMAND_REGISTRATION_DONE
1618 struct flash_driver stm32f2x_flash
= {
1620 .commands
= stm32x_command_handlers
,
1621 .flash_bank_command
= stm32x_flash_bank_command
,
1622 .erase
= stm32x_erase
,
1623 .protect
= stm32x_protect
,
1624 .write
= stm32x_write
,
1625 .read
= default_flash_read
,
1626 .probe
= stm32x_probe
,
1627 .auto_probe
= stm32x_auto_probe
,
1628 .erase_check
= default_flash_blank_check
,
1629 .protect_check
= stm32x_protect_check
,
1630 .info
= get_stm32x_info
,
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)