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