DSP280049C初学(4)-FLASH烧录以及部分程序转移至RAM运行
实现目的:代码在RAM中调试完成后,就需要将其固化下载到FLASH中,但是FLASH中运行所有程序的话会存在计算或程序运行时间过长的问题,故还需要将部分代码转移至RAM中运行。
1. 更换CMD文件
在前文基于DSP280049C的正点原子PMSM电机驱动(一):CCS文件夹的建立与IQmath导入,我们创建的文件所运用的CMD是28004x_iqmath_ram_link_cmd,当我们要进行FLASH烧录时,则需要选择28004x_iqmath_flash_link_cmd(该CMD文件将会放在文末附录中免费下载)。
1.1 内存分块
先来看28004x_iqmath_flash_link_cmd关于内存的分块:
其中“PAGE 0 ”主要是放置函数的内存区域,“PAGE 1 ”主要是放置数据的内存区域,每个内存块都可以自定义它的大小。
MEMORY
{
PAGE 0 :
/* Program Memory */
/* Memory (RAM/FLASH) blocks can be moved to PAGE1 for data allocation */
/* BEGIN is used for the "boot to FLASH" bootloader mode */
BEGIN : origin = 0x080000, length = 0x000002
RAMM0 : origin = 0x0000F3, length = 0x00030D
RAMLS0 : origin = 0x008000, length = 0x002000
RAMLS1 : origin = 0x00A000, length = 0x000800
RESET : origin = 0x3FFFC0, length = 0x000002
/* Flash sectors */
/* BANK 0 */
FLASH_BANK0_SEC0 : origin = 0x080002, length = 0x000FFE /* on-chip Flash */
FLASH_BANK0_SEC1 : origin = 0x081000, length = 0x001000 /* on-chip Flash */
FLASH_BANK0_SEC2 : origin = 0x082000, length = 0x001000 /* on-chip Flash */
FLASH_BANK0_SEC3 : origin = 0x083000, length = 0x001000 /* on-chip Flash */
FLASH_BANK0_SEC4 : origin = 0x084000, length = 0x001000 /* on-chip Flash */
FLASH_BANK0_SEC5 : origin = 0x085000, length = 0x001000 /* on-chip Flash */
FLASH_BANK0_SEC6 : origin = 0x086000, length = 0x001000 /* on-chip Flash */
FLASH_BANK0_SEC7 : origin = 0x087000, length = 0x001000 /* on-chip Flash */
FLASH_BANK0_SEC8 : origin = 0x088000, length = 0x001000 /* on-chip Flash */
FLASH_BANK0_SEC9 : origin = 0x089000, length = 0x001000 /* on-chip Flash */
FLASH_BANK0_SEC10 : origin = 0x08A000, length = 0x001000 /* on-chip Flash */
FLASH_BANK0_SEC11 : origin = 0x08B000, length = 0x001000 /* on-chip Flash */
FLASH_BANK0_SEC12 : origin = 0x08C000, length = 0x001000 /* on-chip Flash */
FLASH_BANK0_SEC13 : origin = 0x08D000, length = 0x001000 /* on-chip Flash */
FLASH_BANK0_SEC14 : origin = 0x08E000, length = 0x001000 /* on-chip Flash */
FLASH_BANK0_SEC15 : origin = 0x08F000, length = 0x001000 /* on-chip Flash */
/* BANK 1 */
FLASH_BANK1_SEC0 : origin = 0x090000, length = 0x001000 /* on-chip Flash */
FLASH_BANK1_SEC1 : origin = 0x091000, length = 0x001000 /* on-chip Flash */
FLASH_BANK1_SEC2 : origin = 0x092000, length = 0x001000 /* on-chip Flash */
FLASH_BANK1_SEC3 : origin = 0x093000, length = 0x001000 /* on-chip Flash */
FLASH_BANK1_SEC4 : origin = 0x094000, length = 0x001000 /* on-chip Flash */
FLASH_BANK1_SEC5 : origin = 0x095000, length = 0x001000 /* on-chip Flash */
FLASH_BANK1_SEC6 : origin = 0x096000, length = 0x001000 /* on-chip Flash */
FLASH_BANK1_SEC7 : origin = 0x097000, length = 0x001000 /* on-chip Flash */
FLASH_BANK1_SEC8 : origin = 0x098000, length = 0x001000 /* on-chip Flash */
FLASH_BANK1_SEC9 : origin = 0x099000, length = 0x001000 /* on-chip Flash */
FLASH_BANK1_SEC10 : origin = 0x09A000, length = 0x001000 /* on-chip Flash */
FLASH_BANK1_SEC11 : origin = 0x09B000, length = 0x001000 /* on-chip Flash */
FLASH_BANK1_SEC12 : origin = 0x09C000, length = 0x001000 /* on-chip Flash */
FLASH_BANK1_SEC13 : origin = 0x09D000, length = 0x001000 /* on-chip Flash */
FLASH_BANK1_SEC14 : origin = 0x09E000, length = 0x001000 /* on-chip Flash */
FLASH_BANK1_SEC15 : origin = 0x09F000, length = 0x000FF0 /* on-chip Flash */
// FLASH_BANK1_SEC15_RSVD : origin = 0x09FFF0, length = 0x000010 /* Reserve and do not use for code as per the errata advisory "Memory: Prefetching Beyond Valid Memory" */
PAGE 1 : /* Data Memory */
/* Memory (RAM/FLASH) blocks can be moved to PAGE0 for program allocation */
BOOT_RSVD : origin = 0x000002, length = 0x0000F1 /* Part of M0, BOOT rom will use this for stack */
RAMM1 : origin = 0x000400, length = 0x0003F8 /* on-chip RAM block M1 */
// RAMM1_RSVD : origin = 0x0007F8, length = 0x000008 /* Reserve and do not use for code as per the errata advisory "Memory: Prefetching Beyond Valid Memory" */
RAMLS5 : origin = 0x00A800, length = 0x000800
RAMLS6 : origin = 0x00B000, length = 0x000800
RAMLS7 : origin = 0x00B800, length = 0x000800
RAMGS0 : origin = 0x00C000, length = 0x002000
RAMGS1 : origin = 0x00E000, length = 0x002000
RAMGS2 : origin = 0x010000, length = 0x002000
RAMGS3 : origin = 0x012000, length = 0x001FF8
// RAMGS3_RSVD : origin = 0x013FF8, length = 0x000008 /* Reserve and do not use for code as per the errata advisory "Memory: Prefetching Beyond Valid Memory" */
}
1.2 内存分配
内存分配这部分可以根据实际需求进行分配,可以在Build程序后,调用"Window-Show View-Memory Allocation"窗口来查看分配情况。
SECTIONS
{
codestart : > BEGIN, PAGE = 0, ALIGN(4)
.text : >> FLASH_BANK0_SEC0 | FLASH_BANK0_SEC1 | FLASH_BANK0_SEC2 | FLASH_BANK0_SEC14 | FLASH_BANK0_SEC15, PAGE = 0, ALIGN(4)
.cinit : > FLASH_BANK0_SEC3|FLASH_BANK0_SEC4|FLASH_BANK0_SEC5, PAGE = 0, ALIGN(4)
.switch : > FLASH_BANK0_SEC6, PAGE = 0, ALIGN(4)
.reset : > RESET, PAGE = 0, TYPE = DSECT /* not used, */
.stack : > RAMM1, PAGE = 1
#if defined(__TI_EABI__)
.init_array : >> FLASH_BANK0_SEC3|FLASH_BANK0_SEC4|FLASH_BANK0_SEC5, PAGE = 0, ALIGN(4)
.bss : >> RAMLS5 | RAMLS6 | RAMLS7, PAGE = 1
.bss:output : > RAMLS0, PAGE = 0
.bss:cio : > RAMLS0, PAGE = 0
.data : > RAMLS5, PAGE = 1
.sysmem : > RAMLS5, PAGE = 1
/* Initalized sections go in Flash */
.const : > FLASH_BANK0_SEC6, PAGE = 0, ALIGN(4)
#else
.pinit : >> FLASH_BANK0_SEC3|FLASH_BANK0_SEC4|FLASH_BANK0_SEC5, PAGE = 0, ALIGN(4)
.ebss : >> RAMLS5 | RAMLS6 | RAMLS7, PAGE = 1
.esysmem : >> RAMLS5 | RAMLS6 | RAMLS7, PAGE = 1
.cio : > RAMLS0, PAGE = 0
.econst : > FLASH_BANK0_SEC6, PAGE = 0, ALIGN(4)
#endif
ramgs0 : > RAMGS0, PAGE = 1
ramgs1 : > RAMGS1, PAGE = 1
#if defined(__TI_EABI__)
.TI.ramfunc : LOAD = FLASH_BANK0_SEC7 | FLASH_BANK0_SEC8 ,
RUN = RAMLS0 ,
LOAD_START(RamfuncsLoadStart),
LOAD_SIZE(RamfuncsLoadSize),
LOAD_END(RamfuncsLoadEnd),
RUN_START(RamfuncsRunStart),
RUN_SIZE(RamfuncsRunSize),
RUN_END(RamfuncsRunEnd),
PAGE = 0, ALIGN(4)
#else
.TI.ramfunc : LOAD = FLASH_BANK0_SEC7 | FLASH_BANK0_SEC8,
RUN = RAMLS0,
LOAD_START(_RamfuncsLoadStart),
LOAD_SIZE(_RamfuncsLoadSize),
LOAD_END(_RamfuncsLoadEnd),
RUN_START(_RamfuncsRunStart),
RUN_SIZE(_RamfuncsRunSize),
RUN_END(_RamfuncsRunEnd),
PAGE = 0, ALIGN(4)
#endif
/* User Define areas below: */
/* User Define areas above: */
/* Allocate IQ math areas: */
IQmath : > FLASH_BANK0_SEC10|FLASH_BANK0_SEC11, PAGE = 0, ALIGN(4) /* Math Code */
IQmathTables : > FLASH_BANK0_SEC12|FLASH_BANK0_SEC13, PAGE = 0, ALIGN(4)
}
对于.text等每部分的含义,可以参考:DSP28377S_程序从FLASH部分复制到RAM中运行详解
2. _FLASH引用
2.1 mani.c中的引用
头文件部分引用如下
#include "F28x_Project.h"
#include "driverlib.h"
#include "device.h"
#include "IQmathLib.h"
参数部分引用如下(在主函数main之前引用即可)
#include "stddef.h"
#define _FLASH
#ifdef _FLASH
// These are defined by the linker (see device linker command file)
extern uint16_t RamfuncsLoadStart;
extern uint16_t RamfuncsLoadSize;
extern uint16_t RamfuncsRunStart;
#endif
main函数中的函数初始化如下
int main(void)
{
Device_init();
Device_initGPIO();
DINT;
Interrupt_initModule();
Interrupt_initVectorTable();
IER = 0x0000;
IFR = 0x0000;
DELAY_US(1000L);
SPIA_int ();
CAP_int();
ADC_int();
DAC_int();
userGPIO_int ();
EPWM1_int();
EINT; //使能全局中断
ERTM;
while (1)
{
}
}
2.2 device.c中的引用
在头文件申明之后添加如下代码即可
#include "stddef.h"
#define _FLASH
#ifdef _FLASH
// These are defined by the linker (see device linker command file)
extern uint16_t RamfuncsLoadStart;
extern uint16_t RamfuncsLoadSize;
extern uint16_t RamfuncsRunStart;
#endif
2.3 将部分函数移植到RAM中运行
只需要在需要移植的函数之前加上这一句代码即可
__attribute__((ramfunc))
比如:
__attribute__((ramfunc))
uint16_t Get_cap(void)
{
static uint32_t cap1cnt;
static uint32_t cap3cnt;
static uint32_t Period1;
uint32_t speed;
cap1cnt = ECap1Regs.CAP1;
cap3cnt = ECap1Regs.CAP3;
Period1 = cap3cnt - cap1cnt;
speed = (100000000)/Period1;
return speed;
}
可以看到如果RAM空间足够,或者说程序较小,是可以将所用到的函数全部移植到RAM中运行的
3. 编译下载运行一气呵成
附录
28004x_iqmath_flash_link_cmd