OpenOCD
stm32lx.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2005 by Dominic Rath *
5  * Dominic.Rath@gmx.de *
6  * *
7  * Copyright (C) 2008 by Spencer Oliver *
8  * spen@spen-soft.co.uk *
9  * *
10  * Copyright (C) 2011 by Clement Burin des Roziers *
11  * clement.burin-des-roziers@hikob.com *
12  ***************************************************************************/
13 
14 #ifdef HAVE_CONFIG_H
15 #include "config.h"
16 #endif
17 
18 #include "imp.h"
19 #include <helper/binarybuffer.h>
20 #include <target/algorithm.h>
21 #include <target/armv7m.h>
22 #include <target/cortex_m.h>
23 
24 /* stm32lx flash register locations */
25 
26 #define FLASH_ACR 0x00
27 #define FLASH_PECR 0x04
28 #define FLASH_PDKEYR 0x08
29 #define FLASH_PEKEYR 0x0C
30 #define FLASH_PRGKEYR 0x10
31 #define FLASH_OPTKEYR 0x14
32 #define FLASH_SR 0x18
33 #define FLASH_OBR 0x1C
34 #define FLASH_WRPR 0x20
35 
36 /* FLASH_ACR bites */
37 #define FLASH_ACR__LATENCY (1<<0)
38 #define FLASH_ACR__PRFTEN (1<<1)
39 #define FLASH_ACR__ACC64 (1<<2)
40 #define FLASH_ACR__SLEEP_PD (1<<3)
41 #define FLASH_ACR__RUN_PD (1<<4)
42 
43 /* FLASH_PECR bits */
44 #define FLASH_PECR__PELOCK (1<<0)
45 #define FLASH_PECR__PRGLOCK (1<<1)
46 #define FLASH_PECR__OPTLOCK (1<<2)
47 #define FLASH_PECR__PROG (1<<3)
48 #define FLASH_PECR__DATA (1<<4)
49 #define FLASH_PECR__FTDW (1<<8)
50 #define FLASH_PECR__ERASE (1<<9)
51 #define FLASH_PECR__FPRG (1<<10)
52 #define FLASH_PECR__EOPIE (1<<16)
53 #define FLASH_PECR__ERRIE (1<<17)
54 #define FLASH_PECR__OBL_LAUNCH (1<<18)
55 
56 /* FLASH_SR bits */
57 #define FLASH_SR__BSY (1<<0)
58 #define FLASH_SR__EOP (1<<1)
59 #define FLASH_SR__ENDHV (1<<2)
60 #define FLASH_SR__READY (1<<3)
61 #define FLASH_SR__WRPERR (1<<8)
62 #define FLASH_SR__PGAERR (1<<9)
63 #define FLASH_SR__SIZERR (1<<10)
64 #define FLASH_SR__OPTVERR (1<<11)
65 
66 /* Unlock keys */
67 #define PEKEY1 0x89ABCDEF
68 #define PEKEY2 0x02030405
69 #define PRGKEY1 0x8C9DAEBF
70 #define PRGKEY2 0x13141516
71 #define OPTKEY1 0xFBEAD9C8
72 #define OPTKEY2 0x24252627
73 
74 /* other registers */
75 #define DBGMCU_IDCODE 0xE0042000
76 #define DBGMCU_IDCODE_L0 0x40015800
77 
78 /* Constants */
79 #define FLASH_SECTOR_SIZE 4096
80 #define FLASH_BANK0_ADDRESS 0x08000000
81 
82 /* option bytes */
83 #define OPTION_BYTES_ADDRESS 0x1FF80000
84 
85 #define OPTION_BYTE_0_PR1 0xFFFF0000
86 #define OPTION_BYTE_0_PR0 0xFF5500AA
87 
89 static int stm32lx_lock_program_memory(struct flash_bank *bank);
91 static int stm32lx_erase_sector(struct flash_bank *bank, int sector);
92 static int stm32lx_wait_until_bsy_clear(struct flash_bank *bank);
93 static int stm32lx_obl_launch(struct flash_bank *bank);
94 static int stm32lx_lock(struct flash_bank *bank);
95 static int stm32lx_unlock(struct flash_bank *bank);
96 static int stm32lx_mass_erase(struct flash_bank *bank);
98 static int stm32lx_update_part_info(struct flash_bank *bank, uint16_t flash_size_in_kb);
99 
100 struct stm32lx_rev {
101  uint16_t rev;
102  const char *str;
103 };
104 
106  uint16_t id;
107  const char *device_str;
108  const struct stm32lx_rev *revs;
109  size_t num_revs;
110  unsigned int page_size;
111  unsigned int pages_per_sector;
113  uint16_t first_bank_size_kb; /* used when has_dual_banks is true */
115 
116  uint32_t flash_base; /* Flash controller registers location */
117  uint32_t fsize_base; /* Location of FSIZE register */
118 };
119 
121  bool probed;
122  uint32_t idcode;
123  uint32_t user_bank_size;
124  uint32_t flash_base;
125 
127 };
128 
129 static const struct stm32lx_rev stm32_416_revs[] = {
130  { 0x1000, "A" }, { 0x1008, "Y" }, { 0x1038, "W" }, { 0x1078, "V" },
131 };
132 static const struct stm32lx_rev stm32_417_revs[] = {
133  { 0x1000, "A" }, { 0x1008, "Z" }, { 0x1018, "Y" }, { 0x1038, "X" }
134 };
135 static const struct stm32lx_rev stm32_425_revs[] = {
136  { 0x1000, "A" }, { 0x2000, "B" }, { 0x2008, "Y" }, { 0x2018, "1, X" },
137 };
138 static const struct stm32lx_rev stm32_427_revs[] = {
139  { 0x1000, "A" }, { 0x1018, "Y" }, { 0x1038, "X" }, { 0x10f8, "V" },
140 };
141 static const struct stm32lx_rev stm32_429_revs[] = {
142  { 0x1000, "A" }, { 0x1018, "Z" },
143 };
144 static const struct stm32lx_rev stm32_436_revs[] = {
145  { 0x1000, "A" }, { 0x1008, "Z" }, { 0x1018, "Y" }, { 0x1038, "X" },
146 };
147 static const struct stm32lx_rev stm32_437_revs[] = {
148  { 0x1000, "A" },
149 };
150 static const struct stm32lx_rev stm32_447_revs[] = {
151  { 0x1000, "A" }, { 0x2000, "B" }, { 0x2008, "Z" },
152 };
153 static const struct stm32lx_rev stm32_457_revs[] = {
154  { 0x1000, "A" }, { 0x1008, "Z" },
155 };
156 
157 static const struct stm32lx_part_info stm32lx_parts[] = {
158  {
159  .id = 0x416,
160  .revs = stm32_416_revs,
161  .num_revs = ARRAY_SIZE(stm32_416_revs),
162  .device_str = "STM32L1xx (Cat.1 - Low/Medium Density)",
163  .page_size = 256,
164  .pages_per_sector = 16,
165  .max_flash_size_kb = 128,
166  .has_dual_banks = false,
167  .flash_base = 0x40023C00,
168  .fsize_base = 0x1FF8004C,
169  },
170  {
171  .id = 0x417,
172  .revs = stm32_417_revs,
173  .num_revs = ARRAY_SIZE(stm32_417_revs),
174  .device_str = "STM32L0xx (Cat. 3)",
175  .page_size = 128,
176  .pages_per_sector = 32,
177  .max_flash_size_kb = 64,
178  .has_dual_banks = false,
179  .flash_base = 0x40022000,
180  .fsize_base = 0x1FF8007C,
181  },
182  {
183  .id = 0x425,
184  .revs = stm32_425_revs,
185  .num_revs = ARRAY_SIZE(stm32_425_revs),
186  .device_str = "STM32L0xx (Cat. 2)",
187  .page_size = 128,
188  .pages_per_sector = 32,
189  .max_flash_size_kb = 32,
190  .has_dual_banks = false,
191  .flash_base = 0x40022000,
192  .fsize_base = 0x1FF8007C,
193  },
194  {
195  .id = 0x427,
196  .revs = stm32_427_revs,
197  .num_revs = ARRAY_SIZE(stm32_427_revs),
198  .device_str = "STM32L1xx (Cat.3 - Medium+ Density)",
199  .page_size = 256,
200  .pages_per_sector = 16,
201  .max_flash_size_kb = 256,
202  .has_dual_banks = false,
203  .flash_base = 0x40023C00,
204  .fsize_base = 0x1FF800CC,
205  },
206  {
207  .id = 0x429,
208  .revs = stm32_429_revs,
209  .num_revs = ARRAY_SIZE(stm32_429_revs),
210  .device_str = "STM32L1xx (Cat.2)",
211  .page_size = 256,
212  .pages_per_sector = 16,
213  .max_flash_size_kb = 128,
214  .has_dual_banks = false,
215  .flash_base = 0x40023C00,
216  .fsize_base = 0x1FF8004C,
217  },
218  {
219  .id = 0x436,
220  .revs = stm32_436_revs,
221  .num_revs = ARRAY_SIZE(stm32_436_revs),
222  .device_str = "STM32L1xx (Cat.4/Cat.3 - Medium+/High Density)",
223  .page_size = 256,
224  .pages_per_sector = 16,
225  .max_flash_size_kb = 384,
226  .first_bank_size_kb = 192,
227  .has_dual_banks = true,
228  .flash_base = 0x40023C00,
229  .fsize_base = 0x1FF800CC,
230  },
231  {
232  .id = 0x437,
233  .revs = stm32_437_revs,
234  .num_revs = ARRAY_SIZE(stm32_437_revs),
235  .device_str = "STM32L1xx (Cat.5/Cat.6)",
236  .page_size = 256,
237  .pages_per_sector = 16,
238  .max_flash_size_kb = 512,
239  .first_bank_size_kb = 0, /* determined in runtime */
240  .has_dual_banks = true,
241  .flash_base = 0x40023C00,
242  .fsize_base = 0x1FF800CC,
243  },
244  {
245  .id = 0x447,
246  .revs = stm32_447_revs,
247  .num_revs = ARRAY_SIZE(stm32_447_revs),
248  .device_str = "STM32L0xx (Cat.5)",
249  .page_size = 128,
250  .pages_per_sector = 32,
251  .max_flash_size_kb = 192,
252  .first_bank_size_kb = 0, /* determined in runtime */
253  .has_dual_banks = false, /* determined in runtime */
254  .flash_base = 0x40022000,
255  .fsize_base = 0x1FF8007C,
256  },
257  {
258  .id = 0x457,
259  .revs = stm32_457_revs,
260  .num_revs = ARRAY_SIZE(stm32_457_revs),
261  .device_str = "STM32L0xx (Cat.1)",
262  .page_size = 128,
263  .pages_per_sector = 32,
264  .max_flash_size_kb = 16,
265  .has_dual_banks = false,
266  .flash_base = 0x40022000,
267  .fsize_base = 0x1FF8007C,
268  },
269 };
270 
271 /* flash bank stm32lx <base> <size> 0 0 <target#>
272  */
273 FLASH_BANK_COMMAND_HANDLER(stm32lx_flash_bank_command)
274 {
275  struct stm32lx_flash_bank *stm32lx_info;
276  if (CMD_ARGC < 6)
278 
279  /* Create the bank structure */
280  stm32lx_info = calloc(1, sizeof(*stm32lx_info));
281 
282  /* Check allocation */
283  if (!stm32lx_info) {
284  LOG_ERROR("failed to allocate bank structure");
285  return ERROR_FAIL;
286  }
287 
288  bank->driver_priv = stm32lx_info;
289 
290  stm32lx_info->probed = false;
291  stm32lx_info->user_bank_size = bank->size;
292 
293  /* the stm32l erased value is 0x00 */
294  bank->default_padded_value = bank->erased_value = 0x00;
295 
296  return ERROR_OK;
297 }
298 
299 COMMAND_HANDLER(stm32lx_handle_mass_erase_command)
300 {
301  if (CMD_ARGC < 1)
303 
304  struct flash_bank *bank;
305  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
306  if (retval != ERROR_OK)
307  return retval;
308 
309  retval = stm32lx_mass_erase(bank);
310  if (retval == ERROR_OK)
311  command_print(CMD, "stm32lx mass erase complete");
312  else
313  command_print(CMD, "stm32lx mass erase failed");
314 
315  return retval;
316 }
317 
318 COMMAND_HANDLER(stm32lx_handle_lock_command)
319 {
320  if (CMD_ARGC < 1)
322 
323  struct flash_bank *bank;
324  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
325  if (retval != ERROR_OK)
326  return retval;
327 
328  retval = stm32lx_lock(bank);
329 
330  if (retval == ERROR_OK)
331  command_print(CMD, "STM32Lx locked, takes effect after power cycle.");
332  else
333  command_print(CMD, "STM32Lx lock failed");
334 
335  return retval;
336 }
337 
338 COMMAND_HANDLER(stm32lx_handle_unlock_command)
339 {
340  if (CMD_ARGC < 1)
342 
343  struct flash_bank *bank;
344  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
345  if (retval != ERROR_OK)
346  return retval;
347 
348  retval = stm32lx_unlock(bank);
349 
350  if (retval == ERROR_OK)
351  command_print(CMD, "STM32Lx unlocked, takes effect after power cycle.");
352  else
353  command_print(CMD, "STM32Lx unlock failed");
354 
355  return retval;
356 }
357 
358 COMMAND_HANDLER(stm32lx_handle_option_load_command)
359 {
360  if (CMD_ARGC != 1)
362 
363  struct flash_bank *bank;
364  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
365 
366  if (retval != ERROR_OK)
367  return retval;
368 
369  retval = stm32lx_obl_launch(bank);
370  if (retval != ERROR_OK) {
371  command_print(CMD, "failed to load option bytes");
372  return retval;
373  }
374 
375  return ERROR_OK;
376 }
377 
379 {
380  int retval;
381  struct target *target = bank->target;
382  struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
383 
384  uint32_t wrpr;
385 
386  /*
387  * Read the WRPR word, and check each bit (corresponding to each
388  * flash sector
389  */
390  retval = target_read_u32(target, stm32lx_info->flash_base + FLASH_WRPR,
391  &wrpr);
392  if (retval != ERROR_OK)
393  return retval;
394 
395  for (unsigned int i = 0; i < bank->num_sectors; i++) {
396  if (wrpr & (1 << i))
397  bank->sectors[i].is_protected = 1;
398  else
399  bank->sectors[i].is_protected = 0;
400  }
401  return ERROR_OK;
402 }
403 
404 static int stm32lx_erase(struct flash_bank *bank, unsigned int first,
405  unsigned int last)
406 {
407  int retval;
408 
409  /*
410  * It could be possible to do a mass erase if all sectors must be
411  * erased, but it is not implemented yet.
412  */
413 
414  if (bank->target->state != TARGET_HALTED) {
415  LOG_ERROR("Target not halted");
417  }
418 
419  /*
420  * Loop over the selected sectors and erase them
421  */
422  for (unsigned int i = first; i <= last; i++) {
423  retval = stm32lx_erase_sector(bank, i);
424  if (retval != ERROR_OK)
425  return retval;
426  bank->sectors[i].is_erased = 1;
427  }
428  return ERROR_OK;
429 }
430 
431 static int stm32lx_write_half_pages(struct flash_bank *bank, const uint8_t *buffer,
432  uint32_t offset, uint32_t count)
433 {
434  struct target *target = bank->target;
435  struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
436 
437  uint32_t hp_nb = stm32lx_info->part_info.page_size / 2;
438  uint32_t buffer_size = (16384 / hp_nb) * hp_nb; /* must be multiple of hp_nb */
439  struct working_area *write_algorithm;
440  struct working_area *source;
441  uint32_t address = bank->base + offset;
442 
443  struct reg_param reg_params[5];
444  struct armv7m_algorithm armv7m_info;
445 
446  int retval = ERROR_OK;
447 
448  static const uint8_t stm32lx_flash_write_code[] = {
449 #include "../../../contrib/loaders/flash/stm32/stm32lx.inc"
450  };
451 
452  /* Make sure we're performing a half-page aligned write. */
453  if (offset % hp_nb) {
454  LOG_ERROR("The offset must be %" PRIu32 "B-aligned but it is %" PRIi32 "B)", hp_nb, offset);
455  return ERROR_FAIL;
456  }
457  if (count % hp_nb) {
458  LOG_ERROR("The byte count must be %" PRIu32 "B-aligned but count is %" PRIu32 "B)", hp_nb, count);
459  return ERROR_FAIL;
460  }
461 
462  /* flash write code */
463  if (target_alloc_working_area(target, sizeof(stm32lx_flash_write_code),
464  &write_algorithm) != ERROR_OK) {
465  LOG_DEBUG("no working area for block memory writes");
467  }
468 
469  /* Write the flashing code */
470  retval = target_write_buffer(target,
471  write_algorithm->address,
472  sizeof(stm32lx_flash_write_code),
473  stm32lx_flash_write_code);
474  if (retval != ERROR_OK) {
475  target_free_working_area(target, write_algorithm);
476  return retval;
477  }
478 
479  /* Allocate half pages memory */
481  if (buffer_size > 1024)
482  buffer_size -= 1024;
483  else
484  buffer_size /= 2;
485 
486  if (buffer_size <= stm32lx_info->part_info.page_size) {
487  /* we already allocated the writing code, but failed to get a
488  * buffer, free the algorithm */
489  target_free_working_area(target, write_algorithm);
490 
491  LOG_WARNING("no large enough working area available, can't do block memory writes");
493  } else {
494  /* Make sure we're still asking for an integral number of half-pages */
495  buffer_size -= buffer_size % hp_nb;
496  }
497  }
498 
499  armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
500  armv7m_info.core_mode = ARM_MODE_THREAD;
501  init_reg_param(&reg_params[0], "r0", 32, PARAM_OUT);
502  init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT);
503  init_reg_param(&reg_params[2], "r2", 32, PARAM_OUT);
504  init_reg_param(&reg_params[3], "r3", 32, PARAM_OUT);
505  init_reg_param(&reg_params[4], "r4", 32, PARAM_OUT);
506 
507  /* Enable half-page write */
509  if (retval != ERROR_OK) {
511  target_free_working_area(target, write_algorithm);
512 
513  destroy_reg_param(&reg_params[0]);
514  destroy_reg_param(&reg_params[1]);
515  destroy_reg_param(&reg_params[2]);
516  destroy_reg_param(&reg_params[3]);
517  destroy_reg_param(&reg_params[4]);
518  return retval;
519  }
520 
521  struct armv7m_common *armv7m = target_to_armv7m(target);
522  if (!armv7m) {
523 
524  /* something is very wrong if armv7m is NULL */
525  LOG_ERROR("unable to get armv7m target");
526  return retval;
527  }
528 
529  /* save any DEMCR flags and configure target to catch any Hard Faults */
530  uint32_t demcr_save = armv7m->demcr;
531  armv7m->demcr = VC_HARDERR;
532 
533  /* Loop while there are bytes to write */
534  while (count > 0) {
535  uint32_t this_count;
536  this_count = (count > buffer_size) ? buffer_size : count;
537 
538  /* Write the next half pages */
539  retval = target_write_buffer(target, source->address, this_count, buffer);
540  if (retval != ERROR_OK)
541  break;
542 
543  /* 4: Store useful information in the registers */
544  /* the destination address of the copy (R0) */
545  buf_set_u32(reg_params[0].value, 0, 32, address);
546  /* The source address of the copy (R1) */
547  buf_set_u32(reg_params[1].value, 0, 32, source->address);
548  /* The number of half pages to copy (R2) */
549  buf_set_u32(reg_params[2].value, 0, 32, this_count / hp_nb);
550  /* The size in byes of a half page (R3) */
551  buf_set_u32(reg_params[3].value, 0, 32, hp_nb);
552  /* The flash base address (R4) */
553  buf_set_u32(reg_params[4].value, 0, 32, stm32lx_info->flash_base);
554 
555  /* 5: Execute the bunch of code */
556  retval = target_run_algorithm(target, 0, NULL,
557  ARRAY_SIZE(reg_params), reg_params,
558  write_algorithm->address, 0, 10000, &armv7m_info);
559  if (retval != ERROR_OK)
560  break;
561 
562  /* check for Hard Fault */
563  if (armv7m->exception_number == 3)
564  break;
565 
566  /* 6: Wait while busy */
568  if (retval != ERROR_OK)
569  break;
570 
571  buffer += this_count;
572  address += this_count;
573  count -= this_count;
574  }
575 
576  /* restore previous flags */
577  armv7m->demcr = demcr_save;
578 
579  if (armv7m->exception_number == 3) {
580 
581  /* the stm32l15x devices seem to have an issue when blank.
582  * if a ram loader is executed on a blank device it will
583  * Hard Fault, this issue does not happen for a already programmed device.
584  * A related issue is described in the stm32l151xx errata (Doc ID 17721 Rev 6 - 2.1.3).
585  * The workaround of handling the Hard Fault exception does work, but makes the
586  * loader more complicated, as a compromise we manually write the pages, programming time
587  * is reduced by 50% using this slower method.
588  */
589 
590  LOG_WARNING("Couldn't use loader, falling back to page memory writes");
591 
592  while (count > 0) {
593  uint32_t this_count;
594  this_count = (count > hp_nb) ? hp_nb : count;
595 
596  /* Write the next half pages */
597  retval = target_write_buffer(target, address, this_count, buffer);
598  if (retval != ERROR_OK)
599  break;
600 
601  /* Wait while busy */
603  if (retval != ERROR_OK)
604  break;
605 
606  buffer += this_count;
607  address += this_count;
608  count -= this_count;
609  }
610  }
611 
612  if (retval == ERROR_OK)
614 
616  target_free_working_area(target, write_algorithm);
617 
618  destroy_reg_param(&reg_params[0]);
619  destroy_reg_param(&reg_params[1]);
620  destroy_reg_param(&reg_params[2]);
621  destroy_reg_param(&reg_params[3]);
622  destroy_reg_param(&reg_params[4]);
623 
624  return retval;
625 }
626 
627 static int stm32lx_write(struct flash_bank *bank, const uint8_t *buffer,
628  uint32_t offset, uint32_t count)
629 {
630  struct target *target = bank->target;
631  struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
632 
633  uint32_t hp_nb = stm32lx_info->part_info.page_size / 2;
634  uint32_t halfpages_number;
635  uint32_t bytes_remaining = 0;
636  uint32_t address = bank->base + offset;
637  uint32_t bytes_written = 0;
638  int retval, retval2;
639 
640  if (bank->target->state != TARGET_HALTED) {
641  LOG_ERROR("Target not halted");
643  }
644 
645  if (offset & 0x3) {
646  LOG_ERROR("offset 0x%" PRIx32 " breaks required 4-byte alignment", offset);
648  }
649 
651  if (retval != ERROR_OK)
652  return retval;
653 
654  /* first we need to write any unaligned head bytes up to
655  * the next 128 byte page */
656 
657  if (offset % hp_nb)
658  bytes_remaining = MIN(count, hp_nb - (offset % hp_nb));
659 
660  while (bytes_remaining > 0) {
661  uint8_t value[4] = {0xff, 0xff, 0xff, 0xff};
662 
663  /* copy remaining bytes into the write buffer */
664  uint32_t bytes_to_write = MIN(4, bytes_remaining);
665  memcpy(value, buffer + bytes_written, bytes_to_write);
666 
667  retval = target_write_buffer(target, address, 4, value);
668  if (retval != ERROR_OK)
669  goto reset_pg_and_lock;
670 
671  bytes_written += bytes_to_write;
672  bytes_remaining -= bytes_to_write;
673  address += 4;
674 
676  if (retval != ERROR_OK)
677  goto reset_pg_and_lock;
678  }
679 
680  offset += bytes_written;
681  count -= bytes_written;
682 
683  /* this should always pass this check here */
684  assert((offset % hp_nb) == 0);
685 
686  /* calculate half pages */
687  halfpages_number = count / hp_nb;
688 
689  if (halfpages_number) {
690  retval = stm32lx_write_half_pages(bank, buffer + bytes_written, offset, hp_nb * halfpages_number);
691  if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE) {
692  /* attempt slow memory writes */
693  LOG_WARNING("couldn't use block writes, falling back to single memory accesses");
694  halfpages_number = 0;
695  } else {
696  if (retval != ERROR_OK)
697  return ERROR_FAIL;
698  }
699  }
700 
701  /* write any remaining bytes */
702  uint32_t page_bytes_written = hp_nb * halfpages_number;
703  bytes_written += page_bytes_written;
704  address += page_bytes_written;
705  bytes_remaining = count - page_bytes_written;
706 
708  if (retval != ERROR_OK)
709  return retval;
710 
711  while (bytes_remaining > 0) {
712  uint8_t value[4] = {0xff, 0xff, 0xff, 0xff};
713 
714  /* copy remaining bytes into the write buffer */
715  uint32_t bytes_to_write = MIN(4, bytes_remaining);
716  memcpy(value, buffer + bytes_written, bytes_to_write);
717 
718  retval = target_write_buffer(target, address, 4, value);
719  if (retval != ERROR_OK)
720  goto reset_pg_and_lock;
721 
722  bytes_written += bytes_to_write;
723  bytes_remaining -= bytes_to_write;
724  address += 4;
725 
727  if (retval != ERROR_OK)
728  goto reset_pg_and_lock;
729  }
730 
731 reset_pg_and_lock:
733  if (retval == ERROR_OK)
734  retval = retval2;
735 
736  return retval;
737 }
738 
739 static int stm32lx_read_id_code(struct target *target, uint32_t *id)
740 {
741  struct armv7m_common *armv7m = target_to_armv7m(target);
742  int retval;
743  if (armv7m->arm.arch == ARM_ARCH_V6M)
744  retval = target_read_u32(target, DBGMCU_IDCODE_L0, id);
745  else
746  /* read stm32 device id register */
747  retval = target_read_u32(target, DBGMCU_IDCODE, id);
748  return retval;
749 }
750 
751 static int stm32lx_probe(struct flash_bank *bank)
752 {
753  struct target *target = bank->target;
754  struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
755  uint16_t flash_size_in_kb;
756  uint32_t device_id;
757  uint32_t base_address = FLASH_BANK0_ADDRESS;
758  uint32_t second_bank_base;
759  unsigned int n;
760 
761  stm32lx_info->probed = false;
762 
763  int retval = stm32lx_read_id_code(bank->target, &device_id);
764  if (retval != ERROR_OK)
765  return retval;
766 
767  stm32lx_info->idcode = device_id;
768 
769  LOG_DEBUG("device id = 0x%08" PRIx32, device_id);
770 
771  for (n = 0; n < ARRAY_SIZE(stm32lx_parts); n++) {
772  if ((device_id & 0xfff) == stm32lx_parts[n].id) {
773  stm32lx_info->part_info = stm32lx_parts[n];
774  break;
775  }
776  }
777 
778  if (n == ARRAY_SIZE(stm32lx_parts)) {
779  LOG_ERROR("Cannot identify target as an STM32 L0 or L1 family device.");
780  return ERROR_FAIL;
781  } else {
782  LOG_INFO("Device: %s", stm32lx_info->part_info.device_str);
783  }
784 
785  stm32lx_info->flash_base = stm32lx_info->part_info.flash_base;
786 
787  /* Get the flash size from target. */
788  retval = target_read_u16(target, stm32lx_info->part_info.fsize_base,
789  &flash_size_in_kb);
790 
791  /* 0x436 devices report their flash size as a 0 or 1 code indicating 384K
792  * or 256K, respectively. Please see RM0038 r8 or newer and refer to
793  * section 30.1.1. */
794  if (retval == ERROR_OK && (device_id & 0xfff) == 0x436) {
795  if (flash_size_in_kb == 0)
796  flash_size_in_kb = 384;
797  else if (flash_size_in_kb == 1)
798  flash_size_in_kb = 256;
799  }
800 
801  /* 0x429 devices only use the lowest 8 bits of the flash size register */
802  if (retval == ERROR_OK && (device_id & 0xfff) == 0x429) {
803  flash_size_in_kb &= 0xff;
804  }
805 
806  /* Failed reading flash size or flash size invalid (early silicon),
807  * default to max target family */
808  if (retval != ERROR_OK || flash_size_in_kb == 0xffff || flash_size_in_kb == 0) {
809  LOG_WARNING("STM32L flash size failed, probe inaccurate - assuming %dk flash",
810  stm32lx_info->part_info.max_flash_size_kb);
811  flash_size_in_kb = stm32lx_info->part_info.max_flash_size_kb;
812  } else if (flash_size_in_kb > stm32lx_info->part_info.max_flash_size_kb) {
813  LOG_WARNING("STM32L probed flash size assumed incorrect since FLASH_SIZE=%dk > %dk, - assuming %dk flash",
814  flash_size_in_kb, stm32lx_info->part_info.max_flash_size_kb,
815  stm32lx_info->part_info.max_flash_size_kb);
816  flash_size_in_kb = stm32lx_info->part_info.max_flash_size_kb;
817  }
818 
819  /* Overwrite default dual-bank configuration */
820  retval = stm32lx_update_part_info(bank, flash_size_in_kb);
821  if (retval != ERROR_OK)
822  return ERROR_FAIL;
823 
824  if (stm32lx_info->part_info.has_dual_banks) {
825  /* Use the configured base address to determine if this is the first or second flash bank.
826  * Verify that the base address is reasonably correct and determine the flash bank size
827  */
828  second_bank_base = base_address +
829  stm32lx_info->part_info.first_bank_size_kb * 1024;
830  if (bank->base == second_bank_base || !bank->base) {
831  /* This is the second bank */
832  base_address = second_bank_base;
833  flash_size_in_kb = flash_size_in_kb -
834  stm32lx_info->part_info.first_bank_size_kb;
835  } else if (bank->base == base_address) {
836  /* This is the first bank */
837  flash_size_in_kb = stm32lx_info->part_info.first_bank_size_kb;
838  } else {
839  LOG_WARNING("STM32L flash bank base address config is incorrect. "
840  TARGET_ADDR_FMT " but should rather be 0x%" PRIx32
841  " or 0x%" PRIx32,
842  bank->base, base_address, second_bank_base);
843  return ERROR_FAIL;
844  }
845  LOG_INFO("STM32L flash has dual banks. Bank (%u) size is %dkb, base address is 0x%" PRIx32,
846  bank->bank_number, flash_size_in_kb, base_address);
847  } else {
848  LOG_INFO("STM32L flash size is %dkb, base address is 0x%" PRIx32, flash_size_in_kb, base_address);
849  }
850 
851  /* if the user sets the size manually then ignore the probed value
852  * this allows us to work around devices that have a invalid flash size register value */
853  if (stm32lx_info->user_bank_size) {
854  flash_size_in_kb = stm32lx_info->user_bank_size / 1024;
855  LOG_INFO("ignoring flash probed value, using configured bank size: %dkbytes", flash_size_in_kb);
856  }
857 
858  /* calculate numbers of sectors (4kB per sector) */
859  unsigned int num_sectors = (flash_size_in_kb * 1024) / FLASH_SECTOR_SIZE;
860 
861  free(bank->sectors);
862 
863  bank->size = flash_size_in_kb * 1024;
864  bank->base = base_address;
865  bank->num_sectors = num_sectors;
866  bank->sectors = malloc(sizeof(struct flash_sector) * num_sectors);
867  if (!bank->sectors) {
868  LOG_ERROR("failed to allocate bank sectors");
869  return ERROR_FAIL;
870  }
871 
872  for (unsigned int i = 0; i < num_sectors; i++) {
873  bank->sectors[i].offset = i * FLASH_SECTOR_SIZE;
874  bank->sectors[i].size = FLASH_SECTOR_SIZE;
875  bank->sectors[i].is_erased = -1;
876  bank->sectors[i].is_protected = -1;
877  }
878 
879  stm32lx_info->probed = true;
880 
881  return ERROR_OK;
882 }
883 
884 static int stm32lx_auto_probe(struct flash_bank *bank)
885 {
886  struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
887 
888  if (stm32lx_info->probed)
889  return ERROR_OK;
890 
891  return stm32lx_probe(bank);
892 }
893 
894 /* This method must return a string displaying information about the bank */
896 {
897  struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
898  const struct stm32lx_part_info *info = &stm32lx_info->part_info;
899  uint16_t rev_id = stm32lx_info->idcode >> 16;
900  const char *rev_str = NULL;
901 
902  if (!stm32lx_info->probed) {
903  int retval = stm32lx_probe(bank);
904  if (retval != ERROR_OK) {
905  command_print_sameline(cmd, "Unable to find bank information.");
906  return retval;
907  }
908  }
909 
910  for (unsigned int i = 0; i < info->num_revs; i++)
911  if (rev_id == info->revs[i].rev)
912  rev_str = info->revs[i].str;
913 
914  if (rev_str) {
915  command_print_sameline(cmd, "%s - Rev: %s", info->device_str, rev_str);
916  } else {
917  command_print_sameline(cmd, "%s - Rev: unknown (0x%04x)", info->device_str, rev_id);
918  }
919 
920  return ERROR_OK;
921 }
922 
923 static const struct command_registration stm32lx_exec_command_handlers[] = {
924  {
925  .name = "mass_erase",
926  .handler = stm32lx_handle_mass_erase_command,
927  .mode = COMMAND_EXEC,
928  .usage = "bank_id",
929  .help = "Erase entire flash device. including available EEPROM",
930  },
931  {
932  .name = "lock",
933  .handler = stm32lx_handle_lock_command,
934  .mode = COMMAND_EXEC,
935  .usage = "bank_id",
936  .help = "Increase the readout protection to Level 1.",
937  },
938  {
939  .name = "unlock",
940  .handler = stm32lx_handle_unlock_command,
941  .mode = COMMAND_EXEC,
942  .usage = "bank_id",
943  .help = "Lower the readout protection from Level 1 to 0.",
944  },
945  {
946  .name = "option_load",
947  .handler = stm32lx_handle_option_load_command,
948  .mode = COMMAND_EXEC,
949  .usage = "bank_id",
950  .help = "Force re-load of device options (will cause device reset).",
951  },
953 };
954 
955 static const struct command_registration stm32lx_command_handlers[] = {
956  {
957  .name = "stm32lx",
958  .mode = COMMAND_ANY,
959  .help = "stm32lx flash command group",
960  .usage = "",
962  },
964 };
965 
966 const struct flash_driver stm32lx_flash = {
967  .name = "stm32lx",
968  .commands = stm32lx_command_handlers,
969  .flash_bank_command = stm32lx_flash_bank_command,
970  .erase = stm32lx_erase,
971  .write = stm32lx_write,
972  .read = default_flash_read,
973  .probe = stm32lx_probe,
974  .auto_probe = stm32lx_auto_probe,
975  .erase_check = default_flash_blank_check,
976  .protect_check = stm32lx_protect_check,
977  .info = stm32lx_get_info,
978  .free_driver_priv = default_flash_free_driver_priv,
979 };
980 
981 /* Static methods implementation */
983 {
984  struct target *target = bank->target;
985  struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
986  int retval;
987  uint32_t reg32;
988 
989  /*
990  * Unlocking the program memory is done by unlocking the PECR,
991  * then by writing the 2 PRGKEY to the PRGKEYR register
992  */
993 
994  /* check flash is not already unlocked */
995  retval = target_read_u32(target, stm32lx_info->flash_base + FLASH_PECR,
996  &reg32);
997  if (retval != ERROR_OK)
998  return retval;
999 
1000  if ((reg32 & FLASH_PECR__PRGLOCK) == 0)
1001  return ERROR_OK;
1002 
1003  /* To unlock the PECR write the 2 PEKEY to the PEKEYR register */
1004  retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_PEKEYR,
1005  PEKEY1);
1006  if (retval != ERROR_OK)
1007  return retval;
1008 
1009  retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_PEKEYR,
1010  PEKEY2);
1011  if (retval != ERROR_OK)
1012  return retval;
1013 
1014  /* Make sure it worked */
1015  retval = target_read_u32(target, stm32lx_info->flash_base + FLASH_PECR,
1016  &reg32);
1017  if (retval != ERROR_OK)
1018  return retval;
1019 
1020  if (reg32 & FLASH_PECR__PELOCK) {
1021  LOG_ERROR("PELOCK is not cleared :(");
1023  }
1024 
1025  retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_PRGKEYR,
1026  PRGKEY1);
1027  if (retval != ERROR_OK)
1028  return retval;
1029  retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_PRGKEYR,
1030  PRGKEY2);
1031  if (retval != ERROR_OK)
1032  return retval;
1033 
1034  /* Make sure it worked */
1035  retval = target_read_u32(target, stm32lx_info->flash_base + FLASH_PECR,
1036  &reg32);
1037  if (retval != ERROR_OK)
1038  return retval;
1039 
1040  if (reg32 & FLASH_PECR__PRGLOCK) {
1041  LOG_ERROR("PRGLOCK is not cleared :(");
1043  }
1044 
1045  return ERROR_OK;
1046 }
1047 
1049 {
1050  struct target *target = bank->target;
1051  struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
1052  int retval;
1053  uint32_t reg32;
1054 
1059  if (retval != ERROR_OK)
1060  return retval;
1061 
1062  retval = target_read_u32(target, stm32lx_info->flash_base + FLASH_PECR,
1063  &reg32);
1064  if (retval != ERROR_OK)
1065  return retval;
1066 
1067  reg32 |= FLASH_PECR__FPRG;
1068  retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_PECR,
1069  reg32);
1070  if (retval != ERROR_OK)
1071  return retval;
1072 
1073  retval = target_read_u32(target, stm32lx_info->flash_base + FLASH_PECR,
1074  &reg32);
1075  if (retval != ERROR_OK)
1076  return retval;
1077 
1078  reg32 |= FLASH_PECR__PROG;
1079  retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_PECR,
1080  reg32);
1081 
1082  return retval;
1083 }
1084 
1086 {
1087  struct target *target = bank->target;
1088  struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
1089  int retval;
1090  uint32_t reg32;
1091 
1092  /* To lock the program memory, simply set the lock bit and lock PECR */
1093 
1094  retval = target_read_u32(target, stm32lx_info->flash_base + FLASH_PECR,
1095  &reg32);
1096  if (retval != ERROR_OK)
1097  return retval;
1098 
1099  reg32 |= FLASH_PECR__PRGLOCK;
1100  retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_PECR,
1101  reg32);
1102  if (retval != ERROR_OK)
1103  return retval;
1104 
1105  retval = target_read_u32(target, stm32lx_info->flash_base + FLASH_PECR,
1106  &reg32);
1107  if (retval != ERROR_OK)
1108  return retval;
1109 
1110  reg32 |= FLASH_PECR__PELOCK;
1111  retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_PECR,
1112  reg32);
1113  if (retval != ERROR_OK)
1114  return retval;
1115 
1116  return ERROR_OK;
1117 }
1118 
1119 static int stm32lx_erase_sector(struct flash_bank *bank, int sector)
1120 {
1121  struct target *target = bank->target;
1122  struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
1123  int retval;
1124  uint32_t reg32;
1125 
1126  /*
1127  * To erase a sector (i.e. stm32lx_info->part_info.pages_per_sector pages),
1128  * first unlock the memory, loop over the pages of this sector
1129  * and write 0x0 to its first word.
1130  */
1131 
1133  if (retval != ERROR_OK)
1134  return retval;
1135 
1136  for (int page = 0; page < (int)stm32lx_info->part_info.pages_per_sector;
1137  page++) {
1139  retval = target_write_u32(target,
1140  stm32lx_info->flash_base + FLASH_PECR, reg32);
1141  if (retval != ERROR_OK)
1142  return retval;
1143 
1145  if (retval != ERROR_OK)
1146  return retval;
1147 
1148  uint32_t addr = bank->base + bank->sectors[sector].offset + (page
1149  * stm32lx_info->part_info.page_size);
1150  retval = target_write_u32(target, addr, 0x0);
1151  if (retval != ERROR_OK)
1152  return retval;
1153 
1155  if (retval != ERROR_OK)
1156  return retval;
1157  }
1158 
1160  if (retval != ERROR_OK)
1161  return retval;
1162 
1163  return ERROR_OK;
1164 }
1165 
1166 static inline int stm32lx_get_flash_status(struct flash_bank *bank, uint32_t *status)
1167 {
1168  struct target *target = bank->target;
1169  struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
1170 
1171  return target_read_u32(target, stm32lx_info->flash_base + FLASH_SR, status);
1172 }
1173 
1175 {
1177 }
1178 
1180 {
1181  struct target *target = bank->target;
1182  struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
1183  int retval;
1184  uint32_t reg32;
1185 
1186  /*
1187  * Unlocking the options bytes is done by unlocking the PECR,
1188  * then by writing the 2 FLASH_PEKEYR to the FLASH_OPTKEYR register
1189  */
1190 
1191  /* check flash is not already unlocked */
1192  retval = target_read_u32(target, stm32lx_info->flash_base + FLASH_PECR, &reg32);
1193  if (retval != ERROR_OK)
1194  return retval;
1195 
1196  if ((reg32 & FLASH_PECR__OPTLOCK) == 0)
1197  return ERROR_OK;
1198 
1199  if ((reg32 & FLASH_PECR__PELOCK) != 0) {
1200 
1201  retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_PEKEYR, PEKEY1);
1202  if (retval != ERROR_OK)
1203  return retval;
1204 
1205  retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_PEKEYR, PEKEY2);
1206  if (retval != ERROR_OK)
1207  return retval;
1208  }
1209 
1210  /* To unlock the PECR write the 2 OPTKEY to the FLASH_OPTKEYR register */
1211  retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_OPTKEYR, OPTKEY1);
1212  if (retval != ERROR_OK)
1213  return retval;
1214 
1215  retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_OPTKEYR, OPTKEY2);
1216  if (retval != ERROR_OK)
1217  return retval;
1218 
1219  return ERROR_OK;
1220 }
1221 
1223 {
1224  struct target *target = bank->target;
1225  struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
1226  uint32_t status;
1227  int retval = ERROR_OK;
1228 
1229  /* wait for busy to clear */
1230  for (;;) {
1231  retval = stm32lx_get_flash_status(bank, &status);
1232  if (retval != ERROR_OK)
1233  return retval;
1234 
1235  LOG_DEBUG("status: 0x%" PRIx32, status);
1236  if ((status & FLASH_SR__BSY) == 0)
1237  break;
1238 
1239  if (timeout-- <= 0) {
1240  LOG_ERROR("timed out waiting for flash");
1241  return ERROR_FAIL;
1242  }
1243  alive_sleep(1);
1244  }
1245 
1246  if (status & FLASH_SR__WRPERR) {
1247  LOG_ERROR("access denied / write protected");
1248  retval = ERROR_FAIL;
1249  }
1250 
1251  if (status & FLASH_SR__PGAERR) {
1252  LOG_ERROR("invalid program address");
1253  retval = ERROR_FAIL;
1254  }
1255 
1256  /* Clear but report errors */
1257  if (status & FLASH_SR__OPTVERR) {
1258  /* If this operation fails, we ignore it and report the original retval */
1260  }
1261 
1262  return retval;
1263 }
1264 
1266 {
1267  struct target *target = bank->target;
1268  struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
1269 
1270  int retval = stm32lx_unlock_options_bytes(bank);
1271  if (retval != ERROR_OK)
1272  return retval;
1273 
1274  /* This will fail as the target gets immediately rebooted */
1275  target_write_u32(target, stm32lx_info->flash_base + FLASH_PECR,
1277 
1278  size_t tries = 10;
1279  do {
1281  retval = target_poll(target);
1282  } while (--tries > 0 &&
1283  (retval != ERROR_OK || target->state != TARGET_HALTED));
1284 
1285  return tries ? ERROR_OK : ERROR_FAIL;
1286 }
1287 
1288 static int stm32lx_lock(struct flash_bank *bank)
1289 {
1290  int retval;
1291  struct target *target = bank->target;
1292 
1293  if (target->state != TARGET_HALTED) {
1294  LOG_ERROR("Target not halted");
1295  return ERROR_TARGET_NOT_HALTED;
1296  }
1297 
1299  if (retval != ERROR_OK)
1300  return retval;
1301 
1302  /* set the RDP protection level to 1 */
1304  if (retval != ERROR_OK)
1305  return retval;
1306 
1307  return ERROR_OK;
1308 }
1309 
1310 static int stm32lx_unlock(struct flash_bank *bank)
1311 {
1312  int retval;
1313  struct target *target = bank->target;
1314 
1315  if (target->state != TARGET_HALTED) {
1316  LOG_ERROR("Target not halted");
1317  return ERROR_TARGET_NOT_HALTED;
1318  }
1319 
1321  if (retval != ERROR_OK)
1322  return retval;
1323 
1324  /* set the RDP protection level to 0 */
1326  if (retval != ERROR_OK)
1327  return retval;
1328 
1329  retval = stm32lx_wait_until_bsy_clear_timeout(bank, 30000);
1330  if (retval != ERROR_OK)
1331  return retval;
1332 
1333  return ERROR_OK;
1334 }
1335 
1337 {
1338  int retval;
1339  struct target *target = bank->target;
1340  struct stm32lx_flash_bank *stm32lx_info = NULL;
1341  uint32_t reg32;
1342 
1343  if (target->state != TARGET_HALTED) {
1344  LOG_ERROR("Target not halted");
1345  return ERROR_TARGET_NOT_HALTED;
1346  }
1347 
1348  stm32lx_info = bank->driver_priv;
1349 
1350  retval = stm32lx_lock(bank);
1351  if (retval != ERROR_OK)
1352  return retval;
1353 
1354  retval = stm32lx_obl_launch(bank);
1355  if (retval != ERROR_OK)
1356  return retval;
1357 
1358  retval = stm32lx_unlock(bank);
1359  if (retval != ERROR_OK)
1360  return retval;
1361 
1362  retval = stm32lx_obl_launch(bank);
1363  if (retval != ERROR_OK)
1364  return retval;
1365 
1366  retval = target_read_u32(target, stm32lx_info->flash_base + FLASH_PECR, &reg32);
1367  if (retval != ERROR_OK)
1368  return retval;
1369 
1370  retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_PECR, reg32 | FLASH_PECR__OPTLOCK);
1371  if (retval != ERROR_OK)
1372  return retval;
1373 
1374  return ERROR_OK;
1375 }
1376 
1377 static int stm32lx_update_part_info(struct flash_bank *bank, uint16_t flash_size_in_kb)
1378 {
1379  struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
1380 
1381  switch (stm32lx_info->part_info.id) {
1382  case 0x447: /* STM32L0xx (Cat.5) devices */
1383  if (flash_size_in_kb == 192 || flash_size_in_kb == 128) {
1384  stm32lx_info->part_info.first_bank_size_kb = flash_size_in_kb / 2;
1385  stm32lx_info->part_info.has_dual_banks = true;
1386  }
1387  break;
1388  case 0x437: /* STM32L1xx (Cat.5/Cat.6) */
1389  stm32lx_info->part_info.first_bank_size_kb = flash_size_in_kb / 2;
1390  break;
1391  }
1392 
1393  return ERROR_OK;
1394 }
void init_reg_param(struct reg_param *param, const char *reg_name, uint32_t size, enum param_direction direction)
Definition: algorithm.c:29
void destroy_reg_param(struct reg_param *param)
Definition: algorithm.c:38
@ PARAM_OUT
Definition: algorithm.h:16
@ ARM_ARCH_V6M
Definition: arm.h:56
@ ARM_MODE_THREAD
Definition: arm.h:94
static struct armv7m_common * target_to_armv7m(struct target *target)
Definition: armv7m.h:273
#define ARMV7M_COMMON_MAGIC
Definition: armv7m.h:229
Support functions to access arbitrary bits in a byte array.
static void buf_set_u32(uint8_t *_buffer, unsigned int first, unsigned int num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
Definition: binarybuffer.h:34
void command_print_sameline(struct command_invocation *cmd, const char *format,...)
Definition: command.c:348
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:371
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:141
#define CALL_COMMAND_HANDLER(name, extra ...)
Use this to macro to call a command helper (or a nested handler).
Definition: command.h:118
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:400
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:151
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:251
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
#define VC_HARDERR
Definition: cortex_m.h:188
uint64_t buffer
Pointer to data buffer to send over SPI.
Definition: dw-spi-helper.h:0
uint32_t buffer_size
Size of dw_spi_program::buffer.
Definition: dw-spi-helper.h:5
uint32_t address
Starting address. Sector aligned.
Definition: dw-spi-helper.h:0
uint8_t bank
Definition: esirisc.c:135
#define ERROR_FLASH_OPERATION_FAILED
Definition: flash/common.h:30
#define ERROR_FLASH_DST_BREAKS_ALIGNMENT
Definition: flash/common.h:32
int default_flash_blank_check(struct flash_bank *bank)
Provides default erased-bank check handling.
int default_flash_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
Provides default read implementation for flash memory.
void default_flash_free_driver_priv(struct flash_bank *bank)
Deallocates bank->driver_priv.
void alive_sleep(uint64_t ms)
Definition: log.c:470
#define LOG_WARNING(expr ...)
Definition: log.h:131
#define ERROR_FAIL
Definition: log.h:175
#define LOG_ERROR(expr ...)
Definition: log.h:134
#define LOG_INFO(expr ...)
Definition: log.h:128
#define LOG_DEBUG(expr ...)
Definition: log.h:111
#define ERROR_OK
Definition: log.h:169
#define MIN(a, b)
Definition: replacements.h:22
target_addr_t addr
Start address to search for the control block.
Definition: rtt/rtt.c:28
struct rtt_source source
Definition: rtt/rtt.c:23
#define FLASH_SR__OPTVERR
Definition: stm32lx.c:64
static const struct stm32lx_rev stm32_425_revs[]
Definition: stm32lx.c:135
static int stm32lx_protect_check(struct flash_bank *bank)
Definition: stm32lx.c:378
static int stm32lx_lock_program_memory(struct flash_bank *bank)
Definition: stm32lx.c:1085
#define FLASH_SR__PGAERR
Definition: stm32lx.c:62
static const struct stm32lx_rev stm32_447_revs[]
Definition: stm32lx.c:150
static const struct stm32lx_rev stm32_436_revs[]
Definition: stm32lx.c:144
static const struct stm32lx_rev stm32_437_revs[]
Definition: stm32lx.c:147
#define DBGMCU_IDCODE_L0
Definition: stm32lx.c:76
#define OPTION_BYTES_ADDRESS
Definition: stm32lx.c:83
static int stm32lx_update_part_info(struct flash_bank *bank, uint16_t flash_size_in_kb)
Definition: stm32lx.c:1377
#define FLASH_SR
Definition: stm32lx.c:32
#define FLASH_WRPR
Definition: stm32lx.c:34
#define FLASH_PECR
Definition: stm32lx.c:27
static int stm32lx_auto_probe(struct flash_bank *bank)
Definition: stm32lx.c:884
#define OPTION_BYTE_0_PR0
Definition: stm32lx.c:86
#define FLASH_PEKEYR
Definition: stm32lx.c:29
#define PRGKEY2
Definition: stm32lx.c:70
#define FLASH_PECR__OPTLOCK
Definition: stm32lx.c:46
#define FLASH_PECR__ERASE
Definition: stm32lx.c:50
static int stm32lx_read_id_code(struct target *target, uint32_t *id)
Definition: stm32lx.c:739
#define FLASH_BANK0_ADDRESS
Definition: stm32lx.c:80
static const struct stm32lx_rev stm32_416_revs[]
Definition: stm32lx.c:129
static int stm32lx_unlock_program_memory(struct flash_bank *bank)
Definition: stm32lx.c:982
static int stm32lx_probe(struct flash_bank *bank)
Definition: stm32lx.c:751
static const struct stm32lx_rev stm32_457_revs[]
Definition: stm32lx.c:153
static int stm32lx_erase_sector(struct flash_bank *bank, int sector)
Definition: stm32lx.c:1119
static int stm32lx_mass_erase(struct flash_bank *bank)
Definition: stm32lx.c:1336
static const struct command_registration stm32lx_exec_command_handlers[]
Definition: stm32lx.c:923
static const struct stm32lx_rev stm32_427_revs[]
Definition: stm32lx.c:138
static int stm32lx_write_half_pages(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: stm32lx.c:431
static const struct stm32lx_rev stm32_429_revs[]
Definition: stm32lx.c:141
#define FLASH_PECR__OBL_LAUNCH
Definition: stm32lx.c:54
#define FLASH_SR__WRPERR
Definition: stm32lx.c:61
#define FLASH_PECR__PROG
Definition: stm32lx.c:47
static const struct command_registration stm32lx_command_handlers[]
Definition: stm32lx.c:955
#define OPTKEY2
Definition: stm32lx.c:72
#define FLASH_PRGKEYR
Definition: stm32lx.c:30
FLASH_BANK_COMMAND_HANDLER(stm32lx_flash_bank_command)
Definition: stm32lx.c:273
static int stm32lx_wait_until_bsy_clear(struct flash_bank *bank)
Definition: stm32lx.c:1174
static const struct stm32lx_rev stm32_417_revs[]
Definition: stm32lx.c:132
static int stm32lx_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
Definition: stm32lx.c:404
static const struct stm32lx_part_info stm32lx_parts[]
Definition: stm32lx.c:157
static int stm32lx_unlock(struct flash_bank *bank)
Definition: stm32lx.c:1310
#define FLASH_PECR__PELOCK
Definition: stm32lx.c:44
static int stm32lx_get_flash_status(struct flash_bank *bank, uint32_t *status)
Definition: stm32lx.c:1166
#define PEKEY2
Definition: stm32lx.c:68
#define FLASH_PECR__FPRG
Definition: stm32lx.c:51
#define OPTION_BYTE_0_PR1
Definition: stm32lx.c:85
#define PRGKEY1
Definition: stm32lx.c:69
#define FLASH_OPTKEYR
Definition: stm32lx.c:31
#define DBGMCU_IDCODE
Definition: stm32lx.c:75
const struct flash_driver stm32lx_flash
Definition: stm32lx.c:966
static int stm32lx_obl_launch(struct flash_bank *bank)
Definition: stm32lx.c:1265
#define PEKEY1
Definition: stm32lx.c:67
COMMAND_HANDLER(stm32lx_handle_mass_erase_command)
Definition: stm32lx.c:299
#define OPTKEY1
Definition: stm32lx.c:71
#define FLASH_SR__BSY
Definition: stm32lx.c:57
static int stm32lx_unlock_options_bytes(struct flash_bank *bank)
Definition: stm32lx.c:1179
static int stm32lx_wait_until_bsy_clear_timeout(struct flash_bank *bank, int timeout)
Definition: stm32lx.c:1222
#define FLASH_SECTOR_SIZE
Definition: stm32lx.c:79
static int stm32lx_enable_write_half_page(struct flash_bank *bank)
Definition: stm32lx.c:1048
static int stm32lx_lock(struct flash_bank *bank)
Definition: stm32lx.c:1288
#define FLASH_PECR__PRGLOCK
Definition: stm32lx.c:45
static int stm32lx_get_info(struct flash_bank *bank, struct command_invocation *cmd)
Definition: stm32lx.c:895
static int stm32lx_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: stm32lx.c:627
enum arm_arch arch
ARM architecture version.
Definition: arm.h:203
unsigned int common_magic
Definition: armv7m.h:306
enum arm_mode core_mode
Definition: armv7m.h:308
int exception_number
Definition: armv7m.h:236
struct arm arm
Definition: armv7m.h:234
uint32_t demcr
Definition: armv7m.h:242
When run_command is called, a new instance will be created on the stack, filled with the proper value...
Definition: command.h:76
const char * name
Definition: command.h:234
Provides details of a flash bank, available either on-chip or through a major interface.
Definition: nor/core.h:75
Provides the implementation-independent structure that defines all of the callbacks required by OpenO...
Definition: nor/driver.h:39
const char * name
Gives a human-readable name of this flash driver, This field is used to select and initialize the dri...
Definition: nor/driver.h:44
Describes the geometry and status of a single flash sector within a flash bank.
Definition: nor/core.h:28
uint32_t idcode
Definition: stm32lx.c:122
uint32_t flash_base
Definition: stm32lx.c:124
uint32_t user_bank_size
Definition: stm32lx.c:123
struct stm32lx_part_info part_info
Definition: stm32lx.c:126
const char * device_str
Definition: stm32lx.c:107
uint32_t fsize_base
Definition: stm32lx.c:117
uint16_t first_bank_size_kb
Definition: stm32lx.c:113
unsigned int pages_per_sector
Definition: stm32lx.c:111
const struct stm32lx_rev * revs
Definition: stm32lx.c:108
unsigned int page_size
Definition: stm32lx.c:110
uint16_t max_flash_size_kb
Definition: stm32lx.c:112
bool has_dual_banks
Definition: stm32lx.c:114
size_t num_revs
Definition: stm32lx.c:109
uint16_t id
Definition: stm32lx.c:106
uint32_t flash_base
Definition: stm32lx.c:116
const char * str
Definition: stm32lx.c:102
uint16_t rev
Definition: stm32lx.c:101
Definition: target.h:119
enum target_state state
Definition: target.h:160
Definition: psoc6.c:83
target_addr_t address
Definition: target.h:89
int target_halt(struct target *target)
Definition: target.c:516
int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
Definition: target.c:2351
int target_run_algorithm(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_param, target_addr_t entry_point, target_addr_t exit_point, unsigned int timeout_ms, void *arch_info)
Downloads a target-specific native code algorithm to the target, and executes it.
Definition: target.c:783
int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
Definition: target.c:2070
int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
Definition: target.c:2650
int target_poll(struct target *target)
Definition: target.c:486
int target_free_working_area(struct target *target, struct working_area *area)
Free a working area.
Definition: target.c:2128
int target_alloc_working_area_try(struct target *target, uint32_t size, struct working_area **area)
Definition: target.c:1976
int target_read_u16(struct target *target, target_addr_t address, uint16_t *value)
Definition: target.c:2583
int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
Definition: target.c:2559
#define ERROR_TARGET_NOT_HALTED
Definition: target.h:786
@ TARGET_HALTED
Definition: target.h:58
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
Definition: target.h:790
#define TARGET_ADDR_FMT
Definition: types.h:286
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
Definition: types.h:57
static struct ublast_lowlevel_priv info
#define NULL
Definition: usb.h:16
uint8_t status[4]
Definition: vdebug.c:17
uint8_t cmd
Definition: vdebug.c:1
uint8_t offset[4]
Definition: vdebug.c:9
uint8_t count[4]
Definition: vdebug.c:22