SRAM初始化和设置NVIC中断表偏移

//=====================================================================
//TITLE:
//    SRAM初始化和设置NVIC中断表偏移
//AUTHOR:
//    norains
//DATE:
//    Friday  8-October-2010
//Environment:
//    KEIL MDK 4.0
//    .NET Micro Framework Porting 4.1
//    RedCow Board
//=====================================================================

 

 

     关于SRAM的初始化和NVIC的设置,其实在叶帆的《【.Net Micro Framework PortingKit - 05】SRAM初始化&设置NVIC中断表偏移》(http://blog.csdn.net/yefanqiu/archive/2010/01/03/5125786.aspx)已经有过详细的描述,只不过我并不太喜欢直接在微软的代码上直接修改,所以才有了本文的一系列繁琐的设置。两种方法各有利弊,大家可以随自己的喜好进行选择。另外,在此注明一下,本文所涉及到的所有代码部分,都来源于叶帆的文章。

 

    1.在我们自己的Solution下以此路径建立一系列文件夹:./Solutions/[MySolution]/DeviceCode/Drivers/Stubs/Processor/stubs_PreStackInit

 


    2.将./Solution /DeviceCode/Drivers/Stubs/Processor/stubs_PreStackInit/dotNetMF.proj拷贝到./Solutions/[MySolution]/DeviceCode/Drivers/Stubs/Processor/stubs_PreStackInit,将dotNetMF.proj文件中相关联地路径改为绝对路径。可能修改有点麻烦,现在将完整的内容列举如下,需要注意的是STM32F103ZE_RedCow是我的Solution名字,请各位读者根据实际情况进行改写。完整内容如下:

<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <AssemblyName>cpu_prestackinit_stubs</AssemblyName>
    <ProjectGuid>{19b183cc-474f-47ce-a544-390acc3e2b62}</ProjectGuid>
    <Size>
    </Size>
    <Description>Pre stack initialization stub driver</Description>
    <Level>HAL</Level>
    <LibraryFile>cpu_prestackinit_stubs.$(LIB_EXT)</LibraryFile>
    <ProjectPath>$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Drivers/Stubs/Processor/stubs_PreStackInitdotNetMF.proj</ProjectPath>
    <ManifestFile>cpu_prestackinit_stubs.$(LIB_EXT).manifest</ManifestFile>
    <Groups>Processor/stubs</Groups>
    <LibraryCategory>
      <MFComponent xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Name="PreStackInit_HAL" Guid="{5A9E3217-7D6D-40de-A685-28BA44D31E9A}" ProjectPath="" Conditional="" xmlns="">
        <VersionDependency xmlns="
http://schemas.microsoft.com/netmf/InventoryFormat.xsd">
          <Major>4</Major>
          <Minor>0</Minor>
          <Revision>0</Revision>
          <Build>0</Build>
          <Extra />
          <Date>2009-04-30</Date>
          <Author>Microsoft</Author>
        </VersionDependency>
        <ComponentType xmlns="
http://schemas.microsoft.com/netmf/InventoryFormat.xsd">LibraryCategory</ComponentType>
      </MFComponent>
    </LibraryCategory>
    <Documentation>
    </Documentation>
    <PlatformIndependent>False</PlatformIndependent>
    <CustomFilter>
    </CustomFilter>
    <Required>False</Required>
    <IgnoreDefaultLibPath>False</IgnoreDefaultLibPath>
    <IsStub>True</IsStub>
    <HasLibraryCategory>True</HasLibraryCategory>
    <Directory>/Solutions/STM32F103ZE_RedCow/DeviceCode/Drivers/Stubs/Processor/stubs_PreStackInit</Directory>
    <PlatformIndependentBuild>false</PlatformIndependentBuild>
    <Version>4.0.0.0</Version>
  </PropertyGroup>
  <Import Project="$(SPOCLIENT)/tools/targets/Microsoft.SPOT.System.Settings" />
  <PropertyGroup>
    <OutputType  >Library</OutputType>
  </PropertyGroup>
  <ItemGroup>
    <Compile Include="$(SPOCLIENT)/DeviceCode/Drivers/Stubs/Processor/stubs_PreStackInit/$(AS_SUBDIR)/PreStackInit_stub.s" />
  </ItemGroup>
  <ItemGroup />
  <Import Project="$(SPOCLIENT)/tools/targets/Microsoft.SPOT.System.Targets" />
</Project>

 


    3.打开NativeSample.proj文件,将依赖的工程做修改。

 

原语句:
<RequiredProjects Include="$(SPOCLIENT)/DeviceCode/Drivers/Stubs/Processor/stubs_PreStackInit/dotNetMF.proj" />

 

更改为:
<RequiredProjects Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Drivers/Stubs/Processor/stubs_PreStackInit/dotNetMF.proj" />

 修改完毕之后,最后调用msbuild命令来编译一次,以确定修改正确。当确定修改正确之后,我们就可以进入下一步了。

 


    4.将./DeviceCode/Drivers/Stubs/Processor/stubs_PreStackInit/RVD_S/PreStackInit_stub.S文件拷贝到./Solutions/STM32F103ZE_RedCow/DeviceCode/Drivers/Stubs/Processor/stubs_PreStackInit/RVDS路径。然后再次用文本编辑器打开之前我们修改好的./Solutions/[MySolution]/DeviceCode/Drivers/Stubs/Processor/stubs_PreStackInit/dotNetMF.proj文件,并做如下修改。

 

     原语句:
<Compile Include="$(SPOCLIENT)/DeviceCode/Drivers/Stubs/Processor/stubs_PreStackInit/$(AS_SUBDIR)/PreStackInit_stub.s" />

 

     修改为:
<Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Drivers/Stubs/Processor/stubs_PreStackInit/RVDS/PreStackInit_stub.s" />

 

    修改完毕后,再一次编译NativeSample工程,以确保路径修改正确。当这一步也能够正常编译通过之后,那么接下来我们就可以着手修改PreStackInit_stub.s文件了。

 


    5. 用文本编辑器打开拷贝过来的PreStackInit_stub.s文件,完善PreStackInit函数。为了简单起见,这里直接将所有的源代码贴出来:

 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Copyright (c) Microsoft Corporation.  All rights reserved.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

    EXPORT  PreStackInit

    IMPORT  PreStackInit_Exit_Pointer

    PRESERVE8

    AREA SectionForBootstrapOperations, CODE, READONLY

    ENTRY

    ; ARM directive is only valid for ARM/THUMB processor, but not CORTEX
    IF :DEF:COMPILE_ARM :LOR: :DEF:COMPILE_THUMB 
    ARM
    ENDIF   

PreStackInit


    ;*************************************************************************
    ;
    ; TODO: Enter your pre stack initialization code here (if needed)
    ;       e.g. SDRAM initialization if you don't have/use SRAM for the stack
    ;
   
    ;Initialize the SRAM
  ; Enable FSMC clock
  LDR R0,= 0x00000114
  LDR R1,= 0x40021014
  STR R0,[R1]                 
                
  ; 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]    
      
  ; Reset the vector list address to the begin of SRAM which is 0x20000000
  IF TargetLocation="RAM"
      LDR R0,= 0x20000000
      LDR R1,= 0xE000ED08
      STR R0,[R1]    
  ENDIF

    ;*************************************************************************
    ; DO NOT CHANGE THE FOLLOWING CODE! we can not use pop to return because we
    ; loaded the PC register to get here (since the stack has not been initialized). 
    ; Make sure the PreStackInit_Exit_Pointer is within range and
    ; in the SectionForBootstrapOperations
    ; go back to the firstentry(_loader) code
    ;
    ;
