From 437aedda11a112407af7b3f7884090023944faef Mon Sep 17 00:00:00 2001 From: Gareth McMullin Date: Sun, 8 Mar 2015 13:35:07 -0700 Subject: Rewrote stm32f1 stub in C and call with parameters in registers. --- src/include/general.h | 2 ++ src/stm32f1.c | 27 ++++++++++++--------------- 2 files changed, 14 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/include/general.h b/src/include/general.h index 2cb4bd9..cf8c1e6 100644 --- a/src/include/general.h +++ b/src/include/general.h @@ -38,5 +38,7 @@ #define DEBUG printf #endif +#define ALIGN(x, n) (((x) + (n) - 1) & ~((n) - 1)) + #endif diff --git a/src/stm32f1.c b/src/stm32f1.c index daba1b0..96faf1f 100644 --- a/src/stm32f1.c +++ b/src/stm32f1.c @@ -36,8 +36,6 @@ #include "command.h" #include "gdb_packet.h" -#define SRAM_BASE 0x20000000 - static bool stm32f1_cmd_erase_mass(target *t); static bool stm32f1_cmd_option(target *t, int argc, char *argv[]); @@ -123,9 +121,12 @@ static const char stm32hd_xml_memory_map[] = "" #define DBGMCU_IDCODE_F0 0x40015800 static const uint16_t stm32f1_flash_write_stub[] = { -#include "../flashstub/stm32.stub" +#include "../flashstub/stm32f1.stub" }; +#define SRAM_BASE 0x20000000 +#define STUB_BUFFER_BASE ALIGN(SRAM_BASE + sizeof(stm32f1_flash_write_stub), 4) + bool stm32f1_probe(struct target_s *target) { target->idcode = target_mem_read32(target, DBGMCU_IDCODE) & 0xfff; @@ -247,22 +248,18 @@ static int stm32f1_flash_write(struct target_s *target, uint32_t dest, const uint8_t *src, size_t len) { uint32_t offset = dest % 4; - uint32_t words = (offset + len + 3) / 4; - if (words > 256) - return -1; - uint32_t data[2 + words]; + uint8_t data[ALIGN(offset + len, 4)]; /* Construct data buffer used by stub */ - data[0] = dest - offset; - data[1] = words * 4; /* length must always be a multiple of 4 */ - data[2] = 0xFFFFFFFF; /* pad partial words with all 1s to avoid */ - data[words + 1] = 0xFFFFFFFF; /* damaging overlapping areas */ - memcpy((uint8_t *)&data[2] + offset, src, len); + /* pad partial words with all 1s to avoid damaging overlapping areas */ + memset(data, 0xff, sizeof(data)); + memcpy((uint8_t *)data + offset, src, len); /* Write stub and data to target ram and set PC */ - target_mem_write(target, 0x2000002C, data, sizeof(data)); - cortexm_run_stub(target, SRAM_BASE, stm32f1_flash_write_stub, 0x2C, - 0, 0, 0, 0); + target_mem_write(target, STUB_BUFFER_BASE, data, sizeof(data)); + cortexm_run_stub(target, SRAM_BASE, stm32f1_flash_write_stub, + sizeof(stm32f1_flash_write_stub), + dest - offset, STUB_BUFFER_BASE, sizeof(data), 0); /* Check for error */ if (target_mem_read32(target, FLASH_SR) & SR_ERROR_MASK) -- cgit v1.2.3