OpenOCD
stm8.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /*
4 * OpenOCD STM8 target driver
5 * Copyright (C) 2017 Ake Rehnman
6 * ake.rehnman(at)gmail.com
7 */
8 
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif
12 
13 #include <helper/log.h>
14 #include "target.h"
15 #include "target_type.h"
16 #include "jtag/adapter.h"
17 #include "jtag/interface.h"
18 #include "jtag/jtag.h"
19 #include "jtag/swim.h"
20 #include "register.h"
21 #include "breakpoints.h"
22 #include "algorithm.h"
23 
24 static struct reg_cache *stm8_build_reg_cache(struct target *target);
25 static int stm8_read_core_reg(struct target *target, unsigned int num);
26 static int stm8_write_core_reg(struct target *target, unsigned int num);
27 static int stm8_save_context(struct target *target);
28 static void stm8_enable_breakpoints(struct target *target);
29 static int stm8_unset_breakpoint(struct target *target,
30  struct breakpoint *breakpoint);
31 static int stm8_set_breakpoint(struct target *target,
32  struct breakpoint *breakpoint);
33 static void stm8_enable_watchpoints(struct target *target);
34 static int stm8_unset_watchpoint(struct target *target,
35  struct watchpoint *watchpoint);
36 static int (*adapter_speed)(int speed);
37 
38 static const struct {
39  unsigned int id;
40  const char *name;
41  const uint8_t bits;
42  enum reg_type type;
43  const char *group;
44  const char *feature;
45  int flag;
46 } stm8_regs[] = {
47  { 0, "pc", 32, REG_TYPE_UINT32, "general", "org.gnu.gdb.stm8.core", 0 },
48  { 1, "a", 8, REG_TYPE_UINT8, "general", "org.gnu.gdb.stm8.core", 0 },
49  { 2, "x", 16, REG_TYPE_UINT16, "general", "org.gnu.gdb.stm8.core", 0 },
50  { 3, "y", 16, REG_TYPE_UINT16, "general", "org.gnu.gdb.stm8.core", 0 },
51  { 4, "sp", 16, REG_TYPE_UINT16, "general", "org.gnu.gdb.stm8.core", 0 },
52  { 5, "cc", 8, REG_TYPE_UINT8, "general", "org.gnu.gdb.stm8.core", 0 },
53 };
54 
55 #define STM8_COMMON_MAGIC 0x53544D38U
56 
57 #define STM8_NUM_REGS ARRAY_SIZE(stm8_regs)
58 #define STM8_PC 0
59 #define STM8_A 1
60 #define STM8_X 2
61 #define STM8_Y 3
62 #define STM8_SP 4
63 #define STM8_CC 5
64 
65 #define CC_I0 0x8
66 #define CC_I1 0x20
67 
68 #define DM_REGS 0x7f00
69 #define DM_REG_A 0x7f00
70 #define DM_REG_PC 0x7f01
71 #define DM_REG_X 0x7f04
72 #define DM_REG_Y 0x7f06
73 #define DM_REG_SP 0x7f08
74 #define DM_REG_CC 0x7f0a
75 
76 #define DM_BKR1E 0x7f90
77 #define DM_BKR2E 0x7f93
78 #define DM_CR1 0x7f96
79 #define DM_CR2 0x7f97
80 #define DM_CSR1 0x7f98
81 #define DM_CSR2 0x7f99
82 
83 #define STE 0x40
84 #define STF 0x20
85 #define RST 0x10
86 #define BRW 0x08
87 #define BK2F 0x04
88 #define BK1F 0x02
89 
90 #define SWBRK 0x20
91 #define SWBKF 0x10
92 #define STALL 0x08
93 #define FLUSH 0x01
94 
95 #define FLASH_CR1_STM8S 0x505A
96 #define FLASH_CR2_STM8S 0x505B
97 #define FLASH_NCR2_STM8S 0x505C
98 #define FLASH_IAPSR_STM8S 0x505F
99 #define FLASH_PUKR_STM8S 0x5062
100 #define FLASH_DUKR_STM8S 0x5064
101 
102 #define FLASH_CR1_STM8L 0x5050
103 #define FLASH_CR2_STM8L 0x5051
104 #define FLASH_NCR2_STM8L 0
105 #define FLASH_PUKR_STM8L 0x5052
106 #define FLASH_DUKR_STM8L 0x5053
107 #define FLASH_IAPSR_STM8L 0x5054
108 
109 /* FLASH_IAPSR */
110 #define HVOFF 0x40
111 #define DUL 0x08
112 #define EOP 0x04
113 #define PUL 0x02
114 #define WR_PG_DIS 0x01
115 
116 /* FLASH_CR2 */
117 #define OPT 0x80
118 #define WPRG 0x40
119 #define ERASE 0x20
120 #define FPRG 0x10
121 #define PRG 0x01
122 
123 /* SWIM_CSR */
124 #define SAFE_MASK 0x80
125 #define NO_ACCESS 0x40
126 #define SWIM_DM 0x20
127 #define HS 0x10
128 #define OSCOFF 0x08
129 #define SWIM_RST 0x04
130 #define HSIT 0x02
131 #define PRI 0x01
132 
133 #define SWIM_CSR 0x7f80
134 
135 #define STM8_BREAK 0x8B
136 
137 enum mem_type {
141  OPTION
142 };
143 
146 };
147 
149  uint32_t num;
150  struct target *target;
151 };
152 
154  /* break on execute */
156  /* break on read */
158  /* break on write */
160  /* break on read, write and execute */
161  HWBRK_ACC
162 };
163 
165  bool used;
166  uint32_t bp_value;
167  uint32_t reg_address;
168  enum hw_break_type type;
169 };
170 
171 struct stm8_common {
172  unsigned int common_magic;
173 
174  void *arch_info;
177 
178  /* working area for fastdata access */
180 
183  uint8_t num_hw_bpoints;
186  uint32_t blocksize;
187  uint32_t flashstart;
188  uint32_t flashend;
189  uint32_t eepromstart;
190  uint32_t eepromend;
191  uint32_t optionstart;
192  uint32_t optionend;
194 
196  uint32_t flash_cr2;
197  uint32_t flash_ncr2;
198  uint32_t flash_iapsr;
199  uint32_t flash_dukr;
200  uint32_t flash_pukr;
201 
202  /* cc value used for interrupt flags restore */
203  uint32_t cc;
204  bool cc_valid;
205 
206  /* register cache to processor synchronization */
207  int (*read_core_reg)(struct target *target, unsigned int num);
208  int (*write_core_reg)(struct target *target, unsigned int num);
209 };
210 
211 static struct stm8_common *target_to_stm8(struct target *target)
212 {
213  return target->arch_info;
214 }
215 
217  uint32_t addr, int size, int count, void *buf)
218 {
219  return swim_read_mem(addr, size, count, buf);
220 }
221 
223  uint32_t addr, int size, int count, const void *buf)
224 {
225  return swim_write_mem(addr, size, count, buf);
226 }
227 
228 static int stm8_write_u8(struct target *target,
229  uint32_t addr, uint8_t val)
230 {
231  uint8_t buf[1];
232 
233  buf[0] = val;
234  return swim_write_mem(addr, 1, 1, buf);
235 }
236 
237 static int stm8_read_u8(struct target *target,
238  uint32_t addr, uint8_t *val)
239 {
240  return swim_read_mem(addr, 1, 1, val);
241 }
242 
243 /*
244  <enable == 0> Disables interrupts.
245  If interrupts are enabled they are masked and the cc register
246  is saved.
247 
248  <enable == 1> Enables interrupts.
249  Enable interrupts is actually restoring I1 I0 state from previous
250  call with enable == 0. Note that if stepping and breaking on a sim
251  instruction will NOT work since the interrupt flags are restored on
252  debug_entry. We don't have any way for the debugger to exclusively
253  disable the interrupts
254 */
255 static int stm8_enable_interrupts(struct target *target, int enable)
256 {
257  struct stm8_common *stm8 = target_to_stm8(target);
258  uint8_t cc;
259 
260  if (enable) {
261  if (!stm8->cc_valid)
262  return ERROR_OK; /* cc was not stashed */
263  /* fetch current cc */
265  /* clear I1 I0 */
266  cc &= ~(CC_I0 + CC_I1);
267  /* restore I1 & I0 from stash*/
268  cc |= (stm8->cc & (CC_I0+CC_I1));
269  /* update current cc */
271  stm8->cc_valid = false;
272  } else {
274  if ((cc & CC_I0) && (cc & CC_I1))
275  return ERROR_OK; /* interrupts already masked */
276  /* stash cc */
277  stm8->cc = cc;
278  stm8->cc_valid = true;
279  /* mask interrupts (disable) */
280  cc |= (CC_I0 + CC_I1);
282  }
283 
284  return ERROR_OK;
285 }
286 
287 static int stm8_set_hwbreak(struct target *target,
288  struct stm8_comparator comparator_list[])
289 {
290  uint8_t buf[3];
291  int i, ret;
292 
293  /* Refer to Table 4 in UM0470 */
294  uint8_t bc = 0x5;
295  uint8_t bir = 0;
296  uint8_t biw = 0;
297 
298  uint32_t data;
299  uint32_t addr;
300 
301  if (!comparator_list[0].used) {
302  comparator_list[0].type = HWBRK_EXEC;
303  comparator_list[0].bp_value = -1;
304  }
305 
306  if (!comparator_list[1].used) {
307  comparator_list[1].type = HWBRK_EXEC;
308  comparator_list[1].bp_value = -1;
309  }
310 
311  if ((comparator_list[0].type == HWBRK_EXEC)
312  && (comparator_list[1].type == HWBRK_EXEC)) {
313  comparator_list[0].reg_address = 0;
314  comparator_list[1].reg_address = 1;
315  }
316 
317  if ((comparator_list[0].type == HWBRK_EXEC)
318  && (comparator_list[1].type != HWBRK_EXEC)) {
319  comparator_list[0].reg_address = 0;
320  comparator_list[1].reg_address = 1;
321  switch (comparator_list[1].type) {
322  case HWBRK_RD:
323  bir = 1;
324  break;
325  case HWBRK_WR:
326  biw = 1;
327  break;
328  default:
329  bir = 1;
330  biw = 1;
331  break;
332  }
333  }
334 
335  if ((comparator_list[1].type == HWBRK_EXEC)
336  && (comparator_list[0].type != HWBRK_EXEC)) {
337  comparator_list[0].reg_address = 1;
338  comparator_list[1].reg_address = 0;
339  switch (comparator_list[0].type) {
340  case HWBRK_RD:
341  bir = 1;
342  break;
343  case HWBRK_WR:
344  biw = 1;
345  break;
346  default:
347  bir = 1;
348  biw = 1;
349  break;
350  }
351  }
352 
353  if ((comparator_list[0].type != HWBRK_EXEC)
354  && (comparator_list[1].type != HWBRK_EXEC)) {
355  if (comparator_list[0].type != comparator_list[1].type) {
356  LOG_ERROR("data hw breakpoints must be of same type");
358  }
359  }
360 
361  for (i = 0; i < 2; i++) {
362  data = comparator_list[i].bp_value;
363  addr = comparator_list[i].reg_address;
364 
365  buf[0] = data >> 16;
366  buf[1] = data >> 8;
367  buf[2] = data;
368 
369  if (addr == 0) {
370  ret = stm8_adapter_write_memory(target, DM_BKR1E, 1, 3, buf);
371  LOG_DEBUG("DM_BKR1E=%" PRIx32, data);
372  } else if (addr == 1) {
373  ret = stm8_adapter_write_memory(target, DM_BKR2E, 1, 3, buf);
374  LOG_DEBUG("DM_BKR2E=%" PRIx32, data);
375  } else {
376  LOG_DEBUG("addr=%" PRIu32, addr);
377  return ERROR_FAIL;
378  }
379 
380  if (ret != ERROR_OK)
381  return ret;
382 
383  ret = stm8_write_u8(target, DM_CR1,
384  (bc << 3) + (bir << 2) + (biw << 1));
385  LOG_DEBUG("DM_CR1=%" PRIx8, buf[0]);
386  if (ret != ERROR_OK)
387  return ret;
388 
389  }
390  return ERROR_OK;
391 }
392 
393 /* read DM control and status regs */
394 static int stm8_read_dm_csrx(struct target *target, uint8_t *csr1,
395  uint8_t *csr2)
396 {
397  int ret;
398  uint8_t buf[2];
399 
400  ret = stm8_adapter_read_memory(target, DM_CSR1, 1, sizeof(buf), buf);
401  if (ret != ERROR_OK)
402  return ret;
403  if (csr1)
404  *csr1 = buf[0];
405  if (csr2)
406  *csr2 = buf[1];
407  return ERROR_OK;
408 }
409 
410 /* set or clear the single step flag in DM */
411 static int stm8_config_step(struct target *target, int enable)
412 {
413  int ret;
414  uint8_t csr1, csr2;
415 
416  ret = stm8_read_dm_csrx(target, &csr1, &csr2);
417  if (ret != ERROR_OK)
418  return ret;
419  if (enable)
420  csr1 |= STE;
421  else
422  csr1 &= ~STE;
423 
424  ret = stm8_write_u8(target, DM_CSR1, csr1);
425  if (ret != ERROR_OK)
426  return ret;
427  return ERROR_OK;
428 }
429 
430 /* set the stall flag in DM */
431 static int stm8_debug_stall(struct target *target)
432 {
433  int ret;
434  uint8_t csr1, csr2;
435 
436  ret = stm8_read_dm_csrx(target, &csr1, &csr2);
437  if (ret != ERROR_OK)
438  return ret;
439  csr2 |= STALL;
440  ret = stm8_write_u8(target, DM_CSR2, csr2);
441  if (ret != ERROR_OK)
442  return ret;
443  return ERROR_OK;
444 }
445 
447 {
448  /* get pointers to arch-specific information */
449  struct stm8_common *stm8 = target_to_stm8(target);
450 
451  if (stm8->bp_scanned)
452  return ERROR_OK;
453 
454  stm8->num_hw_bpoints = 2;
455  stm8->num_hw_bpoints_avail = stm8->num_hw_bpoints;
456 
457  stm8->hw_break_list = calloc(stm8->num_hw_bpoints,
458  sizeof(struct stm8_comparator));
459 
460  stm8->hw_break_list[0].reg_address = 0;
461  stm8->hw_break_list[1].reg_address = 1;
462 
463  LOG_DEBUG("hw breakpoints: numinst %i numdata %i", stm8->num_hw_bpoints,
464  stm8->num_hw_bpoints);
465 
466  stm8->bp_scanned = true;
467 
468  return ERROR_OK;
469 }
470 
472 {
473  int retval;
474  uint8_t csr1, csr2;
475 
476  retval = stm8_read_dm_csrx(target, &csr1, &csr2);
477  if (retval == ERROR_OK)
478  LOG_DEBUG("csr1 = 0x%02X csr2 = 0x%02X", csr1, csr2);
479 
482 
483  if (retval != ERROR_OK)
484  return retval;
485 
486  if (csr1 & RST)
487  /* halted on reset */
489 
490  if (csr1 & (BK1F+BK2F))
491  /* we have halted on a breakpoint (or wp)*/
493 
494  if (csr2 & SWBKF)
495  /* we have halted on a breakpoint */
497 
498  }
499 
500  return ERROR_OK;
501 }
502 
503 static int stm8_debug_entry(struct target *target)
504 {
505  struct stm8_common *stm8 = target_to_stm8(target);
506 
507  /* restore interrupts */
509 
511 
512  /* make sure stepping disabled STE bit in CSR1 cleared */
514 
515  /* attempt to find halt reason */
517 
518  LOG_DEBUG("entered debug state at PC 0x%" PRIx32 ", target->state: %s",
519  buf_get_u32(stm8->core_cache->reg_list[STM8_PC].value, 0, 32),
521 
522  return ERROR_OK;
523 }
524 
525 /* clear stall flag in DM and flush instruction pipe */
526 static int stm8_exit_debug(struct target *target)
527 {
528  int ret;
529  uint8_t csr1, csr2;
530 
531  ret = stm8_read_dm_csrx(target, &csr1, &csr2);
532  if (ret != ERROR_OK)
533  return ret;
534  csr2 |= FLUSH;
535  ret = stm8_write_u8(target, DM_CSR2, csr2);
536  if (ret != ERROR_OK)
537  return ret;
538 
539  csr2 &= ~STALL;
540  csr2 |= SWBRK;
541  ret = stm8_write_u8(target, DM_CSR2, csr2);
542  if (ret != ERROR_OK)
543  return ret;
544  return ERROR_OK;
545 }
546 
547 static int stm8_read_regs(struct target *target, uint32_t regs[])
548 {
549  int ret;
550  uint8_t buf[11];
551 
552  ret = stm8_adapter_read_memory(target, DM_REGS, 1, sizeof(buf), buf);
553  if (ret != ERROR_OK)
554  return ret;
555 
556  regs[0] = be_to_h_u24(buf+DM_REG_PC-DM_REGS);
557  regs[1] = buf[DM_REG_A-DM_REGS];
558  regs[2] = be_to_h_u16(buf+DM_REG_X-DM_REGS);
559  regs[3] = be_to_h_u16(buf+DM_REG_Y-DM_REGS);
560  regs[4] = be_to_h_u16(buf+DM_REG_SP-DM_REGS);
561  regs[5] = buf[DM_REG_CC-DM_REGS];
562 
563  return ERROR_OK;
564 }
565 
566 static int stm8_write_regs(struct target *target, uint32_t regs[])
567 {
568  int ret;
569  uint8_t buf[11];
570 
572  buf[DM_REG_A-DM_REGS] = regs[1];
573  h_u16_to_be(buf+DM_REG_X-DM_REGS, regs[2]);
574  h_u16_to_be(buf+DM_REG_Y-DM_REGS, regs[3]);
576  buf[DM_REG_CC-DM_REGS] = regs[5];
577 
578  ret = stm8_adapter_write_memory(target, DM_REGS, 1, sizeof(buf), buf);
579  if (ret != ERROR_OK)
580  return ret;
581 
582  return ERROR_OK;
583 }
584 
585 static int stm8_get_core_reg(struct reg *reg)
586 {
587  struct stm8_core_reg *stm8_reg = reg->arch_info;
588  struct target *target = stm8_reg->target;
589  struct stm8_common *stm8 = target_to_stm8(target);
590 
591  if (target->state != TARGET_HALTED)
593 
594  return stm8->read_core_reg(target, stm8_reg->num);
595 }
596 
597 static int stm8_set_core_reg(struct reg *reg, uint8_t *buf)
598 {
599  struct stm8_core_reg *stm8_reg = reg->arch_info;
600  struct target *target = stm8_reg->target;
601  uint32_t value = buf_get_u32(buf, 0, reg->size);
602 
603  if (target->state != TARGET_HALTED)
605 
606  buf_set_u32(reg->value, 0, 32, value);
607  reg->dirty = true;
608  reg->valid = true;
609 
610  return ERROR_OK;
611 }
612 
613 static int stm8_save_context(struct target *target)
614 {
615  unsigned int i;
616 
617  /* get pointers to arch-specific information */
618  struct stm8_common *stm8 = target_to_stm8(target);
619 
620  /* read core registers */
622 
623  for (i = 0; i < STM8_NUM_REGS; i++) {
624  if (!stm8->core_cache->reg_list[i].valid)
625  stm8->read_core_reg(target, i);
626  }
627 
628  return ERROR_OK;
629 }
630 
631 static int stm8_restore_context(struct target *target)
632 {
633  unsigned int i;
634 
635  /* get pointers to arch-specific information */
636  struct stm8_common *stm8 = target_to_stm8(target);
637 
638  for (i = 0; i < STM8_NUM_REGS; i++) {
639  if (stm8->core_cache->reg_list[i].dirty)
640  stm8->write_core_reg(target, i);
641  }
642 
643  /* write core regs */
645 
646  return ERROR_OK;
647 }
648 
649 static int stm8_unlock_flash(struct target *target)
650 {
651  uint8_t data[1];
652 
653  struct stm8_common *stm8 = target_to_stm8(target);
654 
655  /* check if flash is unlocked */
656  stm8_read_u8(target, stm8->flash_iapsr, data);
657  if (~data[0] & PUL) {
658  /* unlock flash */
659  stm8_write_u8(target, stm8->flash_pukr, 0x56);
660  stm8_write_u8(target, stm8->flash_pukr, 0xae);
661  }
662 
663  stm8_read_u8(target, stm8->flash_iapsr, data);
664  if (~data[0] & PUL)
665  return ERROR_FAIL;
666  return ERROR_OK;
667 }
668 
669 static int stm8_unlock_eeprom(struct target *target)
670 {
671  uint8_t data[1];
672 
673  struct stm8_common *stm8 = target_to_stm8(target);
674 
675  /* check if eeprom is unlocked */
676  stm8_read_u8(target, stm8->flash_iapsr, data);
677  if (~data[0] & DUL) {
678  /* unlock eeprom */
679  stm8_write_u8(target, stm8->flash_dukr, 0xae);
680  stm8_write_u8(target, stm8->flash_dukr, 0x56);
681  }
682 
683  stm8_read_u8(target, stm8->flash_iapsr, data);
684  if (~data[0] & DUL)
685  return ERROR_FAIL;
686  return ERROR_OK;
687 }
688 
689 static int stm8_write_flash(struct target *target, enum mem_type type,
690  uint32_t address,
691  uint32_t size, uint32_t count, uint32_t blocksize_param,
692  const uint8_t *buffer)
693 {
694  struct stm8_common *stm8 = target_to_stm8(target);
695 
696  uint8_t iapsr;
697  uint8_t opt = 0;
698  unsigned int i;
699  uint32_t blocksize = 0;
700  uint32_t bytecnt;
701  int res;
702 
703  switch (type) {
704  case FLASH:
706  break;
707  case EEPROM:
709  break;
710  case OPTION:
712  opt = OPT;
713  break;
714  default:
715  LOG_ERROR("BUG: wrong mem_type %d", type);
716  assert(0);
717  }
718 
719  if (size == 2) {
720  /* we don't support short writes */
721  count = count * 2;
722  size = 1;
723  }
724 
725  bytecnt = count * size;
726 
727  while (bytecnt) {
728  if ((bytecnt >= blocksize_param) && ((address & (blocksize_param-1)) == 0)) {
729  if (stm8->flash_cr2)
730  stm8_write_u8(target, stm8->flash_cr2, PRG + opt);
731  if (stm8->flash_ncr2)
732  stm8_write_u8(target, stm8->flash_ncr2, ~(PRG + opt));
733  blocksize = blocksize_param;
734  } else if ((bytecnt >= 4) && ((address & 0x3) == 0)) {
735  if (stm8->flash_cr2)
736  stm8_write_u8(target, stm8->flash_cr2, WPRG + opt);
737  if (stm8->flash_ncr2)
738  stm8_write_u8(target, stm8->flash_ncr2, ~(WPRG + opt));
739  blocksize = 4;
740  } else if (blocksize != 1) {
741  if (stm8->flash_cr2)
742  stm8_write_u8(target, stm8->flash_cr2, opt);
743  if (stm8->flash_ncr2)
744  stm8_write_u8(target, stm8->flash_ncr2, ~opt);
745  blocksize = 1;
746  }
747 
749  if (res != ERROR_OK)
750  return res;
751  address += blocksize;
752  buffer += blocksize;
753  bytecnt -= blocksize;
754 
755  /* lets hang here until end of program (EOP) */
756  for (i = 0; i < 16; i++) {
757  stm8_read_u8(target, stm8->flash_iapsr, &iapsr);
758  if (iapsr & EOP)
759  break;
760  else
761  usleep(1000);
762  }
763  if (i == 16)
764  return ERROR_FAIL;
765  }
766 
767  /* disable write access */
768  res = stm8_write_u8(target, stm8->flash_iapsr, 0x0);
769 
770  if (res != ERROR_OK)
771  return ERROR_FAIL;
772 
773  return ERROR_OK;
774 }
775 
777  uint32_t size, uint32_t count,
778  const uint8_t *buffer)
779 {
780  struct stm8_common *stm8 = target_to_stm8(target);
781 
782  LOG_DEBUG("address: 0x%8.8" TARGET_PRIxADDR
783  ", size: 0x%8.8" PRIx32
784  ", count: 0x%8.8" PRIx32,
785  address, size, count);
786 
787  if (target->state != TARGET_HALTED)
788  LOG_WARNING("target not halted");
789 
790  int retval;
791 
792  if ((address >= stm8->flashstart) && (address <= stm8->flashend))
794  stm8->blocksize, buffer);
795  else if ((address >= stm8->eepromstart) && (address <= stm8->eepromend))
797  stm8->blocksize, buffer);
798  else if ((address >= stm8->optionstart) && (address <= stm8->optionend))
800  else
802  buffer);
803 
804  if (retval != ERROR_OK)
805  return ERROR_TARGET_FAILURE;
806 
807  return retval;
808 }
809 
811  uint32_t size, uint32_t count, uint8_t *buffer)
812 {
813  LOG_DEBUG("address: 0x%8.8" TARGET_PRIxADDR
814  ", size: 0x%8.8" PRIx32
815  ", count: 0x%8.8" PRIx32,
816  address, size, count);
817 
818  if (target->state != TARGET_HALTED)
819  LOG_WARNING("target not halted");
820 
821  int retval;
823 
824  if (retval != ERROR_OK)
825  return ERROR_TARGET_FAILURE;
826 
827  return retval;
828 }
829 
830 static int stm8_speed(int speed)
831 {
832  int retval;
833  uint8_t csr;
834 
835  LOG_DEBUG("stm8_speed: %d", speed);
836 
837  csr = SAFE_MASK | SWIM_DM;
838  if (speed >= SWIM_FREQ_HIGH)
839  csr |= HS;
840 
841  LOG_DEBUG("writing B0 to SWIM_CSR (SAFE_MASK + SWIM_DM + HS:%d)", csr & HS ? 1 : 0);
842  retval = stm8_write_u8(NULL, SWIM_CSR, csr);
843  if (retval != ERROR_OK)
844  return retval;
845  return adapter_speed(speed);
846 }
847 
848 static int stm8_init(struct command_context *cmd_ctx, struct target *target)
849 {
850  /*
851  * FIXME: this is a temporarily hack that needs better implementation.
852  * Being the only overwrite of adapter_driver, it prevents declaring const
853  * the struct adapter_driver.
854  * intercept adapter_driver->speed() calls
855  */
858 
860 
861  return ERROR_OK;
862 }
863 
864 static int stm8_poll(struct target *target)
865 {
866  int retval = ERROR_OK;
867  uint8_t csr1, csr2;
868 
869 #ifdef LOG_STM8
870  LOG_DEBUG("target->state %s", target_state_name(target));
871 #endif
872 
873  /* read dm_csrx control regs */
874  retval = stm8_read_dm_csrx(target, &csr1, &csr2);
875  if (retval != ERROR_OK) {
876  LOG_DEBUG("stm8_read_dm_csrx failed retval=%d", retval);
877  /*
878  We return ERROR_OK here even if we didn't get an answer.
879  openocd will call target_wait_state until we get target state TARGET_HALTED
880  */
881  return ERROR_OK;
882  }
883 
884  /* check for processor halted */
885  if (csr2 & STALL) {
886  if (target->state != TARGET_HALTED) {
887  if (target->state == TARGET_UNKNOWN)
888  LOG_DEBUG("DM_CSR2_STALL already set during server startup.");
889 
890  retval = stm8_debug_entry(target);
891  if (retval != ERROR_OK) {
892  LOG_DEBUG("stm8_debug_entry failed retval=%d", retval);
893  return ERROR_TARGET_FAILURE;
894  }
895 
899  } else {
902  }
903  }
904  } else
906 #ifdef LOG_STM8
907  LOG_DEBUG("csr1 = 0x%02X csr2 = 0x%02X", csr1, csr2);
908 #endif
909  return ERROR_OK;
910 }
911 
912 static int stm8_halt(struct target *target)
913 {
914  LOG_DEBUG("target->state: %s", target_state_name(target));
915 
916  if (target->state == TARGET_HALTED) {
917  LOG_DEBUG("target was already halted");
918  return ERROR_OK;
919  }
920 
921  if (target->state == TARGET_UNKNOWN)
922  LOG_WARNING("target was in unknown state when halt was requested");
923 
924  if (target->state == TARGET_RESET) {
925  /* we came here in a reset_halt or reset_init sequence
926  * debug entry was already prepared in stm8_assert_reset()
927  */
929 
930  return ERROR_OK;
931  }
932 
933 
934  /* break processor */
936 
938 
939  return ERROR_OK;
940 }
941 
942 static int stm8_reset_assert(struct target *target)
943 {
944  int res = ERROR_OK;
945  struct stm8_common *stm8 = target_to_stm8(target);
946  bool use_srst_fallback = true;
947 
949 
951  res = adapter_assert_reset();
952  if (res == ERROR_OK)
953  /* hardware srst supported */
954  use_srst_fallback = false;
955  else if (res != ERROR_COMMAND_NOTFOUND)
956  /* some other failure */
957  return res;
958  }
959 
960  if (use_srst_fallback) {
961  LOG_DEBUG("Hardware srst not supported, falling back to swim reset");
962  res = swim_system_reset();
963  if (res != ERROR_OK)
964  return res;
965  }
966 
967  /* registers are now invalid */
969 
972 
973  if (target->reset_halt) {
974  res = target_halt(target);
975  if (res != ERROR_OK)
976  return res;
977  }
978 
979  return ERROR_OK;
980 }
981 
982 static int stm8_reset_deassert(struct target *target)
983 {
984  int res;
986 
988  res = adapter_deassert_reset();
989  if ((res != ERROR_OK) && (res != ERROR_COMMAND_NOTFOUND))
990  return res;
991  }
992 
993  /* The cpu should now be stalled. If halt was requested
994  let poll detect the stall */
995  if (target->reset_halt)
996  return ERROR_OK;
997 
998  /* Instead of going through saving context, polling and
999  then resuming target again just clear stall and proceed. */
1001  return stm8_exit_debug(target);
1002 }
1003 
1004 /* stm8_single_step_core() is only used for stepping over breakpoints
1005  from stm8_resume() */
1007 {
1008  struct stm8_common *stm8 = target_to_stm8(target);
1009 
1010  /* configure single step mode */
1012 
1013  /* disable interrupts while stepping */
1014  if (!stm8->enable_step_irq)
1016 
1017  /* exit debug mode */
1019 
1021 
1022  return ERROR_OK;
1023 }
1024 
1025 static int stm8_resume(struct target *target, bool current,
1026  target_addr_t address, bool handle_breakpoints,
1027  bool debug_execution)
1028 {
1029  struct stm8_common *stm8 = target_to_stm8(target);
1030  struct breakpoint *breakpoint = NULL;
1031  uint32_t resume_pc;
1032 
1033  LOG_DEBUG("%d " TARGET_ADDR_FMT " %d %d", current, address,
1034  handle_breakpoints, debug_execution);
1035 
1036  if (target->state != TARGET_HALTED) {
1037  LOG_TARGET_ERROR(target, "not halted");
1038  return ERROR_TARGET_NOT_HALTED;
1039  }
1040 
1041  if (!debug_execution) {
1045  struct stm8_comparator *comparator_list = stm8->hw_break_list;
1046  stm8_set_hwbreak(target, comparator_list);
1047  }
1048 
1049  /* current = true: continue on current pc,
1050  otherwise continue at <address> */
1051  if (!current) {
1053  0, 32, address);
1054  stm8->core_cache->reg_list[STM8_PC].dirty = true;
1055  stm8->core_cache->reg_list[STM8_PC].valid = true;
1056  }
1057 
1058  if (!current)
1059  resume_pc = address;
1060  else
1061  resume_pc = buf_get_u32(
1062  stm8->core_cache->reg_list[STM8_PC].value,
1063  0, 32);
1064 
1066 
1067  /* the front-end may request us not to handle breakpoints */
1068  if (handle_breakpoints) {
1069  /* Single step past breakpoint at current address */
1070  breakpoint = breakpoint_find(target, resume_pc);
1071  if (breakpoint) {
1072  LOG_DEBUG("unset breakpoint at " TARGET_ADDR_FMT,
1073  breakpoint->address);
1077  }
1078  }
1079 
1080  /* disable interrupts if we are debugging */
1081  if (debug_execution)
1083 
1084  /* exit debug mode */
1087 
1088  /* registers are now invalid */
1090 
1091  if (!debug_execution) {
1094  LOG_DEBUG("target resumed at 0x%" PRIx32, resume_pc);
1095  } else {
1098  LOG_DEBUG("target debug resumed at 0x%" PRIx32, resume_pc);
1099  }
1100 
1101  return ERROR_OK;
1102 }
1103 
1104 static int stm8_init_flash_regs(bool enable_stm8l, struct stm8_common *stm8)
1105 {
1106  stm8->enable_stm8l = enable_stm8l;
1107 
1108  if (stm8->enable_stm8l) {
1109  stm8->flash_cr2 = FLASH_CR2_STM8L;
1110  stm8->flash_ncr2 = FLASH_NCR2_STM8L;
1112  stm8->flash_dukr = FLASH_DUKR_STM8L;
1113  stm8->flash_pukr = FLASH_PUKR_STM8L;
1114  } else {
1115  stm8->flash_cr2 = FLASH_CR2_STM8S;
1116  stm8->flash_ncr2 = FLASH_NCR2_STM8S;
1118  stm8->flash_dukr = FLASH_DUKR_STM8S;
1119  stm8->flash_pukr = FLASH_PUKR_STM8S;
1120  }
1121  return ERROR_OK;
1122 }
1123 
1124 static int stm8_init_arch_info(struct target *target,
1125  struct stm8_common *stm8, struct jtag_tap *tap)
1126 {
1128  target->arch_info = stm8;
1130  stm8->fast_data_area = NULL;
1131  stm8->blocksize = 0x80;
1132  stm8->flashstart = 0x8000;
1133  stm8->flashend = 0xffff;
1134  stm8->eepromstart = 0x4000;
1135  stm8->eepromend = 0x43ff;
1136  stm8->optionstart = 0x4800;
1137  stm8->optionend = 0x487F;
1138 
1139  /* has breakpoint/watchpoint unit been scanned */
1140  stm8->bp_scanned = false;
1141  stm8->hw_break_list = NULL;
1142 
1145 
1146  stm8_init_flash_regs(0, stm8);
1147 
1148  return ERROR_OK;
1149 }
1150 
1151 static int stm8_target_create(struct target *target)
1152 {
1153 
1154  struct stm8_common *stm8 = calloc(1, sizeof(struct stm8_common));
1155 
1158 
1159  return ERROR_OK;
1160 }
1161 
1162 static int stm8_read_core_reg(struct target *target, unsigned int num)
1163 {
1164  uint32_t reg_value;
1165 
1166  /* get pointers to arch-specific information */
1167  struct stm8_common *stm8 = target_to_stm8(target);
1168 
1169  if (num >= STM8_NUM_REGS)
1171 
1172  reg_value = stm8->core_regs[num];
1173  LOG_DEBUG("read core reg %i value 0x%" PRIx32, num, reg_value);
1174  buf_set_u32(stm8->core_cache->reg_list[num].value, 0, 32, reg_value);
1175  stm8->core_cache->reg_list[num].valid = true;
1176  stm8->core_cache->reg_list[num].dirty = false;
1177 
1178  return ERROR_OK;
1179 }
1180 
1181 static int stm8_write_core_reg(struct target *target, unsigned int num)
1182 {
1183  uint32_t reg_value;
1184 
1185  /* get pointers to arch-specific information */
1186  struct stm8_common *stm8 = target_to_stm8(target);
1187 
1188  if (num >= STM8_NUM_REGS)
1190 
1191  reg_value = buf_get_u32(stm8->core_cache->reg_list[num].value, 0, 32);
1192  stm8->core_regs[num] = reg_value;
1193  LOG_DEBUG("write core reg %i value 0x%" PRIx32, num, reg_value);
1194  stm8->core_cache->reg_list[num].valid = true;
1195  stm8->core_cache->reg_list[num].dirty = false;
1196 
1197  return ERROR_OK;
1198 }
1199 
1200 static const char *stm8_get_gdb_arch(const struct target *target)
1201 {
1202  return "stm8";
1203 }
1204 
1205 static int stm8_get_gdb_reg_list(struct target *target, struct reg **reg_list[],
1206  int *reg_list_size, enum target_register_class reg_class)
1207 {
1208  /* get pointers to arch-specific information */
1209  struct stm8_common *stm8 = target_to_stm8(target);
1210  unsigned int i;
1211 
1212  *reg_list_size = STM8_NUM_REGS;
1213  *reg_list = malloc(sizeof(struct reg *) * (*reg_list_size));
1214 
1215  for (i = 0; i < STM8_NUM_REGS; i++)
1216  (*reg_list)[i] = &stm8->core_cache->reg_list[i];
1217 
1218  return ERROR_OK;
1219 }
1220 
1221 static const struct reg_arch_type stm8_reg_type = {
1223  .set = stm8_set_core_reg,
1224 };
1225 
1227 {
1228  /* get pointers to arch-specific information */
1229  struct stm8_common *stm8 = target_to_stm8(target);
1230 
1231  int num_regs = STM8_NUM_REGS;
1232  struct reg_cache **cache_p = register_get_last_cache_p(&target->reg_cache);
1233  struct reg_cache *cache = malloc(sizeof(struct reg_cache));
1234  struct reg *reg_list = calloc(num_regs, sizeof(struct reg));
1235  struct stm8_core_reg *arch_info = malloc(
1236  sizeof(struct stm8_core_reg) * num_regs);
1237  struct reg_feature *feature;
1238  int i;
1239 
1240  /* Build the process context cache */
1241  cache->name = "stm8 registers";
1242  cache->next = NULL;
1243  cache->reg_list = reg_list;
1244  cache->num_regs = num_regs;
1245  (*cache_p) = cache;
1246  stm8->core_cache = cache;
1247 
1248  for (i = 0; i < num_regs; i++) {
1249  arch_info[i].num = stm8_regs[i].id;
1250  arch_info[i].target = target;
1251 
1252  reg_list[i].name = stm8_regs[i].name;
1253  reg_list[i].size = stm8_regs[i].bits;
1254 
1255  reg_list[i].value = calloc(1, 4);
1256  reg_list[i].valid = false;
1257  reg_list[i].type = &stm8_reg_type;
1258  reg_list[i].arch_info = &arch_info[i];
1259 
1260  reg_list[i].reg_data_type = calloc(1, sizeof(struct reg_data_type));
1261  if (reg_list[i].reg_data_type)
1262  reg_list[i].reg_data_type->type = stm8_regs[i].type;
1263  else {
1264  LOG_ERROR("unable to allocate reg type list");
1265  return NULL;
1266  }
1267 
1268  reg_list[i].dirty = false;
1269  reg_list[i].group = stm8_regs[i].group;
1270  reg_list[i].number = stm8_regs[i].id;
1271  reg_list[i].exist = true;
1272  reg_list[i].caller_save = true; /* gdb defaults to true */
1273 
1274  feature = calloc(1, sizeof(struct reg_feature));
1275  if (feature) {
1276  feature->name = stm8_regs[i].feature;
1277  reg_list[i].feature = feature;
1278  } else
1279  LOG_ERROR("unable to allocate feature list");
1280  }
1281 
1282  return cache;
1283 }
1284 
1285 static void stm8_free_reg_cache(struct target *target)
1286 {
1287  struct stm8_common *stm8 = target_to_stm8(target);
1288  struct reg_cache *cache;
1289  struct reg *reg;
1290  unsigned int i;
1291 
1292  cache = stm8->core_cache;
1293 
1294  if (!cache)
1295  return;
1296 
1297  for (i = 0; i < cache->num_regs; i++) {
1298  reg = &cache->reg_list[i];
1299 
1300  free(reg->feature);
1301  free(reg->reg_data_type);
1302  free(reg->value);
1303  }
1304 
1305  free(cache->reg_list[0].arch_info);
1306  free(cache->reg_list);
1307  free(cache);
1308 
1309  stm8->core_cache = NULL;
1310 }
1311 
1312 static void stm8_deinit(struct target *target)
1313 {
1314  struct stm8_common *stm8 = target_to_stm8(target);
1315 
1316  free(stm8->hw_break_list);
1317 
1319 
1320  free(stm8);
1321 }
1322 
1323 static int stm8_arch_state(struct target *target)
1324 {
1325  struct stm8_common *stm8 = target_to_stm8(target);
1326 
1327  LOG_USER("target halted due to %s, pc: 0x%8.8" PRIx32,
1329  buf_get_u32(stm8->core_cache->reg_list[STM8_PC].value, 0, 32));
1330 
1331  return ERROR_OK;
1332 }
1333 
1334 static int stm8_step(struct target *target, bool current,
1335  target_addr_t address, bool handle_breakpoints)
1336 {
1337  LOG_DEBUG("%x " TARGET_ADDR_FMT " %x",
1338  current, address, handle_breakpoints);
1339 
1340  /* get pointers to arch-specific information */
1341  struct stm8_common *stm8 = target_to_stm8(target);
1342  struct breakpoint *breakpoint = NULL;
1343 
1344  if (target->state != TARGET_HALTED) {
1345  LOG_TARGET_ERROR(target, "not halted");
1346  return ERROR_TARGET_NOT_HALTED;
1347  }
1348 
1349  /* current = true: continue on current pc, otherwise continue at <address> */
1350  if (!current) {
1352  stm8->core_cache->reg_list[STM8_PC].dirty = true;
1353  stm8->core_cache->reg_list[STM8_PC].valid = true;
1354  }
1355 
1356  /* the front-end may request us not to handle breakpoints */
1357  if (handle_breakpoints) {
1359  buf_get_u32(stm8->core_cache->reg_list[STM8_PC].value, 0, 32));
1360  if (breakpoint)
1362  }
1363 
1364  /* restore context */
1366 
1367  /* configure single step mode */
1369 
1371 
1373 
1374  /* disable interrupts while stepping */
1375  if (!stm8->enable_step_irq)
1377 
1378  /* exit debug mode */
1380 
1381  /* registers are now invalid */
1383 
1384  LOG_DEBUG("target stepped ");
1386 
1387  if (breakpoint)
1389 
1391 
1392  return ERROR_OK;
1393 }
1394 
1396 {
1398 
1399  /* set any pending breakpoints */
1400  while (breakpoint) {
1401  if (!breakpoint->is_set)
1404  }
1405 }
1406 
1407 static int stm8_set_breakpoint(struct target *target,
1408  struct breakpoint *breakpoint)
1409 {
1410  struct stm8_common *stm8 = target_to_stm8(target);
1411  struct stm8_comparator *comparator_list = stm8->hw_break_list;
1412  int retval;
1413 
1414  if (breakpoint->is_set) {
1415  LOG_WARNING("breakpoint already set");
1416  return ERROR_OK;
1417  }
1418 
1419  if (breakpoint->type == BKPT_HARD) {
1420  int bp_num = 0;
1421 
1422  while (comparator_list[bp_num].used && (bp_num < stm8->num_hw_bpoints))
1423  bp_num++;
1424  if (bp_num >= stm8->num_hw_bpoints) {
1425  LOG_ERROR("Can not find free breakpoint register (bpid: %" PRIu32 ")",
1428  }
1429  breakpoint_hw_set(breakpoint, bp_num);
1430  comparator_list[bp_num].used = true;
1431  comparator_list[bp_num].bp_value = breakpoint->address;
1432  comparator_list[bp_num].type = HWBRK_EXEC;
1433 
1434  retval = stm8_set_hwbreak(target, comparator_list);
1435  if (retval != ERROR_OK)
1436  return retval;
1437 
1438  LOG_DEBUG("bpid: %" PRIu32 ", bp_num %i bp_value 0x%" PRIx32,
1440  bp_num, comparator_list[bp_num].bp_value);
1441  } else if (breakpoint->type == BKPT_SOFT) {
1442  LOG_DEBUG("bpid: %" PRIu32, breakpoint->unique_id);
1443  if (breakpoint->length == 1) {
1444  uint8_t verify = 0x55;
1445 
1448  if (retval != ERROR_OK)
1449  return retval;
1451  if (retval != ERROR_OK)
1452  return retval;
1453 
1454  retval = target_read_u8(target, breakpoint->address, &verify);
1455  if (retval != ERROR_OK)
1456  return retval;
1457  if (verify != STM8_BREAK) {
1458  LOG_ERROR("Unable to set breakpoint at address " TARGET_ADDR_FMT
1459  " - check that memory is read/writable",
1460  breakpoint->address);
1462  }
1463  } else {
1465  }
1466  breakpoint->is_set = true;
1467  }
1468 
1469  return ERROR_OK;
1470 }
1471 
1472 static int stm8_add_breakpoint(struct target *target,
1473  struct breakpoint *breakpoint)
1474 {
1475  struct stm8_common *stm8 = target_to_stm8(target);
1476  int ret;
1477 
1478  if (breakpoint->type == BKPT_HARD) {
1479  if (stm8->num_hw_bpoints_avail < 1) {
1480  LOG_INFO("no hardware breakpoint available");
1482  }
1483 
1485  if (ret != ERROR_OK)
1486  return ret;
1487 
1488  stm8->num_hw_bpoints_avail--;
1489  return ERROR_OK;
1490  }
1491 
1493  if (ret != ERROR_OK)
1494  return ret;
1495 
1496  return ERROR_OK;
1497 }
1498 
1500  struct breakpoint *breakpoint)
1501 {
1502  /* get pointers to arch-specific information */
1503  struct stm8_common *stm8 = target_to_stm8(target);
1504  struct stm8_comparator *comparator_list = stm8->hw_break_list;
1505  int retval;
1506 
1507  if (!breakpoint->is_set) {
1508  LOG_WARNING("breakpoint not set");
1509  return ERROR_OK;
1510  }
1511 
1512  if (breakpoint->type == BKPT_HARD) {
1513  int bp_num = breakpoint->number;
1514  if (bp_num >= stm8->num_hw_bpoints) {
1515  LOG_DEBUG("Invalid comparator number in breakpoint (bpid: %" PRIu32 ")",
1517  return ERROR_OK;
1518  }
1519  LOG_DEBUG("bpid: %" PRIu32 " - releasing hw: %d",
1521  bp_num);
1522  comparator_list[bp_num].used = false;
1523  retval = stm8_set_hwbreak(target, comparator_list);
1524  if (retval != ERROR_OK)
1525  return retval;
1526  } else {
1527  /* restore original instruction (kept in target endianness) */
1528  LOG_DEBUG("bpid: %" PRIu32, breakpoint->unique_id);
1529  if (breakpoint->length == 1) {
1530  uint8_t current_instr;
1531 
1532  /* check that user program has not
1533  modified breakpoint instruction */
1534  retval = target_read_memory(target, breakpoint->address, 1, 1,
1535  (uint8_t *)&current_instr);
1536  if (retval != ERROR_OK)
1537  return retval;
1538 
1539  if (current_instr == STM8_BREAK) {
1540  retval = target_write_memory(target, breakpoint->address, 1, 1,
1542  if (retval != ERROR_OK)
1543  return retval;
1544  }
1545  } else
1546  return ERROR_FAIL;
1547  }
1548  breakpoint->is_set = false;
1549 
1550  return ERROR_OK;
1551 }
1552 
1554  struct breakpoint *breakpoint)
1555 {
1556  /* get pointers to arch-specific information */
1557  struct stm8_common *stm8 = target_to_stm8(target);
1558 
1559  if (target->state != TARGET_HALTED) {
1560  LOG_TARGET_ERROR(target, "not halted");
1561  return ERROR_TARGET_NOT_HALTED;
1562  }
1563 
1564  if (breakpoint->is_set)
1566 
1567  if (breakpoint->type == BKPT_HARD)
1568  stm8->num_hw_bpoints_avail++;
1569 
1570  return ERROR_OK;
1571 }
1572 
1573 static int stm8_set_watchpoint(struct target *target,
1574  struct watchpoint *watchpoint)
1575 {
1576  struct stm8_common *stm8 = target_to_stm8(target);
1577  struct stm8_comparator *comparator_list = stm8->hw_break_list;
1578  int wp_num = 0;
1579  int ret;
1580 
1581  if (watchpoint->is_set) {
1582  LOG_WARNING("watchpoint already set");
1583  return ERROR_OK;
1584  }
1585 
1586  while (comparator_list[wp_num].used && (wp_num < stm8->num_hw_bpoints))
1587  wp_num++;
1588  if (wp_num >= stm8->num_hw_bpoints) {
1589  LOG_ERROR("Can not find free hw breakpoint");
1591  }
1592 
1593  if (watchpoint->length != 1) {
1594  LOG_ERROR("Only watchpoints of length 1 are supported");
1596  }
1597 
1598  enum hw_break_type enable = 0;
1599 
1600  switch (watchpoint->rw) {
1601  case WPT_READ:
1602  enable = HWBRK_RD;
1603  break;
1604  case WPT_WRITE:
1605  enable = HWBRK_WR;
1606  break;
1607  case WPT_ACCESS:
1608  enable = HWBRK_ACC;
1609  break;
1610  default:
1611  LOG_ERROR("BUG: watchpoint->rw neither read, write nor access");
1612  }
1613 
1614  comparator_list[wp_num].used = true;
1615  comparator_list[wp_num].bp_value = watchpoint->address;
1616  comparator_list[wp_num].type = enable;
1617 
1618  ret = stm8_set_hwbreak(target, comparator_list);
1619  if (ret != ERROR_OK) {
1620  comparator_list[wp_num].used = false;
1621  return ret;
1622  }
1623 
1624  watchpoint_set(watchpoint, wp_num);
1625 
1626  LOG_DEBUG("wp_num %i bp_value 0x%" PRIx32,
1627  wp_num,
1628  comparator_list[wp_num].bp_value);
1629 
1630  return ERROR_OK;
1631 }
1632 
1633 static int stm8_add_watchpoint(struct target *target,
1634  struct watchpoint *watchpoint)
1635 {
1636  int ret;
1637  struct stm8_common *stm8 = target_to_stm8(target);
1638 
1639  if (stm8->num_hw_bpoints_avail < 1) {
1640  LOG_INFO("no hardware watchpoints available");
1642  }
1643 
1645  if (ret != ERROR_OK)
1646  return ret;
1647 
1648  stm8->num_hw_bpoints_avail--;
1649  return ERROR_OK;
1650 }
1651 
1653 {
1655 
1656  /* set any pending watchpoints */
1657  while (watchpoint) {
1658  if (!watchpoint->is_set)
1661  }
1662 }
1663 
1665  struct watchpoint *watchpoint)
1666 {
1667  /* get pointers to arch-specific information */
1668  struct stm8_common *stm8 = target_to_stm8(target);
1669  struct stm8_comparator *comparator_list = stm8->hw_break_list;
1670 
1671  if (!watchpoint->is_set) {
1672  LOG_WARNING("watchpoint not set");
1673  return ERROR_OK;
1674  }
1675 
1676  int wp_num = watchpoint->number;
1677  if (wp_num >= stm8->num_hw_bpoints) {
1678  LOG_DEBUG("Invalid hw comparator number in watchpoint");
1679  return ERROR_OK;
1680  }
1681  comparator_list[wp_num].used = false;
1682  watchpoint->is_set = false;
1683 
1684  stm8_set_hwbreak(target, comparator_list);
1685 
1686  return ERROR_OK;
1687 }
1688 
1690  struct watchpoint *watchpoint)
1691 {
1692  /* get pointers to arch-specific information */
1693  struct stm8_common *stm8 = target_to_stm8(target);
1694 
1695  if (target->state != TARGET_HALTED) {
1696  LOG_TARGET_ERROR(target, "not halted");
1697  return ERROR_TARGET_NOT_HALTED;
1698  }
1699 
1700  if (watchpoint->is_set)
1702 
1703  stm8->num_hw_bpoints_avail++;
1704 
1705  return ERROR_OK;
1706 }
1707 
1708 static int stm8_examine(struct target *target)
1709 {
1710  int retval;
1711  uint8_t csr1, csr2;
1712  /* get pointers to arch-specific information */
1713  struct stm8_common *stm8 = target_to_stm8(target);
1715 
1716  if (!target_was_examined(target)) {
1717  if (!stm8->swim_configured) {
1718  stm8->swim_configured = true;
1719  /*
1720  Now is the time to deassert reset if connect_under_reset.
1721  Releasing reset line will cause the option bytes to load.
1722  The core will still be stalled.
1723  */
1727  else
1728  LOG_WARNING("\'srst_nogate\' reset_config option is required");
1729  }
1730  } else {
1731  LOG_INFO("trying to reconnect");
1732 
1733  retval = swim_reconnect();
1734  if (retval != ERROR_OK) {
1735  LOG_ERROR("reconnect failed");
1736  return ERROR_FAIL;
1737  }
1738 
1739  /* read dm_csrx control regs */
1740  retval = stm8_read_dm_csrx(target, &csr1, &csr2);
1741  if (retval != ERROR_OK) {
1742  LOG_ERROR("state query failed");
1743  return ERROR_FAIL;
1744  }
1745  }
1746 
1747  return ERROR_OK;
1748  }
1749 
1750  return ERROR_OK;
1751 }
1752 
1755  struct target_memory_check_block *blocks, unsigned int num_blocks,
1756  uint8_t erased_value, unsigned int *checked)
1757 {
1758  struct working_area *erase_check_algorithm;
1759  struct reg_param reg_params[2];
1760  struct mem_param mem_params[2];
1761  struct stm8_algorithm stm8_info;
1762 
1763  static const uint8_t stm8_erase_check_code[] = {
1764 #include "../../contrib/loaders/erase_check/stm8_erase_check.inc"
1765  };
1766 
1767  if (erased_value != 0xff) {
1768  LOG_ERROR("Erase value 0x%02" PRIx8 " not yet supported for STM8",
1769  erased_value);
1770  return ERROR_FAIL;
1771  }
1772 
1773  /* make sure we have a working area */
1774  if (target_alloc_working_area(target, sizeof(stm8_erase_check_code),
1775  &erase_check_algorithm) != ERROR_OK)
1777 
1778  target_write_buffer(target, erase_check_algorithm->address,
1779  sizeof(stm8_erase_check_code), stm8_erase_check_code);
1780 
1781  stm8_info.common_magic = STM8_COMMON_MAGIC;
1782 
1783  init_mem_param(&mem_params[0], 0x0, 3, PARAM_OUT);
1784  buf_set_u32(mem_params[0].value, 0, 24, blocks[0].address);
1785 
1786  init_mem_param(&mem_params[1], 0x3, 3, PARAM_OUT);
1787  buf_set_u32(mem_params[1].value, 0, 24, blocks[0].size);
1788 
1789  init_reg_param(&reg_params[0], "a", 32, PARAM_IN_OUT);
1790  buf_set_u32(reg_params[0].value, 0, 32, erased_value);
1791 
1792  init_reg_param(&reg_params[1], "sp", 32, PARAM_OUT);
1793  buf_set_u32(reg_params[1].value, 0, 32, erase_check_algorithm->address);
1794 
1795  int retval = target_run_algorithm(target, 2, mem_params, 2, reg_params,
1796  erase_check_algorithm->address + 6,
1797  erase_check_algorithm->address + (sizeof(stm8_erase_check_code) - 1),
1798  10000, &stm8_info);
1799 
1800  if (retval == ERROR_OK) {
1801  blocks[0].result = (*(reg_params[0].value) == 0xff);
1802  *checked = 1; /* only one block has been checked */
1803  }
1804 
1805  destroy_mem_param(&mem_params[0]);
1806  destroy_mem_param(&mem_params[1]);
1807  destroy_reg_param(&reg_params[0]);
1808  destroy_reg_param(&reg_params[1]);
1809 
1810  target_free_working_area(target, erase_check_algorithm);
1811 
1812  return retval;
1813 }
1814 
1816  uint32_t count, uint32_t *checksum)
1817 {
1818  /* let image_calculate_checksum() take care of business */
1820 }
1821 
1822 /* run to exit point. return error if exit point was not reached. */
1823 static int stm8_run_and_wait(struct target *target, uint32_t entry_point,
1824  unsigned int timeout_ms, uint32_t exit_point, struct stm8_common *stm8)
1825 {
1826  uint32_t pc;
1827  int retval;
1828  /* This code relies on the target specific resume() and
1829  poll()->debug_entry() sequence to write register values to the
1830  processor and the read them back */
1831  retval = target_resume(target, false, entry_point, false, true);
1832  if (retval != ERROR_OK)
1833  return retval;
1834 
1835  retval = target_wait_state(target, TARGET_HALTED, timeout_ms);
1836  /* If the target fails to halt due to the breakpoint, force a halt */
1837  if (retval != ERROR_OK || target->state != TARGET_HALTED) {
1838  retval = target_halt(target);
1839  if (retval != ERROR_OK)
1840  return retval;
1841  retval = target_wait_state(target, TARGET_HALTED, 500);
1842  if (retval != ERROR_OK)
1843  return retval;
1844  return ERROR_TARGET_TIMEOUT;
1845  }
1846 
1847  pc = buf_get_u32(stm8->core_cache->reg_list[STM8_PC].value, 0, 32);
1848  if (exit_point && (pc != exit_point)) {
1849  LOG_DEBUG("failed algorithm halted at 0x%" PRIx32 " ", pc);
1850  return ERROR_TARGET_TIMEOUT;
1851  }
1852 
1853  return ERROR_OK;
1854 }
1855 
1856 static int stm8_run_algorithm(struct target *target, int num_mem_params,
1857  struct mem_param *mem_params, int num_reg_params,
1858  struct reg_param *reg_params, target_addr_t entry_point,
1859  target_addr_t exit_point, unsigned int timeout_ms, void *arch_info)
1860 {
1861  struct stm8_common *stm8 = target_to_stm8(target);
1862 
1863  uint32_t context[STM8_NUM_REGS];
1864  int retval = ERROR_OK;
1865 
1866  LOG_DEBUG("Running algorithm");
1867 
1868  /* NOTE: stm8_run_algorithm requires that each
1869  algorithm uses a software breakpoint
1870  at the exit point */
1871 
1872  if (stm8->common_magic != STM8_COMMON_MAGIC) {
1873  LOG_ERROR("current target isn't a STM8 target");
1874  return ERROR_TARGET_INVALID;
1875  }
1876 
1877  if (target->state != TARGET_HALTED) {
1878  LOG_WARNING("target not halted");
1879  return ERROR_TARGET_NOT_HALTED;
1880  }
1881 
1882  /* refresh core register cache */
1883  for (unsigned int i = 0; i < STM8_NUM_REGS; i++) {
1884  if (!stm8->core_cache->reg_list[i].valid)
1885  stm8->read_core_reg(target, i);
1886  context[i] = buf_get_u32(stm8->core_cache->reg_list[i].value, 0, 32);
1887  }
1888 
1889  for (int i = 0; i < num_mem_params; i++) {
1890  if (mem_params[i].direction == PARAM_IN)
1891  continue;
1892  retval = target_write_buffer(target, mem_params[i].address,
1893  mem_params[i].size, mem_params[i].value);
1894  if (retval != ERROR_OK)
1895  return retval;
1896  }
1897 
1898  for (int i = 0; i < num_reg_params; i++) {
1899  if (reg_params[i].direction == PARAM_IN)
1900  continue;
1901 
1902  struct reg *reg = register_get_by_name(stm8->core_cache,
1903  reg_params[i].reg_name, false);
1904 
1905  if (!reg) {
1906  LOG_ERROR("BUG: register '%s' not found", reg_params[i].reg_name);
1908  }
1909 
1910  if (reg_params[i].size != 32) {
1911  LOG_ERROR("BUG: register '%s' size doesn't match reg_params[i].size",
1912  reg_params[i].reg_name);
1914  }
1915 
1916  stm8_set_core_reg(reg, reg_params[i].value);
1917  }
1918 
1919  retval = stm8_run_and_wait(target, entry_point,
1920  timeout_ms, exit_point, stm8);
1921 
1922  if (retval != ERROR_OK)
1923  return retval;
1924 
1925  for (int i = 0; i < num_mem_params; i++) {
1926  if (mem_params[i].direction != PARAM_OUT) {
1927  retval = target_read_buffer(target, mem_params[i].address,
1928  mem_params[i].size, mem_params[i].value);
1929  if (retval != ERROR_OK)
1930  return retval;
1931  }
1932  }
1933 
1934  for (int i = 0; i < num_reg_params; i++) {
1935  if (reg_params[i].direction != PARAM_OUT) {
1936  struct reg *reg = register_get_by_name(stm8->core_cache,
1937  reg_params[i].reg_name, false);
1938  if (!reg) {
1939  LOG_ERROR("BUG: register '%s' not found",
1940  reg_params[i].reg_name);
1942  }
1943 
1944  if (reg_params[i].size != 32) {
1945  LOG_ERROR("BUG: register '%s' size doesn't match reg_params[i].size",
1946  reg_params[i].reg_name);
1948  }
1949 
1950  buf_set_u32(reg_params[i].value,
1951  0, 32, buf_get_u32(reg->value, 0, 32));
1952  }
1953  }
1954 
1955  /* restore everything we saved before */
1956  for (unsigned int i = 0; i < STM8_NUM_REGS; i++) {
1957  uint32_t regvalue;
1958  regvalue = buf_get_u32(stm8->core_cache->reg_list[i].value, 0, 32);
1959  if (regvalue != context[i]) {
1960  LOG_DEBUG("restoring register %s with value 0x%8.8" PRIx32,
1961  stm8->core_cache->reg_list[i].name, context[i]);
1963  0, 32, context[i]);
1964  stm8->core_cache->reg_list[i].valid = true;
1965  stm8->core_cache->reg_list[i].dirty = true;
1966  }
1967  }
1968 
1969  return ERROR_OK;
1970 }
1971 
1972 static int stm8_jim_configure(struct target *target, struct jim_getopt_info *goi)
1973 {
1974  struct stm8_common *stm8 = target_to_stm8(target);
1975  jim_wide w;
1976  int e;
1977  const char *arg;
1978 
1979  arg = Jim_GetString(goi->argv[0], NULL);
1980  if (!strcmp(arg, "-blocksize")) {
1981  e = jim_getopt_string(goi, &arg, NULL);
1982  if (e != JIM_OK)
1983  return e;
1984 
1985  if (goi->argc == 0) {
1986  Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv,
1987  "-blocksize ?bytes? ...");
1988  return JIM_ERR;
1989  }
1990 
1991  e = jim_getopt_wide(goi, &w);
1992  if (e != JIM_OK)
1993  return e;
1994 
1995  stm8->blocksize = w;
1996  LOG_DEBUG("blocksize=%8.8" PRIx32, stm8->blocksize);
1997  return JIM_OK;
1998  }
1999  if (!strcmp(arg, "-flashstart")) {
2000  e = jim_getopt_string(goi, &arg, NULL);
2001  if (e != JIM_OK)
2002  return e;
2003 
2004  if (goi->argc == 0) {
2005  Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv,
2006  "-flashstart ?address? ...");
2007  return JIM_ERR;
2008  }
2009 
2010  e = jim_getopt_wide(goi, &w);
2011  if (e != JIM_OK)
2012  return e;
2013 
2014  stm8->flashstart = w;
2015  LOG_DEBUG("flashstart=%8.8" PRIx32, stm8->flashstart);
2016  return JIM_OK;
2017  }
2018  if (!strcmp(arg, "-flashend")) {
2019  e = jim_getopt_string(goi, &arg, NULL);
2020  if (e != JIM_OK)
2021  return e;
2022 
2023  if (goi->argc == 0) {
2024  Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv,
2025  "-flashend ?address? ...");
2026  return JIM_ERR;
2027  }
2028 
2029  e = jim_getopt_wide(goi, &w);
2030  if (e != JIM_OK)
2031  return e;
2032 
2033  stm8->flashend = w;
2034  LOG_DEBUG("flashend=%8.8" PRIx32, stm8->flashend);
2035  return JIM_OK;
2036  }
2037  if (!strcmp(arg, "-eepromstart")) {
2038  e = jim_getopt_string(goi, &arg, NULL);
2039  if (e != JIM_OK)
2040  return e;
2041 
2042  if (goi->argc == 0) {
2043  Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv,
2044  "-eepromstart ?address? ...");
2045  return JIM_ERR;
2046  }
2047 
2048  e = jim_getopt_wide(goi, &w);
2049  if (e != JIM_OK)
2050  return e;
2051 
2052  stm8->eepromstart = w;
2053  LOG_DEBUG("eepromstart=%8.8" PRIx32, stm8->eepromstart);
2054  return JIM_OK;
2055  }
2056  if (!strcmp(arg, "-eepromend")) {
2057  e = jim_getopt_string(goi, &arg, NULL);
2058  if (e != JIM_OK)
2059  return e;
2060 
2061  if (goi->argc == 0) {
2062  Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv,
2063  "-eepromend ?address? ...");
2064  return JIM_ERR;
2065  }
2066 
2067  e = jim_getopt_wide(goi, &w);
2068  if (e != JIM_OK)
2069  return e;
2070 
2071  stm8->eepromend = w;
2072  LOG_DEBUG("eepromend=%8.8" PRIx32, stm8->eepromend);
2073  return JIM_OK;
2074  }
2075  if (!strcmp(arg, "-optionstart")) {
2076  e = jim_getopt_string(goi, &arg, NULL);
2077  if (e != JIM_OK)
2078  return e;
2079 
2080  if (goi->argc == 0) {
2081  Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv,
2082  "-optionstart ?address? ...");
2083  return JIM_ERR;
2084  }
2085 
2086  e = jim_getopt_wide(goi, &w);
2087  if (e != JIM_OK)
2088  return e;
2089 
2090  stm8->optionstart = w;
2091  LOG_DEBUG("optionstart=%8.8" PRIx32, stm8->optionstart);
2092  return JIM_OK;
2093  }
2094  if (!strcmp(arg, "-optionend")) {
2095  e = jim_getopt_string(goi, &arg, NULL);
2096  if (e != JIM_OK)
2097  return e;
2098 
2099  if (goi->argc == 0) {
2100  Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv,
2101  "-optionend ?address? ...");
2102  return JIM_ERR;
2103  }
2104 
2105  e = jim_getopt_wide(goi, &w);
2106  if (e != JIM_OK)
2107  return e;
2108 
2109  stm8->optionend = w;
2110  LOG_DEBUG("optionend=%8.8" PRIx32, stm8->optionend);
2111  return JIM_OK;
2112  }
2113  if (!strcmp(arg, "-enable_step_irq")) {
2114  e = jim_getopt_string(goi, &arg, NULL);
2115  if (e != JIM_OK)
2116  return e;
2117 
2118  stm8->enable_step_irq = true;
2119  LOG_DEBUG("enable_step_irq=%8.8x", stm8->enable_step_irq);
2120  return JIM_OK;
2121  }
2122  if (!strcmp(arg, "-enable_stm8l")) {
2123  e = jim_getopt_string(goi, &arg, NULL);
2124  if (e != JIM_OK)
2125  return e;
2126 
2127  stm8->enable_stm8l = true;
2128  LOG_DEBUG("enable_stm8l=%8.8x", stm8->enable_stm8l);
2129  stm8_init_flash_regs(stm8->enable_stm8l, stm8);
2130  return JIM_OK;
2131  }
2132  return JIM_CONTINUE;
2133 }
2134 
2135 COMMAND_HANDLER(stm8_handle_enable_step_irq_command)
2136 {
2137  const char *msg;
2139  struct stm8_common *stm8 = target_to_stm8(target);
2140  bool enable = stm8->enable_step_irq;
2141 
2142  if (CMD_ARGC > 0) {
2143  COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
2144  stm8->enable_step_irq = enable;
2145  }
2146  msg = stm8->enable_step_irq ? "enabled" : "disabled";
2147  command_print(CMD, "enable_step_irq = %s", msg);
2148  return ERROR_OK;
2149 }
2150 
2151 COMMAND_HANDLER(stm8_handle_enable_stm8l_command)
2152 {
2153  const char *msg;
2155  struct stm8_common *stm8 = target_to_stm8(target);
2156  bool enable = stm8->enable_stm8l;
2157 
2158  if (CMD_ARGC > 0) {
2159  COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
2160  stm8->enable_stm8l = enable;
2161  }
2162  msg = stm8->enable_stm8l ? "enabled" : "disabled";
2163  command_print(CMD, "enable_stm8l = %s", msg);
2164  stm8_init_flash_regs(stm8->enable_stm8l, stm8);
2165  return ERROR_OK;
2166 }
2167 
2168 static const struct command_registration stm8_exec_command_handlers[] = {
2169  {
2170  .name = "enable_step_irq",
2171  .handler = stm8_handle_enable_step_irq_command,
2172  .mode = COMMAND_ANY,
2173  .help = "Enable/disable irq handling during step",
2174  .usage = "[1/0]",
2175  },
2176  {
2177  .name = "enable_stm8l",
2178  .handler = stm8_handle_enable_stm8l_command,
2179  .mode = COMMAND_ANY,
2180  .help = "Enable/disable STM8L flash programming",
2181  .usage = "[1/0]",
2182  },
2184 };
2185 
2186 static const struct command_registration stm8_command_handlers[] = {
2187  {
2188  .name = "stm8",
2189  .mode = COMMAND_ANY,
2190  .help = "stm8 command group",
2191  .usage = "",
2192  .chain = stm8_exec_command_handlers,
2193  },
2195 };
2196 
2197 struct target_type stm8_target = {
2198  .name = "stm8",
2199 
2200  .poll = stm8_poll,
2201  .arch_state = stm8_arch_state,
2202 
2203  .halt = stm8_halt,
2204  .resume = stm8_resume,
2205  .step = stm8_step,
2206 
2207  .assert_reset = stm8_reset_assert,
2208  .deassert_reset = stm8_reset_deassert,
2209 
2210  .get_gdb_arch = stm8_get_gdb_arch,
2211  .get_gdb_reg_list = stm8_get_gdb_reg_list,
2212 
2213  .read_memory = stm8_read_memory,
2214  .write_memory = stm8_write_memory,
2215  .checksum_memory = stm8_checksum_memory,
2216  .blank_check_memory = stm8_blank_check_memory,
2217 
2218  .run_algorithm = stm8_run_algorithm,
2219 
2220  .add_breakpoint = stm8_add_breakpoint,
2221  .remove_breakpoint = stm8_remove_breakpoint,
2222  .add_watchpoint = stm8_add_watchpoint,
2223  .remove_watchpoint = stm8_remove_watchpoint,
2224 
2225  .commands = stm8_command_handlers,
2226  .target_create = stm8_target_create,
2227  .init_target = stm8_init,
2228  .examine = stm8_examine,
2229 
2230  .deinit_target = stm8_deinit,
2231  .target_jim_configure = stm8_jim_configure,
2232 };
void destroy_mem_param(struct mem_param *param)
Definition: algorithm.c:23
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
void init_mem_param(struct mem_param *param, uint32_t address, uint32_t size, enum param_direction direction)
Definition: algorithm.c:15
@ PARAM_OUT
Definition: algorithm.h:16
@ PARAM_IN
Definition: algorithm.h:15
@ PARAM_IN_OUT
Definition: algorithm.h:17
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
struct breakpoint * breakpoint_find(struct target *target, target_addr_t address)
Definition: breakpoints.c:472
@ BKPT_HARD
Definition: breakpoints.h:18
@ BKPT_SOFT
Definition: breakpoints.h:19
static void watchpoint_set(struct watchpoint *watchpoint, unsigned int number)
Definition: breakpoints.h:81
static void breakpoint_hw_set(struct breakpoint *breakpoint, unsigned int hw_number)
Definition: breakpoints.h:65
@ WPT_ACCESS
Definition: breakpoints.h:23
@ WPT_READ
Definition: breakpoints.h:23
@ WPT_WRITE
Definition: breakpoints.h:23
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 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 ERROR_COMMAND_NOTFOUND
Definition: command.h:406
#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_ENABLE(in, out)
parses an enable/disable command argument
Definition: command.h:536
#define CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
Definition: command.h:151
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:256
@ COMMAND_ANY
Definition: command.h:42
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 address
Starting address. Sector aligned.
Definition: dw-spi-helper.h:0
uint8_t csr
Definition: esirisc.c:136
static uint16_t direction
Definition: ftdi.c:157
int jim_getopt_wide(struct jim_getopt_info *goi, jim_wide *puthere)
Remove argv[0] as wide.
Definition: jim-nvp.c:222
int jim_getopt_string(struct jim_getopt_info *goi, const char **puthere, int *len)
Remove argv[0] as string.
Definition: jim-nvp.c:188
static enum reset_types jtag_reset_config
Definition: jtag/core.c:89
int adapter_deassert_reset(void)
Definition: jtag/core.c:1907
enum reset_types jtag_get_reset_config(void)
Definition: jtag/core.c:1742
int adapter_assert_reset(void)
Definition: jtag/core.c:1887
The JTAG interface can be implemented with a software or hardware fifo.
reset_types
Definition: jtag.h:215
@ RESET_SRST_NO_GATING
Definition: jtag.h:224
@ RESET_HAS_SRST
Definition: jtag.h:218
@ RESET_CNCT_UNDER_SRST
Definition: jtag.h:225
static const struct @113 regs[]
#define LOG_USER(expr ...)
Definition: log.h:150
#define LOG_WARNING(expr ...)
Definition: log.h:144
#define ERROR_FAIL
Definition: log.h:188
#define LOG_TARGET_ERROR(target, fmt_str,...)
Definition: log.h:176
#define LOG_ERROR(expr ...)
Definition: log.h:147
#define LOG_INFO(expr ...)
Definition: log.h:141
#define LOG_DEBUG(expr ...)
Definition: log.h:124
#define ERROR_OK
Definition: log.h:182
struct reg * register_get_by_name(struct reg_cache *first, const char *name, bool search_all)
Definition: register.c:50
struct reg_cache ** register_get_last_cache_p(struct reg_cache **first)
Definition: register.c:72
void register_cache_invalidate(struct reg_cache *cache)
Marks the contents of the register cache as invalid (and clean).
Definition: register.c:94
reg_type
Definition: register.h:19
@ REG_TYPE_UINT16
Definition: register.h:29
@ REG_TYPE_UINT32
Definition: register.h:30
@ REG_TYPE_UINT8
Definition: register.h:28
target_addr_t addr
Start address to search for the control block.
Definition: rtt/rtt.c:28
struct target * target
Definition: rtt/rtt.c:26
#define FLASH_IAPSR_STM8L
Definition: stm8.c:107
#define DM_REG_PC
Definition: stm8.c:70
hw_break_type
Definition: stm8.c:153
@ HWBRK_EXEC
Definition: stm8.c:155
@ HWBRK_WR
Definition: stm8.c:159
@ HWBRK_ACC
Definition: stm8.c:161
@ HWBRK_RD
Definition: stm8.c:157
#define FLASH_PUKR_STM8S
Definition: stm8.c:99
#define SAFE_MASK
Definition: stm8.c:124
#define FLASH_NCR2_STM8S
Definition: stm8.c:97
static int stm8_init_arch_info(struct target *target, struct stm8_common *stm8, struct jtag_tap *tap)
Definition: stm8.c:1124
static int stm8_checksum_memory(struct target *target, target_addr_t address, uint32_t count, uint32_t *checksum)
Definition: stm8.c:1815
static int stm8_arch_state(struct target *target)
Definition: stm8.c:1323
static int stm8_examine(struct target *target)
Definition: stm8.c:1708
#define DM_REG_Y
Definition: stm8.c:72
static int stm8_blank_check_memory(struct target *target, struct target_memory_check_block *blocks, unsigned int num_blocks, uint8_t erased_value, unsigned int *checked)
Checks whether a memory region is erased.
Definition: stm8.c:1754
static const struct command_registration stm8_command_handlers[]
Definition: stm8.c:2186
static const char * stm8_get_gdb_arch(const struct target *target)
Definition: stm8.c:1200
static int stm8_set_hwbreak(struct target *target, struct stm8_comparator comparator_list[])
Definition: stm8.c:287
static void stm8_enable_breakpoints(struct target *target)
Definition: stm8.c:1395
#define SWIM_CSR
Definition: stm8.c:133
#define CC_I1
Definition: stm8.c:66
static int stm8_config_step(struct target *target, int enable)
Definition: stm8.c:411
static int stm8_remove_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: stm8.c:1553
#define CC_I0
Definition: stm8.c:65
#define FLUSH
Definition: stm8.c:93
static int stm8_exit_debug(struct target *target)
Definition: stm8.c:526
#define OPT
Definition: stm8.c:117
#define SWBRK
Definition: stm8.c:90
static int(* adapter_speed)(int speed)
Definition: stm8.c:36
static int stm8_examine_debug_reason(struct target *target)
Definition: stm8.c:471
#define STALL
Definition: stm8.c:92
static int stm8_read_dm_csrx(struct target *target, uint8_t *csr1, uint8_t *csr2)
Definition: stm8.c:394
#define DM_REGS
Definition: stm8.c:68
static const struct @132 stm8_regs[]
static int stm8_write_core_reg(struct target *target, unsigned int num)
Definition: stm8.c:1181
static int stm8_unset_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: stm8.c:1499
static int stm8_enable_interrupts(struct target *target, int enable)
Definition: stm8.c:255
static int stm8_step(struct target *target, bool current, target_addr_t address, bool handle_breakpoints)
Definition: stm8.c:1334
static int stm8_unset_watchpoint(struct target *target, struct watchpoint *watchpoint)
Definition: stm8.c:1664
static int stm8_set_watchpoint(struct target *target, struct watchpoint *watchpoint)
Definition: stm8.c:1573
#define BK1F
Definition: stm8.c:88
static int stm8_debug_entry(struct target *target)
Definition: stm8.c:503
#define FLASH_NCR2_STM8L
Definition: stm8.c:104
COMMAND_HANDLER(stm8_handle_enable_step_irq_command)
Definition: stm8.c:2135
#define FLASH_DUKR_STM8L
Definition: stm8.c:106
static int stm8_read_core_reg(struct target *target, unsigned int num)
Definition: stm8.c:1162
struct target_type stm8_target
Definition: stm8.c:2197
#define DM_CR1
Definition: stm8.c:78
static int stm8_poll(struct target *target)
Definition: stm8.c:864
static int stm8_restore_context(struct target *target)
Definition: stm8.c:631
static int stm8_adapter_write_memory(struct target *target, uint32_t addr, int size, int count, const void *buf)
Definition: stm8.c:222
static int stm8_resume(struct target *target, bool current, target_addr_t address, bool handle_breakpoints, bool debug_execution)
Definition: stm8.c:1025
static int stm8_write_flash(struct target *target, enum mem_type type, uint32_t address, uint32_t size, uint32_t count, uint32_t blocksize_param, const uint8_t *buffer)
Definition: stm8.c:689
static int stm8_init(struct command_context *cmd_ctx, struct target *target)
Definition: stm8.c:848
static int stm8_add_watchpoint(struct target *target, struct watchpoint *watchpoint)
Definition: stm8.c:1633
#define DM_REG_A
Definition: stm8.c:69
#define PRG
Definition: stm8.c:121
#define SWBKF
Definition: stm8.c:91
static int stm8_jim_configure(struct target *target, struct jim_getopt_info *goi)
Definition: stm8.c:1972
static int stm8_adapter_read_memory(struct target *target, uint32_t addr, int size, int count, void *buf)
Definition: stm8.c:216
static int stm8_write_u8(struct target *target, uint32_t addr, uint8_t val)
Definition: stm8.c:228
const char * group
Definition: stm8.c:43
static int stm8_read_regs(struct target *target, uint32_t regs[])
Definition: stm8.c:547
static int stm8_set_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: stm8.c:1407
#define EOP
Definition: stm8.c:112
#define DM_REG_SP
Definition: stm8.c:73
#define BK2F
Definition: stm8.c:87
#define HS
Definition: stm8.c:127
static struct reg_cache * stm8_build_reg_cache(struct target *target)
Definition: stm8.c:1226
#define DM_REG_X
Definition: stm8.c:71
static int stm8_get_core_reg(struct reg *reg)
Definition: stm8.c:585
static int stm8_speed(int speed)
Definition: stm8.c:830
static int stm8_run_algorithm(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_params, target_addr_t entry_point, target_addr_t exit_point, unsigned int timeout_ms, void *arch_info)
Definition: stm8.c:1856
static int stm8_read_u8(struct target *target, uint32_t addr, uint8_t *val)
Definition: stm8.c:237
#define FLASH_CR2_STM8L
Definition: stm8.c:103
#define FLASH_PUKR_STM8L
Definition: stm8.c:105
#define PUL
Definition: stm8.c:113
static void stm8_free_reg_cache(struct target *target)
Definition: stm8.c:1285
#define DUL
Definition: stm8.c:111
static int stm8_reset_assert(struct target *target)
Definition: stm8.c:942
const uint8_t bits
Definition: stm8.c:41
static int stm8_add_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: stm8.c:1472
static int stm8_halt(struct target *target)
Definition: stm8.c:912
static const struct command_registration stm8_exec_command_handlers[]
Definition: stm8.c:2168
#define STM8_PC
Definition: stm8.c:58
#define FLASH_IAPSR_STM8S
Definition: stm8.c:98
static int stm8_unlock_eeprom(struct target *target)
Definition: stm8.c:669
static int stm8_get_gdb_reg_list(struct target *target, struct reg **reg_list[], int *reg_list_size, enum target_register_class reg_class)
Definition: stm8.c:1205
static void stm8_enable_watchpoints(struct target *target)
Definition: stm8.c:1652
static void stm8_deinit(struct target *target)
Definition: stm8.c:1312
#define STM8_COMMON_MAGIC
Definition: stm8.c:55
enum reg_type type
Definition: stm8.c:42
#define FLASH_DUKR_STM8S
Definition: stm8.c:100
static int stm8_unlock_flash(struct target *target)
Definition: stm8.c:649
static int stm8_set_core_reg(struct reg *reg, uint8_t *buf)
Definition: stm8.c:597
#define DM_CSR2
Definition: stm8.c:81
unsigned int id
Definition: stm8.c:39
#define DM_BKR2E
Definition: stm8.c:77
static int stm8_write_regs(struct target *target, uint32_t regs[])
Definition: stm8.c:566
static const struct reg_arch_type stm8_reg_type
Definition: stm8.c:1221
#define RST
Definition: stm8.c:85
static int stm8_save_context(struct target *target)
Definition: stm8.c:613
static struct stm8_common * target_to_stm8(struct target *target)
Definition: stm8.c:211
static int stm8_read_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
Definition: stm8.c:810
static int stm8_run_and_wait(struct target *target, uint32_t entry_point, unsigned int timeout_ms, uint32_t exit_point, struct stm8_common *stm8)
Definition: stm8.c:1823
#define STE
Definition: stm8.c:83
#define FLASH_CR2_STM8S
Definition: stm8.c:96
static int stm8_init_flash_regs(bool enable_stm8l, struct stm8_common *stm8)
Definition: stm8.c:1104
mem_type
Definition: stm8.c:137
@ FLASH
Definition: stm8.c:139
@ RAM
Definition: stm8.c:138
@ EEPROM
Definition: stm8.c:140
@ OPTION
Definition: stm8.c:141
static int stm8_reset_deassert(struct target *target)
Definition: stm8.c:982
#define DM_CSR1
Definition: stm8.c:80
#define STM8_BREAK
Definition: stm8.c:135
int flag
Definition: stm8.c:45
#define WPRG
Definition: stm8.c:118
static int stm8_single_step_core(struct target *target)
Definition: stm8.c:1006
#define DM_REG_CC
Definition: stm8.c:74
static int stm8_debug_stall(struct target *target)
Definition: stm8.c:431
#define DM_BKR1E
Definition: stm8.c:76
#define STM8_NUM_REGS
Definition: stm8.c:57
const char * feature
Definition: stm8.c:44
static int stm8_configure_break_unit(struct target *target)
Definition: stm8.c:446
static int stm8_write_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Definition: stm8.c:776
static int stm8_target_create(struct target *target)
Definition: stm8.c:1151
#define SWIM_DM
Definition: stm8.c:126
static int stm8_remove_watchpoint(struct target *target, struct watchpoint *watchpoint)
Definition: stm8.c:1689
Represents a driver for a debugging interface.
Definition: interface.h:208
int(* speed)(int speed)
Set the interface speed.
Definition: interface.h:271
struct breakpoint * next
Definition: breakpoints.h:34
unsigned int length
Definition: breakpoints.h:29
uint8_t * orig_instr
Definition: breakpoints.h:33
enum breakpoint_type type
Definition: breakpoints.h:30
uint32_t unique_id
Definition: breakpoints.h:35
bool is_set
Definition: breakpoints.h:31
unsigned int number
Definition: breakpoints.h:32
target_addr_t address
Definition: breakpoints.h:27
const char * name
Definition: command.h:239
A TCL -ish GetOpt like code.
Definition: jim-nvp.h:136
Jim_Interp * interp
Definition: jim-nvp.h:137
Jim_Obj *const * argv
Definition: jim-nvp.h:139
Definition: jtag.h:101
int(* get)(struct reg *reg)
Definition: register.h:152
const char * name
Definition: register.h:145
unsigned int num_regs
Definition: register.h:148
struct reg * reg_list
Definition: register.h:147
struct reg_cache * next
Definition: register.h:146
enum reg_type type
Definition: register.h:100
uint8_t * value
Definition: algorithm.h:30
const char * reg_name
Definition: algorithm.h:28
Definition: register.h:111
bool caller_save
Definition: register.h:119
bool valid
Definition: register.h:126
bool exist
Definition: register.h:128
uint32_t size
Definition: register.h:132
const char * group
Definition: register.h:138
uint8_t * value
Definition: register.h:122
struct reg_feature * feature
Definition: register.h:117
struct reg_data_type * reg_data_type
Definition: register.h:135
uint32_t number
Definition: register.h:115
void * arch_info
Definition: register.h:140
bool dirty
Definition: register.h:124
const struct reg_arch_type * type
Definition: register.h:141
const char * name
Definition: register.h:113
int common_magic
Definition: stm8.c:145
uint32_t cc
Definition: stm8.c:203
uint32_t flash_pukr
Definition: stm8.c:200
uint32_t eepromstart
Definition: stm8.c:189
uint32_t optionstart
Definition: stm8.c:191
uint8_t num_hw_bpoints_avail
Definition: stm8.c:184
struct stm8_comparator * hw_break_list
Definition: stm8.c:185
uint8_t num_hw_bpoints
Definition: stm8.c:183
uint32_t flash_ncr2
Definition: stm8.c:197
bool enable_stm8l
Definition: stm8.c:195
uint32_t optionend
Definition: stm8.c:192
bool bp_scanned
Definition: stm8.c:182
uint32_t flash_dukr
Definition: stm8.c:199
uint32_t flash_iapsr
Definition: stm8.c:198
uint32_t flashstart
Definition: stm8.c:187
bool enable_step_irq
Definition: stm8.c:193
void * arch_info
Definition: stm8.c:174
int(* write_core_reg)(struct target *target, unsigned int num)
Definition: stm8.c:208
int(* read_core_reg)(struct target *target, unsigned int num)
Definition: stm8.c:207
uint32_t eepromend
Definition: stm8.c:190
struct working_area * fast_data_area
Definition: stm8.c:179
bool cc_valid
Definition: stm8.c:204
uint32_t flash_cr2
Definition: stm8.c:196
uint32_t core_regs[STM8_NUM_REGS]
Definition: stm8.c:176
uint32_t blocksize
Definition: stm8.c:186
uint32_t flashend
Definition: stm8.c:188
bool swim_configured
Definition: stm8.c:181
unsigned int common_magic
Definition: stm8.c:172
struct reg_cache * core_cache
Definition: stm8.c:175
enum hw_break_type type
Definition: stm8.c:168
uint32_t bp_value
Definition: stm8.c:166
bool used
Definition: stm8.c:165
uint32_t reg_address
Definition: stm8.c:167
struct target * target
Definition: stm8.c:150
uint32_t num
Definition: stm8.c:149
This holds methods shared between all instances of a given target type.
Definition: target_type.h:26
const char * name
Name of this type of target.
Definition: target_type.h:31
Definition: target.h:119
struct jtag_tap * tap
Definition: target.h:122
enum target_debug_reason debug_reason
Definition: target.h:164
enum target_state state
Definition: target.h:167
enum target_endianness endianness
Definition: target.h:165
struct reg_cache * reg_cache
Definition: target.h:168
struct breakpoint * breakpoints
Definition: target.h:169
struct watchpoint * watchpoints
Definition: target.h:170
void * arch_info
Definition: target.h:174
bool reset_halt
Definition: target.h:154
enum watchpoint_rw rw
Definition: breakpoints.h:46
bool is_set
Definition: breakpoints.h:47
struct watchpoint * next
Definition: breakpoints.h:49
unsigned int length
Definition: breakpoints.h:43
unsigned int number
Definition: breakpoints.h:48
target_addr_t address
Definition: breakpoints.h:42
target_addr_t address
Definition: target.h:89
int swim_read_mem(uint32_t addr, uint32_t size, uint32_t count, uint8_t *buffer)
Definition: swim.c:28
int swim_reconnect(void)
Definition: swim.c:44
int swim_system_reset(void)
Definition: swim.c:21
int swim_write_mem(uint32_t addr, uint32_t size, uint32_t count, const uint8_t *buffer)
Definition: swim.c:36
This file implements support for STMicroelectronics debug protocol SWIM (Single Wire Interface Module...
#define SWIM_FREQ_HIGH
Definition: swim.h:17
int target_call_event_callbacks(struct target *target, enum target_event event)
Definition: target.c:1794
void target_free_all_working_areas(struct target *target)
Definition: target.c:2180
int target_halt(struct target *target)
Definition: target.c:517
int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
Definition: target.c:2369
int target_write_u8(struct target *target, target_addr_t address, uint8_t value)
Definition: target.c:2669
int target_read_buffer(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
Definition: target.c:2434
int target_read_u8(struct target *target, target_addr_t address, uint8_t *value)
Definition: target.c:2601
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:786
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:1288
int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
Definition: target.c:2090
const char * target_state_name(const struct target *t)
Return the name of this targets current state.
Definition: target.c:270
int target_free_working_area(struct target *target, struct working_area *area)
Free a working area.
Definition: target.c:2148
int target_resume(struct target *target, bool current, target_addr_t address, bool handle_breakpoints, bool debug_execution)
Make the target (re)start executing using its saved execution context (possibly with some modificatio...
Definition: target.c:566
int target_read_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
Read count items of size bytes from the memory of target at the address given.
Definition: target.c:1260
const char * debug_reason_name(const struct target *t)
Definition: target.c:257
int target_wait_state(struct target *target, enum target_state state, unsigned int ms)
Definition: target.c:3167
struct target * get_current_target(struct command_context *cmd_ctx)
Definition: target.c:468
@ DBG_REASON_UNDEFINED
Definition: target.h:80
@ DBG_REASON_NOTHALTED
Definition: target.h:77
@ DBG_REASON_DBGRQ
Definition: target.h:72
@ DBG_REASON_SINGLESTEP
Definition: target.h:76
@ DBG_REASON_BREAKPOINT
Definition: target.h:73
target_register_class
Definition: target.h:113
#define ERROR_TARGET_NOT_HALTED
Definition: target.h:817
static bool target_was_examined(const struct target *target)
Definition: target.h:443
#define ERROR_TARGET_UNALIGNED_ACCESS
Definition: target.h:819
#define ERROR_TARGET_INVALID
Definition: target.h:814
@ TARGET_EVENT_DEBUG_RESUMED
Definition: target.h:285
@ TARGET_EVENT_HALTED
Definition: target.h:265
@ TARGET_EVENT_RESUMED
Definition: target.h:266
@ TARGET_EVENT_DEBUG_HALTED
Definition: target.h:284
@ TARGET_RESET
Definition: target.h:59
@ TARGET_DEBUG_RUNNING
Definition: target.h:60
@ TARGET_UNKNOWN
Definition: target.h:56
@ TARGET_HALTED
Definition: target.h:58
@ TARGET_RUNNING
Definition: target.h:57
@ TARGET_BIG_ENDIAN
Definition: target.h:85
#define ERROR_TARGET_TIMEOUT
Definition: target.h:816
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
Definition: target.h:821
#define ERROR_TARGET_FAILURE
Definition: target.h:818
static uint32_t be_to_h_u24(const uint8_t *buf)
Definition: types.h:144
static void h_u16_to_be(uint8_t *buf, uint16_t val)
Definition: types.h:214
#define TARGET_ADDR_FMT
Definition: types.h:286
uint64_t target_addr_t
Definition: types.h:279
static void h_u24_to_be(uint8_t *buf, unsigned int val)
Definition: types.h:201
static uint16_t be_to_h_u16(const uint8_t *buf)
Definition: types.h:149
#define TARGET_PRIxADDR
Definition: types.h:284
#define NULL
Definition: usb.h:16
uint8_t count[4]
Definition: vdebug.c:22