Move USB defines into usb.h, move chip register defines into new reg.h
authorJerry Jacobs <jerry.jacobs@xor-gate.org>
Fri, 20 May 2016 21:11:43 +0000 (23:11 +0200)
committerJerry Jacobs <jerry.jacobs@xor-gate.org>
Fri, 20 May 2016 21:30:36 +0000 (23:30 +0200)
include/stlink.h
include/stlink/reg.h [new file with mode: 0644]
include/stlink/usb.h
src/common.c
src/flash_loader.c
src/gdbserver/gdb-server.c
src/go/main [new file with mode: 0755]
src/sg.c
src/usb.c

index a20f815cbd2a9299d8cbdad27c38e0cea90953c7..3b860052fdc200ed682b08340b01b07f6c87264c 100644 (file)
@@ -9,6 +9,7 @@
 
 #include <stdint.h>
 #include <stddef.h>
+#include <stdbool.h>
 
 #ifdef __cplusplus
 extern "C" {
@@ -21,15 +22,7 @@ extern "C" {
     //#define Q_BUF_LEN        96
 #define Q_BUF_LEN                      (1024 * 100)
 
-    // st-link vendor cmd's
-#define USB_ST_VID                     0x0483
-#define USB_STLINK_PID                 0x3744
-#define USB_STLINK_32L_PID             0x3748
-#define USB_STLINK_NUCLEO_PID  0x374b
-
     // STLINK_DEBUG_RESETSYS, etc:
-#define STLINK_OK                      0x80
-#define STLINK_FALSE                   0x81
 #define STLINK_CORE_RUNNING            0x80
 #define STLINK_CORE_HALTED             0x81
 #define STLINK_CORE_STAT_UNKNOWN       -1
@@ -41,7 +34,6 @@ extern "C" {
 #define STLINK_DEBUG_COMMAND           0xF2
 #define STLINK_DFU_COMMAND             0xF3
 #define STLINK_DFU_EXIT                0x07
-    // enter dfu could be 0x08?
 
     // STLINK_GET_CURRENT_MODE
 #define STLINK_DEV_DFU_MODE            0x00
@@ -55,20 +47,10 @@ extern "C" {
 #define STLINK_JTAG_WRITEDEBUG_32BIT 0x35
 #define STLINK_JTAG_READDEBUG_32BIT 0x36
 #define STLINK_JTAG_DRIVE_NRST 0x3c
-#define STLINK_JTAG_DRIVE_NRST 0x3c
-
-    // cortex m3 technical reference manual
-#define CM3_REG_CPUID 0xE000ED00
-#define CM3_REG_FP_CTRL 0xE0002000
-#define CM3_REG_FP_COMP0 0xE0002008
 
     /* cortex core ids */
     // TODO clean this up...
 #define STM32VL_CORE_ID 0x1ba01477
-#define STM32L_CORE_ID 0x2ba01477
-#define STM32F3_CORE_ID 0x2ba01477
-#define STM32F4_CORE_ID 0x2ba01477
-#define STM32F0_CORE_ID 0xbb11477
 #define CORE_M3_R1 0x1BA00477
 #define CORE_M3_R2 0x4BA00477
 #define CORE_M4_R0 0x2BA01477
@@ -251,12 +233,9 @@ typedef struct flash_loader {
     int stlink_fwrite_sram(stlink_t *sl, const char* path, stm32_addr_t addr);
     int stlink_verify_write_flash(stlink_t *sl, stm32_addr_t address, uint8_t *data, uint32_t length);
 
-    // PUBLIC
     int stlink_chip_id(stlink_t *sl, uint32_t *chip_id);
     int stlink_cpu_id(stlink_t *sl, cortex_m3_cpuid_t *cpuid);
 
-    // privates, publics, the rest....
-    // TODO sort what is private, and what is not
     int stlink_erase_flash_page(stlink_t* sl, stm32_addr_t flashaddr);
     uint32_t stlink_calculate_pagesize(stlink_t *sl, uint32_t flashaddr);
     uint16_t read_uint16(const unsigned char *c, const int pt);
@@ -266,7 +245,7 @@ typedef struct flash_loader {
     uint32_t read_uint32(const unsigned char *c, const int pt);
     void write_uint32(unsigned char* buf, uint32_t ui);
     void write_uint16(unsigned char* buf, uint16_t ui);
-    unsigned int is_core_halted(stlink_t *sl);
+    bool stlink_is_core_halted(stlink_t *sl);
     int write_buffer_to_sram(stlink_t *sl, flash_loader_t* fl, const uint8_t* buf, size_t size);
     int write_loader_to_sram(stlink_t *sl, stm32_addr_t* addr, size_t* size);
     int stlink_fread(stlink_t* sl, const char* path, stm32_addr_t addr, size_t size);
@@ -275,6 +254,7 @@ typedef struct flash_loader {
 
 #include "stlink/sg.h"
 #include "stlink/usb.h"
+#include "stlink/reg.h"
 #include "stlink/commands.h"
 #include "stlink/chipid.h"
 #include "stlink/flash_loader.h"
diff --git a/include/stlink/reg.h b/include/stlink/reg.h
new file mode 100644 (file)
index 0000000..c6af166
--- /dev/null
@@ -0,0 +1,8 @@
+#ifndef STLINK_REG_H_
+#define STLINK_REG_H_
+
+#define STLINK_REG_CM3_CPUID    0xE000ED00
+#define STLINK_REG_CM3_FP_CTRL  0xE0002000
+#define STLINK_REG_CM3_FP_COMP0 0xE0002008
+
+#endif /* STLINK_REG_H_ */
index 76ac9211f014a2219221d84643d0479000566acc..46e7419cf480719293aa910f24891ebd3f9796e5 100644 (file)
 extern "C" {
 #endif
 
+#define STLINK_USB_VID_ST            0x0483
+#define STLINK_USB_PID_STLINK        0x3744
+#define STLINK_USB_PID_STLINK_32L    0x3748
+#define STLINK_USB_PID_STLINK_NUCLEO 0x374b
+
 #define STLINK_SG_SIZE 31
 #define STLINK_CMD_SIZE 16
 
index 72704812570622decbe73a50dff7ee9982584000..493f1b90ef8fdeac5a868c8ed817ee0d34dc2384 100644 (file)
@@ -550,7 +550,7 @@ int stlink_chip_id(stlink_t *sl, uint32_t *chip_id) {
 int stlink_cpu_id(stlink_t *sl, cortex_m3_cpuid_t *cpuid) {
     uint32_t raw;
 
-    if (stlink_read_debug32(sl, CM3_REG_CPUID, &raw))
+    if (stlink_read_debug32(sl, STLINK_REG_CM3_CPUID, &raw))
         return -1;
 
     cpuid->implementer_id = (raw >> 24) & 0x7f;
@@ -692,7 +692,7 @@ int stlink_version(stlink_t *sl) {
 
     _parse_version(sl, &sl->version);
 
-    DLOG("st vid         = 0x%04x (expect 0x%04x)\n", sl->version.st_vid, USB_ST_VID);
+    DLOG("st vid         = 0x%04x (expect 0x%04x)\n", sl->version.st_vid, STLINK_USB_VID_ST);
     DLOG("stlink pid     = 0x%04x\n", sl->version.stlink_pid);
     DLOG("stlink version = 0x%x\n", sl->version.stlink_v);
     DLOG("jtag version   = 0x%x\n", sl->version.jtag_v);
@@ -836,10 +836,15 @@ int stlink_write_unsupported_reg(stlink_t *sl, uint32_t val, int r_idx, reg *reg
     return sl->backend->write_unsupported_reg(sl, val, r_convert, regp);
 }
 
-unsigned int is_core_halted(stlink_t *sl) {
-    /* return non zero if core is halted */
-    stlink_status(sl);
-    return sl->q_buf[0] == STLINK_CORE_HALTED;
+bool stlink_is_core_halted(stlink_t *sl)
+{
+       bool ret = false;
+
+       stlink_status(sl);
+       if (sl->q_buf[0] == STLINK_CORE_HALTED)
+               ret = true;
+
+       return ret;
 }
 
 int stlink_step(stlink_t *sl) {
@@ -900,7 +905,7 @@ void stlink_run_at(stlink_t *sl, stm32_addr_t addr) {
 
     stlink_run(sl);
 
-    while (is_core_halted(sl) == 0)
+    while (stlink_is_core_halted(sl))
         usleep(3000000);
 }
 
index bf4c92f1462442dbbaea3f43cea02c25b6d46aba..23f235b5b4d590e3ea3c28a5929489777c3f82ff 100644 (file)
@@ -293,7 +293,7 @@ int stlink_flash_loader_run(stlink_t *sl, flash_loader_t* fl, stm32_addr_t targe
     /* wait until done (reaches breakpoint) */
     for (i = 0; i < WAIT_ROUNDS; i++) {
         usleep(10);
-        if (is_core_halted(sl))
+        if (stlink_is_core_halted(sl))
             break;
     }
 
index b57a299acbd4bb0b759af9899c1ad604a11d894a..c50e5be32cbc85ac4b0a45a0f78197dcd39d2088 100644 (file)
@@ -585,8 +585,8 @@ struct code_hw_breakpoint code_breaks[CODE_BREAK_NUM_MAX];
 static void init_code_breakpoints(stlink_t *sl) {
     unsigned int val;
     memset(sl->q_buf, 0, 4);
-    stlink_write_debug32(sl, CM3_REG_FP_CTRL, 0x03 /*KEY | ENABLE4*/);
-    stlink_read_debug32(sl, CM3_REG_FP_CTRL, &val);
+    stlink_write_debug32(sl, STLINK_REG_CM3_FP_CTRL, 0x03 /*KEY | ENABLE4*/);
+    stlink_read_debug32(sl, STLINK_REG_CM3_FP_CTRL, &val);
     code_break_num = ((val >> 4) & 0xf);
     code_lit_num = ((val >> 8) & 0xf);
 
@@ -594,7 +594,7 @@ static void init_code_breakpoints(stlink_t *sl) {
 
     for(int i = 0; i < code_break_num; i++) {
         code_breaks[i].type = 0;
-        stlink_write_debug32(sl, CM3_REG_FP_COMP0 + i * 4, 0);
+        stlink_write_debug32(sl, STLINK_REG_CM3_FP_COMP0 + i * 4, 0);
     }
 }
 
diff --git a/src/go/main b/src/go/main
new file mode 100755 (executable)
index 0000000..25baad2
Binary files /dev/null and b/src/go/main differ
index 40570981d311eac41a45ee92c60e16770b8ae3fd..646091aaca3fc20c195d358ea3412862f61f6d2d 100644 (file)
--- a/src/sg.c
+++ b/src/sg.c
@@ -85,6 +85,9 @@
 #include "stlink.h"
 #include "stlink/logging.h"
 
+#define STLINK_OK    0x80
+#define STLINK_FALSE 0x81
+
 static void clear_cdb(struct stlink_libsg *sl) {
     for (size_t i = 0; i < sizeof (sl->cdb_cmd_blk); i++)
         sl->cdb_cmd_blk[i] = 0;
@@ -952,7 +955,7 @@ static stlink_t* stlink_open(const int verbose) {
 
     libusb_set_debug(slsg->libusb_ctx, 3);
 
-    slsg->usb_handle = libusb_open_device_with_vid_pid(slsg->libusb_ctx, USB_ST_VID, USB_STLINK_PID);
+    slsg->usb_handle = libusb_open_device_with_vid_pid(slsg->libusb_ctx, STLINK_USB_VID_ST, STLINK_USB_PID_STLINK);
     if (slsg->usb_handle == NULL) {
         WLOG("Failed to find an stlink v1 by VID:PID\n");
         libusb_close(slsg->usb_handle);
@@ -1041,7 +1044,7 @@ stlink_t* stlink_v1_open_inner(const int verbose) {
     }
 
     stlink_version(sl);
-    if ((sl->version.st_vid != USB_ST_VID) || (sl->version.stlink_pid != USB_STLINK_PID)) {
+    if ((sl->version.st_vid != STLINK_USB_VID_ST) || (sl->version.stlink_pid != STLINK_USB_PID_STLINK)) {
         ELOG("WTF? successfully opened, but unable to read version details. BROKEN!\n");
         return NULL;
     }
@@ -1063,7 +1066,7 @@ stlink_t* stlink_v1_open_inner(const int verbose) {
 
     // re-query device info (and retest)
     stlink_version(sl);
-    if ((sl->version.st_vid != USB_ST_VID) || (sl->version.stlink_pid != USB_STLINK_PID)) {
+    if ((sl->version.st_vid != STLINK_USB_VID_ST) || (sl->version.stlink_pid != STLINK_USB_PID_STLINK)) {
         ELOG("WTF? successfully opened, but unable to read version details. BROKEN!\n");
         return NULL;
     }
index 9c04ec8608e2562e0f0fb41bff488b141818ba78..b30399835146e70bfe5daa4907afcaae0139aacf 100644 (file)
--- a/src/usb.c
+++ b/src/usb.c
@@ -761,7 +761,7 @@ stlink_t *stlink_open_usb(enum ugly_loglevel verbose, bool reset, char serial[16
 
     while (cnt--) {
         libusb_get_device_descriptor( list[cnt], &desc );
-        if (desc.idVendor != USB_ST_VID)
+        if (desc.idVendor != STLINK_USB_VID_ST)
             continue;
 
         if (devBus && devAddr) {
@@ -771,7 +771,7 @@ stlink_t *stlink_open_usb(enum ugly_loglevel verbose, bool reset, char serial[16
             }
         }
 
-        if ((desc.idProduct == USB_STLINK_32L_PID) || (desc.idProduct == USB_STLINK_NUCLEO_PID)) {
+        if ((desc.idProduct == STLINK_USB_PID_STLINK_32L) || (desc.idProduct == STLINK_USB_PID_STLINK_NUCLEO)) {
             struct libusb_device_handle *handle;
 
             libusb_open(list[cnt], &handle);
@@ -791,7 +791,7 @@ stlink_t *stlink_open_usb(enum ugly_loglevel verbose, bool reset, char serial[16
             continue;
         }
 
-        if (desc.idProduct == USB_STLINK_PID) {
+        if (desc.idProduct == STLINK_USB_PID_STLINK) {
             slu->protocoll = 1;
             break;
         }
@@ -841,7 +841,7 @@ stlink_t *stlink_open_usb(enum ugly_loglevel verbose, bool reset, char serial[16
 
     // TODO - could use the scanning techniq from stm8 code here...
     slu->ep_rep = 1 /* ep rep */ | LIBUSB_ENDPOINT_IN;
-    if (desc.idProduct == USB_STLINK_NUCLEO_PID) {
+    if (desc.idProduct == STLINK_USB_PID_STLINK_NUCLEO) {
         slu->ep_req = 1 /* ep req */ | LIBUSB_ENDPOINT_OUT;
     } else {
         slu->ep_req = 2 /* ep req */ | LIBUSB_ENDPOINT_OUT;
@@ -906,8 +906,8 @@ static size_t stlink_probe_usb_devs(libusb_device **devs, stlink_t **sldevs[]) {
             break;
         }
 
-        if (desc.idProduct != USB_STLINK_32L_PID &&
-            desc.idProduct != USB_STLINK_NUCLEO_PID)
+        if (desc.idProduct != STLINK_USB_PID_STLINK_32L &&
+            desc.idProduct != STLINK_USB_PID_STLINK_NUCLEO)
             continue;
 
         slcnt++;
@@ -930,8 +930,8 @@ static size_t stlink_probe_usb_devs(libusb_device **devs, stlink_t **sldevs[]) {
             break;
         }
 
-        if (desc.idProduct != USB_STLINK_32L_PID &&
-            desc.idProduct != USB_STLINK_NUCLEO_PID)
+        if (desc.idProduct != STLINK_USB_PID_STLINK_32L &&
+            desc.idProduct != STLINK_USB_PID_STLINK_NUCLEO)
             continue;
 
         struct libusb_device_handle* handle;