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" },
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 */
293 if (stm32lx_info
== NULL
) {
294 LOG_ERROR("failed to allocate bank structure");
298 bank
->driver_priv
= stm32lx_info
;
300 stm32lx_info
->probed
= 0;
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
)
314 return ERROR_COMMAND_SYNTAX_ERROR
;
316 struct flash_bank
*bank
;
317 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
318 if (ERROR_OK
!= retval
)
321 retval
= stm32lx_mass_erase(bank
);
322 if (retval
== ERROR_OK
) {
323 /* set all sectors as erased */
324 for (i
= 0; i
< bank
->num_sectors
; i
++)
325 bank
->sectors
[i
].is_erased
= 1;
327 command_print(CMD_CTX
, "stm32lx mass erase complete");
329 command_print(CMD_CTX
, "stm32lx mass erase failed");
335 COMMAND_HANDLER(stm32lx_handle_lock_command
)
338 return ERROR_COMMAND_SYNTAX_ERROR
;
340 struct flash_bank
*bank
;
341 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
342 if (ERROR_OK
!= retval
)
345 retval
= stm32lx_lock(bank
);
347 if (retval
== ERROR_OK
)
348 command_print(CMD_CTX
, "STM32Lx locked, takes effect after power cycle.");
350 command_print(CMD_CTX
, "STM32Lx lock failed");
355 COMMAND_HANDLER(stm32lx_handle_unlock_command
)
358 return ERROR_COMMAND_SYNTAX_ERROR
;
360 struct flash_bank
*bank
;
361 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
362 if (ERROR_OK
!= retval
)
365 retval
= stm32lx_unlock(bank
);
367 if (retval
== ERROR_OK
)
368 command_print(CMD_CTX
, "STM32Lx unlocked, takes effect after power cycle.");
370 command_print(CMD_CTX
, "STM32Lx unlock failed");
375 static int stm32lx_protect_check(struct flash_bank
*bank
)
378 struct target
*target
= bank
->target
;
379 struct stm32lx_flash_bank
*stm32lx_info
= bank
->driver_priv
;
384 * Read the WRPR word, and check each bit (corresponding to each
387 retval
= target_read_u32(target
, stm32lx_info
->flash_base
+ FLASH_WRPR
,
389 if (retval
!= ERROR_OK
)
392 for (int i
= 0; i
< bank
->num_sectors
; i
++) {
394 bank
->sectors
[i
].is_protected
= 1;
396 bank
->sectors
[i
].is_protected
= 0;
401 static int stm32lx_erase(struct flash_bank
*bank
, int first
, int last
)
406 * It could be possible to do a mass erase if all sectors must be
407 * erased, but it is not implemented yet.
410 if (bank
->target
->state
!= TARGET_HALTED
) {
411 LOG_ERROR("Target not halted");
412 return ERROR_TARGET_NOT_HALTED
;
416 * Loop over the selected sectors and erase them
418 for (int i
= first
; i
<= last
; i
++) {
419 retval
= stm32lx_erase_sector(bank
, i
);
420 if (retval
!= ERROR_OK
)
422 bank
->sectors
[i
].is_erased
= 1;
427 static int stm32lx_protect(struct flash_bank
*bank
, int set
, int first
,
430 LOG_WARNING("protection of the STM32L flash is not implemented");
434 static int stm32lx_write_half_pages(struct flash_bank
*bank
, const uint8_t *buffer
,
435 uint32_t offset
, uint32_t count
)
437 struct target
*target
= bank
->target
;
438 struct stm32lx_flash_bank
*stm32lx_info
= bank
->driver_priv
;
440 uint32_t hp_nb
= stm32lx_info
->part_info
.page_size
/ 2;
441 uint32_t buffer_size
= 16384;
442 struct working_area
*write_algorithm
;
443 struct working_area
*source
;
444 uint32_t address
= bank
->base
+ offset
;
446 struct reg_param reg_params
[3];
447 struct armv7m_algorithm armv7m_info
;
449 int retval
= ERROR_OK
;
451 /* see contib/loaders/flash/stm32lx.S for src */
453 static const uint8_t stm32lx_flash_write_code
[] = {
454 0x92, 0x00, 0x8A, 0x18, 0x01, 0xE0, 0x08, 0xC9, 0x08, 0xC0, 0x91, 0x42, 0xFB, 0xD1, 0x00, 0xBE
457 /* Make sure we're performing a half-page aligned write. */
459 LOG_ERROR("The byte count must be %" PRIu32
"B-aligned but count is %" PRIi32
"B)", hp_nb
, count
);
463 /* flash write code */
464 if (target_alloc_working_area(target
, sizeof(stm32lx_flash_write_code
),
465 &write_algorithm
) != ERROR_OK
) {
466 LOG_DEBUG("no working area for block memory writes");
467 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
470 /* Write the flashing code */
471 retval
= target_write_buffer(target
,
472 write_algorithm
->address
,
473 sizeof(stm32lx_flash_write_code
),
474 stm32lx_flash_write_code
);
475 if (retval
!= ERROR_OK
) {
476 target_free_working_area(target
, write_algorithm
);
480 /* Allocate half pages memory */
481 while (target_alloc_working_area_try(target
, buffer_size
, &source
) != ERROR_OK
) {
482 if (buffer_size
> 1024)
487 if (buffer_size
<= stm32lx_info
->part_info
.page_size
) {
488 /* we already allocated the writing code, but failed to get a
489 * buffer, free the algorithm */
490 target_free_working_area(target
, write_algorithm
);
492 LOG_WARNING("no large enough working area available, can't do block memory writes");
493 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
497 armv7m_info
.common_magic
= ARMV7M_COMMON_MAGIC
;
498 armv7m_info
.core_mode
= ARM_MODE_THREAD
;
499 init_reg_param(®_params
[0], "r0", 32, PARAM_OUT
);
500 init_reg_param(®_params
[1], "r1", 32, PARAM_OUT
);
501 init_reg_param(®_params
[2], "r2", 32, PARAM_OUT
);
503 /* Enable half-page write */
504 retval
= stm32lx_enable_write_half_page(bank
);
505 if (retval
!= ERROR_OK
) {
506 target_free_working_area(target
, source
);
507 target_free_working_area(target
, write_algorithm
);
509 destroy_reg_param(®_params
[0]);
510 destroy_reg_param(®_params
[1]);
511 destroy_reg_param(®_params
[2]);
515 struct armv7m_common
*armv7m
= target_to_armv7m(target
);
516 if (armv7m
== NULL
) {
518 /* something is very wrong if armv7m is NULL */
519 LOG_ERROR("unable to get armv7m target");
523 /* save any DEMCR flags and configure target to catch any Hard Faults */
524 uint32_t demcr_save
= armv7m
->demcr
;
525 armv7m
->demcr
= VC_HARDERR
;
527 /* Loop while there are bytes to write */
530 this_count
= (count
> buffer_size
) ? buffer_size
: count
;
532 /* Write the next half pages */
533 retval
= target_write_buffer(target
, source
->address
, this_count
, buffer
);
534 if (retval
!= ERROR_OK
)
537 /* 4: Store useful information in the registers */
538 /* the destination address of the copy (R0) */
539 buf_set_u32(reg_params
[0].value
, 0, 32, address
);
540 /* The source address of the copy (R1) */
541 buf_set_u32(reg_params
[1].value
, 0, 32, source
->address
);
542 /* The length of the copy (R2) */
543 buf_set_u32(reg_params
[2].value
, 0, 32, this_count
/ 4);
545 /* 5: Execute the bunch of code */
546 retval
= target_run_algorithm(target
, 0, NULL
, sizeof(reg_params
)
547 / sizeof(*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]);
615 static int stm32lx_write(struct flash_bank
*bank
, const uint8_t *buffer
,
616 uint32_t offset
, uint32_t count
)
618 struct target
*target
= bank
->target
;
619 struct stm32lx_flash_bank
*stm32lx_info
= bank
->driver_priv
;
621 uint32_t hp_nb
= stm32lx_info
->part_info
.page_size
/ 2;
622 uint32_t halfpages_number
;
623 uint32_t bytes_remaining
= 0;
624 uint32_t address
= bank
->base
+ offset
;
625 uint32_t bytes_written
= 0;
628 if (bank
->target
->state
!= TARGET_HALTED
) {
629 LOG_ERROR("Target not halted");
630 return ERROR_TARGET_NOT_HALTED
;
634 LOG_ERROR("offset 0x%" PRIx32
" breaks required 4-byte alignment", offset
);
635 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
638 retval
= stm32lx_unlock_program_memory(bank
);
639 if (retval
!= ERROR_OK
)
642 /* first we need to write any unaligned head bytes upto
643 * the next 128 byte page */
646 bytes_remaining
= MIN(count
, hp_nb
- (offset
% hp_nb
));
648 while (bytes_remaining
> 0) {
649 uint8_t value
[4] = {0xff, 0xff, 0xff, 0xff};
651 /* copy remaining bytes into the write buffer */
652 uint32_t bytes_to_write
= MIN(4, bytes_remaining
);
653 memcpy(value
, buffer
+ bytes_written
, bytes_to_write
);
655 retval
= target_write_buffer(target
, address
, 4, value
);
656 if (retval
!= ERROR_OK
)
657 goto reset_pg_and_lock
;
659 bytes_written
+= bytes_to_write
;
660 bytes_remaining
-= bytes_to_write
;
663 retval
= stm32lx_wait_until_bsy_clear(bank
);
664 if (retval
!= ERROR_OK
)
665 goto reset_pg_and_lock
;
668 offset
+= bytes_written
;
669 count
-= bytes_written
;
671 /* this should always pass this check here */
672 assert((offset
% hp_nb
) == 0);
674 /* calculate half pages */
675 halfpages_number
= count
/ hp_nb
;
677 if (halfpages_number
) {
678 retval
= stm32lx_write_half_pages(bank
, buffer
+ bytes_written
, offset
, hp_nb
* halfpages_number
);
679 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
) {
680 /* attempt slow memory writes */
681 LOG_WARNING("couldn't use block writes, falling back to single memory accesses");
682 halfpages_number
= 0;
684 if (retval
!= ERROR_OK
)
689 /* write any remaining bytes */
690 uint32_t page_bytes_written
= hp_nb
* halfpages_number
;
691 bytes_written
+= page_bytes_written
;
692 address
+= page_bytes_written
;
693 bytes_remaining
= count
- page_bytes_written
;
695 retval
= stm32lx_unlock_program_memory(bank
);
696 if (retval
!= ERROR_OK
)
699 while (bytes_remaining
> 0) {
700 uint8_t value
[4] = {0xff, 0xff, 0xff, 0xff};
702 /* copy remaining bytes into the write buffer */
703 uint32_t bytes_to_write
= MIN(4, bytes_remaining
);
704 memcpy(value
, buffer
+ bytes_written
, bytes_to_write
);
706 retval
= target_write_buffer(target
, address
, 4, value
);
707 if (retval
!= ERROR_OK
)
708 goto reset_pg_and_lock
;
710 bytes_written
+= bytes_to_write
;
711 bytes_remaining
-= bytes_to_write
;
714 retval
= stm32lx_wait_until_bsy_clear(bank
);
715 if (retval
!= ERROR_OK
)
716 goto reset_pg_and_lock
;
720 retval2
= stm32lx_lock_program_memory(bank
);
721 if (retval
== ERROR_OK
)
727 static int stm32lx_read_id_code(struct target
*target
, uint32_t *id
)
729 /* read stm32 device id register */
730 int retval
= target_read_u32(target
, DBGMCU_IDCODE
, id
);
731 if (retval
!= ERROR_OK
)
734 /* STM32L0 parts will have 0 there, try reading the L0's location for
735 * DBG_IDCODE in case this is an L0 part. */
737 retval
= target_read_u32(target
, DBGMCU_IDCODE_L0
, id
);
742 static int stm32lx_probe(struct flash_bank
*bank
)
744 struct target
*target
= bank
->target
;
745 struct stm32lx_flash_bank
*stm32lx_info
= bank
->driver_priv
;
747 uint16_t flash_size_in_kb
;
749 uint32_t base_address
= FLASH_BANK0_ADDRESS
;
750 uint32_t second_bank_base
;
753 stm32lx_info
->probed
= 0;
755 int retval
= stm32lx_read_id_code(bank
->target
, &device_id
);
756 if (retval
!= ERROR_OK
)
759 stm32lx_info
->idcode
= device_id
;
761 LOG_DEBUG("device id = 0x%08" PRIx32
"", device_id
);
763 for (n
= 0; n
< ARRAY_SIZE(stm32lx_parts
); n
++) {
764 if ((device_id
& 0xfff) == stm32lx_parts
[n
].id
) {
765 stm32lx_info
->part_info
= stm32lx_parts
[n
];
770 if (n
== ARRAY_SIZE(stm32lx_parts
)) {
771 LOG_WARNING("Cannot identify target as a STM32L family.");
774 LOG_INFO("Device: %s", stm32lx_info
->part_info
.device_str
);
777 stm32lx_info
->flash_base
= stm32lx_info
->part_info
.flash_base
;
779 /* Get the flash size from target. */
780 retval
= target_read_u16(target
, stm32lx_info
->part_info
.fsize_base
,
783 /* 0x436 devices report their flash size as a 0 or 1 code indicating 384K
784 * or 256K, respectively. Please see RM0038 r8 or newer and refer to
786 if (retval
== ERROR_OK
&& (device_id
& 0xfff) == 0x436) {
787 if (flash_size_in_kb
== 0)
788 flash_size_in_kb
= 384;
789 else if (flash_size_in_kb
== 1)
790 flash_size_in_kb
= 256;
793 /* 0x429 devices only use the lowest 8 bits of the flash size register */
794 if (retval
== ERROR_OK
&& (device_id
& 0xfff) == 0x429) {
795 flash_size_in_kb
&= 0xff;
798 /* Failed reading flash size or flash size invalid (early silicon),
799 * default to max target family */
800 if (retval
!= ERROR_OK
|| flash_size_in_kb
== 0xffff || flash_size_in_kb
== 0) {
801 LOG_WARNING("STM32L flash size failed, probe inaccurate - assuming %dk flash",
802 stm32lx_info
->part_info
.max_flash_size_kb
);
803 flash_size_in_kb
= stm32lx_info
->part_info
.max_flash_size_kb
;
804 } else if (flash_size_in_kb
> stm32lx_info
->part_info
.max_flash_size_kb
) {
805 LOG_WARNING("STM32L probed flash size assumed incorrect since FLASH_SIZE=%dk > %dk, - assuming %dk flash",
806 flash_size_in_kb
, stm32lx_info
->part_info
.max_flash_size_kb
,
807 stm32lx_info
->part_info
.max_flash_size_kb
);
808 flash_size_in_kb
= stm32lx_info
->part_info
.max_flash_size_kb
;
811 /* Overwrite default dual-bank configuration */
812 retval
= stm32lx_update_part_info(bank
, flash_size_in_kb
);
813 if (retval
!= ERROR_OK
)
816 if (stm32lx_info
->part_info
.has_dual_banks
) {
817 /* Use the configured base address to determine if this is the first or second flash bank.
818 * Verify that the base address is reasonably correct and determine the flash bank size
820 second_bank_base
= base_address
+
821 stm32lx_info
->part_info
.first_bank_size_kb
* 1024;
822 if (bank
->base
== second_bank_base
|| !bank
->base
) {
823 /* This is the second bank */
824 base_address
= second_bank_base
;
825 flash_size_in_kb
= flash_size_in_kb
-
826 stm32lx_info
->part_info
.first_bank_size_kb
;
827 } else if (bank
->base
== base_address
) {
828 /* This is the first bank */
829 flash_size_in_kb
= stm32lx_info
->part_info
.first_bank_size_kb
;
831 LOG_WARNING("STM32L flash bank base address config is incorrect."
832 " 0x%" PRIx32
" but should rather be 0x%" PRIx32
" or 0x%" PRIx32
,
833 bank
->base
, base_address
, second_bank_base
);
836 LOG_INFO("STM32L flash has dual banks. Bank (%d) size is %dkb, base address is 0x%" PRIx32
,
837 bank
->bank_number
, flash_size_in_kb
, base_address
);
839 LOG_INFO("STM32L flash size is %dkb, base address is 0x%" PRIx32
, flash_size_in_kb
, base_address
);
842 /* if the user sets the size manually then ignore the probed value
843 * this allows us to work around devices that have a invalid flash size register value */
844 if (stm32lx_info
->user_bank_size
) {
845 flash_size_in_kb
= stm32lx_info
->user_bank_size
/ 1024;
846 LOG_INFO("ignoring flash probed value, using configured bank size: %dkbytes", flash_size_in_kb
);
849 /* calculate numbers of sectors (4kB per sector) */
850 int num_sectors
= (flash_size_in_kb
* 1024) / FLASH_SECTOR_SIZE
;
854 bank
->sectors
= NULL
;
857 bank
->size
= flash_size_in_kb
* 1024;
858 bank
->base
= base_address
;
859 bank
->num_sectors
= num_sectors
;
860 bank
->sectors
= malloc(sizeof(struct flash_sector
) * num_sectors
);
861 if (bank
->sectors
== NULL
) {
862 LOG_ERROR("failed to allocate bank sectors");
866 for (i
= 0; i
< num_sectors
; i
++) {
867 bank
->sectors
[i
].offset
= i
* FLASH_SECTOR_SIZE
;
868 bank
->sectors
[i
].size
= FLASH_SECTOR_SIZE
;
869 bank
->sectors
[i
].is_erased
= -1;
870 bank
->sectors
[i
].is_protected
= 1;
873 stm32lx_info
->probed
= 1;
878 static int stm32lx_auto_probe(struct flash_bank
*bank
)
880 struct stm32lx_flash_bank
*stm32lx_info
= bank
->driver_priv
;
882 if (stm32lx_info
->probed
)
885 return stm32lx_probe(bank
);
888 /* This method must return a string displaying information about the bank */
889 static int stm32lx_get_info(struct flash_bank
*bank
, char *buf
, int buf_size
)
891 struct stm32lx_flash_bank
*stm32lx_info
= bank
->driver_priv
;
892 const struct stm32lx_part_info
*info
= &stm32lx_info
->part_info
;
893 uint16_t rev_id
= stm32lx_info
->idcode
>> 16;
894 const char *rev_str
= NULL
;
896 if (!stm32lx_info
->probed
) {
897 int retval
= stm32lx_probe(bank
);
898 if (retval
!= ERROR_OK
) {
899 snprintf(buf
, buf_size
,
900 "Unable to find bank information.");
905 for (unsigned int i
= 0; i
< info
->num_revs
; i
++)
906 if (rev_id
== info
->revs
[i
].rev
)
907 rev_str
= info
->revs
[i
].str
;
909 if (rev_str
!= NULL
) {
910 snprintf(buf
, buf_size
,
912 info
->device_str
, rev_str
);
914 snprintf(buf
, buf_size
,
915 "%s - Rev: unknown (0x%04x)",
916 info
->device_str
, rev_id
);
922 static const struct command_registration stm32lx_exec_command_handlers
[] = {
924 .name
= "mass_erase",
925 .handler
= stm32lx_handle_mass_erase_command
,
926 .mode
= COMMAND_EXEC
,
928 .help
= "Erase entire flash device. including available EEPROM",
932 .handler
= stm32lx_handle_lock_command
,
933 .mode
= COMMAND_EXEC
,
935 .help
= "Increase the readout protection to Level 1.",
939 .handler
= stm32lx_handle_unlock_command
,
940 .mode
= COMMAND_EXEC
,
942 .help
= "Lower the readout protection from Level 1 to 0.",
944 COMMAND_REGISTRATION_DONE
947 static const struct command_registration stm32lx_command_handlers
[] = {
951 .help
= "stm32lx flash command group",
953 .chain
= stm32lx_exec_command_handlers
,
955 COMMAND_REGISTRATION_DONE
958 struct flash_driver stm32lx_flash
= {
960 .commands
= stm32lx_command_handlers
,
961 .flash_bank_command
= stm32lx_flash_bank_command
,
962 .erase
= stm32lx_erase
,
963 .protect
= stm32lx_protect
,
964 .write
= stm32lx_write
,
965 .read
= default_flash_read
,
966 .probe
= stm32lx_probe
,
967 .auto_probe
= stm32lx_auto_probe
,
968 .erase_check
= default_flash_blank_check
,
969 .protect_check
= stm32lx_protect_check
,
970 .info
= stm32lx_get_info
,
973 /* Static methods implementation */
974 static int stm32lx_unlock_program_memory(struct flash_bank
*bank
)
976 struct target
*target
= bank
->target
;
977 struct stm32lx_flash_bank
*stm32lx_info
= bank
->driver_priv
;
982 * Unlocking the program memory is done by unlocking the PECR,
983 * then by writing the 2 PRGKEY to the PRGKEYR register
986 /* check flash is not already unlocked */
987 retval
= target_read_u32(target
, stm32lx_info
->flash_base
+ FLASH_PECR
,
989 if (retval
!= ERROR_OK
)
992 if ((reg32
& FLASH_PECR__PRGLOCK
) == 0)
995 /* To unlock the PECR write the 2 PEKEY to the PEKEYR register */
996 retval
= target_write_u32(target
, stm32lx_info
->flash_base
+ FLASH_PEKEYR
,
998 if (retval
!= ERROR_OK
)
1001 retval
= target_write_u32(target
, stm32lx_info
->flash_base
+ FLASH_PEKEYR
,
1003 if (retval
!= ERROR_OK
)
1006 /* Make sure it worked */
1007 retval
= target_read_u32(target
, stm32lx_info
->flash_base
+ FLASH_PECR
,
1009 if (retval
!= ERROR_OK
)
1012 if (reg32
& FLASH_PECR__PELOCK
) {
1013 LOG_ERROR("PELOCK is not cleared :(");
1014 return ERROR_FLASH_OPERATION_FAILED
;
1017 retval
= target_write_u32(target
, stm32lx_info
->flash_base
+ FLASH_PRGKEYR
,
1019 if (retval
!= ERROR_OK
)
1021 retval
= target_write_u32(target
, stm32lx_info
->flash_base
+ FLASH_PRGKEYR
,
1023 if (retval
!= ERROR_OK
)
1026 /* Make sure it worked */
1027 retval
= target_read_u32(target
, stm32lx_info
->flash_base
+ FLASH_PECR
,
1029 if (retval
!= ERROR_OK
)
1032 if (reg32
& FLASH_PECR__PRGLOCK
) {
1033 LOG_ERROR("PRGLOCK is not cleared :(");
1034 return ERROR_FLASH_OPERATION_FAILED
;
1040 static int stm32lx_enable_write_half_page(struct flash_bank
*bank
)
1042 struct target
*target
= bank
->target
;
1043 struct stm32lx_flash_bank
*stm32lx_info
= bank
->driver_priv
;
1048 * Unlock the program memory, then set the FPRG bit in the PECR register.
1050 retval
= stm32lx_unlock_program_memory(bank
);
1051 if (retval
!= ERROR_OK
)
1054 retval
= target_read_u32(target
, stm32lx_info
->flash_base
+ FLASH_PECR
,
1056 if (retval
!= ERROR_OK
)
1059 reg32
|= FLASH_PECR__FPRG
;
1060 retval
= target_write_u32(target
, stm32lx_info
->flash_base
+ FLASH_PECR
,
1062 if (retval
!= ERROR_OK
)
1065 retval
= target_read_u32(target
, stm32lx_info
->flash_base
+ FLASH_PECR
,
1067 if (retval
!= ERROR_OK
)
1070 reg32
|= FLASH_PECR__PROG
;
1071 retval
= target_write_u32(target
, stm32lx_info
->flash_base
+ FLASH_PECR
,
1077 static int stm32lx_lock_program_memory(struct flash_bank
*bank
)
1079 struct target
*target
= bank
->target
;
1080 struct stm32lx_flash_bank
*stm32lx_info
= bank
->driver_priv
;
1084 /* To lock the program memory, simply set the lock bit and lock PECR */
1086 retval
= target_read_u32(target
, stm32lx_info
->flash_base
+ FLASH_PECR
,
1088 if (retval
!= ERROR_OK
)
1091 reg32
|= FLASH_PECR__PRGLOCK
;
1092 retval
= target_write_u32(target
, stm32lx_info
->flash_base
+ FLASH_PECR
,
1094 if (retval
!= ERROR_OK
)
1097 retval
= target_read_u32(target
, stm32lx_info
->flash_base
+ FLASH_PECR
,
1099 if (retval
!= ERROR_OK
)
1102 reg32
|= FLASH_PECR__PELOCK
;
1103 retval
= target_write_u32(target
, stm32lx_info
->flash_base
+ FLASH_PECR
,
1105 if (retval
!= ERROR_OK
)
1111 static int stm32lx_erase_sector(struct flash_bank
*bank
, int sector
)
1113 struct target
*target
= bank
->target
;
1114 struct stm32lx_flash_bank
*stm32lx_info
= bank
->driver_priv
;
1119 * To erase a sector (i.e. stm32lx_info->part_info.pages_per_sector pages),
1120 * first unlock the memory, loop over the pages of this sector
1121 * and write 0x0 to its first word.
1124 retval
= stm32lx_unlock_program_memory(bank
);
1125 if (retval
!= ERROR_OK
)
1128 for (int page
= 0; page
< (int)stm32lx_info
->part_info
.pages_per_sector
;
1130 reg32
= FLASH_PECR__PROG
| FLASH_PECR__ERASE
;
1131 retval
= target_write_u32(target
,
1132 stm32lx_info
->flash_base
+ FLASH_PECR
, reg32
);
1133 if (retval
!= ERROR_OK
)
1136 retval
= stm32lx_wait_until_bsy_clear(bank
);
1137 if (retval
!= ERROR_OK
)
1140 uint32_t addr
= bank
->base
+ bank
->sectors
[sector
].offset
+ (page
1141 * stm32lx_info
->part_info
.page_size
);
1142 retval
= target_write_u32(target
, addr
, 0x0);
1143 if (retval
!= ERROR_OK
)
1146 retval
= stm32lx_wait_until_bsy_clear(bank
);
1147 if (retval
!= ERROR_OK
)
1151 retval
= stm32lx_lock_program_memory(bank
);
1152 if (retval
!= ERROR_OK
)
1158 static inline int stm32lx_get_flash_status(struct flash_bank
*bank
, uint32_t *status
)
1160 struct target
*target
= bank
->target
;
1161 struct stm32lx_flash_bank
*stm32lx_info
= bank
->driver_priv
;
1163 return target_read_u32(target
, stm32lx_info
->flash_base
+ FLASH_SR
, status
);
1166 static int stm32lx_wait_until_bsy_clear(struct flash_bank
*bank
)
1168 return stm32lx_wait_until_bsy_clear_timeout(bank
, 100);
1171 static int stm32lx_unlock_options_bytes(struct flash_bank
*bank
)
1173 struct target
*target
= bank
->target
;
1174 struct stm32lx_flash_bank
*stm32lx_info
= bank
->driver_priv
;
1179 * Unlocking the options bytes is done by unlocking the PECR,
1180 * then by writing the 2 FLASH_PEKEYR to the FLASH_OPTKEYR register
1183 /* check flash is not already unlocked */
1184 retval
= target_read_u32(target
, stm32lx_info
->flash_base
+ FLASH_PECR
, ®32
);
1185 if (retval
!= ERROR_OK
)
1188 if ((reg32
& FLASH_PECR__OPTLOCK
) == 0)
1191 if ((reg32
& FLASH_PECR__PELOCK
) != 0) {
1193 retval
= target_write_u32(target
, stm32lx_info
->flash_base
+ FLASH_PEKEYR
, PEKEY1
);
1194 if (retval
!= ERROR_OK
)
1197 retval
= target_write_u32(target
, stm32lx_info
->flash_base
+ FLASH_PEKEYR
, PEKEY2
);
1198 if (retval
!= ERROR_OK
)
1202 /* To unlock the PECR write the 2 OPTKEY to the FLASH_OPTKEYR register */
1203 retval
= target_write_u32(target
, stm32lx_info
->flash_base
+ FLASH_OPTKEYR
, OPTKEY1
);
1204 if (retval
!= ERROR_OK
)
1207 retval
= target_write_u32(target
, stm32lx_info
->flash_base
+ FLASH_OPTKEYR
, OPTKEY2
);
1208 if (retval
!= ERROR_OK
)
1214 static int stm32lx_wait_until_bsy_clear_timeout(struct flash_bank
*bank
, int timeout
)
1216 struct target
*target
= bank
->target
;
1217 struct stm32lx_flash_bank
*stm32lx_info
= bank
->driver_priv
;
1219 int retval
= ERROR_OK
;
1221 /* wait for busy to clear */
1223 retval
= stm32lx_get_flash_status(bank
, &status
);
1224 if (retval
!= ERROR_OK
)
1227 LOG_DEBUG("status: 0x%" PRIx32
"", status
);
1228 if ((status
& FLASH_SR__BSY
) == 0)
1231 if (timeout
-- <= 0) {
1232 LOG_ERROR("timed out waiting for flash");
1238 if (status
& FLASH_SR__WRPERR
) {
1239 LOG_ERROR("access denied / write protected");
1240 retval
= ERROR_FAIL
;
1243 if (status
& FLASH_SR__PGAERR
) {
1244 LOG_ERROR("invalid program address");
1245 retval
= ERROR_FAIL
;
1248 /* Clear but report errors */
1249 if (status
& FLASH_SR__OPTVERR
) {
1250 /* If this operation fails, we ignore it and report the original retval */
1251 target_write_u32(target
, stm32lx_info
->flash_base
+ FLASH_SR
, status
& FLASH_SR__OPTVERR
);
1257 static int stm32lx_obl_launch(struct flash_bank
*bank
)
1259 struct target
*target
= bank
->target
;
1260 struct stm32lx_flash_bank
*stm32lx_info
= bank
->driver_priv
;
1263 /* This will fail as the target gets immediately rebooted */
1264 target_write_u32(target
, stm32lx_info
->flash_base
+ FLASH_PECR
,
1265 FLASH_PECR__OBL_LAUNCH
);
1269 target_halt(target
);
1270 retval
= target_poll(target
);
1271 } while (--tries
> 0 &&
1272 (retval
!= ERROR_OK
|| target
->state
!= TARGET_HALTED
));
1274 return tries
? ERROR_OK
: ERROR_FAIL
;
1277 static int stm32lx_lock(struct flash_bank
*bank
)
1280 struct target
*target
= bank
->target
;
1282 if (target
->state
!= TARGET_HALTED
) {
1283 LOG_ERROR("Target not halted");
1284 return ERROR_TARGET_NOT_HALTED
;
1287 retval
= stm32lx_unlock_options_bytes(bank
);
1288 if (retval
!= ERROR_OK
)
1291 /* set the RDP protection level to 1 */
1292 retval
= target_write_u32(target
, OPTION_BYTES_ADDRESS
, OPTION_BYTE_0_PR1
);
1293 if (retval
!= ERROR_OK
)
1299 static int stm32lx_unlock(struct flash_bank
*bank
)
1302 struct target
*target
= bank
->target
;
1304 if (target
->state
!= TARGET_HALTED
) {
1305 LOG_ERROR("Target not halted");
1306 return ERROR_TARGET_NOT_HALTED
;
1309 retval
= stm32lx_unlock_options_bytes(bank
);
1310 if (retval
!= ERROR_OK
)
1313 /* set the RDP protection level to 0 */
1314 retval
= target_write_u32(target
, OPTION_BYTES_ADDRESS
, OPTION_BYTE_0_PR0
);
1315 if (retval
!= ERROR_OK
)
1318 retval
= stm32lx_wait_until_bsy_clear_timeout(bank
, 30000);
1319 if (retval
!= ERROR_OK
)
1325 static int stm32lx_mass_erase(struct flash_bank
*bank
)
1328 struct target
*target
= bank
->target
;
1329 struct stm32lx_flash_bank
*stm32lx_info
= NULL
;
1332 if (target
->state
!= TARGET_HALTED
) {
1333 LOG_ERROR("Target not halted");
1334 return ERROR_TARGET_NOT_HALTED
;
1337 stm32lx_info
= bank
->driver_priv
;
1339 retval
= stm32lx_lock(bank
);
1340 if (retval
!= ERROR_OK
)
1343 retval
= stm32lx_obl_launch(bank
);
1344 if (retval
!= ERROR_OK
)
1347 retval
= stm32lx_unlock(bank
);
1348 if (retval
!= ERROR_OK
)
1351 retval
= stm32lx_obl_launch(bank
);
1352 if (retval
!= ERROR_OK
)
1355 retval
= target_read_u32(target
, stm32lx_info
->flash_base
+ FLASH_PECR
, ®32
);
1356 if (retval
!= ERROR_OK
)
1359 retval
= target_write_u32(target
, stm32lx_info
->flash_base
+ FLASH_PECR
, reg32
| FLASH_PECR__OPTLOCK
);
1360 if (retval
!= ERROR_OK
)
1366 static int stm32lx_update_part_info(struct flash_bank
*bank
, uint16_t flash_size_in_kb
)
1368 struct stm32lx_flash_bank
*stm32lx_info
= bank
->driver_priv
;
1370 switch (stm32lx_info
->part_info
.id
) {
1371 case 0x447: /* STM32L0xx (Cat.5) devices */
1372 if (flash_size_in_kb
== 192 || flash_size_in_kb
== 128) {
1373 stm32lx_info
->part_info
.first_bank_size_kb
= flash_size_in_kb
/ 2;
1374 stm32lx_info
->part_info
.has_dual_banks
= true;
1377 case 0x437: /* STM32L1xx (Cat.5/Cat.6) */
1378 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)