+/*
+ * DWT_COMP0 0xE0001020
+ * DWT_MASK0 0xE0001024
+ * DWT_FUNCTION0 0xE0001028
+ * DWT_COMP1 0xE0001030
+ * DWT_MASK1 0xE0001034
+ * DWT_FUNCTION1 0xE0001038
+ * DWT_COMP2 0xE0001040
+ * DWT_MASK2 0xE0001044
+ * DWT_FUNCTION2 0xE0001048
+ * DWT_COMP3 0xE0001050
+ * DWT_MASK3 0xE0001054
+ * DWT_FUNCTION3 0xE0001058
+ */
+
+#define DATA_WATCH_NUM 4
+
+enum watchfun { WATCHDISABLED = 0, WATCHREAD = 5, WATCHWRITE = 6, WATCHACCESS = 7 };
+
+struct code_hw_watchpoint {
+ stm32_addr_t addr;
+ uint8_t mask;
+ enum watchfun fun;
+};
+
+struct code_hw_watchpoint data_watches[DATA_WATCH_NUM];
+
+static void init_data_watchpoints(struct stlink *sl) {
+ int i;
+
+ #ifdef DEBUG
+ printf("init watchpoints\n");
+ #endif
+
+ // set trcena in debug command to turn on dwt unit
+ stlink_read_mem32(sl, 0xE000EDFC, 4);
+ sl->q_buf[3] |= 1;
+ stlink_write_mem32(sl, 0xE000EDFC, 4);
+
+ // make sure all watchpoints are cleared
+ memset(sl->q_buf, 0, 4);
+ for(int i = 0; i < DATA_WATCH_NUM; i++) {
+ data_watches[i].fun = WATCHDISABLED;
+ stlink_write_mem32(sl, 0xe0001028 + i * 16, 4);
+ }
+}
+
+static int add_data_watchpoint(struct stlink* sl, enum watchfun wf, stm32_addr_t addr, unsigned int len)
+{
+ int i = 0;
+ uint32_t mask, temp;
+
+ // computer mask
+ // find a free watchpoint
+ // configure
+
+ mask = -1;
+ i = len;
+ while(i) {
+ i >>= 1;
+ mask++;
+ }
+
+ if((mask != -1) && (mask < 16)) {
+ for(i = 0; i < DATA_WATCH_NUM; i++) {
+ // is this an empty slot ?
+ if(data_watches[i].fun == WATCHDISABLED) {
+ #ifdef DEBUG
+ printf("insert watchpoint %d addr %x wf %u mask %u len %d\n", i, addr, wf, mask, len);
+ #endif
+
+ data_watches[i].fun = wf;
+ data_watches[i].addr = addr;
+ data_watches[i].mask = mask;
+
+ // insert comparator address
+ sl->q_buf[0] = (addr & 0xff);
+ sl->q_buf[1] = ((addr >> 8) & 0xff);
+ sl->q_buf[2] = ((addr >> 16) & 0xff);
+ sl->q_buf[3] = ((addr >> 24) & 0xff);
+
+ stlink_write_mem32(sl, 0xE0001020 + i * 16, 4);
+
+ // insert mask
+ memset(sl->q_buf, 0, 4);
+ sl->q_buf[0] = mask;
+ stlink_write_mem32(sl, 0xE0001024 + i * 16, 4);
+
+ // insert function
+ memset(sl->q_buf, 0, 4);
+ sl->q_buf[0] = wf;
+ stlink_write_mem32(sl, 0xE0001028 + i * 16, 4);
+
+ // just to make sure the matched bit is clear !
+ stlink_read_mem32(sl, 0xE0001028 + i * 16, 4);
+ return 0;
+ }
+ }
+ }
+
+ #ifdef DEBUG
+ printf("failure: add watchpoints addr %x wf %u len %u\n", addr, wf, len);
+ #endif
+ return -1;
+}
+
+static int delete_data_watchpoint(struct stlink* sl, stm32_addr_t addr)
+{
+ int i;
+
+ for(i = 0 ; i < DATA_WATCH_NUM; i++) {
+ if((data_watches[i].addr == addr) && (data_watches[i].fun != WATCHDISABLED)) {
+ #ifdef DEBUG
+ printf("delete watchpoint %d addr %x\n", i, addr);
+ #endif
+
+ memset(sl->q_buf, 0, 4);
+ data_watches[i].fun = WATCHDISABLED;
+ stlink_write_mem32(sl, 0xe0001028 + i * 16, 4);
+
+ return 0;
+ }
+ }
+
+ #ifdef DEBUG
+ printf("failure: delete watchpoint addr %x\n", addr);
+ #endif
+
+ return -1;
+}
+
+#define CODE_BREAK_NUM 6