aboutsummaryrefslogtreecommitdiff
path: root/flashstub
diff options
context:
space:
mode:
authorGareth McMullin2015-03-08 22:43:12 -0700
committerGareth McMullin2015-03-19 21:49:09 -0700
commitacfd258aa7b02ea23a6d26c29c3432bf3e857039 (patch)
treed4d23b80985c1fe9ddbe05eefaab1f5ba724c883 /flashstub
parent8ddb186b35267e90832deb2db4b0c58f77de4b87 (diff)
Rewrite STM32F4 stub in C and use new interface.
Diffstat (limited to 'flashstub')
-rw-r--r--flashstub/Makefile6
-rw-r--r--flashstub/stm32f4.c40
-rw-r--r--flashstub/stm32f4.s44
-rw-r--r--flashstub/stm32f4.stub1
4 files changed, 46 insertions, 45 deletions
diff --git a/flashstub/Makefile b/flashstub/Makefile
index eeb1803..c955ca8 100644
--- a/flashstub/Makefile
+++ b/flashstub/Makefile
@@ -8,7 +8,7 @@ ifneq ($(V), 1)
Q = @
endif
-CFLAGS=-mcpu=cortex-m3 -mthumb -I../libopencm3/include
+CFLAGS=-Os -std=gnu99 -mcpu=cortex-m3 -mthumb -I../libopencm3/include
ASFLAGS=-mcpu=cortex-m3 -mthumb
all: lmi.stub stm32f4.stub nrf51.stub stm32f1.stub
@@ -17,6 +17,10 @@ stm32f1.o: stm32f1.c
$(Q)echo " CC $<"
$(Q)$(CC) $(CFLAGS) -DSTM32F1 -o $@ -c $<
+stm32f4.o: stm32f4.c
+ $(Q)echo " CC $<"
+ $(Q)$(CC) $(CFLAGS) -DSTM32F4 -o $@ -c $<
+
%.o: %.s
$(Q)echo " AS $<"
$(Q)$(AS) $(ASFLAGS) -o $@ $<
diff --git a/flashstub/stm32f4.c b/flashstub/stm32f4.c
new file mode 100644
index 0000000..6732897
--- /dev/null
+++ b/flashstub/stm32f4.c
@@ -0,0 +1,40 @@
+/*
+ * This file is part of the Black Magic Debug project.
+ *
+ * Copyright (C) 2015 Black Sphere Technologies Ltd.
+ * Written by Gareth McMullin <gareth@blacksphere.co.nz>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include "libopencm3/stm32/flash.h"
+#include "stub.h"
+
+#define SR_ERROR_MASK 0xF2
+
+void __attribute__((naked))
+stm32f4_flash_write_stub(uint32_t *dest, uint32_t *src, uint32_t size)
+{
+ for (int i = 0; i < size; i += 4) {
+ FLASH_CR = FLASH_CR_PROGRAM_X32 | FLASH_CR_PG;
+ *dest++ = *src++;
+ while (FLASH_SR & FLASH_SR_BSY)
+ ;
+ }
+
+ if (FLASH_SR & SR_ERROR_MASK)
+ stub_exit(1);
+
+ stub_exit(0);
+}
+
diff --git a/flashstub/stm32f4.s b/flashstub/stm32f4.s
deleted file mode 100644
index fa3fda0..0000000
--- a/flashstub/stm32f4.s
+++ /dev/null
@@ -1,44 +0,0 @@
-.global _start
-
-_start:
- ldr r0, _flashbase
- ldr r1, _addr
- mov r2, pc
- add r2, #(_data - . - 2)
- ldr r3, _size
- ldr r5, _cr
-_next:
- cbz r3, _done
- @ Write PG command to FLASH_CR
- str r5, [r0, #0x10]
- @ Write data to flash (word)
- ldr r4, [r2]
- str r4, [r1]
-
-_wait: @ Wait for BSY bit to clear
- ldrh r4, [r0, #0x0E]
- mov r6, #1
- tst r4, r6
- bne _wait
-
- sub r3, #4
- add r1, #4
- add r2, #4
- b _next
-_done:
- bkpt
-
-@.align 4
-.org 0x28
-_cr:
- .word 0x00000201
-_flashbase:
- .word 0x40023C00
-_addr:
- .word 0x0800bf78
-_size:
- .word 8
-_data:
- .word 0xAAAAAAAA
- .word 0xBBBBBBBB
- .word 0xCCCCCCCC
diff --git a/flashstub/stm32f4.stub b/flashstub/stm32f4.stub
new file mode 100644
index 0000000..25b5d9a
--- /dev/null
+++ b/flashstub/stm32f4.stub
@@ -0,0 +1 @@
+0x2300, 0x4293, 0x4C09, 0xD20B, 0x4D09, 0xF240, 0x2601, 0x602E, 0x58CD, 0x50C5, 0x6825, 0xF415, 0x3F80, 0xD1FB, 0x3304, 0xE7F0, 0x6823, 0xF013, 0x0FF2, 0xD000, 0xBE01, 0xBE00, 0x3C0C, 0x4002, 0x3C10, 0x4002, \ No newline at end of file