STM32程序运行在外部SRAM中
方式一:STM32程序运行在外部SRAM中
资料来源:
https://blog.csdn.net/qq_27379251/article/details/89416075
方式二:STM32程序运行在片内SRAM中
据说Flash的擦写次数是有限的,所以在调试的时候擦来擦去不好,看到boot0、boot1可以配置从SRam启动,
发现无法下载。查了下购买评论,原来这种板子发货时已经锁定了flash。鼓捣了2、3天,最后采取的办法是通过sram启动方式,来运行一个flash解锁程序,程序运行后就可以将板子恢复。在此将该过程记录下来。
1 硬件跳线
将两个黄色跳线帽全部插在1端,即BOOT0、BOO1全部接1,即从内部SRAM启动。
2 MDK5设置
2.1 新建一个工程,取名Flash_Unlock。
设置Run-time,选择CMSIS->CORE、Device->Startup、Device->StdPeriph Device->Framework、Device->StdPeriph Device->Flash,新建并添加main.c文件;
2.2 添加代码
#include "stm32f10x.h"
#include "stm32f10x_flash.h"
int main(void)
{
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
FLASH_Unlock();//解锁flash
FLASH_ReadOutProtection(DISABLE);//解除读保护
return 0;
}
2.3 设置Option参数
在Device选项卡中选择STM32F103C8型号;
在Target选项卡中设置8MHz,选择Use MicroLIBsram的起始地址为0x2000 0000,此型号芯片ram大小为20KB,即0x5000。因此将ROM划分16KB,填入0x4000,RAM划分为4KB,起始地址为0x2000 4000,大小为0x1000;
在Output选项卡中选中“Create HEX File”;
在C/C++选项卡中Define栏中填入:VECT_TAB_SRAM;
在Linker选项卡总先取消选择Use Memory Layout from Target Independent,更改R/O Base为0x20000000,更改R/I BASE为0x20004000,在选择Use Memory Layout from Target Independent;
在Debug选项卡中根据实际情况选择调试器,这里选择ST-LINK,取消选择Load Application at Startup,在工程文件夹中新建一个txt文件,改名为RAM2.ini,打开并复制如下代码,并在Initialization File一栏中加载此文件;
/******************************************************************************/
/* RAM.INI: RAM Initialization File */
/******************************************************************************/
// <<< Use Configuration Wizard in Context Menu >>> //
/******************************************************************************/
/* This file is part of the uVision/ARM development tools. */
/* Copyright (c) 2005-2007 Keil Software. All rights reserved. */
/* This software may only be used under the terms of a valid, current, */
/* end user licence from KEIL for a compatible version of KEIL software */
/* development tools. Nothing else gives you the right to use this software. */
/******************************************************************************/
FUNC void Setup (void) {
SP = _RDWORD(0x20000000); // Setup Stack Pointer
PC = _RDWORD(0x20000004); // Setup Program Counter
_WDWORD(0xE000ED08, 0x20000000); // Setup Vector Table
}
LOAD ARM\Blinky.axf INCREMENTAL // Download
Setup(); // Setup for Running
g, main
另一版本
/*----------------------------------------------------------------------------
* Name: Dbg_RAM.ini
* Purpose: RAM Debug Initialization File
* Note(s):
*----------------------------------------------------------------------------
* This file is part of the uVision/ARM development tools.
* This software may only be used under the terms of a valid, current,
* end user licence from KEIL for a compatible version of KEIL software
* development tools. Nothing else gives you the right to use this software.
*
* This software is supplied "AS IS" without warranties of any kind.
*
* Copyright (c) 2008-2011 Keil - An ARM Company. All rights reserved.
*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------
Setup() configure PC & SP for RAM Debug
*----------------------------------------------------------------------------*/
FUNC void Setup (void) {
SP = _RDWORD(0x20000000); // Setup Stack Pointer
PC = _RDWORD(0x20000004); // Setup Program Counter
_WDWORD(0xE000ED08, 0x20000000); // Setup Vector Table Offset Register
}
LOAD %L INCREMENTAL // load the application
Setup(); // Setup for Running
g, main
在Utilities选项卡中Init File一栏加载RAM.ini文件,取消选择Update Target before Debugging;
在Utilities选项卡中点击Setting,在弹出的窗口中选择Do not Erase,并设置RAM和ROM地址;
点击调试按钮,进入调试窗口后按F5,flash即可解锁,将芯片断电,跳线帽恢复至0、0,可以正常下载程序。
转载于:https://www.cnblogs.com/linux-embedded/p/9462897.html
资料来源:
https://blog.csdn.net/weixin_30532369/article/details/97629864