keil 下模拟u-boot的cmd功能

主要实现CMD命令专门分布在一个段内

(转载请注明出处,Dreamxi嵌入式  http://www.cnblogs.com/Dreamxi/p/3507932.html )

代码如下:

 1  typedef struct __init_cmd {  
 2         char        *name;      /* Command Name         */  
 3         void     (*cmd)(void);  
 4     }init_cmd_t;
 5 #define Struct_Section  __attribute__ ((used,section("INIT_CMD"))) 
 6 #define INIT_CMD(name,cmd)  const init_cmd_t    __init_cmd_##name     Struct_Section = {#name,  cmd } 
 7 
 8 
 9 INIT_CMD(test1,Test_parm);
10 INIT_CMD(test2,Test_parm2);
11 INIT_CMD(test3,Test_parm2);
12 extern unsigned char Load$$INIT_SECTION$$Base[];
13 extern unsigned char Load$$INIT_SECTION$$Length[];
14 
15 
16 void Init_all(unsigned long addr,unsigned long len)
17 {
18     init_cmd_t* pCmd;
19     unsigned long i;
20     
21     for(i=0;i<len;)
22     {
23         pCmd = (init_cmd_t*)(addr+i);
24         u_printf("\nadd:0x%x\n",(unsigned long)(pCmd));
25         pCmd->cmd();
26         u_printf("name:%s\n",pCmd->name);
27         i+=sizeof(init_cmd_t);
28     }
29     
30 }
31 
32 
33 
34 int main(void)
35 {
36     int i,RX_status;
37     unsigned long add = (unsigned long)Load$$INIT_SECTION$$Base;
38     unsigned long len = (unsigned long)Load$$INIT_SECTION$$Length;
39 
40 
41     u_printf("add:0x%x  ---len:0x%x\n",add,len);
42     
43     Init_all(add,len);
44     
45     u_printf("\ninitover\n");
46 }
1 void Test_parm(void)
2 {
3     u_printf("this is Test_parm!\n");
4 }
5 
6 void Test_parm2(void)
7 {
8     u_printf("this is Test_parm2!\n");
9 }

sct文件如下:

 1 LR_IROM1 0x08000000 0x00040000  {    ; load region size_region
 2   ER_IROM1 0x08000000 0x00040000  {  ; load address = execution address
 3    *.o (RESET, +First)
 4    *(InRoot$$Sections)
 5    .ANY (+RO)
 6   }
 7   
 8   INIT_SECTION +0 
 9   {
10     *(INIT_CMD)
11   }
12   
13   RW_IRAM1 0x20000000 0x0000C000  {  ; RW data
14    .ANY (+RW +ZI)
15   }
16 }

最后生成的map文件

 

posted on 2014-01-06 21:54  Dreamxi  阅读(1286)  评论(0编辑  收藏  举报