在Net Micro Framework Porting中使用stm32f10x_stdperiph_lib

//=====================================================================
//TITLE:
//    在Net Micro Framework Porting中使用stm32f10x_stdperiph_lib
//AUTHOR:
//    norains
//DATE:
//    第一版:Thursday  30-September-2010
//    第二版:Friday  8-October-2010
//            修正STM32F103ZE的STM32F10X_CL的定义为STM32F10X_HD
//Environment:
//    KEIL MDK 4.0
//    .NET Micro Framework Porting
//=====================================================================


    熟悉STM3210x开发的朋友可能都知道,ST为了方便开发者,特意发布了一套名为stm32f10x_stdperiph_lib的驱动库。其实库的内容很简单,只是将常用的寄存器操作封装为函数。但这些看似简单的函数,却大大简化了开发者的工作量。既然我们要将.Net Micro Framework移植到STM32F103ZE上,为什么不让它成为我们的助力呢?

 

    不过,要搭建这个环境虽然并不复杂,但对于初学者来说,还是有一定难度的。所以,我们就一步一步来吧!

 

    1.  首先在./Solutions/[MySolution]/DeviceCode/路径下建立一个libraries文件夹,接着在该文件夹新建一个libraries.proj文件,并输入如下内容:
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <AssemblyName>STM32F10x_StdPeriph_Driver</AssemblyName>
    <ProjectGuid>{BFB710BE-EA70-40a3-AED1-2BF6BA70DB0B}</ProjectGuid>
    <Size>
    </Size>
    <Description>STM32F10x StdPeriph Driver</Description>
    <Level>HAL</Level>
    <LibraryFile>STM32F10x_StdPeriph_Driver.$(LIB_EXT)</LibraryFile>
    <ProjectPath>$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/Libraries.proj</ProjectPath>
    <ManifestFile>STM32F10x_StdPeriph_Driver.$(LIB_EXT).manifest</ManifestFile>
    <Groups>STM32F10x</Groups>
    <Documentation>
    </Documentation>
    <PlatformIndependent>False</PlatformIndependent>
    <CustomFilter>
    </CustomFilter>
    <Required>False</Required>
    <IgnoreDefaultLibPath>False</IgnoreDefaultLibPath>
    <IsStub>False</IsStub>
    <Directory>Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries</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>
  <PropertyGroup>
    <ARMBUILD_ONLY>true</ARMBUILD_ONLY>
  </PropertyGroup>
  <ItemGroup>
    <IncludePaths Include="Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/Configure" />
  <IncludePaths Include="Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/inc" />
   <IncludePaths Include="Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/CMSIS/Core/CM3/" />    
  </ItemGroup> 
  <ItemGroup>         
    <Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/CMSIS/Core/CM3/core_cm3.c" />
  <Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/CMSIS/Core/CM3/system_stm32f10x.c" />
  <Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/misc.c" />
  <Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_adc.c" />
  <Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_bkp.c" />
  <Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_can.c" />
  <Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_crc.c" />
  <Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_dac.c" />
  <Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_dbgmcu.c" />
  <Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_dma.c" />
  <Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_exti.c" />
  <Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_flash.c" />
  <Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_fsmc.c" />
  <Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_gpio.c" />
  <Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_i2c.c" />
  <Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_iwdg.c" />
  <Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_pwr.c" />
  <Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_rcc.c" />
  <Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_rtc.c" />
  <Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_sdio.c" />
  <Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_spi.c" />
  <Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_tim.c" />
  <Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_usart.c" />
  <Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_wwdg.c" />   
  </ItemGroup> 
  <ItemGroup />  
  <Import Project="$(SPOCLIENT)/tools/targets/Microsoft.SPOT.System.Targets" /> 
