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/interface.h"
17 #include "jtag/jtag.h"
18 #include "jtag/swim.h"
19 #include "register.h"
20 #include "breakpoints.h"
21 #include "algorithm.h"
22 
23 static struct reg_cache *stm8_build_reg_cache(struct target *target);
24 static int stm8_read_core_reg(struct target *target, unsigned int num);
25 static int stm8_write_core_reg(struct target *target, unsigned int num);
26 static int stm8_save_context(struct target *target);
27 static void stm8_enable_breakpoints(struct target *target);
28 static int stm8_unset_breakpoint(struct target *target,
29  struct breakpoint *breakpoint);
30 static int stm8_set_breakpoint(struct target *target,
31  struct breakpoint *breakpoint);
32 static void stm8_enable_watchpoints(struct target *target);
33 static int stm8_unset_watchpoint(struct target *target,
34  struct watchpoint *watchpoint);
35 static int (*adapter_speed)(int speed);
36 extern struct adapter_driver *adapter_driver;
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  int retval;
588  struct stm8_core_reg *stm8_reg = reg->arch_info;
589  struct target *target = stm8_reg->target;
590  struct stm8_common *stm8 = target_to_stm8(target);
591 
592  if (target->state != TARGET_HALTED)
594 
595  retval = stm8->read_core_reg(target, stm8_reg->num);
596 
597  return retval;
598 }
599 
600 static int stm8_set_core_reg(struct reg *reg, uint8_t *buf)
601 {
602  struct stm8_core_reg *stm8_reg = reg->arch_info;
603  struct target *target = stm8_reg->target;
604  uint32_t value = buf_get_u32(buf, 0, reg->size);
605 
606  if (target->state != TARGET_HALTED)
608 
609  buf_set_u32(reg->value, 0, 32, value);
610  reg->dirty = true;
611  reg->valid = true;
612 
613  return ERROR_OK;
614 }
615 
616 static int stm8_save_context(struct target *target)
617 {
618  unsigned int i;
619 
620  /* get pointers to arch-specific information */
621  struct stm8_common *stm8 = target_to_stm8(target);
622 
623  /* read core registers */
625 
626  for (i = 0; i < STM8_NUM_REGS; i++) {
627  if (!stm8->core_cache->reg_list[i].valid)
628  stm8->read_core_reg(target, i);
629  }
630 
631  return ERROR_OK;
632 }
633 
634 static int stm8_restore_context(struct target *target)
635 {
636  unsigned int i;
637 
638  /* get pointers to arch-specific information */
639  struct stm8_common *stm8 = target_to_stm8(target);
640 
641  for (i = 0; i < STM8_NUM_REGS; i++) {
642  if (stm8->core_cache->reg_list[i].dirty)
643  stm8->write_core_reg(target, i);
644  }
645 
646  /* write core regs */
648 
649  return ERROR_OK;
650 }
651 
652 static int stm8_unlock_flash(struct target *target)
653 {
654  uint8_t data[1];
655 
656  struct stm8_common *stm8 = target_to_stm8(target);
657 
658  /* check if flash is unlocked */
659  stm8_read_u8(target, stm8->flash_iapsr, data);
660  if (~data[0] & PUL) {
661  /* unlock flash */
662  stm8_write_u8(target, stm8->flash_pukr, 0x56);
663  stm8_write_u8(target, stm8->flash_pukr, 0xae);
664  }
665 
666  stm8_read_u8(target, stm8->flash_iapsr, data);
667  if (~data[0] & PUL)
668  return ERROR_FAIL;
669  return ERROR_OK;
670 }
671 
672 static int stm8_unlock_eeprom(struct target *target)
673 {
674  uint8_t data[1];
675 
676  struct stm8_common *stm8 = target_to_stm8(target);
677 
678  /* check if eeprom is unlocked */
679  stm8_read_u8(target, stm8->flash_iapsr, data);
680  if (~data[0] & DUL) {
681  /* unlock eeprom */
682  stm8_write_u8(target, stm8->flash_dukr, 0xae);
683  stm8_write_u8(target, stm8->flash_dukr, 0x56);
684  }
685 
686  stm8_read_u8(target, stm8->flash_iapsr, data);
687  if (~data[0] & DUL)
688  return ERROR_FAIL;
689  return ERROR_OK;
690 }
691 
692 static int stm8_write_flash(struct target *target, enum mem_type type,
693  uint32_t address,
694  uint32_t size, uint32_t count, uint32_t blocksize_param,
695  const uint8_t *buffer)
696 {
697  struct stm8_common *stm8 = target_to_stm8(target);
698 
699  uint8_t iapsr;
700  uint8_t opt = 0;
701  unsigned int i;
702  uint32_t blocksize = 0;
703  uint32_t bytecnt;
704  int res;
705 
706  switch (type) {
707  case (FLASH):
709  break;
710  case (EEPROM):
712  break;
713  case (OPTION):
715  opt = OPT;
716  break;
717  default:
718  LOG_ERROR("BUG: wrong mem_type %d", type);
719  assert(0);
720  }
721 
722  if (size == 2) {
723  /* we don't support short writes */
724  count = count * 2;
725  size = 1;
726  }
727 
728  bytecnt = count * size;
729 
730  while (bytecnt) {
731  if ((bytecnt >= blocksize_param) && ((address & (blocksize_param-1)) == 0)) {
732  if (stm8->flash_cr2)
733  stm8_write_u8(target, stm8->flash_cr2, PRG + opt);
734  if (stm8->flash_ncr2)
735  stm8_write_u8(target, stm8->flash_ncr2, ~(PRG + opt));
736  blocksize = blocksize_param;
737  } else if ((bytecnt >= 4) && ((address & 0x3) == 0)) {
738  if (stm8->flash_cr2)
739  stm8_write_u8(target, stm8->flash_cr2, WPRG + opt);
740  if (stm8->flash_ncr2)
741  stm8_write_u8(target, stm8->flash_ncr2, ~(WPRG + opt));
742  blocksize = 4;
743  } else if (blocksize != 1) {
744  if (stm8->flash_cr2)
745  stm8_write_u8(target, stm8->flash_cr2, opt);
746  if (stm8->flash_ncr2)
747  stm8_write_u8(target, stm8->flash_ncr2, ~opt);
748  blocksize = 1;
749  }
750 
752  if (res != ERROR_OK)
753  return res;
754  address += blocksize;
755  buffer += blocksize;
756  bytecnt -= blocksize;
757 
758  /* lets hang here until end of program (EOP) */
759  for (i = 0; i < 16; i++) {
760  stm8_read_u8(target, stm8->flash_iapsr, &iapsr);
761  if (iapsr & EOP)
762  break;
763  else
764  usleep(1000);
765  }
766  if (i == 16)
767  return ERROR_FAIL;
768  }
769 
770  /* disable write access */
771  res = stm8_write_u8(target, stm8->flash_iapsr, 0x0);
772 
773  if (res != ERROR_OK)
774  return ERROR_FAIL;
775 
776  return ERROR_OK;
777 }
778 
780  uint32_t size, uint32_t count,
781  const uint8_t *buffer)
782 {
783  struct stm8_common *stm8 = target_to_stm8(target);
784 
785  LOG_DEBUG("address: 0x%8.8" TARGET_PRIxADDR
786  ", size: 0x%8.8" PRIx32
787  ", count: 0x%8.8" PRIx32,
788  address, size, count);
789 
790  if (target->state != TARGET_HALTED)
791  LOG_WARNING("target not halted");
792 
793  int retval;
794 
795  if ((address >= stm8->flashstart) && (address <= stm8->flashend))
797  stm8->blocksize, buffer);
798  else if ((address >= stm8->eepromstart) && (address <= stm8->eepromend))
800  stm8->blocksize, buffer);
801  else if ((address >= stm8->optionstart) && (address <= stm8->optionend))
803  else
805  buffer);
806 
807  if (retval != ERROR_OK)
808  return ERROR_TARGET_FAILURE;
809 
810  return retval;
811 }
812 
814  uint32_t size, uint32_t count, uint8_t *buffer)
815 {
816  LOG_DEBUG("address: 0x%8.8" TARGET_PRIxADDR
817  ", size: 0x%8.8" PRIx32
818  ", count: 0x%8.8" PRIx32,
819  address, size, count);
820 
821  if (target->state != TARGET_HALTED)
822  LOG_WARNING("target not halted");
823 
824  int retval;
826 
827  if (retval != ERROR_OK)
828  return ERROR_TARGET_FAILURE;
829 
830  return retval;
831 }
832 
833 static int stm8_speed(int speed)
834 {
835  int retval;
836  uint8_t csr;
837 
838  LOG_DEBUG("stm8_speed: %d", speed);
839 
840  csr = SAFE_MASK | SWIM_DM;
841  if (speed >= SWIM_FREQ_HIGH)
842  csr |= HS;
843 
844  LOG_DEBUG("writing B0 to SWIM_CSR (SAFE_MASK + SWIM_DM + HS:%d)", csr & HS ? 1 : 0);
845  retval = stm8_write_u8(NULL, SWIM_CSR, csr);
846  if (retval != ERROR_OK)
847  return retval;
848  return adapter_speed(speed);
849 }
850 
851 static int stm8_init(struct command_context *cmd_ctx, struct target *target)
852 {
853  /*
854  * FIXME: this is a temporarily hack that needs better implementation.
855  * Being the only overwrite of adapter_driver, it prevents declaring const
856  * the struct adapter_driver.
857  * intercept adapter_driver->speed() calls
858  */
861 
863 
864  return ERROR_OK;
865 }
866 
867 static int stm8_poll(struct target *target)
868 {
869  int retval = ERROR_OK;
870  uint8_t csr1, csr2;
871 
872 #ifdef LOG_STM8
873  LOG_DEBUG("target->state=%d", target->state);
874 #endif
875 
876  /* read dm_csrx control regs */
877  retval = stm8_read_dm_csrx(target, &csr1, &csr2);
878  if (retval != ERROR_OK) {
879  LOG_DEBUG("stm8_read_dm_csrx failed retval=%d", retval);
880  /*
881  We return ERROR_OK here even if we didn't get an answer.
882  openocd will call target_wait_state until we get target state TARGET_HALTED
883  */
884  return ERROR_OK;
885  }
886 
887  /* check for processor halted */
888  if (csr2 & STALL) {
889  if (target->state != TARGET_HALTED) {
890  if (target->state == TARGET_UNKNOWN)
891  LOG_DEBUG("DM_CSR2_STALL already set during server startup.");
892 
893  retval = stm8_debug_entry(target);
894  if (retval != ERROR_OK) {
895  LOG_DEBUG("stm8_debug_entry failed retval=%d", retval);
896  return ERROR_TARGET_FAILURE;
897  }
898 
902  } else {
905  }
906  }
907  } else
909 #ifdef LOG_STM8
910  LOG_DEBUG("csr1 = 0x%02X csr2 = 0x%02X", csr1, csr2);
911 #endif
912  return ERROR_OK;
913 }
914 
915 static int stm8_halt(struct target *target)
916 {
917  LOG_DEBUG("target->state: %s", target_state_name(target));
918 
919  if (target->state == TARGET_HALTED) {
920  LOG_DEBUG("target was already halted");
921  return ERROR_OK;
922  }
923 
924  if (target->state == TARGET_UNKNOWN)
925  LOG_WARNING("target was in unknown state when halt was requested");
926 
927  if (target->state == TARGET_RESET) {
928  /* we came here in a reset_halt or reset_init sequence
929  * debug entry was already prepared in stm8_assert_reset()
930  */
932 
933  return ERROR_OK;
934  }
935 
936 
937  /* break processor */
939 
941 
942  return ERROR_OK;
943 }
944 
945 static int stm8_reset_assert(struct target *target)
946 {
947  int res = ERROR_OK;
948  struct stm8_common *stm8 = target_to_stm8(target);
949  bool use_srst_fallback = true;
950 
952 
954  res = adapter_assert_reset();
955  if (res == ERROR_OK)
956  /* hardware srst supported */
957  use_srst_fallback = false;
958  else if (res != ERROR_COMMAND_NOTFOUND)
959  /* some other failure */
960  return res;
961  }
962 
963  if (use_srst_fallback) {
964  LOG_DEBUG("Hardware srst not supported, falling back to swim reset");
965  res = swim_system_reset();
966  if (res != ERROR_OK)
967  return res;
968  }
969 
970  /* registers are now invalid */
972 
975 
976  if (target->reset_halt) {
977  res = target_halt(target);
978  if (res != ERROR_OK)
979  return res;
980  }
981 
982  return ERROR_OK;
983 }
984 
985 static int stm8_reset_deassert(struct target *target)
986 {
987  int res;
989 
991  res = adapter_deassert_reset();
992  if ((res != ERROR_OK) && (res != ERROR_COMMAND_NOTFOUND))
993  return res;
994  }
995 
996  /* The cpu should now be stalled. If halt was requested
997  let poll detect the stall */
998  if (target->reset_halt)
999  return ERROR_OK;
1000 
1001  /* Instead of going through saving context, polling and
1002  then resuming target again just clear stall and proceed. */
1004  return stm8_exit_debug(target);
1005 }
1006 
1007 /* stm8_single_step_core() is only used for stepping over breakpoints
1008  from stm8_resume() */
1010 {
1011  struct stm8_common *stm8 = target_to_stm8(target);
1012 
1013  /* configure single step mode */
1015 
1016  /* disable interrupts while stepping */
1017  if (!stm8->enable_step_irq)
1019 
1020  /* exit debug mode */
1022 
1024 
1025  return ERROR_OK;
1026 }
1027 
1028 static int stm8_resume(struct target *target, bool current,
1029  target_addr_t address, bool handle_breakpoints,
1030  bool debug_execution)
1031 {
1032  struct stm8_common *stm8 = target_to_stm8(target);
1033  struct breakpoint *breakpoint = NULL;
1034  uint32_t resume_pc;
1035 
1036  LOG_DEBUG("%d " TARGET_ADDR_FMT " %d %d", current, address,
1037  handle_breakpoints, debug_execution);
1038 
1039  if (target->state != TARGET_HALTED) {
1040  LOG_TARGET_ERROR(target, "not halted");
1041  return ERROR_TARGET_NOT_HALTED;
1042  }
1043 
1044  if (!debug_execution) {
1048  struct stm8_comparator *comparator_list = stm8->hw_break_list;
1049  stm8_set_hwbreak(target, comparator_list);
1050  }
1051 
1052  /* current = true: continue on current pc,
1053  otherwise continue at <address> */
1054  if (!current) {
1056  0, 32, address);
1057  stm8->core_cache->reg_list[STM8_PC].dirty = true;
1058  stm8->core_cache->reg_list[STM8_PC].valid = true;
1059  }
1060 
1061  if (!current)
1062  resume_pc = address;
1063  else
1064  resume_pc = buf_get_u32(
1065  stm8->core_cache->reg_list[STM8_PC].value,
1066  0, 32);
1067 
1069 
1070  /* the front-end may request us not to handle breakpoints */
1071  if (handle_breakpoints) {
1072  /* Single step past breakpoint at current address */
1073  breakpoint = breakpoint_find(target, resume_pc);
1074  if (breakpoint) {
1075  LOG_DEBUG("unset breakpoint at " TARGET_ADDR_FMT,
1076  breakpoint->address);
1080  }
1081  }
1082 
1083  /* disable interrupts if we are debugging */
1084  if (debug_execution)
1086 
1087  /* exit debug mode */
1090 
1091  /* registers are now invalid */
1093 
1094  if (!debug_execution) {
1097  LOG_DEBUG("target resumed at 0x%" PRIx32 "", resume_pc);
1098  } else {
1101  LOG_DEBUG("target debug resumed at 0x%" PRIx32 "", resume_pc);
1102  }
1103 
1104  return ERROR_OK;
1105 }
1106 
1107 static int stm8_init_flash_regs(bool enable_stm8l, struct stm8_common *stm8)
1108 {
1109  stm8->enable_stm8l = enable_stm8l;
1110 
1111  if (stm8->enable_stm8l) {
1112  stm8->flash_cr2 = FLASH_CR2_STM8L;
1113  stm8->flash_ncr2 = FLASH_NCR2_STM8L;
1115  stm8->flash_dukr = FLASH_DUKR_STM8L;
1116  stm8->flash_pukr = FLASH_PUKR_STM8L;
1117  } else {
1118  stm8->flash_cr2 = FLASH_CR2_STM8S;
1119  stm8->flash_ncr2 = FLASH_NCR2_STM8S;
1121  stm8->flash_dukr = FLASH_DUKR_STM8S;
1122  stm8->flash_pukr = FLASH_PUKR_STM8S;
1123  }
1124  return ERROR_OK;
1125 }
1126 
1127 static int stm8_init_arch_info(struct target *target,
1128  struct stm8_common *stm8, struct jtag_tap *tap)
1129 {
1131  target->arch_info = stm8;
1133  stm8->fast_data_area = NULL;
1134  stm8->blocksize = 0x80;
1135  stm8->flashstart = 0x8000;
1136  stm8->flashend = 0xffff;
1137  stm8->eepromstart = 0x4000;
1138  stm8->eepromend = 0x43ff;
1139  stm8->optionstart = 0x4800;
1140  stm8->optionend = 0x487F;
1141 
1142  /* has breakpoint/watchpoint unit been scanned */
1143  stm8->bp_scanned = false;
1144  stm8->hw_break_list = NULL;
1145 
1148 
1149  stm8_init_flash_regs(0, stm8);
1150 
1151  return ERROR_OK;
1152 }
1153 
1154 static int stm8_target_create(struct target *target)
1155 {
1156 
1157  struct stm8_common *stm8 = calloc(1, sizeof(struct stm8_common));
1158 
1161 
1162  return ERROR_OK;
1163 }
1164 
1165 static int stm8_read_core_reg(struct target *target, unsigned int num)
1166 {
1167  uint32_t reg_value;
1168 
1169  /* get pointers to arch-specific information */
1170  struct stm8_common *stm8 = target_to_stm8(target);
1171 
1172  if (num >= STM8_NUM_REGS)
1174 
1175  reg_value = stm8->core_regs[num];
1176  LOG_DEBUG("read core reg %i value 0x%" PRIx32 "", num, reg_value);
1177  buf_set_u32(stm8->core_cache->reg_list[num].value, 0, 32, reg_value);
1178  stm8->core_cache->reg_list[num].valid = true;
1179  stm8->core_cache->reg_list[num].dirty = false;
1180 
1181  return ERROR_OK;
1182 }
1183 
1184 static int stm8_write_core_reg(struct target *target, unsigned int num)
1185 {
1186  uint32_t reg_value;
1187 
1188  /* get pointers to arch-specific information */
1189  struct stm8_common *stm8 = target_to_stm8(target);
1190 
1191  if (num >= STM8_NUM_REGS)
1193 
1194  reg_value = buf_get_u32(stm8->core_cache->reg_list[num].value, 0, 32);
1195  stm8->core_regs[num] = reg_value;
1196  LOG_DEBUG("write core reg %i value 0x%" PRIx32 "", num, reg_value);
1197  stm8->core_cache->reg_list[num].valid = true;
1198  stm8->core_cache->reg_list[num].dirty = false;
1199 
1200  return ERROR_OK;
1201 }
1202 
1203 static const char *stm8_get_gdb_arch(const struct target *target)
1204 {
1205  return "stm8";
1206 }
1207 
1208 static int stm8_get_gdb_reg_list(struct target *target, struct reg **reg_list[],
1209  int *reg_list_size, enum target_register_class reg_class)
1210 {
1211  /* get pointers to arch-specific information */
1212  struct stm8_common *stm8 = target_to_stm8(target);
1213  unsigned int i;
1214 
1215  *reg_list_size = STM8_NUM_REGS;
1216  *reg_list = malloc(sizeof(struct reg *) * (*reg_list_size));
1217 
1218  for (i = 0; i < STM8_NUM_REGS; i++)
1219  (*reg_list)[i] = &stm8->core_cache->reg_list[i];
1220 
1221  return ERROR_OK;
1222 }
1223 
1224 static const struct reg_arch_type stm8_reg_type = {
1226  .set = stm8_set_core_reg,
1227 };
1228 
1230 {
1231  /* get pointers to arch-specific information */
1232  struct stm8_common *stm8 = target_to_stm8(target);
1233 
1234  int num_regs = STM8_NUM_REGS;
1235  struct reg_cache **cache_p = register_get_last_cache_p(&target->reg_cache);
1236  struct reg_cache *cache = malloc(sizeof(struct reg_cache));
1237  struct reg *reg_list = calloc(num_regs, sizeof(struct reg));
1238  struct stm8_core_reg *arch_info = malloc(
1239  sizeof(struct stm8_core_reg) * num_regs);
1240  struct reg_feature *feature;
1241  int i;
1242 
1243  /* Build the process context cache */
1244  cache->name = "stm8 registers";
1245  cache->next = NULL;
1246  cache->reg_list = reg_list;
1247  cache->num_regs = num_regs;
1248  (*cache_p) = cache;
1249  stm8->core_cache = cache;
1250 
1251  for (i = 0; i < num_regs; i++) {
1252  arch_info[i].num = stm8_regs[i].id;
1253  arch_info[i].target = target;
1254 
1255  reg_list[i].name = stm8_regs[i].name;
1256  reg_list[i].size = stm8_regs[i].bits;
1257 
1258  reg_list[i].value = calloc(1, 4);
1259  reg_list[i].valid = false;
1260  reg_list[i].type = &stm8_reg_type;
1261  reg_list[i].arch_info = &arch_info[i];
1262 
1263  reg_list[i].reg_data_type = calloc(1, sizeof(struct reg_data_type));
1264  if (reg_list[i].reg_data_type)
1265  reg_list[i].reg_data_type->type = stm8_regs[i].type;
1266  else {
1267  LOG_ERROR("unable to allocate reg type list");
1268  return NULL;
1269  }
1270 
1271  reg_list[i].dirty = false;
1272  reg_list[i].group = stm8_regs[i].group;
1273  reg_list[i].number = stm8_regs[i].id;
1274  reg_list[i].exist = true;
1275  reg_list[i].caller_save = true; /* gdb defaults to true */
1276 
1277  feature = calloc(1, sizeof(struct reg_feature));
1278  if (feature) {
1279  feature->name = stm8_regs[i].feature;
1280  reg_list[i].feature = feature;
1281  } else
1282  LOG_ERROR("unable to allocate feature list");
1283  }
1284 
1285  return cache;
1286 }
1287 
1288 static void stm8_free_reg_cache(struct target *target)
1289 {
1290  struct stm8_common *stm8 = target_to_stm8(target);
1291  struct reg_cache *cache;
1292  struct reg *reg;
1293  unsigned int i;
1294 
1295  cache = stm8->core_cache;
1296 
1297  if (!cache)
1298  return;
1299 
1300  for (i = 0; i < cache->num_regs; i++) {
1301  reg = &cache->reg_list[i];
1302 
1303  free(reg->feature);
1304  free(reg->reg_data_type);
1305  free(reg->value);
1306  }
1307 
1308  free(cache->reg_list[0].arch_info);
1309  free(cache->reg_list);
1310  free(cache);
1311 
1312  stm8->core_cache = NULL;
1313 }
1314 
1315 static void stm8_deinit(struct target *target)
1316 {
1317  struct stm8_common *stm8 = target_to_stm8(target);
1318 
1319  free(stm8->hw_break_list);
1320 
1322 
1323  free(stm8);
1324 }
1325 
1326 static int stm8_arch_state(struct target *target)
1327 {
1328  struct stm8_common *stm8 = target_to_stm8(target);
1329 
1330  LOG_USER("target halted due to %s, pc: 0x%8.8" PRIx32 "",
1332  buf_get_u32(stm8->core_cache->reg_list[STM8_PC].value, 0, 32));
1333 
1334  return ERROR_OK;
1335 }
1336 
1337 static int stm8_step(struct target *target, bool current,
1338  target_addr_t address, bool handle_breakpoints)
1339 {
1340  LOG_DEBUG("%x " TARGET_ADDR_FMT " %x",
1341  current, address, handle_breakpoints);
1342 
1343  /* get pointers to arch-specific information */
1344  struct stm8_common *stm8 = target_to_stm8(target);
1345  struct breakpoint *breakpoint = NULL;
1346 
1347  if (target->state != TARGET_HALTED) {
1348  LOG_TARGET_ERROR(target, "not halted");
1349  return ERROR_TARGET_NOT_HALTED;
1350  }
1351 
1352  /* current = true: continue on current pc, otherwise continue at <address> */
1353  if (!current) {
1355  stm8->core_cache->reg_list[STM8_PC].dirty = true;
1356  stm8->core_cache->reg_list[STM8_PC].valid = true;
1357  }
1358 
1359  /* the front-end may request us not to handle breakpoints */
1360  if (handle_breakpoints) {
1362  buf_get_u32(stm8->core_cache->reg_list[STM8_PC].value, 0, 32));
1363  if (breakpoint)
1365  }
1366 
1367  /* restore context */
1369 
1370  /* configure single step mode */
1372 
1374 
1376 
1377  /* disable interrupts while stepping */
1378  if (!stm8->enable_step_irq)
1380 
1381  /* exit debug mode */
1383 
1384  /* registers are now invalid */
1386 
1387  LOG_DEBUG("target stepped ");
1389 
1390  if (breakpoint)
1392 
1394 
1395  return ERROR_OK;
1396 }
1397 
1399 {
1401 
1402  /* set any pending breakpoints */
1403  while (breakpoint) {
1404  if (!breakpoint->is_set)
1407  }
1408 }
1409 
1410 static int stm8_set_breakpoint(struct target *target,
1411  struct breakpoint *breakpoint)
1412 {
1413  struct stm8_common *stm8 = target_to_stm8(target);
1414  struct stm8_comparator *comparator_list = stm8->hw_break_list;
1415  int retval;
1416 
1417  if (breakpoint->is_set) {
1418  LOG_WARNING("breakpoint already set");
1419  return ERROR_OK;
1420  }
1421 
1422  if (breakpoint->type == BKPT_HARD) {
1423  int bp_num = 0;
1424 
1425  while (comparator_list[bp_num].used && (bp_num < stm8->num_hw_bpoints))
1426  bp_num++;
1427  if (bp_num >= stm8->num_hw_bpoints) {
1428  LOG_ERROR("Can not find free breakpoint register (bpid: %" PRIu32 ")",
1431  }
1432  breakpoint_hw_set(breakpoint, bp_num);
1433  comparator_list[bp_num].used = true;
1434  comparator_list[bp_num].bp_value = breakpoint->address;
1435  comparator_list[bp_num].type = HWBRK_EXEC;
1436 
1437  retval = stm8_set_hwbreak(target, comparator_list);
1438  if (retval != ERROR_OK)
1439  return retval;
1440 
1441  LOG_DEBUG("bpid: %" PRIu32 ", bp_num %i bp_value 0x%" PRIx32 "",
1443  bp_num, comparator_list[bp_num].bp_value);
1444  } else if (breakpoint->type == BKPT_SOFT) {
1445  LOG_DEBUG("bpid: %" PRIu32, breakpoint->unique_id);
1446  if (breakpoint->length == 1) {
1447  uint8_t verify = 0x55;
1448 
1451  if (retval != ERROR_OK)
1452  return retval;
1454  if (retval != ERROR_OK)
1455  return retval;
1456 
1457  retval = target_read_u8(target, breakpoint->address, &verify);
1458  if (retval != ERROR_OK)
1459  return retval;
1460  if (verify != STM8_BREAK) {
1461  LOG_ERROR("Unable to set breakpoint at address " TARGET_ADDR_FMT
1462  " - check that memory is read/writable",
1463  breakpoint->address);
1465  }
1466  } else {
1468  }
1469  breakpoint->is_set = true;
1470  }
1471 
1472  return ERROR_OK;
1473 }
1474 
1475 static int stm8_add_breakpoint(struct target *target,
1476  struct breakpoint *breakpoint)
1477 {
1478  struct stm8_common *stm8 = target_to_stm8(target);
1479  int ret;
1480 
1481  if (breakpoint->type == BKPT_HARD) {
1482  if (stm8->num_hw_bpoints_avail < 1) {
1483  LOG_INFO("no hardware breakpoint available");
1485  }
1486 
1488  if (ret != ERROR_OK)
1489  return ret;
1490 
1491  stm8->num_hw_bpoints_avail--;
1492  return ERROR_OK;
1493  }
1494 
1496  if (ret != ERROR_OK)
1497  return ret;
1498 
1499  return ERROR_OK;
1500 }
1501 
1503  struct breakpoint *breakpoint)
1504 {
1505  /* get pointers to arch-specific information */
1506  struct stm8_common *stm8 = target_to_stm8(target);
1507  struct stm8_comparator *comparator_list = stm8->hw_break_list;
1508  int retval;
1509 
1510  if (!breakpoint->is_set) {
1511  LOG_WARNING("breakpoint not set");
1512  return ERROR_OK;
1513  }
1514 
1515  if (breakpoint->type == BKPT_HARD) {
1516  int bp_num = breakpoint->number;
1517  if (bp_num >= stm8->num_hw_bpoints) {
1518  LOG_DEBUG("Invalid comparator number in breakpoint (bpid: %" PRIu32 ")",
1520  return ERROR_OK;
1521  }
1522  LOG_DEBUG("bpid: %" PRIu32 " - releasing hw: %d",
1524  bp_num);
1525  comparator_list[bp_num].used = false;
1526  retval = stm8_set_hwbreak(target, comparator_list);
1527  if (retval != ERROR_OK)
1528  return retval;
1529  } else {
1530  /* restore original instruction (kept in target endianness) */
1531  LOG_DEBUG("bpid: %" PRIu32, breakpoint->unique_id);
1532  if (breakpoint->length == 1) {
1533  uint8_t current_instr;
1534 
1535  /* check that user program has not
1536  modified breakpoint instruction */
1537  retval = target_read_memory(target, breakpoint->address, 1, 1,
1538  (uint8_t *)&current_instr);
1539  if (retval != ERROR_OK)
1540  return retval;
1541 
1542  if (current_instr == STM8_BREAK) {
1543  retval = target_write_memory(target, breakpoint->address, 1, 1,
1545  if (retval != ERROR_OK)
1546  return retval;
1547  }
1548  } else
1549  return ERROR_FAIL;
1550  }
1551  breakpoint->is_set = false;
1552 
1553  return ERROR_OK;
1554 }
1555 
1557  struct breakpoint *breakpoint)
1558 {
1559  /* get pointers to arch-specific information */
1560  struct stm8_common *stm8 = target_to_stm8(target);
1561 
1562  if (target->state != TARGET_HALTED) {
1563  LOG_TARGET_ERROR(target, "not halted");
1564  return ERROR_TARGET_NOT_HALTED;
1565  }
1566 
1567  if (breakpoint->is_set)
1569 
1570  if (breakpoint->type == BKPT_HARD)
1571  stm8->num_hw_bpoints_avail++;
1572 
1573  return ERROR_OK;
1574 }
1575 
1576 static int stm8_set_watchpoint(struct target *target,
1577  struct watchpoint *watchpoint)
1578 {
1579  struct stm8_common *stm8 = target_to_stm8(target);
1580  struct stm8_comparator *comparator_list = stm8->hw_break_list;
1581  int wp_num = 0;
1582  int ret;
1583 
1584  if (watchpoint->is_set) {
1585  LOG_WARNING("watchpoint already set");
1586  return ERROR_OK;
1587  }
1588 
1589  while (comparator_list[wp_num].used && (wp_num < stm8->num_hw_bpoints))
1590  wp_num++;
1591  if (wp_num >= stm8->num_hw_bpoints) {
1592  LOG_ERROR("Can not find free hw breakpoint");
1594  }
1595 
1596  if (watchpoint->length != 1) {
1597  LOG_ERROR("Only watchpoints of length 1 are supported");
1599  }
1600 
1601  enum hw_break_type enable = 0;
1602 
1603  switch (watchpoint->rw) {
1604  case WPT_READ:
1605  enable = HWBRK_RD;
1606  break;
1607  case WPT_WRITE:
1608  enable = HWBRK_WR;
1609  break;
1610  case WPT_ACCESS:
1611  enable = HWBRK_ACC;
1612  break;
1613  default:
1614  LOG_ERROR("BUG: watchpoint->rw neither read, write nor access");
1615  }
1616 
1617  comparator_list[wp_num].used = true;
1618  comparator_list[wp_num].bp_value = watchpoint->address;
1619  comparator_list[wp_num].type = enable;
1620 
1621  ret = stm8_set_hwbreak(target, comparator_list);
1622  if (ret != ERROR_OK) {
1623  comparator_list[wp_num].used = false;
1624  return ret;
1625  }
1626 
1627  watchpoint_set(watchpoint, wp_num);
1628 
1629  LOG_DEBUG("wp_num %i bp_value 0x%" PRIx32 "",
1630  wp_num,
1631  comparator_list[wp_num].bp_value);
1632 
1633  return ERROR_OK;
1634 }
1635 
1636 static int stm8_add_watchpoint(struct target *target,
1637  struct watchpoint *watchpoint)
1638 {
1639  int ret;
1640  struct stm8_common *stm8 = target_to_stm8(target);
1641 
1642  if (stm8->num_hw_bpoints_avail < 1) {
1643  LOG_INFO("no hardware watchpoints available");
1645  }
1646 
1648  if (ret != ERROR_OK)
1649  return ret;
1650 
1651  stm8->num_hw_bpoints_avail--;
1652  return ERROR_OK;
1653 }
1654 
1656 {
1658 
1659  /* set any pending watchpoints */
1660  while (watchpoint) {
1661  if (!watchpoint->is_set)
1664  }
1665 }
1666 
1668  struct watchpoint *watchpoint)
1669 {
1670  /* get pointers to arch-specific information */
1671  struct stm8_common *stm8 = target_to_stm8(target);
1672  struct stm8_comparator *comparator_list = stm8->hw_break_list;
1673 
1674  if (!watchpoint->is_set) {
1675  LOG_WARNING("watchpoint not set");
1676  return ERROR_OK;
1677  }
1678 
1679  int wp_num = watchpoint->number;
1680  if (wp_num >= stm8->num_hw_bpoints) {
1681  LOG_DEBUG("Invalid hw comparator number in watchpoint");
1682  return ERROR_OK;
1683  }
1684  comparator_list[wp_num].used = false;
1685  watchpoint->is_set = false;
1686 
1687  stm8_set_hwbreak(target, comparator_list);
1688 
1689  return ERROR_OK;
1690 }
1691 
1693  struct watchpoint *watchpoint)
1694 {
1695  /* get pointers to arch-specific information */
1696  struct stm8_common *stm8 = target_to_stm8(target);
1697 
1698  if (target->state != TARGET_HALTED) {
1699  LOG_TARGET_ERROR(target, "not halted");
1700  return ERROR_TARGET_NOT_HALTED;
1701  }
1702 
1703  if (watchpoint->is_set)
1705 
1706  stm8->num_hw_bpoints_avail++;
1707 
1708  return ERROR_OK;
1709 }
1710 
1711 static int stm8_examine(struct target *target)
1712 {
1713  int retval;
1714  uint8_t csr1, csr2;
1715  /* get pointers to arch-specific information */
1716  struct stm8_common *stm8 = target_to_stm8(target);
1718 
1719  if (!target_was_examined(target)) {
1720  if (!stm8->swim_configured) {
1721  stm8->swim_configured = true;
1722  /*
1723  Now is the time to deassert reset if connect_under_reset.
1724  Releasing reset line will cause the option bytes to load.
1725  The core will still be stalled.
1726  */
1730  else
1731  LOG_WARNING("\'srst_nogate\' reset_config option is required");
1732  }
1733  } else {
1734  LOG_INFO("trying to reconnect");
1735 
1736  retval = swim_reconnect();
1737  if (retval != ERROR_OK) {
1738  LOG_ERROR("reconnect failed");
1739  return ERROR_FAIL;
1740  }
1741 
1742  /* read dm_csrx control regs */
1743  retval = stm8_read_dm_csrx(target, &csr1, &csr2);
1744  if (retval != ERROR_OK) {
1745  LOG_ERROR("state query failed");
1746  return ERROR_FAIL;
1747  }
1748  }
1749 
1751 
1752  return ERROR_OK;
1753  }
1754 
1755  return ERROR_OK;
1756 }
1757 
1760  struct target_memory_check_block *blocks, int num_blocks, uint8_t erased_value)
1761 {
1762  struct working_area *erase_check_algorithm;
1763  struct reg_param reg_params[2];
1764  struct mem_param mem_params[2];
1765  struct stm8_algorithm stm8_info;
1766 
1767  static const uint8_t stm8_erase_check_code[] = {
1768 #include "../../contrib/loaders/erase_check/stm8_erase_check.inc"
1769  };
1770 
1771  if (erased_value != 0xff) {
1772  LOG_ERROR("Erase value 0x%02" PRIx8 " not yet supported for STM8",
1773  erased_value);
1774  return ERROR_FAIL;
1775  }
1776 
1777  /* make sure we have a working area */
1778  if (target_alloc_working_area(target, sizeof(stm8_erase_check_code),
1779  &erase_check_algorithm) != ERROR_OK)
1781 
1782  target_write_buffer(target, erase_check_algorithm->address,
1783  sizeof(stm8_erase_check_code), stm8_erase_check_code);
1784 
1785  stm8_info.common_magic = STM8_COMMON_MAGIC;
1786 
1787  init_mem_param(&mem_params[0], 0x0, 3, PARAM_OUT);
1788  buf_set_u32(mem_params[0].value, 0, 24, blocks[0].address);
1789 
1790  init_mem_param(&mem_params[1], 0x3, 3, PARAM_OUT);
1791  buf_set_u32(mem_params[1].value, 0, 24, blocks[0].size);
1792 
1793  init_reg_param(&reg_params[0], "a", 32, PARAM_IN_OUT);
1794  buf_set_u32(reg_params[0].value, 0, 32, erased_value);
1795 
1796  init_reg_param(&reg_params[1], "sp", 32, PARAM_OUT);
1797  buf_set_u32(reg_params[1].value, 0, 32, erase_check_algorithm->address);
1798 
1799  int retval = target_run_algorithm(target, 2, mem_params, 2, reg_params,
1800  erase_check_algorithm->address + 6,
1801  erase_check_algorithm->address + (sizeof(stm8_erase_check_code) - 1),
1802  10000, &stm8_info);
1803 
1804  if (retval == ERROR_OK)
1805  blocks[0].result = (*(reg_params[0].value) == 0xff);
1806 
1807  destroy_mem_param(&mem_params[0]);
1808  destroy_mem_param(&mem_params[1]);
1809  destroy_reg_param(&reg_params[0]);
1810  destroy_reg_param(&reg_params[1]);
1811 
1812  target_free_working_area(target, erase_check_algorithm);
1813 
1814  if (retval != ERROR_OK)
1815  return retval;
1816 
1817  return 1; /* only one block has been checked */
1818 }
1819 
1821  uint32_t count, uint32_t *checksum)
1822 {
1823  /* let image_calculate_checksum() take care of business */
1825 }
1826 
1827 /* run to exit point. return error if exit point was not reached. */
1828 static int stm8_run_and_wait(struct target *target, uint32_t entry_point,
1829  unsigned int timeout_ms, uint32_t exit_point, struct stm8_common *stm8)
1830 {
1831  uint32_t pc;
1832  int retval;
1833  /* This code relies on the target specific resume() and
1834  poll()->debug_entry() sequence to write register values to the
1835  processor and the read them back */
1836  retval = target_resume(target, false, entry_point, false, true);
1837  if (retval != ERROR_OK)
1838  return retval;
1839 
1840  retval = target_wait_state(target, TARGET_HALTED, timeout_ms);
1841  /* If the target fails to halt due to the breakpoint, force a halt */
1842  if (retval != ERROR_OK || target->state != TARGET_HALTED) {
1843  retval = target_halt(target);
1844  if (retval != ERROR_OK)
1845  return retval;
1846  retval = target_wait_state(target, TARGET_HALTED, 500);
1847  if (retval != ERROR_OK)
1848  return retval;
1849  return ERROR_TARGET_TIMEOUT;
1850  }
1851 
1852  pc = buf_get_u32(stm8->core_cache->reg_list[STM8_PC].value, 0, 32);
1853  if (exit_point && (pc != exit_point)) {
1854  LOG_DEBUG("failed algorithm halted at 0x%" PRIx32 " ", pc);
1855  return ERROR_TARGET_TIMEOUT;
1856  }
1857 
1858  return ERROR_OK;
1859 }
1860 
1861 static int stm8_run_algorithm(struct target *target, int num_mem_params,
1862  struct mem_param *mem_params, int num_reg_params,
1863  struct reg_param *reg_params, target_addr_t entry_point,
1864  target_addr_t exit_point, unsigned int timeout_ms, void *arch_info)
1865 {
1866  struct stm8_common *stm8 = target_to_stm8(target);
1867 
1868  uint32_t context[STM8_NUM_REGS];
1869  int retval = ERROR_OK;
1870 
1871  LOG_DEBUG("Running algorithm");
1872 
1873  /* NOTE: stm8_run_algorithm requires that each
1874  algorithm uses a software breakpoint
1875  at the exit point */
1876 
1877  if (stm8->common_magic != STM8_COMMON_MAGIC) {
1878  LOG_ERROR("current target isn't a STM8 target");
1879  return ERROR_TARGET_INVALID;
1880  }
1881 
1882  if (target->state != TARGET_HALTED) {
1883  LOG_WARNING("target not halted");
1884  return ERROR_TARGET_NOT_HALTED;
1885  }
1886 
1887  /* refresh core register cache */
1888  for (unsigned int i = 0; i < STM8_NUM_REGS; i++) {
1889  if (!stm8->core_cache->reg_list[i].valid)
1890  stm8->read_core_reg(target, i);
1891  context[i] = buf_get_u32(stm8->core_cache->reg_list[i].value, 0, 32);
1892  }
1893 
1894  for (int i = 0; i < num_mem_params; i++) {
1895  if (mem_params[i].direction == PARAM_IN)
1896  continue;
1897  retval = target_write_buffer(target, mem_params[i].address,
1898  mem_params[i].size, mem_params[i].value);
1899  if (retval != ERROR_OK)
1900  return retval;
1901  }
1902 
1903  for (int i = 0; i < num_reg_params; i++) {
1904  if (reg_params[i].direction == PARAM_IN)
1905  continue;
1906 
1907  struct reg *reg = register_get_by_name(stm8->core_cache,
1908  reg_params[i].reg_name, false);
1909 
1910  if (!reg) {
1911  LOG_ERROR("BUG: register '%s' not found", reg_params[i].reg_name);
1913  }
1914 
1915  if (reg_params[i].size != 32) {
1916  LOG_ERROR("BUG: register '%s' size doesn't match reg_params[i].size",
1917  reg_params[i].reg_name);
1919  }
1920 
1921  stm8_set_core_reg(reg, reg_params[i].value);
1922  }
1923 
1924  retval = stm8_run_and_wait(target, entry_point,
1925  timeout_ms, exit_point, stm8);
1926 
1927  if (retval != ERROR_OK)
1928  return retval;
1929 
1930  for (int i = 0; i < num_mem_params; i++) {
1931  if (mem_params[i].direction != PARAM_OUT) {
1932  retval = target_read_buffer(target, mem_params[i].address,
1933  mem_params[i].size, mem_params[i].value);
1934  if (retval != ERROR_OK)
1935  return retval;
1936  }
1937  }
1938 
1939  for (int i = 0; i < num_reg_params; i++) {
1940  if (reg_params[i].direction != PARAM_OUT) {
1941  struct reg *reg = register_get_by_name(stm8->core_cache,
1942  reg_params[i].reg_name, false);
1943  if (!reg) {
1944  LOG_ERROR("BUG: register '%s' not found",
1945  reg_params[i].reg_name);
1947  }
1948 
1949  if (reg_params[i].size != 32) {
1950  LOG_ERROR("BUG: register '%s' size doesn't match reg_params[i].size",
1951  reg_params[i].reg_name);
1953  }
1954 
1955  buf_set_u32(reg_params[i].value,
1956  0, 32, buf_get_u32(reg->value, 0, 32));
1957  }
1958  }
1959 
1960  /* restore everything we saved before */
1961  for (unsigned int i = 0; i < STM8_NUM_REGS; i++) {
1962  uint32_t regvalue;
1963  regvalue = buf_get_u32(stm8->core_cache->reg_list[i].value, 0, 32);
1964  if (regvalue != context[i]) {
1965  LOG_DEBUG("restoring register %s with value 0x%8.8" PRIx32,
1966  stm8->core_cache->reg_list[i].name, context[i]);
1968  0, 32, context[i]);
1969  stm8->core_cache->reg_list[i].valid = true;
1970  stm8->core_cache->reg_list[i].dirty = true;
1971  }
1972  }
1973 
1974  return ERROR_OK;
1975 }
1976 
1977 static int stm8_jim_configure(struct target *target, struct jim_getopt_info *goi)
1978 {
1979  struct stm8_common *stm8 = target_to_stm8(target);
1980  jim_wide w;
1981  int e;
1982  const char *arg;
1983 
1984  arg = Jim_GetString(goi->argv[0], NULL);
1985  if (!strcmp(arg, "-blocksize")) {
1986  e = jim_getopt_string(goi, &arg, NULL);
1987  if (e != JIM_OK)
1988  return e;
1989 
1990  if (goi->argc == 0) {
1991  Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv,
1992  "-blocksize ?bytes? ...");
1993  return JIM_ERR;
1994  }
1995 
1996  e = jim_getopt_wide(goi, &w);
1997  if (e != JIM_OK)
1998  return e;
1999 
2000  stm8->blocksize = w;
2001  LOG_DEBUG("blocksize=%8.8" PRIx32, stm8->blocksize);
2002  return JIM_OK;
2003  }
2004  if (!strcmp(arg, "-flashstart")) {
2005  e = jim_getopt_string(goi, &arg, NULL);
2006  if (e != JIM_OK)
2007  return e;
2008 
2009  if (goi->argc == 0) {
2010  Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv,
2011  "-flashstart ?address? ...");
2012  return JIM_ERR;
2013  }
2014 
2015  e = jim_getopt_wide(goi, &w);
2016  if (e != JIM_OK)
2017  return e;
2018 
2019  stm8->flashstart = w;
2020  LOG_DEBUG("flashstart=%8.8" PRIx32, stm8->flashstart);
2021  return JIM_OK;
2022  }
2023  if (!strcmp(arg, "-flashend")) {
2024  e = jim_getopt_string(goi, &arg, NULL);
2025  if (e != JIM_OK)
2026  return e;
2027 
2028  if (goi->argc == 0) {
2029  Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv,
2030  "-flashend ?address? ...");
2031  return JIM_ERR;
2032  }
2033 
2034  e = jim_getopt_wide(goi, &w);
2035  if (e != JIM_OK)
2036  return e;
2037 
2038  stm8->flashend = w;
2039  LOG_DEBUG("flashend=%8.8" PRIx32, stm8->flashend);
2040  return JIM_OK;
2041  }
2042  if (!strcmp(arg, "-eepromstart")) {
2043  e = jim_getopt_string(goi, &arg, NULL);
2044  if (e != JIM_OK)
2045  return e;
2046 
2047  if (goi->argc == 0) {
2048  Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv,
2049  "-eepromstart ?address? ...");
2050  return JIM_ERR;
2051  }
2052 
2053  e = jim_getopt_wide(goi, &w);
2054  if (e != JIM_OK)
2055  return e;
2056 
2057  stm8->eepromstart = w;
2058  LOG_DEBUG("eepromstart=%8.8" PRIx32, stm8->eepromstart);
2059  return JIM_OK;
2060  }
2061  if (!strcmp(arg, "-eepromend")) {
2062  e = jim_getopt_string(goi, &arg, NULL);
2063  if (e != JIM_OK)
2064  return e;
2065 
2066  if (goi->argc == 0) {
2067  Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv,
2068  "-eepromend ?address? ...");
2069  return JIM_ERR;
2070  }
2071 
2072  e = jim_getopt_wide(goi, &w);
2073  if (e != JIM_OK)
2074  return e;
2075 
2076  stm8->eepromend = w;
2077  LOG_DEBUG("eepromend=%8.8" PRIx32, stm8->eepromend);
2078  return JIM_OK;
2079  }
2080  if (!strcmp(arg, "-optionstart")) {
2081  e = jim_getopt_string(goi, &arg, NULL);
2082  if (e != JIM_OK)
2083  return e;
2084 
2085  if (goi->argc == 0) {
2086  Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv,
2087  "-optionstart ?address? ...");
2088  return JIM_ERR;
2089  }
2090 
2091  e = jim_getopt_wide(goi, &w);
2092  if (e != JIM_OK)
2093  return e;
2094 
2095  stm8->optionstart = w;
2096  LOG_DEBUG("optionstart=%8.8" PRIx32, stm8->optionstart);
2097  return JIM_OK;
2098  }
2099  if (!strcmp(arg, "-optionend")) {
2100  e = jim_getopt_string(goi, &arg, NULL);
2101  if (e != JIM_OK)
2102  return e;
2103 
2104  if (goi->argc == 0) {
2105  Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv,
2106  "-optionend ?address? ...");
2107  return JIM_ERR;
2108  }
2109 
2110  e = jim_getopt_wide(goi, &w);
2111  if (e != JIM_OK)
2112  return e;
2113 
2114  stm8->optionend = w;
2115  LOG_DEBUG("optionend=%8.8" PRIx32, stm8->optionend);
2116  return JIM_OK;
2117  }
2118  if (!strcmp(arg, "-enable_step_irq")) {
2119  e = jim_getopt_string(goi, &arg, NULL);
2120  if (e != JIM_OK)
2121  return e;
2122 
2123  stm8->enable_step_irq = true;
2124  LOG_DEBUG("enable_step_irq=%8.8x", stm8->enable_step_irq);
2125  return JIM_OK;
2126  }
2127  if (!strcmp(arg, "-enable_stm8l")) {
2128  e = jim_getopt_string(goi, &arg, NULL);
2129  if (e != JIM_OK)
2130  return e;
2131 
2132  stm8->enable_stm8l = true;
2133  LOG_DEBUG("enable_stm8l=%8.8x", stm8->enable_stm8l);
2134  stm8_init_flash_regs(stm8->enable_stm8l, stm8);
2135  return JIM_OK;
2136  }
2137  return JIM_CONTINUE;
2138 }
2139 
2140 COMMAND_HANDLER(stm8_handle_enable_step_irq_command)
2141 {
2142  const char *msg;
2144  struct stm8_common *stm8 = target_to_stm8(target);
2145  bool enable = stm8->enable_step_irq;
2146 
2147  if (CMD_ARGC > 0) {
2148  COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
2149  stm8->enable_step_irq = enable;
2150  }
2151  msg = stm8->enable_step_irq ? "enabled" : "disabled";
2152  command_print(CMD, "enable_step_irq = %s", msg);
2153  return ERROR_OK;
2154 }
2155 
2156 COMMAND_HANDLER(stm8_handle_enable_stm8l_command)
2157 {
2158  const char *msg;
2160  struct stm8_common *stm8 = target_to_stm8(target);
2161  bool enable = stm8->enable_stm8l;
2162 
2163  if (CMD_ARGC > 0) {
2164  COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
2165  stm8->enable_stm8l = enable;
2166  }
2167  msg = stm8->enable_stm8l ? "enabled" : "disabled";
2168  command_print(CMD, "enable_stm8l = %s", msg);
2169  stm8_init_flash_regs(stm8->enable_stm8l, stm8);
2170  return ERROR_OK;
2171 }
2172 
2173 static const struct command_registration stm8_exec_command_handlers[] = {
2174  {
2175  .name = "enable_step_irq",
2176  .handler = stm8_handle_enable_step_irq_command,
2177  .mode = COMMAND_ANY,
2178  .help = "Enable/disable irq handling during step",
2179  .usage = "[1/0]",
2180  },
2181  {
2182  .name = "enable_stm8l",
2183  .handler = stm8_handle_enable_stm8l_command,
2184  .mode = COMMAND_ANY,
2185  .help = "Enable/disable STM8L flash programming",
2186  .usage = "[1/0]",
2187  },
2189 };
2190 
2191 static const struct command_registration stm8_command_handlers[] = {
2192  {
2193  .name = "stm8",
2194  .mode = COMMAND_ANY,
2195  .help = "stm8 command group",
2196  .usage = "",
2197  .chain = stm8_exec_command_handlers,
2198  },
2200 };
2201 
2202 struct target_type stm8_target = {
2203  .name = "stm8",
2204 
2205  .poll = stm8_poll,
2206  .arch_state = stm8_arch_state,
2207 
2208  .halt = stm8_halt,
2209  .resume = stm8_resume,
2210  .step = stm8_step,
2211 
2212  .assert_reset = stm8_reset_assert,
2213  .deassert_reset = stm8_reset_deassert,
2214 
2215  .get_gdb_arch = stm8_get_gdb_arch,
2216  .get_gdb_reg_list = stm8_get_gdb_reg_list,
2217 
2218  .read_memory = stm8_read_memory,
2219  .write_memory = stm8_write_memory,
2220  .checksum_memory = stm8_checksum_memory,
2221  .blank_check_memory = stm8_blank_check_memory,
2222 
2223  .run_algorithm = stm8_run_algorithm,
2224 
2225  .add_breakpoint = stm8_add_breakpoint,
2226  .remove_breakpoint = stm8_remove_breakpoint,
2227  .add_watchpoint = stm8_add_watchpoint,
2228  .remove_watchpoint = stm8_remove_watchpoint,
2229 
2230  .commands = stm8_command_handlers,
2231  .target_create = stm8_target_create,
2232  .init_target = stm8_init,
2233  .examine = stm8_examine,
2234 
2235  .deinit_target = stm8_deinit,
2236  .target_jim_configure = stm8_jim_configure,
2237 };
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:489
@ 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:83
static void breakpoint_hw_set(struct breakpoint *breakpoint, unsigned int hw_number)
Definition: breakpoints.h:66
@ 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:376
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:141
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:156
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:402
#define ERROR_COMMAND_NOTFOUND
Definition: command.h:403
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:151
#define COMMAND_PARSE_ENABLE(in, out)
parses an enable/disable command argument
Definition: command.h:533
#define CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
Definition: command.h:146
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:253
@ 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:120
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:1912
enum reset_types jtag_get_reset_config(void)
Definition: jtag/core.c:1747
int adapter_assert_reset(void)
Definition: jtag/core.c:1892
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 @110 regs[]
#define LOG_USER(expr ...)
Definition: log.h:136
#define LOG_WARNING(expr ...)
Definition: log.h:130
#define ERROR_FAIL
Definition: log.h:174
#define LOG_TARGET_ERROR(target, fmt_str,...)
Definition: log.h:162
#define LOG_ERROR(expr ...)
Definition: log.h:133
#define LOG_INFO(expr ...)
Definition: log.h:127
#define LOG_DEBUG(expr ...)
Definition: log.h:110
#define ERROR_OK
Definition: log.h:168
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:1127
static int stm8_checksum_memory(struct target *target, target_addr_t address, uint32_t count, uint32_t *checksum)
Definition: stm8.c:1820
static int stm8_arch_state(struct target *target)
Definition: stm8.c:1326
static int stm8_examine(struct target *target)
Definition: stm8.c:1711
#define DM_REG_Y
Definition: stm8.c:72
static const struct command_registration stm8_command_handlers[]
Definition: stm8.c:2191
static const char * stm8_get_gdb_arch(const struct target *target)
Definition: stm8.c:1203
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:1398
#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:1556
#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:35
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 int stm8_write_core_reg(struct target *target, unsigned int num)
Definition: stm8.c:1184
static int stm8_unset_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: stm8.c:1502
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:1337
static int stm8_unset_watchpoint(struct target *target, struct watchpoint *watchpoint)
Definition: stm8.c:1667
static int stm8_set_watchpoint(struct target *target, struct watchpoint *watchpoint)
Definition: stm8.c:1576
#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:2140
#define FLASH_DUKR_STM8L
Definition: stm8.c:106
static int stm8_read_core_reg(struct target *target, unsigned int num)
Definition: stm8.c:1165
struct target_type stm8_target
Definition: stm8.c:2202
#define DM_CR1
Definition: stm8.c:78
static int stm8_blank_check_memory(struct target *target, struct target_memory_check_block *blocks, int num_blocks, uint8_t erased_value)
Checks whether a memory region is erased.
Definition: stm8.c:1759
static int stm8_poll(struct target *target)
Definition: stm8.c:867
static int stm8_restore_context(struct target *target)
Definition: stm8.c:634
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:1028
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:692
static int stm8_init(struct command_context *cmd_ctx, struct target *target)
Definition: stm8.c:851
static int stm8_add_watchpoint(struct target *target, struct watchpoint *watchpoint)
Definition: stm8.c:1636
#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:1977
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:1410
#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:1229
#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:833
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:1861
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:1288
#define DUL
Definition: stm8.c:111
static int stm8_reset_assert(struct target *target)
Definition: stm8.c:945
const uint8_t bits
Definition: stm8.c:41
static int stm8_add_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: stm8.c:1475
static int stm8_halt(struct target *target)
Definition: stm8.c:915
static const struct command_registration stm8_exec_command_handlers[]
Definition: stm8.c:2173
#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:672
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:1208
static void stm8_enable_watchpoints(struct target *target)
Definition: stm8.c:1655
static void stm8_deinit(struct target *target)
Definition: stm8.c:1315
#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:652
static int stm8_set_core_reg(struct reg *reg, uint8_t *buf)
Definition: stm8.c:600
#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:1224
#define RST
Definition: stm8.c:85
static int stm8_save_context(struct target *target)
Definition: stm8.c:616
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:813
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:1828
#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:1107
struct adapter_driver * adapter_driver
Definition: adapter.c:27
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:985
#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:1009
#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
static const struct @123 stm8_regs[]
#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:779
static int stm8_target_create(struct target *target)
Definition: stm8.c:1154
#define SWIM_DM
Definition: stm8.c:126
static int stm8_remove_watchpoint(struct target *target, struct watchpoint *watchpoint)
Definition: stm8.c:1692
Represents a driver for a debugging interface.
Definition: interface.h:208
int(* speed)(int speed)
Set the interface speed.
Definition: interface.h:271
const char *const name
The name of the interface driver.
Definition: interface.h:210
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:235
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:116
struct jtag_tap * tap
Definition: target.h:119
enum target_debug_reason debug_reason
Definition: target.h:154
enum target_state state
Definition: target.h:157
enum target_endianness endianness
Definition: target.h:155
struct reg_cache * reg_cache
Definition: target.h:158
struct breakpoint * breakpoints
Definition: target.h:159
struct watchpoint * watchpoints
Definition: target.h:160
void * arch_info
Definition: target.h:164
bool reset_halt
Definition: target.h:144
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:86
int swim_read_mem(uint32_t addr, uint32_t size, uint32_t count, uint8_t *buffer)
Definition: swim.c:29
int swim_reconnect(void)
Definition: swim.c:45
int swim_system_reset(void)
Definition: swim.c:22
int swim_write_mem(uint32_t addr, uint32_t size, uint32_t count, const uint8_t *buffer)
Definition: swim.c:37
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:1773
void target_free_all_working_areas(struct target *target)
Definition: target.c:2159
int target_halt(struct target *target)
Definition: target.c:515
int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
Definition: target.c:2350
int target_write_u8(struct target *target, target_addr_t address, uint8_t value)
Definition: target.c:2691
int target_read_buffer(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
Definition: target.c:2415
int target_read_u8(struct target *target, target_addr_t address, uint8_t *value)
Definition: target.c:2606
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:782
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:1274
int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
Definition: target.c:2069
const char * target_state_name(const struct target *t)
Return the name of this targets current state.
Definition: target.c:268
int target_free_working_area(struct target *target, struct working_area *area)
Free a working area.
Definition: target.c:2127
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:564
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:1246
const char * debug_reason_name(const struct target *t)
Definition: target.c:255
int target_wait_state(struct target *target, enum target_state state, unsigned int ms)
Definition: target.c:3221
struct target * get_current_target(struct command_context *cmd_ctx)
Definition: target.c:466
@ DBG_REASON_UNDEFINED
Definition: target.h:77
@ DBG_REASON_NOTHALTED
Definition: target.h:74
@ DBG_REASON_DBGRQ
Definition: target.h:69
@ DBG_REASON_SINGLESTEP
Definition: target.h:73
@ DBG_REASON_BREAKPOINT
Definition: target.h:70
target_register_class
Definition: target.h:110
#define ERROR_TARGET_NOT_HALTED
Definition: target.h:783
static bool target_was_examined(const struct target *target)
Definition: target.h:429
#define ERROR_TARGET_UNALIGNED_ACCESS
Definition: target.h:785
#define ERROR_TARGET_INVALID
Definition: target.h:780
@ TARGET_EVENT_DEBUG_RESUMED
Definition: target.h:272
@ TARGET_EVENT_HALTED
Definition: target.h:252
@ TARGET_EVENT_RESUMED
Definition: target.h:253
@ TARGET_EVENT_DEBUG_HALTED
Definition: target.h:271
@ TARGET_RESET
Definition: target.h:57
@ TARGET_DEBUG_RUNNING
Definition: target.h:58
@ TARGET_UNKNOWN
Definition: target.h:54
@ TARGET_HALTED
Definition: target.h:56
@ TARGET_RUNNING
Definition: target.h:55
@ TARGET_BIG_ENDIAN
Definition: target.h:82
#define ERROR_TARGET_TIMEOUT
Definition: target.h:782
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
Definition: target.h:787
static void target_set_examined(struct target *target)
Sets the examined flag for the given target.
Definition: target.h:436
#define ERROR_TARGET_FAILURE
Definition: target.h:784
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:342
uint64_t target_addr_t
Definition: types.h:335
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:340
#define NULL
Definition: usb.h:16
uint8_t count[4]
Definition: vdebug.c:22