Extra registers integration with GDB
[fw/stlink] / src / stlink-usb.c
index 580722766cb385f914119d079795786d23a1fa68..fdd636b619d74469a3624a745c1b1a19aa9a03cf 100644 (file)
 #define WLOG(format, args...)         ugly_log(UWARN, LOG_TAG, format, ## args)
 #define fatal(format, args...)        ugly_log(UFATAL, LOG_TAG, format, ## args)
 
-#ifndef timersub
-/* This is a copy from GNU C Library (GNU LGPL 2.1), sys/time.h. */
-# define timersub(a, b, result)                                               \
-  do {                                                                        \
-    (result)->tv_sec = (a)->tv_sec - (b)->tv_sec;                             \
-    (result)->tv_usec = (a)->tv_usec - (b)->tv_usec;                          \
-    if ((result)->tv_usec < 0) {                                              \
-      --(result)->tv_sec;                                                     \
-      (result)->tv_usec += 1000000;                                           \
-    }                                                                         \
-  } while (0)
+/* code from bsd timersub.h 
+http://www.gnu-darwin.org/www001/src/ports/net/libevnet/work/libevnet-0.3.8/libnostd/bsd/sys/time/timersub.h.html
+*/
+#if !defined timersub
+#define        timersub(a, b, r) do {                                  \
+       (r)->tv_sec     = (a)->tv_sec - (b)->tv_sec;            \
+       (r)->tv_usec    = (a)->tv_usec - (b)->tv_usec;          \
+       if ((r)->tv_usec < 0) {                                 \
+               --(r)->tv_sec;                                  \
+               (r)->tv_usec += 1000000;                        \
+       }                                                       \
+} while (0)
 #endif
 
 enum SCSI_Generic_Direction {SG_DXFER_TO_DEV=0, SG_DXFER_FROM_DEV=0x80};
@@ -57,6 +58,10 @@ struct trans_ctx {
     volatile unsigned long flags;
 };
 
+#ifndef LIBUSB_CALL
+# define LIBUSB_CALL
+#endif
+
 static void LIBUSB_CALL on_trans_done(struct libusb_transfer * trans) {
     struct trans_ctx * const ctx = trans->user_data;
 
@@ -571,6 +576,45 @@ void _stlink_usb_read_reg(stlink_t *sl, int r_idx, reg *regp) {
     }
 }
 
+/* See section C1.6 of the ARMv7-M Architecture Reference Manual */
+void _stlink_usb_read_unsupported_reg(stlink_t *sl, int r_idx, reg *regp) {
+    uint32_t r;
+
+    sl->q_buf[0] = (unsigned char) r_idx;
+    for (int i = 1; i < 4; i++) {
+        sl->q_buf[i] = 0;
+    }
+
+    _stlink_usb_write_mem32(sl, 0xE000EDF4, 4);
+    _stlink_usb_read_mem32(sl, 0xE000EDF8, 4);
+
+    r = read_uint32(sl->q_buf, 0);
+    DLOG("r_idx (%2d) = 0x%08x\n", r_idx, r);
+
+    switch (r_idx) {
+        case 0x14:
+            regp->primask = (uint8_t) (r & 0xFF);
+            regp->basepri = (uint8_t) ((r>>8) & 0xFF);
+            regp->faultmask = (uint8_t) ((r>>16) & 0xFF);
+            regp->control = (uint8_t) ((r>>24) & 0xFF);
+            break;
+        case 0x21:
+            regp->fpscr = r;
+            break;
+        default:
+            regp->s[r_idx - 0x40] = r;
+            break;
+    }
+}
+
+void _stlink_usb_read_all_unsupported_regs(stlink_t *sl, reg *regp) {
+    _stlink_usb_read_unsupported_reg(sl, 0x21, regp);
+
+    for (int i = 0; i < 32; i++) {
+        _stlink_usb_read_unsupported_reg(sl, 0x40+i, regp);
+    }
+}
+
 void _stlink_usb_write_reg(stlink_t *sl, uint32_t reg, int idx) {
     struct stlink_libusb * const slu = sl->backend_data;
     unsigned char* const data = sl->q_buf;
@@ -611,6 +655,8 @@ stlink_backend_t _stlink_usb_backend = {
     _stlink_usb_write_mem8,
     _stlink_usb_read_all_regs,
     _stlink_usb_read_reg,
+    _stlink_usb_read_all_unsupported_regs,
+    _stlink_usb_read_unsupported_reg,
     _stlink_usb_write_reg,
     _stlink_usb_step,
     _stlink_usb_current_mode,
@@ -647,7 +693,7 @@ stlink_t* stlink_open_usb(const int verbose) {
     if (slu->usb_handle == NULL) {
        slu->usb_handle = libusb_open_device_with_vid_pid(slu->libusb_ctx, USB_ST_VID, USB_STLINK_PID);
        if (slu->usb_handle == NULL) {
-           WLOG("Couldn't find any ST-Link/V2 devices");
+           WLOG("Couldn't find any ST-Link/V2 devices\n");
            goto on_error;
        }
        slu->protocoll = 1;