#include <stdint.h>
#include <stddef.h>
+#include <stdbool.h>
#ifdef __cplusplus
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
#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
#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
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);
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);
#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"
--- /dev/null
+#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_ */
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
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;
_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);
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) {
stlink_run(sl);
- while (is_core_halted(sl) == 0)
+ while (stlink_is_core_halted(sl))
usleep(3000000);
}
/* 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;
}
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);
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);
}
}
#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;
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);
}
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;
}
// 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;
}
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) {
}
}
- 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);
continue;
}
- if (desc.idProduct == USB_STLINK_PID) {
+ if (desc.idProduct == STLINK_USB_PID_STLINK) {
slu->protocoll = 1;
break;
}
// 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;
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++;
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;