使用keil 生成 Flash Loader image

时间:2024-01-21 14:12:03

1.1参考文档及路径

  1. DUI0459F_02_mdk_fromelf_user_guide.pdf;

    2.Cortex-M3 技术参考手册.pdf;

1.2 Flash Loader简介

  平时我们使用的仿真器(ST-LINK,J-LINK)并不是直接将数据烧写到芯片的flash存储器中。而是通过一段专门的程序Flash Loader。它实际上是一段主要用来擦除,写入Flash的小程序。从原理上来说就是仿真器先下载Flash Loader到RAM中,然后操作内核寄存器调用Flash Loader函数集。当函数返回,会产生一个中断,仿真器就会知道函数已经完成,进而继续调用Flash Loader

  简单的说,仿真器从Flash Loader调用函数来实现对芯片的烧写,擦除操作。将程序下载到flash,有两种方式:

  1. 直接操作Flash存储器

            通过调试接口(DAP)操作芯片相关的FLASH寄存器,具体怎么做需要查阅芯片的相关说明文档。

  2. 间接操作Flash存储器

           A)下载Flash Loader到RAM中;

   B)将应用程序的映像下载到RAM缓冲区中;

     C)Flash Loader通过仿真器启动,将RAM Buffer里读出的数据编程到Flash中;

   D)现在,应用程序的映像文件就在Flash存储器中了。

 

1.3 Flash Loader编写

  根据keil提供的模板C:\Keil_v5\ARM\Flash\_Template,至少需要以下3个文件:

   A) FlashDev.c,Device Decription,是一个结构体变量,里面定义的是Flash相关信息

 1 /***********************************************************************/
 2 /*  This file is part of the ARM Toolchain package                     */
 3 /*  Copyright (c) 2010 Keil - An ARM Company. All rights reserved.     */
 4 /***********************************************************************/
 5 /*                                                                     */
 6 /*  FlashDev.C:  Device Description for New Device Flash               */
 7 /*                                                                     */
 8 /***********************************************************************/
 9 
10 #include "..\FlashOS.H"        // FlashOS Structures
11 
12 
13 struct FlashDevice const FlashDevice  =  {
14    FLASH_DRV_VERS,             // Driver Version, do not modify!
15    "New Device 256kB Flash",   // Device Name 
16    ONCHIP,                     // Device Type
17    0x00000000,                 // Device Start Address
18    0x00040000,                 // Device Size in Bytes (256kB)
19    1024,                       // Programming Page Size
20    0,                          // Reserved, must be 0
21    0xFF,                       // Initial Content of Erased Memory
22    100,                        // Program Page Timeout 100 mSec
23    3000,                       // Erase Sector Timeout 3000 mSec
24 
25 // Specify Size and Address of Sectors
26    0x002000, 0x000000,         // Sector Size  8kB (8 Sectors)
27    0x010000, 0x010000,         // Sector Size 64kB (2 Sectors) 
28    0x002000, 0x030000,         // Sector Size  8kB (8 Sectors)
29    SECTOR_END
30 };

 

  B)FlashPrg.c,Program Functions实现Flash的初始化,擦除,写等函数

 1 /***********************************************************************/
 2 /*  This file is part of the ARM Toolchain package                     */
 3 /*  Copyright (c) 2010 Keil - An ARM Company. All rights reserved.     */
 4 /***********************************************************************/
 5 /*                                                                     */
 6 /*  FlashDev.C:  Flash Programming Functions adapted                   */
 7 /*               for New Device 256kB Flash                            */
 8 /*                                                                     */
 9 /***********************************************************************/
