上篇《修改启动代码&重写向量表》文章中我们介绍了中断向量表的编写,这次我们编写SRAM初始化代码,在《调试初步:点亮LED灯》文章中,我们提到了MDK脚本代码,其实这部分代码和MDK脚本代码实现类似的功能。

   1、编写PreStackInit代码

   我们从.\DeviceCode\Drivers\Stubs\Processor\stubs_PreStackInit目录到.\DeviceCode\Targets\Native\CortexM3\DeviceCode,并修改目录的名字为PreStackInit,下一步我们在子目录RVD_S下的PreStackInit.s中添加如下汇编代码:

             EXPORT  PreStackInit

         IMPORT  PreStackInit_Exit_Pointer

 

         PRESERVE8

 

         AREA SectionForBootstrapOperations, CODE, READONLY

 

         ENTRY

 

PreStackInit

 

; 初始化SRAM

; 使能FSMC时钟

; Enable FSMC clock

         LDR R0,= 0x00000114

         LDR R1,= 0x40021014

         STR R0,[R1]                    

; 使能FSMC相关的GPIO的时钟               

; Enable GPIOD, GPIOE, GPIOF and GPIOG clocks

         LDR R0,= 0x000001E0

         LDR R1,= 0x40021018

         STR R0,[R1]      

 

; SRAM Data lines, NOE and NWE configuration

; SRAM Address lines configuration

; NOE and NWE configuration  

; NE3 configuration

; NBL0, NBL1 configuration

 

         LDR R0,= 0x44BB44BB

         LDR R1,= 0x40011400

         STR R0,[R1]            

 

         LDR R0,= 0xBBBBBBBB

         LDR R1,= 0x40011404

         STR R0,[R1]            

 

         LDR R0,= 0xB44444BB

         LDR R1,= 0x40011800

         STR R0,[R1]            

 

         LDR R0,= 0xBBBBBBBB

         LDR R1,= 0x40011804

         STR R0,[R1]            

 

         LDR R0,= 0x44BBBBBB

         LDR R1,= 0x40011C00

         STR R0,[R1]       

 

         LDR R0,= 0xBBBB4444

         LDR R1,= 0x40011C04

         STR R0,[R1]       

 

         LDR R0,= 0x44BBBBBB

         LDR R1,= 0x40012000

         STR R0,[R1]            

 

         LDR R0,= 0x44444B44

         LDR R1,= 0x40012004

         STR R0,[R1]     

                                             

; FSMC Configuration  

; Enable FSMC Bank1_SRAM Bank

                  

         LDR R0,= 0x00001000

         LDR R1,= 0xA0000010

         STR R0,[R1]     

 

         LDR R0,= 0x00000200

         LDR R1,= 0xA0000014

         STR R0,[R1]     

                  

         LDR R0,= 0x0FFFFFFF

         LDR R1,= 0xA0000114

         STR R0,[R1]     

                  

         LDR R0,= 0x00001001

         LDR R1,= 0xA0000010

         STR R0,[R1]     

 

; 重设向量表的地址 (定位到SRAM起始地址 0x20000000)

         IF TargetLocation="RAM"

             LDR R0,= 0x20000000

             LDR R1,= 0xE000ED08

             STR R0,[R1]     

         ENDIF

 

PreStackEnd

         B     PreStackInit_Exit_Pointer

 

         END

 

其中下面的代码很重要,如果你通过RAM进行调试,并且该中断表位于RAM其实位置,则需要执行该命令。

        IF TargetLocation="RAM"

             LDR R0,= 0x20000000

             LDR R1,= 0xE000ED08

             STR R0,[R1]     

         ENDIF

2、修改.\Solutions\STM3210E\NativeSample\NativeSample.proj文件

   在NativeSample.proj文件中作如下修改:

   <ItemGroup>

    <RequiredProjects Include="$(SPOCLIENT)\DeviceCode\Drivers\Stubs\Processor\stubs_PreStackInit\dotNetMF.proj" />

    <DriverLibs Include="cpu_prestackinit_stubs.$(LIB_EXT)" />

   </ItemGroup>

   修改为:

   <ItemGroup>

    <RequiredProjects Include="$(SPOCLIENT)\DeviceCode\Targets\Native\CortexM3\DeviceCode\PreStackInit\dotNetMF.proj" />

    <DriverLibs Include="PreStackInit.$(LIB_EXT)" />

   </ItemGroup>

 

3、修改.\Solutions\STM3210E\NativeSample\Scatterfile_tools_mdk.xml文件

   <ExecRegion Name="ER_RAM_RO" Base="0x00000000" Options="ABSOLUTE" Size="0x0005ffe0">

   修改为:

   <ExecRegion Name="ER_RAM_RO" Base="0x20000000" Options="ABSOLUTE" Size="">

 

4、编译测试

   上一篇文章和本篇文章所完成的操作其实只是铺垫,外在并没有什么效果表现。不过我们下一步将要做的设置芯片时钟将是重要一步,经过初始化时钟后,我们的开发板将会运行在72M的最高频率上,这时我们原先做LED闪烁节奏将大大提速。