在上三篇《移植初步:环境搭建》《STM3210E平台构建》《调试初步:点亮LED灯》文章中,我们介绍了如何搭建开发环境,并初步写了测试代码,下一步我们将根据Cortex-M3的架构特点,修改启动代码和重写中断向量表。

   Cortex-M3的中断架构非常有特色,芯片内建一个嵌套向量中断控制器NVIC(Nested Vectored Interrupt Controller),它与内核是紧耦合的,提供如下的功能:可嵌套中断支持,向量中断支持,动态优先级调整支持,中断延迟大大缩短,中断可屏蔽。NVIC 支持240 个优先级可动态配置的中断,每个中断的优先级有256 个选择。低延迟的中断处理可以通过紧耦合的NVIC 和处理器内核接口来实现,让新进的中断可以得到有效的处理。NVIC通过时刻关注压栈(嵌套)中断来实现中断的末尾连锁(tail-chaining)。

   .Net Micro Framework的中断处理构建在Cortex-M3的中断架构之上,将大大提升的.Net Micro Framework的实时性能,不过由于架构全新,需要大幅度改写.Net Micro Framework原有中断处理代码,不破不立,这对性能有所诟病的.Net Micro Framework也许是个好事情。

    下面就新中断架构,进行代码改写。

 

    1、修改FirstEntry.s代码

    由于废弃了.\devicecode\cores\arm\assemblycode\thumb2\rvd_s\VectorsHanlers.s 所以为了编译通过,我们要在同目录下的FirstEntry.s中添加一些代码,

在文件头添加如下代码:    

    

  1. EXPORT  HARD_Breakpoint  
  2.  
  3.     ;IMPORT  HARD_Breakpoint_Handler   ; HARD_Breakpoint_Handler(unsigned int*, unsigned int, unsigned int)  
  4.  

 

 