10 
11 #include "..\FlashOS.H"        // FlashOS Structures
12 /*
13  *  Initialize Flash Programming Functions
14  *    Parameter:      adr:  Device Base Address
15  *                    clk:  Clock Frequency (Hz)
16  *                    fnc:  Function Code (1 - Erase, 2 - Program, 3 - Verify)
17  *    Return Value:   0 - OK,  1 - Failed
18  */
19 
20 int Init (unsigned long adr, unsigned long clk, unsigned long fnc) {
21 
22   /* Add your Code */
23   return (0);                                  // Finished without Errors
24 }
25 
26 
27 /*
28  *  De-Initialize Flash Programming Functions
29  *    Parameter:      fnc:  Function Code (1 - Erase, 2 - Program, 3 - Verify)
30  *    Return Value:   0 - OK,  1 - Failed
31  */
32 
33 int UnInit (unsigned long fnc) {
34 
35   /* Add your Code */
36   return (0);                                  // Finished without Errors
37 }
38 
39 
40 /*
41  *  Erase complete Flash Memory
42  *    Return Value:   0 - OK,  1 - Failed
43  */
44 
45 int EraseChip (void) {
46 
47   /* Add your Code */
48   return (0);                                  // Finished without Errors
49 }
50 
51 
52 /*
53  *  Erase Sector in Flash Memory
54  *    Parameter:      adr:  Sector Address
55  *    Return Value:   0 - OK,  1 - Failed
56  */
57 
58 int EraseSector (unsigned long adr) {
59 
60   /* Add your Code */
61   return (0);                                  // Finished without Errors
62 }
63 
64 
65 /*
66  *  Program Page in Flash Memory
67  *    Parameter:      adr:  Page Start Address
68  *                    sz:   Page Size
69  *                    buf:  Page Data
70  *    Return Value:   0 - OK,  1 - Failed
71  */
72 
73 int ProgramPage (unsigned long adr, unsigned long sz, unsigned char *buf) {
74 
75   /* Add your Code */
76   return (0);                                  // Finished without Errors
77 }

  C)FlashOS.h, 结构体定义

 1 /* -----------------------------------------------------------------------------
 2  * Copyright (c) 2014 ARM Ltd.
 3  *
 4  * This software is provided \'as-is\', without any express or implied warranty. 
 5  * In no event will the authors be held liable for any damages arising from 
 6  * the use of this software. Permission is granted to anyone to use this 
 7  * software for any purpose, including commercial applications, and to alter 
 8  * it and redistribute it freely, subject to the following restrictions:
 9  *
10  * 1. The origin of this software must not be misrepresented; you must not 
11  *    claim that you wrote the original software. If you use this software in
12  *    a product, an acknowledgment in the product documentation would be 
13  *    appreciated but is not required. 
14  * 
15  * 2. Altered source versions must be plainly marked as such, and must not be 
16  *    misrepresented as being the original software. 
17  * 
18  * 3. This notice may not be removed or altered from any source distribution.
19  *   
20  *
21  * $Date:        14. Jan 2014
22  * $Revision:    V1.00
23  *  
24  * Project:      FlashOS Headerfile for Flash drivers
25  * --------------------------------------------------------------------------- */
26 
27 /* History:
28  *  Version 1.00
29  *    Initial release
30  */ 
31 
32 #define VERS       1           // Interface Version 1.01
33 
34 #define UNKNOWN    0           // Unknown
35 #define ONCHIP     1           // On-chip Flash Memory
36 #define EXT8BIT    2           // External Flash Device on 8-bit  Bus
37 #define EXT16BIT   3           // External Flash Device on 16-bit Bus
38 #define EXT32BIT   4           // External Flash Device on 32-bit Bus
39 #define EXTSPI     5           // External Flash Device on SPI
40 
41 #define SECTOR_NUM 512         // Max Number of Sector Items
42 #define PAGE_MAX   65536       // Max Page Size for Programming
43 
44 struct FlashSectors  {
45   unsigned long   szSector;    // Sector Size in Bytes
46   unsigned long AddrSector;    // Address of Sector
47 };
48 
49 #define SECTOR_END 0xFFFFFFFF, 0xFFFFFFFF
50 
51 struct FlashDevice  {
52    unsigned short     Vers;    // Version Number and Architecture
53    char       DevName[128];    // Device Name and Description
54    unsigned short  DevType;    // Device Type: ONCHIP, EXT8BIT, EXT16BIT, ...
55    unsigned long    DevAdr;    // Default Device Start Address
56    unsigned long     szDev;    // Total Size of Device
57    unsigned long    szPage;    // Programming Page Size
58    unsigned long       Res;    // Reserved for future Extension
59    unsigned char  valEmpty;    // Content of Erased Memory
60 
61    unsigned long    toProg;    // Time Out of Program Page Function
62    unsigned long   toErase;    // Time Out of Erase Sector Function
63 
64    struct FlashSectors sectors[SECTOR_NUM];
65 };
66 
67 #define FLASH_DRV_VERS (0x0100+VERS)   // Driver Version, do not modify!
68 
69 // Flash Programming Functions (Called by FlashOS)
70 extern          int  Init        (unsigned long adr,   // Initialize Flash
71                                   unsigned long clk,
72                                   unsigned long fnc);
73 extern          int  UnInit      (unsigned long fnc);  // De-initialize Flash
74 extern          int  BlankCheck  (unsigned long adr,   // Blank Check
75                                   unsigned long sz,
76                                   unsigned char pat);
77 extern          int  EraseChip   (void);               // Erase complete Device
78 extern          int  EraseSector (unsigned long adr);  // Erase Sector Function
79 extern          int  ProgramPage (unsigned long adr,   // Program Page Function
80                                   unsigned long sz,
81                                   unsigned char *buf);
82 extern unsigned long Verify      (unsigned long adr,   // Verify Function
83                                   unsigned long sz,
84                                   unsigned char *buf);

  

  绝大多数的Flash Loader可以去keil的官网上下载。