Rewrite STM32F4 stub in C and use new interface.

This commit is contained in:
Gareth McMullin 2015-03-08 22:43:12 -07:00
parent 8ddb186b35
commit acfd258aa7
5 changed files with 61 additions and 106 deletions

View File

@ -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 $@ $<

40
flashstub/stm32f4.c Normal file
View File

@ -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);
}

View File

@ -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

1
flashstub/stm32f4.stub Normal file
View File

@ -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,

View File

@ -33,6 +33,7 @@
#include "general.h"
#include "adiv5.h"
#include "target.h"
#include "cortexm.h"
#include "command.h"
#include "gdb_packet.h"
@ -120,44 +121,12 @@ static const char stm32f4_xml_memory_map[] = "<?xml version=\"1.0\"?>"
/* This routine is uses word access. Only usable on target voltage >2.7V */
static const uint16_t stm32f4_flash_write_stub[] = {
// _start:
0x480a, // ldr r0, [pc, #40] // _flashbase
0x490b, // ldr r1, [pc, #44] // _addr
0x467a, // mov r2, pc
0x3230, // adds r2, #48
0x4b0a, // ldr r3, [pc, #36] // _size
0x4d07, // ldr r5, [pc, #28] // _cr
// _next:
0xb153, // cbz r3, _done
0x6105, // str r5, [r0, #16]
0x6814, // ldr r4, [r2]
0x600c, // str r4, [r1]
// _wait:
0x89c4, // ldrb r4, [r0, #14]
0x2601, // movs r6, #1
0x4234, // tst r4, r6
0xd1fb, // bne _wait
0x3b04, // subs r3, #4
0x3104, // adds r1, #4
0x3204, // adds r2, #4
0xe7f3, // b _next
// _done:
0xbe00, // bkpt
0x0000,
// .org 0x28
//_cr:
0x0201, 0x0000, //.word 0x00000201 (Value to write to FLASH_CR) */
// _flashbase:
0x3c00, 0x4002, // .word 0x40023c00 (FPEC_BASE)
// _addr:
// 0x0000, 0x0000,
// _size:
// 0x0000, 0x0000,
// _data:
// ...
#include "../flashstub/stm32f4.stub"
};
#define SRAM_BASE 0x20000000
#define STUB_BUFFER_BASE ALIGN(SRAM_BASE + sizeof(stm32f4_flash_write_stub), 4)
bool stm32f4_probe(struct target_s *target)
{
uint32_t idcode;
@ -239,34 +208,19 @@ static int stm32f4_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;
uint32_t data[2 + words];
uint16_t sr;
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, 0x20000000, stm32f4_flash_write_stub, 0x30);
target_mem_write(target, 0x20000030, data, sizeof(data));
target_pc_write(target, 0x20000000);
if(target_check_error(target))
return -1;
/* Execute the stub */
target_halt_resume(target, 0);
while(!target_halt_wait(target));
/* Check for error */
sr = target_mem_read32(target, FLASH_SR);
if(sr & SR_ERROR_MASK)
return -1;
return 0;
/* Write buffer to target ram call stub */
target_mem_write(target, STUB_BUFFER_BASE, data, sizeof(data));
return cortexm_run_stub(target, SRAM_BASE, stm32f4_flash_write_stub,
sizeof(stm32f4_flash_write_stub),
dest - offset, STUB_BUFFER_BASE, sizeof(data),
0);
}
static bool stm32f4_cmd_erase_mass(target *t)