(一)#pragma DATA_SECTION
利用CCS進行DSP編程時,如果不指定變量的存儲位置,那么編譯器會自動給變量分配存儲位置,但是,有些時候,需要將某個變量存放到某個特定的位置,這個時候就可以利用#pragma DATA_SECTION指令了。
第一步,利用#pragma DATA_SECTION指令將變量xxxCmdBuf關聯到SECTIONS“ramdata”;
#pragma DATA_SECTION(xxxCmdBuf,"ramdata"); uint16_t xxxCmdBuf[4];
第二步,修改CMD文件使得“ramdata”映射到指定的地址空間。
MEMORY { PAGE 0 : /* Program Memory */ ...... PAGE 1 : /* Data Memory */ ...... RAML_XXXCMD : origin = 0x00BFF0, length = 0x000004 } ...... SECTIONS { ...... ramdata : > RAML_XXXCMD, PAGE = 1 ...... }
只需完成上述簡單的兩步就可以將某個變量指定到特定的位置,一些博文指出采用這種方法時,需要先利用#pragma DATA_SECTION開辟一個空間,然后在定義該空間的大小,即第一步中的兩行代碼是有先后順序的。
TI 的技術文檔《Programming TMS320x28xx and 28xxx Peripherals in C/C++》(SPRAA85D–November 2005–Revised January 2013)對該方法也有相應的描述,如下所示。
The syntax for the DATA_SECTION pragma in C is: #pragma DATA_SECTION (symbol,"section name") The syntax for the DATA_SECTION pragma in C++ is: #pragma DATA_SECTION ("section name")
Example 5. Assigning Variables to Data Sections
/******************************************************************** * Assign variables to data sections using the #pragma compiler statement * C and C++ use different forms of the #pragma statement * When compiling a C++ program, the compiler will define __cplusplus automatically ********************************************************************/ //---------------------------------------- #ifdef __cplusplus #pragma DATA_SECTION("SciaRegsFile") #else #pragma DATA_SECTION(SciaRegs,"SciaRegsFile"); #endif volatile struct SCI_REGS SciaRegs; //---------------------------------------- #ifdef __cplusplus #pragma DATA_SECTION("ScibRegsFile") #else #pragma DATA_SECTION(ScibRegs,"ScibRegsFile"); #endif volatile struct SCI_REGS ScibRegs;
Example 6. Mapping Data Sections to Register Memory Locations
/******************************************************************** * Memory linker .cmd file * Assign the SCI register-file structures to the corresponding memory ********************************************************************/ MEMORY { ... PAGE 1: SCIA : origin = 0x007050, length = 0x000010 /* SCI-A registers */ SCIB : origin = 0x007750, length = 0x000010 /* SCI-B registers */ ... } SECTIONS { ... SciaRegsFile : > SCIA, PAGE = 1 ScibRegsFile : > SCIB, PAGE = 1 ... }
利用#pragma CODE_SECTION指令可以將程序從Flash搬到RAM中運行,從而提高程序執行速率,該方法需要完成以下四步。
第一步,利用#pragma CODE_SECTION指令關聯程序和SECTIONS;
#pragma CODE_SECTION(mainISR,"ramfuncs");
第二步,為鏈接創建相關變量;
// Used for running BackGround in flash, and ISR in RAM extern uint16_t *RamfuncsLoadStart, *RamfuncsLoadEnd, *RamfuncsRunStart;
第三步,復制時間關鍵代碼以及Flash設置代碼到RAM;
// Copy time critical code and Flash setup code to RAM // The RamfuncsLoadStart, RamfuncsLoadEnd, and // RamfuncsRunStart symbols are created by the linker. // Refer to the linker files. memCopy((uint16_t *)&RamfuncsLoadStart,(uint16_t *)&RamfuncsLoadEnd,(uint16_t *)&RamfuncsRunStart);
第四步,修改CMD文件。
SECTIONS { /* Allocate program areas: */ ...... ramfuncs : LOAD = FLASHD, RUN = RAML0_1, LOAD_START(_RamfuncsLoadStart), LOAD_END(_RamfuncsLoadEnd), RUN_START(_RamfuncsRunStart), PAGE = 0
上面代碼中,
LOAD = FLASHD, //指定了要加載程序在Flash里的地址段 RUN = RAML0_1, //指定了在RAM里運行程序的RAM地址段 LOAD_START(_RamfuncsLoadStart), // 所要加載程序在Flash里的初始地址 LOAD_END(_RamfuncsLoadEnd), // 所要加載程序在Flash里的結束地址 RUN_START(_RamfuncsRunStart), // 程序運行的起始地址