查看: 5287|回复: 2

[已解决] 关于把全局变量从ROM区拷贝到RAM的问题(已解决)

[复制链接]

该用户从未签到

35

主题

83

帖子

0

新手上路

Rank: 1

积分
0
最后登录
1970-1-1
发表于 2013-10-11 22:14:20 | 显示全部楼层 |阅读模式
我们都知道,全局变量是烧在ROM区的,在芯片启动以后,全局变量是手动从ROM区拷贝到RAM区的,既然全局变量是可以修改值得,那么我们在程序中用到的全局变量应该是拷贝到RAM区中的全局变量,但是烧写的时候是烧在ROM中的,那么程序是怎么定位到RAM区中的(从ROM到RAM的拷贝手动的,程序应该从源地址ROM中取值,而不是RAM中)?
我知道答案 目前已有1人回答
回复

使用道具 举报

  • TA的每日心情
    难过
    2021-12-15 16:01
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]初来乍到

    305

    主题

    4701

    帖子

    0

    中级会员

    Rank: 3Rank: 3

    积分
    377
    最后登录
    2023-8-16
    发表于 2013-10-12 08:59:18 | 显示全部楼层

    RE:关于把全局变量从ROM区拷贝到RAM的问题

    变量只能定义在RAM中,常量可以定义在ROM中,因为ROM中的数据不能改动,除非你用FLASH的擦写命令来进行修改。
    该会员没有填写今日想说内容.
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    35

    主题

    508

    帖子

    0

    金牌会员

    Rank: 6Rank: 6

    积分
    2167
    最后登录
    1970-1-1
    发表于 2013-10-14 14:41:17 | 显示全部楼层

    回复:关于把全局变量从ROM区拷贝到RAM的问题

     不知道你用的是什么编译工具?CodeWarrior还是IAR?飞思卡尔例程中提供了数据从ROM拷贝到RAM的代码。
     
     
    /********************************************************************/
    void
    common_startup(void)
    {
     
    #if (defined(CW))
        extern char __START_BSS[];
        extern char __END_BSS[];
        extern uint32 __DATA_ROM[];
        extern uint32 __DATA_RAM[];
        extern char __DATA_END[];
    #endif
     
        /* Declare a counter we'll use in all of the copy loops */
        uint32 n;
     
        /* Declare pointers for various data sections. These pointers
         * are initialized using values pulled in from the linker file
         */
        uint8 * data_ram, * data_rom, * data_rom_end;
        uint8 * bss_start, * bss_end;
     
     
        /* Addresses for VECTOR_TABLE and VECTOR_RAM come from the linker file */
        extern uint32 __VECTOR_TABLE[];
        extern uint32 __VECTOR_RAM[];
     
        /* Copy the vector table to RAM */
        if (__VECTOR_RAM != __VECTOR_TABLE)
        {
            for (n = 0; n < 0x410; n++)
                __VECTOR_RAM[n] = __VECTOR_TABLE[n];
        }
        /* Point the VTOR to the new copy of the vector table */
        write_vtor((uint32)__VECTOR_RAM);
     
        /* Get the addresses for the .data section (initialized data section) */
    #if (defined(CW))
            data_ram = (uint8 *)__DATA_RAM;
       data_rom = (uint8 *)__DATA_ROM;
       data_rom_end  = (uint8 *)__DATA_END; /* This is actually a RAM address in CodeWarrior */
       n = data_rom_end - data_ram;
        #elif (defined(IAR))
    data_ram = __section_begin(".data");
    data_rom = __section_begin(".data_init");
    data_rom_end = __section_end(".data_init");
    n = data_rom_end - data_rom;
    #endif
     
    /* Copy initialized data from ROM to RAM */
    while (n--)
    *data_ram++ = *data_rom++;
     
     
        /* Get the addresses for the .bss section (zero-initialized data) */
    #if (defined(CW))
    bss_start = (uint8 *)__START_BSS;
    bss_end = (uint8 *)__END_BSS;
    #elif (defined(IAR))
    bss_start = __section_begin(".bss");
    bss_end = __section_end(".bss");
    #endif
     
     
     
     
        /* Clear the zero-initialized data section */
        n = bss_end - bss_start;
        while(n--)
          *bss_start++ = 0;
     
    /* Get addresses for any code sections that need to be copied from ROM to RAM.
    * The IAR tools have a predefined keyword that can be used to mark individual
    * functions for execution from RAM. Add "__ramfunc" before the return type in
    * the function prototype for any routines you need to execute from RAM instead
    * of ROM. ex: __ramfunc void foo(void);
    */
    #if (defined(IAR))
    uint8* code_relocate_ram = __section_begin("CodeRelocateRam");
    uint8* code_relocate = __section_begin("CodeRelocate");
    uint8* code_relocate_end = __section_end("CodeRelocate");
     
    /* Copy functions from ROM to RAM */
    n = code_relocate_end - code_relocate;
    while (n--)
    *code_relocate_ram++ = *code_relocate++;
    #endif
    }
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /3 下一条

    Archiver|手机版|小黑屋|恩智浦技术社区

    GMT+8, 2025-7-19 20:40 , Processed in 0.089597 second(s), 23 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.

    快速回复 返回顶部 返回列表