最近將兩年前買的STM32F103最小系統板拿出來准備學習,安裝完MDK5后寫了一個點亮LED程序,發現無法下載。查了下購買評論,原來這種板子發貨時已經鎖定了flash。鼓搗了2、3天,最后采取的辦法是通過sram啟動方式,來運行一個flash解鎖程序,程序運行后就可以將板子恢復。在此將該過程記錄下來。
一、系統板外形是這種。

二、硬件跳線
將兩個黃色跳線帽全部插在1端,即BOOT0、BOO1全部接1。
三、MDK5設置
3-1、新建一個工程,取名Flash_Unlock。設置Run-time,選擇CMSIS->CORE、Device->Startup、Device->StdPeriph Device->Framework、Device->StdPeriph Device->Flash,新建並添加main.c文件;
3-2、添加代碼:
1 #include "stm32f10x.h"
2 #include "stm32f10x_flash.h"
3
4 int main(void) 5 { 6 NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); 7 FLASH_Unlock();//解鎖flash
8 FLASH_ReadOutProtection(DISABLE);//解除讀保護
9 return 0;
10 }
3-3、設置Option參數
3-3-1、在Device選項卡中選擇STM32F103C8型號;
3-3-2、在Target選項卡中設置8MHz,選擇Use MicroLIBsram的起始地址為0x2000 0000,此型號芯片ram大小為20KB,即0x5000。因此將ROM划分16KB,填入0x4000,RAM划分為4KB,起始地址為0x2000 4000,大小為0x1000;

3-3-3、在Output選項卡中選中“Create HEX File”;
3-3-4、在C/C++選項卡中Define欄中填入:VECT_TAB_SRAM;

3-3-5、在Linker選項卡總先取消選擇Use Memory Layout from Target Independent,更改R/O Base為0x20000000,更改R/I BASE為0x20004000,在選擇Use Memory Layout from Target Independent;

3-3-6、在Debug選項卡中根據實際情況選擇調試器,這里選擇ST-LINK,取消選擇Load Application at Startup,在工程文件夾中新建一個txt文件,改名為RAM2.ini,打開並復制如下代碼,並在Initialization File一欄中加載此文件;
1 /******************************************************************************/
2 /* RAM.INI: RAM Initialization File */
3 /******************************************************************************/
4 // <<< Use Configuration Wizard in Context Menu >>> //
5 /******************************************************************************/
6 /* This file is part of the uVision/ARM development tools. */
7 /* Copyright (c) 2005-2007 Keil Software. All rights reserved. */
8 /* This software may only be used under the terms of a valid, current, */
9 /* end user licence from KEIL for a compatible version of KEIL software */
10 /* development tools. Nothing else gives you the right to use this software. */
11 /******************************************************************************/
12
13 FUNC void Setup (void) { 14 SP = _RDWORD(0x20000000); // Setup Stack Pointer
15 PC = _RDWORD(0x20000004); // Setup Program Counter
16 _WDWORD(0xE000ED08, 0x20000000); // Setup Vector Table
17 } 18
19 LOAD ARM\Blinky.axf INCREMENTAL // Download
20
21 Setup(); // Setup for Running
22 g, main
3-3-7、在Utilities選項卡中Init File一欄加載RAM.ini文件,取消選擇Update Target before Debugging;

3-3-8、在Utilities選項卡中點擊Setting,在彈出的窗口中選擇Do not Erase,並設置RAM和ROM地址;

3-3-9、點擊調試按鈕,進入調試窗口后按F5,flash即可解鎖,將芯片斷電,跳線帽恢復至0、0,可以正常下載程序。
