OpenOCD
stm32f2x.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 Øyvind Harboe *
11  * oyvind.harboe@zylin.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/cortex_m.h>
22 
23 /* Regarding performance:
24  *
25  * Short story - it might be best to leave the performance at
26  * current levels.
27  *
28  * You may see a jump in speed if you change to using
29  * 32bit words for the block programming.
30  *
31  * Its a shame you cannot use the double word as its
32  * even faster - but you require external VPP for that mode.
33  *
34  * Having said all that 16bit writes give us the widest vdd
35  * operating range, so may be worth adding a note to that effect.
36  *
37  */
38 
39 /* Danger!!!! The STM32F1x and STM32F2x series actually have
40  * quite different flash controllers.
41  *
42  * What's more scary is that the names of the registers and their
43  * addresses are the same, but the actual bits and what they do are
44  * can be very different.
45  *
46  * To reduce testing complexity and dangers of regressions,
47  * a separate file is used for stm32fx2x.
48  *
49  * Sector sizes in kiBytes:
50  * 1 MiByte part with 4 x 16, 1 x 64, 7 x 128.
51  * 1.5 MiByte part with 4 x 16, 1 x 64, 11 x 128.
52  * 2 MiByte part with 4 x 16, 1 x 64, 7 x 128, 4 x 16, 1 x 64, 7 x 128.
53  * 1 MiByte STM32F42x/43x part with DB1M Option set:
54  * 4 x 16, 1 x 64, 3 x 128, 4 x 16, 1 x 64, 3 x 128.
55  *
56  * STM32F7[2|3]
57  * 512 kiByte part with 4 x 16, 1 x 64, 3 x 128.
58  *
59  * STM32F7[4|5]
60  * 1 MiByte part with 4 x 32, 1 x 128, 3 x 256.
61  *
62  * STM32F7[6|7]
63  * 1 MiByte part in single bank mode with 4 x 32, 1 x 128, 3 x 256.
64  * 1 MiByte part in dual-bank mode two banks with 4 x 16, 1 x 64, 3 x 128 each.
65  * 2 MiByte part in single-bank mode with 4 x 32, 1 x 128, 7 x 256.
66  * 2 MiByte part in dual-bank mode two banks with 4 x 16, 1 x 64, 7 x 128 each.
67  *
68  * Protection size is sector size.
69  *
70  * Tested with STM3220F-EVAL board.
71  *
72  * STM32F4xx series for reference.
73  *
74  * RM0090
75  * http://www.st.com/web/en/resource/technical/document/reference_manual/DM00031020.pdf
76  *
77  * PM0059
78  * www.st.com/internet/com/TECHNICAL_RESOURCES/TECHNICAL_LITERATURE/
79  * PROGRAMMING_MANUAL/CD00233952.pdf
80  *
81  * STM32F7xx series for reference.
82  *
83  * RM0385
84  * http://www.st.com/web/en/resource/technical/document/reference_manual/DM00124865.pdf
85  *
86  * RM0410
87  * http://www.st.com/resource/en/reference_manual/dm00224583.pdf
88  *
89  * RM0430
90  * http://www.st.com/resource/en/reference_manual/dm00305666.pdf
91  *
92  * RM0431
93  * http://www.st.com/resource/en/reference_manual/dm00305990.pdf
94  *
95  * STM32F1x series - notice that this code was copy, pasted and knocked
96  * into a stm32f2x driver, so in case something has been converted or
97  * bugs haven't been fixed, here are the original manuals:
98  *
99  * RM0008 - Reference manual
100  *
101  * RM0042, the Flash programming manual for low-, medium- high-density and
102  * connectivity line STM32F10x devices
103  *
104  * PM0068, the Flash programming manual for XL-density STM32F10x devices.
105  *
106  */
107 
108 /* Erase time can be as high as 1000ms, 10x this and it's toast... */
109 #define FLASH_ERASE_TIMEOUT 10000
110 #define FLASH_WRITE_TIMEOUT 5
111 
112 /* Mass erase time can be as high as 32 s in x8 mode. */
113 #define FLASH_MASS_ERASE_TIMEOUT 33000
114 
115 #define FLASH_BANK_BASE 0x80000000
116 
117 #define STM32F2_OTP_SIZE 512
118 #define STM32F2_OTP_SECTOR_SIZE 32
119 #define STM32F2_OTP_BANK_BASE 0x1fff7800
120 #define STM32F2_OTP_LOCK_BASE ((STM32F2_OTP_BANK_BASE) + (STM32F2_OTP_SIZE))
121 
122 /* see RM0410 section 3.6 "One-time programmable bytes" */
123 #define STM32F7_OTP_SECTOR_SIZE 64
124 #define STM32F7_OTP_SIZE 1024
125 #define STM32F7_OTP_BANK_BASE 0x1ff0f000
126 #define STM32F7_OTP_LOCK_BASE ((STM32F7_OTP_BANK_BASE) + (STM32F7_OTP_SIZE))
127 
128 #define STM32_FLASH_BASE 0x40023c00
129 #define STM32_FLASH_ACR 0x40023c00
130 #define STM32_FLASH_KEYR 0x40023c04
131 #define STM32_FLASH_OPTKEYR 0x40023c08
132 #define STM32_FLASH_SR 0x40023c0C
133 #define STM32_FLASH_CR 0x40023c10
134 #define STM32_FLASH_OPTCR 0x40023c14
135 #define STM32_FLASH_OPTCR1 0x40023c18
136 #define STM32_FLASH_OPTCR2 0x40023c1c
137 
138 /* FLASH_CR register bits */
139 #define FLASH_PG (1 << 0)
140 #define FLASH_SER (1 << 1)
141 #define FLASH_MER (1 << 2) /* MER/MER1 for f76x/77x */
142 #define FLASH_MER1 (1 << 15) /* MER2 for f76x/77x, confusing ... */
143 #define FLASH_STRT (1 << 16)
144 #define FLASH_PSIZE_8 (0 << 8)
145 #define FLASH_PSIZE_16 (1 << 8)
146 #define FLASH_PSIZE_32 (2 << 8)
147 #define FLASH_PSIZE_64 (3 << 8)
148 /* The sector number encoding is not straight binary for dual bank flash. */
149 #define FLASH_SNB(a) ((a) << 3)
150 #define FLASH_LOCK (1 << 31)
151 
152 /* FLASH_SR register bits */
153 #define FLASH_BSY (1 << 16)
154 #define FLASH_PGSERR (1 << 7) /* Programming sequence error */
155 #define FLASH_PGPERR (1 << 6) /* Programming parallelism error */
156 #define FLASH_PGAERR (1 << 5) /* Programming alignment error */
157 #define FLASH_WRPERR (1 << 4) /* Write protection error */
158 #define FLASH_OPERR (1 << 1) /* Operation error */
159 
160 #define FLASH_ERROR (FLASH_PGSERR | FLASH_PGPERR | FLASH_PGAERR | FLASH_WRPERR | FLASH_OPERR)
161 
162 /* STM32_FLASH_OPTCR register bits */
163 #define OPTCR_LOCK (1 << 0)
164 #define OPTCR_START (1 << 1)
165 #define OPTCR_NDBANK (1 << 29) /* not dual bank mode */
166 #define OPTCR_DB1M (1 << 30) /* 1 MiB devices dual flash bank option */
167 #define OPTCR_SPRMOD (1 << 31) /* switches PCROPi/nWPRi interpretation */
168 
169 /* STM32_FLASH_OPTCR2 register bits */
170 #define OPTCR2_PCROP_RDP (1 << 31) /* erase PCROP zone when decreasing RDP */
171 
172 /* register unlock keys */
173 #define KEY1 0x45670123
174 #define KEY2 0xCDEF89AB
175 
176 /* option register unlock key */
177 #define OPTKEY1 0x08192A3B
178 #define OPTKEY2 0x4C5D6E7F
179 
180 struct stm32x_options {
181  uint8_t RDP;
182  uint16_t user_options; /* bit 0-7 usual options, bit 8-11 extra options */
183  uint32_t protection;
184  uint32_t boot_addr;
185  uint32_t optcr2_pcrop;
186 };
187 
188 struct stm32x_flash_bank {
190  bool probed;
192  bool has_large_mem; /* F42x/43x/469/479/7xx in dual bank mode */
193  bool has_extra_options; /* F42x/43x/469/479/7xx */
194  bool has_boot_addr; /* F7xx */
195  bool has_optcr2_pcrop; /* F72x/73x */
196  unsigned int protection_bits; /* F413/423 */
197  uint32_t user_bank_size;
198 };
199 
200 static bool stm32x_is_otp(struct flash_bank *bank)
201 {
202  return bank->base == STM32F2_OTP_BANK_BASE ||
203  bank->base == STM32F7_OTP_BANK_BASE;
204 }
205 
206 static bool stm32x_otp_is_f7(struct flash_bank *bank)
207 {
208  return bank->base == STM32F7_OTP_BANK_BASE;
209 }
210 
212 {
213  struct stm32x_flash_bank *stm32x_info = bank->driver_priv;
214 
215  return stm32x_info->otp_unlocked;
216 }
217 
218 static int stm32x_otp_disable(struct flash_bank *bank)
219 {
220  struct stm32x_flash_bank *stm32x_info = bank->driver_priv;
221 
222  LOG_INFO("OTP memory bank #%u is disabled for write commands.",
223  bank->bank_number);
224  stm32x_info->otp_unlocked = false;
225  return ERROR_OK;
226 }
227 
228 static int stm32x_otp_enable(struct flash_bank *bank)
229 {
230  struct stm32x_flash_bank *stm32x_info = bank->driver_priv;
231 
232  if (!stm32x_info->otp_unlocked) {
233  LOG_INFO("OTP memory bank #%u is enabled for write commands.",
234  bank->bank_number);
235  stm32x_info->otp_unlocked = true;
236  } else {
237  LOG_WARNING("OTP memory bank #%u is already enabled for write commands.",
238  bank->bank_number);
239  }
240  return ERROR_OK;
241 }
242 
243 /* flash bank stm32x <base> <size> 0 0 <target#>
244  */
245 FLASH_BANK_COMMAND_HANDLER(stm32x_flash_bank_command)
246 {
247  struct stm32x_flash_bank *stm32x_info;
248 
249  if (CMD_ARGC < 6)
251 
252  stm32x_info = malloc(sizeof(struct stm32x_flash_bank));
253  if (!stm32x_info) {
254  LOG_ERROR("Out of memory");
255  return ERROR_FAIL;
256  }
257  bank->driver_priv = stm32x_info;
258 
259  stm32x_info->probed = false;
260  stm32x_info->otp_unlocked = false;
261  stm32x_info->user_bank_size = bank->size;
262 
263  return ERROR_OK;
264 }
265 
266 static inline int stm32x_get_flash_reg(struct flash_bank *bank, uint32_t reg)
267 {
268  return reg;
269 }
270 
271 static inline int stm32x_get_flash_status(struct flash_bank *bank, uint32_t *status)
272 {
273  struct target *target = bank->target;
275 }
276 
278 {
279  struct target *target = bank->target;
280  uint32_t status;
281  int retval = ERROR_OK;
282 
283  /* wait for busy to clear */
284  for (;;) {
285  retval = stm32x_get_flash_status(bank, &status);
286  if (retval != ERROR_OK)
287  return retval;
288  LOG_DEBUG("status: 0x%" PRIx32, status);
289  if ((status & FLASH_BSY) == 0)
290  break;
291  if (timeout-- <= 0) {
292  LOG_ERROR("timed out waiting for flash");
293  return ERROR_FAIL;
294  }
295  alive_sleep(1);
296  }
297 
298 
299  if (status & FLASH_WRPERR) {
300  LOG_ERROR("stm32x device protected");
301  retval = ERROR_FAIL;
302  }
303 
304  /* Clear but report errors */
305  if (status & FLASH_ERROR) {
306  if (retval == ERROR_OK)
307  retval = ERROR_FAIL;
308  /* If this operation fails, we ignore it and report the original
309  * retval
310  */
312  status & FLASH_ERROR);
313  }
314  return retval;
315 }
316 
317 static int stm32x_unlock_reg(struct target *target)
318 {
319  uint32_t ctrl;
320 
321  /* first check if not already unlocked
322  * otherwise writing on STM32_FLASH_KEYR will fail
323  */
324  int retval = target_read_u32(target, STM32_FLASH_CR, &ctrl);
325  if (retval != ERROR_OK)
326  return retval;
327 
328  if ((ctrl & FLASH_LOCK) == 0)
329  return ERROR_OK;
330 
331  /* unlock flash registers */
333  if (retval != ERROR_OK)
334  return retval;
335 
337  if (retval != ERROR_OK)
338  return retval;
339 
341  if (retval != ERROR_OK)
342  return retval;
343 
344  if (ctrl & FLASH_LOCK) {
345  LOG_ERROR("flash not unlocked STM32_FLASH_CR: 0x%" PRIx32, ctrl);
346  return ERROR_TARGET_FAILURE;
347  }
348 
349  return ERROR_OK;
350 }
351 
353 {
354  uint32_t ctrl;
355 
356  int retval = target_read_u32(target, STM32_FLASH_OPTCR, &ctrl);
357  if (retval != ERROR_OK)
358  return retval;
359 
360  if ((ctrl & OPTCR_LOCK) == 0)
361  return ERROR_OK;
362 
363  /* unlock option registers */
365  if (retval != ERROR_OK)
366  return retval;
367 
369  if (retval != ERROR_OK)
370  return retval;
371 
373  if (retval != ERROR_OK)
374  return retval;
375 
376  if (ctrl & OPTCR_LOCK) {
377  LOG_ERROR("options not unlocked STM32_FLASH_OPTCR: 0x%" PRIx32, ctrl);
378  return ERROR_TARGET_FAILURE;
379  }
380 
381  return ERROR_OK;
382 }
383 
385 {
386  uint32_t optiondata;
387  struct stm32x_flash_bank *stm32x_info = NULL;
388  struct target *target = bank->target;
389 
390  stm32x_info = bank->driver_priv;
391 
392  /* read current option bytes */
393  int retval = target_read_u32(target, STM32_FLASH_OPTCR, &optiondata);
394  if (retval != ERROR_OK)
395  return retval;
396 
397  /* caution: F2 implements 5 bits (WDG_SW only)
398  * whereas F7 6 bits (IWDG_SW and WWDG_SW) in user_options */
399  stm32x_info->option_bytes.user_options = optiondata & 0xfc;
400  stm32x_info->option_bytes.RDP = (optiondata >> 8) & 0xff;
401  stm32x_info->option_bytes.protection =
402  (optiondata >> 16) & (~(0xffff << stm32x_info->protection_bits) & 0xffff);
403 
404  if (stm32x_info->has_extra_options) {
405  /* F42x/43x/469/479 and 7xx have up to 4 bits of extra options */
406  stm32x_info->option_bytes.user_options |= (optiondata >> 20) &
407  ((0xf00 << (stm32x_info->protection_bits - 12)) & 0xf00);
408  }
409 
410  if (stm32x_info->has_large_mem || stm32x_info->has_boot_addr) {
411  retval = target_read_u32(target, STM32_FLASH_OPTCR1, &optiondata);
412  if (retval != ERROR_OK)
413  return retval;
414 
415  /* FLASH_OPTCR1 has quite different meanings ... */
416  if (stm32x_info->has_boot_addr) {
417  /* for F7xx it contains boot0 and boot1 */
418  stm32x_info->option_bytes.boot_addr = optiondata;
419  } else {
420  /* for F42x/43x/469/479 it contains 12 additional protection bits */
421  stm32x_info->option_bytes.protection |= (optiondata >> 4) & 0x00fff000;
422  }
423  }
424 
425  if (stm32x_info->has_optcr2_pcrop) {
426  retval = target_read_u32(target, STM32_FLASH_OPTCR2, &optiondata);
427  if (retval != ERROR_OK)
428  return retval;
429 
430  stm32x_info->option_bytes.optcr2_pcrop = optiondata;
431  if (stm32x_info->has_optcr2_pcrop &&
432  (stm32x_info->option_bytes.optcr2_pcrop & ~OPTCR2_PCROP_RDP)) {
433  LOG_INFO("PCROP Engaged");
434  }
435  } else {
436  stm32x_info->option_bytes.optcr2_pcrop = 0x0;
437  }
438 
439  if (stm32x_info->option_bytes.RDP != 0xAA)
440  LOG_INFO("Device Security Bit Set");
441 
442  return ERROR_OK;
443 }
444 
446 {
447  struct stm32x_flash_bank *stm32x_info = NULL;
448  struct target *target = bank->target;
449  uint32_t optiondata, optiondata2;
450 
451  stm32x_info = bank->driver_priv;
452 
453  int retval = stm32x_unlock_option_reg(target);
454  if (retval != ERROR_OK)
455  return retval;
456 
457  /* rebuild option data */
458  optiondata = stm32x_info->option_bytes.user_options & 0xfc;
459  optiondata |= stm32x_info->option_bytes.RDP << 8;
460  optiondata |= (stm32x_info->option_bytes.protection &
461  (~(0xffff << stm32x_info->protection_bits))) << 16;
462 
463  if (stm32x_info->has_extra_options) {
464  /* F42x/43x/469/479 and 7xx have up to 4 bits of extra options */
465  optiondata |= (stm32x_info->option_bytes.user_options &
466  ((0xf00 << (stm32x_info->protection_bits - 12)) & 0xf00)) << 20;
467  }
468 
469  if (stm32x_info->has_large_mem || stm32x_info->has_boot_addr) {
470  if (stm32x_info->has_boot_addr) {
471  /* F7xx uses FLASH_OPTCR1 for boot0 and boot1 ... */
472  optiondata2 = stm32x_info->option_bytes.boot_addr;
473  } else {
474  /* F42x/43x/469/479 uses FLASH_OPTCR1 for additional protection bits */
475  optiondata2 = (stm32x_info->option_bytes.protection & 0x00fff000) << 4;
476  }
477 
478  retval = target_write_u32(target, STM32_FLASH_OPTCR1, optiondata2);
479  if (retval != ERROR_OK)
480  return retval;
481  }
482 
483  /* program extra pcrop register */
484  if (stm32x_info->has_optcr2_pcrop) {
486  stm32x_info->option_bytes.optcr2_pcrop);
487  if (retval != ERROR_OK)
488  return retval;
489  }
490 
491  /* program options */
492  retval = target_write_u32(target, STM32_FLASH_OPTCR, optiondata);
493  if (retval != ERROR_OK)
494  return retval;
495 
496  /* start programming cycle */
497  retval = target_write_u32(target, STM32_FLASH_OPTCR, optiondata | OPTCR_START);
498  if (retval != ERROR_OK)
499  return retval;
500 
501  /* wait for completion, this might trigger a security erase and take a while */
503  if (retval != ERROR_OK)
504  return retval;
505 
506  /* relock registers */
507  retval = target_write_u32(target, STM32_FLASH_OPTCR, optiondata | OPTCR_LOCK);
508  if (retval != ERROR_OK)
509  return retval;
510 
511  return ERROR_OK;
512 }
513 
515 {
516  struct target *target = bank->target;
517  uint32_t lock_base;
518  int retval;
519  uint8_t lock;
520 
523 
524  for (unsigned int i = 0; i < bank->num_sectors; i++) {
525  retval = target_read_u8(target, lock_base + i, &lock);
526  if (retval != ERROR_OK)
527  return retval;
528  bank->sectors[i].is_protected = !lock;
529  }
530 
531  return ERROR_OK;
532 }
533 
534 static int stm32x_otp_protect(struct flash_bank *bank, unsigned int first,
535  unsigned int last)
536 {
537  struct target *target = bank->target;
538  uint32_t lock_base;
539  int i, retval;
540  uint8_t lock;
541 
542  assert((first <= last) && (last < bank->num_sectors));
543 
546 
547  for (i = first; first <= last; i++) {
548  retval = target_read_u8(target, lock_base + i, &lock);
549  if (retval != ERROR_OK)
550  return retval;
551  if (lock)
552  continue;
553 
554  lock = 0xff;
555  retval = target_write_u8(target, lock_base + i, lock);
556  if (retval != ERROR_OK)
557  return retval;
558  }
559 
560  return ERROR_OK;
561 }
562 
564 {
565  struct stm32x_flash_bank *stm32x_info = bank->driver_priv;
566  struct flash_sector *prot_blocks;
567  unsigned int num_prot_blocks;
568  int retval;
569 
570  /* if it's the OTP bank, look at the lock bits there */
571  if (stm32x_is_otp(bank))
573 
574  /* read write protection settings */
575  retval = stm32x_read_options(bank);
576  if (retval != ERROR_OK) {
577  LOG_DEBUG("unable to read option bytes");
578  return retval;
579  }
580 
581  if (bank->prot_blocks) {
582  num_prot_blocks = bank->num_prot_blocks;
583  prot_blocks = bank->prot_blocks;
584  } else {
585  num_prot_blocks = bank->num_sectors;
586  prot_blocks = bank->sectors;
587  }
588 
589  for (unsigned int i = 0; i < num_prot_blocks; i++)
590  prot_blocks[i].is_protected =
591  ~(stm32x_info->option_bytes.protection >> i) & 1;
592 
593  return ERROR_OK;
594 }
595 
596 static int stm32x_erase(struct flash_bank *bank, unsigned int first,
597  unsigned int last)
598 {
599  struct stm32x_flash_bank *stm32x_info = bank->driver_priv;
600  struct target *target = bank->target;
601 
602  if (stm32x_is_otp(bank)) {
603  LOG_ERROR("Cannot erase OTP memory");
604  return ERROR_FAIL;
605  }
606 
607  assert((first <= last) && (last < bank->num_sectors));
608 
609  if (bank->target->state != TARGET_HALTED) {
610  LOG_ERROR("Target not halted");
612  }
613 
614  int retval;
615  retval = stm32x_unlock_reg(target);
616  if (retval != ERROR_OK)
617  return retval;
618 
619  /*
620  Sector Erase
621  To erase a sector, follow the procedure below:
622  1. Check that no Flash memory operation is ongoing by checking the BSY bit in the
623  FLASH_SR register
624  2. Set the SER bit and select the sector
625  you wish to erase (SNB) in the FLASH_CR register
626  3. Set the STRT bit in the FLASH_CR register
627  4. Wait for the BSY bit to be cleared
628  */
629 
630  for (unsigned int i = first; i <= last; i++) {
631  unsigned int snb;
632  if (stm32x_info->has_large_mem && i >= (bank->num_sectors / 2))
633  snb = (i - (bank->num_sectors / 2)) | 0x10;
634  else
635  snb = i;
636 
637  retval = target_write_u32(target,
639  if (retval != ERROR_OK)
640  return retval;
641 
643  if (retval != ERROR_OK)
644  return retval;
645  }
646 
648  if (retval != ERROR_OK)
649  return retval;
650 
651  return ERROR_OK;
652 }
653 
654 static int stm32x_protect(struct flash_bank *bank, int set, unsigned int first,
655  unsigned int last)
656 {
657  struct target *target = bank->target;
658  struct stm32x_flash_bank *stm32x_info = bank->driver_priv;
659 
660  if (target->state != TARGET_HALTED) {
661  LOG_ERROR("Target not halted");
663  }
664 
665  if (stm32x_is_otp(bank)) {
666  if (!set) {
667  LOG_ERROR("OTP protection can only be enabled");
669  }
670 
671  return stm32x_otp_protect(bank, first, last);
672  }
673 
674  /* read protection settings */
675  int retval = stm32x_read_options(bank);
676  if (retval != ERROR_OK) {
677  LOG_DEBUG("unable to read option bytes");
678  return retval;
679  }
680 
681  for (unsigned int i = first; i <= last; i++) {
682  if (set)
683  stm32x_info->option_bytes.protection &= ~(1 << i);
684  else
685  stm32x_info->option_bytes.protection |= (1 << i);
686  }
687 
688  retval = stm32x_write_options(bank);
689  if (retval != ERROR_OK)
690  return retval;
691 
692  return ERROR_OK;
693 }
694 
695 static int stm32x_write_block(struct flash_bank *bank, const uint8_t *buffer,
696  uint32_t offset, uint32_t count)
697 {
698  struct target *target = bank->target;
699  uint32_t buffer_size = 16384;
700  struct working_area *write_algorithm;
701  struct working_area *source;
702  uint32_t address = bank->base + offset;
703  struct reg_param reg_params[5];
704  struct armv7m_algorithm armv7m_info;
705  int retval = ERROR_OK;
706 
707  static const uint8_t stm32x_flash_write_code[] = {
708 #include "../../../contrib/loaders/flash/stm32/stm32f2x.inc"
709  };
710 
712  LOG_ERROR("OTP memory bank is disabled for write commands.");
713  return ERROR_FAIL;
714  }
715 
716  if (target_alloc_working_area(target, sizeof(stm32x_flash_write_code),
717  &write_algorithm) != ERROR_OK) {
718  LOG_WARNING("no working area available, can't do block memory writes");
720  }
721 
722  retval = target_write_buffer(target, write_algorithm->address,
723  sizeof(stm32x_flash_write_code),
724  stm32x_flash_write_code);
725  if (retval != ERROR_OK) {
726  target_free_working_area(target, write_algorithm);
727  return retval;
728  }
729 
730  /* memory buffer */
732  buffer_size /= 2;
733  if (buffer_size <= 256) {
734  /* we already allocated the writing code, but failed to get a
735  * buffer, free the algorithm */
736  target_free_working_area(target, write_algorithm);
737 
738  LOG_WARNING("no large enough working area available, can't do block memory writes");
740  }
741  }
742 
743  armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
744  armv7m_info.core_mode = ARM_MODE_THREAD;
745 
746  init_reg_param(&reg_params[0], "r0", 32, PARAM_IN_OUT); /* buffer start, status (out) */
747  init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT); /* buffer end */
748  init_reg_param(&reg_params[2], "r2", 32, PARAM_OUT); /* target address */
749  init_reg_param(&reg_params[3], "r3", 32, PARAM_OUT); /* count (halfword-16bit) */
750  init_reg_param(&reg_params[4], "r4", 32, PARAM_OUT); /* flash base */
751 
752  buf_set_u32(reg_params[0].value, 0, 32, source->address);
753  buf_set_u32(reg_params[1].value, 0, 32, source->address + source->size);
754  buf_set_u32(reg_params[2].value, 0, 32, address);
755  buf_set_u32(reg_params[3].value, 0, 32, count);
756  buf_set_u32(reg_params[4].value, 0, 32, STM32_FLASH_BASE);
757 
759  0, NULL,
760  5, reg_params,
761  source->address, source->size,
762  write_algorithm->address, 0,
763  &armv7m_info);
764 
765  if (retval == ERROR_FLASH_OPERATION_FAILED) {
766  LOG_ERROR("error executing stm32x flash write algorithm");
767 
768  uint32_t error = buf_get_u32(reg_params[0].value, 0, 32) & FLASH_ERROR;
769 
770  if (error & FLASH_WRPERR)
771  LOG_ERROR("flash memory write protected");
772 
773  if (error != 0) {
774  LOG_ERROR("flash write failed = 0x%08" PRIx32, error);
775  /* Clear but report errors */
777  retval = ERROR_FAIL;
778  }
779  }
780 
782  target_free_working_area(target, write_algorithm);
783 
784  destroy_reg_param(&reg_params[0]);
785  destroy_reg_param(&reg_params[1]);
786  destroy_reg_param(&reg_params[2]);
787  destroy_reg_param(&reg_params[3]);
788  destroy_reg_param(&reg_params[4]);
789 
790  return retval;
791 }
792 
793 static int stm32x_write(struct flash_bank *bank, const uint8_t *buffer,
794  uint32_t offset, uint32_t count)
795 {
796  struct target *target = bank->target;
797  uint32_t words_remaining = (count / 2);
798  uint32_t bytes_remaining = (count & 0x00000001);
799  uint32_t address = bank->base + offset;
800  uint32_t bytes_written = 0;
801  int retval;
802 
803  if (bank->target->state != TARGET_HALTED) {
804  LOG_ERROR("Target not halted");
806  }
807 
808  if (offset & 0x1) {
809  LOG_WARNING("offset 0x%" PRIx32 " breaks required 2-byte alignment", offset);
811  }
812 
813  retval = stm32x_unlock_reg(target);
814  if (retval != ERROR_OK)
815  return retval;
816 
817  /* multiple half words (2-byte) to be programmed? */
818  if (words_remaining > 0) {
819  /* try using a block write */
820  retval = stm32x_write_block(bank, buffer, offset, words_remaining);
821  if (retval != ERROR_OK) {
822  if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE) {
823  /* if block write failed (no sufficient working area),
824  * we use normal (slow) single dword accesses */
825  LOG_WARNING("couldn't use block writes, falling back to single memory accesses");
826  }
827  } else {
828  buffer += words_remaining * 2;
829  address += words_remaining * 2;
830  words_remaining = 0;
831  }
832  }
833 
834  if ((retval != ERROR_OK) && (retval != ERROR_TARGET_RESOURCE_NOT_AVAILABLE))
835  return retval;
836 
837  /*
838  Standard programming
839  The Flash memory programming sequence is as follows:
840  1. Check that no main Flash memory operation is ongoing by checking the BSY bit in the
841  FLASH_SR register.
842  2. Set the PG bit in the FLASH_CR register
843  3. Perform the data write operation(s) to the desired memory address (inside main
844  memory block or OTP area):
845  – – Half-word access in case of x16 parallelism
846  – Word access in case of x32 parallelism
847  –
848  4.
849  Byte access in case of x8 parallelism
850  Double word access in case of x64 parallelism
851  Wait for the BSY bit to be cleared
852  */
853  while (words_remaining > 0) {
856  if (retval != ERROR_OK)
857  return retval;
858 
859  retval = target_write_memory(target, address, 2, 1, buffer + bytes_written);
860  if (retval != ERROR_OK)
861  return retval;
862 
864  if (retval != ERROR_OK)
865  return retval;
866 
867  bytes_written += 2;
868  words_remaining--;
869  address += 2;
870  }
871 
872  if (bytes_remaining) {
875  if (retval != ERROR_OK)
876  return retval;
877  retval = target_write_u8(target, address, buffer[bytes_written]);
878  if (retval != ERROR_OK)
879  return retval;
880 
882  if (retval != ERROR_OK)
883  return retval;
884  }
885 
887 }
888 
889 static void setup_sector(struct flash_bank *bank, unsigned int i,
890  unsigned int size)
891 {
892  assert(i < bank->num_sectors);
893  bank->sectors[i].offset = bank->size;
894  bank->sectors[i].size = size;
895  bank->size += bank->sectors[i].size;
896  LOG_DEBUG("sector %u: %ukBytes", i, size >> 10);
897 }
898 
899 static uint16_t sector_size_in_kb(unsigned int i, uint16_t max_sector_size_in_kb)
900 {
901  if (i < 4)
902  return max_sector_size_in_kb / 8;
903  if (i == 4)
904  return max_sector_size_in_kb / 2;
905  return max_sector_size_in_kb;
906 }
907 
908 static unsigned int calculate_number_of_sectors(struct flash_bank *bank,
909  uint16_t flash_size_in_kb,
910  uint16_t max_sector_size_in_kb)
911 {
912  struct stm32x_flash_bank *stm32x_info = bank->driver_priv;
913  uint16_t remaining_flash_size_in_kb = flash_size_in_kb;
914  unsigned int nr_sectors;
915 
916  /* Dual Bank Flash has two identically-arranged banks of sectors. */
917  if (stm32x_info->has_large_mem)
918  remaining_flash_size_in_kb /= 2;
919 
920  for (nr_sectors = 0; remaining_flash_size_in_kb > 0; nr_sectors++) {
921  uint16_t size_in_kb = sector_size_in_kb(nr_sectors, max_sector_size_in_kb);
922  if (size_in_kb > remaining_flash_size_in_kb) {
923  LOG_INFO("%s Bank %" PRIu16 " kiB final sector clipped to %" PRIu16 " kiB",
924  stm32x_info->has_large_mem ? "Dual" : "Single",
925  flash_size_in_kb, remaining_flash_size_in_kb);
926  remaining_flash_size_in_kb = 0;
927  } else {
928  remaining_flash_size_in_kb -= size_in_kb;
929  }
930  }
931 
932  return stm32x_info->has_large_mem ? nr_sectors*2 : nr_sectors;
933 }
934 
935 static void setup_bank(struct flash_bank *bank, unsigned int start,
936  uint16_t flash_size_in_kb, uint16_t max_sector_size_in_kb)
937 {
938  uint16_t remaining_flash_size_in_kb = flash_size_in_kb;
939  unsigned int sector_index = 0;
940  while (remaining_flash_size_in_kb > 0) {
941  uint16_t size_in_kb = sector_size_in_kb(sector_index, max_sector_size_in_kb);
942  if (size_in_kb > remaining_flash_size_in_kb) {
943  /* Clip last sector. Already warned in
944  * calculate_number_of_sectors. */
945  size_in_kb = remaining_flash_size_in_kb;
946  }
947  setup_sector(bank, start + sector_index, size_in_kb * 1024);
948  remaining_flash_size_in_kb -= size_in_kb;
949  sector_index++;
950  }
951 }
952 
953 static int stm32x_get_device_id(struct flash_bank *bank, uint32_t *device_id)
954 {
955  /* this checks for a stm32f4x errata issue where a
956  * stm32f2x DBGMCU_IDCODE is incorrectly returned.
957  * If the issue is detected target is forced to stm32f4x Rev A.
958  * Only effects Rev A silicon */
959 
960  struct target *target = bank->target;
961 
962  /* read stm32 device id register */
963  int retval = target_read_u32(target, 0xE0042000, device_id);
964  if (retval != ERROR_OK)
965  return retval;
966 
967  if ((*device_id & 0xfff) == 0x411
969  *device_id &= ~((0xFFFF << 16) | 0xfff);
970  *device_id |= (0x1000 << 16) | 0x413;
971  LOG_INFO("stm32f4x errata detected - fixing incorrect MCU_IDCODE");
972  }
973  return retval;
974 }
975 
976 static int stm32x_probe(struct flash_bank *bank)
977 {
978  struct target *target = bank->target;
979  struct stm32x_flash_bank *stm32x_info = bank->driver_priv;
980  unsigned int num_prot_blocks, num_sectors;
981  uint16_t flash_size_in_kb;
982  uint16_t otp_size_in_b;
983  uint16_t otp_sector_size;
984  uint32_t flash_size_reg = 0x1FFF7A22;
985  uint16_t max_sector_size_in_kb = 128;
986  uint16_t max_flash_size_in_kb;
987  uint32_t device_id;
988  uint32_t base_address = 0x08000000;
989 
990  stm32x_info->probed = false;
991  stm32x_info->has_large_mem = false;
992  stm32x_info->has_boot_addr = false;
993  stm32x_info->has_extra_options = false;
994  stm32x_info->has_optcr2_pcrop = false;
995  stm32x_info->protection_bits = 12; /* max. number of nWRPi bits (in FLASH_OPTCR !!!) */
996  num_prot_blocks = 0;
997 
998  free(bank->sectors);
999  bank->num_sectors = 0;
1000  bank->sectors = NULL;
1001 
1002  free(bank->prot_blocks);
1003  bank->num_prot_blocks = 0;
1004  bank->prot_blocks = NULL;
1005 
1006  if (!target_was_examined(target)) {
1007  LOG_ERROR("Target not examined yet");
1009  }
1010 
1011  /* if explicitly called out as OTP bank, short circuit probe */
1012  if (stm32x_is_otp(bank)) {
1013  if (stm32x_otp_is_f7(bank)) {
1014  otp_size_in_b = STM32F7_OTP_SIZE;
1015  otp_sector_size = STM32F7_OTP_SECTOR_SIZE;
1016  } else {
1017  otp_size_in_b = STM32F2_OTP_SIZE;
1018  otp_sector_size = STM32F2_OTP_SECTOR_SIZE;
1019  }
1020 
1021  num_sectors = otp_size_in_b / otp_sector_size;
1022  LOG_INFO("flash size = %" PRIu16 " bytes", otp_size_in_b);
1023 
1024  assert(num_sectors > 0);
1025 
1026  bank->num_sectors = num_sectors;
1027  bank->sectors = calloc(num_sectors, sizeof(struct flash_sector));
1028 
1029  if (stm32x_otp_is_f7(bank))
1030  bank->size = STM32F7_OTP_SIZE;
1031  else
1032  bank->size = STM32F2_OTP_SIZE;
1033 
1034  for (unsigned int i = 0; i < num_sectors; i++) {
1035  bank->sectors[i].offset = i * otp_sector_size;
1036  bank->sectors[i].size = otp_sector_size;
1037  bank->sectors[i].is_erased = 1;
1038  bank->sectors[i].is_protected = 0;
1039  }
1040 
1041  stm32x_info->probed = true;
1042  return ERROR_OK;
1043  }
1044 
1045  /* read stm32 device id register */
1046  int retval = stm32x_get_device_id(bank, &device_id);
1047  if (retval != ERROR_OK)
1048  return retval;
1049  LOG_INFO("device id = 0x%08" PRIx32, device_id);
1050  device_id &= 0xfff; /* only bits 0-11 are used further on */
1051 
1052  /* set max flash size depending on family, id taken from AN2606 */
1053  switch (device_id) {
1054  case 0x411: /* F20x/21x */
1055  case 0x413: /* F40x/41x */
1056  max_flash_size_in_kb = 1024;
1057  break;
1058 
1059  case 0x419: /* F42x/43x */
1060  case 0x434: /* F469/479 */
1061  stm32x_info->has_extra_options = true;
1062  max_flash_size_in_kb = 2048;
1063  break;
1064 
1065  case 0x423: /* F401xB/C */
1066  max_flash_size_in_kb = 256;
1067  break;
1068 
1069  case 0x421: /* F446 */
1070  case 0x431: /* F411 */
1071  case 0x433: /* F401xD/E */
1072  case 0x441: /* F412 */
1073  max_flash_size_in_kb = 512;
1074  break;
1075 
1076  case 0x458: /* F410 */
1077  max_flash_size_in_kb = 128;
1078  break;
1079 
1080  case 0x449: /* F74x/75x */
1081  max_flash_size_in_kb = 1024;
1082  max_sector_size_in_kb = 256;
1083  flash_size_reg = 0x1FF0F442;
1084  stm32x_info->has_extra_options = true;
1085  stm32x_info->has_boot_addr = true;
1086  break;
1087 
1088  case 0x451: /* F76x/77x */
1089  max_flash_size_in_kb = 2048;
1090  max_sector_size_in_kb = 256;
1091  flash_size_reg = 0x1FF0F442;
1092  stm32x_info->has_extra_options = true;
1093  stm32x_info->has_boot_addr = true;
1094  break;
1095 
1096  case 0x452: /* F72x/73x */
1097  max_flash_size_in_kb = 512;
1098  flash_size_reg = 0x1FF07A22; /* yes, 0x1FF*0*7A22, not 0x1FF*F*7A22 */
1099  stm32x_info->has_extra_options = true;
1100  stm32x_info->has_boot_addr = true;
1101  stm32x_info->has_optcr2_pcrop = true;
1102  break;
1103 
1104  case 0x463: /* F413x/423x */
1105  max_flash_size_in_kb = 1536;
1106  stm32x_info->has_extra_options = true;
1107  stm32x_info->protection_bits = 15;
1108  num_prot_blocks = 15;
1109  break;
1110 
1111  default:
1112  LOG_WARNING("Cannot identify target as a STM32 family.");
1113  return ERROR_FAIL;
1114  }
1115 
1116  /* get flash size from target. */
1117  retval = target_read_u16(target, flash_size_reg, &flash_size_in_kb);
1118 
1119  /* failed reading flash size or flash size invalid (early silicon),
1120  * default to max target family */
1121  if (retval != ERROR_OK || flash_size_in_kb == 0xffff || flash_size_in_kb == 0) {
1122  LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming %" PRIu16 "k flash",
1123  max_flash_size_in_kb);
1124  flash_size_in_kb = max_flash_size_in_kb;
1125  }
1126 
1127  /* if the user sets the size manually then ignore the probed value
1128  * this allows us to work around devices that have a invalid flash size register value */
1129  if (stm32x_info->user_bank_size) {
1130  LOG_INFO("ignoring flash probed value, using configured bank size");
1131  flash_size_in_kb = stm32x_info->user_bank_size / 1024;
1132  }
1133 
1134  LOG_INFO("flash size = %" PRIu16 " KiB", flash_size_in_kb);
1135 
1136  /* did we assign flash size? */
1137  assert(flash_size_in_kb != 0xffff);
1138 
1139  /* F42x/43x/469/479 1024 kiByte devices have a dual bank option */
1140  if ((device_id == 0x419) || (device_id == 0x434)) {
1141  uint32_t optiondata;
1142  retval = target_read_u32(target, STM32_FLASH_OPTCR, &optiondata);
1143  if (retval != ERROR_OK) {
1144  LOG_DEBUG("unable to read option bytes");
1145  return retval;
1146  }
1147  if ((flash_size_in_kb > 1024) || (optiondata & OPTCR_DB1M)) {
1148  stm32x_info->has_large_mem = true;
1149  LOG_INFO("Dual Bank %" PRIu16 " kiB STM32F42x/43x/469/479 found", flash_size_in_kb);
1150  } else {
1151  stm32x_info->has_large_mem = false;
1152  LOG_INFO("Single Bank %" PRIu16 " kiB STM32F42x/43x/469/479 found", flash_size_in_kb);
1153  }
1154  }
1155 
1156  /* F76x/77x devices have a dual bank option */
1157  if (device_id == 0x451) {
1158  uint32_t optiondata;
1159  retval = target_read_u32(target, STM32_FLASH_OPTCR, &optiondata);
1160  if (retval != ERROR_OK) {
1161  LOG_DEBUG("unable to read option bytes");
1162  return retval;
1163  }
1164  if (optiondata & OPTCR_NDBANK) {
1165  stm32x_info->has_large_mem = false;
1166  LOG_INFO("Single Bank %" PRIu16 " kiB STM32F76x/77x found", flash_size_in_kb);
1167  } else {
1168  stm32x_info->has_large_mem = true;
1169  max_sector_size_in_kb >>= 1; /* sector size divided by 2 in dual-bank mode */
1170  LOG_INFO("Dual Bank %" PRIu16 " kiB STM32F76x/77x found", flash_size_in_kb);
1171  }
1172  }
1173 
1174  /* calculate numbers of pages */
1175  unsigned int num_pages = calculate_number_of_sectors(
1176  bank, flash_size_in_kb, max_sector_size_in_kb);
1177 
1178  bank->base = base_address;
1179  bank->num_sectors = num_pages;
1180  bank->sectors = calloc(num_pages, sizeof(struct flash_sector));
1181  for (unsigned int i = 0; i < num_pages; i++) {
1182  bank->sectors[i].is_erased = -1;
1183  bank->sectors[i].is_protected = 0;
1184  }
1185  bank->size = 0;
1186  LOG_DEBUG("allocated %u sectors", num_pages);
1187 
1188  /* F76x/77x in dual bank mode */
1189  if ((device_id == 0x451) && stm32x_info->has_large_mem)
1190  num_prot_blocks = num_pages >> 1;
1191 
1192  if (num_prot_blocks) {
1193  bank->prot_blocks = malloc(sizeof(struct flash_sector) * num_prot_blocks);
1194  for (unsigned int i = 0; i < num_prot_blocks; i++)
1195  bank->prot_blocks[i].is_protected = 0;
1196  LOG_DEBUG("allocated %u prot blocks", num_prot_blocks);
1197  }
1198 
1199  if (stm32x_info->has_large_mem) {
1200  /* dual-bank */
1201  setup_bank(bank, 0, flash_size_in_kb >> 1, max_sector_size_in_kb);
1202  setup_bank(bank, num_pages >> 1, flash_size_in_kb >> 1,
1203  max_sector_size_in_kb);
1204 
1205  /* F767x/F77x in dual mode, one protection bit refers to two adjacent sectors */
1206  if (device_id == 0x451) {
1207  for (unsigned int i = 0; i < num_prot_blocks; i++) {
1208  bank->prot_blocks[i].offset = bank->sectors[i << 1].offset;
1209  bank->prot_blocks[i].size = bank->sectors[i << 1].size
1210  + bank->sectors[(i << 1) + 1].size;
1211  }
1212  }
1213  } else {
1214  /* single-bank */
1215  setup_bank(bank, 0, flash_size_in_kb, max_sector_size_in_kb);
1216 
1217  /* F413/F423, sectors 14 and 15 share one common protection bit */
1218  if (device_id == 0x463) {
1219  for (unsigned int i = 0; i < num_prot_blocks; i++) {
1220  bank->prot_blocks[i].offset = bank->sectors[i].offset;
1221  bank->prot_blocks[i].size = bank->sectors[i].size;
1222  }
1223  bank->prot_blocks[num_prot_blocks - 1].size <<= 1;
1224  }
1225  }
1226  bank->num_prot_blocks = num_prot_blocks;
1227  assert((bank->size >> 10) == flash_size_in_kb);
1228 
1229  stm32x_info->probed = true;
1230  return ERROR_OK;
1231 }
1232 
1233 static int stm32x_auto_probe(struct flash_bank *bank)
1234 {
1235  struct stm32x_flash_bank *stm32x_info = bank->driver_priv;
1236  if (stm32x_info->probed)
1237  return ERROR_OK;
1238  return stm32x_probe(bank);
1239 }
1240 
1242 {
1243  uint32_t dbgmcu_idcode;
1244 
1245  /* read stm32 device id register */
1246  int retval = stm32x_get_device_id(bank, &dbgmcu_idcode);
1247  if (retval != ERROR_OK)
1248  return retval;
1249 
1250  uint16_t device_id = dbgmcu_idcode & 0xfff;
1251  uint16_t rev_id = dbgmcu_idcode >> 16;
1252  const char *device_str;
1253  const char *rev_str = NULL;
1254 
1255  switch (device_id) {
1256  case 0x411:
1257  device_str = "STM32F2xx";
1258 
1259  switch (rev_id) {
1260  case 0x1000:
1261  rev_str = "A";
1262  break;
1263 
1264  case 0x2000:
1265  rev_str = "B";
1266  break;
1267 
1268  case 0x1001:
1269  rev_str = "Z";
1270  break;
1271 
1272  case 0x2001:
1273  rev_str = "Y";
1274  break;
1275 
1276  case 0x2003:
1277  rev_str = "X";
1278  break;
1279 
1280  case 0x2007:
1281  rev_str = "1";
1282  break;
1283 
1284  case 0x200F:
1285  rev_str = "V";
1286  break;
1287 
1288  case 0x201F:
1289  rev_str = "2";
1290  break;
1291  }
1292  break;
1293 
1294  case 0x413:
1295  case 0x419:
1296  case 0x434:
1297  device_str = "STM32F4xx";
1298 
1299  switch (rev_id) {
1300  case 0x1000:
1301  rev_str = "A";
1302  break;
1303 
1304  case 0x1001:
1305  rev_str = "Z";
1306  break;
1307 
1308  case 0x1003:
1309  rev_str = "Y";
1310  break;
1311 
1312  case 0x1007:
1313  rev_str = "1";
1314  break;
1315 
1316  case 0x2001:
1317  rev_str = "3";
1318  break;
1319  }
1320  break;
1321 
1322  case 0x421:
1323  device_str = "STM32F446";
1324 
1325  switch (rev_id) {
1326  case 0x1000:
1327  rev_str = "A";
1328  break;
1329  }
1330  break;
1331 
1332  case 0x423:
1333  case 0x431:
1334  case 0x433:
1335  case 0x458:
1336  case 0x441:
1337  device_str = "STM32F4xx (Low Power)";
1338 
1339  switch (rev_id) {
1340  case 0x1000:
1341  rev_str = "A";
1342  break;
1343 
1344  case 0x1001:
1345  rev_str = "Z";
1346  break;
1347 
1348  case 0x2000:
1349  rev_str = "B";
1350  break;
1351 
1352  case 0x3000:
1353  rev_str = "C";
1354  break;
1355  }
1356  break;
1357 
1358  case 0x449:
1359  device_str = "STM32F7[4|5]x";
1360 
1361  switch (rev_id) {
1362  case 0x1000:
1363  rev_str = "A";
1364  break;
1365 
1366  case 0x1001:
1367  rev_str = "Z";
1368  break;
1369  }
1370  break;
1371 
1372  case 0x451:
1373  device_str = "STM32F7[6|7]x";
1374 
1375  switch (rev_id) {
1376  case 0x1000:
1377  rev_str = "A";
1378  break;
1379  case 0x1001:
1380  rev_str = "Z";
1381  break;
1382  }
1383  break;
1384 
1385  case 0x452:
1386  device_str = "STM32F7[2|3]x";
1387 
1388  switch (rev_id) {
1389  case 0x1000:
1390  rev_str = "A";
1391  break;
1392  }
1393  break;
1394 
1395  case 0x463:
1396  device_str = "STM32F4[1|2]3";
1397 
1398  switch (rev_id) {
1399  case 0x1000:
1400  rev_str = "A";
1401  break;
1402  }
1403  break;
1404 
1405  default:
1406  command_print_sameline(cmd, "Cannot identify target as a STM32F2/4/7\n");
1407  return ERROR_FAIL;
1408  }
1409 
1410  if (rev_str)
1411  command_print_sameline(cmd, "%s - Rev: %s", device_str, rev_str);
1412  else
1413  command_print_sameline(cmd, "%s - Rev: unknown (0x%04" PRIx16 ")", device_str, rev_id);
1414 
1415  return ERROR_OK;
1416 }
1417 
1418 COMMAND_HANDLER(stm32x_handle_lock_command)
1419 {
1420  struct target *target = NULL;
1421  struct stm32x_flash_bank *stm32x_info = NULL;
1422 
1423  if (CMD_ARGC < 1)
1425 
1426  struct flash_bank *bank;
1427  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
1428  if (retval != ERROR_OK)
1429  return retval;
1430 
1431  stm32x_info = bank->driver_priv;
1432  target = bank->target;
1433 
1434  if (target->state != TARGET_HALTED) {
1435  LOG_INFO("Target not halted");
1436  /* return ERROR_TARGET_NOT_HALTED; */
1437  }
1438 
1439  if (stm32x_read_options(bank) != ERROR_OK) {
1440  command_print(CMD, "%s failed to read options", bank->driver->name);
1441  return ERROR_OK;
1442  }
1443 
1444  /* set readout protection */
1445  stm32x_info->option_bytes.RDP = 0;
1446 
1448  command_print(CMD, "%s failed to lock device", bank->driver->name);
1449  return ERROR_OK;
1450  }
1451 
1452  command_print(CMD, "%s locked", bank->driver->name);
1453 
1454  return ERROR_OK;
1455 }
1456 
1457 COMMAND_HANDLER(stm32x_handle_unlock_command)
1458 {
1459  struct target *target = NULL;
1460  struct stm32x_flash_bank *stm32x_info = NULL;
1461 
1462  if (CMD_ARGC < 1)
1464 
1465  struct flash_bank *bank;
1466  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
1467  if (retval != ERROR_OK)
1468  return retval;
1469 
1470  stm32x_info = bank->driver_priv;
1471  target = bank->target;
1472 
1473  if (target->state != TARGET_HALTED) {
1474  LOG_INFO("Target not halted");
1475  /* return ERROR_TARGET_NOT_HALTED; */
1476  }
1477 
1478  if (stm32x_read_options(bank) != ERROR_OK) {
1479  command_print(CMD, "%s failed to read options", bank->driver->name);
1480  return ERROR_OK;
1481  }
1482 
1483  /* clear readout protection and complementary option bytes
1484  * this will also force a device unlock if set */
1485  stm32x_info->option_bytes.RDP = 0xAA;
1486  if (stm32x_info->has_optcr2_pcrop) {
1487  stm32x_info->option_bytes.optcr2_pcrop = OPTCR2_PCROP_RDP | (~1U << bank->num_sectors);
1488  }
1489 
1491  command_print(CMD, "%s failed to unlock device", bank->driver->name);
1492  return ERROR_OK;
1493  }
1494 
1495  command_print(CMD, "%s unlocked.\n"
1496  "INFO: a reset or power cycle is required "
1497  "for the new settings to take effect.", bank->driver->name);
1498 
1499  return ERROR_OK;
1500 }
1501 
1502 static int stm32x_mass_erase(struct flash_bank *bank)
1503 {
1504  int retval;
1505  uint32_t flash_mer;
1506  struct target *target = bank->target;
1507  struct stm32x_flash_bank *stm32x_info = NULL;
1508 
1509  if (target->state != TARGET_HALTED) {
1510  LOG_ERROR("Target not halted");
1511  return ERROR_TARGET_NOT_HALTED;
1512  }
1513 
1514  stm32x_info = bank->driver_priv;
1515 
1516  retval = stm32x_unlock_reg(target);
1517  if (retval != ERROR_OK)
1518  return retval;
1519 
1520  /* mass erase flash memory */
1521  if (stm32x_info->has_large_mem)
1522  flash_mer = FLASH_MER | FLASH_MER1;
1523  else
1524  flash_mer = FLASH_MER;
1525 
1527  if (retval != ERROR_OK)
1528  return retval;
1530  flash_mer | FLASH_STRT);
1531  if (retval != ERROR_OK)
1532  return retval;
1533 
1535  if (retval != ERROR_OK)
1536  return retval;
1537 
1539  if (retval != ERROR_OK)
1540  return retval;
1541 
1542  return ERROR_OK;
1543 }
1544 
1545 COMMAND_HANDLER(stm32x_handle_mass_erase_command)
1546 {
1547  if (CMD_ARGC != 1)
1549 
1550  struct flash_bank *bank;
1551  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
1552  if (retval != ERROR_OK)
1553  return retval;
1554 
1555  retval = stm32x_mass_erase(bank);
1556  if (retval == ERROR_OK) {
1557  command_print(CMD, "stm32x mass erase complete");
1558  } else {
1559  command_print(CMD, "stm32x mass erase failed");
1560  }
1561 
1562  return retval;
1563 }
1564 
1565 COMMAND_HANDLER(stm32f2x_handle_options_read_command)
1566 {
1567  int retval;
1568  struct flash_bank *bank;
1569  struct stm32x_flash_bank *stm32x_info = NULL;
1570 
1571  if (CMD_ARGC != 1)
1573 
1574  retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
1575  if (retval != ERROR_OK)
1576  return retval;
1577 
1578  retval = stm32x_read_options(bank);
1579  if (retval != ERROR_OK)
1580  return retval;
1581 
1582  stm32x_info = bank->driver_priv;
1583  if (stm32x_info->has_extra_options) {
1584  if (stm32x_info->has_boot_addr) {
1585  uint32_t boot_addr = stm32x_info->option_bytes.boot_addr;
1586 
1587  command_print(CMD, "stm32f2x user_options 0x%03" PRIX16 ","
1588  " boot_add0 0x%04" PRIX32 ", boot_add1 0x%04" PRIX32,
1589  stm32x_info->option_bytes.user_options,
1590  boot_addr & 0xffff, (boot_addr & 0xffff0000) >> 16);
1591  if (stm32x_info->has_optcr2_pcrop) {
1592  command_print(CMD, "stm32f2x optcr2_pcrop 0x%08" PRIX32,
1593  stm32x_info->option_bytes.optcr2_pcrop);
1594  }
1595  } else {
1596  command_print(CMD, "stm32f2x user_options 0x%03" PRIX16,
1597  stm32x_info->option_bytes.user_options);
1598  }
1599  } else {
1600  command_print(CMD, "stm32f2x user_options 0x%02" PRIX16,
1601  stm32x_info->option_bytes.user_options);
1602 
1603  }
1604 
1605  return retval;
1606 }
1607 
1608 COMMAND_HANDLER(stm32f2x_handle_options_write_command)
1609 {
1610  int retval;
1611  struct flash_bank *bank;
1612  struct stm32x_flash_bank *stm32x_info = NULL;
1613  uint16_t user_options, boot_addr0, boot_addr1, options_mask;
1614 
1615  if (CMD_ARGC < 1)
1617 
1618  retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
1619  if (retval != ERROR_OK)
1620  return retval;
1621 
1622  retval = stm32x_read_options(bank);
1623  if (retval != ERROR_OK)
1624  return retval;
1625 
1626  stm32x_info = bank->driver_priv;
1627  if (stm32x_info->has_boot_addr) {
1628  if (CMD_ARGC != 4)
1630 
1631  COMMAND_PARSE_NUMBER(u16, CMD_ARGV[2], boot_addr0);
1632  COMMAND_PARSE_NUMBER(u16, CMD_ARGV[3], boot_addr1);
1633  stm32x_info->option_bytes.boot_addr = boot_addr0 | (((uint32_t) boot_addr1) << 16);
1634  } else if (CMD_ARGC != 2) {
1636  }
1637 
1638  COMMAND_PARSE_NUMBER(u16, CMD_ARGV[1], user_options);
1639  options_mask = !stm32x_info->has_extra_options ? ~0xfc :
1640  ~(((0xf00 << (stm32x_info->protection_bits - 12)) | 0xff) & 0xffc);
1641  if (user_options & options_mask) {
1642  command_print(CMD, "stm32f2x invalid user_options");
1644  }
1645 
1646  stm32x_info->option_bytes.user_options = user_options;
1647 
1649  command_print(CMD, "stm32f2x failed to write options");
1650  return ERROR_OK;
1651  }
1652 
1653  /* switching between single- and dual-bank modes requires re-probe */
1654  /* ... and reprogramming of whole flash */
1655  stm32x_info->probed = false;
1656 
1657  command_print(CMD, "stm32f2x write options complete.\n"
1658  "INFO: a reset or power cycle is required "
1659  "for the new settings to take effect.");
1660  return retval;
1661 }
1662 
1663 COMMAND_HANDLER(stm32f2x_handle_optcr2_write_command)
1664 {
1665  int retval;
1666  struct flash_bank *bank;
1667  struct stm32x_flash_bank *stm32x_info = NULL;
1668  uint32_t optcr2_pcrop;
1669 
1670  if (CMD_ARGC != 2)
1672 
1673  retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
1674  if (retval != ERROR_OK)
1675  return retval;
1676 
1677  stm32x_info = bank->driver_priv;
1678  if (!stm32x_info->has_optcr2_pcrop) {
1679  command_print(CMD, "no optcr2 register");
1681  }
1682 
1683  command_print(CMD, "INFO: To disable PCROP, set PCROP_RDP"
1684  " with PCROPi bits STILL SET, then\nlock device and"
1685  " finally unlock it. Clears PCROP and mass erases flash.");
1686 
1687  retval = stm32x_read_options(bank);
1688  if (retval != ERROR_OK)
1689  return retval;
1690 
1691  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], optcr2_pcrop);
1692  stm32x_info->option_bytes.optcr2_pcrop = optcr2_pcrop;
1693 
1695  command_print(CMD, "stm32f2x failed to write options");
1696  return ERROR_OK;
1697  }
1698 
1699  command_print(CMD, "stm32f2x optcr2_write complete.");
1700  return retval;
1701 }
1702 
1703 COMMAND_HANDLER(stm32x_handle_otp_command)
1704 {
1705  if (CMD_ARGC != 2)
1707 
1708  struct flash_bank *bank;
1709  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
1710  if (retval != ERROR_OK)
1711  return retval;
1712  if (stm32x_is_otp(bank)) {
1713  if (strcmp(CMD_ARGV[1], "enable") == 0) {
1715  } else if (strcmp(CMD_ARGV[1], "disable") == 0) {
1717  } else if (strcmp(CMD_ARGV[1], "show") == 0) {
1719  "OTP memory bank #%u is %s for write commands.",
1720  bank->bank_number,
1721  stm32x_is_otp_unlocked(bank) ? "enabled" : "disabled");
1722  } else {
1724  }
1725  } else {
1726  command_print(CMD, "Failed: not an OTP bank.");
1727  }
1728 
1729  return retval;
1730 }
1731 
1732 static const struct command_registration stm32f2x_exec_command_handlers[] = {
1733  {
1734  .name = "lock",
1735  .handler = stm32x_handle_lock_command,
1736  .mode = COMMAND_EXEC,
1737  .usage = "bank_id",
1738  .help = "Lock entire flash device.",
1739  },
1740  {
1741  .name = "unlock",
1742  .handler = stm32x_handle_unlock_command,
1743  .mode = COMMAND_EXEC,
1744  .usage = "bank_id",
1745  .help = "Unlock entire protected flash device.",
1746  },
1747  {
1748  .name = "mass_erase",
1749  .handler = stm32x_handle_mass_erase_command,
1750  .mode = COMMAND_EXEC,
1751  .usage = "bank_id",
1752  .help = "Erase entire flash device.",
1753  },
1754  {
1755  .name = "options_read",
1756  .handler = stm32f2x_handle_options_read_command,
1757  .mode = COMMAND_EXEC,
1758  .usage = "bank_id",
1759  .help = "Read and display device option bytes.",
1760  },
1761  {
1762  .name = "options_write",
1763  .handler = stm32f2x_handle_options_write_command,
1764  .mode = COMMAND_EXEC,
1765  .usage = "bank_id user_options [ boot_add0 boot_add1 ]",
1766  .help = "Write option bytes",
1767  },
1768  {
1769  .name = "optcr2_write",
1770  .handler = stm32f2x_handle_optcr2_write_command,
1771  .mode = COMMAND_EXEC,
1772  .usage = "bank_id optcr2",
1773  .help = "Write optcr2 word",
1774  },
1775  {
1776  .name = "otp",
1777  .handler = stm32x_handle_otp_command,
1778  .mode = COMMAND_EXEC,
1779  .usage = "bank_id (enable|disable|show)",
1780  .help = "OTP (One Time Programmable) memory write enable/disable.",
1781  },
1783 };
1784 
1785 static const struct command_registration stm32f2x_command_handlers[] = {
1786  {
1787  .name = "stm32f2x",
1788  .mode = COMMAND_ANY,
1789  .help = "stm32f2x flash command group",
1790  .usage = "",
1792  },
1794 };
1795 
1796 const struct flash_driver stm32f2x_flash = {
1797  .name = "stm32f2x",
1798  .commands = stm32f2x_command_handlers,
1799  .flash_bank_command = stm32x_flash_bank_command,
1800  .erase = stm32x_erase,
1801  .protect = stm32x_protect,
1802  .write = stm32x_write,
1803  .read = default_flash_read,
1804  .probe = stm32x_probe,
1805  .auto_probe = stm32x_auto_probe,
1806  .erase_check = default_flash_blank_check,
1807  .protect_check = stm32x_protect_check,
1808  .info = get_stm32x_info,
1809  .free_driver_priv = default_flash_free_driver_priv,
1810 };
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
@ PARAM_IN_OUT
Definition: algorithm.h:17
@ ARM_MODE_THREAD
Definition: arm.h:94
#define ARMV7M_COMMON_MAGIC
Definition: armv7m.h:229
Support functions to access arbitrary bits in a byte array.
static uint32_t buf_get_u32(const uint8_t *_buffer, unsigned int first, unsigned int num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 32-bit word.
Definition: binarybuffer.h:104
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:378
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:389
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:146
#define CALL_COMMAND_HANDLER(name, extra ...)
Use this to macro to call a command helper (or a nested handler).
Definition: command.h:123
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:161
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:405
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:156
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
Definition: command.h:445
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:256
#define ERROR_COMMAND_ARGUMENT_INVALID
Definition: command.h:407
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
static enum cortex_m_impl_part cortex_m_get_impl_part(struct target *target)
Definition: cortex_m.h:378
@ CORTEX_M4_PARTNO
Definition: cortex_m.h:58
uint64_t buffer
Pointer to data buffer to send over SPI.
Definition: dw-spi-helper.h:0
uint32_t size
Size of dw_spi_transaction::buffer.
Definition: dw-spi-helper.h:4
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:472
static int64_t start
Definition: log.c:54
#define LOG_WARNING(expr ...)
Definition: log.h:137
#define ERROR_FAIL
Definition: log.h:181
#define LOG_ERROR(expr ...)
Definition: log.h:140
#define LOG_INFO(expr ...)
Definition: log.h:134
#define LOG_DEBUG(expr ...)
Definition: log.h:117
#define ERROR_OK
Definition: log.h:175
struct rtt_control ctrl
Control block.
Definition: rtt/rtt.c:25
struct rtt_source source
Definition: rtt/rtt.c:23
static int stm32x_auto_probe(struct flash_bank *bank)
Definition: stm32f2x.c:1233
#define STM32F7_OTP_SIZE
Definition: stm32f2x.c:124
#define STM32F7_OTP_SECTOR_SIZE
Definition: stm32f2x.c:123
#define OPTCR_NDBANK
Definition: stm32f2x.c:165
const struct flash_driver stm32f2x_flash
Definition: stm32f2x.c:1796
static int stm32x_probe(struct flash_bank *bank)
Definition: stm32f2x.c:976
#define FLASH_ERASE_TIMEOUT
Definition: stm32f2x.c:109
static int get_stm32x_info(struct flash_bank *bank, struct command_invocation *cmd)
Definition: stm32f2x.c:1241
static int stm32x_write_block(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: stm32f2x.c:695
static int stm32x_otp_enable(struct flash_bank *bank)
Definition: stm32f2x.c:228
static int stm32x_get_flash_status(struct flash_bank *bank, uint32_t *status)
Definition: stm32f2x.c:271
#define FLASH_PG
Definition: stm32f2x.c:139
#define FLASH_MER
Definition: stm32f2x.c:141
static uint16_t sector_size_in_kb(unsigned int i, uint16_t max_sector_size_in_kb)
Definition: stm32f2x.c:899
#define KEY2
Definition: stm32f2x.c:174
static unsigned int calculate_number_of_sectors(struct flash_bank *bank, uint16_t flash_size_in_kb, uint16_t max_sector_size_in_kb)
Definition: stm32f2x.c:908
static int stm32x_get_flash_reg(struct flash_bank *bank, uint32_t reg)
Definition: stm32f2x.c:266
#define STM32F2_OTP_SIZE
Definition: stm32f2x.c:117
static bool stm32x_is_otp(struct flash_bank *bank)
Definition: stm32f2x.c:200
#define STM32_FLASH_OPTKEYR
Definition: stm32f2x.c:131
static const struct command_registration stm32f2x_exec_command_handlers[]
Definition: stm32f2x.c:1732
#define STM32_FLASH_BASE
Definition: stm32f2x.c:128
FLASH_BANK_COMMAND_HANDLER(stm32x_flash_bank_command)
Definition: stm32f2x.c:245
#define FLASH_PSIZE_16
Definition: stm32f2x.c:145
static int stm32x_is_otp_unlocked(struct flash_bank *bank)
Definition: stm32f2x.c:211
static void setup_bank(struct flash_bank *bank, unsigned int start, uint16_t flash_size_in_kb, uint16_t max_sector_size_in_kb)
Definition: stm32f2x.c:935
#define STM32F2_OTP_LOCK_BASE
Definition: stm32f2x.c:120
static int stm32x_read_options(struct flash_bank *bank)
Definition: stm32f2x.c:384
#define STM32_FLASH_OPTCR
Definition: stm32f2x.c:134
#define STM32F7_OTP_BANK_BASE
Definition: stm32f2x.c:125
static int stm32x_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: stm32f2x.c:793
static int stm32x_get_device_id(struct flash_bank *bank, uint32_t *device_id)
Definition: stm32f2x.c:953
#define FLASH_BSY
Definition: stm32f2x.c:153
#define OPTCR_LOCK
Definition: stm32f2x.c:163
#define STM32_FLASH_KEYR
Definition: stm32f2x.c:130
#define FLASH_SER
Definition: stm32f2x.c:140
#define FLASH_WRPERR
Definition: stm32f2x.c:157
#define FLASH_ERROR
Definition: stm32f2x.c:160
#define OPTKEY2
Definition: stm32f2x.c:178
static int stm32x_write_options(struct flash_bank *bank)
Definition: stm32f2x.c:445
COMMAND_HANDLER(stm32x_handle_lock_command)
Definition: stm32f2x.c:1418
#define FLASH_PSIZE_8
Definition: stm32f2x.c:144
#define STM32F7_OTP_LOCK_BASE
Definition: stm32f2x.c:126
#define OPTCR_DB1M
Definition: stm32f2x.c:166
#define FLASH_LOCK
Definition: stm32f2x.c:150
static int stm32x_unlock_reg(struct target *target)
Definition: stm32f2x.c:317
#define STM32F2_OTP_BANK_BASE
Definition: stm32f2x.c:119
static int stm32x_wait_status_busy(struct flash_bank *bank, int timeout)
Definition: stm32f2x.c:277
static int stm32x_otp_read_protect(struct flash_bank *bank)
Definition: stm32f2x.c:514
#define STM32F2_OTP_SECTOR_SIZE
Definition: stm32f2x.c:118
#define STM32_FLASH_OPTCR1
Definition: stm32f2x.c:135
static int stm32x_unlock_option_reg(struct target *target)
Definition: stm32f2x.c:352
static int stm32x_mass_erase(struct flash_bank *bank)
Definition: stm32f2x.c:1502
#define FLASH_STRT
Definition: stm32f2x.c:143
static bool stm32x_otp_is_f7(struct flash_bank *bank)
Definition: stm32f2x.c:206
static int stm32x_otp_disable(struct flash_bank *bank)
Definition: stm32f2x.c:218
#define STM32_FLASH_CR
Definition: stm32f2x.c:133
static int stm32x_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
Definition: stm32f2x.c:596
#define OPTCR2_PCROP_RDP
Definition: stm32f2x.c:170
#define STM32_FLASH_SR
Definition: stm32f2x.c:132
#define FLASH_MER1
Definition: stm32f2x.c:142
#define STM32_FLASH_OPTCR2
Definition: stm32f2x.c:136
#define FLASH_SNB(a)
Definition: stm32f2x.c:149
#define OPTKEY1
Definition: stm32f2x.c:177
static int stm32x_protect_check(struct flash_bank *bank)
Definition: stm32f2x.c:563
#define OPTCR_START
Definition: stm32f2x.c:164
static const struct command_registration stm32f2x_command_handlers[]
Definition: stm32f2x.c:1785
static void setup_sector(struct flash_bank *bank, unsigned int i, unsigned int size)
Definition: stm32f2x.c:889
#define FLASH_WRITE_TIMEOUT
Definition: stm32f2x.c:110
#define FLASH_MASS_ERASE_TIMEOUT
Definition: stm32f2x.c:113
static int stm32x_protect(struct flash_bank *bank, int set, unsigned int first, unsigned int last)
Definition: stm32f2x.c:654
#define KEY1
Definition: stm32f2x.c:173
static int stm32x_otp_protect(struct flash_bank *bank, unsigned int first, unsigned int last)
Definition: stm32f2x.c:534
unsigned int common_magic
Definition: armv7m.h:306
enum arm_mode core_mode
Definition: armv7m.h:308
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:239
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
int is_protected
Indication of protection status: 0 = unprotected/unlocked, 1 = protected/locked, other = unknown.
Definition: nor/core.h:55
Definition: register.h:111
uint32_t user_bank_size
Definition: stm32f1x.c:119
struct stm32x_options option_bytes
Definition: stm32f1x.c:108
unsigned int protection_bits
Definition: stm32f2x.c:196
bool has_extra_options
Definition: stm32f2x.c:193
bool has_optcr2_pcrop
Definition: stm32f2x.c:195
uint8_t RDP
Definition: stm32f2x.c:181
uint16_t user_options
Definition: stm32f2x.c:182
uint32_t optcr2_pcrop
Definition: stm32f2x.c:185
uint32_t boot_addr
Definition: stm32f2x.c:184
uint32_t protection
Definition: stm32f1x.c:104
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_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
Definition: target.c:2359
int target_write_u8(struct target *target, target_addr_t address, uint8_t value)
Definition: target.c:2658
int target_read_u8(struct target *target, target_addr_t address, uint8_t *value)
Definition: target.c:2590
int target_write_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Write count items of size bytes to the memory of target at the address given.
Definition: target.c:1283
int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
Definition: target.c:2078
int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
Definition: target.c:2624
int target_free_working_area(struct target *target, struct working_area *area)
Free a working area.
Definition: target.c:2136
int target_alloc_working_area_try(struct target *target, uint32_t size, struct working_area **area)
Definition: target.c:1984
int target_read_u16(struct target *target, target_addr_t address, uint16_t *value)
Definition: target.c:2570
int target_run_flash_async_algorithm(struct target *target, const uint8_t *buffer, uint32_t count, int block_size, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_params, uint32_t buffer_start, uint32_t buffer_size, uint32_t entry_point, uint32_t exit_point, void *arch_info)
Streams data to a circular buffer on target intended for consumption by code running asynchronously o...
Definition: target.c:940
int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
Definition: target.c:2550
#define ERROR_TARGET_NOT_HALTED
Definition: target.h:795
static bool target_was_examined(const struct target *target)
Definition: target.h:432
@ TARGET_HALTED
Definition: target.h:58
#define ERROR_TARGET_NOT_EXAMINED
Definition: target.h:802
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
Definition: target.h:799
#define ERROR_TARGET_FAILURE
Definition: target.h:796
#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