From a87def9593085d1c5af5eaeb71b12d71738325d5 Mon Sep 17 00:00:00 2001 From: Karl Palsson Date: Fri, 7 Oct 2011 19:45:41 +0000 Subject: [PATCH] Remove stm32l directory now that code is merged. Keep the documentation, that will still be useful... --- stm32l/build/Makefile | 25 - stm32l/src/main.c | 878 ------------------ {stm32l/doc => stm32l_notes}/break.csv | 0 .../device_connection.csv | 0 {stm32l/doc => stm32l_notes}/go.csv | 0 {stm32l/doc => stm32l_notes}/notes | 0 {stm32l/doc => stm32l_notes}/read_memory.csv | 0 {stm32l/doc => stm32l_notes}/step_into.csv | 0 8 files changed, 903 deletions(-) delete mode 100644 stm32l/build/Makefile delete mode 100644 stm32l/src/main.c rename {stm32l/doc => stm32l_notes}/break.csv (100%) rename {stm32l/doc => stm32l_notes}/device_connection.csv (100%) rename {stm32l/doc => stm32l_notes}/go.csv (100%) rename {stm32l/doc => stm32l_notes}/notes (100%) rename {stm32l/doc => stm32l_notes}/read_memory.csv (100%) rename {stm32l/doc => stm32l_notes}/step_into.csv (100%) diff --git a/stm32l/build/Makefile b/stm32l/build/Makefile deleted file mode 100644 index c50d61a..0000000 --- a/stm32l/build/Makefile +++ /dev/null @@ -1,25 +0,0 @@ -PRG := stm32l -DEBUG := # -DDEBUG -CONFIG := -DCONFIG_USE_LIBUSB=1 -CFLAGS+= -I../../src - -all: $(PRG) - -LIBS := \ - -L$(HOME)/install/lib -lusb-1.0 - -OBJS += \ - main.o - -$(PRG): $(OBJS) - gcc -o $(PRG) $(OBJS) $(LIBS) - -%.o: ../src/%.c - gcc -O3 -g3 -Wall -Werror -c -std=gnu99 -MMD -MP \ - -fno-strict-aliasing -Wno-unused $(DEBUG) $(CONFIG) \ - $(CFLAGS) \ - -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)"\ - -o "$@" "$<" - -clean: - @rm -vf *.d *.o $(PRG) diff --git a/stm32l/src/main.c b/stm32l/src/main.c deleted file mode 100644 index 9a68aa0..0000000 --- a/stm32l/src/main.c +++ /dev/null @@ -1,878 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include "stlink-hw.h" - -/* endianess related */ -static inline unsigned int is_bigendian(void) -{ - static volatile const unsigned int i = 1; - return *(volatile const char*) &i == 0; -} - -static void write_uint32(unsigned char* buf, uint32_t ui) -{ - if (!is_bigendian()) { // le -> le (don't swap) - buf[0] = ((unsigned char*) &ui)[0]; - buf[1] = ((unsigned char*) &ui)[1]; - buf[2] = ((unsigned char*) &ui)[2]; - buf[3] = ((unsigned char*) &ui)[3]; - } else { - buf[0] = ((unsigned char*) &ui)[3]; - buf[1] = ((unsigned char*) &ui)[2]; - buf[2] = ((unsigned char*) &ui)[1]; - buf[3] = ((unsigned char*) &ui)[0]; - } -} - -static void write_uint16(unsigned char* buf, uint16_t ui) -{ - if (!is_bigendian()) { // le -> le (don't swap) - buf[0] = ((unsigned char*) &ui)[0]; - buf[1] = ((unsigned char*) &ui)[1]; - } else { - buf[0] = ((unsigned char*) &ui)[1]; - buf[1] = ((unsigned char*) &ui)[0]; - } -} - -static uint32_t read_uint32(const unsigned char *c, const int pt) -{ - uint32_t ui; - char *p = (char *) &ui; - - if (!is_bigendian()) { // le -> le (don't swap) - p[0] = c[pt]; - p[1] = c[pt + 1]; - p[2] = c[pt + 2]; - p[3] = c[pt + 3]; - } else { - p[0] = c[pt + 3]; - p[1] = c[pt + 2]; - p[2] = c[pt + 1]; - p[3] = c[pt]; - } - return ui; -} - -static uint16_t read_uint16(const unsigned char *c, const int pt) -{ - uint32_t ui; - char *p = (char *) &ui; - - if (!is_bigendian()) { // le -> le (don't swap) - p[0] = c[pt]; - p[1] = c[pt + 1]; - } else { - p[0] = c[pt + 1]; - p[1] = c[pt]; - } - return ui; -} - -/* libusb transport layer */ - -libusb_context* libusb_ctx = NULL; - -struct trans_ctx -{ -#define TRANS_FLAGS_IS_DONE (1 << 0) -#define TRANS_FLAGS_HAS_ERROR (1 << 1) - volatile unsigned long flags; -}; - -static void on_trans_done(struct libusb_transfer* trans) -{ - struct trans_ctx* const ctx = trans->user_data; - - if (trans->status != LIBUSB_TRANSFER_COMPLETED) - ctx->flags |= TRANS_FLAGS_HAS_ERROR; - - ctx->flags |= TRANS_FLAGS_IS_DONE; -} - - -static int submit_wait(struct libusb_transfer* trans) -{ - struct timeval start; - struct timeval now; - struct timeval diff; - struct trans_ctx trans_ctx; - enum libusb_error error; - - trans_ctx.flags = 0; - - /* brief intrusion inside the libusb interface */ - trans->callback = on_trans_done; - trans->user_data = &trans_ctx; - - if ((error = libusb_submit_transfer(trans))) - { - printf("libusb_submit_transfer(%d)\n", error); - return -1; - } - - gettimeofday(&start, NULL); - - while (trans_ctx.flags == 0) - { - struct timeval timeout; - timeout.tv_sec = 3; - timeout.tv_usec = 0; - if (libusb_handle_events_timeout(libusb_ctx, &timeout)) - { - printf("libusb_handle_events()\n"); - return -1; - } - - gettimeofday(&now, NULL); - timersub(&now, &start, &diff); - if (diff.tv_sec >= 3) - { - printf("libusb_handle_events() timeout\n"); - return -1; - } - } - - if (trans_ctx.flags & TRANS_FLAGS_HAS_ERROR) - { - printf("libusb_handle_events() | has_error\n"); - return -1; - } - - return 0; -} - -static ssize_t send_recv -( - struct stlink_libusb* handle, - unsigned char* txbuf, size_t txsize, - unsigned char* rxbuf, size_t rxsize -) -{ - /* note: txbuf and rxbuf can point to the same area */ - - libusb_fill_bulk_transfer - ( - handle->req_trans, - handle->usb_handle, - handle->ep_req, - txbuf, txsize, - NULL, NULL, - 0 - ); - - printf("submit_wait(req)\n"); - - if (submit_wait(handle->req_trans)) return -1; - - /* send_only */ - if (rxsize == 0) return 0; - - /* read the response */ - - libusb_fill_bulk_transfer - ( - handle->rep_trans, - handle->usb_handle, - handle->ep_rep, - rxbuf, rxsize, - NULL, NULL, - 0 - ); - - printf("submit_wait(rep)\n"); - - if (submit_wait(handle->rep_trans)) return -1; - - return handle->rep_trans->actual_length; -} - - -static inline int send_only -(struct stlink_libusb* handle, unsigned char* txbuf, size_t txsize) -{ - return send_recv(handle, txbuf, txsize, NULL, 0); -} - - -// KARL - fixme, common code! (or, one per backend) -int stlink_initialize(enum transport_type tt) -{ - switch (tt) - { -#if CONFIG_USE_LIBUSB - case TRANSPORT_TYPE_LIBUSB: - { - if (libusb_ctx != NULL) return -1; - if (libusb_init(&libusb_ctx)) - { - printf("libusb_init()\n"); - return -1; - } - break ; - } -#endif /* CONFIG_USE_LIBUSB */ - - default: break ; - } - - return 0; -} - -// candidate for common code... -void stlink_finalize(enum transport_type tt) -{ - switch (tt) - { -#if CONFIG_USE_LIBUSB - case TRANSPORT_TYPE_LIBUSB: - { - libusb_exit(libusb_ctx); - break ; - } -#endif /* CONFIG_USE_LIBUSB */ - - default: break; - } -} - -#if CONFIG_USE_LIBUSB -static int is_stlink_device(libusb_device* dev) -{ - struct libusb_device_descriptor desc; - - if (libusb_get_device_descriptor(dev, &desc)) - return 0; - - printf("device: 0x%04x, 0x%04x\n", desc.idVendor, desc.idProduct); - - if (desc.idVendor != 0x0483) - return 0; - - if (desc.idProduct != 0x3748) - return 0; - - return 1; -} -#endif /* CONFIG_USE_LIBUSB */ - - -struct stlink* stlink_quirk_open -(enum transport_type tt, const char *dev_name, const int verbose) -{ - struct stlink* sl = NULL; - struct stlink_libusb* slu = NULL; - - sl = malloc(sizeof(struct stlink)); - slu = malloc(sizeof(struct stlink_libusb)); - if (sl == NULL) goto on_error; - - sl->tt = tt; - - switch (tt) - { -#if CONFIG_USE_LIBUSB - case TRANSPORT_TYPE_LIBUSB: - { - - int error = -1; - - libusb_device** devs = NULL; - libusb_device* dev; - ssize_t i; - ssize_t count; - int config; - - count = libusb_get_device_list(libusb_ctx, &devs); - if (count < 0) - { - printf("libusb_get_device_list\n"); - goto on_libusb_error; - } - - for (i = 0; i < count; ++i) - { - dev = devs[i]; - if (is_stlink_device(dev)) break; - } - if (i == count) return NULL; - - if (libusb_open(dev, &(slu->usb_handle))) - { - printf("libusb_open()\n"); - goto on_libusb_error; - } - - if (libusb_get_configuration(slu->usb_handle, &config)) - { - /* this may fail for a previous configured device */ - printf("libusb_get_configuration()\n"); - goto on_libusb_error; - } - - if (config != 1) - { - printf("setting new configuration (%d -> 1)\n", config); - if (libusb_set_configuration(slu->usb_handle, 1)) - { - /* this may fail for a previous configured device */ - printf("libusb_set_configuration()\n"); - goto on_libusb_error; - } - } - - if (libusb_claim_interface(slu->usb_handle, 0)) - { - printf("libusb_claim_interface()\n"); - goto on_libusb_error; - } - - slu->req_trans = libusb_alloc_transfer(0); - if (slu->req_trans == NULL) - { - printf("libusb_alloc_transfer\n"); - goto on_libusb_error; - } - - slu->rep_trans = libusb_alloc_transfer(0); - if (slu->rep_trans == NULL) - { - printf("libusb_alloc_transfer\n"); - goto on_libusb_error; - } - - slu->ep_rep = 1 /* ep rep */ | LIBUSB_ENDPOINT_IN; - slu->ep_req = 2 /* ep req */ | LIBUSB_ENDPOINT_OUT; - - /* libusb_reset_device(slu->usb_handle); */ - - /* success */ - error = 0; - - on_libusb_error: - if (devs != NULL) libusb_free_device_list(devs, 1); - - if (error == -1) - { - stlink_close(sl); - return NULL; - } - sl->transport.libusb = slu; - - break ; - } -#endif /* CONFIG_USE_LIBUSB */ - -#if CONFIG_USE_LIBSG - case transport_type_libsg: - { - break ; - } -#endif /* CONFIG_USE_LIBSG */ - - default: break ; - } - - /* success */ - return sl; - - on_error: - if (sl != NULL) free(sl); - return 0; -} - -void stlink_close(struct stlink *sl) -{ - switch (sl->tt) - { -#if CONFIG_USE_LIBUSB - case TRANSPORT_TYPE_LIBUSB: - { - struct stlink_libusb* const handle = sl->transport.libusb; - - if (handle->req_trans != NULL) - libusb_free_transfer(handle->req_trans); - - if (handle->rep_trans != NULL) - libusb_free_transfer(handle->rep_trans); - - if (handle->usb_handle != NULL) - libusb_close(handle->usb_handle); - - break ; - } -#endif /* CONFIG_USE_LIBUSB */ - -#if CONFIG_USE_LIBSG - case TRANSPORT_TYPE_LIBSG: - { - break ; - } -#endif /* CONFIG_USE_LIBSG */ - - default: break ; - } - - free(sl); -} - -void stlink_version(struct stlink* sl) -{ - switch (sl->tt) - { -#if CONFIG_USE_LIBUSB - case TRANSPORT_TYPE_LIBUSB: - { - struct stlink_libusb* const slu = sl->transport.libusb; - unsigned char* const buf = sl->q_buf; - ssize_t size; - - memset(buf, 0, sizeof(sl->q_buf)); - buf[0] = STLINK_GET_VERSION; - buf[1] = 0x80; - - size = send_recv(slu, buf, 16, buf, sizeof(sl->q_buf)); - if (size == -1) - { - printf("[!] send_recv\n"); - return ; - } - -#if 1 /* DEBUG */ - { - unsigned int i; - for (i = 0; i < size; ++i) printf("%02x", buf[i]); - printf("\n"); - } -#endif /* DEBUG */ - - break ; - } -#endif /* CONFIG_USE_LIBUSB */ - - default: break ; - } -} - -int stlink_current_mode(struct stlink *sl) -{ - int mode = -1; - - switch (sl->tt) - { -#if CONFIG_USE_LIBUSB - case TRANSPORT_TYPE_LIBUSB: - { - struct stlink_libusb* const slu = sl->transport.libusb; - unsigned char* const buf = sl->q_buf; - ssize_t size; - - memset(buf, 0, sizeof(sl->q_buf)); - - buf[0] = STLINK_GET_CURRENT_MODE; - - size = send_recv(slu, buf, 16, buf, sizeof(sl->q_buf)); - if (size == -1) - { - printf("[!] send_recv\n"); - return -1; - } - - /* mode = (int)read_uint16(buf, 0); */ - mode = (int)buf[0]; - -#if 1 /* DEBUG */ - printf("mode == 0x%x\n", mode); -#endif /* DEBUG */ - - break ; - } -#endif /* CONFIG_USE_LIBUSB */ - - default: break ; - } - - return mode; -} - -void stlink_core_id(struct stlink *sl) -{ - switch (sl->tt) - { -#if CONFIG_USE_LIBUSB - case TRANSPORT_TYPE_LIBUSB: - { - struct stlink_libusb* const slu = sl->transport.libusb; - unsigned char* const buf = sl->q_buf; - ssize_t size; - - memset(buf, 0, sizeof(sl->q_buf)); - buf[0] = STLINK_DEBUG_COMMAND; - buf[1] = STLINK_DEBUG_READCOREID; - - size = send_recv(slu, buf, 16, buf, sizeof(sl->q_buf)); - if (size == -1) - { - printf("[!] send_recv\n"); - return ; - } - - sl->core_id = read_uint32(buf, 0); - -#if 1 /* DEBUG */ - printf("core_id == 0x%x\n", sl->core_id); -#endif /* DEBUG */ - - break ; - } -#endif /* CONFIG_USE_LIBUSB */ - - default: break ; - } -} - -void stlink_status(struct stlink *sl) -{ - switch (sl->tt) - { -#if CONFIG_USE_LIBUSB - case TRANSPORT_TYPE_LIBUSB: - { - struct stlink_libusb* const slu = sl->transport.libusb; - unsigned char* const buf = sl->q_buf; - ssize_t size; - - memset(buf, 0, sizeof(sl->q_buf)); - - buf[0] = STLINK_DEBUG_COMMAND; - buf[1] = STLINK_DEBUG_GETSTATUS; - - size = send_recv(slu, buf, 16, buf, sizeof(sl->q_buf)); - if (size == -1) - { - printf("[!] send_recv\n"); - return ; - } - - /* todo: stlink_core_stat */ - -#if 1 /* DEBUG */ - printf("status == 0x%x\n", buf[0]); -#endif /* DEBUG */ - - break ; - } -#endif /* CONFIG_USE_LIBUSB */ - - default: break ; - } -} - -void stlink_enter_swd_mode(struct stlink *sl) -{ - switch (sl->tt) - { -#if CONFIG_USE_LIBUSB - case TRANSPORT_TYPE_LIBUSB: - { - struct stlink_libusb* const slu = sl->transport.libusb; - unsigned char* const buf = sl->q_buf; - ssize_t size; - - memset(buf, 0, sizeof(sl->q_buf)); - - buf[0] = STLINK_DEBUG_COMMAND; - buf[1] = 0x30; /* needed byte */ - buf[2] = STLINK_DEBUG_ENTER_JTAG; - - size = send_recv(slu, buf, 16, buf, sizeof(sl->q_buf)); - if (size == -1) - { - printf("[!] send_recv\n"); - return ; - } - - break ; - } -#endif /* CONFIG_USE_LIBUSB */ - - default: break ; - } -} - -void stlink_exit_dfu_mode(struct stlink *sl) -{ - switch (sl->tt) - { -#if CONFIG_USE_LIBUSB - case TRANSPORT_TYPE_LIBUSB: - { - struct stlink_libusb* const slu = sl->transport.libusb; - unsigned char* const buf = sl->q_buf; - ssize_t size; - - memset(buf, 0, sizeof(sl->q_buf)); - buf[0] = STLINK_DFU_COMMAND; - buf[1] = STLINK_DFU_EXIT; - - size = send_only(slu, buf, 16); - if (size == -1) - { - printf("[!] send_recv\n"); - return ; - } - - break ; - } -#endif /* CONFIG_USE_LIBUSB */ - - default: break ; - } -} - -void stlink_reset(struct stlink *sl) -{ - switch (sl->tt) - { -#if CONFIG_USE_LIBUSB - case TRANSPORT_TYPE_LIBUSB: - { - struct stlink_libusb* const slu = sl->transport.libusb; - unsigned char* const buf = sl->q_buf; - ssize_t size; - - memset(buf, 0, sizeof(sl->q_buf)); - buf[0] = STLINK_DEBUG_COMMAND; - buf[1] = STLINK_DEBUG_RESETSYS; - - size = send_recv(slu, buf, 16, buf, sizeof(sl->q_buf)); - if (size == -1) - { - printf("[!] send_recv\n"); - return ; - } - - break ; - } -#endif /* CONFIG_USE_LIBUSB */ - - default: break ; - } -} - -void stlink_step(struct stlink *sl) -{ - switch (sl->tt) - { -#if CONFIG_USE_LIBUSB - case TRANSPORT_TYPE_LIBUSB: - { - struct stlink_libusb* const slu = sl->transport.libusb; - unsigned char* const buf = sl->q_buf; - ssize_t size; - - memset(buf, 0, sizeof(sl->q_buf)); - buf[0] = STLINK_DEBUG_COMMAND; - buf[1] = STLINK_DEBUG_STEPCORE; - - size = send_recv(slu, buf, 16, buf, sizeof(sl->q_buf)); - if (size == -1) - { - printf("[!] send_recv\n"); - return ; - } - - break ; - } -#endif /* CONFIG_USE_LIBUSB */ - - default: break ; - } -} - -void stlink_run(struct stlink *sl) -{ - switch (sl->tt) - { -#if CONFIG_USE_LIBUSB - case TRANSPORT_TYPE_LIBUSB: - { - struct stlink_libusb* const slu = sl->transport.libusb; - unsigned char* const buf = sl->q_buf; - ssize_t size; - - memset(buf, 0, sizeof(sl->q_buf)); - buf[0] = STLINK_DEBUG_COMMAND; - buf[1] = STLINK_DEBUG_RUNCORE; - - size = send_recv(slu, buf, 16, buf, sizeof(sl->q_buf)); - if (size == -1) - { - printf("[!] send_recv\n"); - return ; - } - - break ; - } -#endif /* CONFIG_USE_LIBUSB */ - - default: break ; - } -} - -void stlink_exit_debug_mode(struct stlink *sl) -{ - switch (sl->tt) - { -#if CONFIG_USE_LIBUSB - case TRANSPORT_TYPE_LIBUSB: - { - struct stlink_libusb* const slu = sl->transport.libusb; - unsigned char* const buf = sl->q_buf; - ssize_t size; - - memset(buf, 0, sizeof(sl->q_buf)); - buf[0] = STLINK_DEBUG_COMMAND; - buf[1] = STLINK_DEBUG_EXIT; - - size = send_only(slu, buf, 16); - if (size == -1) - { - printf("[!] send_only\n"); - return ; - } - - break ; - } -#endif /* CONFIG_USE_LIBUSB */ - - default: break ; - } -} - -static void stlink_print_data(struct stlink *sl) -{ - size_t i; - for (i = 0; i < sl->q_len; ++i) - { - if ((i % 16) == 0) printf("\n"); - printf(" %02x", sl->q_buf[i]); - } - printf("\n"); -} - - -void stlink_read_mem32(struct stlink *sl, uint32_t addr, uint16_t len) -{ - switch (sl->tt) - { -#if CONFIG_USE_LIBUSB - case TRANSPORT_TYPE_LIBUSB: - { - struct stlink_libusb* const slu = sl->transport.libusb; - unsigned char* const buf = sl->q_buf; - ssize_t size; - - /* assume len < sizeof(sl->q_buf) */ - - memset(buf, 0, sizeof(sl->q_buf)); - buf[0] = STLINK_DEBUG_COMMAND; - buf[1] = STLINK_DEBUG_READMEM_32BIT; - write_uint32(buf + 2, addr); - write_uint16(buf + 6, len); -#if 0 - /* windows usb logs show only one byte is used for length ... */ - buf[6] = (uint8_t)len; -#endif - - size = send_recv(slu, buf, 0x10, buf, sizeof(sl->q_buf)); - if (size == -1) - { - printf("[!] send_recv\n"); - return ; - } - - sl->q_len = (size_t)size; - - stlink_print_data(sl); - - break ; - } -#endif /* CONFIG_USE_LIBUSB */ - - default: break ; - } -} - - - -/* main */ - -int main(int ac, char** av) -{ - struct stlink* sl; - - stlink_initialize(TRANSPORT_TYPE_LIBUSB); - sl = stlink_quirk_open(TRANSPORT_TYPE_LIBUSB, NULL, 0); - if (sl != NULL) - { - printf("-- version\n"); - stlink_version(sl); - - if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE) - { - printf("-- exit_dfu_mode\n"); - stlink_exit_dfu_mode(sl); - } - - printf("-- enter_swd_mode\n"); - stlink_enter_swd_mode(sl); - - printf("-- current_mode\n"); - stlink_current_mode(sl); - -/* printf("-- core_id\n"); */ -/* stlink_core_id(sl); */ - - printf("-- read_sram\n"); - static const uint32_t sram_base = 0x8000000; - uint32_t off; - for (off = 0; off < 16; off += 4) - stlink_read_mem32(sl, sram_base + off, 4); - - printf("-- read_mem, cpuid\n"); - stlink_read_mem32(sl, 0xe000e008, 4); -/* stlink_read_mem32(sl, 0xe000ed90, 4); */ -/* stlink_read_mem32(sl, 0xe000edf0, 4); */ -/* stlink_read_mem32(sl, 0x4001100c, 4); */ - - printf("-- status\n"); - stlink_status(sl); - - printf("-- reset\n"); - stlink_reset(sl); - - printf("-- status\n"); - stlink_status(sl); - - printf("-- step\n"); - stlink_step(sl); - getchar(); - - printf("-- run\n"); - stlink_run(sl); - - printf("-- exit_debug_mode\n"); - stlink_exit_debug_mode(sl); - - stlink_close(sl); - } - stlink_finalize(TRANSPORT_TYPE_LIBUSB); - - return 0; -} diff --git a/stm32l/doc/break.csv b/stm32l_notes/break.csv similarity index 100% rename from stm32l/doc/break.csv rename to stm32l_notes/break.csv diff --git a/stm32l/doc/device_connection.csv b/stm32l_notes/device_connection.csv similarity index 100% rename from stm32l/doc/device_connection.csv rename to stm32l_notes/device_connection.csv diff --git a/stm32l/doc/go.csv b/stm32l_notes/go.csv similarity index 100% rename from stm32l/doc/go.csv rename to stm32l_notes/go.csv diff --git a/stm32l/doc/notes b/stm32l_notes/notes similarity index 100% rename from stm32l/doc/notes rename to stm32l_notes/notes diff --git a/stm32l/doc/read_memory.csv b/stm32l_notes/read_memory.csv similarity index 100% rename from stm32l/doc/read_memory.csv rename to stm32l_notes/read_memory.csv diff --git a/stm32l/doc/step_into.csv b/stm32l_notes/step_into.csv similarity index 100% rename from stm32l/doc/step_into.csv rename to stm32l_notes/step_into.csv -- 2.47.2