在文件尾“END”命令前添加如下代码:

  

  1. ;AREA ||i.HARD_Breakpoint||, CODE, READONLY     ; void HARD_Breakpoint()  
  2.  
  3. RD_Breakpoint  
  4.  
  5.   ; on entry, were are being called from C/C++ in system mode  
  6.  
  7.   ;b       HARD_Breakpoint_Handler     ; address of vector routine in C to jump to, never expect to return  

 

 

    2、重写中断向量表(VectorsTrampolines.s)

        重写同目录下的VectorsTrampolines.s文件为如下内容:

   

  1.   ; 导入异常或中断处理程序  
  2.  
  3.          IMPORT  NMIException  
  4.  
  5.          IMPORT  HardFaultException  
  6.  
  7.          IMPORT  MemManageException  
  8.  
  9.          IMPORT  BusFaultException  
  10.  
  11.          IMPORT  UsageFaultException  
  12.  
  13.          IMPORT  SVCHandler  
  14.  
  15.          IMPORT  DebugMonitor  
  16.  
  17.          IMPORT  PendSVC  
  18.  
  19.          IMPORT  SysTickHandler  
  20.  
  21.          IMPORT  WWDG_IRQHandler  
  22.  
  23.          IMPORT  PVD_IRQHandler  
  24.  
  25.          IMPORT  TAMPER_IRQHandler  
  26.  
  27.          IMPORT  RTC_IRQHandler  
  28.  
  29.          IMPORT  FLASH_IRQHandler  
  30.  
  31.          IMPORT  RCC_IRQHandler  
  32.  
  33.          IMPORT  EXTI0_IRQHandler  
  34.  
  35.          IMPORT  EXTI1_IRQHandler  
  36.  
  37.          IMPORT  EXTI2_IRQHandler  
  38.  
  39.          IMPORT  EXTI3_IRQHandler  
  40.  
  41.          IMPORT  EXTI4_IRQHandler  
  42.  
  43.          IMPORT  DMA1_Channel1_IRQHandler  
  44.  
  45.          IMPORT  DMA1_Channel2_IRQHandler  
  46.  
  47.          IMPORT  DMA1_Channel3_IRQHandler  
  48.  
  49.          IMPORT  DMA1_Channel4_IRQHandler  
  50.  
  51.          IMPORT  DMA1_Channel5_IRQHandler  
  52.  
  53.          IMPORT  DMA1_Channel6_IRQHandler  
  54.  
  55.          IMPORT  DMA1_Channel7_IRQHandler  
  56.  
  57.          IMPORT  ADC1_2_IRQHandler  
  58.  
  59.          IMPORT  USB_HP_CAN_TX_IRQHandler  
  60.  
  61.          IMPORT  USB_LP_CAN_RX0_IRQHandler  
  62.  
  63.          IMPORT  CAN_RX1_IRQHandler  
  64.  
  65.          IMPORT  CAN_SCE_IRQHandler  
  66.  
  67.          IMPORT  EXTI9_5_IRQHandler  
  68.  
  69.          IMPORT  TIM1_BRK_IRQHandler  
  70.  
  71.          IMPORT  TIM1_UP_IRQHandler  
  72.  
  73.          IMPORT  TIM1_TRG_COM_IRQHandler  
  74.  
  75.          IMPORT  TIM1_CC_IRQHandler  
  76.  
  77.          IMPORT  TIM2_IRQHandler  
  78.  
  79.          IMPORT  TIM3_IRQHandler  
  80.  
  81.          IMPORT  TIM4_IRQHandler  
  82.  
  83.          IMPORT  I2C1_EV_IRQHandler  
  84.  
  85.          IMPORT  I2C1_ER_IRQHandler  
  86.  
  87.          IMPORT  I2C2_EV_IRQHandler  
  88.  
  89.          IMPORT  I2C2_ER_IRQHandler  
  90.  
  91.          IMPORT  SPI1_IRQHandler  
  92.  
  93.          IMPORT  SPI2_IRQHandler  
  94.  
  95.          IMPORT  USART1_IRQHandler  
  96.  
  97.          IMPORT  USART2_IRQHandler  
  98.  
  99.          IMPORT  USART3_IRQHandler  
  100.  
  101.          IMPORT  EXTI15_10_IRQHandler  
  102.  
  103.          IMPORT  RTCAlarm_IRQHandler  
  104.  
  105.          IMPORT  USBWakeUp_IRQHandler  
  106.  
  107.          IMPORT  TIM8_BRK_IRQHandler  
  108.  
  109.          IMPORT  TIM8_UP_IRQHandler  
  110.  
  111.          IMPORT  TIM8_TRG_COM_IRQHandler  
  112.  
  113.          IMPORT  TIM8_CC_IRQHandler  
  114.  
  115.          IMPORT  ADC3_IRQHandler  
  116.  
  117.          IMPORT  FSMC_IRQHandler  
  118.  
  119.          IMPORT  SDIO_IRQHandler  
  120.  
  121.          IMPORT  TIM5_IRQHandler  
  122.  
  123.          IMPORT  SPI3_IRQHandler  
  124.  
  125.          IMPORT  UART4_IRQHandler  
  126.  
  127.          IMPORT  UART5_IRQHandler  
  128.  
  129.          IMPORT  TIM6_IRQHandler  
  130.  
  131.          IMPORT  TIM7_IRQHandler  
  132.  
  133.          IMPORT  DMA2_Channel1_IRQHandler  
  134.  
  135.          IMPORT  DMA2_Channel2_IRQHandler  
  136.  
  137.          IMPORT  DMA2_Channel3_IRQHandler  
  138.  
  139.          IMPORT  DMA2_Channel4_5_IRQHandler  
  140.  
  141.    
  142.  
  143.    
  144.  
  145.     EXPORT  ARM_Vectors  
  146.  
  147.    
  148.  
  149.     IMPORT  StackTop  
  150.  
  151.     IMPORT  EntryPoint  
  152.  
  153.       
  154.  
  155. ;*****************************************************************************  
  156.  
  157.    
  158.  
  159.     AREA |.text|, CODE, READONLY  
  160.  
  161.    
  162.  
  163.     ;向量表   
  164.  
  165. ARM_Vectors  
  166.  
  167.          DCD  StackTop                   ; Top of Stack  栈顶  
  168.  
  169.          DCD  EntryPoint                                    ; 复位  
  170.  
  171.          DCD  NMIException  
  172.  
  173.          DCD  HardFaultException  
  174.  
  175.          DCD  MemManageException  
  176.  
  177.          DCD  BusFaultException  
  178.  
  179.          DCD  UsageFaultException  
  180.  
  181.          DCD  0                 ; Reserved  
  182.  
  183.          DCD  0                 ; Reserved  
  184.  
  185.          DCD  0                 ; Reserved  
  186.  
  187.          DCD  0                 ; Reserved  
  188.  
  189.          DCD  SVCHandler  
  190.  
  191.          DCD  DebugMonitor  
  192.  
  193.          DCD  0                 ; Reserved  
  194.  
  195.          DCD  PendSVC  
  196.  
  197.          DCD  SysTickHandler  
  198.  
  199.          DCD  WWDG_IRQHandler  
  200.  
  201.          DCD  PVD_IRQHandler  
  202.  
  203.          DCD  TAMPER_IRQHandler  
  204.  
  205.          DCD  RTC_IRQHandler  
  206.  
  207.          DCD  FLASH_IRQHandler  
  208.  
  209.          DCD  RCC_IRQHandler  
  210.  
  211.          DCD  EXTI0_IRQHandler  
  212.  
  213.          DCD  EXTI1_IRQHandler  
  214.  
  215.          DCD  EXTI2_IRQHandler  
  216.  
  217.          DCD  EXTI3_IRQHandler  
  218.  
  219.          DCD  EXTI4_IRQHandler  
  220.  
  221.          DCD  DMA1_Channel1_IRQHandler  
  222.  
  223.          DCD  DMA1_Channel2_IRQHandler  
  224.  
  225.          DCD  DMA1_Channel3_IRQHandler  
  226.  
  227.          DCD  DMA1_Channel4_IRQHandler  
  228.  
  229.          DCD  DMA1_Channel5_IRQHandler  
  230.  
  231.          DCD  DMA1_Channel6_IRQHandler  
  232.  
  233.          DCD  DMA1_Channel7_IRQHandler  
  234.  
  235.          DCD  ADC1_2_IRQHandler  
  236.  
  237.          DCD  USB_HP_CAN_TX_IRQHandler  
  238.  
  239.          DCD  USB_LP_CAN_RX0_IRQHandler  
  240.  
  241.          DCD  CAN_RX1_IRQHandler  
  242.  
  243.          DCD  CAN_SCE_IRQHandler  
  244.  
  245.          DCD  EXTI9_5_IRQHandler  
  246.  
  247.          DCD  TIM1_BRK_IRQHandler  
  248.  
  249.          DCD  TIM1_UP_IRQHandler  
  250.  
  251.          DCD  TIM1_TRG_COM_IRQHandler  
  252.  
  253.          DCD  TIM1_CC_IRQHandler  
  254.  
  255.          DCD  TIM2_IRQHandler  
  256.  
  257.          DCD  TIM3_IRQHandler  
  258.  
  259.          DCD  TIM4_IRQHandler  
  260.  
  261.          DCD  I2C1_EV_IRQHandler  
  262.  
  263.          DCD  I2C1_ER_IRQHandler  
  264.  
  265.          DCD  I2C2_EV_IRQHandler  
  266.  
  267.          DCD  I2C2_ER_IRQHandler  
  268.  
  269.          DCD  SPI1_IRQHandler  
  270.  
  271.          DCD  SPI2_IRQHandler  
  272.  
  273.          DCD  USART1_IRQHandler  
  274.  
  275.          DCD  USART2_IRQHandler  
  276.  
  277.          DCD  USART3_IRQHandler  
  278.  
  279.          DCD  EXTI15_10_IRQHandler  
  280.  
  281.          DCD  RTCAlarm_IRQHandler  
  282.  
  283.          DCD  USBWakeUp_IRQHandler   
  284.  
  285.          DCD  TIM8_BRK_IRQHandler  
  286.  
  287.          DCD  TIM8_UP_IRQHandler  
  288.  
  289.          DCD  TIM8_TRG_COM_IRQHandler  
  290.  
  291.          DCD  TIM8_CC_IRQHandler  
  292.  
  293.          DCD  ADC3_IRQHandler  
  294.  
  295.          DCD  FSMC_IRQHandler  
  296.  
  297.          DCD  SDIO_IRQHandler  
  298.  
  299.          DCD  TIM5_IRQHandler  
  300.  
  301.          DCD  SPI3_IRQHandler  
  302.  
  303.          DCD  UART4_IRQHandler  
  304.  
  305.          DCD  UART5_IRQHandler  
  306.  
  307.          DCD  TIM6_IRQHandler  
  308.  
  309.          DCD  TIM7_IRQHandler  
  310.  
  311.          DCD  DMA2_Channel1_IRQHandler  
  312.  
  313.          DCD  DMA2_Channel2_IRQHandler  
  314.  
  315.          DCD  DMA2_Channel3_IRQHandler  
  316.  
  317.          DCD  DMA2_Channel4_5_IRQHandler  
  318.  
  319.    
  320.  
  321.    
  322.  
  323. ;*****************************************************************************  
  324.  
  325.    
  326.  
  327.     END  
  328.  

