[merge] karl/uwe_usb branch
[fw/stlink] / src / stlink-common.c
index e3d6631d45ca19fd5c4af486aaa32e147e09eb22..707316cc988051daf4bd4085a486870f0084db65 100644 (file)
@@ -258,7 +258,6 @@ static void disable_flash_read_protection(stlink_t *sl) {
 void stlink_close(stlink_t *sl) {
     D(sl, "\n*** stlink_close ***\n");
     sl->backend->close(sl);
-
     free(sl);
 }
 
@@ -272,23 +271,51 @@ void stlink_enter_swd_mode(stlink_t *sl) {
     sl->backend->enter_swd_mode(sl);
 }
 
+// Force the core into the debug mode -> halted state.
+void stlink_force_debug(stlink_t *sl) {
+    D(sl, "\n*** stlink_force_debug_mode ***\n");
+    sl->backend->force_debug(sl);
+}
+
 void stlink_exit_dfu_mode(stlink_t *sl) {
     D(sl, "\n*** stlink_exit_dfu_mode ***\n");
     sl->backend->exit_dfu_mode(sl);
 }
 
-void stlink_core_id(stlink_t *sl) {
+uint32_t stlink_core_id(stlink_t *sl) {
     D(sl, "\n*** stlink_core_id ***\n");
     sl->backend->core_id(sl);
     if (sl->verbose > 2)
         stlink_print_data(sl);
     DD(sl, "core_id = 0x%08x\n", sl->core_id);
+    return sl->core_id;
+}
+
+uint16_t stlink_chip_id(stlink_t *sl) {
+    stlink_read_mem32(sl, 0xE0042000, 4);
+    uint32_t chip_id = sl->q_buf[0] | (sl->q_buf[1] << 8) | (sl->q_buf[2] << 16) |
+            (sl->q_buf[3] << 24);
+    return chip_id;
+}
+
+/**
+ * Cortex m3 tech ref manual, CPUID register description
+ * @param sl stlink context
+ * @param cpuid pointer to the result object
+ */
+void stlink_cpu_id(stlink_t *sl, cortex_m3_cpuid_t *cpuid) {
+    stlink_read_mem32(sl, CM3_REG_CPUID, 4);
+    uint32_t raw = read_uint32(sl->q_buf, 0);
+    cpuid->implementer_id = (raw >> 24) & 0x7f;
+    cpuid->variant = (raw >> 20) & 0xf;
+    cpuid->part = (raw >> 4) & 0xfff;
+    cpuid->revision = raw & 0xf;
+    return;
 }
 
 void stlink_reset(stlink_t *sl) {
     D(sl, "\n*** stlink_reset ***\n");
     sl->backend->reset(sl);
-
 }
 
 void stlink_run(stlink_t *sl) {
@@ -404,18 +431,16 @@ void stlink_step(stlink_t *sl) {
 }
 
 int stlink_current_mode(stlink_t *sl) {
-    D(sl, "\n*** stlink_current_mode ***\n");
     int mode = sl->backend->current_mode(sl);
-    stlink_print_data(sl);
     switch (mode) {
         case STLINK_DEV_DFU_MODE:
-            DD(sl, "stlink mode: dfu\n");
+            DD(sl, "stlink current mode: dfu\n");
             return mode;
         case STLINK_DEV_DEBUG_MODE:
-            DD(sl, "stlink mode: debug (jtag or swd)\n");
+            DD(sl, "stlink current mode: debug (jtag or swd)\n");
             return mode;
         case STLINK_DEV_MASS_MODE:
-            DD(sl, "stlink mode: mass\n");
+            DD(sl, "stlink current mode: mass\n");
             return mode;
     }
     DD(sl, "stlink mode: unknown!\n");