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 by Clement Burin des Roziers *
9 * clement.burin-des-roziers@hikob.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>
33 #include <target/cortex_m.h>
35 /* stm32lx flash register locations */
37 #define FLASH_ACR 0x00
38 #define FLASH_PECR 0x04
39 #define FLASH_PDKEYR 0x08
40 #define FLASH_PEKEYR 0x0C
41 #define FLASH_PRGKEYR 0x10
42 #define FLASH_OPTKEYR 0x14
44 #define FLASH_OBR 0x1C
45 #define FLASH_WRPR 0x20
48 #define FLASH_ACR__LATENCY (1<<0)
49 #define FLASH_ACR__PRFTEN (1<<1)
50 #define FLASH_ACR__ACC64 (1<<2)
51 #define FLASH_ACR__SLEEP_PD (1<<3)
52 #define FLASH_ACR__RUN_PD (1<<4)
55 #define FLASH_PECR__PELOCK (1<<0)
56 #define FLASH_PECR__PRGLOCK (1<<1)
57 #define FLASH_PECR__OPTLOCK (1<<2)
58 #define FLASH_PECR__PROG (1<<3)
59 #define FLASH_PECR__DATA (1<<4)
60 #define FLASH_PECR__FTDW (1<<8)
61 #define FLASH_PECR__ERASE (1<<9)
62 #define FLASH_PECR__FPRG (1<<10)
63 #define FLASH_PECR__EOPIE (1<<16)
64 #define FLASH_PECR__ERRIE (1<<17)
65 #define FLASH_PECR__OBL_LAUNCH (1<<18)
68 #define FLASH_SR__BSY (1<<0)
69 #define FLASH_SR__EOP (1<<1)
70 #define FLASH_SR__ENDHV (1<<2)
71 #define FLASH_SR__READY (1<<3)
72 #define FLASH_SR__WRPERR (1<<8)
73 #define FLASH_SR__PGAERR (1<<9)
74 #define FLASH_SR__SIZERR (1<<10)
75 #define FLASH_SR__OPTVERR (1<<11)
78 #define PEKEY1 0x89ABCDEF
79 #define PEKEY2 0x02030405
80 #define PRGKEY1 0x8C9DAEBF
81 #define PRGKEY2 0x13141516
82 #define OPTKEY1 0xFBEAD9C8
83 #define OPTKEY2 0x24252627
86 #define DBGMCU_IDCODE 0xE0042000
87 #define DBGMCU_IDCODE_L0 0x40015800
90 #define FLASH_SECTOR_SIZE 4096
91 #define FLASH_BANK0_ADDRESS 0x08000000
94 #define OPTION_BYTES_ADDRESS 0x1FF80000
96 #define OPTION_BYTE_0_PR1 0xFFFF0000
97 #define OPTION_BYTE_0_PR0 0xFF5500AA
99 static int stm32lx_unlock_program_memory(struct flash_bank
*bank
);
100 static int stm32lx_lock_program_memory(struct flash_bank
*bank
);
101 static int stm32lx_enable_write_half_page(struct flash_bank
*bank
);
102 static int stm32lx_erase_sector(struct flash_bank
*bank
, int sector
);
103 static int stm32lx_wait_until_bsy_clear(struct flash_bank
*bank
);
104 static int stm32lx_lock(struct flash_bank
*bank
);
105 static int stm32lx_unlock(struct flash_bank
*bank
);
106 static int stm32lx_mass_erase(struct flash_bank
*bank
);
107 static int stm32lx_wait_until_bsy_clear_timeout(struct flash_bank
*bank
, int timeout
);
108 static int stm32lx_update_part_info(struct flash_bank
*bank
, uint16_t flash_size_in_kb
);
115 struct stm32lx_part_info
{
117 const char *device_str
;
118 const struct stm32lx_rev
*revs
;
120 unsigned int page_size
;
121 unsigned int pages_per_sector
;
122 uint16_t max_flash_size_kb
;
123 uint16_t first_bank_size_kb
; /* used when has_dual_banks is true */
126 uint32_t flash_base
; /* Flash controller registers location */
127 uint32_t fsize_base
; /* Location of FSIZE register */
130 struct stm32lx_flash_bank
{
133 uint32_t user_bank_size
;
136 struct stm32lx_part_info part_info
;
139 static const struct stm32lx_rev stm32_416_revs
[] = {
140 { 0x1000, "A" }, { 0x1008, "Y" }, { 0x1038, "W" }, { 0x1078, "V" },
142 static const struct stm32lx_rev stm32_417_revs
[] = {
143 { 0x1000, "A" }, { 0x1008, "Z" }, { 0x1018, "Y" }, { 0x1038, "X" }
145 static const struct stm32lx_rev stm32_425_revs
[] = {
146 { 0x1000, "A" }, { 0x2000, "B" }, { 0x2008, "Y" },
148 static const struct stm32lx_rev stm32_427_revs
[] = {
149 { 0x1000, "A" }, { 0x1018, "Y" }, { 0x1038, "X" }, { 0x10f8, "V" },
151 static const struct stm32lx_rev stm32_429_revs
[] = {
152 { 0x1000, "A" }, { 0x1018, "Z" },
154 static const struct stm32lx_rev stm32_436_revs
[] = {
155 { 0x1000, "A" }, { 0x1008, "Z" }, { 0x1018, "Y" },
157 static const struct stm32lx_rev stm32_437_revs
[] = {
160 static const struct stm32lx_rev stm32_447_revs
[] = {
161 { 0x1000, "A" }, { 0x2000, "B" }, { 0x2008, "Z" },
163 static const struct stm32lx_rev stm32_457_revs
[] = {
164 { 0x1000, "A" }, { 0x1008, "Z" },
167 static const struct stm32lx_part_info stm32lx_parts
[] = {
170 .revs
= stm32_416_revs
,
171 .num_revs
= ARRAY_SIZE(stm32_416_revs
),
172 .device_str
= "STM32L1xx (Cat.1 - Low/Medium Density)",
174 .pages_per_sector
= 16,
175 .max_flash_size_kb
= 128,
176 .has_dual_banks
= false,
177 .flash_base
= 0x40023C00,
178 .fsize_base
= 0x1FF8004C,
182 .revs
= stm32_417_revs
,
183 .num_revs
= ARRAY_SIZE(stm32_417_revs
),
184 .device_str
= "STM32L0xx (Cat. 3)",
186 .pages_per_sector
= 32,
187 .max_flash_size_kb
= 64,
188 .has_dual_banks
= false,
189 .flash_base
= 0x40022000,
190 .fsize_base
= 0x1FF8007C,
194 .revs
= stm32_425_revs
,
195 .num_revs
= ARRAY_SIZE(stm32_425_revs
),
196 .device_str
= "STM32L0xx (Cat. 2)",
198 .pages_per_sector
= 32,
199 .max_flash_size_kb
= 32,
200 .has_dual_banks
= false,
201 .flash_base
= 0x40022000,
202 .fsize_base
= 0x1FF8007C,
206 .revs
= stm32_427_revs
,
207 .num_revs
= ARRAY_SIZE(stm32_427_revs
),
208 .device_str
= "STM32L1xx (Cat.3 - Medium+ Density)",
210 .pages_per_sector
= 16,
211 .max_flash_size_kb
= 256,
212 .has_dual_banks
= false,
213 .flash_base
= 0x40023C00,
214 .fsize_base
= 0x1FF800CC,
218 .revs
= stm32_429_revs
,
219 .num_revs
= ARRAY_SIZE(stm32_429_revs
),
220 .device_str
= "STM32L1xx (Cat.2)",
222 .pages_per_sector
= 16,
223 .max_flash_size_kb
= 128,
224 .has_dual_banks
= false,
225 .flash_base
= 0x40023C00,
226 .fsize_base
= 0x1FF8004C,
230 .revs
= stm32_436_revs
,
231 .num_revs
= ARRAY_SIZE(stm32_436_revs
),
232 .device_str
= "STM32L1xx (Cat.4/Cat.3 - Medium+/High Density)",
234 .pages_per_sector
= 16,
235 .max_flash_size_kb
= 384,
236 .first_bank_size_kb
= 192,
237 .has_dual_banks
= true,
238 .flash_base
= 0x40023C00,
239 .fsize_base
= 0x1FF800CC,
243 .revs
= stm32_437_revs
,
244 .num_revs
= ARRAY_SIZE(stm32_437_revs
),
245 .device_str
= "STM32L1xx (Cat.5/Cat.6)",
247 .pages_per_sector
= 16,
248 .max_flash_size_kb
= 512,
249 .first_bank_size_kb
= 0, /* determined in runtime */
250 .has_dual_banks
= true,
251 .flash_base
= 0x40023C00,
252 .fsize_base
= 0x1FF800CC,
256 .revs
= stm32_447_revs
,
257 .num_revs
= ARRAY_SIZE(stm32_447_revs
),
258 .device_str
= "STM32L0xx (Cat.5)",
260 .pages_per_sector
= 32,
261 .max_flash_size_kb
= 192,
262 .first_bank_size_kb
= 0, /* determined in runtime */
263 .has_dual_banks
= false, /* determined in runtime */
264 .flash_base
= 0x40022000,
265 .fsize_base
= 0x1FF8007C,
269 .revs
= stm32_457_revs
,
270 .num_revs
= ARRAY_SIZE(stm32_457_revs
),
271 .device_str
= "STM32L0xx (Cat.1)",
273 .pages_per_sector
= 32,
274 .max_flash_size_kb
= 16,
275 .has_dual_banks
= false,
276 .flash_base
= 0x40022000,
277 .fsize_base
= 0x1FF8007C,
281 /* flash bank stm32lx <base> <size> 0 0 <target#>
283 FLASH_BANK_COMMAND_HANDLER(stm32lx_flash_bank_command
)
285 struct stm32lx_flash_bank
*stm32lx_info
;
287 return ERROR_COMMAND_SYNTAX_ERROR
;
289 /* Create the bank structure */
290 stm32lx_info
= calloc(1, sizeof(*stm32lx_info
));
292 /* Check allocation */
294 LOG_ERROR("failed to allocate bank structure");
298 bank
->driver_priv
= stm32lx_info
;
300 stm32lx_info
->probed
= false;
301 stm32lx_info
->user_bank_size
= bank
->size
;
303 /* the stm32l erased value is 0x00 */
304 bank
->default_padded_value
= bank
->erased_value
= 0x00;
309 COMMAND_HANDLER(stm32lx_handle_mass_erase_command
)
312 return ERROR_COMMAND_SYNTAX_ERROR
;
314 struct flash_bank
*bank
;
315 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
316 if (retval
!= ERROR_OK
)
319 retval
= stm32lx_mass_erase(bank
);
320 if (retval
== ERROR_OK
)
321 command_print(CMD
, "stm32lx mass erase complete");
323 command_print(CMD
, "stm32lx mass erase failed");
328 COMMAND_HANDLER(stm32lx_handle_lock_command
)
331 return ERROR_COMMAND_SYNTAX_ERROR
;
333 struct flash_bank
*bank
;
334 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
335 if (retval
!= ERROR_OK
)
338 retval
= stm32lx_lock(bank
);
340 if (retval
== ERROR_OK
)
341 command_print(CMD
, "STM32Lx locked, takes effect after power cycle.");
343 command_print(CMD
, "STM32Lx lock failed");
348 COMMAND_HANDLER(stm32lx_handle_unlock_command
)
351 return ERROR_COMMAND_SYNTAX_ERROR
;
353 struct flash_bank
*bank
;
354 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
355 if (retval
!= ERROR_OK
)
358 retval
= stm32lx_unlock(bank
);
360 if (retval
== ERROR_OK
)
361 command_print(CMD
, "STM32Lx unlocked, takes effect after power cycle.");
363 command_print(CMD
, "STM32Lx unlock failed");
368 static int stm32lx_protect_check(struct flash_bank
*bank
)
371 struct target
*target
= bank
->target
;
372 struct stm32lx_flash_bank
*stm32lx_info
= bank
->driver_priv
;
377 * Read the WRPR word, and check each bit (corresponding to each
380 retval
= target_read_u32(target
, stm32lx_info
->flash_base
+ FLASH_WRPR
,
382 if (retval
!= ERROR_OK
)
385 for (unsigned int i
= 0; i
< bank
->num_sectors
; i
++) {
387 bank
->sectors
[i
].is_protected
= 1;
389 bank
->sectors
[i
].is_protected
= 0;
394 static int stm32lx_erase(struct flash_bank
*bank
, unsigned int first
,
400 * It could be possible to do a mass erase if all sectors must be
401 * erased, but it is not implemented yet.
404 if (bank
->target
->state
!= TARGET_HALTED
) {
405 LOG_ERROR("Target not halted");
406 return ERROR_TARGET_NOT_HALTED
;
410 * Loop over the selected sectors and erase them
412 for (unsigned int i
= first
; i
<= last
; i
++) {
413 retval
= stm32lx_erase_sector(bank
, i
);
414 if (retval
!= ERROR_OK
)
416 bank
->sectors
[i
].is_erased
= 1;
421 static int stm32lx_write_half_pages(struct flash_bank
*bank
, const uint8_t *buffer
,
422 uint32_t offset
, uint32_t count
)
424 struct target
*target
= bank
->target
;
425 struct stm32lx_flash_bank
*stm32lx_info
= bank
->driver_priv
;
427 uint32_t hp_nb
= stm32lx_info
->part_info
.page_size
/ 2;
428 uint32_t buffer_size
= (16384 / hp_nb
) * hp_nb
; /* must be multiple of hp_nb */
429 struct working_area
*write_algorithm
;
430 struct working_area
*source
;
431 uint32_t address
= bank
->base
+ offset
;
433 struct reg_param reg_params
[5];
434 struct armv7m_algorithm armv7m_info
;
436 int retval
= ERROR_OK
;
438 static const uint8_t stm32lx_flash_write_code
[] = {
439 #include "../../../contrib/loaders/flash/stm32/stm32lx.inc"
442 /* Make sure we're performing a half-page aligned write. */
443 if (offset
% hp_nb
) {
444 LOG_ERROR("The offset must be %" PRIu32
"B-aligned but it is %" PRIi32
"B)", hp_nb
, offset
);
448 LOG_ERROR("The byte count must be %" PRIu32
"B-aligned but count is %" PRIu32
"B)", hp_nb
, count
);
452 /* flash write code */
453 if (target_alloc_working_area(target
, sizeof(stm32lx_flash_write_code
),
454 &write_algorithm
) != ERROR_OK
) {
455 LOG_DEBUG("no working area for block memory writes");
456 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
459 /* Write the flashing code */
460 retval
= target_write_buffer(target
,
461 write_algorithm
->address
,
462 sizeof(stm32lx_flash_write_code
),
463 stm32lx_flash_write_code
);
464 if (retval
!= ERROR_OK
) {
465 target_free_working_area(target
, write_algorithm
);
469 /* Allocate half pages memory */
470 while (target_alloc_working_area_try(target
, buffer_size
, &source
) != ERROR_OK
) {
471 if (buffer_size
> 1024)
476 if (buffer_size
<= stm32lx_info
->part_info
.page_size
) {
477 /* we already allocated the writing code, but failed to get a
478 * buffer, free the algorithm */
479 target_free_working_area(target
, write_algorithm
);
481 LOG_WARNING("no large enough working area available, can't do block memory writes");
482 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
484 /* Make sure we're still asking for an integral number of half-pages */
485 buffer_size
-= buffer_size
% hp_nb
;
489 armv7m_info
.common_magic
= ARMV7M_COMMON_MAGIC
;
490 armv7m_info
.core_mode
= ARM_MODE_THREAD
;
491 init_reg_param(®_params
[0], "r0", 32, PARAM_OUT
);
492 init_reg_param(®_params
[1], "r1", 32, PARAM_OUT
);
493 init_reg_param(®_params
[2], "r2", 32, PARAM_OUT
);
494 init_reg_param(®_params
[3], "r3", 32, PARAM_OUT
);
495 init_reg_param(®_params
[4], "r4", 32, PARAM_OUT
);
497 /* Enable half-page write */
498 retval
= stm32lx_enable_write_half_page(bank
);
499 if (retval
!= ERROR_OK
) {
500 target_free_working_area(target
, source
);
501 target_free_working_area(target
, write_algorithm
);
503 destroy_reg_param(®_params
[0]);
504 destroy_reg_param(®_params
[1]);
505 destroy_reg_param(®_params
[2]);
506 destroy_reg_param(®_params
[3]);
507 destroy_reg_param(®_params
[4]);
511 struct armv7m_common
*armv7m
= target_to_armv7m(target
);
514 /* something is very wrong if armv7m is NULL */
515 LOG_ERROR("unable to get armv7m target");
519 /* save any DEMCR flags and configure target to catch any Hard Faults */
520 uint32_t demcr_save
= armv7m
->demcr
;
521 armv7m
->demcr
= VC_HARDERR
;
523 /* Loop while there are bytes to write */
526 this_count
= (count
> buffer_size
) ? buffer_size
: count
;
528 /* Write the next half pages */
529 retval
= target_write_buffer(target
, source
->address
, this_count
, buffer
);
530 if (retval
!= ERROR_OK
)
533 /* 4: Store useful information in the registers */
534 /* the destination address of the copy (R0) */
535 buf_set_u32(reg_params
[0].value
, 0, 32, address
);
536 /* The source address of the copy (R1) */
537 buf_set_u32(reg_params
[1].value
, 0, 32, source
->address
);
538 /* The number of half pages to copy (R2) */
539 buf_set_u32(reg_params
[2].value
, 0, 32, this_count
/ hp_nb
);
540 /* The size in byes of a half page (R3) */
541 buf_set_u32(reg_params
[3].value
, 0, 32, hp_nb
);
542 /* The flash base address (R4) */
543 buf_set_u32(reg_params
[4].value
, 0, 32, stm32lx_info
->flash_base
);
545 /* 5: Execute the bunch of code */
546 retval
= target_run_algorithm(target
, 0, NULL
,
547 ARRAY_SIZE(reg_params
), reg_params
,
548 write_algorithm
->address
, 0, 10000, &armv7m_info
);
549 if (retval
!= ERROR_OK
)
552 /* check for Hard Fault */
553 if (armv7m
->exception_number
== 3)
556 /* 6: Wait while busy */
557 retval
= stm32lx_wait_until_bsy_clear(bank
);
558 if (retval
!= ERROR_OK
)
561 buffer
+= this_count
;
562 address
+= this_count
;
566 /* restore previous flags */
567 armv7m
->demcr
= demcr_save
;
569 if (armv7m
->exception_number
== 3) {
571 /* the stm32l15x devices seem to have an issue when blank.
572 * if a ram loader is executed on a blank device it will
573 * Hard Fault, this issue does not happen for a already programmed device.
574 * A related issue is described in the stm32l151xx errata (Doc ID 17721 Rev 6 - 2.1.3).
575 * The workaround of handling the Hard Fault exception does work, but makes the
576 * loader more complicated, as a compromise we manually write the pages, programming time
577 * is reduced by 50% using this slower method.
580 LOG_WARNING("Couldn't use loader, falling back to page memory writes");
584 this_count
= (count
> hp_nb
) ? hp_nb
: count
;
586 /* Write the next half pages */
587 retval
= target_write_buffer(target
, address
, this_count
, buffer
);
588 if (retval
!= ERROR_OK
)
591 /* Wait while busy */
592 retval
= stm32lx_wait_until_bsy_clear(bank
);
593 if (retval
!= ERROR_OK
)
596 buffer
+= this_count
;
597 address
+= this_count
;
602 if (retval
== ERROR_OK
)
603 retval
= stm32lx_lock_program_memory(bank
);
605 target_free_working_area(target
, source
);
606 target_free_working_area(target
, write_algorithm
);
608 destroy_reg_param(®_params
[0]);
609 destroy_reg_param(®_params
[1]);
610 destroy_reg_param(®_params
[2]);
611 destroy_reg_param(®_params
[3]);
612 destroy_reg_param(®_params
[4]);
617 static int stm32lx_write(struct flash_bank
*bank
, const uint8_t *buffer
,
618 uint32_t offset
, uint32_t count
)
620 struct target
*target
= bank
->target
;
621 struct stm32lx_flash_bank
*stm32lx_info
= bank
->driver_priv
;
623 uint32_t hp_nb
= stm32lx_info
->part_info
.page_size
/ 2;
624 uint32_t halfpages_number
;
625 uint32_t bytes_remaining
= 0;
626 uint32_t address
= bank
->base
+ offset
;
627 uint32_t bytes_written
= 0;
630 if (bank
->target
->state
!= TARGET_HALTED
) {
631 LOG_ERROR("Target not halted");
632 return ERROR_TARGET_NOT_HALTED
;
636 LOG_ERROR("offset 0x%" PRIx32
" breaks required 4-byte alignment", offset
);
637 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
640 retval
= stm32lx_unlock_program_memory(bank
);
641 if (retval
!= ERROR_OK
)
644 /* first we need to write any unaligned head bytes up to
645 * the next 128 byte page */
648 bytes_remaining
= MIN(count
, hp_nb
- (offset
% hp_nb
));
650 while (bytes_remaining
> 0) {
651 uint8_t value
[4] = {0xff, 0xff, 0xff, 0xff};
653 /* copy remaining bytes into the write buffer */
654 uint32_t bytes_to_write
= MIN(4, bytes_remaining
);
655 memcpy(value
, buffer
+ bytes_written
, bytes_to_write
);
657 retval
= target_write_buffer(target
, address
, 4, value
);
658 if (retval
!= ERROR_OK
)
659 goto reset_pg_and_lock
;
661 bytes_written
+= bytes_to_write
;
662 bytes_remaining
-= bytes_to_write
;
665 retval
= stm32lx_wait_until_bsy_clear(bank
);
666 if (retval
!= ERROR_OK
)
667 goto reset_pg_and_lock
;
670 offset
+= bytes_written
;
671 count
-= bytes_written
;
673 /* this should always pass this check here */
674 assert((offset
% hp_nb
) == 0);
676 /* calculate half pages */
677 halfpages_number
= count
/ hp_nb
;
679 if (halfpages_number
) {
680 retval
= stm32lx_write_half_pages(bank
, buffer
+ bytes_written
, offset
, hp_nb
* halfpages_number
);
681 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
) {
682 /* attempt slow memory writes */
683 LOG_WARNING("couldn't use block writes, falling back to single memory accesses");
684 halfpages_number
= 0;
686 if (retval
!= ERROR_OK
)
691 /* write any remaining bytes */
692 uint32_t page_bytes_written
= hp_nb
* halfpages_number
;
693 bytes_written
+= page_bytes_written
;
694 address
+= page_bytes_written
;
695 bytes_remaining
= count
- page_bytes_written
;
697 retval
= stm32lx_unlock_program_memory(bank
);
698 if (retval
!= ERROR_OK
)
701 while (bytes_remaining
> 0) {
702 uint8_t value
[4] = {0xff, 0xff, 0xff, 0xff};
704 /* copy remaining bytes into the write buffer */
705 uint32_t bytes_to_write
= MIN(4, bytes_remaining
);
706 memcpy(value
, buffer
+ bytes_written
, bytes_to_write
);
708 retval
= target_write_buffer(target
, address
, 4, value
);
709 if (retval
!= ERROR_OK
)
710 goto reset_pg_and_lock
;
712 bytes_written
+= bytes_to_write
;
713 bytes_remaining
-= bytes_to_write
;
716 retval
= stm32lx_wait_until_bsy_clear(bank
);
717 if (retval
!= ERROR_OK
)
718 goto reset_pg_and_lock
;
722 retval2
= stm32lx_lock_program_memory(bank
);
723 if (retval
== ERROR_OK
)
729 static int stm32lx_read_id_code(struct target
*target
, uint32_t *id
)
731 struct armv7m_common
*armv7m
= target_to_armv7m(target
);
733 if (armv7m
->arm
.arch
== ARM_ARCH_V6M
)
734 retval
= target_read_u32(target
, DBGMCU_IDCODE_L0
, id
);
736 /* read stm32 device id register */
737 retval
= target_read_u32(target
, DBGMCU_IDCODE
, id
);
741 static int stm32lx_probe(struct flash_bank
*bank
)
743 struct target
*target
= bank
->target
;
744 struct stm32lx_flash_bank
*stm32lx_info
= bank
->driver_priv
;
745 uint16_t flash_size_in_kb
;
747 uint32_t base_address
= FLASH_BANK0_ADDRESS
;
748 uint32_t second_bank_base
;
751 stm32lx_info
->probed
= false;
753 int retval
= stm32lx_read_id_code(bank
->target
, &device_id
);
754 if (retval
!= ERROR_OK
)
757 stm32lx_info
->idcode
= device_id
;
759 LOG_DEBUG("device id = 0x%08" PRIx32
"", device_id
);
761 for (n
= 0; n
< ARRAY_SIZE(stm32lx_parts
); n
++) {
762 if ((device_id
& 0xfff) == stm32lx_parts
[n
].id
) {
763 stm32lx_info
->part_info
= stm32lx_parts
[n
];
768 if (n
== ARRAY_SIZE(stm32lx_parts
)) {
769 LOG_ERROR("Cannot identify target as an STM32 L0 or L1 family device.");
772 LOG_INFO("Device: %s", stm32lx_info
->part_info
.device_str
);
775 stm32lx_info
->flash_base
= stm32lx_info
->part_info
.flash_base
;
777 /* Get the flash size from target. */
778 retval
= target_read_u16(target
, stm32lx_info
->part_info
.fsize_base
,
781 /* 0x436 devices report their flash size as a 0 or 1 code indicating 384K
782 * or 256K, respectively. Please see RM0038 r8 or newer and refer to
784 if (retval
== ERROR_OK
&& (device_id
& 0xfff) == 0x436) {
785 if (flash_size_in_kb
== 0)
786 flash_size_in_kb
= 384;
787 else if (flash_size_in_kb
== 1)
788 flash_size_in_kb
= 256;
791 /* 0x429 devices only use the lowest 8 bits of the flash size register */
792 if (retval
== ERROR_OK
&& (device_id
& 0xfff) == 0x429) {
793 flash_size_in_kb
&= 0xff;
796 /* Failed reading flash size or flash size invalid (early silicon),
797 * default to max target family */
798 if (retval
!= ERROR_OK
|| flash_size_in_kb
== 0xffff || flash_size_in_kb
== 0) {
799 LOG_WARNING("STM32L flash size failed, probe inaccurate - assuming %dk flash",
800 stm32lx_info
->part_info
.max_flash_size_kb
);
801 flash_size_in_kb
= stm32lx_info
->part_info
.max_flash_size_kb
;
802 } else if (flash_size_in_kb
> stm32lx_info
->part_info
.max_flash_size_kb
) {
803 LOG_WARNING("STM32L probed flash size assumed incorrect since FLASH_SIZE=%dk > %dk, - assuming %dk flash",
804 flash_size_in_kb
, stm32lx_info
->part_info
.max_flash_size_kb
,
805 stm32lx_info
->part_info
.max_flash_size_kb
);
806 flash_size_in_kb
= stm32lx_info
->part_info
.max_flash_size_kb
;
809 /* Overwrite default dual-bank configuration */
810 retval
= stm32lx_update_part_info(bank
, flash_size_in_kb
);
811 if (retval
!= ERROR_OK
)
814 if (stm32lx_info
->part_info
.has_dual_banks
) {
815 /* Use the configured base address to determine if this is the first or second flash bank.
816 * Verify that the base address is reasonably correct and determine the flash bank size
818 second_bank_base
= base_address
+
819 stm32lx_info
->part_info
.first_bank_size_kb
* 1024;
820 if (bank
->base
== second_bank_base
|| !bank
->base
) {
821 /* This is the second bank */
822 base_address
= second_bank_base
;
823 flash_size_in_kb
= flash_size_in_kb
-
824 stm32lx_info
->part_info
.first_bank_size_kb
;
825 } else if (bank
->base
== base_address
) {
826 /* This is the first bank */
827 flash_size_in_kb
= stm32lx_info
->part_info
.first_bank_size_kb
;
829 LOG_WARNING("STM32L flash bank base address config is incorrect. "
830 TARGET_ADDR_FMT
" but should rather be 0x%" PRIx32
832 bank
->base
, base_address
, second_bank_base
);
835 LOG_INFO("STM32L flash has dual banks. Bank (%u) size is %dkb, base address is 0x%" PRIx32
,
836 bank
->bank_number
, flash_size_in_kb
, base_address
);
838 LOG_INFO("STM32L flash size is %dkb, base address is 0x%" PRIx32
, flash_size_in_kb
, base_address
);
841 /* if the user sets the size manually then ignore the probed value
842 * this allows us to work around devices that have a invalid flash size register value */
843 if (stm32lx_info
->user_bank_size
) {
844 flash_size_in_kb
= stm32lx_info
->user_bank_size
/ 1024;
845 LOG_INFO("ignoring flash probed value, using configured bank size: %dkbytes", flash_size_in_kb
);
848 /* calculate numbers of sectors (4kB per sector) */
849 unsigned int num_sectors
= (flash_size_in_kb
* 1024) / FLASH_SECTOR_SIZE
;
853 bank
->size
= flash_size_in_kb
* 1024;
854 bank
->base
= base_address
;
855 bank
->num_sectors
= num_sectors
;
856 bank
->sectors
= malloc(sizeof(struct flash_sector
) * num_sectors
);
857 if (!bank
->sectors
) {
858 LOG_ERROR("failed to allocate bank sectors");
862 for (unsigned int i
= 0; i
< num_sectors
; i
++) {
863 bank
->sectors
[i
].offset
= i
* FLASH_SECTOR_SIZE
;
864 bank
->sectors
[i
].size
= FLASH_SECTOR_SIZE
;
865 bank
->sectors
[i
].is_erased
= -1;
866 bank
->sectors
[i
].is_protected
= -1;
869 stm32lx_info
->probed
= true;
874 static int stm32lx_auto_probe(struct flash_bank
*bank
)
876 struct stm32lx_flash_bank
*stm32lx_info
= bank
->driver_priv
;
878 if (stm32lx_info
->probed
)
881 return stm32lx_probe(bank
);
884 /* This method must return a string displaying information about the bank */
885 static int stm32lx_get_info(struct flash_bank
*bank
, struct command_invocation
*cmd
)
887 struct stm32lx_flash_bank
*stm32lx_info
= bank
->driver_priv
;
888 const struct stm32lx_part_info
*info
= &stm32lx_info
->part_info
;
889 uint16_t rev_id
= stm32lx_info
->idcode
>> 16;
890 const char *rev_str
= NULL
;
892 if (!stm32lx_info
->probed
) {
893 int retval
= stm32lx_probe(bank
);
894 if (retval
!= ERROR_OK
) {
895 command_print_sameline(cmd
, "Unable to find bank information.");
900 for (unsigned int i
= 0; i
< info
->num_revs
; i
++)
901 if (rev_id
== info
->revs
[i
].rev
)
902 rev_str
= info
->revs
[i
].str
;
905 command_print_sameline(cmd
, "%s - Rev: %s", info
->device_str
, rev_str
);
907 command_print_sameline(cmd
, "%s - Rev: unknown (0x%04x)", info
->device_str
, rev_id
);
913 static const struct command_registration stm32lx_exec_command_handlers
[] = {
915 .name
= "mass_erase",
916 .handler
= stm32lx_handle_mass_erase_command
,
917 .mode
= COMMAND_EXEC
,
919 .help
= "Erase entire flash device. including available EEPROM",
923 .handler
= stm32lx_handle_lock_command
,
924 .mode
= COMMAND_EXEC
,
926 .help
= "Increase the readout protection to Level 1.",
930 .handler
= stm32lx_handle_unlock_command
,
931 .mode
= COMMAND_EXEC
,
933 .help
= "Lower the readout protection from Level 1 to 0.",
935 COMMAND_REGISTRATION_DONE
938 static const struct command_registration stm32lx_command_handlers
[] = {
942 .help
= "stm32lx flash command group",
944 .chain
= stm32lx_exec_command_handlers
,
946 COMMAND_REGISTRATION_DONE
949 const struct flash_driver stm32lx_flash
= {
951 .commands
= stm32lx_command_handlers
,
952 .flash_bank_command
= stm32lx_flash_bank_command
,
953 .erase
= stm32lx_erase
,
954 .write
= stm32lx_write
,
955 .read
= default_flash_read
,
956 .probe
= stm32lx_probe
,
957 .auto_probe
= stm32lx_auto_probe
,
958 .erase_check
= default_flash_blank_check
,
959 .protect_check
= stm32lx_protect_check
,
960 .info
= stm32lx_get_info
,
961 .free_driver_priv
= default_flash_free_driver_priv
,
964 /* Static methods implementation */
965 static int stm32lx_unlock_program_memory(struct flash_bank
*bank
)
967 struct target
*target
= bank
->target
;
968 struct stm32lx_flash_bank
*stm32lx_info
= bank
->driver_priv
;
973 * Unlocking the program memory is done by unlocking the PECR,
974 * then by writing the 2 PRGKEY to the PRGKEYR register
977 /* check flash is not already unlocked */
978 retval
= target_read_u32(target
, stm32lx_info
->flash_base
+ FLASH_PECR
,
980 if (retval
!= ERROR_OK
)
983 if ((reg32
& FLASH_PECR__PRGLOCK
) == 0)
986 /* To unlock the PECR write the 2 PEKEY to the PEKEYR register */
987 retval
= target_write_u32(target
, stm32lx_info
->flash_base
+ FLASH_PEKEYR
,
989 if (retval
!= ERROR_OK
)
992 retval
= target_write_u32(target
, stm32lx_info
->flash_base
+ FLASH_PEKEYR
,
994 if (retval
!= ERROR_OK
)
997 /* Make sure it worked */
998 retval
= target_read_u32(target
, stm32lx_info
->flash_base
+ FLASH_PECR
,
1000 if (retval
!= ERROR_OK
)
1003 if (reg32
& FLASH_PECR__PELOCK
) {
1004 LOG_ERROR("PELOCK is not cleared :(");
1005 return ERROR_FLASH_OPERATION_FAILED
;
1008 retval
= target_write_u32(target
, stm32lx_info
->flash_base
+ FLASH_PRGKEYR
,
1010 if (retval
!= ERROR_OK
)
1012 retval
= target_write_u32(target
, stm32lx_info
->flash_base
+ FLASH_PRGKEYR
,
1014 if (retval
!= ERROR_OK
)
1017 /* Make sure it worked */
1018 retval
= target_read_u32(target
, stm32lx_info
->flash_base
+ FLASH_PECR
,
1020 if (retval
!= ERROR_OK
)
1023 if (reg32
& FLASH_PECR__PRGLOCK
) {
1024 LOG_ERROR("PRGLOCK is not cleared :(");
1025 return ERROR_FLASH_OPERATION_FAILED
;
1031 static int stm32lx_enable_write_half_page(struct flash_bank
*bank
)
1033 struct target
*target
= bank
->target
;
1034 struct stm32lx_flash_bank
*stm32lx_info
= bank
->driver_priv
;
1039 * Unlock the program memory, then set the FPRG bit in the PECR register.
1041 retval
= stm32lx_unlock_program_memory(bank
);
1042 if (retval
!= ERROR_OK
)
1045 retval
= target_read_u32(target
, stm32lx_info
->flash_base
+ FLASH_PECR
,
1047 if (retval
!= ERROR_OK
)
1050 reg32
|= FLASH_PECR__FPRG
;
1051 retval
= target_write_u32(target
, stm32lx_info
->flash_base
+ FLASH_PECR
,
1053 if (retval
!= ERROR_OK
)
1056 retval
= target_read_u32(target
, stm32lx_info
->flash_base
+ FLASH_PECR
,
1058 if (retval
!= ERROR_OK
)
1061 reg32
|= FLASH_PECR__PROG
;
1062 retval
= target_write_u32(target
, stm32lx_info
->flash_base
+ FLASH_PECR
,
1068 static int stm32lx_lock_program_memory(struct flash_bank
*bank
)
1070 struct target
*target
= bank
->target
;
1071 struct stm32lx_flash_bank
*stm32lx_info
= bank
->driver_priv
;
1075 /* To lock the program memory, simply set the lock bit and lock PECR */
1077 retval
= target_read_u32(target
, stm32lx_info
->flash_base
+ FLASH_PECR
,
1079 if (retval
!= ERROR_OK
)
1082 reg32
|= FLASH_PECR__PRGLOCK
;
1083 retval
= target_write_u32(target
, stm32lx_info
->flash_base
+ FLASH_PECR
,
1085 if (retval
!= ERROR_OK
)
1088 retval
= target_read_u32(target
, stm32lx_info
->flash_base
+ FLASH_PECR
,
1090 if (retval
!= ERROR_OK
)
1093 reg32
|= FLASH_PECR__PELOCK
;
1094 retval
= target_write_u32(target
, stm32lx_info
->flash_base
+ FLASH_PECR
,
1096 if (retval
!= ERROR_OK
)
1102 static int stm32lx_erase_sector(struct flash_bank
*bank
, int sector
)
1104 struct target
*target
= bank
->target
;
1105 struct stm32lx_flash_bank
*stm32lx_info
= bank
->driver_priv
;
1110 * To erase a sector (i.e. stm32lx_info->part_info.pages_per_sector pages),
1111 * first unlock the memory, loop over the pages of this sector
1112 * and write 0x0 to its first word.
1115 retval
= stm32lx_unlock_program_memory(bank
);
1116 if (retval
!= ERROR_OK
)
1119 for (int page
= 0; page
< (int)stm32lx_info
->part_info
.pages_per_sector
;
1121 reg32
= FLASH_PECR__PROG
| FLASH_PECR__ERASE
;
1122 retval
= target_write_u32(target
,
1123 stm32lx_info
->flash_base
+ FLASH_PECR
, reg32
);
1124 if (retval
!= ERROR_OK
)
1127 retval
= stm32lx_wait_until_bsy_clear(bank
);
1128 if (retval
!= ERROR_OK
)
1131 uint32_t addr
= bank
->base
+ bank
->sectors
[sector
].offset
+ (page
1132 * stm32lx_info
->part_info
.page_size
);
1133 retval
= target_write_u32(target
, addr
, 0x0);
1134 if (retval
!= ERROR_OK
)
1137 retval
= stm32lx_wait_until_bsy_clear(bank
);
1138 if (retval
!= ERROR_OK
)
1142 retval
= stm32lx_lock_program_memory(bank
);
1143 if (retval
!= ERROR_OK
)
1149 static inline int stm32lx_get_flash_status(struct flash_bank
*bank
, uint32_t *status
)
1151 struct target
*target
= bank
->target
;
1152 struct stm32lx_flash_bank
*stm32lx_info
= bank
->driver_priv
;
1154 return target_read_u32(target
, stm32lx_info
->flash_base
+ FLASH_SR
, status
);
1157 static int stm32lx_wait_until_bsy_clear(struct flash_bank
*bank
)
1159 return stm32lx_wait_until_bsy_clear_timeout(bank
, 100);
1162 static int stm32lx_unlock_options_bytes(struct flash_bank
*bank
)
1164 struct target
*target
= bank
->target
;
1165 struct stm32lx_flash_bank
*stm32lx_info
= bank
->driver_priv
;
1170 * Unlocking the options bytes is done by unlocking the PECR,
1171 * then by writing the 2 FLASH_PEKEYR to the FLASH_OPTKEYR register
1174 /* check flash is not already unlocked */
1175 retval
= target_read_u32(target
, stm32lx_info
->flash_base
+ FLASH_PECR
, ®32
);
1176 if (retval
!= ERROR_OK
)
1179 if ((reg32
& FLASH_PECR__OPTLOCK
) == 0)
1182 if ((reg32
& FLASH_PECR__PELOCK
) != 0) {
1184 retval
= target_write_u32(target
, stm32lx_info
->flash_base
+ FLASH_PEKEYR
, PEKEY1
);
1185 if (retval
!= ERROR_OK
)
1188 retval
= target_write_u32(target
, stm32lx_info
->flash_base
+ FLASH_PEKEYR
, PEKEY2
);
1189 if (retval
!= ERROR_OK
)
1193 /* To unlock the PECR write the 2 OPTKEY to the FLASH_OPTKEYR register */
1194 retval
= target_write_u32(target
, stm32lx_info
->flash_base
+ FLASH_OPTKEYR
, OPTKEY1
);
1195 if (retval
!= ERROR_OK
)
1198 retval
= target_write_u32(target
, stm32lx_info
->flash_base
+ FLASH_OPTKEYR
, OPTKEY2
);
1199 if (retval
!= ERROR_OK
)
1205 static int stm32lx_wait_until_bsy_clear_timeout(struct flash_bank
*bank
, int timeout
)
1207 struct target
*target
= bank
->target
;
1208 struct stm32lx_flash_bank
*stm32lx_info
= bank
->driver_priv
;
1210 int retval
= ERROR_OK
;
1212 /* wait for busy to clear */
1214 retval
= stm32lx_get_flash_status(bank
, &status
);
1215 if (retval
!= ERROR_OK
)
1218 LOG_DEBUG("status: 0x%" PRIx32
"", status
);
1219 if ((status
& FLASH_SR__BSY
) == 0)
1222 if (timeout
-- <= 0) {
1223 LOG_ERROR("timed out waiting for flash");
1229 if (status
& FLASH_SR__WRPERR
) {
1230 LOG_ERROR("access denied / write protected");
1231 retval
= ERROR_FAIL
;
1234 if (status
& FLASH_SR__PGAERR
) {
1235 LOG_ERROR("invalid program address");
1236 retval
= ERROR_FAIL
;
1239 /* Clear but report errors */
1240 if (status
& FLASH_SR__OPTVERR
) {
1241 /* If this operation fails, we ignore it and report the original retval */
1242 target_write_u32(target
, stm32lx_info
->flash_base
+ FLASH_SR
, status
& FLASH_SR__OPTVERR
);
1248 static int stm32lx_obl_launch(struct flash_bank
*bank
)
1250 struct target
*target
= bank
->target
;
1251 struct stm32lx_flash_bank
*stm32lx_info
= bank
->driver_priv
;
1254 /* This will fail as the target gets immediately rebooted */
1255 target_write_u32(target
, stm32lx_info
->flash_base
+ FLASH_PECR
,
1256 FLASH_PECR__OBL_LAUNCH
);
1260 target_halt(target
);
1261 retval
= target_poll(target
);
1262 } while (--tries
> 0 &&
1263 (retval
!= ERROR_OK
|| target
->state
!= TARGET_HALTED
));
1265 return tries
? ERROR_OK
: ERROR_FAIL
;
1268 static int stm32lx_lock(struct flash_bank
*bank
)
1271 struct target
*target
= bank
->target
;
1273 if (target
->state
!= TARGET_HALTED
) {
1274 LOG_ERROR("Target not halted");
1275 return ERROR_TARGET_NOT_HALTED
;
1278 retval
= stm32lx_unlock_options_bytes(bank
);
1279 if (retval
!= ERROR_OK
)
1282 /* set the RDP protection level to 1 */
1283 retval
= target_write_u32(target
, OPTION_BYTES_ADDRESS
, OPTION_BYTE_0_PR1
);
1284 if (retval
!= ERROR_OK
)
1290 static int stm32lx_unlock(struct flash_bank
*bank
)
1293 struct target
*target
= bank
->target
;
1295 if (target
->state
!= TARGET_HALTED
) {
1296 LOG_ERROR("Target not halted");
1297 return ERROR_TARGET_NOT_HALTED
;
1300 retval
= stm32lx_unlock_options_bytes(bank
);
1301 if (retval
!= ERROR_OK
)
1304 /* set the RDP protection level to 0 */
1305 retval
= target_write_u32(target
, OPTION_BYTES_ADDRESS
, OPTION_BYTE_0_PR0
);
1306 if (retval
!= ERROR_OK
)
1309 retval
= stm32lx_wait_until_bsy_clear_timeout(bank
, 30000);
1310 if (retval
!= ERROR_OK
)
1316 static int stm32lx_mass_erase(struct flash_bank
*bank
)
1319 struct target
*target
= bank
->target
;
1320 struct stm32lx_flash_bank
*stm32lx_info
= NULL
;
1323 if (target
->state
!= TARGET_HALTED
) {
1324 LOG_ERROR("Target not halted");
1325 return ERROR_TARGET_NOT_HALTED
;
1328 stm32lx_info
= bank
->driver_priv
;
1330 retval
= stm32lx_lock(bank
);
1331 if (retval
!= ERROR_OK
)
1334 retval
= stm32lx_obl_launch(bank
);
1335 if (retval
!= ERROR_OK
)
1338 retval
= stm32lx_unlock(bank
);
1339 if (retval
!= ERROR_OK
)
1342 retval
= stm32lx_obl_launch(bank
);
1343 if (retval
!= ERROR_OK
)
1346 retval
= target_read_u32(target
, stm32lx_info
->flash_base
+ FLASH_PECR
, ®32
);
1347 if (retval
!= ERROR_OK
)
1350 retval
= target_write_u32(target
, stm32lx_info
->flash_base
+ FLASH_PECR
, reg32
| FLASH_PECR__OPTLOCK
);
1351 if (retval
!= ERROR_OK
)
1357 static int stm32lx_update_part_info(struct flash_bank
*bank
, uint16_t flash_size_in_kb
)
1359 struct stm32lx_flash_bank
*stm32lx_info
= bank
->driver_priv
;
1361 switch (stm32lx_info
->part_info
.id
) {
1362 case 0x447: /* STM32L0xx (Cat.5) devices */
1363 if (flash_size_in_kb
== 192 || flash_size_in_kb
== 128) {
1364 stm32lx_info
->part_info
.first_bank_size_kb
= flash_size_in_kb
/ 2;
1365 stm32lx_info
->part_info
.has_dual_banks
= true;
1368 case 0x437: /* STM32L1xx (Cat.5/Cat.6) */
1369 stm32lx_info
->part_info
.first_bank_size_kb
= flash_size_in_kb
/ 2;
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)