3、新建VectorsHandler_Temp库文件

    在.\DeviceCode\Targets\Native\CortexM3\DeviceCode目录下,新建目录VectorsHandler_Temp,并新建两个文件dotNetMF.proj(编译配置,可参见其它同类文件),VectorsHandler_Temp.c(空的中断处理函数)。

 

    VectorsHandler_Temp.c的内容如下:

   

  1. void NMIException(void) {}  
  2.  
  3.     void HardFaultException(void){while (1);}  
  4.  
  5.     void MemManageException(void){while (1);}  
  6.  
  7.     void BusFaultException(void){while (1);}  
  8.  
  9.     void UsageFaultException(void){while (1);}  
  10.  
  11.     void DebugMonitor(void){}  
  12.  
  13.     void SVCHandler(void){}  
  14.  
  15.    
  16.  
  17.     // 省略部分代码 .....  
  18.  
  19.    
  20.  
  21.     void TIM6_IRQHandler(void){}  
  22.  
  23.     void TIM7_IRQHandler(void){}  
  24.  
  25.     void DMA2_Channel1_IRQHandler(void){}  
  26.  
  27.     void DMA2_Channel2_IRQHandler(void){}  
  28.  
  29.     void DMA2_Channel3_IRQHandler(void){}  
  30.  
  31.     void DMA2_Channel4_5_IRQHandler(void){}  
  32.  
  33.       
  34.    
  35.  

 

