#include <stdlib.h>
#include <string.h>
#include <stdint.h>
-#include <time.h>
+#include <sys/time.h>
#include <sys/types.h>
-#include <libusb-1.0/libusb.h>
+#include <libusb.h>
#include "stlink-common.h"
#include "stlink-usb.h"
#define WLOG(format, args...) ugly_log(UWARN, LOG_TAG, format, ## args)
#define fatal(format, args...) ugly_log(UFATAL, LOG_TAG, format, ## args)
+/* 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};
volatile unsigned long flags;
};
-static void on_trans_done(struct libusb_transfer * trans) {
+#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;
if (trans->status != LIBUSB_TRANSFER_COMPLETED)
}
if ((handle->protocoll == 1) && terminate) {
- fprintf(stderr, "This is never used....\n");
- exit(EXIT_FAILURE);
/* Read the SG reply */
unsigned char sg_buf[13];
libusb_fill_bulk_transfer
int i = 0;
memset(cmd, 0, sizeof (sl->c_buf));
if(slu->protocoll == 1) {
- fprintf(stderr, "This is never used....\n");
cmd[i++] = 'U';
cmd[i++] = 'S';
cmd[i++] = 'B';
size = send_recv(slu, 1, cmd, slu->cmd_len, rdata, rep_len);
if (size == -1) {
printf("[!] send_recv\n");
- return;
+ return 0;
}
return read_uint32(rdata, 4);
}
}
}
+/* 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;
_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,
slu->usb_handle = libusb_open_device_with_vid_pid(slu->libusb_ctx, USB_ST_VID, USB_STLINK_32L_PID);
if (slu->usb_handle == NULL) {
- // TODO - free usb context too...
- WLOG("Couldn't find any ST-Link/V2 devices");
- goto on_error;
+ 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\n");
+ goto on_error;
+ }
+ slu->protocoll = 1;
}
-
+
if (libusb_kernel_driver_active(slu->usb_handle, 0) == 1) {
int r;
stlink_enter_swd_mode(sl);
}
- stlink_version(sl);
+ stlink_reset(sl);
stlink_load_device_params(sl);
+ stlink_version(sl);
error = 0;