</Project>

 

    2.  解压下载回来的stm32f10x_stdperiph_lib.zip文件,并把Libraries文件夹下的CMSIS和STM32F10x_StdPeriph_Driver拷贝到我们新建的Libraries文件夹,也就是:./Solutions /[MySolution]/ DeviceCode/ Libraries。

 

 

    3.  要使用这个Libraries,还需要定义一个USE_STDPERIPH_DRIVER宏。按理说,在libraries.proj文件中应该能通过如下语句建立相应的C/C++宏:

 

 <ItemGroup>
     <CC_CPP_MARCO_FLAGS Include="USE_STDPERIPH_DRIVER" />
     <CC_CPP_MARCO_FLAGS Include="STM32F10X_HD" />
 </ItemGroup>

    但很可惜的是,经过测试,发现这样的定义无法生效。不知道是不是我的语法有问题,如果大家知道真正的方法,麻烦告诉norains,在此先谢过了。

 

    既然无法在Libraries.proj中动手,就只能手动在.h文件中进行折腾了了。打开./Solutions/[MySolution]/DeviceCode/Libraries/CMSIS/Core/CM3/stm32f10x.H文件,并且将注释掉的USE_STDPERIPH_DRIVER宏恢复,如: 

 #if !defined USE_STDPERIPH_DRIVER /** * @brief Comment the line below if you will not use the peripherals drivers. In this case, these drivers will not be included and the application code will be based on direct access to peripherals registers */ #define USE_STDPERIPH_DRIVER /*norains 2010-9-30 11:47 I don't know how define the macro in .proj file,so I remove the original comment here.*/ #endif

 

     当USE_STDPERIPH_DRIVER宏被启用后,如果没有定义相应的STM32F10X的类型,那么默认就会使用STM32F10X_CL,然而STM32F103ZE并不具备互联网功能,所以需要更改相应的宏定义,如下所示:

#if !defined (STM32F10X_LD) && !defined (STM32F10X_MD) && !defined (STM32F10X_HD) && !defined (STM32F10X_CL) /* #define STM32F10X_LD */ /*!< STM32F10X_LD: STM32 Low density devices */ /* #define STM32F10X_MD */ /*!< STM32F10X_MD: STM32 Medium density devices */ #define STM32F10X_HD /*!< STM32F10X_HD: STM32 High density devices */ /*#define STM32F10X_CL */ /*!< STM32F10X_CL: STM32 Connectivity line devices */ #endif

 

    4  当定义了USE_STDPERIPH_DRIVER宏后,还需要包含一个stm32f10x_conf.h头文件。因为这个是和平台有关的,所以我们独立给它分配个Configure文件夹,也就是位于./Solutions/[MySolution]/ DeviceCode/Libraries/Configure目录。其实这个头文件在下载的stm32f10x_stdperiph_lib包里面有,如果你觉得麻烦,你完全可以自己建立并输入如下内容:

 /** ****************************************************************************** * @file Project/Template/stm32f10x_conf.h * @author MCD Application Team * @version V3.1.2 * @date 09/28/2009 * @brief Library configuration file. ****************************************************************************** * @copy * * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. * * <h2><center>© COPYRIGHT 2009 STMicroelectronics</center></h2> */ /* Define to prevent recursive inclusion -------------------------------------*/ #ifndef __STM32F10x_CONF_H #define __STM32F10x_CONF_H /* Includes ------------------------------------------------------------------*/ /* Uncomment the line below to enable peripheral header file inclusion */ /* #include "stm32f10x_adc.h" */ /* #include "stm32f10x_bkp.h" */ /* #include "stm32f10x_can.h" */ /* #include "stm32f10x_crc.h" */ /* #include "stm32f10x_dac.h" */ /* #include "stm32f10x_dbgmcu.h" */ /* #include "stm32f10x_dma.h" */ #include "stm32f10x_exti.h" /* #include "stm32f10x_flash.h" */ #include "stm32f10x_fsmc.h" #include "stm32f10x_gpio.h" /* #include "stm32f10x_i2c.h" */ /* #include "stm32f10x_iwdg.h" */ /* #include "stm32f10x_pwr.h" */ #include "stm32f10x_rcc.h" /* #include "stm32f10x_rtc.h" */ /* #include "stm32f10x_sdio.h" */ #include "stm32f10x_spi.h" /* #include "stm32f10x_tim.h" */ #include "stm32f10x_usart.h" /* #include "stm32f10x_wwdg.h" */ #include "misc.h" /* High level functions for NVIC and SysTick (add-on to CMSIS functions) */ /* Exported types ------------------------------------------------------------*/ /* Exported constants --------------------------------------------------------*/ /* Uncomment the line below to expanse the "assert_param" macro in the Standard Peripheral Library drivers code */ /* #define USE_FULL_ASSERT 1 */ /* Exported macro ------------------------------------------------------------*/ #ifdef USE_FULL_ASSERT /** * @brief The assert_param macro is used for function's parameters check. * @param expr: If expr is false, it calls assert_failed function * which reports the name of the source file and the source * line number of the call that failed. * If expr is true, it returns no value. * @retval None */ #define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__)) /* Exported functions ------------------------------------------------------- */ void assert_failed(uint8_t* file, uint32_t line); #else #define assert_param(expr) ((void)0) #endif /* USE_FULL_ASSERT */ #endif /* __STM32F10x_CONF_H */ /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/

 

 

    5.  如果我们要在NativeSample工程用上STM32F10x_StdPeriph_Lib的代码,还需要对NativeSample.proj文件进行一些修改。打开NativeSample.proj文件,增加如下语句:

 