以上函数为暂且为空,以后我们在根据需要再添加相应的中断处理代码。
4、dotNetMF.proj文件调整

   (1)对.\DeviceCode\cores\arm\dotNetMF.proj文件做如下调整:

 

  1. <ItemGroup Condition="'$(INSTRUCTION_SET)'=='thumb2'"> 
  2.  
  3.    <Compile Include="AssemblyCode\thumb2\$(AS_SUBDIR)\FirstEntry.s" /> 
  4.  
  5.    <Compile Include="AssemblyCode\thumb2\$(AS_SUBDIR)\IDelayLoop.s" /> 
  6.  
  7.    <Compile Include="AssemblyCode\thumb2\$(AS_SUBDIR)\Sampling_Profiler_RAM.s" /> 
  8.  
  9.    <Compile Include="AssemblyCode\thumb2\$(AS_SUBDIR)\VectorsHandlers.s" /> 
  10.  
  11.    <Compile Include="AssemblyCode\thumb2\$(AS_SUBDIR)\VectorsTrampolines.s" /> 
  12.  
  13.  </ItemGroup> 

  修改为:

  

  1. <ItemGroup Condition="'$(INSTRUCTION_SET)'=='thumb2'"> 
  2.  
  3.     <Compile Include="AssemblyCode\thumb2\$(AS_SUBDIR)\FirstEntry.s" /> 
  4.  
  5.     <Compile Include="AssemblyCode\thumb2\$(AS_SUBDIR)\VectorsTrampolines.s" /> 
  6.  
  7.   </ItemGroup> 
  8.  
  9.      
  10.  
  11.   <ItemGroup> 
  12.  
  13.     <HFiles Include="..\..\Initialization\MasterConfig.h" /> 
  14.  
  15.     <Compile Include="Diagnostics\Aborts.cpp" /> 
  16.  
  17.     <Compile Include="Diagnostics\FIQ_Profiler.cpp" /> 
  18.  
  19.     <Compile Include="Diagnostics\RamTest.cpp" /> 
  20.  
  21.     <Compile Include="Diagnostics\RamTest.h" /> 
  22.  
  23.     <LIB_FIRSTENTRY_OBJ Include="$(OBJ_DIR)\FirstEntry.$(OBJ_EXT)" /> 
  24.  
  25.   </ItemGroup> 
  26.  

  修改为:

  

  1. <ItemGroup> 
  2.  
  3.     <LIB_FIRSTENTRY_OBJ Include="$(OBJ_DIR)\FirstEntry.$(OBJ_EXT)" /> 
  4.  
  5.   </ItemGroup> 
  6.  

 

  (2)在.\Solutions\STM3210E\NativeSample\NativeSample.proj文件中添加如下内容:

 

  1. <ItemGroup> 
  2.  
  3.    <RequiredProjects Include="$(SPOCLIENT)\DeviceCode\Targets\Native\CortexM3\DeviceCode\VectorsHandler_Temp\dotNetMF.proj" /> 
  4.  
  5.    <DriverLibs Include="VectorsHandler_Temp.$(LIB_EXT)" /> 
  6.  
  7.  </ItemGroup>    
  8.  
  9.  

5、编译

   编译我们的代码,看看是否能编译成功。编译成功后,在用MDK下载的开发板试一试,如果运行正常,这一步工作将告一段落。下一步我们将编写SRAM初始化代码和设置NVIC中断表偏移。