PreStackEnd
    B     PreStackInit_Exit_Pointer

    ;
    ;**************************************************************************
    IF :DEF:COMPILE_THUMB 
    THUMB
    ENDIF  


    END

 

对于STM32H723,使用Backup SRAM需要进行如下初始化步骤: 1. 使能Vbat电源时钟,例如: ```c __HAL_RCC_PWR_CLK_ENABLE(); HAL_PWREx_EnableVbat(); ``` 2. 使能备份域时钟,例如: ```c __HAL_RCC_BKPRAM_CLK_ENABLE(); ``` 3. 配置备份域的访问权限,例如: ```c HAL_PWR_EnableBkUpAccess(); ``` 4. 配置Backup SRAM的大小,例如: ```c HAL_PWR_EnableBkUpRegulator(); __HAL_RCC_BKPRAM_CLK_ENABLE(); HAL_RCCEx_SelectLSEClock(RCC_LSE_LOWPOWER_MODE, RCC_LSE_DIV2); // 选择LSE作为RTC时钟源 HAL_RCCEx_EnableLSECSS(); // 使能LSE时钟监测 while(!HAL_RCCEx_LSECSS_GetFlagStatus()); // 等待LSE时钟监测完成 PWR_BackupAccessCmd(ENABLE); __HAL_RCC_BKPRAM_CLK_ENABLE(); /* 设置备份SRAM的容量为4KB */ HAL_StatusTypeDef status = HAL_PWREx_EnableBkUpReg(BKP_DR1, PWR_BKPREG_SIZE_4B); if(status != HAL_OK) { /* 备份SRAM初始化失败 */ while(1); } ``` 需要注意的是,备份SRAM的容量在STM32H723中通过调用`HAL_PWREx_EnableBkUpReg()`函数来设置,该函数的第一个参数为备份寄存器的地址,第二个参数为备份寄存器的大小,可以选择2B、4B或16B。在上述代码中,我们将备份SRAM的容量设置为4KB,备份寄存器的地址为BKP_DR1。 5. 读写Backup SRAM,例如: ```c #define BKPSRAM_BASE (0x40024000UL) #define BKPSRAM_SIZE (0x1000UL) // 4KB uint32_t* p_bkpsram = (uint32_t*)BKPSRAM_BASE; *p_bkpsram = 0x12345678; ``` 在进行Backup SRAM的读写操作时,需要注意地址的范围不要超出备份SRAM的容量,否则会出现未定义的行为。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值