<ItemGroup>
    <DriverLibs Include="STM32F10x_StdPeriph_Driver.$(LIB_EXT)" />
<RequiredProjects Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/Libraries.proj" />
</ItemGroup>
<ItemGroup>
    <IncludePaths Include="Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/Configure" />
  <IncludePaths Include="Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/inc" />
   <IncludePaths Include="Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/CMSIS/Core/CM3/" />  
  </ItemGroup>
 

 

 

    6.  当顺利完成如上步骤之后,就可以来实际试试这个STM32F10x_StdPeriph_Lib了。如果大家比较关心.Net Micro Framework Porting,可能对叶帆的这篇文章《【.Net Micro Framework PortingKit - 03】调试初步:点亮LED灯》(http://blog.csdn.net/yefanqiu/archive/2010/01/02/5121851.aspx)记忆犹新。只不过在那篇文章中是自己定义类型,然后再对寄存器进行操作。在这里我们简化操作,直接采用STM32F10x_StdPeriph_Lib定义的函数,来对LED进行操控。

 

    NativeSample.cpp的完整代码如下:

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Copyright (c) Microsoft Corporation. All rights reserved. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #include <tinyhal.h> #include <Tests.h> #include "nativesample.h" #include "stm32f10x_gpio.h" //--// HAL_DECLARE_NULL_HEAP(); void ApplicationEntryPoint() { //不要删除下面这个语句,否则会编译出错 UART usartTest ( COMTestPort, 9600, USART_PARITY_NONE, 8, USART_STOP_BITS_ONE, USART_FLOW_NONE ); //声明一个GPIO_InitTypeDef GPIO_InitTypeDef GPIO_InitStructure; //指定初始化的引脚位 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; //指定初始化GPIO的速度 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //设置GPIO的模式 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //利用GPIO_InitStructure来初始化GPIO GPIO_Init(GPIOF, &GPIO_InitStructure); BOOL bLight = TRUE; while(TRUE) { for(long i=0;i<1000000;i++); if(bLight == FALSE) { GPIO_SetBits(GPIOF, GPIO_Pin_6 | GPIO_Pin_7); } else { GPIO_ResetBits(GPIOF, GPIO_Pin_6 | GPIO_Pin_7); } bLight = !bLight; } }

 

    相对而言,是不是觉得简单很多呢?

你可能感兴趣的:(Microsoft,File,application,documentation,include,Constants)