Problem:
When using APM8S007, the customer application may want to improve the running speed. APM8S007 provides 2K RAM, and if we run the software in xRAM, it will directly enhance the running efficiency of the code.
Solution
Open the sample project “Flash_To_SRAM_Run” in the SDK of APM8S007X4. To load the function from Flash to RAM, you need to compile the function to the RAM address first, so as to avoid the program jumping back to Flash space after running in RAM due to the AJMP\LJMP instruction. In the example project, the function add_func_inram(u8 cef1, u8 cef2) and the function dec_func_inram(u8 cef1, u8 cef2) are loaded into RAM to run.
Step 1: Compile the function into RAM.
(1)Divide the RAM space into Flash space:
Divide RAM space into Flash space
The RAM space size of APM8S007X4 series is between 0×60000×66FF, the example is to divide 0×60000×62FF into xdata space and 0×63000×66FF into Flash space.
**Note here that the Code space needs to be set to 0×3FFC, and you need to reserve 4 byte at the end for the CRC of the bin file.
Note:**When allocating RAM as Flash, the address needs to be 0×100 aligned and the length needs to be 0×100 aligned.
(2)The function to be loaded into RAM is specified to be compiled into RAM:
Compile function designation into RAM space
In the file “Demo.m51” to find the function to be compiled after the identification of SEGMENT NAME, the identifier of the two functions are:
add_func_inram(?PR?_ADD_FUNC_INRAM?MAIN)
dec_func_inram(?PR?_DEC_FUNC_INRAM?MAIN)
Find SEGMENT NAME in the file “Demo.m51”.
After finding the function’s corresponding SEGMENT NAME, set it to Figure 7.2, spacing each additional segment using “,” and only requiring the first segment to specify the address of the RAM.
Step 2: Operationalizing the program
(1)RAM space and the corresponding Flash storage space declaration:
The following variables are declared in RAM space and Flash space, where the:
l FLASH_TO_RAM_LEN: is the size of the space in which RAM runs as a function, consistent with the configuration in Figure 7.1, set to 0×400, Align according to 0×100;
l sram_func_xdata: is the buf for RAM to run as a function, declared here to facilitate the operation of loading data from Flash to RAM, note that the address needs to be specified later, which is also consistent with the configuration in Figure. set to 0×6300. Addresses are aligned according to 0×100;
l sram_func_code: is RAM as a function when the data is cached into FLASH, the operation will be executed at the time of download, but the project needs to declare the space in advance. It should be noted that the buf also needs to specify the address space, the address space is mapped from 0×60000×66FF to 0×30000×36FF. So if RAM as the starting address space of the function is 0×6300, then the mapped FLASH is 0×3300.
Figure RAM space and Flash space variable declarations
(2)Loading functions from Flash into RAM:
Flash To RAM Function Implementation
Flash To RAM Function Calls
Once the program is running, the Flash functions need to be loaded into RAM. Then the function is just called normally, no other additional operations are needed.
Note:To support the features described above, the GeehyLink driver needs to be upgraded to the
“GeehyLink_keil_C51_driver_install_V1.2.9″ and above can realize the above function.
Step 3: Burning the file with the burner GeehyProgrammer
(1)Since the space of RAM is turned into Flash space, the Flash data of the compiled hex file will definitely exceed the address of 0×4000, so the file needs to be converted。
Open the “Flash_To_SRAM_Run” sample project, go to the “Project\Keil\Release” directory, there will be more BinScript.binScript, FlashTo_Ram.binScript, BinScript.exe and make.bat files than other sample projects. BinScript, FlashToRam.BinScript, BinScript.exe and make.bat files, if you are porting to other projects, you need to port these files over.。
Open Settings in the Edit screen and add the make.bat batch to the compiled call configuration.
Adding a batch to the setup
Users can also run make.batmanually if not added in the above configuration.
(2)Batch modification.
Similarly if the configuration in Figure is modified, the batch configuration file needs to be modified simultaneously, open the file FlashToRam.BinScript and modify it in the corresponding position in Figure.
Figure Modifying batch files
(3)Finally, note that the compiled bin file must be less than or equal to 16K-4, if it is larger than 16K-4, it will report an error if you use a burner to download it. Both hex and bin files can be downloaded.
Figure- Modifying batch files
add_res = add_func_inram(20, 5);
printf("RAM:add_res = %d\r\n", add_res);
dec_res = dec_func_inram(20, 5);
printf("RAM:dec_res = %d\r\n", dec_res);
add_res = add_func_inflash(50, 5);
printf("Flash: add_func = %d\r\n", add_res);
Specific code can be seen in the attached file.